4

我使用以下代码指示 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("\302\303  \302\"\210)\302\207" [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")

如何在外部应用程序中打开文件而不抛出错误?

4

2 回答 2

2

我的建议是与在外部应用程序中打开文件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

于 2014-08-04T18:04:05.033 回答
0

如果文件的路径在缓冲区中,我将其保存为M-w然后调用shell-command并调用xdg-open C-y. 这将抽出先前保存的文件的路径并使用关联的程序打开它。xdg-open负责找到正确的程序来打开该文件(屏幕截图)。

于 2021-04-21T08:15:28.060 回答