Emacs: TeX master document support.

master
Pierre Neidhardt 2013-02-26 20:38:54 +01:00
parent d73db87d11
commit c5aecec97b
1 changed files with 78 additions and 42 deletions

View File

@ -152,6 +152,12 @@ If value is nil, the compiler will be tex-my-default-compiler for
TeX mode, and latex-my-default-compiler for LaTeX mode." TeX mode, and latex-my-default-compiler for LaTeX mode."
:safe 'stringp) :safe 'stringp)
(defcustom tex-my-masterfile nil
"[Local variable]
The file that should be compiled."
:safe 'stringp)
(defcustom tex-my-default-compiler "pdftex" (defcustom tex-my-default-compiler "pdftex"
"Default compiler for TeX mode. Used if tex-my-compiler is "Default compiler for TeX mode. Used if tex-my-compiler is
empty." empty."
@ -215,7 +221,6 @@ variable, e.g. on the first line:
" "
:safe 'stringp) :safe 'stringp)
;; TODO: master document support.
(defun tex-my-compile () (defun tex-my-compile ()
"Use compile to process your TeX-based document. Use a prefix "Use compile to process your TeX-based document. Use a prefix
argument to call the compiler along the '-shell-escape' argument to call the compiler along the '-shell-escape'
@ -232,12 +237,18 @@ WARNING: the -shell-escape option is a potential security issue."
;; compiler otherwise. ;; compiler otherwise.
(local-compiler (local-compiler
(if (not tex-my-compiler) (if (not tex-my-compiler)
(cond (cond
((string= "latex-mode" major-mode) latex-my-default-compiler) ((string= "latex-mode" major-mode) latex-my-default-compiler)
((string= "plain-tex-mode" major-mode) tex-my-default-compiler) ((string= "plain-tex-mode" major-mode) tex-my-default-compiler)
(t (message "Warning: unknown major mode. Trying pdftex.") "pdftex")) (t (message "Warning: unknown major mode. Trying pdftex.") "pdftex"))
tex-my-compiler)) tex-my-compiler))
;; Master file
(local-master
(if (not tex-my-masterfile)
buffer-file-name
tex-my-masterfile))
;; If tex-my-startcommands has some content, we make sure it is a string ;; If tex-my-startcommands has some content, we make sure it is a string
;; that loads the file. ;; that loads the file.
(local-start-cmd (local-start-cmd
@ -248,15 +259,19 @@ WARNING: the -shell-escape option is a potential security issue."
(local-shell-escape (local-shell-escape
(if (equal current-prefix-arg '(4)) "-shell-escape" ""))) (if (equal current-prefix-arg '(4)) "-shell-escape" "")))
(setq tex-my-compile-command (concat local-compiler " " local-shell-escape " " tex-my-compiler-options " " local-start-cmd " \"" buffer-file-name "\"")) (let (
;; (message tex-my-compile-command) ;; Debug only. ;; Final command
(save-buffer) (local-compile-command
(setq compilation-scroll-output t) (concat local-compiler " " local-shell-escape " " tex-my-compiler-options " " local-start-cmd " \"" local-master "\"")))
(compile tex-my-compile-command)
;; If no user interaction for 2 seconds, hide the compilation window. ;; (message tex-my-compile-command) ;; Debug only.
(sit-for 2) (save-buffer)
(delete-windows-on "*compilation*"))) (setq compilation-scroll-output t)
(compile local-compile-command)
;; If no user interaction for 2 seconds, hide the compilation window.
(sit-for 2)
(delete-windows-on "*compilation*"))))
(defcustom tex-my-extension-list '(".aux" ".glg" ".glo" ".gls" ".idx" ".ilg" ".ind" ".lof" ".log" ".nav" ".out" ".snm" ".synctex" ".synctex.gz" ".tns" ".toc" ".xdy") (defcustom tex-my-extension-list '(".aux" ".glg" ".glo" ".gls" ".idx" ".ilg" ".ind" ".lof" ".log" ".nav" ".out" ".snm" ".synctex" ".synctex.gz" ".tns" ".toc" ".xdy")
@ -268,11 +283,18 @@ WARNING: the -shell-escape option is a potential security issue."
"Remove all TeX temporary files. This command should be safe, "Remove all TeX temporary files. This command should be safe,
but there is no warranty." but there is no warranty."
(interactive) (interactive)
(setq file-noext (replace-regexp-in-string ".tex" "" (file-name-nondirectory buffer-file-name))) (let (
(shell-command ;; Master file.
(concat "rm -f \"" file-noext (local-master
(mapconcat 'identity tex-my-extension-list (concat "\" \"" file-noext)) (if (not tex-my-masterfile)
"\""))) buffer-file-name
tex-my-masterfile)))
(setq file-noext (replace-regexp-in-string ".tex" "" (file-name-nondirectory local-master)))
(shell-command
(concat "rm -f \"" file-noext
(mapconcat 'identity tex-my-extension-list (concat "\" \"" file-noext))
"\""))))
;; TODO: use LISP functions. ;; TODO: use LISP functions.
(defun tex-pdf-compress () (defun tex-pdf-compress ()
@ -280,34 +302,48 @@ but there is no warranty."
compression depends on the fonts used. Do not use this command if compression depends on the fonts used. Do not use this command if
your document embeds raster graphics." your document embeds raster graphics."
(interactive) (interactive)
(setq file-noext (replace-regexp-in-string ".tex" "" (file-name-nondirectory buffer-file-name))) (let (
(setq file (replace-regexp-in-string "tex" "pdf" (file-name-nondirectory buffer-file-name))) ;; Master file.
(shell-command (local-master
(concat "if [ -e " (if (not tex-my-masterfile)
file buffer-file-name
" ]; then gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=" tex-my-masterfile)))
file-noext
"-COMPRESSED.pdf " (setq file-noext (replace-regexp-in-string ".tex" "" (file-name-nondirectory local-master)))
file (setq file (replace-regexp-in-string "tex" "pdf" (file-name-nondirectory local-master)))
" && rm -rf " (shell-command
file (concat "if [ -e "
" && mv " file
file-noext " ]; then gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="
"-COMPRESSED.pdf " file-noext
file "-COMPRESSED.pdf "
" ; fi" file
))) " && rm -rf "
file
" && mv "
file-noext
"-COMPRESSED.pdf "
file
" ; fi"
))))
(defun tex-pdf-view () (defun tex-pdf-view ()
"Call a PDF viewer for current buffer file. File name should be "Call a PDF viewer for current buffer file. File name should be
properly escaped with double-quotes in case it has spaces." properly escaped with double-quotes in case it has spaces."
(interactive) (interactive)
(shell-command (let (
(concat tex-my-viewer ;; Master file.
" \"" (local-master
(replace-regexp-in-string "\.tex$" "\.pdf" (file-name-nondirectory buffer-file-name)) (if (not tex-my-masterfile)
"\" &" )) buffer-file-name
(delete-windows-on "*Async Shell Command*")) tex-my-masterfile)))
(shell-command
(concat tex-my-viewer
" \""
(replace-regexp-in-string "\.tex$" "\.pdf" (file-name-nondirectory local-master))
"\" &" ))
(delete-windows-on "*Async Shell Command*")))
(add-hook (add-hook
'tex-mode-hook 'tex-mode-hook