reddit / emacs

How to open a file with external program with `embark-act`?

stuudente · 2022-11-27 04:12
For reading pdf, epub, ps files, I prefer using the external program `zathura` more than emacs. Upon `find-file`, I know in principle I can apply a customized elisp function `#'open-with-zathura` on the selected file with `embark-act`. However, I do I add `#'open-with-zathura` to the list of actions of `embark-act`?
akirakom · 2022-11-27 05:14
I use `consult-file-externally`.
oantolin · 2022-11-27 09:57
Embark doesn't actually have a fixed "list of actions" you need to put your function on! Embark lets you run any interactive function at all as an action. So you can already use `open-with-zathura` as an Embark action without any configuration: you can run the command as you normally do without Embark, for example using `M-x open-with-zathura` or through whatever keybinding you have asigned to `open-with-zathura`. But if you want to call it with a single keystroke you need only bind it in `embark-file-map`, say, by evaluating `(define-key embark-file-map "z" #'open-with-zathura)`.
stuudente · 2022-11-27 13:44
That is very very cool! `embark-act` is more general than I thought. For those who are interested, this is what I use now. (defun async/open-with-zathura (files) " It can be used programatically (it \"~/a.pdf\" \"~/b.pdf\") interactively, or via #'embark-act. " (interactive "sOpen file with zathura: ") (when (stringp files) (setf files (list files))) (let ((buf (current-buffer)) (command `("zathura" ,@(mapcar (lambda (x) (format "%s" x)) files)))) (message (format "Running command: %S" command)) (make-process :name "dired/async/open-with-zathura" :command command :buffer (get-buffer "*Messages*") :sentinel (lambda (proc type) (declare (ignore (proc type))) (with-current-buffer buf (revert-buffer)))))) (define-key embark-file-map "z" #'async/open-with-zathura) (defun dired/async/open-with-zathura () "Open selected files with zathura." (interactive) (dired/funcall-on-marked-files #'async/open-with-zathura))