bling / fzf.el

A front-end for fzf
GNU General Public License v3.0
360 stars 50 forks source link

Windows support? #110

Open JohnC32 opened 1 year ago

JohnC32 commented 1 year ago

The fzf integration in Emacs is very nice, but doesn't work on Windows. I spent a little time looking at adding Windows support. The underlying term.el used by fzf.el leverage pty's and Windows doesn't have a pseudo terminal concept. Processes communicate via pipes, so the path that fzf.el took on Unix leveraging term.el took will never work on Windows.

I see a path to supporting fzf in Emacs on Windows and that's to open an external cmd window to run fzf there and get the result back to Emacs. The basics are:

(defvar fzf--selected-result-file)

(defun fzf--windows-cmd-sentinel (proc msg)
  (when (string-match-p "finished" msg)
    (with-temp-buffer
      (insert-file-contents-literally fzf--selected-result-file)
      (message "result: %S" (replace-regexp-in-string
                             "[\n\r]+$"
                             ""
                             (buffer-substring (point-min) (point-max)))))))

(defun fzf--windows-cmd ()
  (interactive)
  (setq fzf--selected-result-file (make-temp-file "fzf_" nil ".txt"))
  (let* ((proc
          (start-process
           "cmd" nil
           "cmd.exe" "/c" "start/wait"
           "cmd.exe" "/c"
           (concat
            "C:/path/to/fzf/windows-amd64/fzf.exe"
            " > " fzf--selected-result-file))))
    (set-process-sentinel proc #'fzf--windows-cmd-sentinel)
    (set-process-query-on-exit-flag proc nil)))

If you take this code and correct the path to fzf.exe for your environment, then M-x fzf-windows-cmd, you see in the *Messages* buffer the selected file.

The only part remaining is how to wire this into the existing fzf.el. I haven't spent enough time looking at fzf.el to do this. Maybe someone can help?

Thanks

JohnC32 commented 1 year ago

term.el which fzf.el uses only supports a subset of the 16-bit colors. I created a pull-request to add colors.

JohnC32 commented 1 year ago

I added windows support - see my pull request

domsch1988 commented 1 year ago

Is this working? I installed your fork with elpaca and i don't get an error anymore. Running any fzf command though flashes open a window and does nothing else. Am i doing something wrong here?

JohnC32 commented 1 year ago

Yes, it's working for us. What should happen is the command window appears and fzf runs in that (Windows doesn't have a notion of a terminal in the same sense as Unix), thus we pop open a cmd.exe window to do the fuzzy find there.

Can you tell me more about your environment. Did you set the fzf/executable? What does "which fzf" return?

If you'd like to help debug, set a breakpoint in fzf--start-windows. To do this, M-x find-function RET fzf--start-windows RET, then M-x edebug-defun. Next do a fuzzy find. There's a .bat file generated that is run from cmd.exe. After the .bat file is created, copy it aside. In a regular cmd window, run the bat file. Does that work?

thanks

JohnC32 commented 1 year ago

Also which fzf function where you running?

JohnC32 commented 1 year ago

I updated by fork, https://github.com/JohnC32/fzf.el/commit/847b72ef710949e1317831cae0a7d9de52329111, to increase debugging. After loading fzf.el, run

M-: (setq fzf-show-debug-messages t) RET
or
M-x eval-expression RET (setq fzf-show-debug-messages t) RET

You will then see messages in the *Messages* buffer as you run fzf commands.

Hopefully, this will help us track down the issue you saw.