【问题标题】:emacs/openwith how to pass arguments to program?emacs/openwith 如何将参数传递给程序?
【发布时间】:2011-07-31 04:25:39
【问题描述】:

我在 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 进程。

有人可以告诉我如何解决这个问题吗?

感谢您的帮助。

【问题讨论】:

    标签: emacs open-with


    【解决方案1】:

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

    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的定义和文档:

    功能:启动进程命名缓冲区或命名程序 &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 的 scratch 缓冲区中写入这些行,然后用 C-M-x 运行它们。

    (car args) 指的是你给openwith-association 的参数,注意'file in (nth 2 oa) 的出现是如何被那个替换的。我现在将其替换为“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))))
    

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

    【讨论】:

    • 请注意,Noufal Ibrahim 已编辑(正确,因为 openwith.el 中的字符串始终有两个反斜杠)您问题中的斜杠。我回答中的最后一个例子应该是正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    • 2023-03-03
    • 2019-06-05
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多