首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Emacs中,如何在外部程序中无错误地打开文件?

在Emacs中,如何在外部程序中无错误地打开文件?
EN

Stack Overflow用户
提问于 2014-08-04 07:14:38
回答 2查看 2.9K关注 0票数 4

我使用以下代码指示Emacs使用外部应用程序打开PDF文件:

代码语言:javascript
复制
(require 'openwith)
'(openwith-associations (quote (("\\.skim\\'" "open" (file)) ("\\.pdf\\'" "open" (file)))))
(openwith-mode t)

当我访问PDF文件时,它成功地在我的外部程序中打开了该文件,但它也给了我错误和回溯:

代码语言:javascript
复制
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")

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

EN

回答 2

Stack Overflow用户

发布于 2014-08-05 02:04:05

我的建议是结合使用start-processdired-mode在外部应用程序中打开文件。

Xah Lee编写了简短而有效的函数来处理在OS上设置的外部默认应用程序中打开文件:http://ergoemacs.org/emacs/emacs_dired_open_file_in_ext_apps.html

代码语言:javascript
复制
(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

票数 2
EN

Stack Overflow用户

发布于 2021-04-21 16:15:28

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25109968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档