正确使用启动过程功能的方法

时间:2016-10-14 11:36:13

标签: emacs

我有一个小的ELisp包,可以为Emacs添加一个外部工具菜单。它适用于Microsoft Windows,但我很难将其用于其他操作系统。在Microsoft Windows上,我使用w32-shell-execute函数。在其他操作系统上,我使用启动过程功能。

我的外部工具 - 执行功能如下。

(defvar external-tools--exec-count 0)
(defun external-tools--exec (command &rest args)
  (if args
      (message "(external-tools--exec %s %s) called" command (mapconcat 'identity args " "))
    (message "(external-tools--exec %s) called" command)
    )
  (setq external-tools--exec-count (+ external-tools--exec-count 1))
  (cond
   ((fboundp 'w32-shell-execute)
    (if args
        (w32-shell-execute "open" command (mapconcat 'identity args " "))
      (w32-shell-execute "open" command)
      )
    )
    (t
     (let ((external-tools--exec-process-name (format "external-tools--exec-%i" external-tools--exec-count)))
       (if args
           (apply 'start-process external-tools--exec-process-name nil command args)
         (start-process external-tools--exec-process-name nil command)
         )
       )
     )
    )
  )

这是我如何使用它的一个例子。

(defun external-tools--explore-here ()
  "Opens Windows Explorer in the current directory."
  (interactive)
  (let ((dir (external-tools--get-default-directory)))
    (when (fboundp 'w32-shell-execute)
      (w32-shell-execute "explore" (format "\"%s\"" dir))
      )
    (when (and (not (fboundp 'w32-shell-execute)) (executable-find "nautilus"))
      (external-tools--exec (executable-find "nautilus") "-w" (format "\"%s\"" dir))
      )
    )
  )

如果args为nil,则external-tools-exec函数有效,但如果指定了参数则不起作用。

我很感激有关如何修复external-tools - exec函数的任何建议。

编辑:我修改了这个函数,使得它不像Stefan推荐的那样使用convert-standard-filename函数,但是该函数仍然不起作用。当我在GNU / Linux上使用external-tools - explore-here函数时,我收到以下错误。

Unable to find the requested file. Please check the spelling and try again.

Unhandled error message: Error when getting information for file '/home/bkey/src/SullivanAndKey.com/SnK/Emacs/Home/.emacs.d/"/home/bkey/src/SullivanAndKey.com/SnK/Emacs/Home/.emacs.d/"': No such file or directory

1 个答案:

答案 0 :(得分:1)

我明白了。该bug不在external-tools-exec函数中。这是在调用函数,外部工具 - 探索 - 这里。我将目录括在引号中,认为有必要处理目录路径中可能有空格的可能性。

事实证明这是不必要的。

新功能如下。

(defun external-tools--explore-here ()
  "Opens Windows Explorer or Nautilus in the current directory."
  (interactive)
  (let ((dir (external-tools--get-default-directory)))
    (when (fboundp 'w32-shell-execute)
      (w32-shell-execute "explore" dir)
      )
    (when (and (not (fboundp 'w32-shell-execute)) (executable-find "nautilus"))
      (external-tools--exec (executable-find "nautilus") "-w" dir)
      )
    )
  )