使用emacs局部变量指定要在命令中使用的路径

时间:2011-02-14 12:32:46

标签: emacs lisp local-variables

我使用emacs + AucTeX来编写LaTeX文件。 .tex文件的底部是一些局部变量:

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: "master-file"
%%% End: 

创建文件时,这些是由AucTeX添加的。

我想做的是写一个lisp函数,它将执行以下操作:

  1. 检查特定的局部变量是否存在(称之为pdf-copy-path
  2. 如果此变量存在,请检查它是否是格式正确的(unix)目录路径
  3. 如果是,请将输出pdf复制到该文件夹​​
  4. 输出pdf与当前.tex文件的名称相同,但扩展名为.pdf

    我的lisp-fu不满足于此,我不知道如何使用函数检查当前文件的局部变量。任何指针都赞赏。

    我为这个问题而不是SU选择了SO,因为它似乎是关于lisp编程的问题而不是其他任何问题。

1 个答案:

答案 0 :(得分:2)

我不知道你是否真的想要一个完整的解决方案,或者宁愿自己探索更多,但这里有一些应该有所帮助的事情。如果你被困住,请再次发帖:

  • 变量file-local-variables-alist包含您要查找的值。您希望使用其中一个assoc函数来获取pdf-copy-path的值。

  • 您可以使用file-exists-p函数检查文件是否存在,以及是否是file-attributes(第一个元素)的目录。

  • 然后使用copy-file

(FWIW,我认为输出PDF输出将匹配TeX-master而不是当前文件。)

[编辑2011-03-24 - 提供代码]

这应该适用于具有局部变量块的TeX文件,如

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: "master"
%%% pdf-copy-path: "/pdf/copy/path"
%%% End: 

请注意TeX-master值和pdf-copy-path值周围的双引号。 TeX-master也可以是t

(defun copy-master-pdf ()
  "Copies the TeX master pdf file into the path defined by the
file-local variable `pdf-copy-path', given that both exist."
  (interactive)
  ;; make sure we have local variables, and the right ones
  (when (and (boundp 'file-local-variables-alist)
             (assoc 'pdf-copy-path file-local-variables-alist)
             (assoc 'TeX-master file-local-variables-alist))
    (let* ((path (cdr (assoc 'pdf-copy-path file-local-variables-alist)))
           (master (cdr (assoc 'TeX-master file-local-variables-alist)))
           (pdf (cond ((stringp master)
                      ;; When master is a string, it should name another file.
                       (concat (file-name-sans-extension master) ".pdf"))
                      ((and master (buffer-file-name))
                      ;; When master is t, the current file is the master.
                       (concat (file-name-sans-extension buffer-file-name) ".pdf"))
                      (t ""))))
      (when (and (file-exists-p pdf)
                 (file-directory-p path))
        ;; The 1 tells copy-file to ask before clobbering
        (copy-file pdf path 1)))))