tl;dr Which keymap is active in helm-recentf
? Or is there a better way to accomplish my goal?
I want to use emacs as my file browser. When I select a file with helm-find-files
or helm-recentf
, I want to check whether the extension is in a whitelist (eg. "pdf", "png", "docx") and if it is, open the file in the appropriate external program. If it's not, the file should be visited normally in emacs.
Solutions I've rejected:
Use
C-c C-x
in helm to open candidate externally. I don't want to press a separate keybinding or to be prompted for a program name, I just want to pressreturn
as usual and have helm figure out the right thing to do.Use the
openwith
package. It breaks things like mu4e attachments and images embedded in org buffers. I only ever want files I explicitly open through helm to be opened externally.
What I've tried so far:
I remapped RET
to a custom function that does what I want. It's probably a horrible hack, but it works for helm-find-files
!
(define-key helm-find-files-map (kbd "RET") 'my-helm-ff-run-open-in-emacs-or-externally)
(defun my-helm-ff-run-open-in-emacs-or-externally ()
(interactive)
(with-helm-alive-p
(helm-exit-and-execute-action 'my-helm-ff-open-in-emacs-or-externally)
)
)
(defun my-helm-ff-open-in-emacs-or-externally (file)
;; based on helm-open-file-externally,
;; Copyright (C) 2012 ~ 2016 Thierry Volpiatto <thierry.volpiatto@gmail.com>
"Open FILE either with emacs or with an external program.
Try to guess which external program to use with `helm-get-default-program-for-file'.
If not found, open file in emacs."
(if (bufferp file)
(switch-to-buffer file)
(let* ((file-name (expand-file-name file))
(collection (helm-external-commands-list-1 'sort))
(default-program (helm-get-default-program-for-file file-name)))
(if (not default-program)
(find-file file) ;; open in emacs
(helm-run-or-raise default-program file) ;; open in external program
))))
The only problem is, I can't define this mapping for helm-recentf
. I assumed that the helm-buffer-map
would be active in helm-recentf
, but it's not:
(define-key helm-buffer-map (kbd "RET") '(lambda () (interactive) (message "define-key worked!")))
The message prints when I select a buffer in helm-mini
, but not when I select a file in helm-recentf
.
Questions
So, which keymap is active in helm-recentf
?
Or, am I doing this all wrong, and is there a much better to intelligently open some files with an external program?