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

发布于 2024-10-17 19:52:36 字数 569 浏览 2 评论 0原文

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

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

这些变量是在我创建文件时由 AucTeX 添加的。

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

  1. 检查特定局部变量是否存在(将其称为 pdf-copy-path
  2. 如果此变量存在,则检查它是否是格式正确的 (unix) 目录路径
  3. 如果是,请将输出 pdf 复制到该文件夹

​​ 输出 pdf 与当前 .tex 文件同名,但使用 .pdf 扩展。

我的 lisp-fu 无法做到这一点,而且我不知道如何让函数检查当前文件中的局部变量。任何指示表示赞赏。

我在这个问题上选择了 SO 而不是 SU,因为这似乎是一个关于 lisp 编程的问题。

I use emacs+AucTeX to write LaTeX files. At the bottom of the .tex file are some local variables:

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

These are added by AucTeX when I create the file.

What I'd like to do is write a lisp function that will do the following:

  1. Check whether a particular local variable exists (call it pdf-copy-path)
  2. If this variable exists check whether it is a well formed (unix) directory path
  3. If it is, copy the output pdf to that folder

The output pdf has the same name as the current .tex file, but with the .pdf extension.

My lisp-fu isn't up to this, and I don't know how to have a function check the current file for a local variable. Any pointers appreciated.

I chose SO for this question rather than SU, because it seems to be a question about lisp programming more than anything else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月如刀 2024-10-24 19:52:36

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

  • 变量 file-local-variables-alist 保存您要查找的值。您需要使用 assoc 函数之一从 alist 中获取 pdf-copy-path 的值。

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

  • 然后使用复制文件

(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)))))

I don't know if you really want a complete solution, or would rather explore more yourself, but here are a few things that should help. Post again if you're stuck:

  • The variable file-local-variables-alist holds the values you're looking for. You'd want to use one of the assoc functions to get the value of pdf-copy-path out of the alist.

  • You can check whether a file exists with the file-exists-p function, and if it's a directory with file-attributes (the first element).

  • Then use copy-file.

(FWIW, I think the output PDF output will match TeX-master and not the current file.)

[Edited 2011-03-24 - provide code]

this should work on TeX files with a local variables block like

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

Note the double quotes around the TeX-master value and the pdf-copy-path value. TeX-master can also be 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)))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文