1 ;;; tex.el --- Support for TeX documents.
3 ;; Copyright (C) 1985-1987, 1991, 1993, 1994, 1996, 1997, 1999-2012
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: auctex-devel@gnu.org
9 ;; This file is part of AUCTeX.
11 ;; AUCTeX is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; AUCTeX is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with AUCTeX; see the file COPYING. If not, write to the Free
23 ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 ;; This file provides AUCTeX support for plain TeX as well as basic
29 ;; functions used by other AUCTeX modes (e.g. for LaTeX, Texinfo and
34 (when (< emacs-major-version 21)
35 (error "AUCTeX requires Emacs 21 or later"))
42 (defgroup TeX-file nil
43 "Files used by AUCTeX."
46 (defgroup TeX-command nil
47 "Calling external commands from AUCTeX."
51 "LaTeX support in AUCTeX."
56 (defgroup TeX-misc nil
57 "Various AUCTeX settings."
60 ;;; Site Customization
62 ;; The following variables are likely to need to be changed for your
63 ;; site. You should do this with customize. Here is the beef: If you
64 ;; want to print, TeX-print-command must be non-nil (if it is nil,
65 ;; you'll get a complaint when using the print menu). If you want to
66 ;; view the queue, TeX-queue-command needs to be non-nil (if it is
67 ;; nil, it won't get mentioned in the menu). If TeX-printer-list is
68 ;; nil, nothing else gets asked: the menu entries lead directly to the
69 ;; respective commands. If those commands contain %p, the value of
70 ;; TeX-printer-default gets inserted there, no questions asked. Now
71 ;; if TeX-printer-list is non-nil, you'll always get asked which
72 ;; printer you want to use. You can enter a configured printer from
73 ;; TeX-printer-list, or an unknown one. The respective menus will
74 ;; show all configured printers. Since you can enter unknown
75 ;; printers, the printer name _must_ be set with %p in
80 (defcustom TeX-print-command "%(o?)dvips -P%p %r %s"
81 "*Command used to print a file.
83 First `%p' is expanded to the printer name, then ordinary expansion is
84 performed as specified in `TeX-expand-list'. If it is nil,
85 then customization is requested."
87 :type '(choice (string :tag "Print command")
88 (const :tag "No print command customized" nil)))
90 (defcustom TeX-command "tex"
91 "Command to run plain TeX."
95 (defcustom TeX-Omega-command "omega"
96 "Command to run plain TeX on Omega."
100 (defcustom LaTeX-command "latex"
101 "Command to run LaTeX."
105 (defcustom LaTeX-Omega-command "lambda"
106 "Command to run LaTeX on Omega."
110 (defcustom ConTeXt-engine nil
111 "Engine to use for --engine in the texexec command.
112 If nil, none is specified."
114 :type '(choice (const :tag "Unspecified" nil)
117 (defcustom ConTeXt-Omega-engine TeX-Omega-command
118 "Engine to use for --engine in the texexec command in Omega mode.
119 If nil, none is specified."
121 :type '(choice (const :tag "Unspecified" nil)
123 ;; At least in TeXLive 2009 ConTeXt does not support an omega option anymore.
124 (make-obsolete-variable 'ConTeXt-Omega-engine 'TeX-engine-alist)
126 (defcustom TeX-queue-command "lpq -P%p"
127 "*Command used to show the status of a printer queue.
129 First `%p' is expanded to the printer name, then ordinary expansion is
130 performed as specified in `TeX-expand-list'. If this is nil,
131 the printer has no corresponding command."
133 :type '(choice (string :tag "Queue check command")
134 (const :tag "No such command" nil)))
136 (defcustom TeX-mode-hook nil
137 "A hook run in TeX mode buffers."
141 (defcustom AmS-TeX-mode-hook nil
142 "A hook run in AmS-TeX mode buffers."
146 ;; This is the major configuration variable. Most sites will only
147 ;; need to change the second string in each entry, which is the name
148 ;; of a command to send to the shell. If you use other formatters
149 ;; like AMSLaTeX or AMSTeX, you can add those to the list. See
150 ;; TeX-expand-list for a description of the % escapes
152 (defcustom TeX-command-list
153 `(("TeX" "%(PDF)%(tex) %`%S%(PDFout)%(mode)%' %t"
155 (plain-tex-mode ams-tex-mode texinfo-mode) :help "Run plain TeX")
156 ("LaTeX" "%`%l%(mode)%' %t"
158 (latex-mode doctex-mode) :help "Run LaTeX")
159 ;; Not part of standard TeX.
160 ("Makeinfo" "makeinfo %t" TeX-run-compile nil
161 (texinfo-mode) :help "Run Makeinfo with Info output")
162 ("Makeinfo HTML" "makeinfo --html %t" TeX-run-compile nil
163 (texinfo-mode) :help "Run Makeinfo with HTML output")
164 ("AmSTeX" "%(PDF)amstex %`%S%(PDFout)%(mode)%' %t"
165 TeX-run-TeX nil (ams-tex-mode) :help "Run AMSTeX")
166 ;; support for ConTeXt --pg
167 ;; first version of ConTeXt to support nonstopmode: 2003.2.10
168 ("ConTeXt" "texexec --once --texutil %(execopts)%t"
169 TeX-run-TeX nil (context-mode) :help "Run ConTeXt once")
170 ("ConTeXt Full" "texexec %(execopts)%t"
172 (context-mode) :help "Run ConTeXt until completion")
173 ("BibTeX" "bibtex %s" TeX-run-BibTeX nil t :help "Run BibTeX")
174 ,(if (or window-system (getenv "DISPLAY"))
175 '("View" "%V" TeX-run-discard-or-function t t :help "Run Viewer")
176 '("View" "dvi2tty -q -w 132 %s" TeX-run-command t t
177 :help "Run Text viewer"))
178 ("Print" "%p" TeX-run-command t t :help "Print the file")
179 ("Queue" "%q" TeX-run-background nil t :help "View the printer queue"
180 :visible TeX-queue-command)
181 ("File" "%(o?)dvips %d -o %f " TeX-run-command t t
182 :help "Generate PostScript file")
183 ("Index" "makeindex %s" TeX-run-command nil t :help "Create index file")
184 ("Check" "lacheck %s" TeX-run-compile nil (latex-mode)
185 :help "Check LaTeX file for correctness")
186 ("Spell" "(TeX-ispell-document \"\")" TeX-run-function nil t
187 :help "Spell-check the document")
188 ("Clean" "TeX-clean" TeX-run-function nil t
189 :help "Delete generated intermediate files")
190 ("Clean All" "(TeX-clean t)" TeX-run-function nil t
191 :help "Delete generated intermediate and output files")
192 ("Other" "" TeX-run-command t t :help "Run an arbitrary command"))
193 "List of commands to execute on the current document.
195 Each element is a list, whose first element is the name of the command
196 as it will be presented to the user.
198 The second element is the string handed to the shell after being
199 expanded. The expansion is done using the information found in
202 The third element is the function which actually start the process.
203 Several such hooks has been defined:
205 TeX-run-command: Start up the process and show the output in a
206 separate buffer. Check that there is not two commands running for the
207 same file. Return the process object.
209 TeX-run-format: As `TeX-run-command', but assume the output is created
210 by a TeX macro package. Return the process object.
212 TeX-run-TeX: For TeX output.
214 TeX-run-interactive: Run TeX or LaTeX interactively.
216 TeX-run-BibTeX: For BibTeX output.
218 TeX-run-compile: Use `compile' to run the process.
220 TeX-run-shell: Use `shell-command' to run the process.
222 TeX-run-discard: Start the process in the background, discarding its
225 TeX-run-background: Start the process in the background, show output
228 TeX-run-silent: Start the process in the background.
230 TeX-run-discard-foreground: Start the process in the foreground,
231 discarding its output.
233 TeX-run-function: Execute the Lisp function or function call
234 specified by the string in the second element. Consequently,
235 this hook does not start a process.
237 TeX-run-discard-or-function: If the command is a Lisp function,
238 execute it as such, otherwise start the command as a process,
239 discarding its output.
241 To create your own hook, define a function taking three arguments: The
242 name of the command, the command string, and the name of the file to
243 process. It might be useful to use `TeX-run-command' in order to
244 create an asynchronous process.
246 If the fourth element is non-nil, the user will get a chance to
247 modify the expanded string.
249 The fifth element indicates in which mode(s) the command should be
250 present in the Command menu. Use t if it should be active in any
251 mode. If it should only be present in some modes, specify a list with
252 the respective mode names.
254 Any additional elements get just transferred to the respective menu entries."
256 :type '(repeat (group :value ("" "" TeX-run-command nil t)
258 (string :tag "Command")
260 :value TeX-run-command
261 (function-item TeX-run-command)
262 (function-item TeX-run-format)
263 (function-item TeX-run-TeX)
264 (function-item TeX-run-interactive)
265 (function-item TeX-run-BibTeX)
266 (function-item TeX-run-compile)
267 (function-item TeX-run-shell)
268 (function-item TeX-run-discard)
269 (function-item TeX-run-background)
270 (function-item TeX-run-silent)
271 (function-item TeX-run-discard-foreground)
272 (function-item TeX-run-function)
273 (function-item TeX-run-discard-or-function)
274 (function :tag "Other"))
275 (boolean :tag "Prompt")
278 (set (const :tag "Plain TeX" plain-tex-mode)
279 (const :tag "LaTeX" latex-mode)
280 (const :tag "DocTeX" doctex-mode)
281 (const :tag "ConTeXt" context-mode)
282 (const :tag "Texinfo" texinfo-mode)
283 (const :tag "AmSTeX" ams-tex-mode)))
284 (repeat :tag "Menu elements" :inline t sexp))))
286 (defcustom TeX-command-output-list
288 ; Add the following line if you want to use htlatex (tex4ht)
289 ; ("\\`htlatex" ("html"))
291 "List of regexps and file extensions.
293 Each element is a list, whose first element is a regular expression to
294 match against the name of the command that will be used to process the TeX
297 The second element is either a string or a list with a string as element.
298 If it is a string this is the default file extension that will be expected
299 for output files that are produced by commands that match the first
300 element. The real file extension will be obtained from the logging output
301 if possible, defaulting to the given string.
302 If it is a list, the element of the list will be the fixed extension used
303 without looking at the logging output.
305 If this list does not yield an extension, the default is either \"dvi\"
306 or \"pdf\", depending on the setting of `TeX-PDF-mode'.
307 Extensions must be given without the \".\"."
310 :type '(repeat (group (regexp :tag "Command Regexp")
311 (choice (string :tag "Default Extension")
312 (group (string :tag "Fixed Extension"))))))
314 ;; You may want to change the default LaTeX version for your site.
315 (defcustom LaTeX-version "2e"
316 "Default LaTeX version. Currently recognized is \"2\" and \"2e\"."
318 :type '(radio (const :format "%v\n%h"
320 The executable `latex' is LaTeX version 2."
322 (const :format "%v\n%h"
324 The executable `latex' is LaTeX version 2e."
326 (string :tag "Other")))
329 ;; Use different compilation commands depending on style.
330 ;; Only works if parsing is enabled.
332 (defcustom LaTeX-command-style
333 ;; They have all been combined in LaTeX 2e.
334 '(("" "%(PDF)%(latex) %S%(PDFout)"))
335 "List of style options and LaTeX commands.
337 If the first element (a regular expression) matches the name of one of
338 the style files, any occurrence of the string `%l' in a command in
339 `TeX-command-list' will be replaced with the second element. The first
340 match is used, if no match is found the `%l' is replaced with the empty
343 :type '(repeat (group :value ("" "")
344 regexp (string :tag "Style"))))
346 ;; Enter the names of the printers available at your site, or nil if
347 ;; you only have one printer.
349 (defcustom TeX-printer-list
350 '(("Default" "%(o?)dvips -f %s | lpr" "lpq"))
351 "List of available printers.
353 The first element of each entry is the printer name.
355 The second element is the command used to print to this
356 printer. It defaults to the value of `TeX-print-command' when nil.
358 The third element is the command used to examine the print queue for
359 this printer. It defaults to the value of `TeX-queue-command' similarly.
361 Any occurrence of `%p' in the second or third element is expanded to
362 the printer name given in the first element, then ordinary expansion
363 is performed as specified in `TeX-expand-list'.
365 If this list is empty, only `TeX-print-command' and `TeX-queue-command'
368 :type '(repeat (group (string :tag "Name")
369 (option (group :inline t
372 (const :tag "default")
373 (string :format "%v"))
374 (option (choice :tag "Queue"
375 (const :tag "default")
379 ;; The name of the most used printer.
381 (defcustom TeX-printer-default (or (getenv "PRINTER")
382 (and TeX-printer-list
383 (car (car TeX-printer-list)))
385 "*Default printer to use with `TeX-command'."
389 (defcustom TeX-print-style '(("^landscape$" "-t landscape"))
390 "List of style options and print options.
392 If the first element (a regular expression) matches the name of one of
393 the style files, any occurrence of the string `%r' in a command in
394 `TeX-command-list' will be replaced with the second element. The first
395 match is used, if no match is found the `%r' is replaced with the empty
398 :type '(repeat (group regexp (string :tag "Command"))))
400 ;; This is the list of expansion for the commands in
401 ;; TeX-command-list. Not likely to be changed, but you may e.g. want
402 ;; to handle .ps files.
404 (defcustom TeX-expand-list
405 '(("%p" TeX-printer-query) ;%p must be the first entry
407 (TeX-printer-query t)))
409 (TeX-source-correlate-start-server-maybe)
410 (TeX-view-command-raw)))
412 (TeX-source-correlate-start-server-maybe)
413 (TeX-output-style-check TeX-output-view-style)))
415 (TeX-source-correlate-start-server-maybe)
416 (TeX-style-check TeX-view-style)))
418 (TeX-style-check TeX-print-style)))
420 (TeX-style-check LaTeX-command-style)))
422 (if (and (eq TeX-engine 'default)
427 ("%(PDFout)" (lambda ()
428 (cond ((and (eq TeX-engine 'xetex)
431 ((and (eq TeX-engine 'luatex)
433 " --output-format=dvi")
434 ((and (eq TeX-engine 'default)
437 " \"\\pdfoutput=0 \"")
439 ("%(mode)" (lambda ()
440 (if TeX-interactive-mode
442 " -interaction=nonstopmode")))
443 ("%(o?)" (lambda () (if (eq TeX-engine 'omega) "o" "")))
444 ("%(tex)" (lambda () (eval (nth 2 (assq TeX-engine (TeX-engine-alist))))))
445 ("%(latex)" (lambda () (eval (nth 3 (assq TeX-engine (TeX-engine-alist))))))
446 ("%(execopts)" ConTeXt-expand-options)
447 ("%S" TeX-source-correlate-expand-options)
448 ("%dS" TeX-source-specials-view-expand-options)
449 ("%cS" TeX-source-specials-view-expand-client)
450 ("%(outpage)" (lambda ()
451 (if TeX-source-correlate-output-page-function
452 (funcall TeX-source-correlate-output-page-function)
454 ;; `file' means to call `TeX-master-file' or `TeX-region-file'
458 (setq TeX-command-pos t TeX-command-text "")))
460 (if (eq TeX-command-pos t)
461 (setq TeX-command-pos pos
463 (setq pos (1+ pos)))))
464 ("\"" (lambda nil (if (numberp TeX-command-pos)
465 (setq TeX-command-text
480 (setq pos (1+ pos)))))
483 (if (stringp TeX-command-text)
485 (setq pos (+ (length TeX-command-text) 9)
487 (and (string-match " "
490 (concat TeX-command-text " \"\\input\""))
491 (setq TeX-command-pos nil)
493 (setq TeX-command-text nil))))
494 ("%n" TeX-current-line)
497 ("%o" (lambda nil (funcall file (TeX-output-extension) t)))
498 ;; for source specials the file name generated for the xdvi
499 ;; command needs to be relative to the master file, just in
500 ;; case the file is in a different subdirectory
501 ("%b" TeX-current-file-name-master-relative)
502 ;; the following is for preview-latex.
503 ("%m" preview-create-subdirectory))
504 "List of expansion strings for TeX command names.
506 Each entry is a list with two or more elements. The first element is
507 the string to be expanded. The second element is the name of a
508 function returning the expanded string when called with the remaining
509 elements as arguments. The special value `file' will be expanded to
510 the name of the file being processed, with an optional extension."
512 :type '(repeat (group (string :tag "Key")
513 (sexp :tag "Expander")
516 (sexp :format "%v")))))
519 ;; The following dependencies are not done with autoload cookies since
520 ;; they are only useful when tex.el is loaded, anyway. tex-buf.el
521 ;; should remain unloaded as long as one is only editing files, so
522 ;; requiring it here would be wrong.
524 (autoload 'TeX-region-create "tex-buf" nil nil)
525 (autoload 'TeX-save-document "tex-buf" nil t)
526 (autoload 'TeX-home-buffer "tex-buf" nil t)
527 (autoload 'TeX-pin-region "tex-buf" nil t)
528 (autoload 'TeX-command-region "tex-buf" nil t)
529 (autoload 'TeX-command-buffer "tex-buf" nil t)
530 (autoload 'TeX-command-master "tex-buf" nil t)
531 (autoload 'TeX-command "tex-buf" nil nil)
532 (autoload 'TeX-kill-job "tex-buf" nil t)
533 (autoload 'TeX-recenter-output-buffer "tex-buf" nil t)
534 (autoload 'TeX-next-error "tex-buf" nil t)
535 (autoload 'TeX-region-file "tex-buf" nil nil)
536 (autoload 'TeX-current-offset "tex-buf" nil nil)
537 (autoload 'TeX-process-set-variable "tex-buf" nil nil)
538 (autoload 'TeX-view "tex-buf" nil t)
545 (if (featurep 'xemacs)
546 (defun TeX-maybe-remove-help (menu)
547 "Removes :help entries from menus, since XEmacs does not like them.
548 Also does other stuff."
550 (cond ((eq (car menu) :help)
551 (TeX-maybe-remove-help (cddr menu)))
552 ((eq (car menu) :visible)
555 (TeX-maybe-remove-help (cddr menu)))))
556 (t (cons (TeX-maybe-remove-help (car menu))
557 (TeX-maybe-remove-help (cdr menu))))))
559 (vconcat (TeX-maybe-remove-help (append menu nil))))
561 (defun TeX-maybe-remove-help (menu)
562 "Compatibility function that would remove :help entries if on XEmacs,
563 but does nothing in Emacs."
565 (defmacro TeX-menu-with-help (menu)
566 "Compatibility macro that removes :help entries if on XEmacs.
567 Also does other stuff."
568 (TeX-maybe-remove-help menu)))
571 ;;; Documentation for Info-goto-emacs-command-node and similar
573 (eval-after-load 'info '(dolist (elt '("TeX" "LaTeX" "ConTeXt" "Texinfo"
575 (add-to-list 'Info-file-list-for-emacs
576 (cons elt "AUCTeX"))))
578 (defadvice hack-one-local-variable (after TeX-hack-one-local-variable-after
580 "Call minor mode function if minor mode variable is found."
581 (let ((var (ad-get-arg 0))
582 (val (ad-get-arg 1)))
583 ;; Instead of checking for each mode explicitely `minor-mode-list'
584 ;; could be used. But this may make the byte compiler pop up.
585 (when (memq var '(TeX-PDF-mode
586 TeX-source-correlate-mode TeX-interactive-mode
587 TeX-fold-mode LaTeX-math-mode))
588 (if (symbol-value val) (funcall var 1) (funcall var 0)))))
590 (defvar TeX-overlay-priority-step 16
591 "Numerical difference of priorities between nested overlays.
592 The step should be big enough to allow setting a priority for new
593 overlays between two existing ones.")
596 ;;; Special support for XEmacs
598 (when (featurep 'xemacs)
600 (defun TeX-read-string
601 (prompt &optional initial-input history default-value)
603 (read-string prompt initial-input history default-value t)
604 (wrong-number-of-arguments
605 (read-string prompt initial-input history default-value))))
607 (defun TeX-mark-active ()
608 ;; In Lucid (mark) returns nil when not active.
613 (defun TeX-active-mark ()
614 (and zmacs-regions (mark)))
616 (fset 'TeX-activate-region (symbol-function 'zmacs-activate-region))
618 ;; I am aware that this counteracts coding conventions but I am sick
620 (unless (fboundp 'line-beginning-position)
621 (defalias 'line-beginning-position 'point-at-bol))
622 (unless (fboundp 'line-end-position)
623 (defalias 'line-end-position 'point-at-eol))
625 (defun TeX-overlay-prioritize (start end)
626 "Calculate a priority for an overlay extending from START to END.
627 The calculated priority is lower than the minimum of priorities
628 of surrounding overlays and higher than the maximum of enclosed
630 (let (inner-priority outer-priority
631 (prios (cons nil nil)))
635 (or (eq (extent-property ov 'category) 'TeX-fold)
636 (extent-property ov 'preview-state))
638 (max (or (car prios) 0)
639 (extent-property ov 'priority))))
641 nil start end prios 'start-and-end-in-region 'priority)
645 (or (eq (extent-property ov 'category) 'TeX-fold)
646 (extent-property ov 'preview-state))
648 (min (or (cdr prios) most-positive-fixnum)
649 (extent-property ov 'priority))))
652 '(start-and-end-in-region negate-in-region) 'priority)
653 (setq inner-priority (car prios) outer-priority (cdr prios))
654 (cond ((and inner-priority (not outer-priority))
655 (+ inner-priority TeX-overlay-priority-step))
656 ((and (not inner-priority) outer-priority)
657 (/ outer-priority 2))
658 ((and inner-priority outer-priority)
659 (+ (/ (- outer-priority inner-priority) 2) inner-priority))
660 (t TeX-overlay-priority-step)))) )
663 (if (fboundp 'completing-read-multiple)
664 (defalias 'TeX-completing-read-multiple 'completing-read-multiple)
665 (defun TeX-completing-read-multiple
666 (prompt table &optional predicate require-match initial-input
667 hist def inherit-input-method)
668 "Poor mans implementation of Emacs' `completing-read-multiple' for XEmacs.
669 The XEmacs package edit-utils-2.32 includes `crm.el'."
670 (multi-prompt "," nil prompt table predicate require-match initial-input
673 (if (fboundp 'line-number-at-pos)
674 (defalias 'TeX-line-number-at-pos 'line-number-at-pos)
675 ;; `line-number-at-pos' from `simple.el' in Emacs CVS (2006-06-07)
676 (defun TeX-line-number-at-pos (&optional pos)
677 "Return (narrowed) buffer line number at position POS.
678 If POS is nil, use current buffer location."
679 (let ((opoint (or pos (point))) start)
681 (goto-char (point-min))
685 (1+ (count-lines start (point)))))))
687 ;;; Special support for GNU Emacs
689 (unless (featurep 'xemacs)
691 (defun TeX-read-string (prompt &optional initial-input history default-value)
692 (read-string prompt initial-input history default-value t))
694 (defun TeX-mark-active ()
695 ;; In FSF 19 mark-active indicates if mark is active.
698 (defun TeX-active-mark ()
699 (and transient-mark-mode mark-active))
701 (defun TeX-activate-region ()
704 (defun TeX-overlay-prioritize (start end)
705 "Calculate a priority for an overlay extending from START to END.
706 The calculated priority is lower than the minimum of priorities
707 of surrounding overlays and higher than the maximum of enclosed
709 (let (outer-priority inner-priority ov-priority)
710 (dolist (ov (overlays-in start end))
711 (when (or (eq (overlay-get ov 'category) 'TeX-fold)
712 (overlay-get ov 'preview-state))
713 (setq ov-priority (overlay-get ov 'priority))
714 (if (>= (overlay-start ov) start)
715 (setq inner-priority (max ov-priority (or inner-priority
717 (setq outer-priority (min ov-priority (or outer-priority
719 (cond ((and inner-priority (not outer-priority))
720 (+ inner-priority TeX-overlay-priority-step))
721 ((and (not inner-priority) outer-priority)
722 (/ outer-priority 2))
723 ((and inner-priority outer-priority)
724 (+ (/ (- outer-priority inner-priority) 2) inner-priority))
725 (t TeX-overlay-priority-step)))) )
727 (defun TeX-delete-dups-by-car (alist &optional keep-list)
728 "Return a list of all elements in ALIST, but each car only once.
729 Elements of KEEP-LIST are not removed even if duplicate."
730 ;; Copy of `reftex-uniquify-by-car' (written by David Kastrup).
731 (setq keep-list (sort (copy-sequence keep-list) #'string<))
732 (setq alist (sort (copy-sequence alist)
734 (string< (car a) (car b)))))
735 (let ((new alist) elt)
737 (setq elt (caar new))
738 (while (and keep-list (string< (car keep-list) elt))
739 (setq keep-list (cdr keep-list)))
740 (unless (and keep-list (string= elt (car keep-list)))
741 (while (string= elt (car (cadr new)))
742 (setcdr new (cddr new))))
743 (setq new (cdr new))))
746 (defun TeX-delete-duplicate-strings (list)
747 "Return a list of all strings in LIST, but each only once."
748 (setq list (TeX-sort-strings list))
749 (let ((new list) elt)
752 (while (string= elt (cadr new))
753 (setcdr new (cddr new)))
754 (setq new (cdr new))))
757 (defun TeX-sort-strings (list)
758 "Return sorted list of all strings in LIST."
759 (sort (copy-sequence list) #'string<))
763 (defgroup TeX-output nil
764 "Parsing TeX output."
768 (defcustom TeX-display-help t
769 "Control type of help display when stepping through errors with \\[TeX-next-error].
770 If t display help buffer. If nil display message about error in
771 echo area. If `expert' display output buffer with raw processor output."
773 :type '(choice (const :tag "Help buffer" t)
774 (const :tag "Echo area" nil)
775 (const :tag "Output buffer" expert)))
777 (defcustom TeX-debug-bad-boxes nil
778 "Non-nil means also find overfull/underfull box warnings with \\[TeX-next-error]."
782 (defcustom TeX-debug-warnings nil
783 "Non-nil means also find LaTeX or package warnings with \\[TeX-next-error]."
787 (defun TeX-toggle-debug-bad-boxes ()
788 "Toggle if the debugger should display \"bad boxes\" too."
790 (setq TeX-debug-bad-boxes (not TeX-debug-bad-boxes))
791 (message (concat "TeX-debug-bad-boxes: "
792 (if TeX-debug-bad-boxes "on" "off"))))
794 (defun TeX-toggle-debug-warnings ()
795 "Toggle if the debugger should display warnings too."
797 (setq TeX-debug-warnings (not TeX-debug-warnings))
798 (message (concat "TeX-debug-warnings: "
799 (if TeX-debug-warnings "on" "off"))))
803 (defvar TeX-base-mode-name nil
804 "Base name of mode.")
805 (make-variable-buffer-local 'TeX-base-mode-name)
807 (defun TeX-set-mode-name (&optional changed local reset)
808 "Build and set the mode name.
809 The base mode name will be concatenated with indicators for
810 helper modes where appropriate.
812 If CHANGED is non-nil, it indicates which global mode
813 may have changed so that all corresponding buffers
814 without a local value might get their name updated.
815 A value of t will thus update all buffer names.
817 If LOCAL is non-nil and CHANGED is buffer-local, only
818 a local change has been performed and only the local
819 name is to be updated.
821 If RESET is non-nil, `TeX-command-next' is reset to
822 `TeX-command-default' in updated buffers."
824 (not (and local (local-variable-p changed (current-buffer)))))
825 (dolist (buffer (buffer-list))
826 (and (local-variable-p 'TeX-mode-p buffer)
827 (not (local-variable-p changed buffer))
828 (with-current-buffer buffer (TeX-set-mode-name nil nil reset))))
830 (let ((trailing-flags
832 (and (boundp 'TeX-fold-mode) TeX-fold-mode "F")
833 (and (boundp 'LaTeX-math-mode) LaTeX-math-mode "M")
834 (and TeX-PDF-mode "P")
835 (and TeX-interactive-mode "I")
836 (and TeX-source-correlate-mode "S"))))
837 (setq mode-name (concat TeX-base-mode-name
838 (when (> (length trailing-flags) 0)
839 (concat "/" trailing-flags))))
841 (TeX-process-set-variable (TeX-master-file)
842 'TeX-command-next TeX-command-default)
843 (TeX-process-set-variable (TeX-region-file)
844 'TeX-command-next TeX-command-default))
845 (set-buffer-modified-p (buffer-modified-p))))))
847 (defun TeX-mode-prefix ()
848 "Return the prefix of the current mode as string."
849 (cdr (assoc major-mode '((plain-tex-mode . "plain-TeX")
850 (latex-mode . "LaTeX")
851 (doctex-mode . "docTeX")
852 (texinfo-mode . "Texinfo")
853 (context-mode . "ConTeXt")))))
857 (defgroup TeX-view nil
858 "Calling viewers from AUCTeX."
861 (defcustom TeX-view-style
863 "^" (regexp-opt '("a4paper" "a4dutch" "a4wide" "sem-a4")) "$")
864 "%(o?)xdvi %dS -paper a4 %d")
865 (,(concat "^" (regexp-opt '("a5paper" "a5comb")) "$")
866 "%(o?)xdvi %dS -paper a5 %d")
867 ("^b5paper$" "%(o?)xdvi %dS -paper b5 %d")
868 ("^letterpaper$" "%(o?)xdvi %dS -paper us %d")
869 ("^legalpaper$" "%(o?)xdvi %dS -paper legal %d")
870 ("^executivepaper$" "%(o?)xdvi %dS -paper 7.25x10.5in %d")
871 ("^landscape$" "%(o?)xdvi %dS -paper a4r -s 0 %d")
872 ;; The latest xdvi can show embedded postscript. If you don't
873 ;; have that, uncomment next line.
874 ;; ("^epsf$" "ghostview %f")
875 ("." "%(o?)xdvi %dS %d"))
876 "List of style options and view options.
878 If the first element (a regular expression) matches the name of
879 one of the style files, any occurrence of the string `%v' in a
880 command in `TeX-command-list' will be replaced with the second
881 element. The first match is used, if no match is found the `%v'
882 is replaced with the empty string.
884 As a default, the \"View\" command in `TeX-command-list' is set
885 to `%V'. This means that `TeX-output-view-style' will be
886 consulted before `TeX-view-style'. Only if no match is found in
887 `TeX-output-view-style' the settings in `TeX-view-style' will be
888 considered. If you want to bypass `TeX-output-view-style', which
889 is not recommended because it is more powerful than
890 `TeX-view-style', use `%v' in the \"View\" command."
892 :type '(repeat (group regexp (string :tag "Command"))))
894 (defcustom TeX-output-view-style
895 `(("^dvi$" ("^landscape$" "^pstricks$\\|^pst-\\|^psfrag$")
896 "%(o?)dvips -t landscape %d -o && gv %f")
897 ("^dvi$" "^pstricks$\\|^pst-\\|^psfrag$" "%(o?)dvips %d -o && gv %f")
899 "^" (regexp-opt '("a4paper" "a4dutch" "a4wide" "sem-a4")) "$")
901 "%(o?)xdvi %dS -paper a4r -s 0 %d")
903 "^" (regexp-opt '("a4paper" "a4dutch" "a4wide" "sem-a4")) "$")
904 "%(o?)xdvi %dS -paper a4 %d")
905 ("^dvi$" (,(concat "^" (regexp-opt '("a5paper" "a5comb")) "$")
907 "%(o?)xdvi %dS -paper a5r -s 0 %d")
908 ("^dvi$" ,(concat "^" (regexp-opt '("a5paper" "a5comb")) "$")
909 "%(o?)xdvi %dS -paper a5 %d")
910 ("^dvi$" "^b5paper$" "%(o?)xdvi %dS -paper b5 %d")
911 ("^dvi$" "^letterpaper$" "%(o?)xdvi %dS -paper us %d")
912 ("^dvi$" "^legalpaper$" "%(o?)xdvi %dS -paper legal %d")
913 ("^dvi$" "^executivepaper$" "%(o?)xdvi %dS -paper 7.25x10.5in %d")
914 ("^dvi$" "." "%(o?)xdvi %dS %d")
915 ("^pdf$" "." "xpdf -remote %s -raise %o %(outpage)")
916 ("^html?$" "." "netscape %o"))
917 "List of output file extensions and view options.
919 If the first element (a regular expression) matches the output
920 file extension, and the second element (a regular expression)
921 matches the name of one of the style options, any occurrence of
922 the string `%V' in a command in `TeX-command-list' will be
923 replaced with the third element. The first match is used; if no
924 match is found the `%V' is replaced with `%v'. The outcome of `%v'
925 is determined by the settings in `TeX-view-style' which therefore
926 serves as a fallback for `TeX-output-view-style'. The second
927 element may also be a list of regular expressions, in which case
928 all the regular expressions must match for the element to apply."
930 :type '(repeat (group
931 (regexp :tag "Extension")
932 (choice regexp (repeat :tag "List" regexp))
933 (string :tag "Command"))))
935 ;;; Viewing (new implementation)
937 (defvar TeX-view-predicate-list-builtin
939 (string-match "dvi" (TeX-output-extension)))
941 (string-match "pdf" (TeX-output-extension)))
943 (string-match "html" (TeX-output-extension)))
945 (TeX-match-style "^pstricks$\\|^pst-\\|^psfrag$"))
947 (eq TeX-engine 'omega))
949 (eq TeX-engine 'xetex))
951 TeX-source-correlate-mode)
953 (TeX-match-style "\\`landscape\\'"))
955 (not (TeX-match-style "\\`landscape\\'")))
957 (TeX-match-style "\\`a4paper\\|a4dutch\\|a4wide\\|sem-a4\\'"))
959 (TeX-match-style "\\`a5paper\\|a5comb\\'"))
961 (TeX-match-style "\\`b5paper\\'"))
963 (TeX-match-style "\\`letterpaper\\'"))
965 (TeX-match-style "\\`legalpaper\\'"))
967 (TeX-match-style "\\`executivepaper\\'")))
968 "Alist of built-in predicates for viewer selection and invocation.
969 See the doc string of `TeX-view-predicate-list' for a short
970 description of each predicate.")
972 (defcustom TeX-view-predicate-list nil
973 "Alist of predicates for viewer selection and invocation.
974 The key of each list item is a symbol and the value a Lisp form
975 to be evaluated. The form should return nil if the predicate is
978 Built-in predicates provided in `TeX-view-predicate-list-builtin'
979 can be overwritten by defining predicates with the same symbol.
981 The following built-in predicates are available:
982 `output-dvi': The output is a DVI file.
983 `output-pdf': The output is a PDF file.
984 `output-html': The output is an HTML file.
985 `style-pstricks': The document loads a PSTricks package.
986 `engine-omega': The Omega engine is used for typesetting.
987 `engine-xetex': The XeTeX engine is used for typesetting.
988 `mode-io-correlate': TeX Source Correlate mode is active.
989 `paper-landscape': The document is typeset in landscape orientation.
990 `paper-portrait': The document is not typeset in landscape orientation.
991 `paper-a4': The paper format is A4.
992 `paper-a5': The paper format is A5.
993 `paper-b5': The paper format is B5.
994 `paper-letter': The paper format is letter.
995 `paper-legal': The paper format is legal.
996 `paper-executive': The paper format is executive."
998 :type '(alist :key-type symbol :value-type (group sexp)))
1000 (defvar TeX-view-program-list-builtin
1002 ((eq system-type 'windows-nt)
1003 '(("Yap" ("yap -1" (mode-io-correlate " -s %n%b") " %o"))
1004 ("dvips and start" "dvips %d -o && start \"\" %f")
1005 ("start" "start \"\" %o")))
1006 ;; XXX: We need the advice of a Mac OS X user to configure this
1007 ;; correctly and test it.
1008 ;; ((eq system-type 'darwin)
1009 ;; '(("Preview.app" "open -a Preview.app %o")
1010 ;; ("Skim" "open -a Skim.app %o")
1011 ;; ("displayline" "displayline %n %o %b")
1012 ;; ("open" "open %o")))
1014 '(("xdvi" ("%(o?)xdvi"
1015 (mode-io-correlate " -sourceposition \"%n %b\" -editor \"%cS\"")
1016 ((paper-a4 paper-portrait) " -paper a4")
1017 ((paper-a4 paper-landscape) " -paper a4r")
1018 ((paper-a5 paper-portrait) " -paper a5")
1019 ((paper-a5 paper-landscape) " -paper a5r")
1020 (paper-b5 " -paper b5")
1021 (paper-letter " -paper us")
1022 (paper-legal " -paper legal")
1023 (paper-executive " -paper 7.25x10.5in")
1025 ("dvips and gv" "%(o?)dvips %d -o && gv %f")
1027 ("xpdf" ("xpdf -remote %s -raise %o" (mode-io-correlate " %(outpage)")))
1028 ("Evince" ("evince" (mode-io-correlate " -p %(outpage)") " %o"))
1029 ("xdg-open" "xdg-open %o"))))
1030 "Alist of built-in viewer specifications.
1031 This variable should not be changed by the user who can use
1032 `TeX-view-program-list' to add new viewers or overwrite the
1033 definition of built-in ones. The latter variable also contains a
1034 description of the data format.")
1036 (defcustom TeX-view-program-list nil
1037 "Alist of viewer specifications.
1038 This variable can be used to specify how a viewer is to be
1039 invoked and thereby add new viewers on top of the built-in list
1040 of viewers defined in `TeX-view-program-list-builtin' or override
1041 entries in the latter.
1043 The car of each item is a string with a user-readable name. The
1044 second element can be a command line to be run as a process or a
1045 Lisp function to be executed. The command line can either be
1046 specified as a single string or a list of strings and two-part
1047 lists. The first element of the two-part lists is a symbol or a
1048 list of symbols referring to one or more of the predicates in
1049 `TeX-view-predicate-list' or `TeX-view-predicate-list-builtin'.
1050 The second part of the two-part lists is a command line part.
1051 The command line for the viewer is constructed by concatenating
1052 the command line parts. Parts with a predicate are only
1053 considered if the predicate was evaluated with a positive result.
1054 Note that the command line can contain placeholders as defined in
1055 `TeX-expand-list' which are expanded before the viewer is called.
1057 The use of a function as the second element only works if the
1058 View command in `TeX-command-list' makes use of the hook
1059 `TeX-run-discard-or-function'.
1061 Note: Predicates defined in the current Emacs session will only
1062 show up in the customization interface for this variable after
1067 :key-type (string :tag "Name")
1070 (group :tag "Command" (string :tag "Command"))
1071 (group :tag "Command parts"
1073 :tag "Command parts"
1075 (string :tag "Command part")
1076 (list :tag "Predicate and command part"
1078 ;; Build the list of available predicates.
1079 (mapc (lambda (spec)
1080 (add-to-list 'list `(const ,(car spec))))
1081 (append TeX-view-predicate-list
1082 TeX-view-predicate-list-builtin))
1083 ;; Sort the list alphabetically.
1084 (setq list (sort list
1087 (downcase (symbol-name (cadr a)))
1088 (downcase (symbol-name (cadr b)))))))
1090 (choice :tag "Predicate" ,@list)
1091 (repeat :tag "List of predicates"
1092 (choice :tag "Predicate" ,@list))))
1093 (string :tag "Command part")))))
1094 (group :tag "Function" function))))
1096 ;; XXX: Regarding a possibility to (manually) run an update command,
1097 ;; one could support this through `TeX-view' by letting it temporarily
1098 ;; set a variable which is checked with a predicate in the viewer
1099 ;; selection. If the check is positive, the update command is run
1100 ;; instead of the normal viewer command. Direct support through the
1101 ;; View command would require a predicate which knows when an update
1103 (defcustom TeX-view-program-selection
1105 ((eq system-type 'windows-nt)
1106 '(((output-dvi style-pstricks) "dvips and start")
1108 (output-pdf "start")
1109 (output-html "start")))
1110 ;; XXX: We need the advice of a Mac OS X user to configure this
1111 ;; correctly and test it.
1112 ;; ((eq system-type 'darwin)
1113 ;; '((output-dvi "open")
1114 ;; (output-pdf "open")
1115 ;; (output-html "open")))
1117 '(((output-dvi style-pstricks) "dvips and gv")
1119 (output-pdf "Evince")
1120 (output-html "xdg-open"))))
1121 "Alist of predicates and viewers.
1122 Each entry consists of a list with two elements. The first
1123 element is a symbol or list of symbols referring to predicates as
1124 defined in `TeX-view-predicate-list' or
1125 `TeX-view-predicate-list-builtin'. The second element is a
1126 string referring to the name of a viewer as defined in
1127 `TeX-view-program-list' or `TeX-view-program-list-builtin'.
1129 When a viewer is called for, the entries are evaluated in turn
1130 and the viewer related to the first entry all predicates of which
1131 are evaluated positively is chosen."
1133 :type `(alist :key-type
1134 ;; Offer list of defined predicates.
1136 (mapc (lambda (spec)
1137 (add-to-list 'list `(const ,(car spec))))
1138 (append TeX-view-predicate-list
1139 TeX-view-predicate-list-builtin))
1140 (setq list (sort list
1143 (downcase (symbol-name (cadr a)))
1144 (downcase (symbol-name (cadr b)))))))
1145 `(choice (choice :tag "Single predicate" ,@list)
1146 (repeat :tag "Multiple predicates"
1149 ;; Offer list of defined viewers.
1150 (group (choice :tag "Viewer"
1152 (mapc (lambda (spec)
1154 `(const ,(car spec))))
1155 (append TeX-view-program-list
1156 TeX-view-program-list-builtin))
1159 (string< (downcase (cadr a))
1160 (downcase (cadr b))))))))))
1162 (defun TeX-match-style (regexp)
1163 "Check if a style matching REGEXP is active."
1164 (TeX-member regexp (TeX-style-list) 'string-match))
1166 (defun TeX-view-match-predicate (predicate)
1167 "Check if PREDICATE is true.
1168 PREDICATE can be a symbol or a list of symbols defined in
1169 `TeX-view-predicate-list-builtin' or `TeX-view-predicate-list'.
1170 In case of a single symbol, return t if the predicate is true,
1171 nil otherwise. In case of a list of symbols, return t if all
1172 predicates are true, nil otherwise."
1173 (let ((pred-symbols (if (listp predicate) predicate (list predicate)))
1174 (pred-defs (append TeX-view-predicate-list
1175 TeX-view-predicate-list-builtin))
1178 (while (and (setq elt (pop pred-symbols)) result)
1179 (unless (eval (cadr (assq elt pred-defs)))
1183 (defun TeX-view-command-raw ()
1184 "Choose a viewer and return its unexpanded command string."
1185 (let ((selection TeX-view-program-selection)
1186 entry viewer spec command)
1187 ;; Find the appropriate viewer.
1188 (while (and (setq entry (pop selection)) (not viewer))
1189 (when (TeX-view-match-predicate (car entry))
1190 (setq viewer (cadr entry))))
1192 (error "No matching viewer found"))
1193 ;; Get the command line or function spec.
1194 (setq spec (cadr (assoc viewer (append TeX-view-program-list
1195 TeX-view-program-list-builtin))))
1196 (cond ((functionp spec)
1197 ;; Converting the function call to a string is ugly, but
1198 ;; the backend currently only supports strings.
1199 (prin1-to-string spec))
1203 ;; Build the unexpanded command line. Pieces with predicates are
1204 ;; only added if the predicate is evaluated positively.
1206 (cond ((stringp elt)
1207 (setq command (concat command elt)))
1209 (when (TeX-view-match-predicate (car elt))
1210 (setq command (concat command (cadr elt)))))))
1215 (defvar TeX-engine-alist-builtin
1216 '((default "Default" TeX-command LaTeX-command ConTeXt-engine)
1217 (xetex "XeTeX" "xetex" "xelatex" "xetex")
1218 (luatex "LuaTeX" "luatex" "lualatex" "luatex")
1219 (omega "Omega" TeX-Omega-command LaTeX-Omega-command ConTeXt-Omega-engine))
1220 "Alist of built-in TeX engines and associated commands.
1221 For a description of the format see `TeX-engine-alist'.")
1223 (defcustom TeX-engine-alist nil
1224 "Alist of TeX engines and associated commands.
1225 Each entry is a list with a maximum of five elements. The first
1226 element is a symbol used to identify the engine. The second is a
1227 string describing the engine. The third is the command to be
1228 used for plain TeX. The fourth is the command to be used for
1229 LaTeX. The fifth is the command to be used for the --engine
1230 parameter of ConTeXt's texexec program. Each command can either
1231 be a variable or a string. An empty string or nil means there is
1232 no command available.
1234 You can override a built-in engine defined in the variable
1235 `TeX-engine-alist-builtin' by adding an entry beginning with the
1236 same symbol as the built-in entry to `TeX-engine-alist'."
1238 :type '(repeat (group symbol
1239 (string :tag "Name")
1240 (choice :tag "Plain TeX command" string variable)
1241 (choice :tag "LaTeX command" string variable)
1242 (choice :tag "ConTeXt command" string variable))))
1244 (defun TeX-engine-alist ()
1245 "Return an alist of TeX engines.
1246 The function appends the built-in engine specs from
1247 `TeX-engine-alist-builtin' and the user-defined engines from
1248 `TeX-engine-alist' and deletes any entries from the built-in part
1249 where an entry with the same car exists in the user-defined part."
1250 (TeX-delete-dups-by-car (append TeX-engine-alist TeX-engine-alist-builtin)))
1252 (defcustom TeX-engine 'default
1253 (concat "Type of TeX engine to use.
1254 It should be one of the following symbols:\n\n"
1255 (mapconcat (lambda (x) (format "* `%s'" (car x)))
1256 (TeX-engine-alist) "\n"))
1258 :type `(choice ,@(mapcar (lambda (x)
1259 `(const :tag ,(nth 1 x) ,(car x)))
1260 (TeX-engine-alist))))
1261 (make-variable-buffer-local 'TeX-engine)
1262 (put 'TeX-engine 'safe-local-variable
1263 (lambda (arg) (memq arg (mapcar 'car TeX-engine-alist-builtin))))
1265 (defun TeX-engine-set (type)
1266 (concat "Set TeX engine to TYPE.
1267 TYPE can be one of the following symbols:\n"
1268 (mapconcat (lambda (x) (format "* `%s'" (car x)))
1269 (TeX-engine-alist) "\n"))
1270 (interactive (list (completing-read "Engine: "
1272 (symbol-name (car x)))
1275 (when (stringp type)
1276 (setq type (intern type)))
1277 (setq TeX-engine type)
1278 ;; Automatically enable or disable TeX PDF mode as a convenience
1279 (cond ((eq type 'xetex) (TeX-PDF-mode 1))
1280 ((eq type 'omega) (TeX-PDF-mode 0))))
1282 (define-minor-mode TeX-Omega-mode
1283 "Minor mode for using the Omega engine."
1286 (TeX-engine-set (if TeX-Omega-mode 'omega 'default)))
1287 (defalias 'tex-omega-mode 'TeX-Omega-mode)
1288 (make-obsolete 'TeX-Omega-mode 'TeX-engine-set)
1289 (make-obsolete-variable 'TeX-Omega-mode 'TeX-engine)
1291 ;;; Forward and inverse search
1293 (defcustom TeX-source-correlate-method 'auto
1294 "Method to use for enabling forward and inverse search.
1295 This can be `source-specials' if source specials should be used,
1296 `synctex' if SyncTeX should be used, or`auto' if AUCTeX should
1299 Setting this variable does not take effect if TeX Source
1300 Correlate mode has already been active. Restart Emacs in this
1302 :type '(choice (const auto) (const synctex) (const source-specials))
1305 (defvar TeX-source-correlate-method-active nil
1306 "Method actually used for forward and inverse search.")
1308 (defvar TeX-source-correlate-output-page-function nil
1309 "Symbol of function returning an output page relating to buffer position.
1310 The function should take no arguments and return the page numer
1312 (make-variable-buffer-local 'TeX-source-correlate-output-page-function)
1314 (defcustom TeX-source-correlate-start-server 'ask
1315 "Control if server should be started for inverse search."
1316 :type '(choice (const :tag "Always" t)
1317 (const :tag "Never" nil)
1318 (const :tag "Ask" ask))
1320 (when (fboundp 'defvaralias)
1321 (defvaralias 'TeX-source-specials-view-start-server
1322 'TeX-source-correlate-start-server))
1324 (defvar TeX-source-correlate-start-server-asked nil
1325 "Keep track if question about server start search was asked.")
1327 (defvar TeX-source-correlate-start-server-flag nil
1328 "If non-nil, `TeX-source-correlate-start-server-maybe' will start a server.
1329 Code related to features requiring a server, e.g. for inverse
1330 search, can set the variable.")
1332 (defun TeX-source-correlate-gnuserv-p ()
1333 "Guess whether to use gnuserv when a server is requested."
1334 (cond ((and (boundp 'gnuserv-process)
1335 (processp gnuserv-process)))
1336 ((and (boundp 'server-process)
1337 (processp server-process))
1339 ((featurep 'xemacs))))
1341 (defun TeX-source-correlate-server-enabled-p ()
1342 "Return non-nil if Emacs server or gnuserv is enabled."
1343 (let* ((gnuserv-p (TeX-source-correlate-gnuserv-p))
1344 (process (if gnuserv-p 'gnuserv-process 'server-process)))
1345 (and (boundp process) (processp (symbol-value process)))))
1347 (defun TeX-source-correlate-start-server-maybe ()
1348 "Start Emacs server or gnuserv if a feature using it is enabled.
1349 This is the case if `TeX-source-correlate-start-server-flag' is non-nil."
1350 (when (and TeX-source-correlate-start-server-flag
1351 (not (TeX-source-correlate-server-enabled-p)))
1352 (let* ((gnuserv-p (TeX-source-correlate-gnuserv-p))
1353 (start (if gnuserv-p 'gnuserv-start 'server-start)))
1355 ;; Server should be started unconditionally
1356 ((eq TeX-source-correlate-start-server t)
1358 ;; Ask user if server is to be started
1359 ((and (eq TeX-source-correlate-start-server 'ask)
1360 (not TeX-source-correlate-start-server-asked)
1362 (y-or-n-p (format "Start %s for inverse search in viewer? "
1366 (setq TeX-source-correlate-start-server-asked t)))
1367 (funcall start))))))
1369 (defun TeX-source-correlate-determine-method ()
1370 "Determine which method is available for forward and inverse search."
1371 (let ((help (condition-case nil
1372 (with-output-to-string
1373 (call-process LaTeX-command
1374 nil (list standard-output nil) nil "--help"))
1376 (if (string-match "^[ ]*-synctex" help)
1380 (defun TeX-source-correlate-expand-options ()
1381 "Return TeX engine command line option for forward search facilities.
1382 The return value depends on the value of `TeX-source-correlate-mode'.
1383 If this is nil, an empty string will be returned."
1384 (if TeX-source-correlate-mode
1385 (if (eq TeX-source-correlate-method-active 'source-specials)
1386 (concat TeX-source-specials-tex-flags
1387 (if TeX-source-specials-places
1388 ;; -src-specials=WHERE: insert source specials
1389 ;; in certain places of the DVI file. WHERE is a
1390 ;; comma-separated value list: cr display hbox
1391 ;; math par parend vbox
1392 (concat "=" (mapconcat 'identity
1393 TeX-source-specials-places ","))))
1394 TeX-synctex-tex-flags)
1397 (defvar TeX-source-correlate-map
1398 (let ((map (make-sparse-keymap)))
1399 ;; (if (featurep 'xemacs)
1400 ;; (define-key map [(control button1)] #'TeX-view-mouse)
1401 ;; (define-key map [C-down-mouse-1] #'TeX-view-mouse))
1403 "Keymap for `TeX-source-correlate-mode'.
1404 You could use this for unusual mouse bindings.")
1406 (define-minor-mode TeX-source-correlate-mode
1407 "Minor mode for forward and inverse search.
1409 If enabled, the viewer can be advised to show the output page
1410 corresponding to the point in the source and vice versa.
1412 The method to be used can be controlled with the variable
1413 `TeX-source-correlate-method'. Currently source specials or
1414 SyncTeX are recognized."
1416 ;; Since this is a global minor mode and we don't want to require
1417 ;; tex.el when the mode variable is set, the mode function is called
1418 ;; explicitely (if necessary) in `VirTeX-common-initialization'. We
1419 ;; do it there because otherwise `kill-all-local-variables' would
1420 ;; reset `TeX-source-correlate-output-page-function' which is
1423 (set-keymap-parent TeX-mode-map (and TeX-source-correlate-mode
1424 TeX-source-correlate-map))
1425 (TeX-set-mode-name 'TeX-source-correlate-mode t t)
1426 (setq TeX-source-correlate-start-server-flag TeX-source-correlate-mode)
1427 (unless TeX-source-correlate-method-active
1428 (setq TeX-source-correlate-method-active
1429 (if (eq TeX-source-correlate-method 'auto)
1430 (TeX-source-correlate-determine-method)
1431 TeX-source-correlate-method)))
1432 (when (eq TeX-source-correlate-method-active 'synctex)
1433 (setq TeX-source-correlate-output-page-function
1434 (when TeX-source-correlate-mode
1435 'TeX-synctex-output-page))))
1436 (defalias 'TeX-source-specials-mode 'TeX-source-correlate-mode)
1437 (make-obsolete 'TeX-source-specials-mode 'TeX-source-correlate-mode)
1438 (defalias 'tex-source-correlate-mode 'TeX-source-correlate-mode)
1439 (put 'TeX-source-correlate-mode 'safe-local-variable 'TeX-booleanp)
1440 ;; We do not want the custom variable to require tex.el. This is only
1441 ;; necessary if AUCTeX was compiled with Emacs 21.
1442 (put 'TeX-source-correlate-mode 'custom-requests nil)
1443 (setq minor-mode-map-alist
1444 (delq (assq 'TeX-source-correlate-mode minor-mode-map-alist)
1445 minor-mode-map-alist))
1450 (defcustom TeX-source-specials-tex-flags "-src-specials"
1451 "Extra flags to pass to TeX commands to generate source specials."
1455 (defcustom TeX-source-specials-places nil
1456 "List of places where to insert source specials into the DVI file.
1457 If nil, use (La)TeX's defaults."
1459 :type '(list (set :inline t
1460 ;; :tag "Options known to work"
1461 ;; cr display hbox math par parend vbox
1470 :tag "Other options"
1473 (defcustom TeX-source-specials-view-position-flags
1474 "-sourceposition \"%n %b\""
1475 "Flags to pass to the DVI viewer commands for the position in the source."
1479 (defcustom TeX-source-specials-view-editor-flags
1481 "Flags to pass to DVI viewer commands for inverse search."
1485 (defcustom TeX-source-specials-view-gnuclient-flags
1487 "Flags to pass to gnuclient for inverse search."
1491 (defcustom TeX-source-specials-view-emacsclient-flags
1492 "--no-wait +%%l %%f"
1493 "Flags to emacsclient for inverse search."
1497 ;; FIXME: Make client binaries configurable.
1498 (defun TeX-source-specials-view-expand-client ()
1499 "Return gnuclient or emacslient executable with options.
1500 Return the full path to the executable if possible."
1501 (let* ((gnuserv-p (TeX-source-correlate-gnuserv-p))
1502 (client-base (if gnuserv-p
1505 (client-full (and invocation-directory
1506 (expand-file-name client-base
1507 invocation-directory)))
1508 (options (if gnuserv-p
1509 TeX-source-specials-view-gnuclient-flags
1510 TeX-source-specials-view-emacsclient-flags)))
1511 (if (and client-full (file-executable-p client-full))
1512 (concat client-full " " options)
1513 (concat client-base " " options))))
1515 (defun TeX-source-specials-view-expand-options (&optional viewer)
1516 "Return source specials command line option for viewer command.
1517 The return value depends on the values of
1518 `TeX-source-correlate-mode' and
1519 `TeX-source-correlate-method-active'. If those are nil or not
1520 `source-specials' respectively, an empty string will be
1522 (if (and TeX-source-correlate-mode
1523 (eq TeX-source-correlate-method-active 'source-specials))
1524 (concat TeX-source-specials-view-position-flags
1525 (when (TeX-source-correlate-server-enabled-p)
1526 (concat " " TeX-source-specials-view-editor-flags)))
1531 (defvar TeX-synctex-tex-flags "--synctex=1"
1532 "Extra flags to pass to TeX commands to enable SyncTeX.")
1534 (defun TeX-synctex-output-page ()
1535 "Return the page corresponding to the current source position.
1536 This method assumes that the document was compiled with SyncTeX
1537 enabled and the `synctex' binary is available."
1538 (let ((synctex-output
1539 (with-output-to-string
1540 (call-process "synctex" nil (list standard-output nil) nil "view"
1541 "-i" (format "%s:%s:%s" (line-number-at-pos)
1543 ;; The file name relative to the
1544 ;; directory of the master file.
1547 (file-name-directory
1548 (TeX-active-master))))
1549 "-o" (TeX-active-master (TeX-output-extension))))))
1550 (when (string-match "Page:\\([0-9]+\\)" synctex-output)
1551 (match-string 1 synctex-output))))
1553 ;;; Miscellaneous minor modes
1555 (defvar TeX-mode-p nil
1556 "This indicates a TeX mode being active.")
1557 (make-variable-buffer-local 'TeX-mode-p)
1559 (defun TeX-mode-set (var value)
1560 (set-default var value)
1561 (TeX-set-mode-name var nil t))
1563 (defcustom TeX-PDF-mode nil nil
1567 (put 'TeX-PDF-mode 'safe-local-variable 'TeX-booleanp)
1569 (define-minor-mode TeX-PDF-mode
1570 "Minor mode for using PDFTeX.
1572 If enabled, PDFTeX will be used as an executable by default.
1573 You can customize an initial value, and you can use the
1574 function `TeX-global-PDF-mode' for toggling this value."
1576 (when (eq TeX-engine 'omega)
1577 (setq TeX-PDF-mode nil))
1578 (setq TeX-PDF-mode-parsed nil)
1579 (TeX-set-mode-name nil nil t)
1580 (setq TeX-output-extension
1581 (if TeX-PDF-mode "pdf" "dvi")))
1582 (add-to-list 'minor-mode-alist '(TeX-PDF-mode ""))
1584 (defun TeX-global-PDF-mode (&optional arg)
1585 "Toggle default for `TeX-PDF-mode'."
1588 (setq-default TeX-PDF-mode
1589 (if arg (> (prefix-numeric-value arg) 0)
1590 (not (default-value 'TeX-PDF-mode))))
1591 (TeX-set-mode-name 'TeX-PDF-mode nil t)))
1593 (defalias 'tex-pdf-mode 'TeX-PDF-mode)
1595 (defvar TeX-PDF-mode-parsed nil
1596 "Set if `TeX-PDF-mode' has come about by parsing.")
1598 (make-variable-buffer-local 'TeX-PDF-mode-parsed)
1600 (defun TeX-PDF-mode-parsed (arg)
1601 "Change `TeX-PDF-mode' to ARG based on parsing.
1602 If this conflicts with previous parsed settings,
1603 just use the default. If an explicit setting is
1604 already established, don't do anything."
1606 ;; Basically we have the following situations:
1607 ;; TeX-PDF-mode-parsed (local-variable-p 'TeX-PDF-mode):
1608 ;; nil nil : virgin state
1609 ;; nil t : stably set state (possibly because of conflicting parse info)
1610 ;; t t : non-conflicting parsed info
1612 (if TeX-PDF-mode-parsed
1613 (unless (eq TeX-PDF-mode arg)
1614 (TeX-PDF-mode (if (default-value 'TeX-PDF-mode) 1 0)))
1615 (unless (local-variable-p 'TeX-PDF-mode (current-buffer))
1616 (TeX-PDF-mode (if arg 1 0))
1617 (setq TeX-PDF-mode-parsed t))))
1619 (defun TeX-PDF-mode-on ()
1620 "Use only from parsing routines."
1621 (TeX-PDF-mode-parsed t))
1623 (defun TeX-PDF-mode-off ()
1624 "Use only from parsing routines."
1625 (TeX-PDF-mode-parsed nil))
1627 (defcustom TeX-DVI-via-PDFTeX nil
1628 "Whether to use PDFTeX also for producing DVI files."
1632 (define-minor-mode TeX-interactive-mode
1633 "Minor mode for interactive runs of TeX."
1636 (TeX-set-mode-name 'TeX-interactive-mode t t))
1637 (defalias 'tex-interactive-mode 'TeX-interactive-mode)
1638 (add-to-list 'minor-mode-alist '(TeX-interactive-mode ""))
1642 (defgroup TeX-command-name nil
1643 "Names for external commands in AUCTeX."
1644 :group 'TeX-command)
1646 (defcustom TeX-command-BibTeX "BibTeX"
1647 "*The name of the BibTeX entry in `TeX-command-list'."
1648 :group 'TeX-command-name
1650 (make-variable-buffer-local 'TeX-command-BibTeX)
1652 (defcustom TeX-command-Show "View"
1653 "*The default command to show (view or print) a TeX file.
1654 Must be the car of an entry in `TeX-command-list'."
1655 :group 'TeX-command-name
1657 (make-variable-buffer-local 'TeX-command-Show)
1659 (defcustom TeX-command-Print "Print"
1660 "The name of the Print entry in `TeX-command-Print'."
1661 :group 'TeX-command-name
1664 (defcustom TeX-command-Queue "Queue"
1665 "The name of the Queue entry in `TeX-command-Queue'."
1666 :group 'TeX-command-name
1669 (defvar TeX-trailer-start nil
1670 "Regular expression delimiting start of trailer in a TeX file.")
1672 (make-variable-buffer-local 'TeX-trailer-start)
1674 (defvar TeX-header-end nil
1675 "Regular expression delimiting end of header in a TeX file.")
1677 (make-variable-buffer-local 'TeX-header-end)
1679 (defvar TeX-command-default nil
1680 "The default command for `TeX-command' in the current major mode.")
1682 (make-variable-buffer-local 'TeX-command-default)
1684 (put 'TeX-command-default 'safe-local-variable 'stringp)
1686 (defvar TeX-clean-default-intermediate-suffixes
1687 '("\\.aux" "\\.bbl" "\\.blg" "\\.brf" "\\.fot"
1688 "\\.glo" "\\.gls" "\\.idx" "\\.ilg" "\\.ind"
1689 "\\.lof" "\\.log" "\\.lot" "\\.nav" "\\.out"
1690 "\\.snm" "\\.toc" "\\.url" "\\.synctex\\.gz")
1691 "List of regexps matching suffixes of files to be cleaned.
1692 Used as a default in TeX, LaTeX and docTeX mode.")
1694 (defvar TeX-clean-default-output-suffixes
1695 '("\\.dvi" "\\.pdf" "\\.ps" "\\.xdv")
1696 "List of regexps matching suffixes of files to be cleaned.
1697 Used as a default in TeX, LaTeX and docTeX mode.")
1699 (defcustom TeX-clean-confirm t
1700 "If non-nil, ask before deleting files."
1702 :group 'TeX-command)
1704 (autoload 'dired-mark-pop-up "dired")
1706 (defun TeX-clean (&optional arg)
1707 "Delete generated files associated with current master and region files.
1708 If prefix ARG is non-nil, not only remove intermediate but also
1711 (let* ((mode-prefix (TeX-mode-prefix))
1712 (suffixes (append (symbol-value
1713 (intern (concat mode-prefix
1714 "-clean-intermediate-suffixes")))
1717 (intern (concat mode-prefix
1718 "-clean-output-suffixes"))))))
1719 (master (TeX-active-master))
1720 (master-dir (file-name-directory master))
1721 (regexp (concat "\\("
1722 (file-name-nondirectory master) "\\|"
1723 (TeX-region-file nil t)
1726 (mapconcat 'identity suffixes "\\|")
1728 "\\|" (TeX-region-file t t)))
1730 (directory-files (or master-dir ".") nil regexp))))
1732 (when (or (not TeX-clean-confirm)
1734 (dired-mark-pop-up " *Deletions*" 'delete
1735 (if (> (length files) 1)
1738 'y-or-n-p "Delete files? ")
1739 (wrong-type-argument ; e.g. with Emacs 21
1740 (y-or-n-p (format "Delete %S? " (car files))))))
1741 (dolist (file files)
1742 (delete-file (concat master-dir file))))
1743 (message "No files to be deleted"))))
1748 (defcustom TeX-master t
1749 "*The master file associated with the current buffer.
1750 If the file being edited is actually included from another file, you
1751 can tell AUCTeX the name of the master file by setting this variable.
1752 If there are multiple levels of nesting, specify the top level file.
1754 If this variable is nil, AUCTeX will query you for the name.
1756 If the variable is t, AUCTeX will assume the file is a master file
1759 If the variable is 'shared, AUCTeX will query for the name, but not
1762 If the variable is 'dwim, AUCTeX will try to avoid querying by
1763 attempting to `do what I mean'; and then change the file.
1765 It is suggested that you use the File Variables (see the info node in
1766 the Emacs manual) to set this variable permanently for each file."
1769 :type '(choice (const :tag "Query" nil)
1770 (const :tag "This file" t)
1771 (const :tag "Shared" shared)
1772 (const :tag "Dwim" dwim)
1773 (string :format "%v")))
1774 (make-variable-buffer-local 'TeX-master)
1775 (put 'TeX-master 'safe-local-variable
1778 (member x (quote (t nil shared dwim))))))
1780 (defcustom TeX-one-master "\\.\\(texi?\\|dtx\\)$"
1781 "*Regular expression matching ordinary TeX files.
1783 You should set this variable to match the name of all files, where
1784 automatically adding a file variable with the name of the master file
1785 is a good idea. When AUCTeX adds the name of the master file as a
1786 file variable, it does not need to ask next time you edit the file.
1788 If you dislike AUCTeX automatically modifying your files, you can set
1789 this variable to \"<none>\"."
1793 (defvar TeX-convert-master t
1794 "*If not nil, automatically convert ``Master:'' lines to file variables.
1795 This will be done when AUCTeX first try to use the master file.")
1797 ;; Can be let-bound temporarily in order to inhibit the master file question
1798 ;; by using its value instead in case `TeX-master' is nil or 'shared.
1799 (defvar TeX-transient-master nil)
1801 (defun TeX-dwim-master ()
1802 "Find a likely `TeX-master'."
1803 (let ((dir default-directory))
1804 (dolist (buf (buffer-list))
1805 (when (with-current-buffer buf
1806 (and (equal dir default-directory)
1807 (stringp TeX-master)))
1808 (return (with-current-buffer buf TeX-master))))))
1810 (defun TeX-master-file-ask ()
1811 "Ask for master file, set `TeX-master' and add local variables."
1813 (if (TeX-local-master-p)
1814 (error "Master file already set")
1815 (let* ((default (TeX-dwim-master))
1816 (name (or (and (eq 'dwim TeX-master) default)
1818 (read-file-name (format "Master file: (default %s) "
1819 (or default "this file"))
1822 (cond ((string= name "<quit>")
1823 (setq TeX-master t))
1824 ((string= name default)
1825 (setq TeX-master default)
1826 (TeX-add-local-master))
1828 ;; Default `read-file-name' proposes and buffer visits a file.
1829 (string= (expand-file-name name) (buffer-file-name))
1830 ;; Default of `read-file-name' and buffer does not visit a file.
1831 (string= name default-directory)
1832 ;; User typed <RET> in an empty minibuffer.
1835 (TeX-add-local-master))
1837 (setq TeX-master (TeX-strip-extension (file-relative-name name)
1838 (list TeX-default-extension)
1840 (TeX-add-local-master))))))
1842 (defun TeX-master-file (&optional extension nondirectory ask)
1843 "Set and return the name of the master file for the current document.
1845 If optional argument EXTENSION is non-nil, add that file extension to
1846 the name. Special value t means use `TeX-default-extension'.
1848 If optional second argument NONDIRECTORY is non-nil, do not include
1851 If optional third argument ASK is non-nil, ask the user for the
1852 name of master file if it cannot be determined otherwise.
1854 Currently it will check for the presence of a ``Master:'' line in
1855 the beginning of the file, but that feature will be phased out."
1857 (if (eq extension t)
1858 (setq extension TeX-default-extension))
1859 (let ((my-name (if (buffer-file-name)
1860 (TeX-strip-extension nil (list TeX-default-extension) t)
1865 (goto-char (point-min))
1867 ((and TeX-transient-master
1868 (or (not TeX-master) (eq TeX-master 'shared)))
1869 (setq TeX-master TeX-transient-master))
1870 ;; Special value 't means it is own master (a free file).
1871 ((equal TeX-master my-name)
1872 (setq TeX-master t))
1874 ;; For files shared between many documents.
1875 ((and (eq 'shared TeX-master) ask)
1877 (let* ((default (TeX-dwim-master))
1878 (name (read-file-name
1879 (format "Master file: (default %s) "
1880 (or default "this file"))
1882 (cond ((string= name default)
1885 ;; Default `read-file-name' proposes and
1886 ;; buffer visits a file.
1887 (string= (expand-file-name name)
1889 ;; Default of `read-file-name' and
1890 ;; buffer does not visit a file.
1891 (string= name default-directory)
1892 ;; User typed <RET> in an empty minibuffer.
1896 (TeX-strip-extension
1897 name (list TeX-default-extension) 'path))))))
1899 ;; We might already know the name.
1900 ((or (eq TeX-master t) (stringp TeX-master)) TeX-master)
1902 ;; Support the ``Master:'' line (under protest!)
1904 "^%% *[Mm]aster:?[ \t]*\\([^ \t\n]+\\)" 500 t)
1906 (TeX-strip-extension (TeX-match-buffer 1)
1907 (list TeX-default-extension)))
1908 (if TeX-convert-master
1912 (TeX-add-local-master))))
1914 ;; Ask the user (but add it as a local variable).
1915 (ask (TeX-master-file-ask)))))
1917 (let ((name (if (stringp TeX-master)
1921 (if (TeX-match-extension name)
1922 ;; If it already has an extension...
1923 (if (equal extension TeX-default-extension)
1924 ;; Use instead of the default extension
1925 (setq extension nil)
1926 ;; Otherwise drop it.
1927 (setq name (TeX-strip-extension name))))
1929 ;; Remove directory if needed.
1931 (setq name (file-name-nondirectory name)))
1934 (concat name "." extension)
1937 (defun TeX-master-directory ()
1938 "Directory of master file."
1939 (file-name-as-directory
1940 (abbreviate-file-name
1941 (substitute-in-file-name
1943 (let ((dir (file-name-directory (TeX-master-file))))
1944 (if dir (directory-file-name dir) "."))
1945 (and buffer-file-name
1946 (file-name-directory buffer-file-name)))))))
1948 (defun TeX-add-local-master ()
1949 "Add local variable for `TeX-master'."
1950 (when (and (buffer-file-name)
1951 (string-match TeX-one-master
1952 (file-name-nondirectory (buffer-file-name)))
1953 (not buffer-read-only))
1954 (goto-char (point-max))
1955 (if (re-search-backward (concat "^\\([^\n]+\\)Local " "Variables:")
1956 (- (point-max) 3000) t)
1957 (let ((prefix (TeX-match-buffer 1)))
1958 (re-search-forward (regexp-quote (concat prefix
1960 (beginning-of-line 1)
1961 (insert prefix "TeX-master: " (prin1-to-string TeX-master) "\n"))
1962 (let ((comment-prefix (cond ((eq major-mode 'texinfo-mode) "@c ")
1963 ((eq major-mode 'doctex-mode) "% ")
1965 (mode (concat (and (boundp 'japanese-TeX-mode) japanese-TeX-mode
1967 (substring (symbol-name major-mode) 0 -5))))
1969 (when (eq major-mode 'doctex-mode)
1970 (insert comment-prefix TeX-esc "endinput\n"))
1972 comment-prefix "Local " "Variables: \n"
1973 comment-prefix "mode: " mode "\n"
1974 comment-prefix "TeX-master: " (prin1-to-string TeX-master) "\n"
1975 comment-prefix "End: \n")))))
1977 (defun TeX-local-master-p ()
1978 "Return non-nil if there is a `TeX-master' entry in local variables spec.
1979 Return nil otherwise."
1981 ;; XXX: Checking -*- line necessary as well?
1982 (goto-char (point-max))
1983 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
1984 (re-search-forward "^%+ *TeX-master:" nil t)))
1988 (defcustom TeX-style-global (expand-file-name "style" TeX-data-directory)
1989 "*Directory containing hand generated TeX information.
1991 These correspond to TeX macros shared by all users of a site."
1995 (defcustom TeX-auto-local "auto"
1996 "*Directory containing automatically generated TeX information.
1998 This correspond to TeX macros found in the current directory, and must
1999 be relative to that."
2003 (defcustom TeX-style-local "style"
2004 "*Directory containing hand generated TeX information.
2006 These correspond to TeX macros found in the current directory, and must
2007 be relative to that."
2011 (defun TeX-split-string (regexp string)
2012 "Return a list of strings.
2013 Given REGEXP the STRING is split into sections which in string was
2014 seperated by REGEXP.
2018 (TeX-split-string \"\:\" \"abc:def:ghi\")
2019 -> (\"abc\" \"def\" \"ghi\")
2021 (TeX-split-string \" +\" \"dvips -Plw -p3 -c4 testfile.dvi\")
2023 -> (\"dvips\" \"-Plw\" \"-p3\" \"-c4\" \"testfile.dvi\")
2025 If REGEXP is nil, or \"\", an error will occur."
2027 (let ((start 0) result match)
2028 (while (setq match (string-match regexp string start))
2029 (push (substring string start match) result)
2030 (setq start (match-end 0)))
2031 (push (substring string start) result)
2034 (defun TeX-parse-path (env)
2035 "Return a list if private TeX directories found in environment variable ENV."
2036 (let* ((value (getenv env))
2039 (if (string-match ";" value) ";" ":")
2044 (setq entry (car entries))
2045 (setq entries (cdr entries))
2046 (setq entry (file-name-as-directory
2047 (if (string-match "/?/?\\'" entry)
2048 (substring entry 0 (match-beginning 0))
2050 (or (not (file-name-absolute-p entry))
2051 (member entry (append '("/" "\\") TeX-macro-global))
2052 (setq answers (cons entry answers))))
2055 (defun TeX-macro-global ()
2056 "Return directories containing the site's TeX macro and style files."
2057 (or (TeX-tree-expand '("$SYSTEXMF" "$TEXMFLOCAL" "$TEXMFMAIN" "$TEXMFDIST")
2058 "latex" '("/tex/" "/bibtex/bst/"))
2059 '("/usr/share/texmf/tex/" "/usr/share/texmf/bibtex/bst/")))
2061 (defun TeX-macro-private ()
2062 "Return directories containing the user's TeX macro and style files."
2063 (TeX-tree-expand '("$TEXMFHOME") "latex" '("/tex/" "/bibtex/bst/")))
2065 (defun TeX-tree-expand (trees program subdirs)
2066 "Return directories corresponding to the TeX trees TREES.
2067 This is done calling `kpsewhich' where PROGRAM is passed as the
2068 parameter for --progname. SUBDIRS are subdirectories which are
2069 appended to the directories of the TeX trees."
2070 (let (path-list path exit-status input-dir-list)
2073 (dotimes (i (safe-length trees))
2074 (setq path (with-output-to-string
2078 (list standard-output nil) nil
2079 "--progname" program
2080 "--expand-braces" (nth i trees)))))
2081 (if (zerop exit-status)
2082 (progn (add-to-list 'path-list path)
2083 (when (zerop i) (throw 'success nil)))
2084 (setq path (with-output-to-string
2088 (list standard-output nil) nil
2089 "--progname" program
2090 "--expand-path" (nth i trees)))))
2091 (when (zerop exit-status) (add-to-list 'path-list path)))))
2093 (dolist (elt path-list)
2094 (let ((separators (if (string-match "^[A-Za-z]:" elt)
2097 (dolist (item (condition-case nil
2098 (split-string elt separators t)
2099 ;; COMPATIBILITY for XEmacs <= 21.4.15
2100 (error (delete "" (split-string elt separators)))))
2101 (when (string-match "^!+" item)
2102 (setq item (substring item (match-end 0) (length item))))
2103 (when (string-match "/+$" item)
2104 (setq item (substring item 0 (match-beginning 0))))
2105 (dolist (subdir subdirs)
2106 (when (file-exists-p (file-name-as-directory (concat item subdir)))
2107 (add-to-list 'input-dir-list (concat item subdir)))))))
2110 (defcustom TeX-macro-global (TeX-macro-global)
2111 "Directories containing the site's TeX macro and style files."
2113 :type '(repeat (directory :format "%v")))
2115 (defcustom TeX-macro-private (or (append (TeX-parse-path "TEXINPUTS")
2116 (TeX-parse-path "BIBINPUTS"))
2117 (TeX-macro-private))
2118 "Directories where you store your personal TeX macros."
2120 :type '(repeat (file :format "%v")))
2122 (defcustom TeX-auto-private
2123 (list (expand-file-name TeX-auto-local
2124 (or (and (boundp 'user-emacs-directory)
2125 (concat user-emacs-directory "auctex/"))
2126 "~/.emacs.d/auctex/")))
2127 "List of directories containing automatically generated AUCTeX style files.
2129 These correspond to the personal TeX macros."
2131 :type '(repeat (file :format "%v")))
2133 (if (stringp TeX-auto-private) ;Backward compatibility
2134 (setq TeX-auto-private (list TeX-auto-private)))
2136 (defcustom TeX-style-private
2137 (list (expand-file-name TeX-style-local
2138 (or (and (boundp 'user-emacs-directory)
2139 (concat user-emacs-directory "auctex/"))
2140 "~/.emacs.d/auctex/")))
2141 "List of directories containing hand-generated AUCTeX style files.
2143 These correspond to the personal TeX macros."
2145 :type '(repeat (file :format "%v")))
2147 (if (stringp TeX-style-private) ;Backward compatibility
2148 (setq TeX-style-private (list TeX-style-private)))
2150 (defcustom TeX-style-path
2152 ;; Put directories in an order where the more local files can
2153 ;; override the more global ones.
2154 (mapcar (lambda (file) (when file (add-to-list 'path file t)))
2155 (append (list TeX-auto-global TeX-style-global)
2156 TeX-auto-private TeX-style-private
2157 (list TeX-auto-local TeX-style-local)))
2159 "List of directories to search for AUCTeX style files.
2160 Per default the list is built from the values of the variables
2161 `TeX-auto-global', `TeX-style-global', `TeX-auto-private',
2162 `TeX-style-private', `TeX-auto-local', and `TeX-style-local'."
2164 :type '(repeat (file :format "%v")))
2166 (defcustom TeX-check-path
2167 (append (list ".") TeX-macro-private TeX-macro-global)
2168 "Directory path to search for dependencies.
2170 If nil, just check the current file.
2171 Used when checking if any files have changed."
2173 :type '(repeat (file :format "%v")))
2177 (defvar TeX-style-hook-list nil
2178 "List of TeX style hooks currently loaded.
2180 Each entry is a list where the first element is the name of the style,
2181 and the remaining elements are hooks to be run when that style is
2184 (defcustom TeX-byte-compile nil
2185 "*Not nil means try to byte compile auto files before loading."
2189 (defun TeX-load-style (style)
2190 "Search for and load each definition for STYLE in `TeX-style-path'."
2191 (cond ((assoc style TeX-style-hook-list)) ; We already found it
2192 ((string-match "\\`\\(.+[/\\]\\)\\([^/\\]*\\)\\'" style) ;Complex path
2193 (let* ((dir (substring style (match-beginning 1) (match-end 1)))
2194 (style (substring style (match-beginning 2) (match-end 2)))
2195 (master-dir (if (stringp TeX-master)
2196 (file-name-directory
2197 (file-relative-name TeX-master))
2199 (TeX-style-path (append (list (expand-file-name
2202 TeX-auto-local master-dir)
2204 TeX-style-local dir)
2206 TeX-style-local master-dir))
2208 (TeX-load-style style)))
2210 ;; Insert empty list to mark the fact that we have searched.
2211 (setq TeX-style-hook-list (cons (list style) TeX-style-hook-list))
2212 ;; Now check each element of the path
2213 (dolist (name TeX-style-path)
2214 (TeX-load-style-file (expand-file-name style name))))))
2216 (defun TeX-load-style-file (file)
2217 "Load FILE checking for a Lisp extensions."
2218 (let ((el (concat file ".el"))
2219 (elc (concat file ".elc")))
2220 (cond ((file-newer-than-file-p el elc)
2221 (if (file-readable-p el)
2222 (if (and TeX-byte-compile
2223 (file-writable-p elc)
2225 ;; `byte-compile-file' switches buffer in Emacs 20.3.
2226 (byte-compile-file el))
2227 (file-readable-p elc))
2230 ((file-readable-p elc)
2232 ((file-readable-p el)
2235 (defun TeX-add-style-hook (style hook)
2236 "Give STYLE yet another HOOK to run."
2237 (let ((entry (assoc style TeX-style-hook-list)))
2239 ;; New style, add entry.
2240 (setq TeX-style-hook-list (cons (list style hook)
2241 TeX-style-hook-list)))
2242 ((member hook entry)
2243 ;; Old style, hook already there, do nothing.
2246 ;; Old style, new hook.
2247 (setcdr entry (cons hook (cdr entry)))))))
2249 (defun TeX-unload-style (style)
2250 "Forget that we once loaded STYLE."
2251 (cond ((null (assoc style TeX-style-hook-list)))
2252 ((equal (car (car TeX-style-hook-list)) style)
2253 (setq TeX-style-hook-list (cdr TeX-style-hook-list)))
2255 (let ((entry TeX-style-hook-list))
2256 (while (not (equal (car (car (cdr entry))) style))
2257 (setq entry (cdr entry)))
2258 (setcdr entry (cdr (cdr entry)))))))
2260 (defcustom TeX-virgin-style (if (and TeX-auto-global
2261 (file-directory-p TeX-auto-global))
2264 "Style all documents use."
2268 (defvar TeX-active-styles nil
2269 "List of styles currently active in the document.")
2270 (make-variable-buffer-local 'TeX-active-styles)
2272 (defun TeX-run-style-hooks (&rest styles)
2273 "Run the TeX style hooks STYLES."
2274 (mapcar (lambda (style)
2276 (unless (TeX-member style TeX-active-styles 'string-equal)
2277 (setq TeX-active-styles
2278 (cons style TeX-active-styles))
2279 (TeX-load-style style)
2280 (let ((default-directory default-directory))
2282 (when (string-match "\\`\\(.+[/\\]\\)\\([^/\\]*\\)\\'" style)
2283 ;; Set `default-directory' to directory of master
2284 ;; file since style files not stored in the fixed
2285 ;; style directories are usually located there.
2286 (setq default-directory (save-match-data
2287 (TeX-master-directory))
2288 style (substring style
2289 (match-beginning 2) (match-end 2))))
2291 (cdr-safe (assoc style TeX-style-hook-list))))))
2294 (defcustom TeX-parse-self nil
2295 "Parse file after loading it if no style hook is found for it."
2299 (defvar TeX-style-hook-applied-p nil
2300 "Nil, unless the style specific hooks have been applied.")
2301 (make-variable-buffer-local 'TeX-style-hook-applied-p)
2303 (defvar TeX-update-style-hook nil
2304 "Hook run as soon as style specific hooks were applied.")
2306 (defun TeX-update-style (&optional force)
2307 "Run style specific hooks for the current document.
2309 Only do this if it has not been done before, or if optional argument
2311 (unless (or (and (boundp 'TeX-auto-update)
2312 (eq TeX-auto-update 'BibTeX)) ; Not a real TeX buffer
2314 TeX-style-hook-applied-p))
2315 (setq TeX-style-hook-applied-p t)
2316 (message "Applying style hooks...")
2317 (TeX-run-style-hooks (TeX-strip-extension nil nil t))
2318 ;; Run parent style hooks if it has a single parent that isn't itself.
2319 (if (or (not (memq TeX-master '(nil t)))
2320 (and (buffer-file-name)
2321 (string-match TeX-one-master
2322 (file-name-nondirectory (buffer-file-name)))))
2323 (TeX-run-style-hooks (TeX-master-file)))
2324 (if (and TeX-parse-self
2325 (null (cdr-safe (assoc (TeX-strip-extension nil nil t)
2326 TeX-style-hook-list))))
2328 (run-hooks 'TeX-update-style-hook)
2329 (message "Applying style hooks... done")))
2331 (defvar TeX-remove-style-hook nil
2332 "List of hooks to call when we remove the style specific information.")
2333 (make-variable-buffer-local 'TeX-remove-style-hook)
2335 (defun TeX-remove-style ()
2336 "Remove all style specific information."
2337 (setq TeX-style-hook-applied-p nil)
2338 (run-hooks 'TeX-remove-style-hook)
2339 (setq TeX-active-styles (list TeX-virgin-style)))
2341 (defun TeX-style-list ()
2342 "Return a list of all styles (subfiles) used by the current document."
2346 ;;; Special Characters
2348 (defvar TeX-esc "\\" "The TeX escape character.")
2349 (make-variable-buffer-local 'TeX-esc)
2351 (defvar TeX-grop "{" "The TeX group opening character.")
2352 (make-variable-buffer-local 'TeX-grop)
2354 (defvar TeX-grcl "}" "The TeX group closing character.")
2355 (make-variable-buffer-local 'TeX-grcl)
2357 (defcustom plain-TeX-enable-toolbar t
2358 "Enable TeX tool bar in plain TeX mode."
2359 :group 'TeX-tool-bar
2362 (defun plain-TeX-maybe-install-toolbar ()
2363 "Conditionally install tool bar buttons for plain TeX mode.
2364 Install tool bar if `plain-TeX-enable-toolbar' is non-nil."
2365 (when plain-TeX-enable-toolbar
2366 ;; Defined in `tex-bar.el':
2367 (TeX-install-toolbar)))
2371 ;; Must be before keymaps.
2373 (defgroup TeX-macro nil
2374 "Support for TeX macros in AUCTeX."
2378 (defcustom TeX-complete-word 'ispell-complete-word
2379 "*Function to call for completing non-macros in `tex-mode'."
2382 (defvar TeX-complete-list nil
2383 "List of ways to complete the preceding text.
2385 Each entry is a list with the following elements:
2387 0. Regexp matching the preceding text.
2388 1. A number indicating the subgroup in the regexp containing the text.
2389 2. A function returning an alist of possible completions.
2390 3. Text to append after a succesful completion.
2394 0. Regexp matching the preceding text.
2395 1. Function to do the actual completion.")
2397 (defun TeX-complete-symbol ()
2398 "Perform completion on TeX/LaTeX symbol preceding point."
2400 (let ((list TeX-complete-list)
2403 (setq entry (car list)
2405 (if (TeX-looking-at-backward (car entry) 250)
2407 (if (numberp (nth 1 entry))
2408 (let* ((sub (nth 1 entry))
2409 (close (nth 3 entry))
2410 (begin (match-beginning sub))
2411 (end (match-end sub))
2412 (pattern (TeX-match-buffer 0))
2413 (symbol (buffer-substring begin end))
2414 (list (funcall (nth 2 entry)))
2415 (completion (try-completion symbol list)))
2416 (cond ((eq completion t)
2418 (not (looking-at (regexp-quote close)))
2421 (error "Can't find completion for \"%s\"" pattern))
2422 ((not (string-equal symbol completion))
2423 (delete-region begin end)
2426 (eq (try-completion completion list) t)
2427 (not (looking-at (regexp-quote close)))
2430 (message "Making completion list...")
2431 (let ((list (all-completions symbol list nil)))
2432 (with-output-to-temp-buffer "*Completions*"
2433 (display-completion-list list)))
2434 (message "Making completion list...done"))))
2435 (funcall (nth 1 entry)))))
2437 (defcustom TeX-default-macro "ref"
2438 "*The default macro when creating new ones with `TeX-insert-macro'."
2442 (make-variable-buffer-local 'TeX-default-macro)
2444 (defcustom TeX-insert-braces t
2445 "*If non-nil, append a empty pair of braces after inserting a macro."
2449 (defcustom TeX-insert-macro-default-style 'show-optional-args
2450 "Specifies whether `TeX-insert-macro' will ask for all optional arguments.
2452 If set to the symbol `show-optional-args', `TeX-insert-macro' asks for
2453 optional arguments of TeX marcos. If set to `mandatory-args-only',
2454 `TeX-insert-macro' asks only for mandatory argument.
2456 When `TeX-insert-macro' is called with \\[universal-argument], it's the other
2459 Note that for some macros, there are special mechanisms, see e.g.
2460 `LaTeX-includegraphics-options-alist'."
2462 :type '(choice (const mandatory-args-only)
2463 (const show-optional-args)))
2465 (defvar TeX-arg-opening-brace nil
2466 "String used as an opening brace for argument insertion.
2467 The variable will be temporarily let-bound with the necessary value.")
2469 (defvar TeX-arg-closing-brace nil
2470 "String used as a closing brace for argument insertion.
2471 The variable will be temporarily let-bound with the necessary value.")
2473 (defvar TeX-after-insert-macro-hook nil
2474 "A hook run after `TeX-insert-macro'.")
2476 (defvar TeX-macro-history nil)
2478 (defun TeX-insert-macro (symbol)
2479 "Insert TeX macro SYMBOL with completion.
2481 AUCTeX knows of some macros and may query for extra arguments, depending on
2482 the value of `TeX-insert-macro-default-style' and whether `TeX-insert-macro'
2483 is called with \\[universal-argument]."
2484 ;; When called with a prefix (C-u), only ask for mandatory arguments,
2485 ;; i.e. all optional arguments are skipped. See `TeX-parse-arguments' for
2486 ;; details. Note that this behavior may be changed in favor of a more
2487 ;; flexible solution in the future, therefore we don't document it at the
2489 (interactive (list (completing-read (concat "Macro (default "
2493 (TeX-symbol-list) nil nil nil
2494 'TeX-macro-history)))
2495 (cond ((string-equal symbol "")
2496 (setq symbol TeX-default-macro))
2498 (setq TeX-default-macro symbol)))
2499 (TeX-parse-macro symbol (cdr-safe (assoc symbol (TeX-symbol-list))))
2500 (run-hooks 'TeX-after-insert-macro-hook))
2502 (defvar TeX-electric-macro-map
2503 (let ((map (make-sparse-keymap)))
2504 (set-keymap-parent map minibuffer-local-completion-map)
2505 (define-key map " " 'minibuffer-complete-and-exit)
2508 (defun TeX-electric-macro ()
2509 "Insert TeX macro with completion.
2511 AUCTeX knows of some macros, and may query for extra arguments.
2512 Space will complete and exit."
2514 (cond ((eq (preceding-char) ?\\)
2515 (call-interactively 'self-insert-command))
2516 ((eq (preceding-char) ?.)
2517 (let ((TeX-default-macro " ")
2518 (minibuffer-local-completion-map TeX-electric-macro-map))
2519 (call-interactively 'TeX-insert-macro)))
2521 (let ((minibuffer-local-completion-map TeX-electric-macro-map))
2522 (call-interactively 'TeX-insert-macro)))))
2524 (defun TeX-parse-macro (symbol args)
2525 "How to parse TeX macros which takes one or more arguments.
2527 First argument SYMBOL is the name of the macro.
2529 If called with no additional arguments, insert macro with point
2530 inside braces. Otherwise, each argument of this function should
2531 match an argument to the TeX macro. What is done depend on the
2534 string: Use the string as a prompt to prompt for the argument.
2536 number: Insert that many braces, leave point inside the first.
2538 nil: Insert empty braces.
2540 t: Insert empty braces, leave point between the braces.
2542 other symbols: Call the symbol as a function. You can define
2543 your own hook, or use one of the predefined argument hooks. If
2544 you add new hooks, you can assume that point is placed directly
2545 after the previous argument, or after the macro name if this is
2546 the first argument. Please leave point located after the
2547 argument you are inserting. If you want point to be located
2548 somewhere else after all hooks have been processed, set the value
2549 of `exit-mark'. It will point nowhere, until the argument hook
2550 set it. By convention, these hooks all start with `TeX-arg-'.
2552 list: If the car is a string, insert it as a prompt and the next
2553 element as initial input. Otherwise, call the car of the list
2554 with the remaining elements as arguments.
2556 vector: Optional argument. If it has more than one element,
2557 parse it as a list, otherwise parse the only element as above.
2558 Use square brackets instead of curly braces, and is not inserted
2559 on empty user input."
2561 (if (and (TeX-active-mark)
2563 (exchange-point-and-mark))
2564 (insert TeX-esc symbol)
2565 (let ((exit-mark (make-marker))
2567 (TeX-parse-arguments args)
2568 (cond ((marker-position exit-mark)
2569 (goto-char (marker-position exit-mark))
2570 (set-marker exit-mark nil))
2571 ((and TeX-insert-braces
2572 ;; Do not add braces for macros defined as `("foo" 0)'
2573 (not (and (= (safe-length args) 1)
2574 (numberp (car args))
2576 (equal position (point))
2577 (string-match "[a-zA-Z]+" symbol)
2580 (if (TeX-active-mark)
2582 (exchange-point-and-mark)
2585 (backward-char))))))
2587 (defun TeX-arg-string (optional &optional prompt initial-input)
2588 "Prompt for a string.
2590 If OPTIONAL is not nil then the PROMPT will start with ``(Optional) ''.
2591 INITIAL-INPUT is a string to insert before reading input."
2592 (TeX-argument-insert
2593 (if (and (not optional) (TeX-active-mark))
2594 (let ((TeX-argument (buffer-substring (point) (mark))))
2595 (delete-region (point) (mark))
2597 (read-string (TeX-argument-prompt optional prompt "Text") initial-input))
2600 (defun TeX-parse-arguments (args)
2601 "Parse TeX macro arguments ARGS.
2603 See `TeX-parse-macro' for details."
2604 (let ((last-optional-rejected nil)
2606 ;; Maybe get rid of all optional arguments. See `TeX-insert-macro' for
2607 ;; more comments. See `TeX-insert-macro-default-style'.
2608 (when (or (and (eq TeX-insert-macro-default-style 'show-optional-args)
2609 (equal current-prefix-arg '(4)))
2610 (and (eq TeX-insert-macro-default-style 'mandatory-args-only)
2611 (null (equal current-prefix-arg '(4)))))
2612 (while (vectorp (car args))
2613 (setq args (cdr args))))
2616 (if (vectorp (car args))
2617 (unless last-optional-rejected
2618 (let ((TeX-arg-opening-brace LaTeX-optop)
2619 (TeX-arg-closing-brace LaTeX-optcl))
2620 (TeX-parse-argument t (if (equal (length (car args)) 1)
2622 (append (car args) nil)))))
2623 (let ((TeX-arg-opening-brace TeX-grop)
2624 (TeX-arg-closing-brace TeX-grcl))
2625 (setq last-optional-rejected nil)
2626 (TeX-parse-argument nil (car args))))
2627 (setq args (cdr args)))))
2629 (defun TeX-parse-argument (optional arg)
2630 "Depending on OPTIONAL, insert TeX macro argument ARG.
2631 If OPTIONAL is set, only insert if there is anything to insert, and
2632 then use square brackets instead of curly braces.
2634 See `TeX-parse-macro' for details."
2636 (cond ((stringp arg)
2637 (TeX-arg-string optional arg)
2638 (setq insert-flag t))
2641 (TeX-parse-argument optional t)
2643 (TeX-parse-argument optional nil)
2644 (setq arg (- arg 1)))))
2646 (insert TeX-arg-opening-brace)
2647 (when (and (not optional) (TeX-active-mark))
2648 (exchange-point-and-mark))
2649 (insert TeX-arg-closing-brace)
2650 (setq insert-flag t))
2652 (insert TeX-arg-opening-brace)
2653 (if (and (not optional) (TeX-active-mark))
2655 (exchange-point-and-mark))
2656 (set-marker exit-mark (point)))
2657 (insert TeX-arg-closing-brace)
2658 (setq insert-flag t))
2660 (funcall arg optional))
2662 (let ((head (car arg))
2664 (cond ((stringp head)
2665 (apply 'TeX-arg-string optional arg))
2667 (apply head optional tail))
2668 (t (error "Unknown list argument type %s"
2669 (prin1-to-string head))))))
2670 (t (error "Unknown argument type %s" (prin1-to-string arg))))
2671 (when (and insert-flag (not optional) (TeX-active-mark))
2672 (TeX-deactivate-mark))))
2674 (defun TeX-argument-insert (name optional &optional prefix)
2675 "Insert NAME surrounded by curly braces.
2677 If OPTIONAL, only insert it if not empty, and then use square brackets.
2678 If PREFIX is given, insert it before NAME."
2679 (if (and optional (string-equal name ""))
2680 (setq last-optional-rejected t)
2681 (insert TeX-arg-opening-brace)
2684 (if (and (string-equal name "")
2685 (null (marker-position exit-mark)))
2686 (set-marker exit-mark (point))
2688 (insert TeX-arg-closing-brace)))
2690 (defun TeX-argument-prompt (optional prompt default &optional complete)
2691 "Return a argument prompt.
2693 If OPTIONAL is not nil then the prompt will start with ``(Optional) ''.
2695 PROMPT will be used if not nil, otherwise use DEFAULT.
2697 Unless optional argument COMPLETE is non-nil, ``: '' will be appended."
2698 (concat (if optional "(Optional) " "")
2699 (if prompt prompt default)
2700 (if complete "" ": ")))
2702 (defun TeX-string-divide-number-unit (string)
2703 "Divide number and unit in STRING.
2704 Return the number as car and unit as cdr."
2705 (if (string-match "[0-9]*\\.?[0-9]+" string)
2706 (list (substring string 0 (string-match "[^.0-9]" string))
2707 (substring string (if (string-match "[^.0-9]" string)
2708 (string-match "[^.0-9]" string)
2712 (defcustom TeX-default-unit-for-image "cm"
2713 "Default unit when prompting for an image size."
2715 :type '(choice (const "cm")
2717 (const "\\linewidth")
2718 (string :tag "Other")))
2720 (defun TeX-arg-maybe (symbol list form)
2721 "Evaluates FORM, if SYMBOL is an element of LIST."
2722 (when (memq symbol list)
2725 (defun TeX-arg-free (optional &rest args)
2726 "Parse its arguments but use no braces when they are inserted."
2727 (let ((TeX-arg-opening-brace "")
2728 (TeX-arg-closing-brace ""))
2729 (if (equal (length args) 1)
2730 (TeX-parse-argument optional (car args))
2731 (TeX-parse-argument optional args))))
2733 (defun TeX-arg-literal (optional &rest args)
2734 "Insert its arguments ARGS into the buffer.
2735 Used for specifying extra syntax for a macro."
2736 ;; FIXME: What is the purpose of OPTIONAL here? -- rs
2737 (apply 'insert args))
2742 (defcustom TeX-install-font-lock 'font-latex-setup
2743 "Function to call to install font lock support.
2744 Choose `ignore' if you don't want AUCTeX to install support for font locking."
2746 :type '(radio (function-item font-latex-setup)
2747 (function-item tex-font-setup)
2748 (function-item ignore)
2749 (function :tag "Other")))
2753 (defvar TeX-format-list
2754 '(("JLATEX" japanese-latex-mode
2755 "\\\\\\(documentstyle\\|documentclass\\)[^%\n]*{\\(j[s-]?\\|t\\)\
2756 \\(article\\|report\\|book\\|slides\\)")
2757 ("JTEX" japanese-plain-tex-mode
2758 "-- string likely in Japanese TeX --")
2759 ("AMSTEX" ams-tex-mode
2761 ("CONTEXT" context-mode
2762 "\\\\\\(start\\(text\\|tekst\\|proje[ck]t\\|proiect\\|\
2763 produ[ck]t\\|produs\\|environment\\|omgeving\\|umgebung\\|prostredi\\|mediu\\|\
2764 component\\|onderdeel\\|komponent[ea]\\|componenta\\)\
2765 \\|inizia\\(testo\\|progetto\\|prodotto\\|ambiente\\|componente\\)\
2766 \\)\\|%.*?interface=")
2768 "\\\\\\(begin\\|\\(?:sub\\)\\{0,2\\}section\\|chapter\\|documentstyle\\|\
2769 documentclass\\)\\b")
2770 ("TEX" plain-tex-mode "."))
2771 "*List of format packages to consider when choosing a TeX mode.
2773 A list with an entry for each format package available at the site.
2775 Each entry is a list with three elements.
2777 1. The name of the format package.
2778 2. The name of the major mode.
2779 3. A regexp typically matched in the beginning of the file.
2781 When entering `tex-mode', each regexp is tried in turn in order to find
2782 the major mode to be used.")
2784 (defcustom TeX-default-mode 'latex-mode
2785 "*Mode to enter for a new file when it can't be determined otherwise."
2787 :type '(radio (function-item latex-mode)
2788 (function-item plain-tex-mode)
2789 (function :tag "Other")))
2791 (defcustom TeX-force-default-mode nil
2792 "*If set to nil, try to infer the mode of the file from its content."
2797 (defun TeX-tex-mode ()
2798 "Major mode in AUCTeX for editing TeX or LaTeX files.
2799 Tries to guess whether this file is for plain TeX or LaTeX.
2801 The algorithm is as follows:
2803 1) if the file is empty or `TeX-force-default-mode' is not set to nil,
2804 `TeX-default-mode' is chosen
2805 2) If \\documentstyle or \\begin{, \\section{, \\part{ or \\chapter{ is
2806 found, `latex-mode' is selected.
2807 3) Otherwise, use `plain-tex-mode'"
2810 (funcall (if (or (equal (buffer-size) 0)
2811 TeX-force-default-mode)
2814 (goto-char (point-min))
2815 (let ((comment-start-skip ;Used by TeX-in-comment
2817 "\\(\\(^\\|[^\\\n]\\)\\("
2818 (regexp-quote TeX-esc)
2819 (regexp-quote TeX-esc)
2820 "\\)*\\)\\(%+ *\\)"))
2821 (entry TeX-format-list)
2823 (while (and entry (not answer))
2824 (if (re-search-forward (nth 2 (car entry))
2826 (if (not (TeX-in-comment))
2827 (setq answer (nth 1 (car entry))))
2828 (setq entry (cdr entry))))
2831 TeX-default-mode))))))
2833 (defun VirTeX-common-initialization ()
2834 "Perform basic initialization."
2835 (kill-all-local-variables)
2837 (setq TeX-output-extension (if TeX-PDF-mode "pdf" "dvi"))
2838 (setq local-abbrev-table text-mode-abbrev-table)
2839 (setq indent-tabs-mode nil)
2842 (make-local-variable 'ispell-parser)
2843 (setq ispell-parser 'tex)
2844 (make-local-variable 'ispell-tex-p)
2845 (setq ispell-tex-p t)
2847 ;; Redefine some standard variables
2848 (make-local-variable 'paragraph-start)
2849 (make-local-variable 'paragraph-separate)
2850 (make-local-variable 'comment-start)
2851 (setq comment-start "%")
2852 (make-local-variable 'comment-start-skip)
2853 (setq comment-start-skip
2855 "\\(\\(^\\|[^\\\n]\\)\\("
2856 (regexp-quote TeX-esc)
2857 (regexp-quote TeX-esc)
2858 "\\)*\\)\\(%+[ \t]*\\)"))
2859 (set (make-local-variable 'comment-end-skip) "[ \t]*\\(\\s>\\|\n\\)")
2860 (set (make-local-variable 'comment-use-syntax) t)
2861 ;; `comment-padding' is defined here as an integer for compatibility
2862 ;; reasons because older Emacsen could not cope with a string.
2863 (make-local-variable 'comment-padding)
2864 (setq comment-padding 1)
2865 ;; Removed as commenting in (La)TeX is done with one `%' not two
2866 ;; (make-local-variable 'comment-add)
2867 ;; (setq comment-add 1) ;default to `%%' in comment-region
2868 (make-local-variable 'comment-indent-function)
2869 (setq comment-indent-function 'TeX-comment-indent)
2870 (make-local-variable 'comment-multi-line)
2871 (setq comment-multi-line nil)
2872 (make-local-variable 'compile-command)
2873 (unless (boundp 'compile-command)
2874 (setq compile-command "make"))
2875 (make-local-variable 'words-include-escapes)
2876 (setq words-include-escapes nil)
2878 ;; Make TAB stand out
2879 ;; (make-local-variable 'buffer-display-table)
2880 ;; (setq buffer-display-table (if standard-display-table
2881 ;; (copy-sequence standard-display-table)
2882 ;; (make-display-table)))
2883 ;; (aset buffer-display-table ?\t (apply 'vector (append "<TAB>" nil)))
2885 ;; Symbol completion.
2886 (make-local-variable 'TeX-complete-list)
2887 (setq TeX-complete-list
2888 (list (list "\\\\\\([a-zA-Z]*\\)"
2889 1 'TeX-symbol-list (if TeX-insert-braces "{}"))
2890 (list "" TeX-complete-word)))
2892 (funcall TeX-install-font-lock)
2894 ;; We want this to be early in the list, so we do not add it before
2895 ;; we enter TeX mode the first time.
2896 (if (boundp 'local-write-file-hooks)
2897 (add-hook 'local-write-file-hooks 'TeX-safe-auto-write)
2898 (add-hook 'write-file-hooks 'TeX-safe-auto-write))
2899 (make-local-variable 'TeX-auto-update)
2900 (setq TeX-auto-update t)
2903 (when TeX-source-correlate-mode
2904 (TeX-source-correlate-mode 1))
2906 ;; Let `TeX-master-file' be called after a new file was opened and
2907 ;; call `TeX-update-style' on any file opened. (The addition to the
2908 ;; hook has to be made here because its local value will be deleted
2909 ;; by `kill-all-local-variables' if it is added e.g. in `tex-mode'.)
2911 ;; `TeX-update-style' has to be called before
2912 ;; `global-font-lock-mode', which may also be specified in
2913 ;; `find-file-hooks', gets called. Otherwise style-based
2914 ;; fontification will break (in XEmacs). That means, `add-hook'
2915 ;; cannot be called with a non-nil value of the APPEND argument.
2917 ;; `(TeX-master-file nil nil t)' has to be called *before*
2918 ;; `TeX-update-style' as the latter will call `TeX-master-file'
2919 ;; without the `ask' bit set.
2920 (when (and (featurep 'xemacs) (not (emacs-version>= 21 5)))
2921 (make-local-hook 'find-file-hooks))
2922 (add-hook 'find-file-hooks
2924 ;; Check if we are looking at a new or shared file.
2925 (when (or (not (file-exists-p (buffer-file-name)))
2926 (eq TeX-master 'shared))
2927 (TeX-master-file nil nil t))
2928 (TeX-update-style t)) nil t))
2932 (defcustom plain-TeX-clean-intermediate-suffixes
2933 TeX-clean-default-intermediate-suffixes
2934 "List of regexps matching suffixes of intermediate files to be deleted.
2935 The regexps will be anchored at the end of the file name to be matched,
2936 i.e. you do _not_ have to cater for this yourself by adding \\\\' or $."
2937 :type '(repeat regexp)
2938 :group 'TeX-command)
2940 (defcustom plain-TeX-clean-output-suffixes TeX-clean-default-output-suffixes
2941 "List of regexps matching suffixes of output files to be deleted.
2942 The regexps will be anchored at the end of the file name to be matched,
2943 i.e. you do _not_ have to cater for this yourself by adding \\\\' or $."
2944 :type '(repeat regexp)
2945 :group 'TeX-command)
2947 (defcustom plain-TeX-mode-hook nil
2948 "A hook run in plain TeX mode buffers."
2953 (defun TeX-plain-tex-mode ()
2954 "Major mode in AUCTeX for editing plain TeX files.
2955 See info under AUCTeX for documentation.
2958 \\{plain-TeX-mode-map}
2960 Entering `plain-tex-mode' calls the value of `text-mode-hook',
2961 then the value of `TeX-mode-hook', and then the value
2962 of plain-TeX-mode-hook."
2964 (plain-TeX-common-initialization)
2965 (setq major-mode 'plain-tex-mode)
2966 (use-local-map plain-TeX-mode-map)
2967 (easy-menu-add plain-TeX-mode-menu plain-TeX-mode-map)
2968 (easy-menu-add plain-TeX-mode-command-menu plain-TeX-mode-map)
2969 (setq TeX-base-mode-name "TeX")
2970 (setq TeX-command-default "TeX")
2971 (setq TeX-sentinel-default-function 'TeX-TeX-sentinel)
2972 (add-hook 'tool-bar-mode-on-hook 'plain-TeX-maybe-install-toolbar nil t)
2973 (when (if (featurep 'xemacs)
2975 (and (boundp 'tool-bar-mode) tool-bar-mode))
2976 (plain-TeX-maybe-install-toolbar))
2977 (TeX-run-mode-hooks 'text-mode-hook 'TeX-mode-hook 'plain-TeX-mode-hook)
2978 (TeX-set-mode-name))
2980 (defun plain-TeX-common-initialization ()
2981 "Common initialization for plain TeX like modes."
2982 (VirTeX-common-initialization)
2983 (set-syntax-table TeX-mode-syntax-table)
2984 (setq paragraph-start
2987 "\\|" (regexp-quote TeX-esc) "par\\|"
2989 (regexp-quote TeX-esc)
2991 "begin\\|end\\|part\\|chapter\\|"
2992 "section\\|subsection\\|subsubsection\\|"
2993 "paragraph\\|include\\|includeonly\\|"
2994 "tableofcontents\\|appendix\\|label\\|caption\\|"
2995 "\\[\\|\\]" ; display math delimitors
2998 "^[ \t]*\\$\\$" ; display math delimitor
3000 (setq paragraph-separate
3004 (regexp-quote TeX-esc) "par\\|"
3008 (regexp-quote TeX-esc)
3010 "begin\\|end\\|label\\|caption\\|part\\|chapter\\|"
3011 "section\\|subsection\\|subsubsection\\|"
3012 "paragraph\\|include\\|includeonly\\|"
3013 "tableofcontents\\|appendix\\|" (regexp-quote TeX-esc)
3016 (setq TeX-header-end (regexp-quote "%**end of header"))
3017 (setq TeX-trailer-start (regexp-quote (concat TeX-esc "bye")))
3018 (TeX-run-style-hooks "TEX"))
3022 (if (boundp 'hilit-patterns-alist)
3023 (let ((latex-patterns (cdr-safe (assq 'latex-mode hilit-patterns-alist)))
3024 (plain-tex-patterns (cdr-safe (assq 'plain-tex-mode
3025 hilit-patterns-alist))))
3026 (if (and latex-patterns plain-tex-patterns)
3027 (setq hilit-patterns-alist
3028 (append (list (cons 'ams-tex-mode plain-tex-patterns))
3029 hilit-patterns-alist)))))
3033 (defgroup TeX-parse nil
3034 "Parsing TeX files from AUCTeX."
3037 (defvar TeX-auto-parser '((styles TeX-auto-file TeX-run-style-hooks)))
3038 ;; Alist of parsed information.
3039 ;; Each entry is a list with the following elements:
3041 ;; 0. Name of information type.
3042 ;; 1. Name of temporary variable used when parsing.
3043 ;; 2. Name of function to add information to add to #3.
3044 ;; 3. Name of variable holding buffer local information.
3045 ;; 4. Name of variable indicating that #3 has changed.
3048 (defconst TeX-auto-parser-temporary 1)
3049 (defconst TeX-auto-parser-add 2)
3050 (defconst TeX-auto-parser-local 3)
3051 (defconst TeX-auto-parser-change 4)
3053 (defun TeX-auto-add-type (name prefix &optional plural)
3054 "Add information about NAME to the parser using PREFIX.
3056 Optional third argument PLURAL is the plural form of TYPE.
3057 By default just add an `s'.
3059 This function create a set of variables and functions to maintain a
3060 separate type of information in the parser."
3061 (let* ((names (or plural (concat name "s")))
3062 (tmp (intern (concat prefix "-auto-" name)))
3063 (add (intern (concat prefix "-add-" names)))
3064 (local (intern (concat prefix "-" name "-list")))
3065 (change (intern (concat prefix "-" name "-changed"))))
3066 (setq TeX-auto-parser
3067 (cons (list name tmp add local change) TeX-auto-parser))
3069 (make-variable-buffer-local local)
3071 (make-variable-buffer-local change)
3072 (fset add `(lambda (&rest entries)
3073 ,(concat "Add information about " (upcase name)
3074 " to the current buffer.
3075 Generated by `TeX-auto-add-type'.")
3076 (TeX-auto-add-information ,name entries)))
3077 (fset local `(lambda nil
3078 ,(concat "List of " names
3079 " active in the current buffer.
3080 Generated by `TeX-auto-add-type'.")
3081 (TeX-auto-list-information ,name)))
3082 (add-hook 'TeX-remove-style-hook
3083 `(lambda nil (setq ,(symbol-name local) nil)))))
3085 (defun TeX-auto-add-information (name entries)
3086 "For NAME in `TeX-auto-parser' add ENTRIES."
3087 (let* ((entry (assoc name TeX-auto-parser))
3088 (change (nth TeX-auto-parser-change entry))
3089 (change-value (symbol-value change))
3090 (local (nth TeX-auto-parser-local entry))
3091 (local-value (symbol-value local)))
3093 (set local (cons entries local-value))
3095 (set local (list entries local-value)))))
3097 (defun TeX-auto-list-information (name)
3098 "Return information in `TeX-auto-parser' about NAME."
3100 (let* ((entry (assoc name TeX-auto-parser))
3101 (change (nth TeX-auto-parser-change entry))
3102 (change-value (symbol-value change))
3103 (local (nth TeX-auto-parser-local entry)))
3104 (if (not change-value)
3108 (message "Sorting %s..." name)
3110 (sort (mapcar 'TeX-listify (apply 'append (symbol-value local)))
3111 'TeX-car-string-lessp))
3113 (message "Removing duplicates...")
3114 (let ((entry (symbol-value local)))
3115 (while (and entry (cdr entry))
3116 (let ((this (car entry))
3117 (next (car (cdr entry))))
3118 (if (not (string-equal (car this) (car next)))
3119 (setq entry (cdr entry))
3120 ;; We have two equal symbols. Use the one with
3122 (if (> (length next) (length this))
3123 (setcdr this (cdr next)))
3124 (setcdr entry (cdr (cdr entry)))))))
3125 (message "Removing duplicates... done"))
3126 (symbol-value local)))
3128 (TeX-auto-add-type "symbol" "TeX")
3130 (defvar TeX-auto-apply-hook nil
3131 "Hook run when a buffer is parsed and the information is applied.")
3133 (defun TeX-auto-apply ()
3134 "Parse and apply TeX information in the current buffer."
3136 (run-hooks 'TeX-auto-apply-hook)
3137 (mapcar 'TeX-auto-apply-entry TeX-auto-parser))
3139 (defun TeX-auto-apply-entry (entry)
3140 "Apply the information in ENTRY in `TeX-auto-parser'."
3141 (let ((value (symbol-value (nth TeX-auto-parser-temporary entry)))
3142 (add (nth TeX-auto-parser-add entry)))
3143 (if value (apply add value))))
3145 (defun TeX-safe-auto-write ()
3146 "Call `TeX-auto-write' safely."
3147 (condition-case name
3148 (and (boundp 'TeX-auto-update)
3152 ;; Continue with the other write file hooks.
3155 (defcustom TeX-auto-save nil
3156 "*Automatically save style information when saving the buffer."
3160 (defcustom TeX-auto-untabify nil
3161 "*Automatically untabify when saving the buffer."
3165 (defun TeX-auto-write ()
3166 "Save all relevant TeX information from the current buffer."
3167 (if TeX-auto-untabify
3168 (untabify (point-min) (point-max)))
3169 (if (and TeX-auto-save TeX-auto-local)
3170 (let* ((file (expand-file-name
3172 (file-name-as-directory TeX-auto-local)
3173 (TeX-strip-extension nil TeX-all-extensions t)
3175 (TeX-master-directory)))
3176 (dir (file-name-directory file)))
3177 ;; Create auto directory if possible.
3178 (if (not (file-exists-p dir))
3179 (condition-case name
3180 (make-directory dir)
3182 (if (file-writable-p file)
3185 (TeX-auto-store file))
3186 (message "Can't write style information.")))))
3188 (defcustom TeX-macro-default (car-safe TeX-macro-private)
3189 "*Default directory to search for TeX macros."
3193 (defcustom TeX-auto-default (car-safe TeX-auto-private)
3194 "*Default directory to place automatically generated TeX information."
3199 (defun TeX-auto-generate (tex auto)
3200 "Generate style file for TEX and store it in AUTO.
3201 If TEX is a directory, generate style files for all files in the directory."
3202 (interactive (list (setq TeX-macro-default
3203 (expand-file-name (read-file-name
3204 "TeX file or directory: "
3206 TeX-macro-default 'confirm)))
3207 (setq TeX-auto-default
3208 (expand-file-name (read-file-name
3209 "AUTO lisp directory: "
3211 TeX-auto-default 'confirm)))))
3212 (cond ((not (file-readable-p tex)))
3213 ((string-match TeX-ignore-file tex))
3214 ((file-directory-p tex)
3215 (let ((files (directory-files (expand-file-name tex)))
3216 (default-directory (file-name-as-directory
3217 (expand-file-name tex)))
3218 (TeX-file-recurse (cond ((symbolp TeX-file-recurse)
3220 ((zerop TeX-file-recurse)
3222 ((1- TeX-file-recurse)))))
3223 (mapcar (lambda (file)
3224 (if (or TeX-file-recurse
3225 (not (file-directory-p file)))
3226 (TeX-auto-generate file auto)))
3228 ((not (file-newer-than-file-p
3230 (concat (file-name-as-directory auto)
3231 (TeX-strip-extension tex TeX-all-extensions t)
3233 ((TeX-match-extension tex (append TeX-file-extensions
3234 BibTeX-file-extensions))
3236 (set-buffer (let (enable-local-eval)
3237 (find-file-noselect tex)))
3238 (message "Parsing %s..." tex)
3239 (TeX-auto-store (concat (file-name-as-directory auto)
3240 (TeX-strip-extension tex
3244 (kill-buffer (current-buffer))
3245 (message "Parsing %s... done" tex)))))
3248 (defun TeX-auto-generate-global ()
3249 "Create global auto directory for global TeX macro definitions."
3251 (unless (file-directory-p TeX-auto-global)
3252 (make-directory TeX-auto-global))
3253 (let ((TeX-file-extensions '("cls" "sty"))
3254 (BibTeX-file-extensions nil))
3255 (mapc (lambda (macro) (TeX-auto-generate macro TeX-auto-global))
3257 (byte-recompile-directory TeX-auto-global 0))
3259 (defun TeX-auto-store (file)
3260 "Extract information for AUCTeX from current buffer and store it in FILE."
3263 (if (member nil (mapcar 'TeX-auto-entry-clear-p TeX-auto-parser))
3264 (let ((style (TeX-strip-extension nil TeX-all-extensions t)))
3265 (TeX-unload-style style)
3267 (set-buffer (generate-new-buffer file))
3269 (insert "(TeX-add-style-hook \"" style "\"\n"
3271 (mapc (lambda (el) (TeX-auto-insert el style))
3274 (write-region (point-min) (point-max) file nil 'silent)
3275 (kill-buffer (current-buffer))))
3276 (if (file-exists-p (concat file "c"))
3277 (delete-file (concat file "c")))
3278 (if (file-exists-p file)
3279 (delete-file file))))
3281 (defun TeX-auto-entry-clear-p (entry)
3282 "Check if the temporary for `TeX-auto-parser' entry ENTRY is clear."
3283 ;; FIXME: This doc-string isn't clear to me. -- rs
3284 (null (symbol-value (nth TeX-auto-parser-temporary entry))))
3286 (defun TeX-auto-insert (entry &optional skip)
3287 "Insert code to initialize ENTRY from `TeX-auto-parser'.
3289 If SKIP is not-nil, don't insert code for SKIP."
3290 (let ((name (symbol-name (nth TeX-auto-parser-add entry)))
3291 (list (symbol-value (nth TeX-auto-parser-temporary entry))))
3293 (insert "\n (" name)
3295 (cond ((and (stringp el) (not (string= el skip)))
3297 (insert (prin1-to-string el)))
3300 (insert "'" (prin1-to-string el)))))
3303 (defvar TeX-auto-ignore
3304 '("csname" "filedate" "fileversion" "docdate" "next" "labelitemi"
3305 "labelitemii" "labelitemiii" "labelitemiv" "labelitemv"
3306 "labelenumi" "labelenumii" "labelenumiii" "labelenumiv"
3307 "labelenumv" "theenumi" "theenumii" "theenumiii" "theenumiv"
3308 "theenumv" "document" "par" "do" "expandafter")
3309 "List of symbols to ignore when scanning a TeX style file.")
3311 (defun TeX-auto-add-regexp (regexp)
3312 "Add REGEXP to `TeX-auto-regexp-list' if not already a member."
3313 (if (symbolp TeX-auto-regexp-list)
3314 (setq TeX-auto-regexp-list (symbol-value TeX-auto-regexp-list)))
3315 (or (memq regexp TeX-auto-regexp-list)
3316 (setq TeX-auto-regexp-list (cons regexp TeX-auto-regexp-list))))
3318 (defvar TeX-auto-empty-regexp-list
3319 '(("<IMPOSSIBLE>\\(\\'\\`\\)" 1 ignore))
3320 "List of regular expressions guaranteed to match nothing.")
3322 (defvar TeX-token-char
3323 (if (featurep 'mule)
3324 "\\(?:[a-zA-Z]\\|\\cj\\)"
3326 "Regexp matching a character in a TeX macro.
3328 Please use a shy group if you use a grouping construct, because
3329 the functions/variables which use `TeX-token-char' expect not to
3330 alter the numbering of any ordinary, non-shy groups.")
3332 (defvar plain-TeX-auto-regexp-list
3333 (let ((token TeX-token-char))
3334 `((,(concat "\\\\def\\\\\\(" token "+\\)[^a-zA-Z@]") 1 TeX-auto-symbol-check)
3335 (,(concat "\\\\let\\\\\\(" token "+\\)[^a-zA-Z@]") 1 TeX-auto-symbol-check)
3336 (,(concat "\\\\font\\\\\\(" token "+\\)[^a-zA-Z@]") 1 TeX-auto-symbol)
3337 (,(concat "\\\\chardef\\\\\\(" token "+\\)[^a-zA-Z@]") 1 TeX-auto-symbol)
3338 (,(concat "\\\\new\\(?:count\\|dimen\\|muskip\\|skip\\)\\\\\\(" token "+\\)[^a-zA-Z@]")
3340 (,(concat "\\\\newfont{?\\\\\\(" token "+\\)}?") 1 TeX-auto-symbol)
3341 (,(concat "\\\\typein\\[\\\\\\(" token "+\\)\\]") 1 TeX-auto-symbol)
3342 ("\\\\input +\\(\\.*[^#%\\\\\\.\n\r]+\\)\\(\\.[^#%\\\\\\.\n\r]+\\)?"
3344 (,(concat "\\\\mathchardef\\\\\\(" token "+\\)[^a-zA-Z@]") 1 TeX-auto-symbol)))
3345 "List of regular expression matching common LaTeX macro definitions.")
3347 (defvar TeX-auto-full-regexp-list plain-TeX-auto-regexp-list
3348 "Full list of regular expression matching TeX macro definitions.")
3350 (defvar TeX-auto-prepare-hook nil
3351 "List of hooks to be called before parsing a TeX file.")
3353 (defvar TeX-auto-cleanup-hook nil
3354 "List of hooks to be called after parsing a TeX file.")
3356 (defcustom TeX-auto-parse-length 999999
3357 "Maximal length of TeX file (in characters) that will be parsed."
3360 (make-variable-buffer-local 'TeX-auto-parse-length)
3362 (defcustom TeX-auto-x-parse-length 0
3363 "Maximum length of TeX file that will be parsed additionally.
3364 Use `TeX-auto-x-regexp-list' for parsing the region between
3365 `TeX-auto-parse-length' and this value."
3368 (make-variable-buffer-local 'TeX-auto-x-parse-length)
3370 (defcustom TeX-auto-x-regexp-list 'LaTeX-auto-label-regexp-list
3371 "List of regular expressions used for additional parsing.
3372 See `TeX-auto-x-parse-length'."
3373 :type '(radio (variable-item TeX-auto-empty-regexp-list)
3374 (variable-item TeX-auto-full-regexp-list)
3375 (variable-item plain-TeX-auto-regexp-list)
3376 (variable-item LaTeX-auto-minimal-regexp-list)
3377 (variable-item LaTeX-auto-label-regexp-list)
3378 (variable-item LaTeX-auto-regexp-list)
3379 (symbol :tag "Other")
3380 (repeat :tag "Specify"
3381 (group (regexp :tag "Match")
3382 (sexp :tag "Groups")
3385 (make-variable-buffer-local 'TeX-auto-x-regexp-list)
3387 (defun TeX-regexp-group-count (regexp)
3388 "Return number of groups in a REGEXP. This is not foolproof:
3389 you should not use something like `[\\(]' for a character range."
3391 (while (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\([^?]"
3393 (setq start (- (match-end 0) 2)
3397 (defun TeX-auto-parse-region (regexp-list beg end)
3398 "Parse TeX information according to REGEXP-LIST between BEG and END."
3399 (if (symbolp regexp-list)
3400 (setq regexp-list (and (boundp regexp-list) (symbol-value regexp-list))))
3402 ;; Extract the information.
3405 (regexp (concat "\\("
3408 (push (cons count x) groups)
3411 (TeX-regexp-group-count (car x))))
3413 regexp-list "\\)\\|\\(")
3418 (goto-char (if end (min end (point-max)) (point-max)))
3419 (while (re-search-backward regexp beg t)
3420 (let* ((entry (cdr (TeX-member nil groups
3422 (match-beginning (car b))))))
3423 (symbol (nth 2 entry))
3424 (match (nth 1 entry)))
3425 (unless (TeX-in-comment)
3426 (looking-at (nth 0 entry))
3427 (if (fboundp symbol)
3428 (funcall symbol match)
3429 (puthash (if (listp match)
3430 (mapcar #'TeX-match-buffer match)
3431 (TeX-match-buffer match))
3432 (setq count (1- count))
3433 (cdr (or (assq symbol syms)
3436 (make-hash-table :test 'equal))
3439 (dolist (symbol syms)
3440 (setq lst (symbol-value (car symbol)))
3443 (setq count (1+ count))
3445 (maphash (lambda (key value)
3446 (push (cons value key) lst))
3448 (clrhash (cdr symbol))
3449 (set (car symbol) (mapcar #'cdr (sort lst #'car-less-than-car)))))))
3452 (defun TeX-auto-parse ()
3453 "Parse TeX information in current buffer.
3455 Call the functions in `TeX-auto-prepare-hook' before parsing, and the
3456 functions in `TeX-auto-cleanup-hook' after parsing."
3458 (let ((case-fold-search nil))
3460 (mapc 'TeX-auto-clear-entry TeX-auto-parser)
3461 (run-hooks 'TeX-auto-prepare-hook)
3464 (and (> TeX-auto-x-parse-length TeX-auto-parse-length)
3465 (> (point-max) TeX-auto-parse-length)
3466 (TeX-auto-parse-region TeX-auto-x-regexp-list
3467 TeX-auto-parse-length
3468 TeX-auto-x-parse-length))
3469 (TeX-auto-parse-region TeX-auto-regexp-list
3470 nil TeX-auto-parse-length))
3472 ;; Cleanup ignored symbols.
3474 ;; NOTE: This is O(N M) where it could be O(N log N + M log M) if we
3475 ;; sorted the lists first.
3476 (while (member (car TeX-auto-symbol) TeX-auto-ignore)
3477 (setq TeX-auto-symbol (cdr TeX-auto-symbol)))
3478 (let ((list TeX-auto-symbol))
3479 (while (and list (cdr list))
3480 (if (member (car (cdr list)) TeX-auto-ignore)
3481 (setcdr list (cdr (cdr list)))
3482 (setq list (cdr list)))))
3484 (run-hooks 'TeX-auto-cleanup-hook)))
3486 (defun TeX-auto-clear-entry (entry)
3487 "Set the temporary variable in ENTRY to nil."
3488 (set (nth TeX-auto-parser-temporary entry) nil))
3490 (defvar LaTeX-auto-end-symbol nil)
3492 (defun TeX-auto-symbol-check (match)
3493 "Add MATCH to TeX-auto-symbols.
3494 Check for potential LaTeX environments."
3495 (let ((symbol (if (listp match)
3496 (mapcar 'TeX-match-buffer match)
3497 (TeX-match-buffer match))))
3498 (if (and (stringp symbol)
3499 (string-match "^end\\(.+\\)$" symbol))
3500 (add-to-list 'LaTeX-auto-end-symbol
3501 (substring symbol (match-beginning 1) (match-end 1)))
3503 (dolist (elt symbol)
3504 (add-to-list 'TeX-auto-symbol elt))
3505 (add-to-list 'TeX-auto-symbol symbol)))))
3509 ;; Some of these functions has little to do with TeX, but nonetheless we
3510 ;; should use the "TeX-" prefix to avoid name clashes.
3512 (defcustom TeX-auto-regexp-list 'TeX-auto-full-regexp-list
3513 "*List of regular expressions used for parsing the current file."
3514 :type '(radio (variable-item TeX-auto-empty-regexp-list)
3515 (variable-item TeX-auto-full-regexp-list)
3516 (variable-item plain-TeX-auto-regexp-list)
3517 (variable-item LaTeX-auto-minimal-regexp-list)
3518 (variable-item LaTeX-auto-label-regexp-list)
3519 (variable-item LaTeX-auto-regexp-list)
3520 (symbol :tag "Other")
3521 (repeat :tag "Specify"
3522 (group (regexp :tag "Match")
3523 (sexp :tag "Groups")
3526 (make-variable-buffer-local 'TeX-auto-regexp-list)
3528 (defgroup TeX-file-extension nil
3529 "File extensions recognized by AUCTeX."
3532 (defcustom TeX-file-extensions '("tex" "sty" "cls" "ltx" "texi" "texinfo" "dtx")
3533 "*File extensions used by manually generated TeX files."
3534 :group 'TeX-file-extension
3535 :type '(repeat (string :format "%v")))
3537 (defcustom TeX-all-extensions '("[^.\n]+")
3538 "All possible file extensions."
3539 :group 'TeX-file-extension
3540 :type '(repeat (regexp :format "%v")))
3542 (defcustom TeX-default-extension "tex"
3543 "*Default extension for TeX files."
3544 :group 'TeX-file-extension
3547 (make-variable-buffer-local 'TeX-default-extension)
3549 (defcustom docTeX-default-extension "dtx"
3550 "*Default extension for docTeX files."
3551 :group 'TeX-file-extension
3554 (defvar TeX-output-extension nil
3555 "Extension of TeX output file.
3556 This is either a string or a list with
3557 a string as element. Its value is obtained from `TeX-command-output-list'.
3558 Access to the value should be through the function `TeX-output-extension'.")
3560 (make-variable-buffer-local 'TeX-output-extension)
3562 (defcustom BibTeX-file-extensions '("bib")
3563 "Valid file extensions for BibTeX files."
3564 :group 'TeX-file-extension
3565 :type '(repeat (string :format "%v")))
3567 (defcustom BibTeX-style-extensions '("bst")
3568 "Valid file extensions for BibTeX styles."
3569 :group 'TeX-file-extension
3570 :type '(repeat (string :format "%v")))
3572 (defcustom TeX-ignore-file "\\(^\\|[/\\]\\)\\(\\.\\|\\.\\.\\|RCS\\|SCCS\\|CVS\\|babel\\..*\\)$"
3573 "Regular expression matching file names to ignore.
3575 These files or directories will not be considered when searching for
3576 TeX files in a directory."
3580 (defcustom TeX-file-recurse t
3581 "*Whether to search TeX directories recursively.
3582 nil means do not recurse, a positive integer means go that far deep in the
3583 directory hierarchy, t means recurse indefinitely."
3585 :type '(choice (const :tag "On" t)
3586 (const :tag "Off" nil)
3587 (integer :tag "Depth" :value 1)))
3589 (defun TeX-match-extension (file &optional extensions)
3590 "Return non-nil if FILE has one of EXTENSIONS.
3592 If EXTENSIONS is not specified or nil, the value of
3593 `TeX-file-extensions' is used instead."
3595 (if (null extensions)
3596 (setq extensions TeX-file-extensions))
3598 (let ((regexp (concat "\\.\\("
3599 (mapconcat 'identity extensions "\\|")
3601 (case-fold-search t))
3602 (string-match regexp file)))
3604 (defun TeX-strip-extension (&optional string extensions nodir nostrip)
3605 "Return STRING without any trailing extension in EXTENSIONS.
3606 If NODIR is t, also remove directory part of STRING.
3607 If NODIR is `path', remove directory part of STRING if it is equal to
3608 the current directory, `TeX-macro-private' or `TeX-macro-global'.
3609 If NOSTRIP is set, do not remove extension after all.
3610 STRING defaults to the name of the current buffer.
3611 EXTENSIONS defaults to `TeX-file-extensions'."
3614 (setq string (or (buffer-file-name) "<none>")))
3616 (if (null extensions)
3617 (setq extensions TeX-file-extensions))
3619 (let* ((strip (if (and (not nostrip)
3620 (TeX-match-extension string extensions))
3621 (substring string 0 (match-beginning 0))
3623 (dir (expand-file-name (or (file-name-directory strip) "./"))))
3624 (if (or (eq nodir t)
3625 (string-equal dir (expand-file-name "./"))
3626 (member dir TeX-macro-global)
3627 (member dir TeX-macro-private))
3628 (file-name-nondirectory strip)
3631 (defcustom TeX-kpathsea-path-delimiter t
3632 "Path delimiter for kpathsea output.
3633 t means autodetect, nil means kpathsea is disabled."
3635 :type '(choice (const ":")
3637 (const :tag "Autodetect" t)
3638 (const :tag "Off" nil)))
3640 (defcustom TeX-kpathsea-format-alist
3641 '(("tex" "${TEXINPUTS.latex}" TeX-file-extensions)
3642 ("sty" "${TEXINPUTS.latex}" '("sty"))
3643 ("dvi" "${TEXDOCS}" '("dvi" "pdf" "ps" "txt" "html"
3644 "dvi.gz" "pdf.gz" "ps.gz" "txt.gz" "html.gz"
3645 "dvi.bz2" "pdf.bz2" "ps.bz2" "txt.bz2" "html.bz2"))
3646 ("eps" "${TEXINPUTS}" LaTeX-includegraphics-extensions)
3647 ("pdf" "${TEXINPUTS}" LaTeX-includegraphics-extensions)
3648 ("png" "${TEXINPUTS}" LaTeX-includegraphics-extensions)
3649 ("jpg" "${TEXINPUTS}" LaTeX-includegraphics-extensions)
3650 ("jpeg" "${TEXINPUTS}" LaTeX-includegraphics-extensions)
3651 ("bib" "$BIBINPUTS" BibTeX-file-extensions)
3652 ("bst" "$BSTINPUTS" BibTeX-style-extensions))
3653 "Formats to search for expansion using kpathsea.
3654 The key of the alist represents the name of the format. The
3655 first element of the cdr of the alist is string to expand by the
3656 respective kpathsea program and the second element is a list of
3657 file extensions to match."
3659 :type '(alist :key-type string :value-type (group string sexp)))
3661 ;; FIXME: Despite the first parameter named `extensions',
3662 ;; `TeX-search-files-kpathsea' basically treats this as a format
3663 ;; specifier. Only the first element in the respective list will be
3664 ;; used to determine the search paths and file extensions with the
3665 ;; help of `TeX-kpathsea-format-alist'. Out of these differences
3666 ;; arises a need to unify the behavior of `TeX-search-files' and
3667 ;; `TeX-search-files-kpathsea' and their treatment of parameters.
3668 ;; Additionally `TeX-search-files-kpathsea' should be made more
3669 ;; general to work with other platforms and TeX systems as well.
3670 (defun TeX-search-files-kpathsea (extensions nodir strip)
3671 "The kpathsea-enabled version of `TeX-search-files'.
3672 Except for DIRECTORIES (a kpathsea string), the arguments for
3673 EXTENSIONS, NODIR and STRIP are explained there."
3674 (and TeX-kpathsea-path-delimiter
3676 (let* ((format-spec (assoc (car extensions)
3677 TeX-kpathsea-format-alist))
3678 (dirs (with-output-to-string
3681 "kpsewhich" nil (list standard-output nil)
3685 (nth 1 format-spec))))
3686 (if (eq TeX-kpathsea-path-delimiter t)
3688 (setq TeX-kpathsea-path-delimiter nil))
3689 (error "kpsewhich error")))))
3691 (when (eq TeX-kpathsea-path-delimiter t)
3692 (setq TeX-kpathsea-path-delimiter
3693 (cond ((string-match ";" dirs)
3695 ((string-match ":" dirs)
3697 (unless TeX-kpathsea-path-delimiter
3698 (throw 'no-kpathsea nil))
3699 (setq dirs (split-string dirs (concat "[\n\r"
3700 TeX-kpathsea-path-delimiter
3702 (setq extensions (concat "\\."
3703 (regexp-opt (eval (nth 2 format-spec)) t)
3708 (lambda(x) (directory-files x
3714 (if (string-match extensions x)
3715 (substring x 0 (match-beginning 0))
3720 (defun TeX-search-files (&optional directories extensions nodir strip)
3721 "Return a list of all reachable files in DIRECTORIES ending with EXTENSIONS.
3722 If optional argument NODIR is set, remove directory part.
3723 If optional argument STRIP is set, remove file extension.
3724 If optional argument DIRECTORIES is set, search in those directories.
3725 Otherwise, search in all TeX macro directories.
3726 If optional argument EXTENSIONS is not set, use `TeX-file-extensions'"
3727 (if (null extensions)
3728 (setq extensions TeX-file-extensions))
3729 (or (TeX-search-files-kpathsea extensions nodir strip)
3731 (if (null directories)
3733 (cons "./" (append TeX-macro-private TeX-macro-global))))
3735 (TeX-file-recurse (cond ((symbolp TeX-file-recurse)
3737 ((zerop TeX-file-recurse)
3739 ((1- TeX-file-recurse)))))
3741 (let* ((directory (car directories))
3742 (content (and directory
3743 (file-readable-p directory)
3744 (file-directory-p directory)
3745 (directory-files directory))))
3746 (setq directories (cdr directories))
3748 (let ((file (concat directory (car content))))
3749 (setq content (cdr content))
3750 (cond ((string-match TeX-ignore-file file))
3751 ((not (file-readable-p file)))
3752 ((file-directory-p file)
3753 (if TeX-file-recurse
3757 (list (file-name-as-directory file))
3760 ((TeX-match-extension file extensions)
3761 (setq match (cons (TeX-strip-extension file
3768 (defun TeX-car-string-lessp (s1 s2)
3769 "Compare the cars of S1 and S2 in lexicographic order.
3770 Return t if first is less than second in lexicographic order."
3771 (string-lessp (car s1) (car s2)))
3773 (defun TeX-listify (elt)
3774 "Return a newly created list with element ELT.
3775 If ELT already is a list, return ELT."
3776 (if (listp elt) elt (list elt)))
3778 (defun TeX-member (elt list how)
3779 "Return the member ELT in LIST. Comparison done with HOW.
3780 Return nil if ELT is not a member of LIST."
3781 (while (and list (not (funcall how elt (car list))))
3782 (setq list (cdr list)))
3785 (defun TeX-elt-of-list-member (elts list)
3786 "Return non-nil if an element of ELTS is a member of LIST."
3789 (when (member elt list)
3790 (throw 'found t)))))
3792 (defun TeX-assoc (key list)
3793 "Return non-nil if KEY is `equal' to the car of an element of LIST.
3794 Like assoc, except case insensitive."
3795 (let ((case-fold-search t))
3796 (TeX-member key list
3798 (string-match (concat "^" (regexp-quote a) "$")
3801 (defun TeX-match-buffer (n)
3802 "Return the substring corresponding to the N'th match.
3803 See `match-data' for details."
3804 (if (match-beginning n)
3805 (buffer-substring-no-properties (match-beginning n) (match-end n))
3808 (defun TeX-function-p (arg)
3809 "Return non-nil if ARG is callable as a function."
3810 (or (and (fboundp 'byte-code-function-p)
3811 (byte-code-function-p arg))
3813 (eq (car arg) 'lambda))
3817 (defun TeX-booleanp (arg)
3818 "Return non-nil if ARG is t or nil."
3819 (memq arg '(t nil)))
3821 (defun TeX-looking-at-backward (regexp &optional limit)
3822 "Return non-nil if the text before point matches REGEXP.
3823 Optional second argument LIMIT gives a max number of characters
3824 to look backward for."
3825 (let ((pos (point)))
3827 (and (re-search-backward regexp
3828 (if limit (max (point-min) (- (point) limit)))
3830 (eq (match-end 0) pos)))))
3832 (defun TeX-current-line ()
3833 "The current line number."
3834 (format "%d" (1+ (TeX-current-offset))))
3836 (defun TeX-current-file-name-master-relative ()
3837 "Return current filename, relative to master directory."
3840 (TeX-master-directory)))
3842 (defun TeX-near-bobp ()
3843 "Return t iff there's nothing but whitespace between (bob) and (point)."
3845 (skip-chars-backward " \t\n")
3848 (defun TeX-deactivate-mark ()
3849 "Deactivate the mark.
3850 This is a compatibility function which works both in Emacs and
3851 XEmacs. In XEmacs the region is deactivated instead of the
3852 mark which is sort of equivalent."
3853 (if (featurep 'xemacs)
3854 (zmacs-deactivate-region)
3857 (defalias 'TeX-run-mode-hooks
3858 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks))
3863 (defvar TeX-mode-syntax-table (make-syntax-table)
3864 "Syntax table used while in TeX mode.")
3866 (make-variable-buffer-local 'TeX-mode-syntax-table)
3868 (progn ; Define TeX-mode-syntax-table.
3869 (modify-syntax-entry (string-to-char TeX-esc)
3870 "\\" TeX-mode-syntax-table)
3871 (modify-syntax-entry ?\f ">" TeX-mode-syntax-table)
3872 (modify-syntax-entry ?\n ">" TeX-mode-syntax-table)
3873 (modify-syntax-entry (string-to-char TeX-grop)
3874 (concat "(" TeX-grcl)
3875 TeX-mode-syntax-table)
3876 (modify-syntax-entry (string-to-char TeX-grcl)
3877 (concat ")" TeX-grop)
3878 TeX-mode-syntax-table)
3879 (modify-syntax-entry ?% "<" TeX-mode-syntax-table)
3880 (modify-syntax-entry ?\" "." TeX-mode-syntax-table)
3881 (modify-syntax-entry ?& "." TeX-mode-syntax-table)
3882 (modify-syntax-entry ?_ "." TeX-mode-syntax-table)
3883 (modify-syntax-entry ?@ "_" TeX-mode-syntax-table)
3884 (modify-syntax-entry ?~ "." TeX-mode-syntax-table)
3885 (modify-syntax-entry ?$ "$" TeX-mode-syntax-table)
3886 (modify-syntax-entry ?' "w" TeX-mode-syntax-table)
3887 (modify-syntax-entry ?« "." TeX-mode-syntax-table)
3888 (modify-syntax-entry ?» "." TeX-mode-syntax-table))
3892 (defvar TeX-command-current 'TeX-command-master
3893 "Specify whether to run command on master, buffer or region.")
3894 ;; Function used to run external command.
3896 (defun TeX-command-select-master ()
3897 "Determine that the next command will be on the master file."
3899 (message "Next command will be on the master file.")
3900 (setq TeX-command-current 'TeX-command-master))
3902 (defun TeX-command-select-buffer ()
3903 "Determine that the next command will be on the buffer."
3905 (message "Next command will be on the buffer")
3906 (setq TeX-command-current 'TeX-command-buffer))
3908 (defun TeX-command-select-region ()
3909 "Determine that the next command will be on the region."
3911 (message "Next command will be on the region")
3912 (setq TeX-command-current 'TeX-command-region))
3914 (defvar TeX-command-force nil)
3915 ;; If non-nil, TeX-command-query will return the value of this
3916 ;; variable instead of quering the user.
3918 (defun TeX-command-menu (name)
3919 "Execute `TeX-command-list' NAME from a menu."
3920 (let ((TeX-command-force name))
3921 (funcall TeX-command-current)))
3923 (defun TeX-command-menu-print (printer command name)
3924 "Print on PRINTER using method COMMAND to run NAME."
3925 (let ((TeX-printer-default (unless (string= printer "Other") printer))
3926 (TeX-printer-list (and (string= printer "Other") TeX-printer-list))
3927 (TeX-print-command command)
3928 (TeX-queue-command command))
3929 (TeX-command-menu name)))
3931 (defun TeX-command-menu-printer-entry (entry lookup command name)
3932 "Return `TeX-printer-list' ENTRY as a menu item."
3933 (vector (nth 0 entry)
3934 (list 'TeX-command-menu-print
3936 (or (nth lookup entry) command)
3939 (defun TeX-command-menu-entry (entry)
3940 "Return `TeX-command-list' ENTRY as a menu item."
3941 (let ((name (car entry)))
3942 (cond ((and (string-equal name TeX-command-Print)
3944 (cons TeX-command-Print
3945 (mapcar (lambda (entry)
3946 (TeX-command-menu-printer-entry
3947 entry 1 TeX-print-command name))
3948 (append TeX-printer-list '(("Other"))))))
3949 ((and (string-equal name TeX-command-Queue)
3951 (cons TeX-command-Queue
3952 (mapcar (lambda (entry)
3953 (TeX-command-menu-printer-entry
3954 entry 2 TeX-queue-command name))
3955 (append TeX-printer-list '(("Other"))))))
3957 (vconcat `(,name (TeX-command-menu ,name))
3958 (nthcdr 5 entry))))))
3960 (defconst TeX-command-menu-name "Command"
3961 "Name to be displayed for the command menu in all modes defined by AUCTeX.")
3965 (defcustom TeX-electric-escape nil
3966 "If non-nil, ``\\'' will be bound to `TeX-electric-macro'."
3970 (defcustom TeX-electric-sub-and-superscript nil
3971 "If non-nil, insert braces after typing `^' and `_' in math mode."
3975 (defcustom TeX-newline-function 'newline
3976 "Function to be called upon pressing `RET'."
3977 :group 'TeX-indentation
3978 :type '(choice (const newline)
3979 (const newline-and-indent)
3980 (const reindent-then-newline-and-indent)
3981 (sexp :tag "Other")))
3983 (defun TeX-insert-backslash (arg)
3984 "Either insert typed key ARG times or call `TeX-electric-macro'.
3985 `TeX-electric-macro' will be called if `TeX-electric-escape' is non-nil."
3987 (if TeX-electric-escape
3988 (TeX-electric-macro)
3989 (self-insert-command arg)))
3991 (defun TeX-insert-sub-or-superscript (arg)
3992 "Insert typed key ARG times and possibly a pair of braces.
3993 Brace insertion is only done if point is in a math construct and
3994 `TeX-electric-sub-and-superscript' has a non-nil value."
3996 (self-insert-command arg)
3997 (when (and TeX-electric-sub-and-superscript (texmathp))
3998 (insert (concat TeX-grop TeX-grcl))
4001 (defun TeX-newline ()
4002 "Call the function specified by the variable `TeX-newline-function'."
4003 (interactive) (funcall TeX-newline-function))
4005 (defvar TeX-mode-map
4006 (let ((map (make-sparse-keymap)))
4008 ;; (define-key map "\177" 'backward-delete-char-untabify)
4009 (define-key map "\C-c}" 'up-list)
4010 (define-key map "\C-c#" 'TeX-normal-mode)
4011 (define-key map "\C-c\C-n" 'TeX-normal-mode)
4012 (define-key map "\C-c?" 'TeX-doc)
4013 (define-key map "\C-c\C-i" 'TeX-goto-info-page)
4014 (define-key map "\r" 'TeX-newline)
4017 (define-key map "\"" 'TeX-insert-quote)
4018 (define-key map "$" 'TeX-insert-dollar)
4019 ;; Removed because LaTeX 2e have a better solution to italic correction.
4020 ;; (define-key map "." 'TeX-insert-punctuation)
4021 ;; (define-key map "," 'TeX-insert-punctuation)
4022 (define-key map "\C-c{" 'TeX-insert-braces)
4023 (define-key map "\C-c\C-f" 'TeX-font)
4024 (define-key map "\C-c\C-m" 'TeX-insert-macro)
4025 (define-key map "\\" 'TeX-insert-backslash)
4026 (define-key map "^" 'TeX-insert-sub-or-superscript)
4027 (define-key map "_" 'TeX-insert-sub-or-superscript)
4028 (define-key map "\e\t" 'TeX-complete-symbol) ;*** Emacs 19 way
4030 (define-key map "\C-c'" 'TeX-comment-or-uncomment-paragraph) ;*** Old way
4031 (define-key map "\C-c:" 'TeX-comment-or-uncomment-region) ;*** Old way
4032 (define-key map "\C-c\"" 'TeX-uncomment) ;*** Old way
4034 (define-key map "\C-c;" 'TeX-comment-or-uncomment-region)
4035 (define-key map "\C-c%" 'TeX-comment-or-uncomment-paragraph)
4037 (define-key map "\C-c\C-t\C-p" 'TeX-PDF-mode)
4038 (define-key map "\C-c\C-t\C-i" 'TeX-interactive-mode)
4039 (define-key map "\C-c\C-t\C-s" 'TeX-source-correlate-mode)
4040 (define-key map "\C-c\C-t\C-r" 'TeX-pin-region)
4041 (define-key map "\C-c\C-w" 'TeX-toggle-debug-bad-boxes); to be removed
4042 (define-key map "\C-c\C-t\C-b" 'TeX-toggle-debug-bad-boxes)
4043 (define-key map "\C-c\C-t\C-w" 'TeX-toggle-debug-warnings)
4044 (define-key map "\C-c\C-v" 'TeX-view)
4046 (define-key map "\C-c\C-d" 'TeX-save-document)
4047 (define-key map "\C-c\C-r" 'TeX-command-region)
4048 (define-key map "\C-c\C-b" 'TeX-command-buffer)
4049 (define-key map "\C-c\C-c" 'TeX-command-master)
4050 (define-key map "\C-c\C-k" 'TeX-kill-job)
4051 (define-key map "\C-c\C-l" 'TeX-recenter-output-buffer)
4052 (define-key map "\C-c^" 'TeX-home-buffer)
4053 (define-key map "\C-c`" 'TeX-next-error)
4054 ;; Remap bindings of `next-error'
4055 (if (featurep 'xemacs)
4056 (substitute-key-definition 'next-error 'TeX-next-error map global-map)
4057 (define-key map [remap next-error] 'TeX-next-error))
4058 ;; Remap bindings of `previous-error'
4059 (if (featurep 'xemacs)
4060 (substitute-key-definition 'previous-error 'TeX-previous-error
4062 (define-key map [remap previous-error] 'TeX-previous-error))
4064 (define-key map "\C-c\C-o\C-f" 'TeX-fold-mode)
4067 (define-key map "\C-c_" 'TeX-master-file-ask) ;*** temporary
4069 "Keymap for common TeX and LaTeX commands.")
4071 (defvar plain-TeX-mode-map
4072 (let ((map (make-sparse-keymap)))
4073 (set-keymap-parent map TeX-mode-map)
4075 "Keymap used in plain TeX mode.")
4077 (defun TeX-mode-specific-command-menu (mode)
4078 "Return a Command menu specific to the major MODE."
4079 ;; COMPATIBILITY for Emacs < 21
4080 (if (and (not (featurep 'xemacs))
4081 (= emacs-major-version 20))
4082 (cons TeX-command-menu-name
4083 (TeX-mode-specific-command-menu-entries mode))
4084 (list TeX-command-menu-name
4085 :filter `(lambda (&rest ignored)
4086 (TeX-mode-specific-command-menu-entries ',mode))
4089 (defun TeX-mode-specific-command-menu-entries (mode)
4090 "Return the entries for a Command menu specific to the major MODE."
4094 [ "Master File" TeX-command-select-master
4095 :keys "C-c C-c" :style radio
4096 :selected (eq TeX-command-current 'TeX-command-master)
4097 :help "Commands in this menu work on the Master File"]
4098 [ "Buffer" TeX-command-select-buffer
4099 :keys "C-c C-b" :style radio
4100 :selected (eq TeX-command-current 'TeX-command-buffer)
4101 :help "Commands in this menu work on the current buffer"]
4102 [ "Region" TeX-command-select-region
4103 :keys "C-c C-r" :style radio
4104 :selected (eq TeX-command-current 'TeX-command-region)
4105 :help "Commands in this menu work on the region"]
4106 [ "Fix the Region" TeX-pin-region
4107 :active (or (if prefix-arg
4108 (<= (prefix-numeric-value prefix-arg) 0)
4109 (and (boundp 'TeX-command-region-begin)
4110 (markerp TeX-command-region-begin)))
4112 ;;:visible (eq TeX-command-current 'TeX-command-region)
4114 :selected (and (boundp 'TeX-command-region-begin)
4115 (markerp TeX-command-region-begin))
4116 :help "Fix the region for \"Command on Region\""]
4118 ["Recenter Output Buffer" TeX-recenter-output-buffer
4119 :help "Show the output of current TeX process"]
4120 ["Kill Job" TeX-kill-job
4121 :help "Kill the current TeX process"]
4122 ["Next Error" TeX-next-error
4123 :help "Jump to the next error of the last TeX run"]
4124 ["Quick View" TeX-view
4125 :help "Start a viewer without prompting"]
4128 ,@(mapcar (lambda (x)
4129 (let ((symbol (car x)) (name (nth 1 x)))
4130 `[ ,(format "Use %s engine" name) (TeX-engine-set ',symbol)
4131 :style radio :selected (eq TeX-engine ',symbol)
4132 :help ,(format "Use %s engine for compiling" name) ]))
4135 [ "Generate PDF" TeX-PDF-mode
4136 :style toggle :selected TeX-PDF-mode
4137 :active (not (eq TeX-engine 'omega))
4138 :help "Use PDFTeX to generate PDF instead of DVI"]
4139 [ "Run Interactively" TeX-interactive-mode
4140 :style toggle :selected TeX-interactive-mode :keys "C-c C-t C-i"
4141 :help "Stop on errors in a TeX run"]
4142 [ "Correlate I/O" TeX-source-correlate-mode
4143 :style toggle :selected TeX-source-correlate-mode
4144 :help "Enable forward and inverse search in the previewer"]
4145 ["Debug Bad Boxes" TeX-toggle-debug-bad-boxes
4146 :style toggle :selected TeX-debug-bad-boxes :keys "C-c C-t C-b"
4147 :help "Make \"Next Error\" show overfull and underfull boxes"]
4148 ["Debug Warnings" TeX-toggle-debug-warnings
4149 :style toggle :selected TeX-debug-warnings
4150 :help "Make \"Next Error\" show warnings"])))
4151 (let ((file 'TeX-command-on-current));; is this actually needed?
4152 (TeX-maybe-remove-help
4154 (mapcar 'TeX-command-menu-entry
4155 (TeX-mode-specific-command-list mode)))))))
4157 (defun TeX-mode-specific-command-list (mode)
4158 "Return the list of commands available in the given MODE."
4159 (let ((full-list TeX-command-list)
4162 (while (setq entry (pop full-list))
4163 ;; `(nth 4 entry)' may be either an atom in case of which the
4164 ;; entry should be present in any mode or a list of major modes.
4165 (if (or (atom (nth 4 entry))
4166 (memq mode (nth 4 entry)))
4167 (push entry out-list)))
4168 (nreverse out-list)))
4170 (defvar TeX-fold-menu
4173 ["Fold Mode" TeX-fold-mode
4175 :selected (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4176 :help "Toggle folding mode"]
4178 ["Hide All in Current Buffer" TeX-fold-buffer
4179 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4180 :help "Hide all configured TeX constructs in the current buffer"]
4181 ["Hide All in Current Region" TeX-fold-region
4182 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4183 :help "Hide all configured TeX constructs in the marked region"]
4184 ["Hide All in Current Paragraph" TeX-fold-paragraph
4185 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4186 :help "Hide all configured TeX constructs in the paragraph containing point"]
4187 ["Hide Current Macro" TeX-fold-macro
4188 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4189 :help "Hide the macro containing point"]
4190 ["Hide Current Environment" TeX-fold-env
4191 :visible (not (eq major-mode 'plain-tex-mode))
4192 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4193 :help "Hide the environment containing point"]
4194 ["Hide Current Comment" TeX-fold-comment
4195 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4196 :help "Hide the comment containing point"]
4198 ["Show All in Current Buffer" TeX-fold-clearout-buffer
4199 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4200 :help "Permanently show all folded content again"]
4201 ["Show All in Current Region" TeX-fold-clearout-region
4202 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4203 :help "Permanently show all folded content in marked region"]
4204 ["Show All in Current Paragraph" TeX-fold-clearout-paragraph
4205 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4206 :help "Permanently show all folded content in paragraph containing point"]
4207 ["Show Current Item" TeX-fold-clearout-item
4208 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4209 :help "Permanently show the item containing point"]
4211 ["Hide or Show Current Item" TeX-fold-dwim
4212 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4213 :help "Hide or show the item containing point"]))
4214 "Menu definition for commands from tex-fold.el.")
4217 ;;; Menus for plain TeX mode
4218 (easy-menu-define plain-TeX-mode-command-menu
4220 "Command menu used in TeX mode."
4221 (TeX-mode-specific-command-menu 'plain-tex-mode))
4223 (defvar TeX-customization-menu nil)
4225 (defvar TeX-common-menu-entries
4227 `(("Multifile/Parsing"
4228 ["Switch to Master File" TeX-home-buffer
4229 :help "Switch to buffer of Master File, or buffer of last TeX command"]
4230 ["Save Document" TeX-save-document
4231 :help "Save all buffers associated with the current Master File"]
4232 ["Set Master File" TeX-master-file-ask
4233 :active (not (TeX-local-master-p))
4234 :help "Set the main file to run TeX commands on"]
4235 ["Reset Buffer" TeX-normal-mode
4236 :help "Save and reparse the current buffer for style information"]
4237 ["Reset AUCTeX" (TeX-normal-mode t) :keys "C-u C-c C-n"
4238 :help "Reset buffer and reload AUCTeX style files"])
4239 ["Find Documentation..." TeX-doc
4240 :help "Get help on commands, packages, or TeX-related topics in general"]
4241 ["Read the AUCTeX Manual" TeX-goto-info-page
4242 :help "Everything worth reading"]
4245 (customize-group 'AUCTeX)
4246 :help "Open the customization buffer for AUCTeX"]
4251 ;; Ugly hack because docTeX mode uses the LaTeX menu.
4252 (list (if (eq major-mode 'doctex-mode) "LaTeX" TeX-base-mode-name))
4253 (or TeX-customization-menu
4254 (setq TeX-customization-menu
4255 (customize-menu-create 'AUCTeX "Customize AUCTeX")))))
4256 :help "Make this menu a full-blown customization menu"])
4257 ["Report AUCTeX Bug" TeX-submit-bug-report
4258 :help ,(format "Problems with AUCTeX %s? Mail us!"
4261 (defvar plain-TeX-menu-entries
4263 `(["Macro..." TeX-insert-macro
4264 :help "Insert a macro and possibly arguments"]
4265 ["Complete" TeX-complete-symbol
4266 :help "Complete the current macro"]
4269 ["Emphasize" (TeX-font nil ?\C-e) :keys "C-c C-f C-e"]
4270 ["Bold" (TeX-font nil ?\C-b) :keys "C-c C-f C-b"]
4271 ["Typewriter" (TeX-font nil ?\C-t) :keys "C-c C-f C-t"]
4272 ["Small Caps" (TeX-font nil ?\C-c) :keys "C-c C-f C-c"]
4273 ["Sans Serif" (TeX-font nil ?\C-f) :keys "C-c C-f C-f"]
4274 ["Italic" (TeX-font nil ?\C-i) :keys "C-c C-f C-i"]
4275 ["Slanted" (TeX-font nil ?\C-s) :keys "C-c C-f C-s"]
4276 ["Roman" (TeX-font nil ?\C-r) :keys "C-c C-f C-r"]
4277 ["Calligraphic" (TeX-font nil ?\C-a) :keys "C-c C-f C-a"])
4279 ["Emphasize" (TeX-font t ?\C-e) :keys "C-u C-c C-f C-e"]
4280 ["Bold" (TeX-font t ?\C-b) :keys "C-u C-c C-f C-b"]
4281 ["Typewriter" (TeX-font t ?\C-t) :keys "C-u C-c C-f C-t"]
4282 ["Small Caps" (TeX-font t ?\C-c) :keys "C-u C-c C-f C-c"]
4283 ["Sans Serif" (TeX-font t ?\C-f) :keys "C-u C-c C-f C-f"]
4284 ["Italic" (TeX-font t ?\C-i) :keys "C-u C-c C-f C-i"]
4285 ["Slanted" (TeX-font t ?\C-s) :keys "C-u C-c C-f C-s"]
4286 ["Roman" (TeX-font t ?\C-r) :keys "C-u C-c C-f C-r"]
4287 ["Calligraphic" (TeX-font t ?\C-a) :keys "C-u C-c C-f C-a"])
4288 ["Delete Font" (TeX-font t ?\C-d) :keys "C-c C-f C-d"]
4290 ["Comment or Uncomment Region" TeX-comment-or-uncomment-region
4291 :help "Comment or uncomment the currently selected region"]
4292 ["Comment or Uncomment Paragraph" TeX-comment-or-uncomment-paragraph
4293 :help "Comment or uncomment the paragraph containing point"]
4295 "-" . ,TeX-common-menu-entries)))
4297 (easy-menu-define plain-TeX-mode-menu
4299 "Menu used in plain TeX mode."
4300 (cons "TeX" plain-TeX-menu-entries))
4304 (defvar AmSTeX-mode-map
4305 (let ((map (make-sparse-keymap)))
4306 (set-keymap-parent map TeX-mode-map)
4308 "Keymap used in `AmSTeX-mode'.")
4310 ;; Menu for AmSTeX mode
4311 (easy-menu-define AmSTeX-mode-command-menu
4313 "Command menu used in AmsTeX mode."
4314 (TeX-mode-specific-command-menu 'ams-tex-mode))
4316 (easy-menu-define AmSTeX-mode-menu
4318 "Menu used in AMS-TeX mode."
4319 (cons "AmS-TeX" plain-TeX-menu-entries))
4322 (defun ams-tex-mode ()
4323 "Major mode in AUCTeX for editing AmS-TeX files.
4324 See info under AUCTeX for documentation.
4329 Entering AmS-tex-mode calls the value of `text-mode-hook',
4330 then the value of `TeX-mode-hook', and then the value
4331 of `AmS-TeX-mode-hook'."
4333 (plain-TeX-common-initialization)
4334 (setq major-mode 'ams-tex-mode)
4335 (use-local-map AmSTeX-mode-map)
4338 (easy-menu-add AmSTeX-mode-menu AmSTeX-mode-map)
4339 (easy-menu-add AmSTeX-mode-command-menu AmSTeX-mode-map)
4341 (setq TeX-base-mode-name "AmS-TeX")
4342 (setq TeX-command-default "AmSTeX")
4343 (TeX-run-mode-hooks 'text-mode-hook 'TeX-mode-hook 'AmS-TeX-mode-hook)
4344 (TeX-set-mode-name))
4347 ;;; Verbatim constructs
4349 (defvar TeX-verbatim-p-function nil
4350 "Mode-specific function to be called by `TeX-verbatim-p'.")
4351 (make-variable-buffer-local 'TeX-verbatim-p-function)
4353 ;; XXX: We only have an implementation for LaTeX mode at the moment (Oct 2009).
4354 (defun TeX-verbatim-p (&optional pos)
4355 "Return non-nil if position POS is in a verbatim-like construct.
4356 A mode-specific implementation is required. If it is not
4357 available, the function always returns nil."
4358 (when TeX-verbatim-p-function
4359 (funcall TeX-verbatim-p-function)))
4364 (defvar TeX-comment-start-regexp "%"
4365 "Regular expression matching a comment starter.
4366 Unlike the variable `comment-start-skip' it should not match any
4367 whitespace after the comment starter or any character before it.")
4368 (make-variable-buffer-local 'TeX-comment-start-regexp)
4370 (defun TeX-comment-region (beg end &optional arg)
4371 "Comment each line in the region from BEG to END.
4372 Numeric prefix arg ARG means use ARG comment characters.
4373 If ARG is negative, delete that many comment characters instead."
4374 (interactive "*r\nP")
4375 ;; `comment-padding' will not be recognized in XEmacs' (21.4)
4376 ;; `comment-region', so we temporarily modify `comment-start' to get
4377 ;; proper spacing. Unfortunately we have to check for the XEmacs
4378 ;; version and cannot test if `comment-padding' is bound as this
4379 ;; gets initialized in `VirTeX-common-initialization'.
4380 (let ((comment-start (if (and (featurep 'xemacs)
4381 (= emacs-major-version 21)
4382 (<= emacs-minor-version 4))
4383 (concat comment-start (TeX-comment-padding-string))
4385 (comment-region beg end arg)))
4388 ;; COMPATIBILITY for Emacs <= 21.3
4389 (if (fboundp 'comment-or-uncomment-region)
4390 (defalias 'TeX-comment-or-uncomment-region 'comment-or-uncomment-region)
4391 ;; The following function was copied from `newcomment.el' on
4392 ;; 2004-01-30 and adapted accordingly
4393 (defun TeX-comment-or-uncomment-region (beg end &optional arg)
4394 "Comment or uncomment a the region from BEG to END.
4395 Call `TeX-comment-region', unless the region only consists of
4396 comments, in which case call `TeX-uncomment-region'. If a prefix
4397 arg ARG is given, it is passed on to the respective function."
4398 (interactive "*r\nP")
4399 (funcall (if (save-excursion ;; check for already commented region
4401 (TeX-comment-forward (point-max))
4403 'TeX-uncomment-region 'TeX-comment-region)
4406 ;; COMPATIBILITY for Emacs <= 20. (Introduced in 21.1?)
4407 (if (fboundp 'uncomment-region)
4408 (defalias 'TeX-uncomment-region 'uncomment-region)
4409 (defun TeX-uncomment-region (beg end &optional arg)
4410 "Remove comment characters from the beginning of each line
4411 in the region from BEG to END. Numeric prefix arg ARG means use
4412 ARG comment characters. If ARG is negative, delete that many
4413 comment characters instead."
4414 (interactive "*r\nP")
4416 ;; Determine the number of comment characters at the
4417 ;; beginning of the first commented line.
4422 (concat "^" TeX-comment-start-regexp "+") end t)
4423 (length (match-string 0)))))
4424 (comment-region beg end (- arg)))))
4426 (defun TeX-uncomment ()
4427 "Delete comment characters from the beginning of each line in a comment."
4430 ;; Find first comment line
4432 (while (and (looking-at (concat "^[ \t]*" TeX-comment-start-regexp))
4435 (let ((beg (point)))
4437 ;; Find last comment line
4438 (while (and (looking-at (concat "^[ \t]*" TeX-comment-start-regexp))
4442 (TeX-uncomment-region beg (point)))))
4444 (defun TeX-comment-or-uncomment-paragraph ()
4445 "Comment or uncomment current paragraph."
4447 (if (TeX-in-commented-line)
4451 ;; Don't do anything if we are in an empty line. If this line
4452 ;; is followed by a lot of commented lines, this shall prevent
4453 ;; that mark-paragraph skips over these lines and marks a
4454 ;; paragraph outside the visible window which might get
4455 ;; commented without the user noticing.
4456 (unless (looking-at "^[ \t]*$")
4458 (TeX-comment-region (point) (mark))))))
4460 (defun TeX-in-comment ()
4461 "Return non-nil if point is in a comment."
4463 (null comment-start-skip)
4464 (eq (preceding-char) ?\r))
4468 (let ((pos (point)))
4470 (and (or (looking-at comment-start-skip)
4471 (re-search-forward comment-start-skip pos t))
4472 (not (TeX-verbatim-p))))))))
4474 (defun TeX-in-commented-line ()
4475 "Return non-nil if point is in a line consisting only of a comment.
4476 The comment can be preceded by whitespace. This means that
4477 `TeX-in-commented-line' is more general than `TeX-in-line-comment'
4478 which will not match commented lines with leading whitespace. But
4479 `TeX-in-commented-line' will match commented lines without leading
4480 whitespace as well."
4483 (skip-chars-forward " \t")
4484 (string= (buffer-substring-no-properties
4485 (point) (min (point-max) (+ (point) (length comment-start))))
4488 (defun TeX-in-line-comment ()
4489 "Return non-nil if point is in a line comment.
4490 A line comment is a comment starting in column one, i.e. there is
4491 no whitespace before the comment sign."
4494 (string= (buffer-substring-no-properties
4495 (point) (min (point-max) (+ (point) (length comment-start))))
4498 (defun TeX-comment-prefix ()
4499 "Return the comment prefix of the current line.
4500 If there are no comment starters after potential whitespace at
4501 the beginning of the line, return nil."
4505 (when (looking-at (concat "\\([ \t]*" TeX-comment-start-regexp "+\\)+"))
4506 (match-string 0)))))
4508 (defun TeX-forward-comment-skip (&optional count limit)
4509 "Move forward to the next comment skip.
4510 This may be a switch between commented and not commented adjacent
4511 lines or between lines with different comment prefixes. With
4512 argument COUNT do it COUNT times. If argument LIMIT is given, do
4513 not move point further than this value."
4514 (unless count (setq count 1))
4515 ;; A value of 0 is nonsense.
4516 (when (= count 0) (setq count 1))
4517 (unless limit (setq limit (point-max)))
4518 (dotimes (i (abs count))
4521 (beginning-of-line))
4522 (let ((prefix (when (looking-at (concat "\\([ \t]*"
4523 TeX-comment-start-regexp "+\\)+"))
4524 (buffer-substring (+ (line-beginning-position)
4525 (current-indentation))
4527 (while (save-excursion
4528 (and (if (> count 0)
4531 (zerop (if (> count 0)
4535 (if (looking-at (concat "\\([ \t]*"
4536 TeX-comment-start-regexp
4538 ;; If the preceding line is a commented line
4539 ;; as well, check if the prefixes are
4543 (+ (line-beginning-position)
4544 (current-indentation))
4547 (not (looking-at (concat "[ \t]*"
4548 TeX-comment-start-regexp))))))
4553 (forward-line 1)))))
4555 (defun TeX-backward-comment-skip (&optional count limit)
4556 "Move backward to the next comment skip.
4557 This may be a switch between commented and not commented adjacent
4558 lines or between lines with different comment prefixes. With
4559 argument COUNT do it COUNT times. If argument LIMIT is given, do
4560 not move point to a position less than this value."
4561 (unless count (setq count 1))
4562 (when (= count 0) (setq count 1))
4563 (unless limit (setq limit (point-min)))
4564 (TeX-forward-comment-skip (- count) limit))
4566 ;; Taken from `comment-forward' in Emacs' CVS on 2006-12-26. Used as
4567 ;; a compatibility function for XEmacs 21.4.
4568 (defun TeX-comment-forward (&optional n)
4569 "Skip forward over N comments.
4570 Just like `forward-comment' but only for positive N
4571 and can use regexps instead of syntax."
4572 (when (fboundp 'comment-normalize-vars)
4573 (comment-normalize-vars))
4574 (if (fboundp 'comment-forward)
4577 (if (< n 0) (error "No comment-backward")
4578 (if comment-use-syntax (forward-comment n)
4581 (if (or (forward-comment 1)
4582 (and (looking-at comment-start-skip)
4583 (goto-char (match-end 0))
4584 (re-search-forward comment-end-skip nil 'move)))
4588 (defun TeX-comment-padding-string ()
4589 "Return comment padding as a string.
4590 The variable `comment-padding' can hold an integer or a string.
4591 This function will return the appropriate string representation
4592 regardless of its data type."
4593 (if (integerp comment-padding)
4594 (make-string comment-padding ? )
4600 (defgroup TeX-indentation nil
4601 "Indentation of TeX buffers in AUCTeX."
4604 (defcustom TeX-brace-indent-level 2
4605 "*The level of indentation produced by an open brace."
4606 :group 'TeX-indentation
4609 (defun TeX-comment-indent ()
4610 "Determine the indentation of a comment."
4611 (if (looking-at "%%%")
4613 (skip-chars-backward " \t")
4614 (max (if (bolp) 0 (1+ (current-column)))
4617 (defun TeX-brace-count-line ()
4618 "Count number of open/closed braces."
4620 (let ((count 0) (limit (line-end-position)) char)
4622 (skip-chars-forward "^{}\\\\" limit)
4623 (when (and (< (point) limit) (not (TeX-in-comment)))
4624 (setq char (char-after))
4626 (cond ((eq char ?\{)
4627 (setq count (+ count TeX-brace-indent-level)))
4629 (setq count (- count TeX-brace-indent-level)))
4631 (when (< (point) limit)
4638 (defvar TeX-search-syntax-table
4639 (let ((table (make-syntax-table (make-char-table (if (featurep 'xemacs)
4642 ;; Preset mode-independent syntax entries. (Mode-dependent
4643 ;; entries are set in the function `TeX-search-syntax-table'.)
4644 ;; ?\", ?\( and ?\) explicitely get whitespace syntax because
4645 ;; Emacs 21.3 and XEmacs don't generate a completely empty syntax
4647 (dolist (elt '((?\f . ">") (?\n . ">") (?\" . " ") (?\( . " ") (?\) . " ")))
4648 (modify-syntax-entry (car elt) (cdr elt) table))
4650 "Syntax table used for searching purposes.
4651 It should be accessed through the function `TeX-search-syntax-table'.")
4653 (defun TeX-search-syntax-table (&rest args)
4654 "Return a syntax table for searching purposes.
4655 ARGS may be a list of characters. For each of them the
4656 respective predefined syntax is set. Currently the parenthetical
4657 characters ?{, ?}, ?[, ?], ?\(, ?\), ?<, and ?> are supported.
4658 The syntax of each of these characters not specified will be
4660 (let ((char-syntax-alist '((?\{ . "(}") (?\} . "){")
4661 (?\[ . "(]") (?\] . ")[")
4662 (?\( . "()") (?\) . ")(")
4663 (?\< . "(>") (?\> . ")<"))))
4664 ;; Clean entries possibly set before.
4665 (modify-syntax-entry ?\\ " " TeX-search-syntax-table)
4666 (modify-syntax-entry ?@ " " TeX-search-syntax-table)
4667 (modify-syntax-entry ?\% " " TeX-search-syntax-table)
4668 ;; Preset mode-dependent syntax entries. (Mode-independent entries
4669 ;; are set when the variable `TeX-search-syntax-table' is created.)
4670 (modify-syntax-entry (string-to-char TeX-esc) "\\" TeX-search-syntax-table)
4671 (unless (eq major-mode 'texinfo-mode)
4672 (modify-syntax-entry ?\% "<" TeX-search-syntax-table))
4673 ;; Clean up the entries which can be specified as arguments.
4674 (dolist (elt char-syntax-alist)
4675 (modify-syntax-entry (car elt) " " TeX-search-syntax-table))
4676 ;; Now set what we got.
4678 (unless (assoc elt char-syntax-alist) (error "Char not supported"))
4679 (modify-syntax-entry elt (cdr (assoc elt char-syntax-alist))
4680 TeX-search-syntax-table))
4681 ;; Return the syntax table.
4682 TeX-search-syntax-table))
4684 (defun TeX-find-balanced-brace (&optional count depth limit)
4685 "Return the position of a balanced brace in a TeX group.
4686 The function scans forward COUNT parenthetical groupings.
4687 Default is 1. If COUNT is negative, it searches backwards. With
4688 optional DEPTH>=1, find that outer level. If LIMIT is non-nil,
4689 do not search further than this position in the buffer."
4690 (let ((count (if count
4691 (if (= count 0) (error "COUNT has to be <> 0") count)
4694 (if (< depth 1) (error "DEPTH has to be > 0") depth)
4699 (narrow-to-region (point-min) limit)
4700 (narrow-to-region limit (point-max))))
4701 (with-syntax-table (TeX-search-syntax-table ?\{ ?\})
4703 (scan-lists (point) count depth)
4706 (defun TeX-find-closing-brace (&optional depth limit)
4707 "Return the position of the closing brace in a TeX group.
4708 The function assumes that point is inside the group, i.e. after
4709 an opening brace. With optional DEPTH>=1, find that outer level.
4710 If LIMIT is non-nil, do not search further down than this
4711 position in the buffer."
4712 (TeX-find-balanced-brace 1 depth limit))
4714 (defun TeX-find-opening-brace (&optional depth limit)
4715 "Return the position of the opening brace in a TeX group.
4716 The function assumes that point is inside the group, i.e. before
4717 a closing brace. With optional DEPTH>=1, find that outer level.
4718 If LIMIT is non-nil, do not search further up than this position
4720 (TeX-find-balanced-brace -1 depth limit))
4722 (defun TeX-find-macro-boundaries (&optional lower-bound)
4723 "Return a list containing the start and end of a macro.
4724 If LOWER-BOUND is given, do not search backward further than this
4725 point in buffer. Arguments enclosed in brackets or braces are
4726 considered part of the macro."
4729 (narrow-to-region lower-bound (point-max)))
4730 (let ((orig-point (point))
4732 ;; Point is located directly at the start of a macro. (-!-\foo{bar})
4733 (when (and (eq (char-after) (aref TeX-esc 0))
4734 (not (TeX-escaped-p)))
4735 (setq start-point (point)))
4736 ;; Point is located on a macro. (\fo-!-o{bar})
4739 (skip-chars-backward "A-Za-z@*")
4740 (when (and (eq (char-before) (aref TeX-esc 0))
4741 (not (TeX-escaped-p (1- (point)))))
4742 (setq start-point (1- (point))))))
4743 ;; Point is located in the argument of a macro. (\foo{ba-!-r})
4747 (let ((parse-sexp-ignore-comments t))
4748 (when (condition-case nil (progn (up-list) t) (error nil))
4750 (condition-case nil (backward-sexp)
4751 (error (throw 'abort nil)))
4752 (forward-comment -1)
4753 (and (memq (char-before) '(?\] ?\}))
4754 (not (TeX-escaped-p (1- (point)))))))
4755 (skip-chars-backward "A-Za-z@*")
4756 (when (and (eq (char-before) (aref TeX-esc 0))
4757 (not (TeX-escaped-p (1- (point)))))
4758 (setq start-point (1- (point)))))))))
4759 ;; Search forward for the end of the macro.
4762 (goto-char (TeX-find-macro-end-helper start-point))
4763 (if (< orig-point (point))
4764 (cons start-point (point))
4767 (defun TeX-find-macro-end-helper (start)
4768 "Find the end of a macro given its START.
4769 START is the position just before the starting token of the macro.
4770 If the macro is followed by square brackets or curly braces,
4771 those will be considered part of it."
4775 (goto-char (1+ start))
4776 (if (zerop (skip-chars-forward "A-Za-z@"))
4778 (skip-chars-forward "*"))
4781 ;; Skip over pairs of square brackets
4782 ((or (looking-at "[ \t]*\n?\\(\\[\\)") ; Be conservative: Consider
4783 ; only consecutive lines.
4784 (and (looking-at (concat "[ \t]*" TeX-comment-start-regexp))
4787 (looking-at "[ \t]*\\(\\[\\)"))))
4788 (goto-char (match-beginning 1))
4791 (scan-error (throw 'found (point)))))
4792 ;; Skip over pairs of curly braces
4793 ((or (looking-at "[ \t]*\n?{") ; Be conservative: Consider
4794 ; only consecutive lines.
4795 (and (looking-at (concat "[ \t]*" TeX-comment-start-regexp))
4798 (looking-at "[ \t]*{"))))
4799 (goto-char (match-end 0))
4800 (goto-char (or (TeX-find-closing-brace)
4801 ;; If we cannot find a regular end, use the
4803 (save-excursion (skip-chars-forward "^ \t\n")
4805 (when (eobp) (throw 'found (point))))
4807 (throw 'found (point)))))))))
4809 (defun TeX-find-macro-start (&optional limit)
4810 "Return the start of a macro.
4811 If LIMIT is given, do not search backward further than this point
4812 in buffer. Arguments enclosed in brackets or braces are
4813 considered part of the macro."
4814 (car (TeX-find-macro-boundaries limit)))
4816 (defun TeX-find-macro-end ()
4817 "Return the end of a macro.
4818 Arguments enclosed in brackets or braces are considered part of
4820 (cdr (TeX-find-macro-boundaries)))
4822 (defun TeX-search-forward-unescaped (string &optional bound noerror)
4823 "Search forward from point for unescaped STRING.
4824 The optional argument BOUND limits the search to the respective
4826 If NOERROR is non-nil, return nil if the search failed instead of
4828 A pattern is escaped, if it is preceded by an odd number of escape
4830 (TeX-search-unescaped string 'forward nil bound noerror))
4832 (defun TeX-search-backward-unescaped (string &optional bound noerror)
4833 "Search backward from point for unescaped STRING.
4834 The optional argument BOUND limits the search to the respective
4836 If NOERROR is non-nil, return nil if the search failed instead of
4838 A pattern is escaped, if it is preceded by an odd number of escape
4840 (TeX-search-unescaped string 'backward nil bound noerror))
4842 (defun TeX-re-search-forward-unescaped (regexp &optional bound noerror)
4843 "Search forward from point for unescaped regular expression REGEXP.
4844 The optional argument BOUND limits the search to the respective
4846 If NOERROR is non-nil, return nil if the search failed instead of
4848 A pattern is escaped, if it is preceded by an odd number of escape
4850 (TeX-search-unescaped regexp 'forward t bound noerror))
4852 (defun TeX-search-unescaped (pattern
4853 &optional direction regexp-flag bound noerror)
4854 "Search for unescaped PATTERN in a certain DIRECTION.
4855 DIRECTION can be indicated by the symbols 'forward and 'backward.
4856 If DIRECTION is omitted, a forward search is carried out.
4857 If REGEXP-FLAG is non-nil, PATTERN may be a regular expression,
4859 The optional argument BOUND limits the search to the respective
4861 If NOERROR is non-nil, return nil if the search failed instead of
4863 A pattern is escaped, if it is preceded by an odd number of escape
4865 (let ((search-fun (if (eq direction 'backward)
4866 (if regexp-flag 're-search-backward 'search-backward)
4867 (if regexp-flag 're-search-forward 'search-forward))))
4869 (while (funcall search-fun pattern bound noerror)
4870 (when (not (TeX-escaped-p (match-beginning 0)))
4871 (throw 'found (point)))))))
4873 (defun TeX-escaped-p (&optional pos)
4874 "Return t if the character at position POS is escaped.
4875 If POS is omitted, examine the character at point.
4876 A character is escaped if it is preceded by an odd number of
4877 escape characters, such as \"\\\" in LaTeX."
4879 (when pos (goto-char pos))
4880 (not (zerop (mod (skip-chars-backward (regexp-quote TeX-esc)) 2)))))
4882 (defun TeX-current-macro ()
4883 "Return the name of the macro containing point, nil if there is none."
4884 (let ((macro-start (TeX-find-macro-start)))
4887 (goto-char macro-start)
4888 (forward-char (length TeX-esc))
4889 (buffer-substring-no-properties
4890 (point) (progn (skip-chars-forward "@A-Za-z") (point)))))))
4892 (defvar TeX-search-forward-comment-start-function nil
4893 "Function to find the start of a comment.
4894 The function should accept an optional argument for specifying
4895 the limit of the search. It should return the position just
4896 before the comment if one is found and nil otherwise. Point
4897 should not be moved.")
4898 (make-variable-buffer-local 'TeX-search-forward-comment-start-function)
4900 (defun TeX-search-forward-comment-start (&optional limit)
4901 "Search forward for a comment start from current position till LIMIT.
4902 If LIMIT is omitted, search till the end of the buffer.
4904 The search relies on `TeX-comment-start-regexp' being set
4905 correctly for the current mode.
4907 Set `TeX-search-forward-comment-start-defun' in order to override
4908 the default implementation."
4909 (if TeX-search-forward-comment-start-function
4910 (funcall TeX-search-forward-comment-start-function limit)
4911 (setq limit (or limit (point-max)))
4912 (when (TeX-re-search-forward-unescaped TeX-comment-start-regexp limit t)
4913 (match-beginning 0))))
4917 (defcustom TeX-font-list '((?\C-b "{\\bf " "}")
4918 (?\C-c "{\\sc " "}")
4919 (?\C-e "{\\em " "\\/}")
4920 (?\C-i "{\\it " "\\/}")
4921 (?\C-r "{\\rm " "}")
4922 (?\C-s "{\\sl " "\\/}")
4923 (?\C-t "{\\tt " "}")
4925 "List of fonts used by `TeX-font'.
4927 Each entry is a list.
4928 The first element is the key to activate the font.
4929 The second element is the string to insert before point, and the third
4930 element is the string to insert after point.
4931 If the fourth and fifth element are strings, they specify the prefix and
4932 suffix to be used in math mode.
4933 An optional fourth (or sixth) element means always replace if t."
4937 :value (?\C-a "" "")
4938 (character :tag "Key")
4939 (string :tag "Prefix")
4940 (string :tag "Suffix")
4943 (string :tag "Math Prefix")
4944 (string :tag "Math Suffix")))
4945 (option (sexp :format "Replace\n" :value t)))))
4947 (defvar TeX-font-replace-function 'TeX-font-replace
4948 "Determines the function which is called when a font should be replaced.")
4950 (defun TeX-describe-font-entry (entry)
4951 "A textual description of an ENTRY in `TeX-font-list'."
4952 (concat (format "%16s " (key-description (char-to-string (nth 0 entry))))
4953 (if (or (eq t (nth 3 entry)) (eq t (nth 5 entry)))
4955 (format "%14s %-3s %14s %-3s"
4956 (nth 1 entry) (nth 2 entry)
4957 (if (stringp (nth 3 entry)) (nth 3 entry) "")
4958 (if (stringp (nth 4 entry)) (nth 4 entry) "")))))
4960 (defun TeX-font (replace what)
4961 "Insert template for font change command.
4962 If REPLACE is not nil, replace current font. WHAT determines the font
4963 to use, as specified by `TeX-font-list'."
4964 (interactive "*P\nc")
4966 (let* ((entry (assoc what TeX-font-list))
4967 (in-math (texmathp))
4968 (before (nth 1 entry))
4969 (after (nth 2 entry)))
4970 (setq replace (or replace (eq t (nth 3 entry)) (eq t (nth 5 entry))))
4971 (if (and in-math (stringp (nth 3 entry)))
4972 (setq before (nth 3 entry)
4973 after (nth 4 entry)))
4977 "KEY TEXTFONT MATHFONT\n\n"
4978 (mapconcat 'TeX-describe-font-entry
4979 TeX-font-list "\n"))))
4980 (with-output-to-temp-buffer "*Help*"
4981 (set-buffer "*Help*")
4984 (funcall TeX-font-replace-function before after))
4987 (cond ((> (mark) (point))
5000 (defun TeX-font-replace (start end)
5001 "Replace font specification around point with START and END.
5002 For modes with font specifications like `{\\font text}'.
5003 See also `TeX-font-replace-macro' and `TeX-font-replace-function'."
5005 (while (not (looking-at "{\\\\[a-zA-Z]+ "))
5009 (replace-match start t t))
5012 (if (looking-at (regexp-quote "\\/}"))
5017 (delete-backward-char 1))
5020 (defun TeX-font-replace-macro (start end)
5021 "Replace font specification around point with START and END.
5022 For modes with font specifications like `\\font{text}'.
5023 See also `TeX-font-replace' and `TeX-font-replace-function'."
5024 (let ((font-list TeX-font-list)
5025 cmds strings regexp)
5027 (setq strings (cdr (car font-list))
5028 font-list (cdr font-list))
5029 (and (stringp (car strings)) (null (string= (car strings) ""))
5030 (setq cmds (cons (car strings) cmds)))
5031 (setq strings (cdr (cdr strings)))
5032 (and (stringp (car strings)) (null (string= (car strings) ""))
5033 (setq cmds (cons (car strings) cmds))))
5034 (setq regexp (mapconcat 'regexp-quote cmds "\\|"))
5038 (if (/= ?\\ (following-char))
5039 (skip-chars-backward "a-zA-Z "))
5040 (skip-chars-backward (regexp-quote TeX-esc))
5041 (if (looking-at regexp)
5044 ;; Use stripped syntax table in order to get stuff like "\emph{(}" right.
5045 (with-syntax-table (TeX-search-syntax-table ?\{ ?\})
5048 (replace-match start t t))
5049 (delete-backward-char 1)
5054 ;; Rewritten from scratch with use of `texmathp' by
5055 ;; Carsten Dominik <dominik@strw.leidenuniv.nl>
5057 (defvar TeX-symbol-marker nil)
5059 (defvar TeX-symbol-marker-pos 0)
5061 ;; The following constants are no longer used, but kept in case some
5062 ;; foreign code uses any of them.
5063 (defvar TeX-dollar-sign ?$
5064 "*Character used to enter and leave math mode in TeX.")
5065 (defconst TeX-dollar-string (char-to-string TeX-dollar-sign))
5066 (defconst TeX-dollar-regexp
5067 (concat "^" (regexp-quote TeX-dollar-string) "\\|[^" TeX-esc "]"
5068 (regexp-quote TeX-dollar-string)))
5070 (defcustom TeX-math-toggle-off-input-method t
5071 "*If non-nil, auto toggle off CJK input methods when entering math mode."
5075 (defcustom TeX-math-close-double-dollar nil
5076 "If non-nil close double dollar math by typing a single `$'."
5080 (defun TeX-insert-dollar (&optional arg)
5081 "Insert dollar sign.
5083 If current math mode was not entered with a dollar, refuse to
5084 insert one. Show matching dollar sign if this dollar sign ends
5085 the TeX math mode and `blink-matching-paren' is non-nil. Ensure
5086 double dollar signs match up correctly by inserting extra dollar
5089 With raw \\[universal-argument] prefix, insert exactly one dollar
5090 sign. With optional ARG, insert that many dollar signs."
5093 ((and arg (listp arg))
5094 ;; C-u always inserts one
5097 ;; Numerical arg inserts that many
5098 (insert (make-string (prefix-numeric-value arg) ?\$)))
5100 ;; This is escaped with `\', so just insert one.
5103 ;; We are inside math mode
5104 (if (and (stringp (car texmathp-why))
5105 (string-equal (substring (car texmathp-why) 0 1) "\$"))
5106 ;; Math mode was turned on with $ or $$ - so finish it accordingly.
5108 (if TeX-math-close-double-dollar
5109 (insert (car texmathp-why))
5111 (when (and blink-matching-paren
5112 (or (string= (car texmathp-why) "$")
5113 (zerop (mod (save-excursion
5114 (skip-chars-backward "$")) 2))))
5116 (goto-char (cdr texmathp-why))
5117 (if (pos-visible-in-window-p)
5119 (message "Matches %s"
5121 (point) (progn (end-of-line) (point))))))))
5122 ;; Math mode was not entered with dollar - we cannot finish it with one.
5123 (message "Math mode started with `%s' cannot be closed with dollar"
5127 ;; Just somewhere in the text.
5129 (TeX-math-input-method-off))
5131 (defvar TeX-math-input-method-off-regexp
5132 "^\\(chinese\\|japanese\\|korean\\bulgarian\\russian\\)"
5133 "Regexp matching input methods to be deactivated when entering math mode.")
5135 (defun TeX-math-input-method-off ()
5136 "Toggle off input method when entering math mode."
5137 (and TeX-math-toggle-off-input-method
5139 (boundp 'current-input-method) current-input-method
5140 (string-match TeX-math-input-method-off-regexp current-input-method)
5141 (inactivate-input-method)))
5145 (defun TeX-normal-mode (arg)
5146 "Remove all information about this buffer, and apply the style hooks again.
5147 Save buffer first including style information.
5148 With optional argument ARG, also reload the style hooks."
5149 ;; FIXME: Shouldn't it be (&optional arg)? -- rs
5152 (setq TeX-style-hook-list nil
5153 BibTeX-global-style-files nil
5154 BibTeX-global-files nil
5155 TeX-global-input-files nil))
5156 (let ((TeX-auto-save t))
5157 (if (buffer-modified-p)
5161 ;; See also addition to `find-file-hooks' in `VirTeX-common-initialization'.
5162 (when (eq TeX-master 'shared) (TeX-master-file nil nil t))
5163 (TeX-update-style t))
5165 (defgroup TeX-quote nil
5166 "Quoting in AUCTeX."
5169 (defcustom TeX-open-quote "``"
5170 "String inserted by typing \\[TeX-insert-quote] to open a quotation."
5174 (defcustom TeX-close-quote "''"
5175 "String inserted by typing \\[TeX-insert-quote] to close a quotation."
5179 (defcustom TeX-quote-after-quote nil
5180 "Behaviour of \\[TeX-insert-quote].
5181 Nil means standard behaviour; when non-nil, opening and closing
5182 quotes are inserted only after \"."
5186 (defcustom TeX-quote-language-alist nil
5187 "Alist for overriding the default language-specific quote insertion.
5188 First element in each item is the name of the language as set by
5189 the language style file as a string. Second element is the
5190 opening quotation mark. Third elemxent is the closing quotation
5191 mark. Opening and closing quotation marks can be specified
5192 directly as strings or as functions returning a string. Fourth
5193 element is a boolean specifying insertion behavior, overriding
5194 `TeX-quote-after-quote'. See Info node `(auctex)European' for
5197 :link '(custom-manual "(auctex)European")
5198 :type '(repeat (group (choice
5204 (const "french") ;; not frenchb or francais
5209 (string :tag "Other Language"))
5210 (choice :tag "Opening quotation mark" string function)
5211 (choice :tag "Closing quotation mark" string function)
5212 (boolean :tag "Insert plain quote first" :value t))))
5214 (defvar TeX-quote-language nil
5215 "If non-nil determines behavior of quote insertion.
5216 It is usually set by language-related style files. Its value has
5217 the same structure as the elements of `TeX-quote-language-alist'.
5218 The symbol 'override can be used as its car in order to override
5219 the settings of style files. Style files should therefore check
5220 if this symbol is present and not alter `TeX-quote-language' if
5222 (make-variable-buffer-local 'TeX-quote-language)
5224 (defun TeX-insert-quote (force)
5225 "Insert the appropriate quotation marks for TeX.
5226 Inserts the value of `TeX-open-quote' (normally ``) or `TeX-close-quote'
5227 \(normally '') depending on the context. If `TeX-quote-after-quote'
5228 is non-nil, this insertion works only after \".
5229 With prefix argument FORCE, always inserts \" characters."
5232 ;; Do not insert TeX quotes in verbatim, math or comment constructs.
5233 (and (fboundp 'font-latex-faces-present-p)
5234 (font-latex-faces-present-p '(font-latex-verbatim-face
5235 font-latex-math-face
5236 font-lock-comment-face))
5237 (font-latex-faces-present-p '(font-latex-verbatim-face
5238 font-latex-math-face
5239 font-lock-comment-face)
5242 (and (TeX-in-comment) (not (eq major-mode 'doctex-mode))))
5243 (self-insert-command (prefix-numeric-value force))
5245 (let* ((lang-override (if (eq (car TeX-quote-language) 'override)
5247 (assoc (car TeX-quote-language)
5248 TeX-quote-language-alist)))
5249 (lang (or lang-override TeX-quote-language))
5250 (open-quote (if lang (nth 1 lang) TeX-open-quote))
5251 (close-quote (if lang (nth 2 lang) TeX-close-quote))
5252 (q-after-q (if lang (nth 3 lang) TeX-quote-after-quote)))
5253 (when (functionp open-quote)
5254 (setq open-quote (funcall open-quote)))
5255 (when (functionp close-quote)
5256 (setq close-quote (funcall close-quote)))
5258 (insert (cond ((bobp)
5261 (TeX-looking-at-backward
5262 (concat (regexp-quote open-quote) "\\|"
5263 (regexp-quote close-quote))
5264 (max (length open-quote) (length close-quote))))
5265 (delete-backward-char (length (match-string 0)))
5267 ((< (save-excursion (skip-chars-backward "\"")) -1)
5269 ((not (= (preceding-char) ?\"))
5274 (delete-backward-char 1)
5277 (forward-char -2) ;;; at -1 there is double quote
5278 (looking-at "[ \t\n]\\|\\s("))
5279 (delete-backward-char 1)
5282 (delete-backward-char 1)
5284 (insert (cond ((bobp)
5286 ((= (preceding-char) (string-to-char TeX-esc))
5288 ((= (preceding-char) ?\")
5291 (forward-char (- (length open-quote)))
5292 (looking-at (regexp-quote open-quote)))
5293 (delete-backward-char (length open-quote))
5296 (forward-char (- (length close-quote)))
5297 (looking-at (regexp-quote close-quote)))
5298 (delete-backward-char (length close-quote))
5302 (looking-at "[ \t\n]\\|\\s("))
5307 (defun TeX-insert-punctuation ()
5308 "Insert point or comma, cleaning up preceding space."
5311 (if (TeX-looking-at-backward "\\\\/\\(}+\\)" 50)
5312 (replace-match "\\1" t))
5313 (call-interactively 'self-insert-command))
5315 (defun TeX-insert-braces (arg)
5316 "Make a pair of braces around next ARG sexps and leave point inside.
5317 No argument is equivalent to zero: just insert braces and leave point
5320 If there is an active region, ARG will be ignored, braces will be
5321 inserted around the region, and point will be left after the
5324 (if (TeX-active-mark)
5326 (if (< (point) (mark))
5327 (exchange-point-and-mark))
5334 (if arg (forward-sexp (prefix-numeric-value arg)))
5335 (insert TeX-grcl))))
5338 (defun TeX-submit-bug-report ()
5339 "Submit a bug report on AUCTeX via mail.
5341 Don't hesitate to report any problems or inaccurate documentation.
5343 If you don't have setup sending mail from (X)Emacs, please copy the
5344 output buffer into your mail program, as it gives us important
5345 information about your AUCTeX version and AUCTeX configuration."
5348 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
5349 (reporter-submit-bug-report
5350 "bug-auctex@gnu.org"
5361 "Remember to cover the basics, that is, what you expected to happen and
5362 what in fact did happen.
5364 Be sure to consult the FAQ section in the manual before submitting
5365 a bug report. In addition check if the bug is reproducable with an
5366 up-to-date version of AUCTeX. So please upgrade to the version
5367 available from http://www.gnu.org/software/auctex/ if your
5368 installation is older than the one available from the web site.
5370 If the bug is triggered by a specific \(La\)TeX file, you should try
5371 to produce a minimal sample file showing the problem and include it
5374 Your bug report will be posted to the AUCTeX bug reporting list.
5375 ------------------------------------------------------------------------")))
5380 (defun TeX-goto-info-page ()
5381 "Read documentation for AUCTeX in the info system."
5385 (autoload 'info-lookup->completions "info-look")
5387 (defvar TeX-doc-backend-alist
5388 '((texdoc (plain-tex-mode latex-mode doctex-mode ams-tex-mode context-mode)
5390 (when (executable-find "texdoc")
5392 ;; Explicitely supply doc directory for
5393 ;; non-kpathsea-based TeX systems.
5394 (unless (stringp TeX-kpathsea-path-delimiter)
5395 (or (TeX-tree-expand
5396 '("$SYSTEXMF" "$TEXMFLOCAL" "$TEXMFMAIN" "$TEXMFDIST")
5398 `(,@TeX-macro-global ,@TeX-macro-private)))
5399 '("dvi" "pdf" "ps" "txt" "html") t t)))
5401 ;; texdoc in MiKTeX requires --view in order to start
5402 ;; the viewer instead of an intermediate web page.
5403 (call-process "texdoc" nil 0 nil "--view" doc)))
5404 (latex-info (latex-mode)
5406 (when (condition-case nil
5407 (save-window-excursion
5408 (let ((buf (generate-new-buffer-name "*info*")))
5415 (if (string-match "\\`\\\\" x)
5416 (substring x 1) x)))
5417 (info-lookup->completions 'symbol 'latex-mode))))
5419 (info-lookup-symbol (concat "\\" doc) 'latex-mode)))
5420 (texinfo-info (texinfo-mode)
5422 (when (condition-case nil
5423 (save-window-excursion
5424 (let ((buf (generate-new-buffer-name "*info*")))
5425 (info "texinfo" buf)
5431 (if (string-match "\\`@" x)
5432 (substring x 1) x)))
5433 (info-lookup->completions 'symbol
5436 (info-lookup-symbol (concat "@" doc) 'texinfo-mode))))
5437 "Alist of backends used for looking up documentation.
5438 Each item consists of four elements.
5440 The first is a symbol describing the backend's name.
5442 The second is a list of modes the backend should be activated in.
5444 The third is a function returning a list of documents available
5445 to the backend. It should return nil if the backend is not
5446 available, e.g. if a required executable is not present on the
5449 The fourth is a function for displaying the documentation. The
5450 function should accept a single argument, the chosen package,
5451 command, or document name.")
5453 (defun TeX-doc (&optional name)
5454 "Display documentation for string NAME.
5455 NAME may be a package, a command, or a document."
5458 ;; Build the lists of available documentation used for completion.
5459 (dolist (elt TeX-doc-backend-alist)
5460 (when (memq major-mode (nth 1 elt))
5461 (let ((completions (funcall (nth 2 elt))))
5462 (unless (null completions)
5463 (add-to-list 'docs (cons completions (nth 0 elt)))))))
5466 (if (executable-find "texdoc")
5467 ;; Fallback if we did not find anything via the backend list.
5468 (let ((doc (read-from-minibuffer "Input for `texdoc': ")))
5469 (when doc (call-process "texdoc" nil 0 nil "--view" doc)))
5471 (message "No documentation found")))
5472 ;; Ask the user about the package, command, or document.
5473 (when (and (interactive-p)
5474 (or (not name) (string= name "")))
5475 (let ((symbol (thing-at-point 'symbol))
5476 contained completions doc)
5477 ;; Is the symbol at point contained in the lists of available
5479 (setq contained (catch 'found
5481 (when (member symbol (car elt))
5482 (throw 'found t)))))
5483 ;; Setup completion list in a format suitable for `completing-read'.
5485 (setq completions (nconc (mapcar 'list (car elt)) completions)))
5487 (setq doc (completing-read
5489 (format "Package, command, or document (default %s): "
5491 "Package, command, or document: ")
5493 (setq name (if (string= doc "") symbol doc))))
5495 (message "No documentation specified")
5496 ;; XXX: Provide way to choose in case a symbol can be found in
5497 ;; more than one backend.
5498 (let* ((backend (catch 'found
5500 (when (member name (car elt))
5501 (throw 'found (cdr elt)))))))
5503 (funcall (nth 3 (assoc backend TeX-doc-backend-alist)) name)
5504 (message "Documentation not found")))))))
5509 ;; FIXME: Document those functions and variables. -- rs
5511 ;; The FSF ispell.el use this.
5512 (defun ispell-tex-buffer-p ()
5513 (and (boundp 'ispell-tex-p) ispell-tex-p))
5515 ;; The FSF ispell.el might one day use this.
5516 (setq ispell-enable-tex-parser t)
5518 (defun TeX-run-ispell (command string file)
5519 "Run ispell on current TeX buffer."
5520 (cond ((and (string-equal file (TeX-region-file))
5521 (fboundp 'ispell-region))
5522 (call-interactively 'ispell-region))
5523 ((string-equal file (TeX-region-file))
5524 (call-interactively 'spell-region))
5525 ((fboundp 'ispell-buffer)
5532 (defun TeX-ispell-document (name)
5533 "Run ispell on all open files belonging to the current document."
5534 (interactive (list (TeX-master-file)))
5535 (if (string-equal name "")
5536 (setq name (TeX-master-file)))
5539 (regexp (concat "\\`\\("
5540 (mapconcat (lambda (dir)
5543 (file-name-as-directory dir))))
5544 (append (when (file-name-directory name)
5545 (list (file-name-directory name)))
5549 (mapconcat 'regexp-quote
5550 (cons (file-name-nondirectory name)
5551 (TeX-style-list)) "\\|")
5553 (mapconcat 'regexp-quote TeX-file-extensions "\\|")
5555 (buffers (buffer-list)))
5557 (let* ((buffer (car buffers))
5558 (name (buffer-file-name buffer)))
5559 (setq buffers (cdr buffers))
5560 (if (and name (string-match regexp name))
5562 (save-excursion (switch-to-buffer buffer) (ispell-buffer))
5563 (setq found t)))))))
5565 ;; Some versions of ispell 3 use this.
5566 (defvar ispell-tex-major-modes nil)
5567 (setq ispell-tex-major-modes
5568 (append '(plain-tex-mode ams-tex-mode latex-mode doctex-mode)
5569 ispell-tex-major-modes))
5572 ;;; Special provisions for other modes and libraries
5574 ;; desktop-locals-to-save is broken by design. Don't have
5575 ;; buffer-local values of it.
5576 (eval-after-load "desktop"
5578 (dolist (elt '(TeX-master))
5579 (unless (member elt (default-value 'desktop-locals-to-save))
5580 (setq-default desktop-locals-to-save
5581 (cons elt (default-value 'desktop-locals-to-save)))))
5582 (add-hook 'desktop-after-read-hook '(lambda ()
5583 (TeX-set-mode-name t)))))
5585 ;; delsel.el, `delete-selection-mode'
5586 (put 'TeX-newline 'delete-selection t)
5587 (put 'TeX-insert-dollar 'delete-selection t)
5588 (put 'TeX-insert-quote 'delete-selection t)
5589 (put 'TeX-insert-backslash 'delete-selection t)
5594 ;; coding: iso-8859-1
5597 ;;; tex.el ends here