在emacs中使用默认的Windows应用程序打开文件

我试图在Windows XP上调整emacs中的dired-find-file函数,以便当我从dired打开(说)一个pdf文件时,它启动一个Acrobat Reader的副本,并打开该文件,而不是打开它在emacs内。 但是我无法弄清楚shell-command/call-process使用什么变体。 以下是我到目前为止:

 (defadvice dired-find-file (around dired-find-file-external (filename &optional wildcards)) "Open non-text files with an appropriate external program." (if (string= ".pdf" (substring filename (- (length filename) 4))) ; obviously I'll replace this with something more general/robust (shell-command filename) ;; what should go here? (ad-do-it))) (ad-activate 'dired-find-file) 

我知道我可以硬编码,通过给它的.exe文件的位置来启动Acrobat Reader。 但我宁愿有一些东西需要更less的search,当默认应用程序移动/更改时不会中断。 我应该使用什么?

  1. 评估以下elisp
  2. 运行dired(Mx dired)
  3. 浏览到目录和文件
  4. 点击文件后,按F3将打开基于Windows扩展名的文件。

    (defun w32-browser (doc) (w32-shell-execute 1 doc))

    (eval-after-load "dired" '(define-key dired-mode-map [f3] (lambda () (interactive) (w32-browser (dired-replace-in-string "/" "\\" (dired-get-filename))))))

为了扩大“组织文件”提案:

 (defun my-dired-find-file (&optional prefix) (interactive "P") (if prefix (org-open-file (dired-get-file-for-visit) 'system) (dired-find-file))) (define-key dired-mode-map "\r" 'my-dired-find-file) 

会让你用`Cu RET'在外部打开一个文件。

http://lists.gnu.org/archive/html/bug-gnu-emacs/2012-11/msg01069.html找到

我发现这个了不起的网页通过谷歌,让我到一个使用RunDll的技术。 我把它放在这里,以防其他人好奇。

这里是关键的一段代码,它使用适当的应用程序打开filename

 (shell-command (concat "rundll32 shell32,ShellExec_RunDLL " (shell-quote-argument filename))) 

这是我的完整解决方案。 (请注意, dired-find-file只是一个不知道文件名的find-file封装,所以你必须建议find-file而不是dired-find-file ,如果你不想要find-file的行为,你可能需要重写dired-find-file或者写更复杂的建议。)

 (defun open-externally (filename) (shell-command (concat "rundll32 shell32,ShellExec_RunDLL " (shell-quote-argument filename)))) (defun is-file-type? (filename type) (string= type (substring filename (- (length filename) (length type))))) (defun should-open-externally? (filename) (let ((file-types '(".pdf" ".doc" ".xls"))) (member t (mapcar #'(lambda (type) (is-file-type? filename type)) file-types)))) (defadvice find-file (around find-file-external-file-advice (filename &optional wildcards)) "Open non-emacs files with an appropriate external program" (if (should-open-externally? filename) (open-externally filename) ad-do-it)) (ad-activate 'find-file) 

为此使用Dired +

  • C-RET使用其Windows文件关联应用程序打开当前行的文件。

  • M-RET将Windows资源管理器打开到文件或文件夹

  • ^ ,当在一个根目录(例如C:\ ),移动到所有的Windows驱动器(本地和远程)类似Dired的列表。

我有这个在我的.emacs中:

 (setq dired-guess-shell-alist-user (list (list "\\.*$" "cmd /k") )) 

这将使用cmd.exe打开文件,该文件将使用任何与文件扩展名关联的程序。 经过测试可以在Windows 8和GNU Emacs 24.2.1上运行。

汤姆史密斯的回答很好,但是你也可以用文件名作为参数运行程序“开始”。

 (shell-command (concat "start " (shell-quote-argument filename))) 

org-open-file是一个独立于系统的外部开启者。 有关如何进一步自定义的信息,请参阅org-file-apps

我会使用(w32-shell-execute "open" file-name)

事实上,在我的init文件中,我有:

 (defun open-externally (file-name) (interactive "fOpen externally: ") (let ((process-connection-type nil)) (start-process "open-externally" nil "xdg-open" file-name))) (when (eq window-system 'w32) (defun open-externally (file-name) (interactive "fOpen externally: ") (w32-shell-execute "open" file-name))) 

其中定义了一个命令(可以交互使用,并根据xdg-open使用默认应用程序打开一个文件,然后,如果我实际上在Windows上),则适当地重新定义该命令。