It is more reliable to add advice to
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.
(advice-add #'deer :after #'+enable-helm-find-files)
(advice-add #'ranger :after #'+enable-helm-find-files))
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 ()
(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.
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.
(defvar +after-switch-to-buffer-hook nil
"Hook run after switched to buffer.")
(defun +run-switch-buffer-hooks (orig-fn buffer-or-name &rest args)
(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)
(defun +run-prev-or-next-buffer-hooks (orig-fn &rest args)
(apply orig-fn args)
(let ((+inhibit-switch-buffer-hooks t))
(when-let (buffer (apply orig-fn args))
inhibit switch-buffer hooks
(defvar +inhibit-switch-buffer-hooks nil
"Letvar for inhibiting `+after-switch-buffer-hook'. Do not set this directly.")
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 ()
(setq +enable-switch-buffer-hooks (not +enable-switch-buffer-hooks))
(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)))
In case something goes wrong, you can disable everything with this.
(defun +disable-helm-find-files-all ()
"Disable the whole thing."
(setq +enable-switch-buffer-hooks nil)
(remove-hook 'ranger-mode-hook #'+call-helm-find-files))
This list is for the buffers you disable with
(defvar +inhibited-find-file-buffers nil
"List of ranger buffers that shouldn’t do ‘helm-find-files’ on switch.")
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’."
(push (current-buffer) +inhibited-find-file-buffers))
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
(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)))
(add-hook '+after-switch-to-buffer-hook #'+enable-helm-find-files)