Using emacs local variables to specify a path to be used in a command
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:
Th开发者_运维技巧ese 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:
- Check whether a particular local variable exists (call it
pdf-copy-path
) - If this variable exists check whether it is a well formed (unix) directory path
- 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.
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 theassoc
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 withfile-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)))))
精彩评论