bling / fzf.el

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

Default directory #75

Open Twix53791 opened 1 year ago

Twix53791 commented 1 year ago

Hello,

I am trying to define a default directory where - always ! - execute fzf. I tried:

Note than I tried with another command, without specifying the directory :init (setenv "FZF_DEFAULT_COMMAND" "fd --type file -c always") and everything is working fine. The problem is not than the command is not recognized, but to open a file when a directory is specified...

Thanks to any clues... PS: is someone knows how I could configure emacs/fzf.el to get an error log of this error...

Twix53791 commented 1 year ago

EDIT: I fact, I found a way... I modified fzf.el file, after rename fzf.elc.save. I commented the two built-in function fzf and fzf-directory, to replace them by:

(defcustom fzf/base-directory nil
  "The path of the default start directory for fzf-directory (custom)."
  :type 'string
  :group 'fzf)

(defcustom fzf/main-directory nil
  "The path of the default start directory for fzf (custom)."
  :type 'string
  :group 'fzf)

(defun fzf ()
  "Starts a fzf session."
  (interactive)
  (let ((d fzf/main-directory))
    (fzf/start d
               (lambda (x)
                 (let ((f (expand-file-name x d)))
                   (when (file-exists-p f)
                     (find-file f)))))
  )
)

(defun fzf-directory ()
  (interactive)
  (let ((d fzf/base-directory))
    (fzf-with-command "fd -t d" #'fzf-from-dir d)
  ))

(defun fzf-from-dir (target)
  "Starts a fzf session."
  (interactive)
  (let ((d target))
    (fzf/start d
               (lambda (x)
                 (let ((f (expand-file-name x d)))
                   (when (file-exists-p f)
                     (find-file f)))))
  )
)

Now, fzf-directory opens a fzf menu showing all the directories into base-directory. We can select one, then another fzf menu is showing, to select a file to open. Fzf start always from the same main-directory.

pierre-rouleau commented 1 year ago

@Twix53791, @bling, this functionality is available as fzf-directory in the main branch.

@Twix53791 , you may want to confirm and close the issue. Thanks.

Twix53791 commented 1 year ago

Even better with the preview function! Thanks

Just a question, to help me save time... As i am not still used to emacs, I did'nt find yet a way to configure a keybindings running fzf-directory function with the optionnal prefix "with-preview", how can I do that?

pierre-rouleau commented 1 year ago

First you need to understand how to type an argument key. There are several ways and it depends on how the interactive function is written. For the ones I wrote, you can use the universal argument key C-u , which is: Hold the Control key and press the u key.

For example, if you want to invoke the fzf-directory command with M-x and you and to activate the preview, type: C-u M-x fzf-directory RET

If you want to be able to use a key binding that will always invoke the preview, then you will have to write some Emacs Lisp code to create an interactive function that calls fzf and pass a non-nil argument. Then you would bind that new function to a key sequence. The new functions could be:

(defun fzf-directory-preview ()
  "Invoke `fzf-directory' with preview."
  (interactive)
  (fzf-directory t))

You could then invoke it with M-x fzf-directory-preview or create a key binding for it.

Now, if you want to create a key binding that will run fzf-directory, then you have to decide whether the key binding will be available from all major mode, a global key binding, or whether the key binding will be available only for a specific major-mode. In the case of fzf commands you will most probably want to make the key sequence available globally. You would use the global-set-key for that. For example:

(global-set-key (kbd "C-c z") 'fzf-directory)
(global-set-key (kbd "C-c p") 'fzf-directory-preview)

With the above you can invoke fzf-directory with preview in the following ways:

There are other types argument keys you can type. You can use numerical argument using the meta key followed by a digit key. Or a - with or without a digit key.

For more info see:

I also wrote PEL which is a way for me to carry my environment and publish it at the same time. I wrote it to learn and remember Emacs. It has a lot of key binding examples inside the pel_keys.el. I did add key bindings for the fzf commands in there and I have a large amount of PDF-based documentation with lots of hyperlinks. See the top level PDF using a browser that can render PDF (instead of downloading to save). PEL is mostly controlled by customization. It will not appeal to ones taht want to write Emacs Lisp code, but can be used to learn.