在Emacs中,如何在外部程序中打开文件而不出错?

时间:2014-08-03 23:14:38

标签: emacs open-with

我使用以下代码指示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")

如何在不丢失错误的情况下打开外部应用程序中的文件?

2 个答案:

答案 0 :(得分:1)

我的建议是将start-processdired-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

答案 1 :(得分:0)

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