我使用以下代码指示Emacs使用外部应用程序打开PDF文件:
(require 'openwith)
'(openwith-associations (quote (("\.skim\'" "open" (file)) ("\.pdf\'" "open" (file)))))
(openwith-mode t)
当我访问一个PDF文件时,它在我的外部程序中成功打开了该文件,但它也给了我错误和回溯:
Debugger entered--Lisp error: (error "Opened Foundation - Isaac Asimov.pdf in external program")
signal(error ("Opened Foundation - Isaac Asimov.pdf in external program"))
error("Opened %s in external program" "Foundation - Isaac Asimov.pdf")
openwith-file-handler(insert-file-contents "/Users/jay/iBooks/Books/Foundation - Isaac Asimov.pdf" t nil nil nil)
insert-file-contents("~/iBooks/Books/Foundation - Isaac Asimov.pdf" t)
byte-code("302303 302"210)302207" [inhibit-read-only filename t insert-file-contents] 3)
find-file-noselect-1(#<killed buffer> "~/iBooks/Books/Foundation - Isaac Asimov.pdf" nil nil "~/Library/Containers/com.apple.BKAgentService/Data/Documents/iBooks/Books/Foundation - Isaac Asimov.pdf" (21490564 16777218))
find-file-noselect("/Users/jay/iBooks/Books/Foundation - Isaac Asimov.pdf" nil nil nil)
find-file("/Users/jay/iBooks/Books/Foundation - Isaac Asimov.pdf")
mapc(find-file ("/Users/jay/iBooks/Books/Foundation - Isaac Asimov.pdf"))
helm-find-many-files("/Users/jay/iBooks/Books/Foundation - Isaac Asimov.pdf")
apply(helm-find-many-files "/Users/jay/iBooks/Books/Foundation - Isaac Asimov.pdf")
如何在外部应用程序中打开文件而不抛出错误?
我的建议是在外部应用程序中使用start-process
和dired-mode
来打开文件。
Xah Lee写了一个简短而有效的函数来处理在操作系统上设置的外部默认应用程序中打开文件:http://ergoemacs.org/emacs/emacs_dired_open_file_in_ext_apps.html
(defun xah-open-in-external-app (&optional file)
"Open the current file or dired marked files in external app.
The app is chosen from your OS's preference."
(interactive)
(let ( doIt
(myFileList
(cond
((string-equal major-mode "dired-mode") (dired-get-marked-files))
((not file) (list (buffer-file-name)))
(file (list file)))))
(setq doIt (if (<= (length myFileList) 5)
t
(y-or-n-p "Open more than 5 files? ") ) )
(when doIt
(cond
((string-equal system-type "windows-nt")
(mapc (lambda (fPath) (w32-shell-execute "open" (replace-regexp-in-string "/" "\" fPath t t)) ) myFileList))
((string-equal system-type "darwin")
(mapc (lambda (fPath) (shell-command (format "open "%s"" fPath)) ) myFileList) )
((string-equal system-type "gnu/linux")
(mapc (lambda (fPath) (let ((process-connection-type nil)) (start-process "" nil "xdg-open" fPath)) ) myFileList) ) ) ) ) )
我使用类似的东西(可以在下面的Github链接中查看),但它不像Xah Lee编写的函数那样直接:https://github.com/lawlist/dired-read-file-name/blob/master/dired-read-file-name.el
在拨号缓冲区中,browse-url-of-dired-file
("要求WWW浏览器显示这一行命名的文件"-我默认绑定W
)在Excel中打开.csv
文件&MacDown中的.md
文件(这两种文件类型都是操作系统默认的)。
这对我来说已经足够好了,我可能会尝试找出如何为当前的缓冲区文件做这件事。
(macOS Monterey 12.6.2, Emacs 28.2)
如果文件的路径在缓冲区中,我将其保存为M-w
,然后调用shell-command
并调用xdg-open C-y
。这将把路径拉到先前保存的文件,并使用相关程序打开它。xdg-open
负责找到正确的程序来打开该文件(截图)。