EDIT START
It is more reliable to add advice to ranger
and deer
to try to ensure that helm-find-files
is always enabled when ranger and dired are called (as opposed to adding a hook to ranger-mode-hook
which is only called once the buffer is created). This should fix the problem of helm-find-files
sometimes not being called immediately.
(with-eval-after-load 'ranger
(advice-add #'deer :after #'+enable-helm-find-files)
(advice-add #'ranger :after #'+enable-helm-find-files))
EDIT END
activate from newly created ranger buffers
helm-find-files
might be what you're looking for. You can add it as a hook to ranger-mode-hook
. I recommend naming it as I did so (1) you can easily remove it and (2) the hooks contents are more readable.
(defun +call-helm-find-files ()
(interactive)
(call-interactively #'helm-find-files))
(add-hook 'ranger-mode-hook #'+call-helm-find-files)
system to switch buffers
Adding a hook to ranger-mode-hook
does not answer your original question which was to enable fuzzy searching whenever you switch to a ranger buffer. Adding a hook will only work when ranger-mode
is enabled. In practice, this means when you call ranger mode (M-x ranger
or M-x deer
) and ranger creates a brand new ranger buffer. However if you're switching from a buffer to an existing ranger buffer ranger-mode-hook
will not be run and therefore helm-find-files
will not be called.
To call helm-find-files
upon switching to a ranger buffer and to do it in a thorough, not as hacky way, I would recommend adapting some of the code from doom-emacs. Currently, there is no hook that runs after switching a buffer. This code sets up such a hook. I adapted it here.
switch hook
(defvar +after-switch-to-buffer-hook nil
"Hook run after switched to buffer.")
advice to switch-to-buffer
(defun +run-switch-buffer-hooks (orig-fn buffer-or-name &rest args)
"Run ‘+after-switch-buffer-hook’."
(if (or +inhibit-switch-buffer-hooks
(eq (current-buffer) (get-buffer buffer-or-name))
(and (eq orig-fn #'switch-to-buffer) (car args)))
(apply orig-fn buffer-or-name args)
(let ((+inhibit-switch-buffer-hooks t))
(when-let (buffer (apply orig-fn buffer-or-name args))
(with-current-buffer (if (windowp buffer)
(window-buffer buffer)
buffer)
(run-hooks '+after-switch-to-buffer-hook))
buffer))))
advice to previous-buffer
and next-buffer
(defun +run-prev-or-next-buffer-hooks (orig-fn &rest args)
(if +inhibit-switch-buffer-hooks
(apply orig-fn args)
(let ((+inhibit-switch-buffer-hooks t))
(when-let (buffer (apply orig-fn args))
(with-current-buffer buffer
(run-hooks '+after-switch-to-buffer-hook))
buffer))))
inhibit switch-buffer hooks
(defvar +inhibit-switch-buffer-hooks nil
"Letvar for inhibiting `+after-switch-buffer-hook'. Do not set this directly.")
toggle
I checked this code and it worked for me. But my checking was not exhaustive so I wrote this function so you can enable and disable the switch buffer hook.
(defvar +enable-switch-buffer-hooks nil
"Whether to enable switch buffer hooks.")
(defun +toggle/switch-window-advice ()
(interactive)
(setq +enable-switch-buffer-hooks (not +enable-switch-buffer-hooks))
(if +enable-switch-buffer-hooks
(progn
(message "disabled switch buffer hooks.")
(advice-remove #'switch-to-buffer #'+run-switch-buffer-hooks)
(advice-remove #'display-buffer #'+run-switch-buffer-hooks)
(advice-remove #'switch-to-next-buffer #'+run-prev-or-next-buffer-hooks)
(advice-remove #'switch-to-prev-buffer #'+run-prev-or-next-buffer-hooks))
(message "enabled switch buffer hooks")
(advice-add #'switch-to-buffer :around #'+run-switch-buffer-hooks)
(advice-add #'display-buffer :around #'+run-switch-buffer-hooks)
(advice-add #'switch-to-next-buffer :around #'+run-prev-or-next-buffer-hooks)
(advice-add #'switch-to-prev-buffer :around #'+run-prev-or-next-buffer-hooks)))
disable all
In case something goes wrong, you can disable everything with this.
(defun +disable-helm-find-files-all ()
"Disable the whole thing."
(interactive)
(setq +enable-switch-buffer-hooks nil)
(+toggle/switch-window-advice)
(remove-hook 'ranger-mode-hook #'+call-helm-find-files))
inhibited files
This list is for the buffers you disable with +toggle/find-file-inhibited
.
(defvar +inhibited-find-file-buffers nil
"List of ranger buffers that shouldn’t do ‘helm-find-files’ on switch.")
toggle inhibited
This is the toggling functionality you requested. You can call this while on a ranger buffer and that buffer will not trigger helm-find-files
when you switch to it.
(defun +toggle/find-file-inhibited ()
"Make current buffer not call ‘helm-find-files’."
(interactive)
(push (current-buffer) +inhibited-find-file-buffers))
activate helm-find-files
when entering buffer
This is the hook to actually call helm-find-files
. It checks to see if the current buffer is a ranger buffer and whether its not a member of those in +inhibited-find-file-buffers
.
(defun +enable-helm-find-files ()
"Enable ‘helm-find-files’ in ranger buffers."
(when (and (eq major-mode 'ranger-mode)
(not (member (current-buffer) +inhibited-find-file-buffers)))
(call-interactively #'helm-find-files)))
(add-hook '+after-switch-to-buffer-hook #'+enable-helm-find-files)