- Install this
emacs-lisp
code
(defvar related-extns-alist
'(("org" "pdf" "html" "odt") ; When I open a `org' file
; with `C-u M-x browse-file',
; I really want to open any of
; the related `pdf', `html',
; or `odt' files
("tex" "pdf")) ; When I open a `tex' file
; with `C-u M-x browse-file',
; I really want to open any of
; `pdf' file
)
(defun browse-file (file-name &optional related-file)
"Open FILE-NAME using the default Desktop Application.
With a prefix argument, open a related file. See
`related-extns-alist' for more information."
(interactive
(list
(read-file-name "File name: "
nil ; dir
nil ; default-file-name
t ; mustmatch
(buffer-file-name))
current-prefix-arg))
(if-let* ((userp (called-interactively-p 'interactive))
(browse-related-file current-prefix-arg)
(related-extns
(assoc-default
(file-name-extension file-name) related-extns-alist))
(other-readable-extns
(seq-filter
(lambda (extn)
(let ((other-file (file-name-with-extension
file-name extn)))
(when (file-readable-p other-file)
other-file)))
related-extns))
(actual-extension
(cond
((= 1 (length other-readable-extns))
(car other-readable-extns))
(t (completing-read "Extn: " other-readable-extns)))))
(browse-url-default-browser (browse-url-file-url
(file-name-with-extension
file-name actual-extension)))
(browse-url-default-browser (browse-url-file-url file-name))
;; (find-file file-name)
))
(global-set-key (kbd "C-x C-S-f") #'browse-file) ; This is C-x C-F
- When you are in a
.tex
file, do C-u C-x C-S-F filename.tex
or C-u M-x browse-file RET filename.tex
. It will open the pdf
file in the app your choice.
If you visiting a .org
file, you will be offered a choice of which of the related files .html
, .pdf
, or .odt
you can use.
Since you are on a Mac
, browse-url-default-browser
will internally use open
. (I am on Linux
, and it uses xdg-open
)