branch: externals/auctex commit 6b3cbea3d456fb556f90413fa26fa1e34f5cb2d4 Author: Mosè Giordano <m...@gnu.org> Commit: Mosè Giordano <m...@gnu.org>
Suggest to run Index when appropriate. * tex-buf.el (LaTeX-idx-md5-alist, LaTeX-idx-changed-alist): New variables. (TeX-command-sequence): Call file function without optional arguments. (TeX-command-default): Suggest "Index" when appropriate. (TeX-run-TeX): Store value of `LaTeX-idx-md5-alist'. (TeX-run-index): New function. (TeX-LaTeX-sentinel): Set `LaTeX-idx-changed-alist'. (TeX-index-sentinel): New function. * tex.el (TeX-command-list): Mention "makeindex" in "Index" help. Suggested by Arash Esbati. * doc/changes.texi: Mention ability to suggest makeindex when needed. * doc/todo.texi (Wishlist): Remove reference to makeindex. --- ChangeLog | 18 +++++++ doc/changes.texi | 5 ++- doc/todo.texi | 3 - tex-buf.el | 149 +++++++++++++++++++++++++++++++++++++++++++++--------- tex.el | 3 +- 5 files changed, 148 insertions(+), 30 deletions(-) diff --git a/ChangeLog b/ChangeLog index c412871..e1fa98a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,23 @@ 2015-09-07 Mos� Giordano <m...@gnu.org> + * tex-buf.el (LaTeX-idx-md5-alist, LaTeX-idx-changed-alist): New + variables. + (TeX-command-sequence): Call file function without optional + arguments. + (TeX-command-default): Suggest "Index" when appropriate. + (TeX-run-TeX): Store value of `LaTeX-idx-md5-alist'. + (TeX-run-index): New function. + (TeX-LaTeX-sentinel): Set `LaTeX-idx-changed-alist'. + (TeX-index-sentinel): New function. + + * tex.el (TeX-command-list): Mention "makeindex" in "Index" help. + Suggested by Arash Esbati. + + * doc/changes.texi: Mention ability to suggest makeindex when + needed. + + * doc/todo.texi (Wishlist): Remove reference to makeindex. + * doc/tex-ref.tex: Update copyright years. Mention C-c C-a key binding. diff --git a/doc/changes.texi b/doc/changes.texi index 0a90934..14fc510 100644 --- a/doc/changes.texi +++ b/doc/changes.texi @@ -22,7 +22,7 @@ current section (or part, chapter, subsection, etc). See @code{LaTeX-command-section} and @code{LaTeX-command-section-change-level}. @ifclear rawfile -Also see the documentation at @xref{Starting a Command}. +@xref{Starting a Command}, for details. @end ifclear @item @@ -42,6 +42,9 @@ style for error messages. Indent @samp{\[...\]} math mode as a regular environment by default. @item +Now @AUCTeX{} suggests to run @command{makeindex} when appropriate. + +@item @code{TeX-view-program-list} can contain, as third optional element of each item, the name of the executable(s) needed to open the viewer. diff --git a/doc/todo.texi b/doc/todo.texi index 59b0f29..8025351 100644 --- a/doc/todo.texi +++ b/doc/todo.texi @@ -138,9 +138,6 @@ This default value for TeX-master could then be controled with mapping based on the extension. @item -Suggest @samp{makeindex} when appropriate. - -@item Use index files (when available) to speed up @kbd{C-c C-m include @key{RET}}. diff --git a/tex-buf.el b/tex-buf.el index c13a87e..07e54ef 100644 --- a/tex-buf.el +++ b/tex-buf.el @@ -336,6 +336,17 @@ This works only with TeX commands and if the (defconst TeX-error-overview-buffer-name "*TeX errors*" "Name of the buffer in which to show error list.") +(defvar LaTeX-idx-md5-alist nil + "Alist of MD5 hashes of idx file. + +Car is the idx file, cdr is its md5 hash.") + +(defvar LaTeX-idx-changed-alist nil + "Whether the idx files changed. + +Car is the idx file, cdr is whether idx changed after LaTeX +run.") + (defcustom TeX-check-engine t "Whether AUCTeX should check the correct engine has been set before running LaTeX commands." :group 'TeX-command @@ -642,7 +653,10 @@ omitted) and `TeX-region-file'." TeX-command-sequence-command command)) (t (setq cmd (TeX-command-default - (funcall TeX-command-sequence-file-function nil t)) + ;; File function should be called with nil `nondirectory' + ;; argument, otherwise `TeX-command-sequence' won't work in + ;; included files not placed in `TeX-master-directory'. + (funcall TeX-command-sequence-file-function)) TeX-command-sequence-command t))) (TeX-command cmd TeX-command-sequence-file-function 0) (when reset @@ -685,28 +699,44 @@ omitted) and `TeX-region-file'." (defun TeX-command-default (name) "Guess the next command to be run on NAME." - (cond ((if (string-equal name TeX-region) - (TeX-check-files (concat name "." (TeX-output-extension)) - (list name) - TeX-file-extensions) - (TeX-save-document (TeX-master-file))) - TeX-command-default) - ((and (memq major-mode '(doctex-mode latex-mode)) - ;; Want to know if bib file is newer than .bbl - ;; We don't care whether the bib files are open in emacs - (TeX-check-files (concat name ".bbl") - (mapcar 'car - (LaTeX-bibliography-list)) - (append BibTeX-file-extensions - TeX-Biber-file-extensions))) - ;; We should check for bst files here as well. - (if LaTeX-using-Biber TeX-command-Biber TeX-command-BibTeX)) - ((TeX-process-get-variable name - 'TeX-command-next - (if (and TeX-PDF-via-dvips-ps2pdf TeX-PDF-mode) - "Dvips" - TeX-command-Show))) - (TeX-command-Show))) + (let ((command-next nil)) + (cond ((if (string-equal name TeX-region) + (TeX-check-files (concat name "." (TeX-output-extension)) + (list name) + TeX-file-extensions) + (TeX-save-document (TeX-master-file))) + TeX-command-default) + ((and (memq major-mode '(doctex-mode latex-mode)) + ;; Want to know if bib file is newer than .bbl + ;; We don't care whether the bib files are open in emacs + (TeX-check-files (concat name ".bbl") + (mapcar 'car + (LaTeX-bibliography-list)) + (append BibTeX-file-extensions + TeX-Biber-file-extensions))) + ;; We should check for bst files here as well. + (if LaTeX-using-Biber TeX-command-Biber TeX-command-BibTeX)) + ((and + ;; Rational: makeindex should be run when final document is almost + ;; complete (see + ;; http://tex.blogoverflow.com/2012/09/dont-forget-to-run-makeindex/), + ;; otherwise, after following latex runs, index pages may change due + ;; to changes in final document, resulting in extra makeindex and + ;; latex runs. + (member + (setq command-next + (TeX-process-get-variable + name + 'TeX-command-next + (if (and TeX-PDF-via-dvips-ps2pdf TeX-PDF-mode) + "Dvips" + TeX-command-Show))) + (list "Dvips" TeX-command-Show)) + (cdr (assoc (expand-file-name (concat name ".idx")) + LaTeX-idx-changed-alist))) + "Index") + (command-next) + (TeX-command-Show)))) (defun TeX-command-query (name) "Query the user for what TeX command to use." @@ -928,7 +958,8 @@ run of `TeX-run-TeX', use ;; Save information in TeX-error-report-switches ;; Initialize error to nil (no error) for current master. ;; Presence of error is reported inside `TeX-TeX-sentinel-check' - (let ((current-master (TeX-master-file))) + (let ((current-master (TeX-master-file)) + (idx-file nil) (element nil)) ;; the current master file is saved because error routines are ;; parsed in other buffers; (setq TeX-error-report-switches @@ -937,7 +968,22 @@ run of `TeX-run-TeX', use ;; reset error to nil (no error) (setq TeX-error-report-switches (plist-put TeX-error-report-switches - (intern current-master) nil))) + (intern current-master) nil)) + + ;; Store md5 hash of the index file before running LaTeX. + (and (memq major-mode '(doctex-mode latex-mode)) + (prog1 (file-exists-p + (setq idx-file (expand-file-name (concat file ".idx")))) + ;; In order to avoid confusion and pollution of + ;; `LaTeX-idx-md5-alist', remove from this alist all md5 hashes of + ;; the current index file. Note `assq-delete-all' doesn't work with + ;; string keys and has problems with non-list elements in Emacs 21 + ;; (see file tex-site.el). + (while (setq element (assoc idx-file LaTeX-idx-md5-alist)) + (setq LaTeX-idx-md5-alist (delq element LaTeX-idx-md5-alist)))) + (with-temp-buffer + (insert-file-contents idx-file) + (push (cons idx-file (md5 (current-buffer))) LaTeX-idx-md5-alist)))) ;; can we assume that TeX-sentinel-function will not be changed ;; during (TeX-run-format ..)? --pg @@ -989,6 +1035,25 @@ run of `TeX-run-TeX', use process (TeX-synchronous-sentinel name file process)))) +(defun TeX-run-index (name command file) + "Create a process for NAME using COMMAND to compile the index file." + (let ((process (TeX-run-command name command file)) + (element nil)) + (setq TeX-sentinel-function 'TeX-index-sentinel) + ;; Same cleaning as that for `LaTeX-idx-md5-alist' in `TeX-run-TeX'. + (while (setq element + ;; `file' has been determined in `TeX-command-buffer', while + ;; this function has `TeX-master-directory' as + ;; `default-directory', then we have to expand `file' file-name + ;; in the same directory of `TeX-command-buffer'. + (assoc (with-current-buffer TeX-command-buffer + (expand-file-name (concat file ".idx"))) + LaTeX-idx-changed-alist)) + (setq LaTeX-idx-changed-alist (delq element LaTeX-idx-changed-alist))) + (if TeX-process-asynchronous + process + (TeX-synchronous-sentinel name file process)))) + (defun TeX-run-compile (name command file) "Ignore first and third argument, start compile with second argument." (let ((default-directory (TeX-master-directory))) @@ -1375,6 +1440,27 @@ Rerun to get mark in right position\\." nil t) (t (message "%s%s%s" name ": problems after " (TeX-current-pages)) (setq TeX-command-next TeX-command-default))) + + ;; Check whether the idx file changed. + (let ((idx-file nil) (master nil)) + (and (file-exists-p + (setq idx-file + (concat + (setq master + (with-current-buffer TeX-command-buffer + (expand-file-name (TeX-active-master)))) ".idx"))) + ;; imakeidx package automatically runs makeindex, thus, we need to be + ;; sure .ind file isn't newer than .idx. + (TeX-check-files (concat master ".ind") + (list (file-name-nondirectory master)) '("idx")) + (with-temp-buffer + (insert-file-contents idx-file) + (not (equal + ;; Compare old md5 hash of the idx file with the new one. + (cdr (assoc idx-file LaTeX-idx-md5-alist)) + (md5 (current-buffer))))) + (push (cons idx-file t) LaTeX-idx-changed-alist))) + (unless TeX-error-list (run-hook-with-args 'TeX-after-TeX-LaTeX-command-finished-hook (with-current-buffer TeX-command-buffer @@ -1456,6 +1542,19 @@ Rerun to get mark in right position\\." nil t) TeX-output-extension "pdf")) (message "ps2pdf finished successfully. ")))) +(defun TeX-index-sentinel (_process _name) + "Cleanup TeX output buffer after compiling index." + (goto-char (point-max)) + (cond + ((search-backward "TeX Output exited abnormally" nil t) + (message "Index failed. Type `%s' to display output." + (substitute-command-keys + "\\<TeX-mode-map>\\[TeX-recenter-output-buffer]"))) + (t + (setq TeX-command-next TeX-command-default) + (message (concat "Index finished successfully. " + "Run LaTeX again to get index right."))))) + (defun TeX-command-sequence-sentinel (process string) "Call the appropriate sentinel for the current process. diff --git a/tex.el b/tex.el index fa2c475..1c9b95b 100644 --- a/tex.el +++ b/tex.el @@ -156,7 +156,8 @@ If nil, none is specified." :help "Convert DVI file to PostScript") ("Ps2pdf" "ps2pdf %f" TeX-run-ps2pdf nil t :help "Convert PostScript file to PDF") - ("Index" "makeindex %s" TeX-run-command nil t :help "Create index file") + ("Index" "makeindex %s" TeX-run-index nil t + :help "Run makeindex to create index file") ("Xindy" "texindy %s" TeX-run-command nil t :help "Run xindy to create index file") ("Check" "lacheck %s" TeX-run-compile nil (latex-mode)