2

我在 emacs 中使用 openwith 包。我想用 xfig 打开带有一些附加选项的 .fig 文件,例如:

xfig -specialtext -latexfont -startlatexFont default file.fig

openwith 在我不需要传递其他选项的其他文件关联中为我工作。我在我的 .emacs 文件中尝试了以下内容

(setq
 openwith-associations 
 '(("\\.fig\\'" "xfig" (file))))

哪个有效,但是

(setq
 openwith-associations 
 '(("\\.fig\\'" "xfig -specialtext -latexfont -startlatexFont default" (file))))

也不(error: Wrong type argument: arrayp, nil)

(setq
 openwith-associations 
 '(("\\.fig\\'" "xfig" (" -specialtext -latexfont -startlatexFont default " file))))

不起作用,尽管在这里我没有收到任何错误。它说“在外部程序中打开了 file.fig”,但没有任何反应。在这种情况下,我注意到有一个使用所有这些选项运行的 xfig 进程。

有人可以让我知道如何解决这个问题吗?

谢谢您的帮助。

4

1 回答 1

2

我不知道这是如何工作的,所以我只是记录了如何通过阅读代码来计算它:

openwith.el 中的重要代码是对 start-process 的调用:

(dolist (oa openwith-associations)
  (let (match)
    (save-match-data
      (setq match (string-match (car oa) (car args))))
    (when match
      (let ((params (mapcar (lambda (x)
                  (if (eq x 'file)
                  (car args)
                  (format "%s" x))) (nth 2 oa))))
    (apply #'start-process "openwith-process" nil
           (cadr oa) params))
      (kill-buffer nil)
      (throw 'openwith-done t))))

在您的情况下 oa 将具有以下结构,并且 cadr 是“xfig”:

(cadr '("\.fig\'" "xfig" (file))) ;; expands to => xfig

这是 start-process 的定义和文档:

功能:启动-进程名buffer-or-name程序&rest args http://www.gnu.org/software/emacs/elisp/html_node/Asynchronous-Processes.html

 args, are strings that specify command line arguments for the program.

一个例子:

(start-process "my-process" "foo" "ls" "-l" "/user/lewis/bin")

现在我们需要弄清楚 params 是如何构造的。在您的示例中,mapcar 的参数是:

(nth 2 '("\.fig\'" "xfig" (file))) ;=> (file)

顺便说一句,您可以在 emacs 的暂存缓冲区中写入这些行并使用 CMx 运行它们。

(car args) 是指您提供给 openwith-association 的参数,请注意 (nth 2 oa) 中的 'file 的出现是如何被它替换的。我现在将其替换为“here.txt”:

(mapcar (lambda (x)
      (if (eq x 'file)
          "here.txt"
          (format "%s" x))) (nth 2 '("\.fig\'" "xfig" (file)))) ;=> ("here.txt")

好的,现在我们看看应该如何构造参数:

(mapcar (lambda (x)
      (if (eq x 'file)
          "here.txt"
          (format "%s" x))) 
    (nth 2 '("\.fig\'" "xfig" 
         ("-specialtext" "-latexfont" "-startlatexFont" "default" file))))
; => ("-specialtext" "-latexfont" "-startlatexFont" "default" "here.txt")

尝试这个:

(setq openwith-associations 
 '(("\\.fig\\'" "xfig" ("-specialtext" "-latexfont" "-startlatexFont" "default" file))))

您必须在参数列表中将每个单词作为单个字符串提供。

于 2011-07-30T19:57:08.003 回答