]> code.delx.au - gnu-emacs-elpa/blob - packages/auctex/tex.el
Get "make -k" to go through
[gnu-emacs-elpa] / packages / auctex / tex.el
1 ;;; tex.el --- Support for TeX documents.
2
3 ;; Copyright (C) 1985-1987, 1991, 1993, 1994, 1996, 1997, 1999-2013
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: auctex-devel@gnu.org
7 ;; Keywords: tex
8
9 ;; This file is part of AUCTeX.
10
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)
14 ;; any later version.
15
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.
20
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
24 ;; 02110-1301, USA.
25
26 ;;; Commentary:
27
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
30 ;; ConTeXt).
31
32 ;;; Code:
33
34 (when (< emacs-major-version 21)
35 (error "AUCTeX requires Emacs 21 or later"))
36
37 (require 'custom)
38 (require 'tex-site)
39 (eval-when-compile
40 (require 'cl))
41
42 (defun TeX--call-3/2 (f arg1 arg2 arg3)
43 (condition-case nil
44 (funcall f arg1 arg2 arg3)
45 (wrong-number-of-arguments (funcall f arg1 arg2))))
46
47 (defgroup TeX-file nil
48 "Files used by AUCTeX."
49 :group 'AUCTeX)
50
51 (defgroup TeX-command nil
52 "Calling external commands from AUCTeX."
53 :group 'AUCTeX)
54
55 (defgroup LaTeX nil
56 "LaTeX support in AUCTeX."
57 :tag "LaTeX"
58 :group 'AUCTeX
59 :prefix "LaTeX-")
60
61 (defgroup TeX-misc nil
62 "Various AUCTeX settings."
63 :group 'AUCTeX)
64
65 ;;; Site Customization
66 ;;
67 ;; The following variables are likely to need to be changed for your
68 ;; site. You should do this with customize. Here is the beef: If you
69 ;; want to print, TeX-print-command must be non-nil (if it is nil,
70 ;; you'll get a complaint when using the print menu). If you want to
71 ;; view the queue, TeX-queue-command needs to be non-nil (if it is
72 ;; nil, it won't get mentioned in the menu). If TeX-printer-list is
73 ;; nil, nothing else gets asked: the menu entries lead directly to the
74 ;; respective commands. If those commands contain %p, the value of
75 ;; TeX-printer-default gets inserted there, no questions asked. Now
76 ;; if TeX-printer-list is non-nil, you'll always get asked which
77 ;; printer you want to use. You can enter a configured printer from
78 ;; TeX-printer-list, or an unknown one. The respective menus will
79 ;; show all configured printers. Since you can enter unknown
80 ;; printers, the printer name _must_ be set with %p in
81 ;; TeX-print-command.
82
83 ;; How to print.
84
85 (defcustom TeX-print-command "%(o?)dvips -P%p %r %s"
86 "*Command used to print a file.
87
88 First `%p' is expanded to the printer name, then ordinary expansion is
89 performed as specified in `TeX-expand-list'. If it is nil,
90 then customization is requested."
91 :group 'TeX-command
92 :type '(choice (string :tag "Print command")
93 (const :tag "No print command customized" nil)))
94
95 (defcustom TeX-command "tex"
96 "Command to run plain TeX."
97 :group 'TeX-command
98 :type 'string)
99
100 (defcustom TeX-Omega-command "omega"
101 "Command to run plain TeX on Omega."
102 :group 'TeX-command
103 :type 'string)
104
105 (defcustom LaTeX-command "latex"
106 "Command to run LaTeX."
107 :group 'TeX-command
108 :type 'string)
109
110 (defcustom LaTeX-Omega-command "lambda"
111 "Command to run LaTeX on Omega."
112 :group 'TeX-command
113 :type 'string)
114
115 (defcustom ConTeXt-engine nil
116 "Engine to use for --engine in the texexec command.
117 If nil, none is specified."
118 :group 'TeX-command
119 :type '(choice (const :tag "Unspecified" nil)
120 string))
121
122 (defcustom ConTeXt-Omega-engine TeX-Omega-command
123 "Engine to use for --engine in the texexec command in Omega mode.
124 If nil, none is specified."
125 :group 'TeX-command
126 :type '(choice (const :tag "Unspecified" nil)
127 string))
128 ;; At least in TeXLive 2009 ConTeXt does not support an omega option anymore.
129 (TeX--call-3/2 #'make-obsolete-variable 'ConTeXt-Omega-engine
130 'TeX-engine-alist "before 11.86")
131
132 (defcustom TeX-queue-command "lpq -P%p"
133 "*Command used to show the status of a printer queue.
134
135 First `%p' is expanded to the printer name, then ordinary expansion is
136 performed as specified in `TeX-expand-list'. If this is nil,
137 the printer has no corresponding command."
138 :group 'TeX-command
139 :type '(choice (string :tag "Queue check command")
140 (const :tag "No such command" nil)))
141
142 (defcustom TeX-mode-hook nil
143 "A hook run in TeX mode buffers."
144 :type 'hook
145 :group 'TeX-misc)
146
147 (defcustom AmS-TeX-mode-hook nil
148 "A hook run in AmS-TeX mode buffers."
149 :type 'hook
150 :group 'TeX-misc)
151
152 ;; This is the major configuration variable. Most sites will only
153 ;; need to change the second string in each entry, which is the name
154 ;; of a command to send to the shell. If you use other formatters
155 ;; like AMSLaTeX or AMSTeX, you can add those to the list. See
156 ;; TeX-expand-list for a description of the % escapes
157
158 (defcustom TeX-command-list
159 `(("TeX" "%(PDF)%(tex) %`%S%(PDFout)%(mode)%' %t"
160 TeX-run-TeX nil
161 (plain-tex-mode ams-tex-mode texinfo-mode) :help "Run plain TeX")
162 ("LaTeX" "%`%l%(mode)%' %t"
163 TeX-run-TeX nil
164 (latex-mode doctex-mode) :help "Run LaTeX")
165 ;; Not part of standard TeX.
166 ("Makeinfo" "makeinfo %t" TeX-run-compile nil
167 (texinfo-mode) :help "Run Makeinfo with Info output")
168 ("Makeinfo HTML" "makeinfo --html %t" TeX-run-compile nil
169 (texinfo-mode) :help "Run Makeinfo with HTML output")
170 ("AmSTeX" "%(PDF)amstex %`%S%(PDFout)%(mode)%' %t"
171 TeX-run-TeX nil (ams-tex-mode) :help "Run AMSTeX")
172 ;; support for ConTeXt --pg
173 ;; first version of ConTeXt to support nonstopmode: 2003.2.10
174 ("ConTeXt" "texexec --once --texutil %(execopts)%t"
175 TeX-run-TeX nil (context-mode) :help "Run ConTeXt once")
176 ("ConTeXt Full" "texexec %(execopts)%t"
177 TeX-run-TeX nil
178 (context-mode) :help "Run ConTeXt until completion")
179 ("BibTeX" "bibtex %s" TeX-run-BibTeX nil t :help "Run BibTeX")
180 ,(if (or window-system (getenv "DISPLAY"))
181 '("View" "%V" TeX-run-discard-or-function t t :help "Run Viewer")
182 '("View" "dvi2tty -q -w 132 %s" TeX-run-command t t
183 :help "Run Text viewer"))
184 ("Print" "%p" TeX-run-command t t :help "Print the file")
185 ("Queue" "%q" TeX-run-background nil t :help "View the printer queue"
186 :visible TeX-queue-command)
187 ("File" "%(o?)dvips %d -o %f " TeX-run-command t t
188 :help "Generate PostScript file")
189 ("Index" "makeindex %s" TeX-run-command nil t :help "Create index file")
190 ("Check" "lacheck %s" TeX-run-compile nil (latex-mode)
191 :help "Check LaTeX file for correctness")
192 ("Spell" "(TeX-ispell-document \"\")" TeX-run-function nil t
193 :help "Spell-check the document")
194 ("Clean" "TeX-clean" TeX-run-function nil t
195 :help "Delete generated intermediate files")
196 ("Clean All" "(TeX-clean t)" TeX-run-function nil t
197 :help "Delete generated intermediate and output files")
198 ("Other" "" TeX-run-command t t :help "Run an arbitrary command"))
199 "List of commands to execute on the current document.
200
201 Each element is a list, whose first element is the name of the command
202 as it will be presented to the user.
203
204 The second element is the string handed to the shell after being
205 expanded. The expansion is done using the information found in
206 `TeX-expand-list'.
207
208 The third element is the function which actually start the process.
209 Several such hooks has been defined:
210
211 TeX-run-command: Start up the process and show the output in a
212 separate buffer. Check that there is not two commands running for the
213 same file. Return the process object.
214
215 TeX-run-format: As `TeX-run-command', but assume the output is created
216 by a TeX macro package. Return the process object.
217
218 TeX-run-TeX: For TeX output.
219
220 TeX-run-interactive: Run TeX or LaTeX interactively.
221
222 TeX-run-BibTeX: For BibTeX output.
223
224 TeX-run-compile: Use `compile' to run the process.
225
226 TeX-run-shell: Use `shell-command' to run the process.
227
228 TeX-run-discard: Start the process in the background, discarding its
229 output.
230
231 TeX-run-background: Start the process in the background, show output
232 in other window.
233
234 TeX-run-silent: Start the process in the background.
235
236 TeX-run-discard-foreground: Start the process in the foreground,
237 discarding its output.
238
239 TeX-run-function: Execute the Lisp function or function call
240 specified by the string in the second element. Consequently,
241 this hook does not start a process.
242
243 TeX-run-discard-or-function: If the command is a Lisp function,
244 execute it as such, otherwise start the command as a process,
245 discarding its output.
246
247 To create your own hook, define a function taking three arguments: The
248 name of the command, the command string, and the name of the file to
249 process. It might be useful to use `TeX-run-command' in order to
250 create an asynchronous process.
251
252 If the fourth element is non-nil, the user will get a chance to
253 modify the expanded string.
254
255 The fifth element indicates in which mode(s) the command should be
256 present in the Command menu. Use t if it should be active in any
257 mode. If it should only be present in some modes, specify a list with
258 the respective mode names.
259
260 Any additional elements get just transferred to the respective menu entries."
261 :group 'TeX-command
262 :type '(repeat (group :value ("" "" TeX-run-command nil t)
263 (string :tag "Name")
264 (string :tag "Command")
265 (choice :tag "How"
266 :value TeX-run-command
267 (function-item TeX-run-command)
268 (function-item TeX-run-format)
269 (function-item TeX-run-TeX)
270 (function-item TeX-run-interactive)
271 (function-item TeX-run-BibTeX)
272 (function-item TeX-run-compile)
273 (function-item TeX-run-shell)
274 (function-item TeX-run-discard)
275 (function-item TeX-run-background)
276 (function-item TeX-run-silent)
277 (function-item TeX-run-discard-foreground)
278 (function-item TeX-run-function)
279 (function-item TeX-run-discard-or-function)
280 (function :tag "Other"))
281 (boolean :tag "Prompt")
282 (choice :tag "Modes"
283 (const :tag "All" t)
284 (set (const :tag "Plain TeX" plain-tex-mode)
285 (const :tag "LaTeX" latex-mode)
286 (const :tag "DocTeX" doctex-mode)
287 (const :tag "ConTeXt" context-mode)
288 (const :tag "Texinfo" texinfo-mode)
289 (const :tag "AmSTeX" ams-tex-mode)))
290 (repeat :tag "Menu elements" :inline t sexp))))
291
292 (defcustom TeX-command-output-list
293 '(
294 ; Add the following line if you want to use htlatex (tex4ht)
295 ; ("\\`htlatex" ("html"))
296 )
297 "List of regexps and file extensions.
298
299 Each element is a list, whose first element is a regular expression to
300 match against the name of the command that will be used to process the TeX
301 file.
302
303 The second element is either a string or a list with a string as element.
304 If it is a string this is the default file extension that will be expected
305 for output files that are produced by commands that match the first
306 element. The real file extension will be obtained from the logging output
307 if possible, defaulting to the given string.
308 If it is a list, the element of the list will be the fixed extension used
309 without looking at the logging output.
310
311 If this list does not yield an extension, the default is either \"dvi\"
312 or \"pdf\", depending on the setting of `TeX-PDF-mode'.
313 Extensions must be given without the \".\"."
314
315 :group 'TeX-command
316 :type '(repeat (group (regexp :tag "Command Regexp")
317 (choice (string :tag "Default Extension")
318 (group (string :tag "Fixed Extension"))))))
319
320 ;; You may want to change the default LaTeX version for your site.
321 (defcustom LaTeX-version "2e"
322 "Default LaTeX version. Currently recognized is \"2\" and \"2e\"."
323 :group 'LaTeX
324 :type '(radio (const :format "%v\n%h"
325 :doc "\
326 The executable `latex' is LaTeX version 2."
327 "2")
328 (const :format "%v\n%h"
329 :doc "\
330 The executable `latex' is LaTeX version 2e."
331 "2e")
332 (string :tag "Other")))
333
334
335 ;; Use different compilation commands depending on style.
336 ;; Only works if parsing is enabled.
337
338 (defcustom LaTeX-command-style
339 ;; They have all been combined in LaTeX 2e.
340 '(("" "%(PDF)%(latex) %S%(PDFout)"))
341 "List of style options and LaTeX commands.
342
343 If the first element (a regular expression) matches the name of one of
344 the style files, any occurrence of the string `%l' in a command in
345 `TeX-command-list' will be replaced with the second element. The first
346 match is used, if no match is found the `%l' is replaced with the empty
347 string."
348 :group 'TeX-command
349 :type '(repeat (group :value ("" "")
350 regexp (string :tag "Style"))))
351
352 ;; Enter the names of the printers available at your site, or nil if
353 ;; you only have one printer.
354
355 (defcustom TeX-printer-list
356 '(("Default" "%(o?)dvips -f %s | lpr" "lpq"))
357 "List of available printers.
358
359 The first element of each entry is the printer name.
360
361 The second element is the command used to print to this
362 printer. It defaults to the value of `TeX-print-command' when nil.
363
364 The third element is the command used to examine the print queue for
365 this printer. It defaults to the value of `TeX-queue-command' similarly.
366
367 Any occurrence of `%p' in the second or third element is expanded to
368 the printer name given in the first element, then ordinary expansion
369 is performed as specified in `TeX-expand-list'.
370
371 If this list is empty, only `TeX-print-command' and `TeX-queue-command'
372 get consulted."
373 :group 'TeX-command
374 :type '(repeat (group (string :tag "Name")
375 (option (group :inline t
376 :extra-offset -4
377 (choice :tag "Print"
378 (const :tag "default")
379 (string :format "%v"))
380 (option (choice :tag "Queue"
381 (const :tag "default")
382 (string
383 :format "%v"))))))))
384
385 ;; The name of the most used printer.
386
387 (defcustom TeX-printer-default (or (getenv "PRINTER")
388 (and TeX-printer-list
389 (car (car TeX-printer-list)))
390 "lp")
391 "*Default printer to use with `TeX-command'."
392 :group 'TeX-command
393 :type 'string)
394
395 (defcustom TeX-print-style '(("^landscape$" "-t landscape"))
396 "List of style options and print options.
397
398 If the first element (a regular expression) matches the name of one of
399 the style files, any occurrence of the string `%r' in a command in
400 `TeX-command-list' will be replaced with the second element. The first
401 match is used, if no match is found the `%r' is replaced with the empty
402 string."
403 :group 'TeX-command
404 :type '(repeat (group regexp (string :tag "Command"))))
405
406 ;; This is the list of expansion for the commands in
407 ;; TeX-command-list. Not likely to be changed, but you may e.g. want
408 ;; to handle .ps files.
409
410 (defcustom TeX-expand-list
411 '(("%p" TeX-printer-query) ;%p must be the first entry
412 ("%q" (lambda ()
413 (TeX-printer-query t)))
414 ("%V" (lambda ()
415 (TeX-source-correlate-start-server-maybe)
416 (TeX-view-command-raw)))
417 ("%vv" (lambda ()
418 (TeX-source-correlate-start-server-maybe)
419 (TeX-output-style-check TeX-output-view-style)))
420 ("%v" (lambda ()
421 (TeX-source-correlate-start-server-maybe)
422 (TeX-style-check TeX-view-style)))
423 ("%r" (lambda ()
424 (TeX-style-check TeX-print-style)))
425 ("%l" (lambda ()
426 (TeX-style-check LaTeX-command-style)))
427 ("%(PDF)" (lambda ()
428 (if (and (eq TeX-engine 'default)
429 (or TeX-PDF-mode
430 TeX-DVI-via-PDFTeX))
431 "pdf"
432 "")))
433 ("%(PDFout)" (lambda ()
434 (cond ((and (eq TeX-engine 'xetex)
435 (not TeX-PDF-mode))
436 " -no-pdf")
437 ((and (eq TeX-engine 'luatex)
438 (not TeX-PDF-mode))
439 " --output-format=dvi")
440 ((and (eq TeX-engine 'default)
441 (not TeX-PDF-mode)
442 TeX-DVI-via-PDFTeX)
443 " \"\\pdfoutput=0 \"")
444 (t ""))))
445 ("%(mode)" (lambda ()
446 (if TeX-interactive-mode
447 ""
448 " -interaction=nonstopmode")))
449 ("%(o?)" (lambda () (if (eq TeX-engine 'omega) "o" "")))
450 ("%(tex)" (lambda () (eval (nth 2 (assq TeX-engine (TeX-engine-alist))))))
451 ("%(latex)" (lambda () (eval (nth 3 (assq TeX-engine (TeX-engine-alist))))))
452 ("%(execopts)" ConTeXt-expand-options)
453 ("%S" TeX-source-correlate-expand-options)
454 ("%dS" TeX-source-specials-view-expand-options)
455 ("%cS" TeX-source-specials-view-expand-client)
456 ("%(outpage)" (lambda ()
457 (if TeX-source-correlate-output-page-function
458 (funcall TeX-source-correlate-output-page-function)
459 "1")))
460 ;; `file' means to call `TeX-master-file' or `TeX-region-file'
461 ("%s" file nil t)
462 ("%t" file t t)
463 ("%`" (lambda nil
464 (setq TeX-command-pos t TeX-command-text "")))
465 (" \"\\" (lambda nil
466 (if (eq TeX-command-pos t)
467 (setq TeX-command-pos pos
468 pos (+ 3 pos))
469 (setq pos (1+ pos)))))
470 ("\"" (lambda nil (if (numberp TeX-command-pos)
471 (setq TeX-command-text
472 (concat
473 TeX-command-text
474 (substring command
475 TeX-command-pos
476 (1+ pos)))
477 command
478 (concat
479 (substring command
480 0
481 TeX-command-pos)
482 (substring command
483 (1+ pos)))
484 pos TeX-command-pos
485 TeX-command-pos t)
486 (setq pos (1+ pos)))))
487 ("%'" (lambda nil
488 (prog1
489 (if (stringp TeX-command-text)
490 (progn
491 (setq pos (+ (length TeX-command-text) 9)
492 TeX-command-pos
493 (and (string-match " "
494 (funcall file t t))
495 "\""))
496 (concat TeX-command-text " \"\\input\""))
497 (setq TeX-command-pos nil)
498 "")
499 (setq TeX-command-text nil))))
500 ("%n" TeX-current-line)
501 ("%d" file "dvi" t)
502 ("%f" file "ps" t)
503 ("%o" (lambda nil (funcall file (TeX-output-extension) t)))
504 ;; for source specials the file name generated for the xdvi
505 ;; command needs to be relative to the master file, just in
506 ;; case the file is in a different subdirectory
507 ("%b" TeX-current-file-name-master-relative)
508 ;; the following is for preview-latex.
509 ("%m" preview-create-subdirectory))
510 "List of expansion strings for TeX command names.
511
512 Each entry is a list with two or more elements. The first element is
513 the string to be expanded. The second element is the name of a
514 function returning the expanded string when called with the remaining
515 elements as arguments. The special value `file' will be expanded to
516 the name of the file being processed, with an optional extension."
517 :group 'TeX-command
518 :type '(repeat (group (string :tag "Key")
519 (sexp :tag "Expander")
520 (repeat :inline t
521 :tag "Arguments"
522 (sexp :format "%v")))))
523
524
525 ;; The following dependencies are not done with autoload cookies since
526 ;; they are only useful when tex.el is loaded, anyway. tex-buf.el
527 ;; should remain unloaded as long as one is only editing files, so
528 ;; requiring it here would be wrong.
529
530 (autoload 'TeX-region-create "tex-buf" nil nil)
531 (autoload 'TeX-save-document "tex-buf" nil t)
532 (autoload 'TeX-home-buffer "tex-buf" nil t)
533 (autoload 'TeX-pin-region "tex-buf" nil t)
534 (autoload 'TeX-command-region "tex-buf" nil t)
535 (autoload 'TeX-command-buffer "tex-buf" nil t)
536 (autoload 'TeX-command-master "tex-buf" nil t)
537 (autoload 'TeX-command "tex-buf" nil nil)
538 (autoload 'TeX-kill-job "tex-buf" nil t)
539 (autoload 'TeX-recenter-output-buffer "tex-buf" nil t)
540 (autoload 'TeX-next-error "tex-buf" nil t)
541 (autoload 'TeX-region-file "tex-buf" nil nil)
542 (autoload 'TeX-current-offset "tex-buf" nil nil)
543 (autoload 'TeX-process-set-variable "tex-buf" nil nil)
544 (autoload 'TeX-view "tex-buf" nil t)
545
546 ;;; Portability.
547
548 (require 'easymenu)
549
550 (eval-and-compile
551 (if (featurep 'xemacs)
552 (defun TeX-maybe-remove-help (menu)
553 "Removes :help entries from menus, since XEmacs does not like them.
554 Also does other stuff."
555 (cond ((consp menu)
556 (cond ((eq (car menu) :help)
557 (TeX-maybe-remove-help (cddr menu)))
558 ((eq (car menu) :visible)
559 (cons :included
560 (cons (cadr menu)
561 (TeX-maybe-remove-help (cddr menu)))))
562 (t (cons (TeX-maybe-remove-help (car menu))
563 (TeX-maybe-remove-help (cdr menu))))))
564 ((vectorp menu)
565 (vconcat (TeX-maybe-remove-help (append menu nil))))
566 (t menu)))
567 (defun TeX-maybe-remove-help (menu)
568 "Compatibility function that would remove :help entries if on XEmacs,
569 but does nothing in Emacs."
570 menu))
571 (defmacro TeX-menu-with-help (menu)
572 "Compatibility macro that removes :help entries if on XEmacs.
573 Also does other stuff."
574 (TeX-maybe-remove-help menu)))
575
576
577 ;;; Documentation for Info-goto-emacs-command-node and similar
578
579 (eval-after-load 'info '(dolist (elt '("TeX" "LaTeX" "ConTeXt" "Texinfo"
580 "docTeX"))
581 (add-to-list 'Info-file-list-for-emacs
582 (cons elt "AUCTeX"))))
583
584 (defadvice hack-one-local-variable (after TeX-hack-one-local-variable-after
585 activate)
586 "Call minor mode function if minor mode variable is found."
587 (let ((var (ad-get-arg 0))
588 (val (ad-get-arg 1)))
589 ;; Instead of checking for each mode explicitely `minor-mode-list'
590 ;; could be used. But this may make the byte compiler pop up.
591 (when (memq var '(TeX-PDF-mode
592 TeX-source-correlate-mode TeX-interactive-mode
593 TeX-fold-mode LaTeX-math-mode))
594 (if (symbol-value val) (funcall var 1) (funcall var 0)))))
595
596 (defvar TeX-overlay-priority-step 16
597 "Numerical difference of priorities between nested overlays.
598 The step should be big enough to allow setting a priority for new
599 overlays between two existing ones.")
600
601
602 ;;; Special support for XEmacs
603
604 (when (featurep 'xemacs)
605
606 (defun TeX-read-string
607 (prompt &optional initial-input history default-value)
608 (condition-case nil
609 (read-string prompt initial-input history default-value t)
610 (wrong-number-of-arguments
611 (read-string prompt initial-input history default-value))))
612
613 (defun TeX-mark-active ()
614 ;; In Lucid (mark) returns nil when not active.
615 (if zmacs-regions
616 (mark)
617 (mark t)))
618
619 (defun TeX-active-mark ()
620 (and zmacs-regions (mark)))
621
622 (fset 'TeX-activate-region (symbol-function 'zmacs-activate-region))
623
624 ;; I am aware that this counteracts coding conventions but I am sick
625 ;; of it.
626 (unless (fboundp 'line-beginning-position)
627 (defalias 'line-beginning-position 'point-at-bol))
628 (unless (fboundp 'line-end-position)
629 (defalias 'line-end-position 'point-at-eol))
630
631 (defun TeX-overlay-prioritize (start end)
632 "Calculate a priority for an overlay extending from START to END.
633 The calculated priority is lower than the minimum of priorities
634 of surrounding overlays and higher than the maximum of enclosed
635 overlays."
636 (let (inner-priority outer-priority
637 (prios (cons nil nil)))
638 (map-extents
639 #'(lambda (ov prios)
640 (and
641 (or (eq (extent-property ov 'category) 'TeX-fold)
642 (extent-property ov 'preview-state))
643 (setcar prios
644 (max (or (car prios) 0)
645 (extent-property ov 'priority))))
646 nil)
647 nil start end prios 'start-and-end-in-region 'priority)
648 (map-extents
649 #'(lambda (ov prios)
650 (and
651 (or (eq (extent-property ov 'category) 'TeX-fold)
652 (extent-property ov 'preview-state))
653 (setcdr prios
654 (min (or (cdr prios) most-positive-fixnum)
655 (extent-property ov 'priority))))
656 nil)
657 nil start end prios
658 '(start-and-end-in-region negate-in-region) 'priority)
659 (setq inner-priority (car prios) outer-priority (cdr prios))
660 (cond ((and inner-priority (not outer-priority))
661 (+ inner-priority TeX-overlay-priority-step))
662 ((and (not inner-priority) outer-priority)
663 (/ outer-priority 2))
664 ((and inner-priority outer-priority)
665 (+ (/ (- outer-priority inner-priority) 2) inner-priority))
666 (t TeX-overlay-priority-step)))) )
667
668
669 (if (fboundp 'completing-read-multiple)
670 (defalias 'TeX-completing-read-multiple 'completing-read-multiple)
671 (defun TeX-completing-read-multiple
672 (prompt table &optional predicate require-match initial-input
673 hist def inherit-input-method)
674 "Poor mans implementation of Emacs' `completing-read-multiple' for XEmacs.
675 The XEmacs package edit-utils-2.32 includes `crm.el'."
676 (multi-prompt "," nil prompt table predicate require-match initial-input
677 hist)))
678
679 (if (fboundp 'line-number-at-pos)
680 (defalias 'TeX-line-number-at-pos 'line-number-at-pos)
681 ;; `line-number-at-pos' from `simple.el' in Emacs CVS (2006-06-07)
682 (defun TeX-line-number-at-pos (&optional pos)
683 "Return (narrowed) buffer line number at position POS.
684 If POS is nil, use current buffer location."
685 (let ((opoint (or pos (point))) start)
686 (save-excursion
687 (goto-char (point-min))
688 (setq start (point))
689 (goto-char opoint)
690 (forward-line 0)
691 (1+ (count-lines start (point)))))))
692
693 ;;; Special support for GNU Emacs
694
695 (unless (featurep 'xemacs)
696
697 (defun TeX-read-string (prompt &optional initial-input history default-value)
698 (read-string prompt initial-input history default-value t))
699
700 (defun TeX-mark-active ()
701 ;; In FSF 19 mark-active indicates if mark is active.
702 mark-active)
703
704 (defun TeX-active-mark ()
705 (and transient-mark-mode mark-active))
706
707 (defun TeX-activate-region ()
708 nil)
709
710 (defun TeX-overlay-prioritize (start end)
711 "Calculate a priority for an overlay extending from START to END.
712 The calculated priority is lower than the minimum of priorities
713 of surrounding overlays and higher than the maximum of enclosed
714 overlays."
715 (let (outer-priority inner-priority ov-priority)
716 (dolist (ov (overlays-in start end))
717 (when (or (eq (overlay-get ov 'category) 'TeX-fold)
718 (overlay-get ov 'preview-state))
719 (setq ov-priority (overlay-get ov 'priority))
720 (if (>= (overlay-start ov) start)
721 (setq inner-priority (max ov-priority (or inner-priority
722 ov-priority)))
723 (setq outer-priority (min ov-priority (or outer-priority
724 ov-priority))))))
725 (cond ((and inner-priority (not outer-priority))
726 (+ inner-priority TeX-overlay-priority-step))
727 ((and (not inner-priority) outer-priority)
728 (/ outer-priority 2))
729 ((and inner-priority outer-priority)
730 (+ (/ (- outer-priority inner-priority) 2) inner-priority))
731 (t TeX-overlay-priority-step)))) )
732
733 (defun TeX-delete-dups-by-car (alist &optional keep-list)
734 "Return a list of all elements in ALIST, but each car only once.
735 Elements of KEEP-LIST are not removed even if duplicate."
736 ;; Copy of `reftex-uniquify-by-car' (written by David Kastrup).
737 (setq keep-list (sort (copy-sequence keep-list) #'string<))
738 (setq alist (sort (copy-sequence alist)
739 (lambda (a b)
740 (string< (car a) (car b)))))
741 (let ((new alist) elt)
742 (while new
743 (setq elt (caar new))
744 (while (and keep-list (string< (car keep-list) elt))
745 (setq keep-list (cdr keep-list)))
746 (unless (and keep-list (string= elt (car keep-list)))
747 (while (string= elt (car (cadr new)))
748 (setcdr new (cddr new))))
749 (setq new (cdr new))))
750 alist)
751
752 (defun TeX-delete-duplicate-strings (list)
753 "Return a list of all strings in LIST, but each only once."
754 (setq list (TeX-sort-strings list))
755 (let ((new list) elt)
756 (while new
757 (setq elt (car new))
758 (while (string= elt (cadr new))
759 (setcdr new (cddr new)))
760 (setq new (cdr new))))
761 list)
762
763 (defun TeX-sort-strings (list)
764 "Return sorted list of all strings in LIST."
765 (sort (copy-sequence list) #'string<))
766
767 ;;; Buffer
768
769 (defgroup TeX-output nil
770 "Parsing TeX output."
771 :prefix "TeX-"
772 :group 'AUCTeX)
773
774 (defcustom TeX-display-help t
775 "Control type of help display when stepping through errors with \\[TeX-next-error].
776 If t display help buffer. If nil display message about error in
777 echo area. If `expert' display output buffer with raw processor output."
778 :group 'TeX-output
779 :type '(choice (const :tag "Help buffer" t)
780 (const :tag "Echo area" nil)
781 (const :tag "Output buffer" expert)))
782
783 (defcustom TeX-debug-bad-boxes nil
784 "Non-nil means also find overfull/underfull box warnings with \\[TeX-next-error]."
785 :group 'TeX-output
786 :type 'boolean)
787
788 (defcustom TeX-debug-warnings nil
789 "Non-nil means also find LaTeX or package warnings with \\[TeX-next-error]."
790 :group 'TeX-output
791 :type 'boolean)
792
793 (defun TeX-toggle-debug-bad-boxes ()
794 "Toggle if the debugger should display \"bad boxes\" too."
795 (interactive)
796 (setq TeX-debug-bad-boxes (not TeX-debug-bad-boxes))
797 (message (concat "TeX-debug-bad-boxes: "
798 (if TeX-debug-bad-boxes "on" "off"))))
799
800 (defun TeX-toggle-debug-warnings ()
801 "Toggle if the debugger should display warnings too."
802 (interactive)
803 (setq TeX-debug-warnings (not TeX-debug-warnings))
804 (message (concat "TeX-debug-warnings: "
805 (if TeX-debug-warnings "on" "off"))))
806
807 ;;; Mode names.
808
809 (defvar TeX-base-mode-name nil
810 "Base name of mode.")
811 (make-variable-buffer-local 'TeX-base-mode-name)
812
813 (defun TeX-set-mode-name (&optional changed local reset)
814 "Build and set the mode name.
815 The base mode name will be concatenated with indicators for
816 helper modes where appropriate.
817
818 If CHANGED is non-nil, it indicates which global mode
819 may have changed so that all corresponding buffers
820 without a local value might get their name updated.
821 A value of t will thus update all buffer names.
822
823 If LOCAL is non-nil and CHANGED is buffer-local, only
824 a local change has been performed and only the local
825 name is to be updated.
826
827 If RESET is non-nil, `TeX-command-next' is reset to
828 `TeX-command-default' in updated buffers."
829 (if (and changed
830 (not (and local (local-variable-p changed (current-buffer)))))
831 (dolist (buffer (buffer-list))
832 (and (local-variable-p 'TeX-mode-p buffer)
833 (not (local-variable-p changed buffer))
834 (with-current-buffer buffer (TeX-set-mode-name nil nil reset))))
835 (if TeX-mode-p
836 (let ((trailing-flags
837 (concat
838 (and (boundp 'TeX-fold-mode) TeX-fold-mode "F")
839 (and (boundp 'LaTeX-math-mode) LaTeX-math-mode "M")
840 (and TeX-PDF-mode "P")
841 (and TeX-interactive-mode "I")
842 (and TeX-source-correlate-mode "S"))))
843 (setq mode-name (concat TeX-base-mode-name
844 (when (> (length trailing-flags) 0)
845 (concat "/" trailing-flags))))
846 (when reset
847 (TeX-process-set-variable (TeX-master-file)
848 'TeX-command-next TeX-command-default)
849 (TeX-process-set-variable (TeX-region-file)
850 'TeX-command-next TeX-command-default))
851 (set-buffer-modified-p (buffer-modified-p))))))
852
853 (defun TeX-mode-prefix ()
854 "Return the prefix of the current mode as string."
855 (cdr (assoc major-mode '((plain-tex-mode . "plain-TeX")
856 (latex-mode . "LaTeX")
857 (doctex-mode . "docTeX")
858 (texinfo-mode . "Texinfo")
859 (context-mode . "ConTeXt")))))
860
861 ;;; Viewing
862
863 (defgroup TeX-view nil
864 "Calling viewers from AUCTeX."
865 :group 'TeX-command)
866
867 (defcustom TeX-view-style
868 `((,(concat
869 "^" (regexp-opt '("a4paper" "a4dutch" "a4wide" "sem-a4")) "$")
870 "%(o?)xdvi %dS -paper a4 %d")
871 (,(concat "^" (regexp-opt '("a5paper" "a5comb")) "$")
872 "%(o?)xdvi %dS -paper a5 %d")
873 ("^b5paper$" "%(o?)xdvi %dS -paper b5 %d")
874 ("^letterpaper$" "%(o?)xdvi %dS -paper us %d")
875 ("^legalpaper$" "%(o?)xdvi %dS -paper legal %d")
876 ("^executivepaper$" "%(o?)xdvi %dS -paper 7.25x10.5in %d")
877 ("^landscape$" "%(o?)xdvi %dS -paper a4r -s 0 %d")
878 ;; The latest xdvi can show embedded postscript. If you don't
879 ;; have that, uncomment next line.
880 ;; ("^epsf$" "ghostview %f")
881 ("." "%(o?)xdvi %dS %d"))
882 "List of style options and view options.
883
884 If the first element (a regular expression) matches the name of
885 one of the style files, any occurrence of the string `%v' in a
886 command in `TeX-command-list' will be replaced with the second
887 element. The first match is used, if no match is found the `%v'
888 is replaced with the empty string.
889
890 As a default, the \"View\" command in `TeX-command-list' is set
891 to `%V'. This means that `TeX-output-view-style' will be
892 consulted before `TeX-view-style'. Only if no match is found in
893 `TeX-output-view-style' the settings in `TeX-view-style' will be
894 considered. If you want to bypass `TeX-output-view-style', which
895 is not recommended because it is more powerful than
896 `TeX-view-style', use `%v' in the \"View\" command."
897 :group 'TeX-view
898 :type '(repeat (group regexp (string :tag "Command"))))
899
900 (defcustom TeX-output-view-style
901 `(("^dvi$" ("^landscape$" "^pstricks$\\|^pst-\\|^psfrag$")
902 "%(o?)dvips -t landscape %d -o && gv %f")
903 ("^dvi$" "^pstricks$\\|^pst-\\|^psfrag$" "%(o?)dvips %d -o && gv %f")
904 ("^dvi$" (,(concat
905 "^" (regexp-opt '("a4paper" "a4dutch" "a4wide" "sem-a4")) "$")
906 "^landscape$")
907 "%(o?)xdvi %dS -paper a4r -s 0 %d")
908 ("^dvi$" ,(concat
909 "^" (regexp-opt '("a4paper" "a4dutch" "a4wide" "sem-a4")) "$")
910 "%(o?)xdvi %dS -paper a4 %d")
911 ("^dvi$" (,(concat "^" (regexp-opt '("a5paper" "a5comb")) "$")
912 "^landscape$")
913 "%(o?)xdvi %dS -paper a5r -s 0 %d")
914 ("^dvi$" ,(concat "^" (regexp-opt '("a5paper" "a5comb")) "$")
915 "%(o?)xdvi %dS -paper a5 %d")
916 ("^dvi$" "^b5paper$" "%(o?)xdvi %dS -paper b5 %d")
917 ("^dvi$" "^letterpaper$" "%(o?)xdvi %dS -paper us %d")
918 ("^dvi$" "^legalpaper$" "%(o?)xdvi %dS -paper legal %d")
919 ("^dvi$" "^executivepaper$" "%(o?)xdvi %dS -paper 7.25x10.5in %d")
920 ("^dvi$" "." "%(o?)xdvi %dS %d")
921 ("^pdf$" "." "xpdf -remote %s -raise %o %(outpage)")
922 ("^html?$" "." "netscape %o"))
923 "List of output file extensions and view options.
924
925 If the first element (a regular expression) matches the output
926 file extension, and the second element (a regular expression)
927 matches the name of one of the style options, any occurrence of
928 the string `%V' in a command in `TeX-command-list' will be
929 replaced with the third element. The first match is used; if no
930 match is found the `%V' is replaced with `%v'. The outcome of `%v'
931 is determined by the settings in `TeX-view-style' which therefore
932 serves as a fallback for `TeX-output-view-style'. The second
933 element may also be a list of regular expressions, in which case
934 all the regular expressions must match for the element to apply."
935 :group 'TeX-view
936 :type '(repeat (group
937 (regexp :tag "Extension")
938 (choice regexp (repeat :tag "List" regexp))
939 (string :tag "Command"))))
940
941 ;;; Viewing (new implementation)
942
943 (defvar TeX-view-predicate-list-builtin
944 '((output-dvi
945 (string-match "dvi" (TeX-output-extension)))
946 (output-pdf
947 (string-match "pdf" (TeX-output-extension)))
948 (output-html
949 (string-match "html" (TeX-output-extension)))
950 (style-pstricks
951 (TeX-match-style "^pstricks$\\|^pst-\\|^psfrag$"))
952 (engine-omega
953 (eq TeX-engine 'omega))
954 (engine-xetex
955 (eq TeX-engine 'xetex))
956 (mode-io-correlate
957 TeX-source-correlate-mode)
958 (paper-landscape
959 (TeX-match-style "\\`landscape\\'"))
960 (paper-portrait
961 (not (TeX-match-style "\\`landscape\\'")))
962 (paper-a4
963 (TeX-match-style "\\`a4paper\\|a4dutch\\|a4wide\\|sem-a4\\'"))
964 (paper-a5
965 (TeX-match-style "\\`a5paper\\|a5comb\\'"))
966 (paper-b5
967 (TeX-match-style "\\`b5paper\\'"))
968 (paper-letter
969 (TeX-match-style "\\`letterpaper\\'"))
970 (paper-legal
971 (TeX-match-style "\\`legalpaper\\'"))
972 (paper-executive
973 (TeX-match-style "\\`executivepaper\\'")))
974 "Alist of built-in predicates for viewer selection and invocation.
975 See the doc string of `TeX-view-predicate-list' for a short
976 description of each predicate.")
977
978 (defcustom TeX-view-predicate-list nil
979 "Alist of predicates for viewer selection and invocation.
980 The key of each list item is a symbol and the value a Lisp form
981 to be evaluated. The form should return nil if the predicate is
982 not fulfilled.
983
984 Built-in predicates provided in `TeX-view-predicate-list-builtin'
985 can be overwritten by defining predicates with the same symbol.
986
987 The following built-in predicates are available:
988 `output-dvi': The output is a DVI file.
989 `output-pdf': The output is a PDF file.
990 `output-html': The output is an HTML file.
991 `style-pstricks': The document loads a PSTricks package.
992 `engine-omega': The Omega engine is used for typesetting.
993 `engine-xetex': The XeTeX engine is used for typesetting.
994 `mode-io-correlate': TeX Source Correlate mode is active.
995 `paper-landscape': The document is typeset in landscape orientation.
996 `paper-portrait': The document is not typeset in landscape orientation.
997 `paper-a4': The paper format is A4.
998 `paper-a5': The paper format is A5.
999 `paper-b5': The paper format is B5.
1000 `paper-letter': The paper format is letter.
1001 `paper-legal': The paper format is legal.
1002 `paper-executive': The paper format is executive."
1003 :group 'TeX-view
1004 :type '(alist :key-type symbol :value-type (group sexp)))
1005
1006 (defvar TeX-view-program-list-builtin
1007 (cond
1008 ((eq system-type 'windows-nt)
1009 '(("Yap" ("yap -1" (mode-io-correlate " -s %n%b") " %o"))
1010 ("dvips and start" "dvips %d -o && start \"\" %f")
1011 ("start" "start \"\" %o")))
1012 ;; XXX: We need the advice of a Mac OS X user to configure this
1013 ;; correctly and test it.
1014 ;; ((eq system-type 'darwin)
1015 ;; '(("Preview.app" "open -a Preview.app %o")
1016 ;; ("Skim" "open -a Skim.app %o")
1017 ;; ("displayline" "displayline %n %o %b")
1018 ;; ("open" "open %o")))
1019 (t
1020 '(("xdvi" ("%(o?)xdvi"
1021 (mode-io-correlate " -sourceposition \"%n %b\" -editor \"%cS\"")
1022 ((paper-a4 paper-portrait) " -paper a4")
1023 ((paper-a4 paper-landscape) " -paper a4r")
1024 ((paper-a5 paper-portrait) " -paper a5")
1025 ((paper-a5 paper-landscape) " -paper a5r")
1026 (paper-b5 " -paper b5")
1027 (paper-letter " -paper us")
1028 (paper-legal " -paper legal")
1029 (paper-executive " -paper 7.25x10.5in")
1030 " %d"))
1031 ("dvips and gv" "%(o?)dvips %d -o && gv %f")
1032 ("gv" "gv %o")
1033 ("xpdf" ("xpdf -remote %s -raise %o" (mode-io-correlate " %(outpage)")))
1034 ("Evince" ("evince" (mode-io-correlate " -p %(outpage)") " %o"))
1035 ("xdg-open" "xdg-open %o"))))
1036 "Alist of built-in viewer specifications.
1037 This variable should not be changed by the user who can use
1038 `TeX-view-program-list' to add new viewers or overwrite the
1039 definition of built-in ones. The latter variable also contains a
1040 description of the data format.")
1041
1042 (defcustom TeX-view-program-list nil
1043 "Alist of viewer specifications.
1044 This variable can be used to specify how a viewer is to be
1045 invoked and thereby add new viewers on top of the built-in list
1046 of viewers defined in `TeX-view-program-list-builtin' or override
1047 entries in the latter.
1048
1049 The car of each item is a string with a user-readable name. The
1050 second element can be a command line to be run as a process or a
1051 Lisp function to be executed. The command line can either be
1052 specified as a single string or a list of strings and two-part
1053 lists. The first element of the two-part lists is a symbol or a
1054 list of symbols referring to one or more of the predicates in
1055 `TeX-view-predicate-list' or `TeX-view-predicate-list-builtin'.
1056 The second part of the two-part lists is a command line part.
1057 The command line for the viewer is constructed by concatenating
1058 the command line parts. Parts with a predicate are only
1059 considered if the predicate was evaluated with a positive result.
1060 Note that the command line can contain placeholders as defined in
1061 `TeX-expand-list' which are expanded before the viewer is called.
1062
1063 The use of a function as the second element only works if the
1064 View command in `TeX-command-list' makes use of the hook
1065 `TeX-run-discard-or-function'.
1066
1067 Note: Predicates defined in the current Emacs session will only
1068 show up in the customization interface for this variable after
1069 restarting Emacs."
1070 :group 'TeX-view
1071 :type
1072 `(alist
1073 :key-type (string :tag "Name")
1074 :value-type
1075 (choice
1076 (group :tag "Command" (string :tag "Command"))
1077 (group :tag "Command parts"
1078 (repeat
1079 :tag "Command parts"
1080 (choice
1081 (string :tag "Command part")
1082 (list :tag "Predicate and command part"
1083 ,(let (list)
1084 ;; Build the list of available predicates.
1085 (mapc (lambda (spec)
1086 (add-to-list 'list `(const ,(car spec))))
1087 (append TeX-view-predicate-list
1088 TeX-view-predicate-list-builtin))
1089 ;; Sort the list alphabetically.
1090 (setq list (sort list
1091 (lambda (a b)
1092 (string<
1093 (downcase (symbol-name (cadr a)))
1094 (downcase (symbol-name (cadr b)))))))
1095 `(choice
1096 (choice :tag "Predicate" ,@list)
1097 (repeat :tag "List of predicates"
1098 (choice :tag "Predicate" ,@list))))
1099 (string :tag "Command part")))))
1100 (group :tag "Function" function))))
1101
1102 ;; XXX: Regarding a possibility to (manually) run an update command,
1103 ;; one could support this through `TeX-view' by letting it temporarily
1104 ;; set a variable which is checked with a predicate in the viewer
1105 ;; selection. If the check is positive, the update command is run
1106 ;; instead of the normal viewer command. Direct support through the
1107 ;; View command would require a predicate which knows when an update
1108 ;; has to be done.
1109 (defcustom TeX-view-program-selection
1110 (cond
1111 ((eq system-type 'windows-nt)
1112 '(((output-dvi style-pstricks) "dvips and start")
1113 (output-dvi "Yap")
1114 (output-pdf "start")
1115 (output-html "start")))
1116 ;; XXX: We need the advice of a Mac OS X user to configure this
1117 ;; correctly and test it.
1118 ;; ((eq system-type 'darwin)
1119 ;; '((output-dvi "open")
1120 ;; (output-pdf "open")
1121 ;; (output-html "open")))
1122 (t
1123 '(((output-dvi style-pstricks) "dvips and gv")
1124 (output-dvi "xdvi")
1125 (output-pdf "Evince")
1126 (output-html "xdg-open"))))
1127 "Alist of predicates and viewers.
1128 Each entry consists of a list with two elements. The first
1129 element is a symbol or list of symbols referring to predicates as
1130 defined in `TeX-view-predicate-list' or
1131 `TeX-view-predicate-list-builtin'. The second element is a
1132 string referring to the name of a viewer as defined in
1133 `TeX-view-program-list' or `TeX-view-program-list-builtin'.
1134
1135 When a viewer is called for, the entries are evaluated in turn
1136 and the viewer related to the first entry all predicates of which
1137 are evaluated positively is chosen."
1138 :group 'TeX-view
1139 :type `(alist :key-type
1140 ;; Offer list of defined predicates.
1141 ,(let (list)
1142 (mapc (lambda (spec)
1143 (add-to-list 'list `(const ,(car spec))))
1144 (append TeX-view-predicate-list
1145 TeX-view-predicate-list-builtin))
1146 (setq list (sort list
1147 (lambda (a b)
1148 (string<
1149 (downcase (symbol-name (cadr a)))
1150 (downcase (symbol-name (cadr b)))))))
1151 `(choice (choice :tag "Single predicate" ,@list)
1152 (repeat :tag "Multiple predicates"
1153 (choice ,@list))))
1154 :value-type
1155 ;; Offer list of defined viewers.
1156 (group (choice :tag "Viewer"
1157 ,@(let (list)
1158 (mapc (lambda (spec)
1159 (add-to-list 'list
1160 `(const ,(car spec))))
1161 (append TeX-view-program-list
1162 TeX-view-program-list-builtin))
1163 (sort list
1164 (lambda (a b)
1165 (string< (downcase (cadr a))
1166 (downcase (cadr b))))))))))
1167
1168 (defun TeX-match-style (regexp)
1169 "Check if a style matching REGEXP is active."
1170 (TeX-member regexp (TeX-style-list) 'string-match))
1171
1172 (defun TeX-view-match-predicate (predicate)
1173 "Check if PREDICATE is true.
1174 PREDICATE can be a symbol or a list of symbols defined in
1175 `TeX-view-predicate-list-builtin' or `TeX-view-predicate-list'.
1176 In case of a single symbol, return t if the predicate is true,
1177 nil otherwise. In case of a list of symbols, return t if all
1178 predicates are true, nil otherwise."
1179 (let ((pred-symbols (if (listp predicate) predicate (list predicate)))
1180 (pred-defs (append TeX-view-predicate-list
1181 TeX-view-predicate-list-builtin))
1182 (result t)
1183 elt)
1184 (while (and (setq elt (pop pred-symbols)) result)
1185 (unless (eval (cadr (assq elt pred-defs)))
1186 (setq result nil)))
1187 result))
1188
1189 (defun TeX-view-command-raw ()
1190 "Choose a viewer and return its unexpanded command string."
1191 (let ((selection TeX-view-program-selection)
1192 entry viewer spec command)
1193 ;; Find the appropriate viewer.
1194 (while (and (setq entry (pop selection)) (not viewer))
1195 (when (TeX-view-match-predicate (car entry))
1196 (setq viewer (cadr entry))))
1197 (unless viewer
1198 (error "No matching viewer found"))
1199 ;; Get the command line or function spec.
1200 (setq spec (cadr (assoc viewer (append TeX-view-program-list
1201 TeX-view-program-list-builtin))))
1202 (cond ((functionp spec)
1203 ;; Converting the function call to a string is ugly, but
1204 ;; the backend currently only supports strings.
1205 (prin1-to-string spec))
1206 ((stringp spec)
1207 spec)
1208 (t
1209 ;; Build the unexpanded command line. Pieces with predicates are
1210 ;; only added if the predicate is evaluated positively.
1211 (dolist (elt spec)
1212 (cond ((stringp elt)
1213 (setq command (concat command elt)))
1214 ((listp elt)
1215 (when (TeX-view-match-predicate (car elt))
1216 (setq command (concat command (cadr elt)))))))
1217 command))))
1218
1219 ;;; Engine
1220
1221 (defvar TeX-engine-alist-builtin
1222 '((default "Default" TeX-command LaTeX-command ConTeXt-engine)
1223 (xetex "XeTeX" "xetex" "xelatex" "xetex")
1224 (luatex "LuaTeX" "luatex" "lualatex" "luatex")
1225 (omega "Omega" TeX-Omega-command LaTeX-Omega-command ConTeXt-Omega-engine))
1226 "Alist of built-in TeX engines and associated commands.
1227 For a description of the format see `TeX-engine-alist'.")
1228
1229 (defcustom TeX-engine-alist nil
1230 "Alist of TeX engines and associated commands.
1231 Each entry is a list with a maximum of five elements. The first
1232 element is a symbol used to identify the engine. The second is a
1233 string describing the engine. The third is the command to be
1234 used for plain TeX. The fourth is the command to be used for
1235 LaTeX. The fifth is the command to be used for the --engine
1236 parameter of ConTeXt's texexec program. Each command can either
1237 be a variable or a string. An empty string or nil means there is
1238 no command available.
1239
1240 You can override a built-in engine defined in the variable
1241 `TeX-engine-alist-builtin' by adding an entry beginning with the
1242 same symbol as the built-in entry to `TeX-engine-alist'."
1243 :group 'TeX-command
1244 :type '(repeat (group symbol
1245 (string :tag "Name")
1246 (choice :tag "Plain TeX command" string variable)
1247 (choice :tag "LaTeX command" string variable)
1248 (choice :tag "ConTeXt command" string variable))))
1249
1250 (defun TeX-engine-alist ()
1251 "Return an alist of TeX engines.
1252 The function appends the built-in engine specs from
1253 `TeX-engine-alist-builtin' and the user-defined engines from
1254 `TeX-engine-alist' and deletes any entries from the built-in part
1255 where an entry with the same car exists in the user-defined part."
1256 (TeX-delete-dups-by-car (append TeX-engine-alist TeX-engine-alist-builtin)))
1257
1258 (defcustom TeX-engine 'default
1259 (concat "Type of TeX engine to use.
1260 It should be one of the following symbols:\n\n"
1261 (mapconcat (lambda (x) (format "* `%s'" (car x)))
1262 (TeX-engine-alist) "\n"))
1263 :group 'TeX-command
1264 :type `(choice ,@(mapcar (lambda (x)
1265 `(const :tag ,(nth 1 x) ,(car x)))
1266 (TeX-engine-alist))))
1267 (make-variable-buffer-local 'TeX-engine)
1268 (put 'TeX-engine 'safe-local-variable
1269 (lambda (arg) (memq arg (mapcar 'car TeX-engine-alist-builtin))))
1270
1271 (defun TeX-engine-set (type)
1272 (concat "Set TeX engine to TYPE.
1273 TYPE can be one of the following symbols:\n"
1274 (mapconcat (lambda (x) (format "* `%s'" (car x)))
1275 (TeX-engine-alist) "\n"))
1276 (interactive (list (completing-read "Engine: "
1277 (mapcar (lambda (x)
1278 (symbol-name (car x)))
1279 (TeX-engine-alist))
1280 nil t)))
1281 (when (stringp type)
1282 (setq type (intern type)))
1283 (setq TeX-engine type)
1284 ;; Automatically enable or disable TeX PDF mode as a convenience
1285 (cond ((eq type 'xetex) (TeX-PDF-mode 1))
1286 ((eq type 'omega) (TeX-PDF-mode 0))))
1287
1288 (define-minor-mode TeX-Omega-mode
1289 "Minor mode for using the Omega engine."
1290 nil nil nil
1291 :group 'TeX-command
1292 (TeX-engine-set (if TeX-Omega-mode 'omega 'default)))
1293 (defalias 'tex-omega-mode 'TeX-Omega-mode)
1294 (TeX--call-3/2 #'make-obsolete 'TeX-Omega-mode 'TeX-engine-set "before 11.86")
1295 (TeX--call-3/2 #'make-obsolete-variable 'TeX-Omega-mode
1296 'TeX-engine "before 11.86")
1297
1298 ;;; Forward and inverse search
1299
1300 (defcustom TeX-source-correlate-method 'auto
1301 "Method to use for enabling forward and inverse search.
1302 This can be `source-specials' if source specials should be used,
1303 `synctex' if SyncTeX should be used, or`auto' if AUCTeX should
1304 decide.
1305
1306 Setting this variable does not take effect if TeX Source
1307 Correlate mode has already been active. Restart Emacs in this
1308 case."
1309 :type '(choice (const auto) (const synctex) (const source-specials))
1310 :group 'TeX-view)
1311
1312 (defvar TeX-source-correlate-method-active nil
1313 "Method actually used for forward and inverse search.")
1314
1315 (defvar TeX-source-correlate-output-page-function nil
1316 "Symbol of function returning an output page relating to buffer position.
1317 The function should take no arguments and return the page numer
1318 as a string.")
1319 (make-variable-buffer-local 'TeX-source-correlate-output-page-function)
1320
1321 (defcustom TeX-source-correlate-start-server 'ask
1322 "Control if server should be started for inverse search."
1323 :type '(choice (const :tag "Always" t)
1324 (const :tag "Never" nil)
1325 (const :tag "Ask" ask))
1326 :group 'TeX-view)
1327 (when (fboundp 'defvaralias)
1328 (defvaralias 'TeX-source-specials-view-start-server
1329 'TeX-source-correlate-start-server))
1330
1331 (defvar TeX-source-correlate-start-server-asked nil
1332 "Keep track if question about server start search was asked.")
1333
1334 (defvar TeX-source-correlate-start-server-flag nil
1335 "If non-nil, `TeX-source-correlate-start-server-maybe' will start a server.
1336 Code related to features requiring a server, e.g. for inverse
1337 search, can set the variable.")
1338
1339 (defun TeX-source-correlate-gnuserv-p ()
1340 "Guess whether to use gnuserv when a server is requested."
1341 (cond ((and (boundp 'gnuserv-process)
1342 (processp gnuserv-process)))
1343 ((and (boundp 'server-process)
1344 (processp server-process))
1345 nil)
1346 ((featurep 'xemacs))))
1347
1348 (defun TeX-source-correlate-server-enabled-p ()
1349 "Return non-nil if Emacs server or gnuserv is enabled."
1350 (let* ((gnuserv-p (TeX-source-correlate-gnuserv-p))
1351 (process (if gnuserv-p 'gnuserv-process 'server-process)))
1352 (and (boundp process) (processp (symbol-value process)))))
1353
1354 (defun TeX-source-correlate-start-server-maybe ()
1355 "Start Emacs server or gnuserv if a feature using it is enabled.
1356 This is the case if `TeX-source-correlate-start-server-flag' is non-nil."
1357 (when (and TeX-source-correlate-start-server-flag
1358 (not (TeX-source-correlate-server-enabled-p)))
1359 (let* ((gnuserv-p (TeX-source-correlate-gnuserv-p))
1360 (start (if gnuserv-p 'gnuserv-start 'server-start)))
1361 (cond
1362 ;; Server should be started unconditionally
1363 ((eq TeX-source-correlate-start-server t)
1364 (funcall start))
1365 ;; Ask user if server is to be started
1366 ((and (eq TeX-source-correlate-start-server 'ask)
1367 (not TeX-source-correlate-start-server-asked)
1368 (prog1
1369 (y-or-n-p (format "Start %s for inverse search in viewer? "
1370 (if gnuserv-p
1371 "gnuserv"
1372 "Emacs server")))
1373 (setq TeX-source-correlate-start-server-asked t)))
1374 (funcall start))))))
1375
1376 (defun TeX-source-correlate-determine-method ()
1377 "Determine which method is available for forward and inverse search."
1378 (let ((help (condition-case nil
1379 (with-output-to-string
1380 (call-process LaTeX-command
1381 nil (list standard-output nil) nil "--help"))
1382 (error ""))))
1383 (if (string-match "^[ ]*-synctex" help)
1384 'synctex
1385 'source-specials)))
1386
1387 (defun TeX-source-correlate-expand-options ()
1388 "Return TeX engine command line option for forward search facilities.
1389 The return value depends on the value of `TeX-source-correlate-mode'.
1390 If this is nil, an empty string will be returned."
1391 (if TeX-source-correlate-mode
1392 (if (eq TeX-source-correlate-method-active 'source-specials)
1393 (concat TeX-source-specials-tex-flags
1394 (if TeX-source-specials-places
1395 ;; -src-specials=WHERE: insert source specials
1396 ;; in certain places of the DVI file. WHERE is a
1397 ;; comma-separated value list: cr display hbox
1398 ;; math par parend vbox
1399 (concat "=" (mapconcat 'identity
1400 TeX-source-specials-places ","))))
1401 TeX-synctex-tex-flags)
1402 ""))
1403
1404 (defvar TeX-source-correlate-map
1405 (let ((map (make-sparse-keymap)))
1406 ;; (if (featurep 'xemacs)
1407 ;; (define-key map [(control button1)] #'TeX-view-mouse)
1408 ;; (define-key map [C-down-mouse-1] #'TeX-view-mouse))
1409 map)
1410 "Keymap for `TeX-source-correlate-mode'.
1411 You could use this for unusual mouse bindings.")
1412
1413 (define-minor-mode TeX-source-correlate-mode
1414 "Minor mode for forward and inverse search.
1415
1416 If enabled, the viewer can be advised to show the output page
1417 corresponding to the point in the source and vice versa.
1418
1419 The method to be used can be controlled with the variable
1420 `TeX-source-correlate-method'. Currently source specials or
1421 SyncTeX are recognized."
1422 :group 'TeX-view
1423 ;; Since this is a global minor mode and we don't want to require
1424 ;; tex.el when the mode variable is set, the mode function is called
1425 ;; explicitely (if necessary) in `VirTeX-common-initialization'. We
1426 ;; do it there because otherwise `kill-all-local-variables' would
1427 ;; reset `TeX-source-correlate-output-page-function' which is
1428 ;; buffer-local.
1429 :global t
1430 (set-keymap-parent TeX-mode-map (and TeX-source-correlate-mode
1431 TeX-source-correlate-map))
1432 (TeX-set-mode-name 'TeX-source-correlate-mode t t)
1433 (setq TeX-source-correlate-start-server-flag TeX-source-correlate-mode)
1434 (unless TeX-source-correlate-method-active
1435 (setq TeX-source-correlate-method-active
1436 (if (eq TeX-source-correlate-method 'auto)
1437 (TeX-source-correlate-determine-method)
1438 TeX-source-correlate-method)))
1439 (when (eq TeX-source-correlate-method-active 'synctex)
1440 (setq TeX-source-correlate-output-page-function
1441 (when TeX-source-correlate-mode
1442 'TeX-synctex-output-page))))
1443 (defalias 'TeX-source-specials-mode 'TeX-source-correlate-mode)
1444 (TeX--call-3/2 #'make-obsolete 'TeX-source-specials-mode
1445 'TeX-source-correlate-mode "before 11.86")
1446 (defalias 'tex-source-correlate-mode 'TeX-source-correlate-mode)
1447 (put 'TeX-source-correlate-mode 'safe-local-variable 'TeX-booleanp)
1448 ;; We do not want the custom variable to require tex.el. This is only
1449 ;; necessary if AUCTeX was compiled with Emacs 21.
1450 (put 'TeX-source-correlate-mode 'custom-requests nil)
1451 (setq minor-mode-map-alist
1452 (delq (assq 'TeX-source-correlate-mode minor-mode-map-alist)
1453 minor-mode-map-alist))
1454
1455
1456 ;;; Source Specials
1457
1458 (defcustom TeX-source-specials-tex-flags "-src-specials"
1459 "Extra flags to pass to TeX commands to generate source specials."
1460 :group 'TeX-view
1461 :type 'string)
1462
1463 (defcustom TeX-source-specials-places nil
1464 "List of places where to insert source specials into the DVI file.
1465 If nil, use (La)TeX's defaults."
1466 :group 'TeX-view
1467 :type '(list (set :inline t
1468 ;; :tag "Options known to work"
1469 ;; cr display hbox math par parend vbox
1470 (const "cr")
1471 (const "display")
1472 (const "hbox")
1473 (const "math")
1474 (const "par")
1475 (const "parend")
1476 (const "vbox"))
1477 (repeat :inline t
1478 :tag "Other options"
1479 (string))))
1480
1481 (defcustom TeX-source-specials-view-position-flags
1482 "-sourceposition \"%n %b\""
1483 "Flags to pass to the DVI viewer commands for the position in the source."
1484 :group 'TeX-view
1485 :type 'string)
1486
1487 (defcustom TeX-source-specials-view-editor-flags
1488 "-editor \"%cS\""
1489 "Flags to pass to DVI viewer commands for inverse search."
1490 :group 'TeX-view
1491 :type 'string)
1492
1493 (defcustom TeX-source-specials-view-gnuclient-flags
1494 "-q +%%l %%f"
1495 "Flags to pass to gnuclient for inverse search."
1496 :group 'TeX-view
1497 :type 'string)
1498
1499 (defcustom TeX-source-specials-view-emacsclient-flags
1500 "--no-wait +%%l %%f"
1501 "Flags to emacsclient for inverse search."
1502 :group 'TeX-view
1503 :type 'string)
1504
1505 ;; FIXME: Make client binaries configurable.
1506 (defun TeX-source-specials-view-expand-client ()
1507 "Return gnuclient or emacslient executable with options.
1508 Return the full path to the executable if possible."
1509 (let* ((gnuserv-p (TeX-source-correlate-gnuserv-p))
1510 (client-base (if gnuserv-p
1511 "gnuclient"
1512 "emacsclient"))
1513 (client-full (and invocation-directory
1514 (expand-file-name client-base
1515 invocation-directory)))
1516 (options (if gnuserv-p
1517 TeX-source-specials-view-gnuclient-flags
1518 TeX-source-specials-view-emacsclient-flags)))
1519 (if (and client-full (file-executable-p client-full))
1520 (concat client-full " " options)
1521 (concat client-base " " options))))
1522
1523 (defun TeX-source-specials-view-expand-options (&optional viewer)
1524 "Return source specials command line option for viewer command.
1525 The return value depends on the values of
1526 `TeX-source-correlate-mode' and
1527 `TeX-source-correlate-method-active'. If those are nil or not
1528 `source-specials' respectively, an empty string will be
1529 returned."
1530 (if (and TeX-source-correlate-mode
1531 (eq TeX-source-correlate-method-active 'source-specials))
1532 (concat TeX-source-specials-view-position-flags
1533 (when (TeX-source-correlate-server-enabled-p)
1534 (concat " " TeX-source-specials-view-editor-flags)))
1535 ""))
1536
1537 ;;; SyncTeX
1538
1539 (defvar TeX-synctex-tex-flags "--synctex=1"
1540 "Extra flags to pass to TeX commands to enable SyncTeX.")
1541
1542 (defun TeX-synctex-output-page ()
1543 "Return the page corresponding to the current source position.
1544 This method assumes that the document was compiled with SyncTeX
1545 enabled and the `synctex' binary is available."
1546 (let ((synctex-output
1547 (with-output-to-string
1548 (call-process "synctex" nil (list standard-output nil) nil "view"
1549 "-i" (format "%s:%s:%s" (line-number-at-pos)
1550 (current-column)
1551 ;; The file name relative to the
1552 ;; directory of the master file.
1553 (file-relative-name
1554 (buffer-file-name)
1555 (file-name-directory
1556 (TeX-active-master))))
1557 "-o" (TeX-active-master (TeX-output-extension))))))
1558 (when (string-match "Page:\\([0-9]+\\)" synctex-output)
1559 (match-string 1 synctex-output))))
1560
1561 ;;; Miscellaneous minor modes
1562
1563 (defvar TeX-mode-p nil
1564 "This indicates a TeX mode being active.")
1565 (make-variable-buffer-local 'TeX-mode-p)
1566
1567 (defun TeX-mode-set (var value)
1568 (set-default var value)
1569 (TeX-set-mode-name var nil t))
1570
1571 (defcustom TeX-PDF-mode nil nil
1572 :group 'TeX-command
1573 :set 'TeX-mode-set
1574 :type 'boolean)
1575 (put 'TeX-PDF-mode 'safe-local-variable 'TeX-booleanp)
1576
1577 (define-minor-mode TeX-PDF-mode
1578 "Minor mode for using PDFTeX.
1579
1580 If enabled, PDFTeX will be used as an executable by default.
1581 You can customize an initial value, and you can use the
1582 function `TeX-global-PDF-mode' for toggling this value."
1583 :group 'TeX-command
1584 (when (eq TeX-engine 'omega)
1585 (setq TeX-PDF-mode nil))
1586 (setq TeX-PDF-mode-parsed nil)
1587 (TeX-set-mode-name nil nil t)
1588 (setq TeX-output-extension
1589 (if TeX-PDF-mode "pdf" "dvi")))
1590 (add-to-list 'minor-mode-alist '(TeX-PDF-mode ""))
1591
1592 (defun TeX-global-PDF-mode (&optional arg)
1593 "Toggle default for `TeX-PDF-mode'."
1594 (interactive "P")
1595 (prog1
1596 (setq-default TeX-PDF-mode
1597 (if arg (> (prefix-numeric-value arg) 0)
1598 (not (default-value 'TeX-PDF-mode))))
1599 (TeX-set-mode-name 'TeX-PDF-mode nil t)))
1600
1601 (defalias 'tex-pdf-mode 'TeX-PDF-mode)
1602
1603 (defvar TeX-PDF-mode-parsed nil
1604 "Set if `TeX-PDF-mode' has come about by parsing.")
1605
1606 (make-variable-buffer-local 'TeX-PDF-mode-parsed)
1607
1608 (defun TeX-PDF-mode-parsed (arg)
1609 "Change `TeX-PDF-mode' to ARG based on parsing.
1610 If this conflicts with previous parsed settings,
1611 just use the default. If an explicit setting is
1612 already established, don't do anything."
1613
1614 ;; Basically we have the following situations:
1615 ;; TeX-PDF-mode-parsed (local-variable-p 'TeX-PDF-mode):
1616 ;; nil nil : virgin state
1617 ;; nil t : stably set state (possibly because of conflicting parse info)
1618 ;; t t : non-conflicting parsed info
1619
1620 (if TeX-PDF-mode-parsed
1621 (unless (eq TeX-PDF-mode arg)
1622 (TeX-PDF-mode (if (default-value 'TeX-PDF-mode) 1 0)))
1623 (unless (local-variable-p 'TeX-PDF-mode (current-buffer))
1624 (TeX-PDF-mode (if arg 1 0))
1625 (setq TeX-PDF-mode-parsed t))))
1626
1627 (defun TeX-PDF-mode-on ()
1628 "Use only from parsing routines."
1629 (TeX-PDF-mode-parsed t))
1630
1631 (defun TeX-PDF-mode-off ()
1632 "Use only from parsing routines."
1633 (TeX-PDF-mode-parsed nil))
1634
1635 (defcustom TeX-DVI-via-PDFTeX nil
1636 "Whether to use PDFTeX also for producing DVI files."
1637 :group 'TeX-command
1638 :type 'boolean)
1639
1640 (define-minor-mode TeX-interactive-mode
1641 "Minor mode for interactive runs of TeX."
1642 nil nil nil
1643 :group 'TeX-command
1644 (TeX-set-mode-name 'TeX-interactive-mode t t))
1645 (defalias 'tex-interactive-mode 'TeX-interactive-mode)
1646 (add-to-list 'minor-mode-alist '(TeX-interactive-mode ""))
1647
1648 ;;; Commands
1649
1650 (defgroup TeX-command-name nil
1651 "Names for external commands in AUCTeX."
1652 :group 'TeX-command)
1653
1654 (defcustom TeX-command-BibTeX "BibTeX"
1655 "*The name of the BibTeX entry in `TeX-command-list'."
1656 :group 'TeX-command-name
1657 :type 'string)
1658 (make-variable-buffer-local 'TeX-command-BibTeX)
1659
1660 (defcustom TeX-command-Show "View"
1661 "*The default command to show (view or print) a TeX file.
1662 Must be the car of an entry in `TeX-command-list'."
1663 :group 'TeX-command-name
1664 :type 'string)
1665 (make-variable-buffer-local 'TeX-command-Show)
1666
1667 (defcustom TeX-command-Print "Print"
1668 "The name of the Print entry in `TeX-command-Print'."
1669 :group 'TeX-command-name
1670 :type 'string)
1671
1672 (defcustom TeX-command-Queue "Queue"
1673 "The name of the Queue entry in `TeX-command-Queue'."
1674 :group 'TeX-command-name
1675 :type 'string)
1676
1677 (defvar TeX-trailer-start nil
1678 "Regular expression delimiting start of trailer in a TeX file.")
1679
1680 (make-variable-buffer-local 'TeX-trailer-start)
1681
1682 (defvar TeX-header-end nil
1683 "Regular expression delimiting end of header in a TeX file.")
1684
1685 (make-variable-buffer-local 'TeX-header-end)
1686
1687 (defvar TeX-command-default nil
1688 "The default command for `TeX-command' in the current major mode.")
1689
1690 (make-variable-buffer-local 'TeX-command-default)
1691
1692 (put 'TeX-command-default 'safe-local-variable 'stringp)
1693
1694 (defvar TeX-clean-default-intermediate-suffixes
1695 '("\\.aux" "\\.bbl" "\\.blg" "\\.brf" "\\.fot"
1696 "\\.glo" "\\.gls" "\\.idx" "\\.ilg" "\\.ind"
1697 "\\.lof" "\\.log" "\\.lot" "\\.nav" "\\.out"
1698 "\\.snm" "\\.toc" "\\.url" "\\.synctex\\.gz")
1699 "List of regexps matching suffixes of files to be cleaned.
1700 Used as a default in TeX, LaTeX and docTeX mode.")
1701
1702 (defvar TeX-clean-default-output-suffixes
1703 '("\\.dvi" "\\.pdf" "\\.ps" "\\.xdv")
1704 "List of regexps matching suffixes of files to be cleaned.
1705 Used as a default in TeX, LaTeX and docTeX mode.")
1706
1707 (defcustom TeX-clean-confirm t
1708 "If non-nil, ask before deleting files."
1709 :type 'boolean
1710 :group 'TeX-command)
1711
1712 (autoload 'dired-mark-pop-up "dired")
1713
1714 (defun TeX-clean (&optional arg)
1715 "Delete generated files associated with current master and region files.
1716 If prefix ARG is non-nil, not only remove intermediate but also
1717 output files."
1718 (interactive "P")
1719 (let* ((mode-prefix (TeX-mode-prefix))
1720 (suffixes (append (symbol-value
1721 (intern (concat mode-prefix
1722 "-clean-intermediate-suffixes")))
1723 (when arg
1724 (symbol-value
1725 (intern (concat mode-prefix
1726 "-clean-output-suffixes"))))))
1727 (master (TeX-active-master))
1728 (master-dir (file-name-directory master))
1729 (regexp (concat "\\("
1730 (file-name-nondirectory master) "\\|"
1731 (TeX-region-file nil t)
1732 "\\)"
1733 "\\("
1734 (mapconcat 'identity suffixes "\\|")
1735 "\\)\\'"
1736 "\\|" (TeX-region-file t t)))
1737 (files (when regexp
1738 (directory-files (or master-dir ".") nil regexp))))
1739 (if files
1740 (when (or (not TeX-clean-confirm)
1741 (condition-case nil
1742 (dired-mark-pop-up " *Deletions*" 'delete
1743 (if (> (length files) 1)
1744 files
1745 (cons t files))
1746 'y-or-n-p "Delete files? ")
1747 (wrong-type-argument ; e.g. with Emacs 21
1748 (y-or-n-p (format "Delete %S? " (car files))))))
1749 (dolist (file files)
1750 (delete-file (concat master-dir file))))
1751 (message "No files to be deleted"))))
1752
1753
1754 ;;; Master File
1755
1756 (defcustom TeX-master t
1757 "*The master file associated with the current buffer.
1758 If the file being edited is actually included from another file, you
1759 can tell AUCTeX the name of the master file by setting this variable.
1760 If there are multiple levels of nesting, specify the top level file.
1761
1762 If this variable is nil, AUCTeX will query you for the name.
1763
1764 If the variable is t, AUCTeX will assume the file is a master file
1765 itself.
1766
1767 If the variable is 'shared, AUCTeX will query for the name, but not
1768 change the file.
1769
1770 If the variable is 'dwim, AUCTeX will try to avoid querying by
1771 attempting to `do what I mean'; and then change the file.
1772
1773 It is suggested that you use the File Variables (see the info node in
1774 the Emacs manual) to set this variable permanently for each file."
1775 :group 'TeX-command
1776 :group 'TeX-parse
1777 :type '(choice (const :tag "Query" nil)
1778 (const :tag "This file" t)
1779 (const :tag "Shared" shared)
1780 (const :tag "Dwim" dwim)
1781 (string :format "%v")))
1782 (make-variable-buffer-local 'TeX-master)
1783 (put 'TeX-master 'safe-local-variable
1784 '(lambda (x)
1785 (or (stringp x)
1786 (member x (quote (t nil shared dwim))))))
1787
1788 (defcustom TeX-one-master "\\.\\(texi?\\|dtx\\)$"
1789 "*Regular expression matching ordinary TeX files.
1790
1791 You should set this variable to match the name of all files, where
1792 automatically adding a file variable with the name of the master file
1793 is a good idea. When AUCTeX adds the name of the master file as a
1794 file variable, it does not need to ask next time you edit the file.
1795
1796 If you dislike AUCTeX automatically modifying your files, you can set
1797 this variable to \"<none>\"."
1798 :group 'TeX-command
1799 :type 'regexp)
1800
1801 (defvar TeX-convert-master t
1802 "*If not nil, automatically convert ``Master:'' lines to file variables.
1803 This will be done when AUCTeX first try to use the master file.")
1804
1805 ;; Can be let-bound temporarily in order to inhibit the master file question
1806 ;; by using its value instead in case `TeX-master' is nil or 'shared.
1807 (defvar TeX-transient-master nil)
1808
1809 (defun TeX-dwim-master ()
1810 "Find a likely `TeX-master'."
1811 (let ((dir default-directory))
1812 (dolist (buf (buffer-list))
1813 (when (with-current-buffer buf
1814 (and (equal dir default-directory)
1815 (stringp TeX-master)))
1816 (return (with-current-buffer buf TeX-master))))))
1817
1818 (defun TeX-master-file-ask ()
1819 "Ask for master file, set `TeX-master' and add local variables."
1820 (interactive)
1821 (if (TeX-local-master-p)
1822 (error "Master file already set")
1823 (let* ((default (TeX-dwim-master))
1824 (name (or (and (eq 'dwim TeX-master) default)
1825 (condition-case nil
1826 (read-file-name (format "Master file: (default %s) "
1827 (or default "this file"))
1828 nil default)
1829 (quit "<quit>")))))
1830 (cond ((string= name "<quit>")
1831 (setq TeX-master t))
1832 ((string= name default)
1833 (setq TeX-master default)
1834 (TeX-add-local-master))
1835 ((or
1836 ;; Default `read-file-name' proposes and buffer visits a file.
1837 (string= (expand-file-name name) (buffer-file-name))
1838 ;; Default of `read-file-name' and buffer does not visit a file.
1839 (string= name default-directory)
1840 ;; User typed <RET> in an empty minibuffer.
1841 (string= name ""))
1842 (setq TeX-master t)
1843 (TeX-add-local-master))
1844 (t
1845 (setq TeX-master (TeX-strip-extension (file-relative-name name)
1846 (list TeX-default-extension)
1847 'path))
1848 (TeX-add-local-master))))))
1849
1850 (defun TeX-master-file (&optional extension nondirectory ask)
1851 "Set and return the name of the master file for the current document.
1852
1853 If optional argument EXTENSION is non-nil, add that file extension to
1854 the name. Special value t means use `TeX-default-extension'.
1855
1856 If optional second argument NONDIRECTORY is non-nil, do not include
1857 the directory.
1858
1859 If optional third argument ASK is non-nil, ask the user for the
1860 name of master file if it cannot be determined otherwise.
1861
1862 Currently it will check for the presence of a ``Master:'' line in
1863 the beginning of the file, but that feature will be phased out."
1864 (interactive)
1865 (if (eq extension t)
1866 (setq extension TeX-default-extension))
1867 (let ((my-name (if (buffer-file-name)
1868 (TeX-strip-extension nil (list TeX-default-extension) t)
1869 "<none>")))
1870 (save-excursion
1871 (save-restriction
1872 (widen)
1873 (goto-char (point-min))
1874 (cond
1875 ((and TeX-transient-master
1876 (or (not TeX-master) (eq TeX-master 'shared)))
1877 (setq TeX-master TeX-transient-master))
1878 ;; Special value 't means it is own master (a free file).
1879 ((equal TeX-master my-name)
1880 (setq TeX-master t))
1881
1882 ;; For files shared between many documents.
1883 ((and (eq 'shared TeX-master) ask)
1884 (setq TeX-master
1885 (let* ((default (TeX-dwim-master))
1886 (name (read-file-name
1887 (format "Master file: (default %s) "
1888 (or default "this file"))
1889 nil default)))
1890 (cond ((string= name default)
1891 default)
1892 ((or
1893 ;; Default `read-file-name' proposes and
1894 ;; buffer visits a file.
1895 (string= (expand-file-name name)
1896 (buffer-file-name))
1897 ;; Default of `read-file-name' and
1898 ;; buffer does not visit a file.
1899 (string= name default-directory)
1900 ;; User typed <RET> in an empty minibuffer.
1901 (string= name ""))
1902 t)
1903 (t
1904 (TeX-strip-extension
1905 name (list TeX-default-extension) 'path))))))
1906
1907 ;; We might already know the name.
1908 ((or (eq TeX-master t) (stringp TeX-master)) TeX-master)
1909
1910 ;; Support the ``Master:'' line (under protest!)
1911 ((re-search-forward
1912 "^%% *[Mm]aster:?[ \t]*\\([^ \t\n]+\\)" 500 t)
1913 (setq TeX-master
1914 (TeX-strip-extension (TeX-match-buffer 1)
1915 (list TeX-default-extension)))
1916 (if TeX-convert-master
1917 (progn
1918 (beginning-of-line)
1919 (kill-line 1)
1920 (TeX-add-local-master))))
1921
1922 ;; Ask the user (but add it as a local variable).
1923 (ask (TeX-master-file-ask)))))
1924
1925 (let ((name (if (stringp TeX-master)
1926 TeX-master
1927 my-name)))
1928
1929 (if (TeX-match-extension name)
1930 ;; If it already has an extension...
1931 (if (equal extension TeX-default-extension)
1932 ;; Use instead of the default extension
1933 (setq extension nil)
1934 ;; Otherwise drop it.
1935 (setq name (TeX-strip-extension name))))
1936
1937 ;; Remove directory if needed.
1938 (if nondirectory
1939 (setq name (file-name-nondirectory name)))
1940
1941 (if extension
1942 (concat name "." extension)
1943 name))))
1944
1945 (defun TeX-master-directory ()
1946 "Directory of master file."
1947 (file-name-as-directory
1948 (abbreviate-file-name
1949 (substitute-in-file-name
1950 (expand-file-name
1951 (let ((dir (file-name-directory (TeX-master-file))))
1952 (if dir (directory-file-name dir) "."))
1953 (and buffer-file-name
1954 (file-name-directory buffer-file-name)))))))
1955
1956 (defun TeX-add-local-master ()
1957 "Add local variable for `TeX-master'."
1958 (when (and (buffer-file-name)
1959 (string-match TeX-one-master
1960 (file-name-nondirectory (buffer-file-name)))
1961 (not buffer-read-only))
1962 (goto-char (point-max))
1963 (if (re-search-backward (concat "^\\([^\n]+\\)Local " "Variables:")
1964 (- (point-max) 3000) t)
1965 (let ((prefix (TeX-match-buffer 1)))
1966 (re-search-forward (regexp-quote (concat prefix
1967 "End:")))
1968 (beginning-of-line 1)
1969 (insert prefix "TeX-master: " (prin1-to-string TeX-master) "\n"))
1970 (let ((comment-prefix (cond ((eq major-mode 'texinfo-mode) "@c ")
1971 ((eq major-mode 'doctex-mode) "% ")
1972 (t "%%% ")))
1973 (mode (concat (and (boundp 'japanese-TeX-mode) japanese-TeX-mode
1974 "japanese-")
1975 (substring (symbol-name major-mode) 0 -5))))
1976 (newline)
1977 (when (eq major-mode 'doctex-mode)
1978 (insert comment-prefix TeX-esc "endinput\n"))
1979 (insert
1980 comment-prefix "Local " "Variables: \n"
1981 comment-prefix "mode: " mode "\n"
1982 comment-prefix "TeX-master: " (prin1-to-string TeX-master) "\n"
1983 comment-prefix "End: \n")))))
1984
1985 (defun TeX-local-master-p ()
1986 "Return non-nil if there is a `TeX-master' entry in local variables spec.
1987 Return nil otherwise."
1988 (save-excursion
1989 ;; XXX: Checking -*- line necessary as well?
1990 (goto-char (point-max))
1991 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
1992 (re-search-forward "^%+ *TeX-master:" nil t)))
1993
1994 ;;; Style Paths
1995
1996 (defcustom TeX-style-global (expand-file-name "style" TeX-data-directory)
1997 "*Directory containing hand generated TeX information.
1998
1999 These correspond to TeX macros shared by all users of a site."
2000 :group 'TeX-file
2001 :type 'directory)
2002
2003 (defcustom TeX-auto-local "auto"
2004 "*Directory containing automatically generated TeX information.
2005
2006 This correspond to TeX macros found in the current directory, and must
2007 be relative to that."
2008 :group 'TeX-file
2009 :type 'string)
2010
2011 (defcustom TeX-style-local "style"
2012 "*Directory containing hand generated TeX information.
2013
2014 These correspond to TeX macros found in the current directory, and must
2015 be relative to that."
2016 :group 'TeX-file
2017 :type 'string)
2018
2019 (defun TeX-split-string (regexp string)
2020 "Return a list of strings.
2021 Given REGEXP the STRING is split into sections which in string was
2022 seperated by REGEXP.
2023
2024 Examples:
2025
2026 (TeX-split-string \"\:\" \"abc:def:ghi\")
2027 -> (\"abc\" \"def\" \"ghi\")
2028
2029 (TeX-split-string \" +\" \"dvips -Plw -p3 -c4 testfile.dvi\")
2030
2031 -> (\"dvips\" \"-Plw\" \"-p3\" \"-c4\" \"testfile.dvi\")
2032
2033 If REGEXP is nil, or \"\", an error will occur."
2034
2035 (let ((start 0) result match)
2036 (while (setq match (string-match regexp string start))
2037 (push (substring string start match) result)
2038 (setq start (match-end 0)))
2039 (push (substring string start) result)
2040 (nreverse result)))
2041
2042 (defun TeX-parse-path (env)
2043 "Return a list if private TeX directories found in environment variable ENV."
2044 (let* ((value (getenv env))
2045 (entries (and value
2046 (TeX-split-string
2047 (if (string-match ";" value) ";" ":")
2048 value)))
2049 entry
2050 answers)
2051 (while entries
2052 (setq entry (car entries))
2053 (setq entries (cdr entries))
2054 (setq entry (file-name-as-directory
2055 (if (string-match "/?/?\\'" entry)
2056 (substring entry 0 (match-beginning 0))
2057 entry)))
2058 (or (not (file-name-absolute-p entry))
2059 (member entry (append '("/" "\\") TeX-macro-global))
2060 (setq answers (cons entry answers))))
2061 answers))
2062
2063 (defun TeX-macro-global ()
2064 "Return directories containing the site's TeX macro and style files."
2065 (or (TeX-tree-expand '("$SYSTEXMF" "$TEXMFLOCAL" "$TEXMFMAIN" "$TEXMFDIST")
2066 "latex" '("/tex/" "/bibtex/bst/"))
2067 '("/usr/share/texmf/tex/" "/usr/share/texmf/bibtex/bst/")))
2068
2069 (defun TeX-macro-private ()
2070 "Return directories containing the user's TeX macro and style files."
2071 (TeX-tree-expand '("$TEXMFHOME") "latex" '("/tex/" "/bibtex/bst/")))
2072
2073 (defun TeX-tree-expand (trees program subdirs)
2074 "Return directories corresponding to the TeX trees TREES.
2075 This is done calling `kpsewhich' where PROGRAM is passed as the
2076 parameter for --progname. SUBDIRS are subdirectories which are
2077 appended to the directories of the TeX trees."
2078 (let (path-list path exit-status input-dir-list)
2079 (condition-case nil
2080 (catch 'success
2081 (dotimes (i (safe-length trees))
2082 (setq path (with-output-to-string
2083 (setq exit-status
2084 (call-process
2085 "kpsewhich" nil
2086 (list standard-output nil) nil
2087 "--progname" program
2088 "--expand-braces" (nth i trees)))))
2089 (if (zerop exit-status)
2090 (progn (add-to-list 'path-list path)
2091 (when (zerop i) (throw 'success nil)))
2092 (setq path (with-output-to-string
2093 (setq exit-status
2094 (call-process
2095 "kpsewhich" nil
2096 (list standard-output nil) nil
2097 "--progname" program
2098 "--expand-path" (nth i trees)))))
2099 (when (zerop exit-status) (add-to-list 'path-list path)))))
2100 (error nil))
2101 (dolist (elt path-list)
2102 (let ((separators (if (string-match "^[A-Za-z]:" elt)
2103 "[\n\r;]"
2104 "[\n\r:]")))
2105 (dolist (item (condition-case nil
2106 (split-string elt separators t)
2107 ;; COMPATIBILITY for XEmacs <= 21.4.15
2108 (error (delete "" (split-string elt separators)))))
2109 (when (string-match "^!+" item)
2110 (setq item (substring item (match-end 0) (length item))))
2111 (when (string-match "/+$" item)
2112 (setq item (substring item 0 (match-beginning 0))))
2113 (dolist (subdir subdirs)
2114 (when (file-exists-p (file-name-as-directory (concat item subdir)))
2115 (add-to-list 'input-dir-list (concat item subdir)))))))
2116 input-dir-list))
2117
2118 (defcustom TeX-macro-global (TeX-macro-global)
2119 "Directories containing the site's TeX macro and style files."
2120 :group 'TeX-file
2121 :type '(repeat (directory :format "%v")))
2122
2123 (defcustom TeX-macro-private (or (append (TeX-parse-path "TEXINPUTS")
2124 (TeX-parse-path "BIBINPUTS"))
2125 (TeX-macro-private))
2126 "Directories where you store your personal TeX macros."
2127 :group 'TeX-file
2128 :type '(repeat (file :format "%v")))
2129
2130 (defcustom TeX-auto-private
2131 (list (expand-file-name TeX-auto-local
2132 (or (and (boundp 'user-emacs-directory)
2133 (concat user-emacs-directory "auctex/"))
2134 "~/.emacs.d/auctex/")))
2135 "List of directories containing automatically generated AUCTeX style files.
2136
2137 These correspond to the personal TeX macros."
2138 :group 'TeX-file
2139 :type '(repeat (file :format "%v")))
2140
2141 (if (stringp TeX-auto-private) ;Backward compatibility
2142 (setq TeX-auto-private (list TeX-auto-private)))
2143
2144 (defcustom TeX-style-private
2145 (list (expand-file-name TeX-style-local
2146 (or (and (boundp 'user-emacs-directory)
2147 (concat user-emacs-directory "auctex/"))
2148 "~/.emacs.d/auctex/")))
2149 "List of directories containing hand-generated AUCTeX style files.
2150
2151 These correspond to the personal TeX macros."
2152 :group 'TeX-file
2153 :type '(repeat (file :format "%v")))
2154
2155 (if (stringp TeX-style-private) ;Backward compatibility
2156 (setq TeX-style-private (list TeX-style-private)))
2157
2158 (defcustom TeX-style-path
2159 (let ((path))
2160 ;; Put directories in an order where the more local files can
2161 ;; override the more global ones.
2162 (mapcar (lambda (file) (when file (add-to-list 'path file t)))
2163 (append (list TeX-auto-global TeX-style-global)
2164 TeX-auto-private TeX-style-private
2165 (list TeX-auto-local TeX-style-local)))
2166 path)
2167 "List of directories to search for AUCTeX style files.
2168 Per default the list is built from the values of the variables
2169 `TeX-auto-global', `TeX-style-global', `TeX-auto-private',
2170 `TeX-style-private', `TeX-auto-local', and `TeX-style-local'."
2171 :group 'TeX-file
2172 :type '(repeat (file :format "%v")))
2173
2174 (defcustom TeX-check-path
2175 (append (list ".") TeX-macro-private TeX-macro-global)
2176 "Directory path to search for dependencies.
2177
2178 If nil, just check the current file.
2179 Used when checking if any files have changed."
2180 :group 'TeX-file
2181 :type '(repeat (file :format "%v")))
2182
2183 ;;; Style Files
2184
2185 (defvar TeX-style-hook-list nil
2186 "List of TeX style hooks currently loaded.
2187
2188 Each entry is a list where the first element is the name of the style,
2189 and the remaining elements are hooks to be run when that style is
2190 active.")
2191
2192 (defcustom TeX-byte-compile nil
2193 "*Not nil means try to byte compile auto files before loading."
2194 :group 'TeX-parse
2195 :type 'boolean)
2196
2197 (defun TeX-load-style (style)
2198 "Search for and load each definition for STYLE in `TeX-style-path'."
2199 (cond ((assoc style TeX-style-hook-list)) ; We already found it
2200 ((string-match "\\`\\(.+[/\\]\\)\\([^/\\]*\\)\\'" style) ;Complex path
2201 (let* ((dir (substring style (match-beginning 1) (match-end 1)))
2202 (style (substring style (match-beginning 2) (match-end 2)))
2203 (master-dir (if (stringp TeX-master)
2204 (file-name-directory
2205 (file-relative-name TeX-master))
2206 "./"))
2207 (TeX-style-path (append (list (expand-file-name
2208 TeX-auto-local dir)
2209 (expand-file-name
2210 TeX-auto-local master-dir)
2211 (expand-file-name
2212 TeX-style-local dir)
2213 (expand-file-name
2214 TeX-style-local master-dir))
2215 TeX-style-path)))
2216 (TeX-load-style style)))
2217 (t ;Relative path
2218 ;; Insert empty list to mark the fact that we have searched.
2219 (setq TeX-style-hook-list (cons (list style) TeX-style-hook-list))
2220 ;; Now check each element of the path
2221 (dolist (name TeX-style-path)
2222 (TeX-load-style-file (expand-file-name style name))))))
2223
2224 (defun TeX-load-style-file (file)
2225 "Load FILE checking for a Lisp extensions."
2226 (let ((el (concat file ".el"))
2227 (elc (concat file ".elc")))
2228 (cond ((file-newer-than-file-p el elc)
2229 (if (file-readable-p el)
2230 (if (and TeX-byte-compile
2231 (file-writable-p elc)
2232 (save-excursion
2233 ;; `byte-compile-file' switches buffer in Emacs 20.3.
2234 (byte-compile-file el))
2235 (file-readable-p elc))
2236 (load-file elc)
2237 (load-file el))))
2238 ((file-readable-p elc)
2239 (load-file elc))
2240 ((file-readable-p el)
2241 (load-file el)))))
2242
2243 (defun TeX-add-style-hook (style hook)
2244 "Give STYLE yet another HOOK to run."
2245 (let ((entry (assoc style TeX-style-hook-list)))
2246 (cond ((null entry)
2247 ;; New style, add entry.
2248 (setq TeX-style-hook-list (cons (list style hook)
2249 TeX-style-hook-list)))
2250 ((member hook entry)
2251 ;; Old style, hook already there, do nothing.
2252 nil)
2253 (t
2254 ;; Old style, new hook.
2255 (setcdr entry (cons hook (cdr entry)))))))
2256
2257 (defun TeX-unload-style (style)
2258 "Forget that we once loaded STYLE."
2259 (cond ((null (assoc style TeX-style-hook-list)))
2260 ((equal (car (car TeX-style-hook-list)) style)
2261 (setq TeX-style-hook-list (cdr TeX-style-hook-list)))
2262 (t
2263 (let ((entry TeX-style-hook-list))
2264 (while (not (equal (car (car (cdr entry))) style))
2265 (setq entry (cdr entry)))
2266 (setcdr entry (cdr (cdr entry)))))))
2267
2268 (defcustom TeX-virgin-style (if (and TeX-auto-global
2269 (file-directory-p TeX-auto-global))
2270 "virtex"
2271 "NoVirtexSymbols")
2272 "Style all documents use."
2273 :group 'TeX-parse
2274 :type 'string)
2275
2276 (defvar TeX-active-styles nil
2277 "List of styles currently active in the document.")
2278 (make-variable-buffer-local 'TeX-active-styles)
2279
2280 (defun TeX-run-style-hooks (&rest styles)
2281 "Run the TeX style hooks STYLES."
2282 (mapcar (lambda (style)
2283 ;; Avoid recursion.
2284 (unless (TeX-member style TeX-active-styles 'string-equal)
2285 (setq TeX-active-styles
2286 (cons style TeX-active-styles))
2287 (TeX-load-style style)
2288 (let ((default-directory default-directory))
2289 ;; Complex path.
2290 (when (string-match "\\`\\(.+[/\\]\\)\\([^/\\]*\\)\\'" style)
2291 ;; Set `default-directory' to directory of master
2292 ;; file since style files not stored in the fixed
2293 ;; style directories are usually located there.
2294 (setq default-directory (save-match-data
2295 (TeX-master-directory))
2296 style (substring style
2297 (match-beginning 2) (match-end 2))))
2298 (mapcar 'funcall
2299 (cdr-safe (assoc style TeX-style-hook-list))))))
2300 styles))
2301
2302 (defcustom TeX-parse-self nil
2303 "Parse file after loading it if no style hook is found for it."
2304 :group 'TeX-parse
2305 :type 'boolean)
2306
2307 (defvar TeX-style-hook-applied-p nil
2308 "Nil, unless the style specific hooks have been applied.")
2309 (make-variable-buffer-local 'TeX-style-hook-applied-p)
2310
2311 (defvar TeX-update-style-hook nil
2312 "Hook run as soon as style specific hooks were applied.")
2313
2314 (defun TeX-update-style (&optional force)
2315 "Run style specific hooks for the current document.
2316
2317 Only do this if it has not been done before, or if optional argument
2318 FORCE is not nil."
2319 (unless (or (and (boundp 'TeX-auto-update)
2320 (eq TeX-auto-update 'BibTeX)) ; Not a real TeX buffer
2321 (and (not force)
2322 TeX-style-hook-applied-p))
2323 (setq TeX-style-hook-applied-p t)
2324 (message "Applying style hooks...")
2325 (TeX-run-style-hooks (TeX-strip-extension nil nil t))
2326 ;; Run parent style hooks if it has a single parent that isn't itself.
2327 (if (or (not (memq TeX-master '(nil t)))
2328 (and (buffer-file-name)
2329 (string-match TeX-one-master
2330 (file-name-nondirectory (buffer-file-name)))))
2331 (TeX-run-style-hooks (TeX-master-file)))
2332 (if (and TeX-parse-self
2333 (null (cdr-safe (assoc (TeX-strip-extension nil nil t)
2334 TeX-style-hook-list))))
2335 (TeX-auto-apply))
2336 (run-hooks 'TeX-update-style-hook)
2337 (message "Applying style hooks... done")))
2338
2339 (defvar TeX-remove-style-hook nil
2340 "List of hooks to call when we remove the style specific information.")
2341 (make-variable-buffer-local 'TeX-remove-style-hook)
2342
2343 (defun TeX-remove-style ()
2344 "Remove all style specific information."
2345 (setq TeX-style-hook-applied-p nil)
2346 (run-hooks 'TeX-remove-style-hook)
2347 (setq TeX-active-styles (list TeX-virgin-style)))
2348
2349 (defun TeX-style-list ()
2350 "Return a list of all styles (subfiles) used by the current document."
2351 (TeX-update-style)
2352 TeX-active-styles)
2353
2354 ;;; Special Characters
2355
2356 (defvar TeX-esc "\\" "The TeX escape character.")
2357 (make-variable-buffer-local 'TeX-esc)
2358
2359 (defvar TeX-grop "{" "The TeX group opening character.")
2360 (make-variable-buffer-local 'TeX-grop)
2361
2362 (defvar TeX-grcl "}" "The TeX group closing character.")
2363 (make-variable-buffer-local 'TeX-grcl)
2364
2365 (defcustom plain-TeX-enable-toolbar t
2366 "Enable TeX tool bar in plain TeX mode."
2367 :group 'TeX-tool-bar
2368 :type 'boolean)
2369
2370 (defun plain-TeX-maybe-install-toolbar ()
2371 "Conditionally install tool bar buttons for plain TeX mode.
2372 Install tool bar if `plain-TeX-enable-toolbar' is non-nil."
2373 (when plain-TeX-enable-toolbar
2374 ;; Defined in `tex-bar.el':
2375 (TeX-install-toolbar)))
2376
2377 ;;; Symbols
2378
2379 ;; Must be before keymaps.
2380
2381 (defgroup TeX-macro nil
2382 "Support for TeX macros in AUCTeX."
2383 :prefix "TeX-"
2384 :group 'AUCTeX)
2385
2386 (defcustom TeX-complete-word 'ispell-complete-word
2387 "*Function to call for completing non-macros in `tex-mode'."
2388 :group 'TeX-macro)
2389
2390 (defvar TeX-complete-list nil
2391 "List of ways to complete the preceding text.
2392
2393 Each entry is a list with the following elements:
2394
2395 0. Regexp matching the preceding text.
2396 1. A number indicating the subgroup in the regexp containing the text.
2397 2. A function returning an alist of possible completions.
2398 3. Text to append after a succesful completion.
2399
2400 Or alternatively:
2401
2402 0. Regexp matching the preceding text.
2403 1. Function to do the actual completion.")
2404
2405 (defun TeX-complete-symbol ()
2406 "Perform completion on TeX/LaTeX symbol preceding point."
2407 (interactive "*")
2408 (let ((list TeX-complete-list)
2409 entry)
2410 (while list
2411 (setq entry (car list)
2412 list (cdr list))
2413 (if (TeX-looking-at-backward (car entry) 250)
2414 (setq list nil)))
2415 (if (numberp (nth 1 entry))
2416 (let* ((sub (nth 1 entry))
2417 (close (nth 3 entry))
2418 (begin (match-beginning sub))
2419 (end (match-end sub))
2420 (pattern (TeX-match-buffer 0))
2421 (symbol (buffer-substring begin end))
2422 (list (funcall (nth 2 entry)))
2423 (completion (try-completion symbol list)))
2424 (cond ((eq completion t)
2425 (and close
2426 (not (looking-at (regexp-quote close)))
2427 (insert close)))
2428 ((null completion)
2429 (error "Can't find completion for \"%s\"" pattern))
2430 ((not (string-equal symbol completion))
2431 (delete-region begin end)
2432 (insert completion)
2433 (and close
2434 (eq (try-completion completion list) t)
2435 (not (looking-at (regexp-quote close)))
2436 (insert close)))
2437 (t
2438 (message "Making completion list...")
2439 (let ((list (all-completions symbol list nil)))
2440 (with-output-to-temp-buffer "*Completions*"
2441 (display-completion-list list)))
2442 (message "Making completion list...done"))))
2443 (funcall (nth 1 entry)))))
2444
2445 (defcustom TeX-default-macro "ref"
2446 "*The default macro when creating new ones with `TeX-insert-macro'."
2447 :group 'TeX-macro
2448 :type 'string)
2449
2450 (make-variable-buffer-local 'TeX-default-macro)
2451
2452 (defcustom TeX-insert-braces t
2453 "*If non-nil, append a empty pair of braces after inserting a macro."
2454 :group 'TeX-macro
2455 :type 'boolean)
2456
2457 (defcustom TeX-insert-macro-default-style 'show-optional-args
2458 "Specifies whether `TeX-insert-macro' will ask for all optional arguments.
2459
2460 If set to the symbol `show-optional-args', `TeX-insert-macro' asks for
2461 optional arguments of TeX marcos. If set to `mandatory-args-only',
2462 `TeX-insert-macro' asks only for mandatory argument.
2463
2464 When `TeX-insert-macro' is called with \\[universal-argument], it's the other
2465 way round.
2466
2467 Note that for some macros, there are special mechanisms, see e.g.
2468 `LaTeX-includegraphics-options-alist'."
2469 :group 'TeX-macro
2470 :type '(choice (const mandatory-args-only)
2471 (const show-optional-args)))
2472
2473 (defvar TeX-arg-opening-brace nil
2474 "String used as an opening brace for argument insertion.
2475 The variable will be temporarily let-bound with the necessary value.")
2476
2477 (defvar TeX-arg-closing-brace nil
2478 "String used as a closing brace for argument insertion.
2479 The variable will be temporarily let-bound with the necessary value.")
2480
2481 (defvar TeX-after-insert-macro-hook nil
2482 "A hook run after `TeX-insert-macro'.")
2483
2484 (defvar TeX-macro-history nil)
2485
2486 (defun TeX-insert-macro (symbol)
2487 "Insert TeX macro SYMBOL with completion.
2488
2489 AUCTeX knows of some macros and may query for extra arguments, depending on
2490 the value of `TeX-insert-macro-default-style' and whether `TeX-insert-macro'
2491 is called with \\[universal-argument]."
2492 ;; When called with a prefix (C-u), only ask for mandatory arguments,
2493 ;; i.e. all optional arguments are skipped. See `TeX-parse-arguments' for
2494 ;; details. Note that this behavior may be changed in favor of a more
2495 ;; flexible solution in the future, therefore we don't document it at the
2496 ;; moment.
2497 (interactive (list (completing-read (concat "Macro (default "
2498 TeX-default-macro
2499 "): "
2500 TeX-esc)
2501 (TeX-symbol-list) nil nil nil
2502 'TeX-macro-history)))
2503 (cond ((string-equal symbol "")
2504 (setq symbol TeX-default-macro))
2505 ((interactive-p)
2506 (setq TeX-default-macro symbol)))
2507 (TeX-parse-macro symbol (cdr-safe (assoc symbol (TeX-symbol-list))))
2508 (run-hooks 'TeX-after-insert-macro-hook))
2509
2510 (defvar TeX-electric-macro-map
2511 (let ((map (make-sparse-keymap)))
2512 (set-keymap-parent map minibuffer-local-completion-map)
2513 (define-key map " " 'minibuffer-complete-and-exit)
2514 map))
2515
2516 (defun TeX-electric-macro ()
2517 "Insert TeX macro with completion.
2518
2519 AUCTeX knows of some macros, and may query for extra arguments.
2520 Space will complete and exit."
2521 (interactive)
2522 (cond ((eq (preceding-char) ?\\)
2523 (call-interactively 'self-insert-command))
2524 ((eq (preceding-char) ?.)
2525 (let ((TeX-default-macro " ")
2526 (minibuffer-local-completion-map TeX-electric-macro-map))
2527 (call-interactively 'TeX-insert-macro)))
2528 (t
2529 (let ((minibuffer-local-completion-map TeX-electric-macro-map))
2530 (call-interactively 'TeX-insert-macro)))))
2531
2532 (defun TeX-parse-macro (symbol args)
2533 "How to parse TeX macros which takes one or more arguments.
2534
2535 First argument SYMBOL is the name of the macro.
2536
2537 If called with no additional arguments, insert macro with point
2538 inside braces. Otherwise, each argument of this function should
2539 match an argument to the TeX macro. What is done depend on the
2540 type of ARGS:
2541
2542 string: Use the string as a prompt to prompt for the argument.
2543
2544 number: Insert that many braces, leave point inside the first.
2545
2546 nil: Insert empty braces.
2547
2548 t: Insert empty braces, leave point between the braces.
2549
2550 other symbols: Call the symbol as a function. You can define
2551 your own hook, or use one of the predefined argument hooks. If
2552 you add new hooks, you can assume that point is placed directly
2553 after the previous argument, or after the macro name if this is
2554 the first argument. Please leave point located after the
2555 argument you are inserting. If you want point to be located
2556 somewhere else after all hooks have been processed, set the value
2557 of `exit-mark'. It will point nowhere, until the argument hook
2558 set it. By convention, these hooks all start with `TeX-arg-'.
2559
2560 list: If the car is a string, insert it as a prompt and the next
2561 element as initial input. Otherwise, call the car of the list
2562 with the remaining elements as arguments.
2563
2564 vector: Optional argument. If it has more than one element,
2565 parse it as a list, otherwise parse the only element as above.
2566 Use square brackets instead of curly braces, and is not inserted
2567 on empty user input."
2568
2569 (if (and (TeX-active-mark)
2570 (> (point) (mark)))
2571 (exchange-point-and-mark))
2572 (insert TeX-esc symbol)
2573 (let ((exit-mark (make-marker))
2574 (position (point)))
2575 (TeX-parse-arguments args)
2576 (cond ((marker-position exit-mark)
2577 (goto-char (marker-position exit-mark))
2578 (set-marker exit-mark nil))
2579 ((and TeX-insert-braces
2580 ;; Do not add braces for macros defined as `("foo" 0)'
2581 (not (and (= (safe-length args) 1)
2582 (numberp (car args))
2583 (= (car args) 0)))
2584 (equal position (point))
2585 (string-match "[a-zA-Z]+" symbol)
2586 (not (texmathp)))
2587 (insert TeX-grop)
2588 (if (TeX-active-mark)
2589 (progn
2590 (exchange-point-and-mark)
2591 (insert TeX-grcl))
2592 (insert TeX-grcl)
2593 (backward-char))))))
2594
2595 (defun TeX-arg-string (optional &optional prompt initial-input)
2596 "Prompt for a string.
2597
2598 If OPTIONAL is not nil then the PROMPT will start with ``(Optional) ''.
2599 INITIAL-INPUT is a string to insert before reading input."
2600 (TeX-argument-insert
2601 (if (and (not optional) (TeX-active-mark))
2602 (let ((TeX-argument (buffer-substring (point) (mark))))
2603 (delete-region (point) (mark))
2604 TeX-argument)
2605 (read-string (TeX-argument-prompt optional prompt "Text") initial-input))
2606 optional))
2607
2608 (defun TeX-parse-arguments (args)
2609 "Parse TeX macro arguments ARGS.
2610
2611 See `TeX-parse-macro' for details."
2612 (let ((last-optional-rejected nil)
2613 skip-opt)
2614 ;; Maybe get rid of all optional arguments. See `TeX-insert-macro' for
2615 ;; more comments. See `TeX-insert-macro-default-style'.
2616 (when (or (and (eq TeX-insert-macro-default-style 'show-optional-args)
2617 (equal current-prefix-arg '(4)))
2618 (and (eq TeX-insert-macro-default-style 'mandatory-args-only)
2619 (null (equal current-prefix-arg '(4)))))
2620 (while (vectorp (car args))
2621 (setq args (cdr args))))
2622
2623 (while args
2624 (if (vectorp (car args))
2625 (unless last-optional-rejected
2626 (let ((TeX-arg-opening-brace LaTeX-optop)
2627 (TeX-arg-closing-brace LaTeX-optcl))
2628 (TeX-parse-argument t (if (equal (length (car args)) 1)
2629 (aref (car args) 0)
2630 (append (car args) nil)))))
2631 (let ((TeX-arg-opening-brace TeX-grop)
2632 (TeX-arg-closing-brace TeX-grcl))
2633 (setq last-optional-rejected nil)
2634 (TeX-parse-argument nil (car args))))
2635 (setq args (cdr args)))))
2636
2637 (defun TeX-parse-argument (optional arg)
2638 "Depending on OPTIONAL, insert TeX macro argument ARG.
2639 If OPTIONAL is set, only insert if there is anything to insert, and
2640 then use square brackets instead of curly braces.
2641
2642 See `TeX-parse-macro' for details."
2643 (let (insert-flag)
2644 (cond ((stringp arg)
2645 (TeX-arg-string optional arg)
2646 (setq insert-flag t))
2647 ((numberp arg)
2648 (unless (< arg 1)
2649 (TeX-parse-argument optional t)
2650 (while (> arg 1)
2651 (TeX-parse-argument optional nil)
2652 (setq arg (- arg 1)))))
2653 ((null arg)
2654 (insert TeX-arg-opening-brace)
2655 (when (and (not optional) (TeX-active-mark))
2656 (exchange-point-and-mark))
2657 (insert TeX-arg-closing-brace)
2658 (setq insert-flag t))
2659 ((eq arg t)
2660 (insert TeX-arg-opening-brace)
2661 (if (and (not optional) (TeX-active-mark))
2662 (progn
2663 (exchange-point-and-mark))
2664 (set-marker exit-mark (point)))
2665 (insert TeX-arg-closing-brace)
2666 (setq insert-flag t))
2667 ((symbolp arg)
2668 (funcall arg optional))
2669 ((listp arg)
2670 (let ((head (car arg))
2671 (tail (cdr arg)))
2672 (cond ((stringp head)
2673 (apply 'TeX-arg-string optional arg))
2674 ((symbolp head)
2675 (apply head optional tail))
2676 (t (error "Unknown list argument type %s"
2677 (prin1-to-string head))))))
2678 (t (error "Unknown argument type %s" (prin1-to-string arg))))
2679 (when (and insert-flag (not optional) (TeX-active-mark))
2680 (TeX-deactivate-mark))))
2681
2682 (defun TeX-argument-insert (name optional &optional prefix)
2683 "Insert NAME surrounded by curly braces.
2684
2685 If OPTIONAL, only insert it if not empty, and then use square brackets.
2686 If PREFIX is given, insert it before NAME."
2687 (if (and optional (string-equal name ""))
2688 (setq last-optional-rejected t)
2689 (insert TeX-arg-opening-brace)
2690 (if prefix
2691 (insert prefix))
2692 (if (and (string-equal name "")
2693 (null (marker-position exit-mark)))
2694 (set-marker exit-mark (point))
2695 (insert name))
2696 (insert TeX-arg-closing-brace)))
2697
2698 (defun TeX-argument-prompt (optional prompt default &optional complete)
2699 "Return a argument prompt.
2700
2701 If OPTIONAL is not nil then the prompt will start with ``(Optional) ''.
2702
2703 PROMPT will be used if not nil, otherwise use DEFAULT.
2704
2705 Unless optional argument COMPLETE is non-nil, ``: '' will be appended."
2706 (concat (if optional "(Optional) " "")
2707 (if prompt prompt default)
2708 (if complete "" ": ")))
2709
2710 (defun TeX-string-divide-number-unit (string)
2711 "Divide number and unit in STRING.
2712 Return the number as car and unit as cdr."
2713 (if (string-match "[0-9]*\\.?[0-9]+" string)
2714 (list (substring string 0 (string-match "[^.0-9]" string))
2715 (substring string (if (string-match "[^.0-9]" string)
2716 (string-match "[^.0-9]" string)
2717 (length string))))
2718 (list "" string)))
2719
2720 (defcustom TeX-default-unit-for-image "cm"
2721 "Default unit when prompting for an image size."
2722 :group 'TeX-macro
2723 :type '(choice (const "cm")
2724 (const "in")
2725 (const "\\linewidth")
2726 (string :tag "Other")))
2727
2728 (defun TeX-arg-maybe (symbol list form)
2729 "Evaluates FORM, if SYMBOL is an element of LIST."
2730 (when (memq symbol list)
2731 (eval form)))
2732
2733 (defun TeX-arg-free (optional &rest args)
2734 "Parse its arguments but use no braces when they are inserted."
2735 (let ((TeX-arg-opening-brace "")
2736 (TeX-arg-closing-brace ""))
2737 (if (equal (length args) 1)
2738 (TeX-parse-argument optional (car args))
2739 (TeX-parse-argument optional args))))
2740
2741 (defun TeX-arg-literal (optional &rest args)
2742 "Insert its arguments ARGS into the buffer.
2743 Used for specifying extra syntax for a macro."
2744 ;; FIXME: What is the purpose of OPTIONAL here? -- rs
2745 (apply 'insert args))
2746
2747
2748 ;;; Font Locking
2749
2750 (defcustom TeX-install-font-lock 'font-latex-setup
2751 "Function to call to install font lock support.
2752 Choose `ignore' if you don't want AUCTeX to install support for font locking."
2753 :group 'TeX-misc
2754 :type '(radio (function-item font-latex-setup)
2755 (function-item tex-font-setup)
2756 (function-item ignore)
2757 (function :tag "Other")))
2758
2759 ;;; The Mode
2760
2761 (defvar TeX-format-list
2762 '(("JLATEX" japanese-latex-mode
2763 "\\\\\\(documentstyle\\|documentclass\\)[^%\n]*{\\(j[s-]?\\|t\\)\
2764 \\(article\\|report\\|book\\|slides\\)")
2765 ("JTEX" japanese-plain-tex-mode
2766 "-- string likely in Japanese TeX --")
2767 ("AMSTEX" ams-tex-mode
2768 "\\\\document\\b")
2769 ("CONTEXT" context-mode
2770 "\\\\\\(start\\(text\\|tekst\\|proje[ck]t\\|proiect\\|\
2771 produ[ck]t\\|produs\\|environment\\|omgeving\\|umgebung\\|prostredi\\|mediu\\|\
2772 component\\|onderdeel\\|komponent[ea]\\|componenta\\)\
2773 \\|inizia\\(testo\\|progetto\\|prodotto\\|ambiente\\|componente\\)\
2774 \\)\\|%.*?interface=")
2775 ("LATEX" latex-mode
2776 "\\\\\\(begin\\|\\(?:sub\\)\\{0,2\\}section\\|chapter\\|documentstyle\\|\
2777 documentclass\\)\\b")
2778 ("TEX" plain-tex-mode "."))
2779 "*List of format packages to consider when choosing a TeX mode.
2780
2781 A list with an entry for each format package available at the site.
2782
2783 Each entry is a list with three elements.
2784
2785 1. The name of the format package.
2786 2. The name of the major mode.
2787 3. A regexp typically matched in the beginning of the file.
2788
2789 When entering `tex-mode', each regexp is tried in turn in order to find
2790 the major mode to be used.")
2791
2792 (defcustom TeX-default-mode 'latex-mode
2793 "*Mode to enter for a new file when it can't be determined otherwise."
2794 :group 'TeX-misc
2795 :type '(radio (function-item latex-mode)
2796 (function-item plain-tex-mode)
2797 (function :tag "Other")))
2798
2799 (defcustom TeX-force-default-mode nil
2800 "*If set to nil, try to infer the mode of the file from its content."
2801 :group 'TeX-misc
2802 :type 'boolean)
2803
2804 ;;;###autoload
2805 (defun TeX-tex-mode ()
2806 "Major mode in AUCTeX for editing TeX or LaTeX files.
2807 Tries to guess whether this file is for plain TeX or LaTeX.
2808
2809 The algorithm is as follows:
2810
2811 1) if the file is empty or `TeX-force-default-mode' is not set to nil,
2812 `TeX-default-mode' is chosen
2813 2) If \\documentstyle or \\begin{, \\section{, \\part{ or \\chapter{ is
2814 found, `latex-mode' is selected.
2815 3) Otherwise, use `plain-tex-mode'"
2816 (interactive)
2817
2818 (funcall (if (or (equal (buffer-size) 0)
2819 TeX-force-default-mode)
2820 TeX-default-mode
2821 (save-excursion
2822 (goto-char (point-min))
2823 (let ((comment-start-skip ;Used by TeX-in-comment
2824 (concat
2825 "\\(\\(^\\|[^\\\n]\\)\\("
2826 (regexp-quote TeX-esc)
2827 (regexp-quote TeX-esc)
2828 "\\)*\\)\\(%+ *\\)"))
2829 (entry TeX-format-list)
2830 answer)
2831 (while (and entry (not answer))
2832 (if (re-search-forward (nth 2 (car entry))
2833 10000 t)
2834 (if (not (TeX-in-comment))
2835 (setq answer (nth 1 (car entry))))
2836 (setq entry (cdr entry))))
2837 (if answer
2838 answer
2839 TeX-default-mode))))))
2840
2841 (defun VirTeX-common-initialization ()
2842 "Perform basic initialization."
2843 (kill-all-local-variables)
2844 (setq TeX-mode-p t)
2845 (setq TeX-output-extension (if TeX-PDF-mode "pdf" "dvi"))
2846 (setq local-abbrev-table text-mode-abbrev-table)
2847 (setq indent-tabs-mode nil)
2848
2849 ;; Ispell support
2850 (make-local-variable 'ispell-parser)
2851 (setq ispell-parser 'tex)
2852 (make-local-variable 'ispell-tex-p)
2853 (setq ispell-tex-p t)
2854
2855 ;; Redefine some standard variables
2856 (make-local-variable 'paragraph-start)
2857 (make-local-variable 'paragraph-separate)
2858 (make-local-variable 'comment-start)
2859 (setq comment-start "%")
2860 (make-local-variable 'comment-start-skip)
2861 (setq comment-start-skip
2862 (concat
2863 "\\(\\(^\\|[^\\\n]\\)\\("
2864 (regexp-quote TeX-esc)
2865 (regexp-quote TeX-esc)
2866 "\\)*\\)\\(%+[ \t]*\\)"))
2867 (set (make-local-variable 'comment-end-skip) "[ \t]*\\(\\s>\\|\n\\)")
2868 (set (make-local-variable 'comment-use-syntax) t)
2869 ;; `comment-padding' is defined here as an integer for compatibility
2870 ;; reasons because older Emacsen could not cope with a string.
2871 (make-local-variable 'comment-padding)
2872 (setq comment-padding 1)
2873 ;; Removed as commenting in (La)TeX is done with one `%' not two
2874 ;; (make-local-variable 'comment-add)
2875 ;; (setq comment-add 1) ;default to `%%' in comment-region
2876 (make-local-variable 'comment-indent-function)
2877 (setq comment-indent-function 'TeX-comment-indent)
2878 (make-local-variable 'comment-multi-line)
2879 (setq comment-multi-line nil)
2880 (make-local-variable 'compile-command)
2881 (unless (boundp 'compile-command)
2882 (setq compile-command "make"))
2883 (make-local-variable 'words-include-escapes)
2884 (setq words-include-escapes nil)
2885
2886 ;; Make TAB stand out
2887 ;; (make-local-variable 'buffer-display-table)
2888 ;; (setq buffer-display-table (if standard-display-table
2889 ;; (copy-sequence standard-display-table)
2890 ;; (make-display-table)))
2891 ;; (aset buffer-display-table ?\t (apply 'vector (append "<TAB>" nil)))
2892
2893 ;; Symbol completion.
2894 (make-local-variable 'TeX-complete-list)
2895 (setq TeX-complete-list
2896 (list (list "\\\\\\([a-zA-Z]*\\)"
2897 1 'TeX-symbol-list (if TeX-insert-braces "{}"))
2898 (list "" TeX-complete-word)))
2899
2900 (funcall TeX-install-font-lock)
2901
2902 ;; We want this to be early in the list, so we do not add it before
2903 ;; we enter TeX mode the first time.
2904 (if (boundp 'local-write-file-hooks)
2905 (add-hook 'local-write-file-hooks 'TeX-safe-auto-write)
2906 (add-hook 'write-file-hooks 'TeX-safe-auto-write))
2907 (make-local-variable 'TeX-auto-update)
2908 (setq TeX-auto-update t)
2909
2910 ;; Minor modes
2911 (when TeX-source-correlate-mode
2912 (TeX-source-correlate-mode 1))
2913
2914 ;; Let `TeX-master-file' be called after a new file was opened and
2915 ;; call `TeX-update-style' on any file opened. (The addition to the
2916 ;; hook has to be made here because its local value will be deleted
2917 ;; by `kill-all-local-variables' if it is added e.g. in `tex-mode'.)
2918 ;;
2919 ;; `TeX-update-style' has to be called before
2920 ;; `global-font-lock-mode', which may also be specified in
2921 ;; `find-file-hooks', gets called. Otherwise style-based
2922 ;; fontification will break (in XEmacs). That means, `add-hook'
2923 ;; cannot be called with a non-nil value of the APPEND argument.
2924 ;;
2925 ;; `(TeX-master-file nil nil t)' has to be called *before*
2926 ;; `TeX-update-style' as the latter will call `TeX-master-file'
2927 ;; without the `ask' bit set.
2928 (when (and (featurep 'xemacs) (not (emacs-version>= 21 5)))
2929 (make-local-hook 'find-file-hooks))
2930 (add-hook 'find-file-hooks
2931 (lambda ()
2932 ;; Check if we are looking at a new or shared file.
2933 (when (or (not (file-exists-p (buffer-file-name)))
2934 (eq TeX-master 'shared))
2935 (TeX-master-file nil nil t))
2936 (TeX-update-style t)) nil t))
2937
2938 ;;; Plain TeX mode
2939
2940 (defcustom plain-TeX-clean-intermediate-suffixes
2941 TeX-clean-default-intermediate-suffixes
2942 "List of regexps matching suffixes of intermediate files to be deleted.
2943 The regexps will be anchored at the end of the file name to be matched,
2944 i.e. you do _not_ have to cater for this yourself by adding \\\\' or $."
2945 :type '(repeat regexp)
2946 :group 'TeX-command)
2947
2948 (defcustom plain-TeX-clean-output-suffixes TeX-clean-default-output-suffixes
2949 "List of regexps matching suffixes of output files to be deleted.
2950 The regexps will be anchored at the end of the file name to be matched,
2951 i.e. you do _not_ have to cater for this yourself by adding \\\\' or $."
2952 :type '(repeat regexp)
2953 :group 'TeX-command)
2954
2955 (defcustom plain-TeX-mode-hook nil
2956 "A hook run in plain TeX mode buffers."
2957 :type 'hook
2958 :group 'TeX-misc)
2959
2960 ;;;###autoload
2961 (defun TeX-plain-tex-mode ()
2962 "Major mode in AUCTeX for editing plain TeX files.
2963 See info under AUCTeX for documentation.
2964
2965 Special commands:
2966 \\{plain-TeX-mode-map}
2967
2968 Entering `plain-tex-mode' calls the value of `text-mode-hook',
2969 then the value of `TeX-mode-hook', and then the value
2970 of plain-TeX-mode-hook."
2971 (interactive)
2972 (plain-TeX-common-initialization)
2973 (setq major-mode 'plain-tex-mode)
2974 (use-local-map plain-TeX-mode-map)
2975 (easy-menu-add plain-TeX-mode-menu plain-TeX-mode-map)
2976 (easy-menu-add plain-TeX-mode-command-menu plain-TeX-mode-map)
2977 (setq TeX-base-mode-name "TeX")
2978 (setq TeX-command-default "TeX")
2979 (setq TeX-sentinel-default-function 'TeX-TeX-sentinel)
2980 (add-hook 'tool-bar-mode-on-hook 'plain-TeX-maybe-install-toolbar nil t)
2981 (when (if (featurep 'xemacs)
2982 (featurep 'toolbar)
2983 (and (boundp 'tool-bar-mode) tool-bar-mode))
2984 (plain-TeX-maybe-install-toolbar))
2985 (TeX-run-mode-hooks 'text-mode-hook 'TeX-mode-hook 'plain-TeX-mode-hook)
2986 (TeX-set-mode-name))
2987
2988 (defun plain-TeX-common-initialization ()
2989 "Common initialization for plain TeX like modes."
2990 (VirTeX-common-initialization)
2991 (set-syntax-table TeX-mode-syntax-table)
2992 (setq paragraph-start
2993 (concat
2994 "\\(^[ \t]*$"
2995 "\\|" (regexp-quote TeX-esc) "par\\|"
2996 "^[ \t]*"
2997 (regexp-quote TeX-esc)
2998 "\\("
2999 "begin\\|end\\|part\\|chapter\\|"
3000 "section\\|subsection\\|subsubsection\\|"
3001 "paragraph\\|include\\|includeonly\\|"
3002 "tableofcontents\\|appendix\\|label\\|caption\\|"
3003 "\\[\\|\\]" ; display math delimitors
3004 "\\)"
3005 "\\|"
3006 "^[ \t]*\\$\\$" ; display math delimitor
3007 "\\)" ))
3008 (setq paragraph-separate
3009 (concat
3010 "[ \t]*"
3011 "\\("
3012 (regexp-quote TeX-esc) "par\\|"
3013 "%\\|"
3014 "$\\|"
3015 "\\$\\$\\|"
3016 (regexp-quote TeX-esc)
3017 "\\("
3018 "begin\\|end\\|label\\|caption\\|part\\|chapter\\|"
3019 "section\\|subsection\\|subsubsection\\|"
3020 "paragraph\\|include\\|includeonly\\|"
3021 "tableofcontents\\|appendix\\|" (regexp-quote TeX-esc)
3022 "\\)"
3023 "\\)"))
3024 (setq TeX-header-end (regexp-quote "%**end of header"))
3025 (setq TeX-trailer-start (regexp-quote (concat TeX-esc "bye")))
3026 (TeX-run-style-hooks "TEX"))
3027
3028 ;;; Hilighting
3029
3030 (if (boundp 'hilit-patterns-alist)
3031 (let ((latex-patterns (cdr-safe (assq 'latex-mode hilit-patterns-alist)))
3032 (plain-tex-patterns (cdr-safe (assq 'plain-tex-mode
3033 hilit-patterns-alist))))
3034 (if (and latex-patterns plain-tex-patterns)
3035 (setq hilit-patterns-alist
3036 (append (list (cons 'ams-tex-mode plain-tex-patterns))
3037 hilit-patterns-alist)))))
3038
3039 ;;; Parsing
3040
3041 (defgroup TeX-parse nil
3042 "Parsing TeX files from AUCTeX."
3043 :group 'AUCTeX)
3044
3045 (defvar TeX-auto-parser '((styles TeX-auto-file TeX-run-style-hooks)))
3046 ;; Alist of parsed information.
3047 ;; Each entry is a list with the following elements:
3048 ;;
3049 ;; 0. Name of information type.
3050 ;; 1. Name of temporary variable used when parsing.
3051 ;; 2. Name of function to add information to add to #3.
3052 ;; 3. Name of variable holding buffer local information.
3053 ;; 4. Name of variable indicating that #3 has changed.
3054
3055
3056 (defconst TeX-auto-parser-temporary 1)
3057 (defconst TeX-auto-parser-add 2)
3058 (defconst TeX-auto-parser-local 3)
3059 (defconst TeX-auto-parser-change 4)
3060
3061 (defun TeX-auto-add-type (name prefix &optional plural)
3062 "Add information about NAME to the parser using PREFIX.
3063
3064 Optional third argument PLURAL is the plural form of TYPE.
3065 By default just add an `s'.
3066
3067 This function create a set of variables and functions to maintain a
3068 separate type of information in the parser."
3069 (let* ((names (or plural (concat name "s")))
3070 (tmp (intern (concat prefix "-auto-" name)))
3071 (add (intern (concat prefix "-add-" names)))
3072 (local (intern (concat prefix "-" name "-list")))
3073 (change (intern (concat prefix "-" name "-changed"))))
3074 (setq TeX-auto-parser
3075 (cons (list name tmp add local change) TeX-auto-parser))
3076 (set local nil)
3077 (make-variable-buffer-local local)
3078 (set change nil)
3079 (make-variable-buffer-local change)
3080 (fset add `(lambda (&rest entries)
3081 ,(concat "Add information about " (upcase name)
3082 " to the current buffer.
3083 Generated by `TeX-auto-add-type'.")
3084 (TeX-auto-add-information ,name entries)))
3085 (fset local `(lambda nil
3086 ,(concat "List of " names
3087 " active in the current buffer.
3088 Generated by `TeX-auto-add-type'.")
3089 (TeX-auto-list-information ,name)))
3090 (add-hook 'TeX-remove-style-hook
3091 `(lambda nil (setq ,(symbol-name local) nil)))))
3092
3093 (defun TeX-auto-add-information (name entries)
3094 "For NAME in `TeX-auto-parser' add ENTRIES."
3095 (let* ((entry (assoc name TeX-auto-parser))
3096 (change (nth TeX-auto-parser-change entry))
3097 (change-value (symbol-value change))
3098 (local (nth TeX-auto-parser-local entry))
3099 (local-value (symbol-value local)))
3100 (if change-value
3101 (set local (cons entries local-value))
3102 (set change t)
3103 (set local (list entries local-value)))))
3104
3105 (defun TeX-auto-list-information (name)
3106 "Return information in `TeX-auto-parser' about NAME."
3107 (TeX-update-style)
3108 (let* ((entry (assoc name TeX-auto-parser))
3109 (change (nth TeX-auto-parser-change entry))
3110 (change-value (symbol-value change))
3111 (local (nth TeX-auto-parser-local entry)))
3112 (if (not change-value)
3113 ()
3114 (set change nil)
3115 ;; Sort it
3116 (message "Sorting %s..." name)
3117 (set local
3118 (sort (mapcar 'TeX-listify (apply 'append (symbol-value local)))
3119 'TeX-car-string-lessp))
3120 ;; Make it unique
3121 (message "Removing duplicates...")
3122 (let ((entry (symbol-value local)))
3123 (while (and entry (cdr entry))
3124 (let ((this (car entry))
3125 (next (car (cdr entry))))
3126 (if (not (string-equal (car this) (car next)))
3127 (setq entry (cdr entry))
3128 ;; We have two equal symbols. Use the one with
3129 ;; most arguments.
3130 (if (> (length next) (length this))
3131 (setcdr this (cdr next)))
3132 (setcdr entry (cdr (cdr entry)))))))
3133 (message "Removing duplicates... done"))
3134 (symbol-value local)))
3135
3136 (TeX-auto-add-type "symbol" "TeX")
3137
3138 (defvar TeX-auto-apply-hook nil
3139 "Hook run when a buffer is parsed and the information is applied.")
3140
3141 (defun TeX-auto-apply ()
3142 "Parse and apply TeX information in the current buffer."
3143 (TeX-auto-parse)
3144 (run-hooks 'TeX-auto-apply-hook)
3145 (mapcar 'TeX-auto-apply-entry TeX-auto-parser))
3146
3147 (defun TeX-auto-apply-entry (entry)
3148 "Apply the information in ENTRY in `TeX-auto-parser'."
3149 (let ((value (symbol-value (nth TeX-auto-parser-temporary entry)))
3150 (add (nth TeX-auto-parser-add entry)))
3151 (if value (apply add value))))
3152
3153 (defun TeX-safe-auto-write ()
3154 "Call `TeX-auto-write' safely."
3155 (condition-case name
3156 (and (boundp 'TeX-auto-update)
3157 TeX-auto-update
3158 (TeX-auto-write))
3159 (error nil))
3160 ;; Continue with the other write file hooks.
3161 nil)
3162
3163 (defcustom TeX-auto-save nil
3164 "*Automatically save style information when saving the buffer."
3165 :group 'TeX-parse
3166 :type 'boolean)
3167
3168 (defcustom TeX-auto-untabify nil
3169 "*Automatically untabify when saving the buffer."
3170 :group 'TeX-parse
3171 :type 'boolean)
3172
3173 (defun TeX-auto-write ()
3174 "Save all relevant TeX information from the current buffer."
3175 (if TeX-auto-untabify
3176 (untabify (point-min) (point-max)))
3177 (if (and TeX-auto-save TeX-auto-local)
3178 (let* ((file (expand-file-name
3179 (concat
3180 (file-name-as-directory TeX-auto-local)
3181 (TeX-strip-extension nil TeX-all-extensions t)
3182 ".el")
3183 (TeX-master-directory)))
3184 (dir (file-name-directory file)))
3185 ;; Create auto directory if possible.
3186 (if (not (file-exists-p dir))
3187 (condition-case name
3188 (make-directory dir)
3189 (error nil)))
3190 (if (file-writable-p file)
3191 (save-excursion
3192 (TeX-update-style)
3193 (TeX-auto-store file))
3194 (message "Can't write style information.")))))
3195
3196 (defcustom TeX-macro-default (car-safe TeX-macro-private)
3197 "*Default directory to search for TeX macros."
3198 :group 'TeX-file
3199 :type 'directory)
3200
3201 (defcustom TeX-auto-default (car-safe TeX-auto-private)
3202 "*Default directory to place automatically generated TeX information."
3203 :group 'TeX-file
3204 :type 'directory)
3205
3206 ;;;###autoload
3207 (defun TeX-auto-generate (tex auto)
3208 "Generate style file for TEX and store it in AUTO.
3209 If TEX is a directory, generate style files for all files in the directory."
3210 (interactive (list (setq TeX-macro-default
3211 (expand-file-name (read-file-name
3212 "TeX file or directory: "
3213 TeX-macro-default
3214 TeX-macro-default 'confirm)))
3215 (setq TeX-auto-default
3216 (expand-file-name (read-file-name
3217 "AUTO lisp directory: "
3218 TeX-auto-default
3219 TeX-auto-default 'confirm)))))
3220 (cond ((not (file-readable-p tex)))
3221 ((string-match TeX-ignore-file tex))
3222 ((file-directory-p tex)
3223 (let ((files (directory-files (expand-file-name tex)))
3224 (default-directory (file-name-as-directory
3225 (expand-file-name tex)))
3226 (TeX-file-recurse (cond ((symbolp TeX-file-recurse)
3227 TeX-file-recurse)
3228 ((zerop TeX-file-recurse)
3229 nil)
3230 ((1- TeX-file-recurse)))))
3231 (mapcar (lambda (file)
3232 (if (or TeX-file-recurse
3233 (not (file-directory-p file)))
3234 (TeX-auto-generate file auto)))
3235 files)))
3236 ((not (file-newer-than-file-p
3237 tex
3238 (concat (file-name-as-directory auto)
3239 (TeX-strip-extension tex TeX-all-extensions t)
3240 ".el"))))
3241 ((TeX-match-extension tex (append TeX-file-extensions
3242 BibTeX-file-extensions))
3243 (save-excursion
3244 (set-buffer (let (enable-local-eval)
3245 (find-file-noselect tex)))
3246 (message "Parsing %s..." tex)
3247 (TeX-auto-store (concat (file-name-as-directory auto)
3248 (TeX-strip-extension tex
3249 TeX-all-extensions
3250 t)
3251 ".el"))
3252 (kill-buffer (current-buffer))
3253 (message "Parsing %s... done" tex)))))
3254
3255 ;;;###autoload
3256 (defun TeX-auto-generate-global ()
3257 "Create global auto directory for global TeX macro definitions."
3258 (interactive)
3259 (unless (file-directory-p TeX-auto-global)
3260 (make-directory TeX-auto-global))
3261 (let ((TeX-file-extensions '("cls" "sty"))
3262 (BibTeX-file-extensions nil))
3263 (mapc (lambda (macro) (TeX-auto-generate macro TeX-auto-global))
3264 TeX-macro-global))
3265 (byte-recompile-directory TeX-auto-global 0))
3266
3267 (defun TeX-auto-store (file)
3268 "Extract information for AUCTeX from current buffer and store it in FILE."
3269 (TeX-auto-parse)
3270
3271 (if (member nil (mapcar 'TeX-auto-entry-clear-p TeX-auto-parser))
3272 (let ((style (TeX-strip-extension nil TeX-all-extensions t)))
3273 (TeX-unload-style style)
3274 (save-excursion
3275 (set-buffer (generate-new-buffer file))
3276 (erase-buffer)
3277 (insert "(TeX-add-style-hook \"" style "\"\n"
3278 " (lambda ()")
3279 (mapc (lambda (el) (TeX-auto-insert el style))
3280 TeX-auto-parser)
3281 (insert "))\n\n")
3282 (write-region (point-min) (point-max) file nil 'silent)
3283 (kill-buffer (current-buffer))))
3284 (if (file-exists-p (concat file "c"))
3285 (delete-file (concat file "c")))
3286 (if (file-exists-p file)
3287 (delete-file file))))
3288
3289 (defun TeX-auto-entry-clear-p (entry)
3290 "Check if the temporary for `TeX-auto-parser' entry ENTRY is clear."
3291 ;; FIXME: This doc-string isn't clear to me. -- rs
3292 (null (symbol-value (nth TeX-auto-parser-temporary entry))))
3293
3294 (defun TeX-auto-insert (entry &optional skip)
3295 "Insert code to initialize ENTRY from `TeX-auto-parser'.
3296
3297 If SKIP is not-nil, don't insert code for SKIP."
3298 (let ((name (symbol-name (nth TeX-auto-parser-add entry)))
3299 (list (symbol-value (nth TeX-auto-parser-temporary entry))))
3300 (unless (null list)
3301 (insert "\n (" name)
3302 (dolist (el list)
3303 (cond ((and (stringp el) (not (string= el skip)))
3304 (insert "\n ")
3305 (insert (prin1-to-string el)))
3306 ((not (stringp el))
3307 (insert "\n ")
3308 (insert "'" (prin1-to-string el)))))
3309 (insert ")"))))
3310
3311 (defvar TeX-auto-ignore
3312 '("csname" "filedate" "fileversion" "docdate" "next" "labelitemi"
3313 "labelitemii" "labelitemiii" "labelitemiv" "labelitemv"
3314 "labelenumi" "labelenumii" "labelenumiii" "labelenumiv"
3315 "labelenumv" "theenumi" "theenumii" "theenumiii" "theenumiv"
3316 "theenumv" "document" "par" "do" "expandafter")
3317 "List of symbols to ignore when scanning a TeX style file.")
3318
3319 (defun TeX-auto-add-regexp (regexp)
3320 "Add REGEXP to `TeX-auto-regexp-list' if not already a member."
3321 (if (symbolp TeX-auto-regexp-list)
3322 (setq TeX-auto-regexp-list (symbol-value TeX-auto-regexp-list)))
3323 (or (memq regexp TeX-auto-regexp-list)
3324 (setq TeX-auto-regexp-list (cons regexp TeX-auto-regexp-list))))
3325
3326 (defvar TeX-auto-empty-regexp-list
3327 '(("<IMPOSSIBLE>\\(\\'\\`\\)" 1 ignore))
3328 "List of regular expressions guaranteed to match nothing.")
3329
3330 (defvar TeX-token-char
3331 (if (featurep 'mule)
3332 "\\(?:[a-zA-Z]\\|\\cj\\)"
3333 "[a-zA-Z]")
3334 "Regexp matching a character in a TeX macro.
3335
3336 Please use a shy group if you use a grouping construct, because
3337 the functions/variables which use `TeX-token-char' expect not to
3338 alter the numbering of any ordinary, non-shy groups.")
3339
3340 (defvar plain-TeX-auto-regexp-list
3341 (let ((token TeX-token-char))
3342 `((,(concat "\\\\def\\\\\\(" token "+\\)[^a-zA-Z@]") 1 TeX-auto-symbol-check)
3343 (,(concat "\\\\let\\\\\\(" token "+\\)[^a-zA-Z@]") 1 TeX-auto-symbol-check)
3344 (,(concat "\\\\font\\\\\\(" token "+\\)[^a-zA-Z@]") 1 TeX-auto-symbol)
3345 (,(concat "\\\\chardef\\\\\\(" token "+\\)[^a-zA-Z@]") 1 TeX-auto-symbol)
3346 (,(concat "\\\\new\\(?:count\\|dimen\\|muskip\\|skip\\)\\\\\\(" token "+\\)[^a-zA-Z@]")
3347 1 TeX-auto-symbol)
3348 (,(concat "\\\\newfont{?\\\\\\(" token "+\\)}?") 1 TeX-auto-symbol)
3349 (,(concat "\\\\typein\\[\\\\\\(" token "+\\)\\]") 1 TeX-auto-symbol)
3350 ("\\\\input +\\(\\.*[^#%\\\\\\.\n\r]+\\)\\(\\.[^#%\\\\\\.\n\r]+\\)?"
3351 1 TeX-auto-file)
3352 (,(concat "\\\\mathchardef\\\\\\(" token "+\\)[^a-zA-Z@]") 1 TeX-auto-symbol)))
3353 "List of regular expression matching common LaTeX macro definitions.")
3354
3355 (defvar TeX-auto-full-regexp-list plain-TeX-auto-regexp-list
3356 "Full list of regular expression matching TeX macro definitions.")
3357
3358 (defvar TeX-auto-prepare-hook nil
3359 "List of hooks to be called before parsing a TeX file.")
3360
3361 (defvar TeX-auto-cleanup-hook nil
3362 "List of hooks to be called after parsing a TeX file.")
3363
3364 (defcustom TeX-auto-parse-length 999999
3365 "Maximal length of TeX file (in characters) that will be parsed."
3366 :group 'TeX-parse
3367 :type 'integer)
3368 (make-variable-buffer-local 'TeX-auto-parse-length)
3369
3370 (defcustom TeX-auto-x-parse-length 0
3371 "Maximum length of TeX file that will be parsed additionally.
3372 Use `TeX-auto-x-regexp-list' for parsing the region between
3373 `TeX-auto-parse-length' and this value."
3374 :group 'TeX-parse
3375 :type 'integer)
3376 (make-variable-buffer-local 'TeX-auto-x-parse-length)
3377
3378 (defcustom TeX-auto-x-regexp-list 'LaTeX-auto-label-regexp-list
3379 "List of regular expressions used for additional parsing.
3380 See `TeX-auto-x-parse-length'."
3381 :type '(radio (variable-item TeX-auto-empty-regexp-list)
3382 (variable-item TeX-auto-full-regexp-list)
3383 (variable-item plain-TeX-auto-regexp-list)
3384 (variable-item LaTeX-auto-minimal-regexp-list)
3385 (variable-item LaTeX-auto-label-regexp-list)
3386 (variable-item LaTeX-auto-regexp-list)
3387 (symbol :tag "Other")
3388 (repeat :tag "Specify"
3389 (group (regexp :tag "Match")
3390 (sexp :tag "Groups")
3391 symbol)))
3392 :group 'TeX-parse)
3393 (make-variable-buffer-local 'TeX-auto-x-regexp-list)
3394
3395 (defun TeX-regexp-group-count (regexp)
3396 "Return number of groups in a REGEXP. This is not foolproof:
3397 you should not use something like `[\\(]' for a character range."
3398 (let (start (n 0))
3399 (while (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\([^?]"
3400 regexp start)
3401 (setq start (- (match-end 0) 2)
3402 n (1+ n)))
3403 n))
3404
3405 (defun TeX-auto-parse-region (regexp-list beg end)
3406 "Parse TeX information according to REGEXP-LIST between BEG and END."
3407 (if (symbolp regexp-list)
3408 (setq regexp-list (and (boundp regexp-list) (symbol-value regexp-list))))
3409 (if regexp-list
3410 ;; Extract the information.
3411 (let* (groups
3412 (count 1)
3413 (regexp (concat "\\("
3414 (mapconcat
3415 (lambda(x)
3416 (push (cons count x) groups)
3417 (setq count
3418 (+ 1 count
3419 (TeX-regexp-group-count (car x))))
3420 (car x))
3421 regexp-list "\\)\\|\\(")
3422 "\\)"))
3423 syms
3424 lst)
3425 (setq count 0)
3426 (goto-char (if end (min end (point-max)) (point-max)))
3427 (while (re-search-backward regexp beg t)
3428 (let* ((entry (cdr (TeX-member nil groups
3429 (lambda (a b)
3430 (match-beginning (car b))))))
3431 (symbol (nth 2 entry))
3432 (match (nth 1 entry)))
3433 (unless (TeX-in-comment)
3434 (looking-at (nth 0 entry))
3435 (if (fboundp symbol)
3436 (funcall symbol match)
3437 (puthash (if (listp match)
3438 (mapcar #'TeX-match-buffer match)
3439 (TeX-match-buffer match))
3440 (setq count (1- count))
3441 (cdr (or (assq symbol syms)
3442 (car (push
3443 (cons symbol
3444 (make-hash-table :test 'equal))
3445 syms)))))))))
3446 (setq count 0)
3447 (dolist (symbol syms)
3448 (setq lst (symbol-value (car symbol)))
3449 (while lst
3450 (puthash (pop lst)
3451 (setq count (1+ count))
3452 (cdr symbol)))
3453 (maphash (lambda (key value)
3454 (push (cons value key) lst))
3455 (cdr symbol))
3456 (clrhash (cdr symbol))
3457 (set (car symbol) (mapcar #'cdr (sort lst #'car-less-than-car)))))))
3458
3459
3460 (defun TeX-auto-parse ()
3461 "Parse TeX information in current buffer.
3462
3463 Call the functions in `TeX-auto-prepare-hook' before parsing, and the
3464 functions in `TeX-auto-cleanup-hook' after parsing."
3465
3466 (let ((case-fold-search nil))
3467
3468 (mapc 'TeX-auto-clear-entry TeX-auto-parser)
3469 (run-hooks 'TeX-auto-prepare-hook)
3470
3471 (save-excursion
3472 (and (> TeX-auto-x-parse-length TeX-auto-parse-length)
3473 (> (point-max) TeX-auto-parse-length)
3474 (TeX-auto-parse-region TeX-auto-x-regexp-list
3475 TeX-auto-parse-length
3476 TeX-auto-x-parse-length))
3477 (TeX-auto-parse-region TeX-auto-regexp-list
3478 nil TeX-auto-parse-length))
3479
3480 ;; Cleanup ignored symbols.
3481
3482 ;; NOTE: This is O(N M) where it could be O(N log N + M log M) if we
3483 ;; sorted the lists first.
3484 (while (member (car TeX-auto-symbol) TeX-auto-ignore)
3485 (setq TeX-auto-symbol (cdr TeX-auto-symbol)))
3486 (let ((list TeX-auto-symbol))
3487 (while (and list (cdr list))
3488 (if (member (car (cdr list)) TeX-auto-ignore)
3489 (setcdr list (cdr (cdr list)))
3490 (setq list (cdr list)))))
3491
3492 (run-hooks 'TeX-auto-cleanup-hook)))
3493
3494 (defun TeX-auto-clear-entry (entry)
3495 "Set the temporary variable in ENTRY to nil."
3496 (set (nth TeX-auto-parser-temporary entry) nil))
3497
3498 (defvar LaTeX-auto-end-symbol nil)
3499
3500 (defun TeX-auto-symbol-check (match)
3501 "Add MATCH to TeX-auto-symbols.
3502 Check for potential LaTeX environments."
3503 (let ((symbol (if (listp match)
3504 (mapcar 'TeX-match-buffer match)
3505 (TeX-match-buffer match))))
3506 (if (and (stringp symbol)
3507 (string-match "^end\\(.+\\)$" symbol))
3508 (add-to-list 'LaTeX-auto-end-symbol
3509 (substring symbol (match-beginning 1) (match-end 1)))
3510 (if (listp symbol)
3511 (dolist (elt symbol)
3512 (add-to-list 'TeX-auto-symbol elt))
3513 (add-to-list 'TeX-auto-symbol symbol)))))
3514
3515 ;;; Utilities
3516 ;;
3517 ;; Some of these functions has little to do with TeX, but nonetheless we
3518 ;; should use the "TeX-" prefix to avoid name clashes.
3519
3520 (defcustom TeX-auto-regexp-list 'TeX-auto-full-regexp-list
3521 "*List of regular expressions used for parsing the current file."
3522 :type '(radio (variable-item TeX-auto-empty-regexp-list)
3523 (variable-item TeX-auto-full-regexp-list)
3524 (variable-item plain-TeX-auto-regexp-list)
3525 (variable-item LaTeX-auto-minimal-regexp-list)
3526 (variable-item LaTeX-auto-label-regexp-list)
3527 (variable-item LaTeX-auto-regexp-list)
3528 (symbol :tag "Other")
3529 (repeat :tag "Specify"
3530 (group (regexp :tag "Match")
3531 (sexp :tag "Groups")
3532 symbol)))
3533 :group 'TeX-parse)
3534 (make-variable-buffer-local 'TeX-auto-regexp-list)
3535
3536 (defgroup TeX-file-extension nil
3537 "File extensions recognized by AUCTeX."
3538 :group 'TeX-file)
3539
3540 (defcustom TeX-file-extensions '("tex" "sty" "cls" "ltx" "texi" "texinfo" "dtx")
3541 "*File extensions used by manually generated TeX files."
3542 :group 'TeX-file-extension
3543 :type '(repeat (string :format "%v")))
3544
3545 (defcustom TeX-all-extensions '("[^.\n]+")
3546 "All possible file extensions."
3547 :group 'TeX-file-extension
3548 :type '(repeat (regexp :format "%v")))
3549
3550 (defcustom TeX-default-extension "tex"
3551 "*Default extension for TeX files."
3552 :group 'TeX-file-extension
3553 :type 'string)
3554
3555 (make-variable-buffer-local 'TeX-default-extension)
3556
3557 (defcustom docTeX-default-extension "dtx"
3558 "*Default extension for docTeX files."
3559 :group 'TeX-file-extension
3560 :type 'string)
3561
3562 (defvar TeX-output-extension nil
3563 "Extension of TeX output file.
3564 This is either a string or a list with
3565 a string as element. Its value is obtained from `TeX-command-output-list'.
3566 Access to the value should be through the function `TeX-output-extension'.")
3567
3568 (make-variable-buffer-local 'TeX-output-extension)
3569
3570 (defcustom BibTeX-file-extensions '("bib")
3571 "Valid file extensions for BibTeX files."
3572 :group 'TeX-file-extension
3573 :type '(repeat (string :format "%v")))
3574
3575 (defcustom BibTeX-style-extensions '("bst")
3576 "Valid file extensions for BibTeX styles."
3577 :group 'TeX-file-extension
3578 :type '(repeat (string :format "%v")))
3579
3580 (defcustom TeX-ignore-file "\\(^\\|[/\\]\\)\\(\\.\\|\\.\\.\\|RCS\\|SCCS\\|CVS\\|babel\\..*\\)$"
3581 "Regular expression matching file names to ignore.
3582
3583 These files or directories will not be considered when searching for
3584 TeX files in a directory."
3585 :group 'TeX-parse
3586 :type 'regexp)
3587
3588 (defcustom TeX-file-recurse t
3589 "*Whether to search TeX directories recursively.
3590 nil means do not recurse, a positive integer means go that far deep in the
3591 directory hierarchy, t means recurse indefinitely."
3592 :group 'TeX-parse
3593 :type '(choice (const :tag "On" t)
3594 (const :tag "Off" nil)
3595 (integer :tag "Depth" :value 1)))
3596
3597 (defun TeX-match-extension (file &optional extensions)
3598 "Return non-nil if FILE has one of EXTENSIONS.
3599
3600 If EXTENSIONS is not specified or nil, the value of
3601 `TeX-file-extensions' is used instead."
3602
3603 (if (null extensions)
3604 (setq extensions TeX-file-extensions))
3605
3606 (let ((regexp (concat "\\.\\("
3607 (mapconcat 'identity extensions "\\|")
3608 "\\)$"))
3609 (case-fold-search t))
3610 (string-match regexp file)))
3611
3612 (defun TeX-strip-extension (&optional string extensions nodir nostrip)
3613 "Return STRING without any trailing extension in EXTENSIONS.
3614 If NODIR is t, also remove directory part of STRING.
3615 If NODIR is `path', remove directory part of STRING if it is equal to
3616 the current directory, `TeX-macro-private' or `TeX-macro-global'.
3617 If NOSTRIP is set, do not remove extension after all.
3618 STRING defaults to the name of the current buffer.
3619 EXTENSIONS defaults to `TeX-file-extensions'."
3620
3621 (if (null string)
3622 (setq string (or (buffer-file-name) "<none>")))
3623
3624 (if (null extensions)
3625 (setq extensions TeX-file-extensions))
3626
3627 (let* ((strip (if (and (not nostrip)
3628 (TeX-match-extension string extensions))
3629 (substring string 0 (match-beginning 0))
3630 string))
3631 (dir (expand-file-name (or (file-name-directory strip) "./"))))
3632 (if (or (eq nodir t)
3633 (string-equal dir (expand-file-name "./"))
3634 (member dir TeX-macro-global)
3635 (member dir TeX-macro-private))
3636 (file-name-nondirectory strip)
3637 strip)))
3638
3639 (defcustom TeX-kpathsea-path-delimiter t
3640 "Path delimiter for kpathsea output.
3641 t means autodetect, nil means kpathsea is disabled."
3642 :group 'TeX-file
3643 :type '(choice (const ":")
3644 (const ";")
3645 (const :tag "Autodetect" t)
3646 (const :tag "Off" nil)))
3647
3648 (defcustom TeX-kpathsea-format-alist
3649 '(("tex" "${TEXINPUTS.latex}" TeX-file-extensions)
3650 ("sty" "${TEXINPUTS.latex}" '("sty"))
3651 ("dvi" "${TEXDOCS}" '("dvi" "pdf" "ps" "txt" "html"
3652 "dvi.gz" "pdf.gz" "ps.gz" "txt.gz" "html.gz"
3653 "dvi.bz2" "pdf.bz2" "ps.bz2" "txt.bz2" "html.bz2"))
3654 ("eps" "${TEXINPUTS}" LaTeX-includegraphics-extensions)
3655 ("pdf" "${TEXINPUTS}" LaTeX-includegraphics-extensions)
3656 ("png" "${TEXINPUTS}" LaTeX-includegraphics-extensions)
3657 ("jpg" "${TEXINPUTS}" LaTeX-includegraphics-extensions)
3658 ("jpeg" "${TEXINPUTS}" LaTeX-includegraphics-extensions)
3659 ("bib" "$BIBINPUTS" BibTeX-file-extensions)
3660 ("bst" "$BSTINPUTS" BibTeX-style-extensions))
3661 "Formats to search for expansion using kpathsea.
3662 The key of the alist represents the name of the format. The
3663 first element of the cdr of the alist is string to expand by the
3664 respective kpathsea program and the second element is a list of
3665 file extensions to match."
3666 :group 'TeX-file
3667 :type '(alist :key-type string :value-type (group string sexp)))
3668
3669 ;; FIXME: Despite the first parameter named `extensions',
3670 ;; `TeX-search-files-kpathsea' basically treats this as a format
3671 ;; specifier. Only the first element in the respective list will be
3672 ;; used to determine the search paths and file extensions with the
3673 ;; help of `TeX-kpathsea-format-alist'. Out of these differences
3674 ;; arises a need to unify the behavior of `TeX-search-files' and
3675 ;; `TeX-search-files-kpathsea' and their treatment of parameters.
3676 ;; Additionally `TeX-search-files-kpathsea' should be made more
3677 ;; general to work with other platforms and TeX systems as well.
3678 (defun TeX-search-files-kpathsea (extensions nodir strip)
3679 "The kpathsea-enabled version of `TeX-search-files'.
3680 Except for DIRECTORIES (a kpathsea string), the arguments for
3681 EXTENSIONS, NODIR and STRIP are explained there."
3682 (and TeX-kpathsea-path-delimiter
3683 (catch 'no-kpathsea
3684 (let* ((format-spec (assoc (car extensions)
3685 TeX-kpathsea-format-alist))
3686 (dirs (with-output-to-string
3687 (unless (zerop
3688 (call-process
3689 "kpsewhich" nil (list standard-output nil)
3690 nil
3691 (concat
3692 "-expand-path="
3693 (nth 1 format-spec))))
3694 (if (eq TeX-kpathsea-path-delimiter t)
3695 (throw 'no-kpathsea
3696 (setq TeX-kpathsea-path-delimiter nil))
3697 (error "kpsewhich error")))))
3698 result)
3699 (when (eq TeX-kpathsea-path-delimiter t)
3700 (setq TeX-kpathsea-path-delimiter
3701 (cond ((string-match ";" dirs)
3702 ";")
3703 ((string-match ":" dirs)
3704 ":"))))
3705 (unless TeX-kpathsea-path-delimiter
3706 (throw 'no-kpathsea nil))
3707 (setq dirs (split-string dirs (concat "[\n\r"
3708 TeX-kpathsea-path-delimiter
3709 "]+")))
3710 (setq extensions (concat "\\."
3711 (regexp-opt (eval (nth 2 format-spec)) t)
3712 "\\'"))
3713 (setq result
3714 (apply #'append
3715 (mapcar
3716 (lambda(x) (directory-files x
3717 (not nodir)
3718 extensions))
3719 dirs)))
3720 (if strip
3721 (mapcar (lambda(x)
3722 (if (string-match extensions x)
3723 (substring x 0 (match-beginning 0))
3724 x))
3725 result)
3726 result)))))
3727
3728 (defun TeX-search-files (&optional directories extensions nodir strip)
3729 "Return a list of all reachable files in DIRECTORIES ending with EXTENSIONS.
3730 If optional argument NODIR is set, remove directory part.
3731 If optional argument STRIP is set, remove file extension.
3732 If optional argument DIRECTORIES is set, search in those directories.
3733 Otherwise, search in all TeX macro directories.
3734 If optional argument EXTENSIONS is not set, use `TeX-file-extensions'"
3735 (if (null extensions)
3736 (setq extensions TeX-file-extensions))
3737 (or (TeX-search-files-kpathsea extensions nodir strip)
3738 (progn
3739 (if (null directories)
3740 (setq directories
3741 (cons "./" (append TeX-macro-private TeX-macro-global))))
3742 (let (match
3743 (TeX-file-recurse (cond ((symbolp TeX-file-recurse)
3744 TeX-file-recurse)
3745 ((zerop TeX-file-recurse)
3746 nil)
3747 ((1- TeX-file-recurse)))))
3748 (while directories
3749 (let* ((directory (car directories))
3750 (content (and directory
3751 (file-readable-p directory)
3752 (file-directory-p directory)
3753 (directory-files directory))))
3754 (setq directories (cdr directories))
3755 (while content
3756 (let ((file (concat directory (car content))))
3757 (setq content (cdr content))
3758 (cond ((string-match TeX-ignore-file file))
3759 ((not (file-readable-p file)))
3760 ((file-directory-p file)
3761 (if TeX-file-recurse
3762 (setq match
3763 (append match
3764 (TeX-search-files
3765 (list (file-name-as-directory file))
3766 extensions
3767 nodir strip)))))
3768 ((TeX-match-extension file extensions)
3769 (setq match (cons (TeX-strip-extension file
3770 extensions
3771 nodir
3772 (not strip))
3773 match))))))))
3774 match))))
3775
3776 (defun TeX-car-string-lessp (s1 s2)
3777 "Compare the cars of S1 and S2 in lexicographic order.
3778 Return t if first is less than second in lexicographic order."
3779 (string-lessp (car s1) (car s2)))
3780
3781 (defun TeX-listify (elt)
3782 "Return a newly created list with element ELT.
3783 If ELT already is a list, return ELT."
3784 (if (listp elt) elt (list elt)))
3785
3786 (defun TeX-member (elt list how)
3787 "Return the member ELT in LIST. Comparison done with HOW.
3788 Return nil if ELT is not a member of LIST."
3789 (while (and list (not (funcall how elt (car list))))
3790 (setq list (cdr list)))
3791 (car-safe list))
3792
3793 (defun TeX-elt-of-list-member (elts list)
3794 "Return non-nil if an element of ELTS is a member of LIST."
3795 (catch 'found
3796 (dolist (elt elts)
3797 (when (member elt list)
3798 (throw 'found t)))))
3799
3800 (defun TeX-assoc (key list)
3801 "Return non-nil if KEY is `equal' to the car of an element of LIST.
3802 Like assoc, except case insensitive."
3803 (let ((case-fold-search t))
3804 (TeX-member key list
3805 (lambda (a b)
3806 (string-match (concat "^" (regexp-quote a) "$")
3807 (car b))))))
3808
3809 (defun TeX-match-buffer (n)
3810 "Return the substring corresponding to the N'th match.
3811 See `match-data' for details."
3812 (if (match-beginning n)
3813 (buffer-substring-no-properties (match-beginning n) (match-end n))
3814 ""))
3815
3816 (defun TeX-function-p (arg)
3817 "Return non-nil if ARG is callable as a function."
3818 (or (and (fboundp 'byte-code-function-p)
3819 (byte-code-function-p arg))
3820 (and (listp arg)
3821 (eq (car arg) 'lambda))
3822 (and (symbolp arg)
3823 (fboundp arg))))
3824
3825 (defun TeX-booleanp (arg)
3826 "Return non-nil if ARG is t or nil."
3827 (memq arg '(t nil)))
3828
3829 (defun TeX-looking-at-backward (regexp &optional limit)
3830 "Return non-nil if the text before point matches REGEXP.
3831 Optional second argument LIMIT gives a max number of characters
3832 to look backward for."
3833 (let ((pos (point)))
3834 (save-excursion
3835 (and (re-search-backward regexp
3836 (if limit (max (point-min) (- (point) limit)))
3837 t)
3838 (eq (match-end 0) pos)))))
3839
3840 (defun TeX-current-line ()
3841 "The current line number."
3842 (format "%d" (1+ (TeX-current-offset))))
3843
3844 (defun TeX-current-file-name-master-relative ()
3845 "Return current filename, relative to master directory."
3846 (file-relative-name
3847 (buffer-file-name)
3848 (TeX-master-directory)))
3849
3850 (defun TeX-near-bobp ()
3851 "Return t iff there's nothing but whitespace between (bob) and (point)."
3852 (save-excursion
3853 (skip-chars-backward " \t\n")
3854 (bobp)))
3855
3856 (defun TeX-deactivate-mark ()
3857 "Deactivate the mark.
3858 This is a compatibility function which works both in Emacs and
3859 XEmacs. In XEmacs the region is deactivated instead of the
3860 mark which is sort of equivalent."
3861 (if (featurep 'xemacs)
3862 (zmacs-deactivate-region)
3863 (deactivate-mark)))
3864
3865 (defalias 'TeX-run-mode-hooks
3866 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks))
3867
3868
3869 ;;; Syntax Table
3870
3871 (defvar TeX-mode-syntax-table (make-syntax-table)
3872 "Syntax table used while in TeX mode.")
3873
3874 (make-variable-buffer-local 'TeX-mode-syntax-table)
3875
3876 (progn ; Define TeX-mode-syntax-table.
3877 (modify-syntax-entry (string-to-char TeX-esc)
3878 "\\" TeX-mode-syntax-table)
3879 (modify-syntax-entry ?\f ">" TeX-mode-syntax-table)
3880 (modify-syntax-entry ?\n ">" TeX-mode-syntax-table)
3881 (modify-syntax-entry (string-to-char TeX-grop)
3882 (concat "(" TeX-grcl)
3883 TeX-mode-syntax-table)
3884 (modify-syntax-entry (string-to-char TeX-grcl)
3885 (concat ")" TeX-grop)
3886 TeX-mode-syntax-table)
3887 (modify-syntax-entry ?% "<" TeX-mode-syntax-table)
3888 (modify-syntax-entry ?\" "." TeX-mode-syntax-table)
3889 (modify-syntax-entry ?& "." TeX-mode-syntax-table)
3890 (modify-syntax-entry ?_ "." TeX-mode-syntax-table)
3891 (modify-syntax-entry ?@ "_" TeX-mode-syntax-table)
3892 (modify-syntax-entry ?~ "." TeX-mode-syntax-table)
3893 (modify-syntax-entry ?$ "$" TeX-mode-syntax-table)
3894 (modify-syntax-entry ?' "w" TeX-mode-syntax-table)
3895 (modify-syntax-entry ?« "." TeX-mode-syntax-table)
3896 (modify-syntax-entry ?» "." TeX-mode-syntax-table))
3897
3898 ;;; Menu Support
3899
3900 (defvar TeX-command-current 'TeX-command-master
3901 "Specify whether to run command on master, buffer or region.")
3902 ;; Function used to run external command.
3903
3904 (defun TeX-command-select-master ()
3905 "Determine that the next command will be on the master file."
3906 (interactive)
3907 (message "Next command will be on the master file.")
3908 (setq TeX-command-current 'TeX-command-master))
3909
3910 (defun TeX-command-select-buffer ()
3911 "Determine that the next command will be on the buffer."
3912 (interactive)
3913 (message "Next command will be on the buffer")
3914 (setq TeX-command-current 'TeX-command-buffer))
3915
3916 (defun TeX-command-select-region ()
3917 "Determine that the next command will be on the region."
3918 (interactive)
3919 (message "Next command will be on the region")
3920 (setq TeX-command-current 'TeX-command-region))
3921
3922 (defvar TeX-command-force nil)
3923 ;; If non-nil, TeX-command-query will return the value of this
3924 ;; variable instead of quering the user.
3925
3926 (defun TeX-command-menu (name)
3927 "Execute `TeX-command-list' NAME from a menu."
3928 (let ((TeX-command-force name))
3929 (funcall TeX-command-current)))
3930
3931 (defun TeX-command-menu-print (printer command name)
3932 "Print on PRINTER using method COMMAND to run NAME."
3933 (let ((TeX-printer-default (unless (string= printer "Other") printer))
3934 (TeX-printer-list (and (string= printer "Other") TeX-printer-list))
3935 (TeX-print-command command)
3936 (TeX-queue-command command))
3937 (TeX-command-menu name)))
3938
3939 (defun TeX-command-menu-printer-entry (entry lookup command name)
3940 "Return `TeX-printer-list' ENTRY as a menu item."
3941 (vector (nth 0 entry)
3942 (list 'TeX-command-menu-print
3943 (nth 0 entry)
3944 (or (nth lookup entry) command)
3945 name)))
3946
3947 (defun TeX-command-menu-entry (entry)
3948 "Return `TeX-command-list' ENTRY as a menu item."
3949 (let ((name (car entry)))
3950 (cond ((and (string-equal name TeX-command-Print)
3951 TeX-printer-list)
3952 (cons TeX-command-Print
3953 (mapcar (lambda (entry)
3954 (TeX-command-menu-printer-entry
3955 entry 1 TeX-print-command name))
3956 (append TeX-printer-list '(("Other"))))))
3957 ((and (string-equal name TeX-command-Queue)
3958 TeX-printer-list)
3959 (cons TeX-command-Queue
3960 (mapcar (lambda (entry)
3961 (TeX-command-menu-printer-entry
3962 entry 2 TeX-queue-command name))
3963 (append TeX-printer-list '(("Other"))))))
3964 (t
3965 (vconcat `(,name (TeX-command-menu ,name))
3966 (nthcdr 5 entry))))))
3967
3968 (defconst TeX-command-menu-name "Command"
3969 "Name to be displayed for the command menu in all modes defined by AUCTeX.")
3970
3971 ;;; Keymap
3972
3973 (defcustom TeX-electric-escape nil
3974 "If non-nil, ``\\'' will be bound to `TeX-electric-macro'."
3975 :group 'TeX-macro
3976 :type 'boolean)
3977
3978 (defcustom TeX-electric-sub-and-superscript nil
3979 "If non-nil, insert braces after typing `^' and `_' in math mode."
3980 :group 'TeX-macro
3981 :type 'boolean)
3982
3983 (defcustom TeX-newline-function 'newline
3984 "Function to be called upon pressing `RET'."
3985 :group 'TeX-indentation
3986 :type '(choice (const newline)
3987 (const newline-and-indent)
3988 (const reindent-then-newline-and-indent)
3989 (sexp :tag "Other")))
3990
3991 (defun TeX-insert-backslash (arg)
3992 "Either insert typed key ARG times or call `TeX-electric-macro'.
3993 `TeX-electric-macro' will be called if `TeX-electric-escape' is non-nil."
3994 (interactive "*p")
3995 (if TeX-electric-escape
3996 (TeX-electric-macro)
3997 (self-insert-command arg)))
3998
3999 (defun TeX-insert-sub-or-superscript (arg)
4000 "Insert typed key ARG times and possibly a pair of braces.
4001 Brace insertion is only done if point is in a math construct and
4002 `TeX-electric-sub-and-superscript' has a non-nil value."
4003 (interactive "*p")
4004 (self-insert-command arg)
4005 (when (and TeX-electric-sub-and-superscript (texmathp))
4006 (insert (concat TeX-grop TeX-grcl))
4007 (backward-char)))
4008
4009 (defun TeX-newline ()
4010 "Call the function specified by the variable `TeX-newline-function'."
4011 (interactive) (funcall TeX-newline-function))
4012
4013 (defvar TeX-mode-map
4014 (let ((map (make-sparse-keymap)))
4015 ;; Standard
4016 ;; (define-key map "\177" 'backward-delete-char-untabify)
4017 (define-key map "\C-c}" 'up-list)
4018 (define-key map "\C-c#" 'TeX-normal-mode)
4019 (define-key map "\C-c\C-n" 'TeX-normal-mode)
4020 (define-key map "\C-c?" 'TeX-doc)
4021 (define-key map "\C-c\C-i" 'TeX-goto-info-page)
4022 (define-key map "\r" 'TeX-newline)
4023
4024 ;; From tex.el
4025 (define-key map "\"" 'TeX-insert-quote)
4026 (define-key map "$" 'TeX-insert-dollar)
4027 ;; Removed because LaTeX 2e have a better solution to italic correction.
4028 ;; (define-key map "." 'TeX-insert-punctuation)
4029 ;; (define-key map "," 'TeX-insert-punctuation)
4030 (define-key map "\C-c{" 'TeX-insert-braces)
4031 (define-key map "\C-c\C-f" 'TeX-font)
4032 (define-key map "\C-c\C-m" 'TeX-insert-macro)
4033 (define-key map "\\" 'TeX-insert-backslash)
4034 (define-key map "^" 'TeX-insert-sub-or-superscript)
4035 (define-key map "_" 'TeX-insert-sub-or-superscript)
4036 (define-key map "\e\t" 'TeX-complete-symbol) ;*** Emacs 19 way
4037
4038 (define-key map "\C-c'" 'TeX-comment-or-uncomment-paragraph) ;*** Old way
4039 (define-key map "\C-c:" 'TeX-comment-or-uncomment-region) ;*** Old way
4040 (define-key map "\C-c\"" 'TeX-uncomment) ;*** Old way
4041
4042 (define-key map "\C-c;" 'TeX-comment-or-uncomment-region)
4043 (define-key map "\C-c%" 'TeX-comment-or-uncomment-paragraph)
4044
4045 (define-key map "\C-c\C-t\C-p" 'TeX-PDF-mode)
4046 (define-key map "\C-c\C-t\C-i" 'TeX-interactive-mode)
4047 (define-key map "\C-c\C-t\C-s" 'TeX-source-correlate-mode)
4048 (define-key map "\C-c\C-t\C-r" 'TeX-pin-region)
4049 (define-key map "\C-c\C-w" 'TeX-toggle-debug-bad-boxes); to be removed
4050 (define-key map "\C-c\C-t\C-b" 'TeX-toggle-debug-bad-boxes)
4051 (define-key map "\C-c\C-t\C-w" 'TeX-toggle-debug-warnings)
4052 (define-key map "\C-c\C-v" 'TeX-view)
4053 ;; From tex-buf.el
4054 (define-key map "\C-c\C-d" 'TeX-save-document)
4055 (define-key map "\C-c\C-r" 'TeX-command-region)
4056 (define-key map "\C-c\C-b" 'TeX-command-buffer)
4057 (define-key map "\C-c\C-c" 'TeX-command-master)
4058 (define-key map "\C-c\C-k" 'TeX-kill-job)
4059 (define-key map "\C-c\C-l" 'TeX-recenter-output-buffer)
4060 (define-key map "\C-c^" 'TeX-home-buffer)
4061 (define-key map "\C-c`" 'TeX-next-error)
4062 ;; Remap bindings of `next-error'
4063 (if (featurep 'xemacs)
4064 (substitute-key-definition 'next-error 'TeX-next-error map global-map)
4065 (define-key map [remap next-error] 'TeX-next-error))
4066 ;; Remap bindings of `previous-error'
4067 (if (featurep 'xemacs)
4068 (substitute-key-definition 'previous-error 'TeX-previous-error
4069 map global-map)
4070 (define-key map [remap previous-error] 'TeX-previous-error))
4071 ;; From tex-fold.el
4072 (define-key map "\C-c\C-o\C-f" 'TeX-fold-mode)
4073
4074 ;; Multifile
4075 (define-key map "\C-c_" 'TeX-master-file-ask) ;*** temporary
4076 map)
4077 "Keymap for common TeX and LaTeX commands.")
4078
4079 (defvar plain-TeX-mode-map
4080 (let ((map (make-sparse-keymap)))
4081 (set-keymap-parent map TeX-mode-map)
4082 map)
4083 "Keymap used in plain TeX mode.")
4084
4085 (defun TeX-mode-specific-command-menu (mode)
4086 "Return a Command menu specific to the major MODE."
4087 ;; COMPATIBILITY for Emacs < 21
4088 (if (and (not (featurep 'xemacs))
4089 (= emacs-major-version 20))
4090 (cons TeX-command-menu-name
4091 (TeX-mode-specific-command-menu-entries mode))
4092 (list TeX-command-menu-name
4093 :filter `(lambda (&rest ignored)
4094 (TeX-mode-specific-command-menu-entries ',mode))
4095 "Bug.")))
4096
4097 (defun TeX-mode-specific-command-menu-entries (mode)
4098 "Return the entries for a Command menu specific to the major MODE."
4099 (append
4100 (TeX-menu-with-help
4101 `("Command on"
4102 [ "Master File" TeX-command-select-master
4103 :keys "C-c C-c" :style radio
4104 :selected (eq TeX-command-current 'TeX-command-master)
4105 :help "Commands in this menu work on the Master File"]
4106 [ "Buffer" TeX-command-select-buffer
4107 :keys "C-c C-b" :style radio
4108 :selected (eq TeX-command-current 'TeX-command-buffer)
4109 :help "Commands in this menu work on the current buffer"]
4110 [ "Region" TeX-command-select-region
4111 :keys "C-c C-r" :style radio
4112 :selected (eq TeX-command-current 'TeX-command-region)
4113 :help "Commands in this menu work on the region"]
4114 [ "Fix the Region" TeX-pin-region
4115 :active (or (if prefix-arg
4116 (<= (prefix-numeric-value prefix-arg) 0)
4117 (and (boundp 'TeX-command-region-begin)
4118 (markerp TeX-command-region-begin)))
4119 (TeX-mark-active))
4120 ;;:visible (eq TeX-command-current 'TeX-command-region)
4121 :style toggle
4122 :selected (and (boundp 'TeX-command-region-begin)
4123 (markerp TeX-command-region-begin))
4124 :help "Fix the region for \"Command on Region\""]
4125 "-"
4126 ["Recenter Output Buffer" TeX-recenter-output-buffer
4127 :help "Show the output of current TeX process"]
4128 ["Kill Job" TeX-kill-job
4129 :help "Kill the current TeX process"]
4130 ["Next Error" TeX-next-error
4131 :help "Jump to the next error of the last TeX run"]
4132 ["Quick View" TeX-view
4133 :help "Start a viewer without prompting"]
4134 "-"
4135 ("TeXing Options"
4136 ,@(mapcar (lambda (x)
4137 (let ((symbol (car x)) (name (nth 1 x)))
4138 `[ ,(format "Use %s engine" name) (TeX-engine-set ',symbol)
4139 :style radio :selected (eq TeX-engine ',symbol)
4140 :help ,(format "Use %s engine for compiling" name) ]))
4141 (TeX-engine-alist))
4142 "-"
4143 [ "Generate PDF" TeX-PDF-mode
4144 :style toggle :selected TeX-PDF-mode
4145 :active (not (eq TeX-engine 'omega))
4146 :help "Use PDFTeX to generate PDF instead of DVI"]
4147 [ "Run Interactively" TeX-interactive-mode
4148 :style toggle :selected TeX-interactive-mode :keys "C-c C-t C-i"
4149 :help "Stop on errors in a TeX run"]
4150 [ "Correlate I/O" TeX-source-correlate-mode
4151 :style toggle :selected TeX-source-correlate-mode
4152 :help "Enable forward and inverse search in the previewer"]
4153 ["Debug Bad Boxes" TeX-toggle-debug-bad-boxes
4154 :style toggle :selected TeX-debug-bad-boxes :keys "C-c C-t C-b"
4155 :help "Make \"Next Error\" show overfull and underfull boxes"]
4156 ["Debug Warnings" TeX-toggle-debug-warnings
4157 :style toggle :selected TeX-debug-warnings
4158 :help "Make \"Next Error\" show warnings"])))
4159 (let ((file 'TeX-command-on-current));; is this actually needed?
4160 (TeX-maybe-remove-help
4161 (delq nil
4162 (mapcar 'TeX-command-menu-entry
4163 (TeX-mode-specific-command-list mode)))))))
4164
4165 (defun TeX-mode-specific-command-list (mode)
4166 "Return the list of commands available in the given MODE."
4167 (let ((full-list TeX-command-list)
4168 out-list
4169 entry)
4170 (while (setq entry (pop full-list))
4171 ;; `(nth 4 entry)' may be either an atom in case of which the
4172 ;; entry should be present in any mode or a list of major modes.
4173 (if (or (atom (nth 4 entry))
4174 (memq mode (nth 4 entry)))
4175 (push entry out-list)))
4176 (nreverse out-list)))
4177
4178 (defvar TeX-fold-menu
4179 (TeX-menu-with-help
4180 '("Show/Hide"
4181 ["Fold Mode" TeX-fold-mode
4182 :style toggle
4183 :selected (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4184 :help "Toggle folding mode"]
4185 "-"
4186 ["Hide All in Current Buffer" TeX-fold-buffer
4187 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4188 :help "Hide all configured TeX constructs in the current buffer"]
4189 ["Hide All in Current Region" TeX-fold-region
4190 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4191 :help "Hide all configured TeX constructs in the marked region"]
4192 ["Hide All in Current Paragraph" TeX-fold-paragraph
4193 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4194 :help "Hide all configured TeX constructs in the paragraph containing point"]
4195 ["Hide Current Macro" TeX-fold-macro
4196 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4197 :help "Hide the macro containing point"]
4198 ["Hide Current Environment" TeX-fold-env
4199 :visible (not (eq major-mode 'plain-tex-mode))
4200 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4201 :help "Hide the environment containing point"]
4202 ["Hide Current Comment" TeX-fold-comment
4203 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4204 :help "Hide the comment containing point"]
4205 "-"
4206 ["Show All in Current Buffer" TeX-fold-clearout-buffer
4207 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4208 :help "Permanently show all folded content again"]
4209 ["Show All in Current Region" TeX-fold-clearout-region
4210 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4211 :help "Permanently show all folded content in marked region"]
4212 ["Show All in Current Paragraph" TeX-fold-clearout-paragraph
4213 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4214 :help "Permanently show all folded content in paragraph containing point"]
4215 ["Show Current Item" TeX-fold-clearout-item
4216 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4217 :help "Permanently show the item containing point"]
4218 "-"
4219 ["Hide or Show Current Item" TeX-fold-dwim
4220 :active (and (boundp 'TeX-fold-mode) TeX-fold-mode)
4221 :help "Hide or show the item containing point"]))
4222 "Menu definition for commands from tex-fold.el.")
4223
4224
4225 ;;; Menus for plain TeX mode
4226 (easy-menu-define plain-TeX-mode-command-menu
4227 plain-TeX-mode-map
4228 "Command menu used in TeX mode."
4229 (TeX-mode-specific-command-menu 'plain-tex-mode))
4230
4231 (defvar TeX-customization-menu nil)
4232
4233 (defvar TeX-common-menu-entries
4234 (TeX-menu-with-help
4235 `(("Multifile/Parsing"
4236 ["Switch to Master File" TeX-home-buffer
4237 :help "Switch to buffer of Master File, or buffer of last TeX command"]
4238 ["Save Document" TeX-save-document
4239 :help "Save all buffers associated with the current Master File"]
4240 ["Set Master File" TeX-master-file-ask
4241 :active (not (TeX-local-master-p))
4242 :help "Set the main file to run TeX commands on"]
4243 ["Reset Buffer" TeX-normal-mode
4244 :help "Save and reparse the current buffer for style information"]
4245 ["Reset AUCTeX" (TeX-normal-mode t) :keys "C-u C-c C-n"
4246 :help "Reset buffer and reload AUCTeX style files"])
4247 ["Find Documentation..." TeX-doc
4248 :help "Get help on commands, packages, or TeX-related topics in general"]
4249 ["Read the AUCTeX Manual" TeX-goto-info-page
4250 :help "Everything worth reading"]
4251 ("Customize AUCTeX"
4252 ["Browse Options"
4253 (customize-group 'AUCTeX)
4254 :help "Open the customization buffer for AUCTeX"]
4255 ["Extend this Menu"
4256 (progn
4257 (easy-menu-add-item
4258 nil
4259 ;; Ugly hack because docTeX mode uses the LaTeX menu.
4260 (list (if (eq major-mode 'doctex-mode) "LaTeX" TeX-base-mode-name))
4261 (or TeX-customization-menu
4262 (setq TeX-customization-menu
4263 (customize-menu-create 'AUCTeX "Customize AUCTeX")))))
4264 :help "Make this menu a full-blown customization menu"])
4265 ["Report AUCTeX Bug" TeX-submit-bug-report
4266 :help ,(format "Problems with AUCTeX %s? Mail us!"
4267 AUCTeX-version)])))
4268
4269 (defvar plain-TeX-menu-entries
4270 (TeX-menu-with-help
4271 `(["Macro..." TeX-insert-macro
4272 :help "Insert a macro and possibly arguments"]
4273 ["Complete" TeX-complete-symbol
4274 :help "Complete the current macro"]
4275 "-"
4276 ("Insert Font"
4277 ["Emphasize" (TeX-font nil ?\C-e) :keys "C-c C-f C-e"]
4278 ["Bold" (TeX-font nil ?\C-b) :keys "C-c C-f C-b"]
4279 ["Typewriter" (TeX-font nil ?\C-t) :keys "C-c C-f C-t"]
4280 ["Small Caps" (TeX-font nil ?\C-c) :keys "C-c C-f C-c"]
4281 ["Sans Serif" (TeX-font nil ?\C-f) :keys "C-c C-f C-f"]
4282 ["Italic" (TeX-font nil ?\C-i) :keys "C-c C-f C-i"]
4283 ["Slanted" (TeX-font nil ?\C-s) :keys "C-c C-f C-s"]
4284 ["Roman" (TeX-font nil ?\C-r) :keys "C-c C-f C-r"]
4285 ["Calligraphic" (TeX-font nil ?\C-a) :keys "C-c C-f C-a"])
4286 ("Replace Font"
4287 ["Emphasize" (TeX-font t ?\C-e) :keys "C-u C-c C-f C-e"]
4288 ["Bold" (TeX-font t ?\C-b) :keys "C-u C-c C-f C-b"]
4289 ["Typewriter" (TeX-font t ?\C-t) :keys "C-u C-c C-f C-t"]
4290 ["Small Caps" (TeX-font t ?\C-c) :keys "C-u C-c C-f C-c"]
4291 ["Sans Serif" (TeX-font t ?\C-f) :keys "C-u C-c C-f C-f"]
4292 ["Italic" (TeX-font t ?\C-i) :keys "C-u C-c C-f C-i"]
4293 ["Slanted" (TeX-font t ?\C-s) :keys "C-u C-c C-f C-s"]
4294 ["Roman" (TeX-font t ?\C-r) :keys "C-u C-c C-f C-r"]
4295 ["Calligraphic" (TeX-font t ?\C-a) :keys "C-u C-c C-f C-a"])
4296 ["Delete Font" (TeX-font t ?\C-d) :keys "C-c C-f C-d"]
4297 "-"
4298 ["Comment or Uncomment Region" TeX-comment-or-uncomment-region
4299 :help "Comment or uncomment the currently selected region"]
4300 ["Comment or Uncomment Paragraph" TeX-comment-or-uncomment-paragraph
4301 :help "Comment or uncomment the paragraph containing point"]
4302 ,TeX-fold-menu
4303 "-" . ,TeX-common-menu-entries)))
4304
4305 (easy-menu-define plain-TeX-mode-menu
4306 plain-TeX-mode-map
4307 "Menu used in plain TeX mode."
4308 (cons "TeX" plain-TeX-menu-entries))
4309
4310 ;;; AmSTeX
4311
4312 (defvar AmSTeX-mode-map
4313 (let ((map (make-sparse-keymap)))
4314 (set-keymap-parent map TeX-mode-map)
4315 map)
4316 "Keymap used in `AmSTeX-mode'.")
4317
4318 ;; Menu for AmSTeX mode
4319 (easy-menu-define AmSTeX-mode-command-menu
4320 AmSTeX-mode-map
4321 "Command menu used in AmsTeX mode."
4322 (TeX-mode-specific-command-menu 'ams-tex-mode))
4323
4324 (easy-menu-define AmSTeX-mode-menu
4325 AmSTeX-mode-map
4326 "Menu used in AMS-TeX mode."
4327 (cons "AmS-TeX" plain-TeX-menu-entries))
4328
4329 ;;;###autoload
4330 (defun ams-tex-mode ()
4331 "Major mode in AUCTeX for editing AmS-TeX files.
4332 See info under AUCTeX for documentation.
4333
4334 Special commands:
4335 \\{AmSTeX-mode-map}
4336
4337 Entering AmS-tex-mode calls the value of `text-mode-hook',
4338 then the value of `TeX-mode-hook', and then the value
4339 of `AmS-TeX-mode-hook'."
4340 (interactive)
4341 (plain-TeX-common-initialization)
4342 (setq major-mode 'ams-tex-mode)
4343 (use-local-map AmSTeX-mode-map)
4344
4345 ;; Menu
4346 (easy-menu-add AmSTeX-mode-menu AmSTeX-mode-map)
4347 (easy-menu-add AmSTeX-mode-command-menu AmSTeX-mode-map)
4348
4349 (setq TeX-base-mode-name "AmS-TeX")
4350 (setq TeX-command-default "AmSTeX")
4351 (TeX-run-mode-hooks 'text-mode-hook 'TeX-mode-hook 'AmS-TeX-mode-hook)
4352 (TeX-set-mode-name))
4353
4354
4355 ;;; Verbatim constructs
4356
4357 (defvar TeX-verbatim-p-function nil
4358 "Mode-specific function to be called by `TeX-verbatim-p'.")
4359 (make-variable-buffer-local 'TeX-verbatim-p-function)
4360
4361 ;; XXX: We only have an implementation for LaTeX mode at the moment (Oct 2009).
4362 (defun TeX-verbatim-p (&optional pos)
4363 "Return non-nil if position POS is in a verbatim-like construct.
4364 A mode-specific implementation is required. If it is not
4365 available, the function always returns nil."
4366 (when TeX-verbatim-p-function
4367 (funcall TeX-verbatim-p-function)))
4368
4369
4370 ;;; Comments
4371
4372 (defvar TeX-comment-start-regexp "%"
4373 "Regular expression matching a comment starter.
4374 Unlike the variable `comment-start-skip' it should not match any
4375 whitespace after the comment starter or any character before it.")
4376 (make-variable-buffer-local 'TeX-comment-start-regexp)
4377
4378 (defun TeX-comment-region (beg end &optional arg)
4379 "Comment each line in the region from BEG to END.
4380 Numeric prefix arg ARG means use ARG comment characters.
4381 If ARG is negative, delete that many comment characters instead."
4382 (interactive "*r\nP")
4383 ;; `comment-padding' will not be recognized in XEmacs' (21.4)
4384 ;; `comment-region', so we temporarily modify `comment-start' to get
4385 ;; proper spacing. Unfortunately we have to check for the XEmacs
4386 ;; version and cannot test if `comment-padding' is bound as this
4387 ;; gets initialized in `VirTeX-common-initialization'.
4388 (let ((comment-start (if (and (featurep 'xemacs)
4389 (= emacs-major-version 21)
4390 (<= emacs-minor-version 4))
4391 (concat comment-start (TeX-comment-padding-string))
4392 comment-start)))
4393 (comment-region beg end arg)))
4394
4395 (eval-and-compile
4396 ;; COMPATIBILITY for Emacs <= 21.3
4397 (if (fboundp 'comment-or-uncomment-region)
4398 (defalias 'TeX-comment-or-uncomment-region 'comment-or-uncomment-region)
4399 ;; The following function was copied from `newcomment.el' on
4400 ;; 2004-01-30 and adapted accordingly
4401 (defun TeX-comment-or-uncomment-region (beg end &optional arg)
4402 "Comment or uncomment a the region from BEG to END.
4403 Call `TeX-comment-region', unless the region only consists of
4404 comments, in which case call `TeX-uncomment-region'. If a prefix
4405 arg ARG is given, it is passed on to the respective function."
4406 (interactive "*r\nP")
4407 (funcall (if (save-excursion ;; check for already commented region
4408 (goto-char beg)
4409 (TeX-comment-forward (point-max))
4410 (<= end (point)))
4411 'TeX-uncomment-region 'TeX-comment-region)
4412 beg end arg)))
4413
4414 ;; COMPATIBILITY for Emacs <= 20. (Introduced in 21.1?)
4415 (if (fboundp 'uncomment-region)
4416 (defalias 'TeX-uncomment-region 'uncomment-region)
4417 (defun TeX-uncomment-region (beg end &optional arg)
4418 "Remove comment characters from the beginning of each line
4419 in the region from BEG to END. Numeric prefix arg ARG means use
4420 ARG comment characters. If ARG is negative, delete that many
4421 comment characters instead."
4422 (interactive "*r\nP")
4423 (or arg
4424 ;; Determine the number of comment characters at the
4425 ;; beginning of the first commented line.
4426 (setq arg
4427 (save-excursion
4428 (goto-char beg)
4429 (re-search-forward
4430 (concat "^" TeX-comment-start-regexp "+") end t)
4431 (length (match-string 0)))))
4432 (comment-region beg end (- arg)))))
4433
4434 (defun TeX-uncomment ()
4435 "Delete comment characters from the beginning of each line in a comment."
4436 (interactive)
4437 (save-excursion
4438 ;; Find first comment line
4439 (beginning-of-line)
4440 (while (and (looking-at (concat "^[ \t]*" TeX-comment-start-regexp))
4441 (not (bobp)))
4442 (forward-line -1))
4443 (let ((beg (point)))
4444 (forward-line 1)
4445 ;; Find last comment line
4446 (while (and (looking-at (concat "^[ \t]*" TeX-comment-start-regexp))
4447 (not (eobp)))
4448 (forward-line 1))
4449 ;; Uncomment region
4450 (TeX-uncomment-region beg (point)))))
4451
4452 (defun TeX-comment-or-uncomment-paragraph ()
4453 "Comment or uncomment current paragraph."
4454 (interactive)
4455 (if (TeX-in-commented-line)
4456 (TeX-uncomment)
4457 (save-excursion
4458 (beginning-of-line)
4459 ;; Don't do anything if we are in an empty line. If this line
4460 ;; is followed by a lot of commented lines, this shall prevent
4461 ;; that mark-paragraph skips over these lines and marks a
4462 ;; paragraph outside the visible window which might get
4463 ;; commented without the user noticing.
4464 (unless (looking-at "^[ \t]*$")
4465 (mark-paragraph)
4466 (TeX-comment-region (point) (mark))))))
4467
4468 (defun TeX-in-comment ()
4469 "Return non-nil if point is in a comment."
4470 (if (or (bolp)
4471 (null comment-start-skip)
4472 (eq (preceding-char) ?\r))
4473 nil
4474 (save-excursion
4475 (save-match-data
4476 (let ((pos (point)))
4477 (beginning-of-line)
4478 (and (or (looking-at comment-start-skip)
4479 (re-search-forward comment-start-skip pos t))
4480 (not (TeX-verbatim-p))))))))
4481
4482 (defun TeX-in-commented-line ()
4483 "Return non-nil if point is in a line consisting only of a comment.
4484 The comment can be preceded by whitespace. This means that
4485 `TeX-in-commented-line' is more general than `TeX-in-line-comment'
4486 which will not match commented lines with leading whitespace. But
4487 `TeX-in-commented-line' will match commented lines without leading
4488 whitespace as well."
4489 (save-excursion
4490 (forward-line 0)
4491 (skip-chars-forward " \t")
4492 (string= (buffer-substring-no-properties
4493 (point) (min (point-max) (+ (point) (length comment-start))))
4494 comment-start)))
4495
4496 (defun TeX-in-line-comment ()
4497 "Return non-nil if point is in a line comment.
4498 A line comment is a comment starting in column one, i.e. there is
4499 no whitespace before the comment sign."
4500 (save-excursion
4501 (forward-line 0)
4502 (string= (buffer-substring-no-properties
4503 (point) (min (point-max) (+ (point) (length comment-start))))
4504 comment-start)))
4505
4506 (defun TeX-comment-prefix ()
4507 "Return the comment prefix of the current line.
4508 If there are no comment starters after potential whitespace at
4509 the beginning of the line, return nil."
4510 (save-excursion
4511 (beginning-of-line)
4512 (save-match-data
4513 (when (looking-at (concat "\\([ \t]*" TeX-comment-start-regexp "+\\)+"))
4514 (match-string 0)))))
4515
4516 (defun TeX-forward-comment-skip (&optional count limit)
4517 "Move forward to the next comment skip.
4518 This may be a switch between commented and not commented adjacent
4519 lines or between lines with different comment prefixes. With
4520 argument COUNT do it COUNT times. If argument LIMIT is given, do
4521 not move point further than this value."
4522 (unless count (setq count 1))
4523 ;; A value of 0 is nonsense.
4524 (when (= count 0) (setq count 1))
4525 (unless limit (setq limit (point-max)))
4526 (dotimes (i (abs count))
4527 (if (< count 0)
4528 (forward-line -1)
4529 (beginning-of-line))
4530 (let ((prefix (when (looking-at (concat "\\([ \t]*"
4531 TeX-comment-start-regexp "+\\)+"))
4532 (buffer-substring (+ (line-beginning-position)
4533 (current-indentation))
4534 (match-end 0)))))
4535 (while (save-excursion
4536 (and (if (> count 0)
4537 (<= (point) limit)
4538 (>= (point) limit))
4539 (zerop (if (> count 0)
4540 (forward-line 1)
4541 (forward-line -1)))
4542 (if prefix
4543 (if (looking-at (concat "\\([ \t]*"
4544 TeX-comment-start-regexp
4545 "+\\)+"))
4546 ;; If the preceding line is a commented line
4547 ;; as well, check if the prefixes are
4548 ;; identical.
4549 (string= prefix
4550 (buffer-substring
4551 (+ (line-beginning-position)
4552 (current-indentation))
4553 (match-end 0)))
4554 nil)
4555 (not (looking-at (concat "[ \t]*"
4556 TeX-comment-start-regexp))))))
4557 (if (> count 0)
4558 (forward-line 1)
4559 (forward-line -1)))
4560 (if (> count 0)
4561 (forward-line 1)))))
4562
4563 (defun TeX-backward-comment-skip (&optional count limit)
4564 "Move backward to the next comment skip.
4565 This may be a switch between commented and not commented adjacent
4566 lines or between lines with different comment prefixes. With
4567 argument COUNT do it COUNT times. If argument LIMIT is given, do
4568 not move point to a position less than this value."
4569 (unless count (setq count 1))
4570 (when (= count 0) (setq count 1))
4571 (unless limit (setq limit (point-min)))
4572 (TeX-forward-comment-skip (- count) limit))
4573
4574 ;; Taken from `comment-forward' in Emacs' CVS on 2006-12-26. Used as
4575 ;; a compatibility function for XEmacs 21.4.
4576 (defun TeX-comment-forward (&optional n)
4577 "Skip forward over N comments.
4578 Just like `forward-comment' but only for positive N
4579 and can use regexps instead of syntax."
4580 (when (fboundp 'comment-normalize-vars)
4581 (comment-normalize-vars))
4582 (if (fboundp 'comment-forward)
4583 (comment-forward n)
4584 (setq n (or n 1))
4585 (if (< n 0) (error "No comment-backward")
4586 (if comment-use-syntax (forward-comment n)
4587 (while (> n 0)
4588 (setq n
4589 (if (or (forward-comment 1)
4590 (and (looking-at comment-start-skip)
4591 (goto-char (match-end 0))
4592 (re-search-forward comment-end-skip nil 'move)))
4593 (1- n) -1)))
4594 (= n 0)))))
4595
4596 (defun TeX-comment-padding-string ()
4597 "Return comment padding as a string.
4598 The variable `comment-padding' can hold an integer or a string.
4599 This function will return the appropriate string representation
4600 regardless of its data type."
4601 (if (integerp comment-padding)
4602 (make-string comment-padding ? )
4603 comment-padding))
4604
4605
4606 ;;; Indentation
4607
4608 (defgroup TeX-indentation nil
4609 "Indentation of TeX buffers in AUCTeX."
4610 :group 'AUCTeX)
4611
4612 (defcustom TeX-brace-indent-level 2
4613 "*The level of indentation produced by an open brace."
4614 :group 'TeX-indentation
4615 :type 'integer)
4616
4617 (defun TeX-comment-indent ()
4618 "Determine the indentation of a comment."
4619 (if (looking-at "%%%")
4620 (current-column)
4621 (skip-chars-backward " \t")
4622 (max (if (bolp) 0 (1+ (current-column)))
4623 comment-column)))
4624
4625 (defun TeX-brace-count-line ()
4626 "Count number of open/closed braces."
4627 (save-excursion
4628 (let ((count 0) (limit (line-end-position)) char)
4629 (while (progn
4630 (skip-chars-forward "^{}\\\\" limit)
4631 (when (and (< (point) limit) (not (TeX-in-comment)))
4632 (setq char (char-after))
4633 (forward-char)
4634 (cond ((eq char ?\{)
4635 (setq count (+ count TeX-brace-indent-level)))
4636 ((eq char ?\})
4637 (setq count (- count TeX-brace-indent-level)))
4638 ((eq char ?\\)
4639 (when (< (point) limit)
4640 (forward-char)
4641 t))))))
4642 count)))
4643
4644 ;;; Navigation
4645
4646 (defvar TeX-search-syntax-table
4647 (let ((table (make-syntax-table (make-char-table (if (featurep 'xemacs)
4648 'syntax
4649 'syntax-table)))))
4650 ;; Preset mode-independent syntax entries. (Mode-dependent
4651 ;; entries are set in the function `TeX-search-syntax-table'.)
4652 ;; ?\", ?\( and ?\) explicitely get whitespace syntax because
4653 ;; Emacs 21.3 and XEmacs don't generate a completely empty syntax
4654 ;; table.
4655 (dolist (elt '((?\f . ">") (?\n . ">") (?\" . " ") (?\( . " ") (?\) . " ")))
4656 (modify-syntax-entry (car elt) (cdr elt) table))
4657 table)
4658 "Syntax table used for searching purposes.
4659 It should be accessed through the function `TeX-search-syntax-table'.")
4660
4661 (defun TeX-search-syntax-table (&rest args)
4662 "Return a syntax table for searching purposes.
4663 ARGS may be a list of characters. For each of them the
4664 respective predefined syntax is set. Currently the parenthetical
4665 characters ?{, ?}, ?[, ?], ?\(, ?\), ?<, and ?> are supported.
4666 The syntax of each of these characters not specified will be
4667 reset to \" \"."
4668 (let ((char-syntax-alist '((?\{ . "(}") (?\} . "){")
4669 (?\[ . "(]") (?\] . ")[")
4670 (?\( . "()") (?\) . ")(")
4671 (?\< . "(>") (?\> . ")<"))))
4672 ;; Clean entries possibly set before.
4673 (modify-syntax-entry ?\\ " " TeX-search-syntax-table)
4674 (modify-syntax-entry ?@ " " TeX-search-syntax-table)
4675 (modify-syntax-entry ?\% " " TeX-search-syntax-table)
4676 ;; Preset mode-dependent syntax entries. (Mode-independent entries
4677 ;; are set when the variable `TeX-search-syntax-table' is created.)
4678 (modify-syntax-entry (string-to-char TeX-esc) "\\" TeX-search-syntax-table)
4679 (unless (eq major-mode 'texinfo-mode)
4680 (modify-syntax-entry ?\% "<" TeX-search-syntax-table))
4681 ;; Clean up the entries which can be specified as arguments.
4682 (dolist (elt char-syntax-alist)
4683 (modify-syntax-entry (car elt) " " TeX-search-syntax-table))
4684 ;; Now set what we got.
4685 (dolist (elt args)
4686 (unless (assoc elt char-syntax-alist) (error "Char not supported"))
4687 (modify-syntax-entry elt (cdr (assoc elt char-syntax-alist))
4688 TeX-search-syntax-table))
4689 ;; Return the syntax table.
4690 TeX-search-syntax-table))
4691
4692 (defun TeX-find-balanced-brace (&optional count depth limit)
4693 "Return the position of a balanced brace in a TeX group.
4694 The function scans forward COUNT parenthetical groupings.
4695 Default is 1. If COUNT is negative, it searches backwards. With
4696 optional DEPTH>=1, find that outer level. If LIMIT is non-nil,
4697 do not search further than this position in the buffer."
4698 (let ((count (if count
4699 (if (= count 0) (error "COUNT has to be <> 0") count)
4700 1))
4701 (depth (if depth
4702 (if (< depth 1) (error "DEPTH has to be > 0") depth)
4703 1)))
4704 (save-restriction
4705 (when limit
4706 (if (> count 0)
4707 (narrow-to-region (point-min) limit)
4708 (narrow-to-region limit (point-max))))
4709 (with-syntax-table (TeX-search-syntax-table ?\{ ?\})
4710 (condition-case nil
4711 (scan-lists (point) count depth)
4712 (error nil))))))
4713
4714 (defun TeX-find-closing-brace (&optional depth limit)
4715 "Return the position of the closing brace in a TeX group.
4716 The function assumes that point is inside the group, i.e. after
4717 an opening brace. With optional DEPTH>=1, find that outer level.
4718 If LIMIT is non-nil, do not search further down than this
4719 position in the buffer."
4720 (TeX-find-balanced-brace 1 depth limit))
4721
4722 (defun TeX-find-opening-brace (&optional depth limit)
4723 "Return the position of the opening brace in a TeX group.
4724 The function assumes that point is inside the group, i.e. before
4725 a closing brace. With optional DEPTH>=1, find that outer level.
4726 If LIMIT is non-nil, do not search further up than this position
4727 in the buffer."
4728 (TeX-find-balanced-brace -1 depth limit))
4729
4730 (defun TeX-find-macro-boundaries (&optional lower-bound)
4731 "Return a list containing the start and end of a macro.
4732 If LOWER-BOUND is given, do not search backward further than this
4733 point in buffer. Arguments enclosed in brackets or braces are
4734 considered part of the macro."
4735 (save-restriction
4736 (when lower-bound
4737 (narrow-to-region lower-bound (point-max)))
4738 (let ((orig-point (point))
4739 start-point)
4740 ;; Point is located directly at the start of a macro. (-!-\foo{bar})
4741 (when (and (eq (char-after) (aref TeX-esc 0))
4742 (not (TeX-escaped-p)))
4743 (setq start-point (point)))
4744 ;; Point is located on a macro. (\fo-!-o{bar})
4745 (unless start-point
4746 (save-excursion
4747 (skip-chars-backward "A-Za-z@*")
4748 (when (and (eq (char-before) (aref TeX-esc 0))
4749 (not (TeX-escaped-p (1- (point)))))
4750 (setq start-point (1- (point))))))
4751 ;; Point is located in the argument of a macro. (\foo{ba-!-r})
4752 (unless start-point
4753 (save-excursion
4754 (catch 'abort
4755 (let ((parse-sexp-ignore-comments t))
4756 (when (condition-case nil (progn (up-list) t) (error nil))
4757 (while (progn
4758 (condition-case nil (backward-sexp)
4759 (error (throw 'abort nil)))
4760 (forward-comment -1)
4761 (and (memq (char-before) '(?\] ?\}))
4762 (not (TeX-escaped-p (1- (point)))))))
4763 (skip-chars-backward "A-Za-z@*")
4764 (when (and (eq (char-before) (aref TeX-esc 0))
4765 (not (TeX-escaped-p (1- (point)))))
4766 (setq start-point (1- (point)))))))))
4767 ;; Search forward for the end of the macro.
4768 (when start-point
4769 (save-excursion
4770 (goto-char (TeX-find-macro-end-helper start-point))
4771 (if (< orig-point (point))
4772 (cons start-point (point))
4773 nil))))))
4774
4775 (defun TeX-find-macro-end-helper (start)
4776 "Find the end of a macro given its START.
4777 START is the position just before the starting token of the macro.
4778 If the macro is followed by square brackets or curly braces,
4779 those will be considered part of it."
4780 (save-excursion
4781 (save-match-data
4782 (catch 'found
4783 (goto-char (1+ start))
4784 (if (zerop (skip-chars-forward "A-Za-z@"))
4785 (forward-char)
4786 (skip-chars-forward "*"))
4787 (while (not (eobp))
4788 (cond
4789 ;; Skip over pairs of square brackets
4790 ((or (looking-at "[ \t]*\n?\\(\\[\\)") ; Be conservative: Consider
4791 ; only consecutive lines.
4792 (and (looking-at (concat "[ \t]*" TeX-comment-start-regexp))
4793 (save-excursion
4794 (forward-line 1)
4795 (looking-at "[ \t]*\\(\\[\\)"))))
4796 (goto-char (match-beginning 1))
4797 (condition-case nil
4798 (forward-sexp)
4799 (scan-error (throw 'found (point)))))
4800 ;; Skip over pairs of curly braces
4801 ((or (looking-at "[ \t]*\n?{") ; Be conservative: Consider
4802 ; only consecutive lines.
4803 (and (looking-at (concat "[ \t]*" TeX-comment-start-regexp))
4804 (save-excursion
4805 (forward-line 1)
4806 (looking-at "[ \t]*{"))))
4807 (goto-char (match-end 0))
4808 (goto-char (or (TeX-find-closing-brace)
4809 ;; If we cannot find a regular end, use the
4810 ;; next whitespace.
4811 (save-excursion (skip-chars-forward "^ \t\n")
4812 (point))))
4813 (when (eobp) (throw 'found (point))))
4814 (t
4815 (throw 'found (point)))))))))
4816
4817 (defun TeX-find-macro-start (&optional limit)
4818 "Return the start of a macro.
4819 If LIMIT is given, do not search backward further than this point
4820 in buffer. Arguments enclosed in brackets or braces are
4821 considered part of the macro."
4822 (car (TeX-find-macro-boundaries limit)))
4823
4824 (defun TeX-find-macro-end ()
4825 "Return the end of a macro.
4826 Arguments enclosed in brackets or braces are considered part of
4827 the macro."
4828 (cdr (TeX-find-macro-boundaries)))
4829
4830 (defun TeX-search-forward-unescaped (string &optional bound noerror)
4831 "Search forward from point for unescaped STRING.
4832 The optional argument BOUND limits the search to the respective
4833 buffer position.
4834 If NOERROR is non-nil, return nil if the search failed instead of
4835 throwing an error.
4836 A pattern is escaped, if it is preceded by an odd number of escape
4837 characters."
4838 (TeX-search-unescaped string 'forward nil bound noerror))
4839
4840 (defun TeX-search-backward-unescaped (string &optional bound noerror)
4841 "Search backward from point for unescaped STRING.
4842 The optional argument BOUND limits the search to the respective
4843 buffer position.
4844 If NOERROR is non-nil, return nil if the search failed instead of
4845 throwing an error.
4846 A pattern is escaped, if it is preceded by an odd number of escape
4847 characters."
4848 (TeX-search-unescaped string 'backward nil bound noerror))
4849
4850 (defun TeX-re-search-forward-unescaped (regexp &optional bound noerror)
4851 "Search forward from point for unescaped regular expression REGEXP.
4852 The optional argument BOUND limits the search to the respective
4853 buffer position.
4854 If NOERROR is non-nil, return nil if the search failed instead of
4855 throwing an error.
4856 A pattern is escaped, if it is preceded by an odd number of escape
4857 characters."
4858 (TeX-search-unescaped regexp 'forward t bound noerror))
4859
4860 (defun TeX-search-unescaped (pattern
4861 &optional direction regexp-flag bound noerror)
4862 "Search for unescaped PATTERN in a certain DIRECTION.
4863 DIRECTION can be indicated by the symbols 'forward and 'backward.
4864 If DIRECTION is omitted, a forward search is carried out.
4865 If REGEXP-FLAG is non-nil, PATTERN may be a regular expression,
4866 otherwise a string.
4867 The optional argument BOUND limits the search to the respective
4868 buffer position.
4869 If NOERROR is non-nil, return nil if the search failed instead of
4870 throwing an error.
4871 A pattern is escaped, if it is preceded by an odd number of escape
4872 characters."
4873 (let ((search-fun (if (eq direction 'backward)
4874 (if regexp-flag 're-search-backward 'search-backward)
4875 (if regexp-flag 're-search-forward 'search-forward))))
4876 (catch 'found
4877 (while (funcall search-fun pattern bound noerror)
4878 (when (not (TeX-escaped-p (match-beginning 0)))
4879 (throw 'found (point)))))))
4880
4881 (defun TeX-escaped-p (&optional pos)
4882 "Return t if the character at position POS is escaped.
4883 If POS is omitted, examine the character at point.
4884 A character is escaped if it is preceded by an odd number of
4885 escape characters, such as \"\\\" in LaTeX."
4886 (save-excursion
4887 (when pos (goto-char pos))
4888 (not (zerop (mod (skip-chars-backward (regexp-quote TeX-esc)) 2)))))
4889
4890 (defun TeX-current-macro ()
4891 "Return the name of the macro containing point, nil if there is none."
4892 (let ((macro-start (TeX-find-macro-start)))
4893 (when macro-start
4894 (save-excursion
4895 (goto-char macro-start)
4896 (forward-char (length TeX-esc))
4897 (buffer-substring-no-properties
4898 (point) (progn (skip-chars-forward "@A-Za-z") (point)))))))
4899
4900 (defvar TeX-search-forward-comment-start-function nil
4901 "Function to find the start of a comment.
4902 The function should accept an optional argument for specifying
4903 the limit of the search. It should return the position just
4904 before the comment if one is found and nil otherwise. Point
4905 should not be moved.")
4906 (make-variable-buffer-local 'TeX-search-forward-comment-start-function)
4907
4908 (defun TeX-search-forward-comment-start (&optional limit)
4909 "Search forward for a comment start from current position till LIMIT.
4910 If LIMIT is omitted, search till the end of the buffer.
4911
4912 The search relies on `TeX-comment-start-regexp' being set
4913 correctly for the current mode.
4914
4915 Set `TeX-search-forward-comment-start-defun' in order to override
4916 the default implementation."
4917 (if TeX-search-forward-comment-start-function
4918 (funcall TeX-search-forward-comment-start-function limit)
4919 (setq limit (or limit (point-max)))
4920 (when (TeX-re-search-forward-unescaped TeX-comment-start-regexp limit t)
4921 (match-beginning 0))))
4922
4923 ;;; Fonts
4924
4925 (defcustom TeX-font-list '((?\C-b "{\\bf " "}")
4926 (?\C-c "{\\sc " "}")
4927 (?\C-e "{\\em " "\\/}")
4928 (?\C-i "{\\it " "\\/}")
4929 (?\C-r "{\\rm " "}")
4930 (?\C-s "{\\sl " "\\/}")
4931 (?\C-t "{\\tt " "}")
4932 (?\C-d "" "" t))
4933 "List of fonts used by `TeX-font'.
4934
4935 Each entry is a list.
4936 The first element is the key to activate the font.
4937 The second element is the string to insert before point, and the third
4938 element is the string to insert after point.
4939 If the fourth and fifth element are strings, they specify the prefix and
4940 suffix to be used in math mode.
4941 An optional fourth (or sixth) element means always replace if t."
4942 :group 'TeX-macro
4943 :type '(repeat
4944 (group
4945 :value (?\C-a "" "")
4946 (character :tag "Key")
4947 (string :tag "Prefix")
4948 (string :tag "Suffix")
4949 (option (group
4950 :inline t
4951 (string :tag "Math Prefix")
4952 (string :tag "Math Suffix")))
4953 (option (sexp :format "Replace\n" :value t)))))
4954
4955 (defvar TeX-font-replace-function 'TeX-font-replace
4956 "Determines the function which is called when a font should be replaced.")
4957
4958 (defun TeX-describe-font-entry (entry)
4959 "A textual description of an ENTRY in `TeX-font-list'."
4960 (concat (format "%16s " (key-description (char-to-string (nth 0 entry))))
4961 (if (or (eq t (nth 3 entry)) (eq t (nth 5 entry)))
4962 "-- delete font"
4963 (format "%14s %-3s %14s %-3s"
4964 (nth 1 entry) (nth 2 entry)
4965 (if (stringp (nth 3 entry)) (nth 3 entry) "")
4966 (if (stringp (nth 4 entry)) (nth 4 entry) "")))))
4967
4968 (defun TeX-font (replace what)
4969 "Insert template for font change command.
4970 If REPLACE is not nil, replace current font. WHAT determines the font
4971 to use, as specified by `TeX-font-list'."
4972 (interactive "*P\nc")
4973 (TeX-update-style)
4974 (let* ((entry (assoc what TeX-font-list))
4975 (in-math (texmathp))
4976 (before (nth 1 entry))
4977 (after (nth 2 entry)))
4978 (setq replace (or replace (eq t (nth 3 entry)) (eq t (nth 5 entry))))
4979 (if (and in-math (stringp (nth 3 entry)))
4980 (setq before (nth 3 entry)
4981 after (nth 4 entry)))
4982 (cond ((null entry)
4983 (let ((help (concat
4984 "Font list: "
4985 "KEY TEXTFONT MATHFONT\n\n"
4986 (mapconcat 'TeX-describe-font-entry
4987 TeX-font-list "\n"))))
4988 (with-output-to-temp-buffer "*Help*"
4989 (set-buffer "*Help*")
4990 (insert help))))
4991 (replace
4992 (funcall TeX-font-replace-function before after))
4993 ((TeX-active-mark)
4994 (save-excursion
4995 (cond ((> (mark) (point))
4996 (insert before)
4997 (goto-char (mark))
4998 (insert after))
4999 (t
5000 (insert after)
5001 (goto-char (mark))
5002 (insert before)))))
5003 (t
5004 (insert before)
5005 (save-excursion
5006 (insert after))))))
5007
5008 (defun TeX-font-replace (start end)
5009 "Replace font specification around point with START and END.
5010 For modes with font specifications like `{\\font text}'.
5011 See also `TeX-font-replace-macro' and `TeX-font-replace-function'."
5012 (save-excursion
5013 (while (not (looking-at "{\\\\[a-zA-Z]+ "))
5014 (up-list -1))
5015 (forward-sexp)
5016 (save-excursion
5017 (replace-match start t t))
5018 (if (save-excursion
5019 (backward-char 3)
5020 (if (looking-at (regexp-quote "\\/}"))
5021 (progn
5022 (delete-char 3)
5023 nil)
5024 t))
5025 (delete-backward-char 1))
5026 (insert end)))
5027
5028 (defun TeX-font-replace-macro (start end)
5029 "Replace font specification around point with START and END.
5030 For modes with font specifications like `\\font{text}'.
5031 See also `TeX-font-replace' and `TeX-font-replace-function'."
5032 (let ((font-list TeX-font-list)
5033 cmds strings regexp)
5034 (while font-list
5035 (setq strings (cdr (car font-list))
5036 font-list (cdr font-list))
5037 (and (stringp (car strings)) (null (string= (car strings) ""))
5038 (setq cmds (cons (car strings) cmds)))
5039 (setq strings (cdr (cdr strings)))
5040 (and (stringp (car strings)) (null (string= (car strings) ""))
5041 (setq cmds (cons (car strings) cmds))))
5042 (setq regexp (mapconcat 'regexp-quote cmds "\\|"))
5043 (save-excursion
5044 (catch 'done
5045 (while t
5046 (if (/= ?\\ (following-char))
5047 (skip-chars-backward "a-zA-Z "))
5048 (skip-chars-backward (regexp-quote TeX-esc))
5049 (if (looking-at regexp)
5050 (throw 'done t)
5051 (up-list -1))))
5052 ;; Use stripped syntax table in order to get stuff like "\emph{(}" right.
5053 (with-syntax-table (TeX-search-syntax-table ?\{ ?\})
5054 (forward-sexp 2))
5055 (save-excursion
5056 (replace-match start t t))
5057 (delete-backward-char 1)
5058 (insert end))))
5059
5060 ;;; Dollars
5061 ;;
5062 ;; Rewritten from scratch with use of `texmathp' by
5063 ;; Carsten Dominik <dominik@strw.leidenuniv.nl>
5064
5065 (defvar TeX-symbol-marker nil)
5066
5067 (defvar TeX-symbol-marker-pos 0)
5068
5069 ;; The following constants are no longer used, but kept in case some
5070 ;; foreign code uses any of them.
5071 (defvar TeX-dollar-sign ?$
5072 "*Character used to enter and leave math mode in TeX.")
5073 (defconst TeX-dollar-string (char-to-string TeX-dollar-sign))
5074 (defconst TeX-dollar-regexp
5075 (concat "^" (regexp-quote TeX-dollar-string) "\\|[^" TeX-esc "]"
5076 (regexp-quote TeX-dollar-string)))
5077
5078 (defcustom TeX-math-toggle-off-input-method t
5079 "*If non-nil, auto toggle off CJK input methods when entering math mode."
5080 :group 'TeX-macro
5081 :type 'boolean)
5082
5083 (defcustom TeX-math-close-double-dollar nil
5084 "If non-nil close double dollar math by typing a single `$'."
5085 :group 'TeX-macro
5086 :type 'boolean)
5087
5088 (defun TeX-insert-dollar (&optional arg)
5089 "Insert dollar sign.
5090
5091 If current math mode was not entered with a dollar, refuse to
5092 insert one. Show matching dollar sign if this dollar sign ends
5093 the TeX math mode and `blink-matching-paren' is non-nil. Ensure
5094 double dollar signs match up correctly by inserting extra dollar
5095 signs when needed.
5096
5097 With raw \\[universal-argument] prefix, insert exactly one dollar
5098 sign. With optional ARG, insert that many dollar signs."
5099 (interactive "P")
5100 (cond
5101 ((and arg (listp arg))
5102 ;; C-u always inserts one
5103 (insert "$"))
5104 (arg
5105 ;; Numerical arg inserts that many
5106 (insert (make-string (prefix-numeric-value arg) ?\$)))
5107 ((TeX-escaped-p)
5108 ;; This is escaped with `\', so just insert one.
5109 (insert "$"))
5110 ((texmathp)
5111 ;; We are inside math mode
5112 (if (and (stringp (car texmathp-why))
5113 (string-equal (substring (car texmathp-why) 0 1) "\$"))
5114 ;; Math mode was turned on with $ or $$ - so finish it accordingly.
5115 (progn
5116 (if TeX-math-close-double-dollar
5117 (insert (car texmathp-why))
5118 (insert "$"))
5119 (when (and blink-matching-paren
5120 (or (string= (car texmathp-why) "$")
5121 (zerop (mod (save-excursion
5122 (skip-chars-backward "$")) 2))))
5123 (save-excursion
5124 (goto-char (cdr texmathp-why))
5125 (if (pos-visible-in-window-p)
5126 (sit-for 1)
5127 (message "Matches %s"
5128 (buffer-substring
5129 (point) (progn (end-of-line) (point))))))))
5130 ;; Math mode was not entered with dollar - we cannot finish it with one.
5131 (message "Math mode started with `%s' cannot be closed with dollar"
5132 (car texmathp-why))
5133 (insert "$")))
5134 (t
5135 ;; Just somewhere in the text.
5136 (insert "$")))
5137 (TeX-math-input-method-off))
5138
5139 (defvar TeX-math-input-method-off-regexp
5140 "^\\(chinese\\|japanese\\|korean\\bulgarian\\russian\\)"
5141 "Regexp matching input methods to be deactivated when entering math mode.")
5142
5143 (defun TeX-math-input-method-off ()
5144 "Toggle off input method when entering math mode."
5145 (and TeX-math-toggle-off-input-method
5146 (texmathp)
5147 (boundp 'current-input-method) current-input-method
5148 (string-match TeX-math-input-method-off-regexp current-input-method)
5149 (inactivate-input-method)))
5150
5151 ;;; Simple Commands
5152
5153 (defun TeX-normal-mode (arg)
5154 "Remove all information about this buffer, and apply the style hooks again.
5155 Save buffer first including style information.
5156 With optional argument ARG, also reload the style hooks."
5157 ;; FIXME: Shouldn't it be (&optional arg)? -- rs
5158 (interactive "*P")
5159 (if arg
5160 (setq TeX-style-hook-list nil
5161 BibTeX-global-style-files nil
5162 BibTeX-global-files nil
5163 TeX-global-input-files nil))
5164 (let ((TeX-auto-save t))
5165 (if (buffer-modified-p)
5166 (save-buffer)
5167 (TeX-auto-write)))
5168 (normal-mode)
5169 ;; See also addition to `find-file-hooks' in `VirTeX-common-initialization'.
5170 (when (eq TeX-master 'shared) (TeX-master-file nil nil t))
5171 (TeX-update-style t))
5172
5173 (defgroup TeX-quote nil
5174 "Quoting in AUCTeX."
5175 :group 'AUCTeX)
5176
5177 (defcustom TeX-open-quote "``"
5178 "String inserted by typing \\[TeX-insert-quote] to open a quotation."
5179 :group 'TeX-quote
5180 :type 'string)
5181
5182 (defcustom TeX-close-quote "''"
5183 "String inserted by typing \\[TeX-insert-quote] to close a quotation."
5184 :group 'TeX-quote
5185 :type 'string)
5186
5187 (defcustom TeX-quote-after-quote nil
5188 "Behaviour of \\[TeX-insert-quote].
5189 Nil means standard behaviour; when non-nil, opening and closing
5190 quotes are inserted only after \"."
5191 :group 'TeX-quote
5192 :type 'boolean)
5193
5194 (defcustom TeX-quote-language-alist nil
5195 "Alist for overriding the default language-specific quote insertion.
5196 First element in each item is the name of the language as set by
5197 the language style file as a string. Second element is the
5198 opening quotation mark. Third elemxent is the closing quotation
5199 mark. Opening and closing quotation marks can be specified
5200 directly as strings or as functions returning a string. Fourth
5201 element is a boolean specifying insertion behavior, overriding
5202 `TeX-quote-after-quote'. See Info node `(auctex)European' for
5203 valid languages."
5204 :group 'TeX-quote
5205 :link '(custom-manual "(auctex)European")
5206 :type '(repeat (group (choice
5207 (const "czech")
5208 (const "danish")
5209 (const "dutch")
5210 (const "german")
5211 (const "ngerman")
5212 (const "french") ;; not frenchb or francais
5213 (const "italian")
5214 (const "polish")
5215 (const "slovak")
5216 (const "swedish")
5217 (string :tag "Other Language"))
5218 (choice :tag "Opening quotation mark" string function)
5219 (choice :tag "Closing quotation mark" string function)
5220 (boolean :tag "Insert plain quote first" :value t))))
5221
5222 (defvar TeX-quote-language nil
5223 "If non-nil determines behavior of quote insertion.
5224 It is usually set by language-related style files. Its value has
5225 the same structure as the elements of `TeX-quote-language-alist'.
5226 The symbol 'override can be used as its car in order to override
5227 the settings of style files. Style files should therefore check
5228 if this symbol is present and not alter `TeX-quote-language' if
5229 it is.")
5230 (make-variable-buffer-local 'TeX-quote-language)
5231
5232 (defun TeX-insert-quote (force)
5233 "Insert the appropriate quotation marks for TeX.
5234 Inserts the value of `TeX-open-quote' (normally ``) or `TeX-close-quote'
5235 \(normally '') depending on the context. If `TeX-quote-after-quote'
5236 is non-nil, this insertion works only after \".
5237 With prefix argument FORCE, always inserts \" characters."
5238 (interactive "*P")
5239 (if (or force
5240 ;; Do not insert TeX quotes in verbatim, math or comment constructs.
5241 (and (fboundp 'font-latex-faces-present-p)
5242 (font-latex-faces-present-p '(font-latex-verbatim-face
5243 font-latex-math-face
5244 font-lock-comment-face))
5245 (font-latex-faces-present-p '(font-latex-verbatim-face
5246 font-latex-math-face
5247 font-lock-comment-face)
5248 (1- (point))))
5249 (texmathp)
5250 (and (TeX-in-comment) (not (eq major-mode 'doctex-mode))))
5251 (self-insert-command (prefix-numeric-value force))
5252 (TeX-update-style)
5253 (let* ((lang-override (if (eq (car TeX-quote-language) 'override)
5254 TeX-quote-language
5255 (assoc (car TeX-quote-language)
5256 TeX-quote-language-alist)))
5257 (lang (or lang-override TeX-quote-language))
5258 (open-quote (if lang (nth 1 lang) TeX-open-quote))
5259 (close-quote (if lang (nth 2 lang) TeX-close-quote))
5260 (q-after-q (if lang (nth 3 lang) TeX-quote-after-quote)))
5261 (when (functionp open-quote)
5262 (setq open-quote (funcall open-quote)))
5263 (when (functionp close-quote)
5264 (setq close-quote (funcall close-quote)))
5265 (if q-after-q
5266 (insert (cond ((bobp)
5267 ?\")
5268 ((save-excursion
5269 (TeX-looking-at-backward
5270 (concat (regexp-quote open-quote) "\\|"
5271 (regexp-quote close-quote))
5272 (max (length open-quote) (length close-quote))))
5273 (delete-backward-char (length (match-string 0)))
5274 "\"\"")
5275 ((< (save-excursion (skip-chars-backward "\"")) -1)
5276 ?\")
5277 ((not (= (preceding-char) ?\"))
5278 ?\")
5279 ((save-excursion
5280 (forward-char -1)
5281 (bobp))
5282 (delete-backward-char 1)
5283 open-quote)
5284 ((save-excursion
5285 (forward-char -2) ;;; at -1 there is double quote
5286 (looking-at "[ \t\n]\\|\\s("))
5287 (delete-backward-char 1)
5288 open-quote)
5289 (t
5290 (delete-backward-char 1)
5291 close-quote)))
5292 (insert (cond ((bobp)
5293 open-quote)
5294 ((= (preceding-char) (string-to-char TeX-esc))
5295 ?\")
5296 ((= (preceding-char) ?\")
5297 ?\")
5298 ((save-excursion
5299 (forward-char (- (length open-quote)))
5300 (looking-at (regexp-quote open-quote)))
5301 (delete-backward-char (length open-quote))
5302 ?\")
5303 ((save-excursion
5304 (forward-char (- (length close-quote)))
5305 (looking-at (regexp-quote close-quote)))
5306 (delete-backward-char (length close-quote))
5307 ?\")
5308 ((save-excursion
5309 (forward-char -1)
5310 (looking-at "[ \t\n]\\|\\s("))
5311 open-quote)
5312 (t
5313 close-quote)))))))
5314
5315 (defun TeX-insert-punctuation ()
5316 "Insert point or comma, cleaning up preceding space."
5317 (interactive)
5318 (expand-abbrev)
5319 (if (TeX-looking-at-backward "\\\\/\\(}+\\)" 50)
5320 (replace-match "\\1" t))
5321 (call-interactively 'self-insert-command))
5322
5323 (defun TeX-insert-braces (arg)
5324 "Make a pair of braces around next ARG sexps and leave point inside.
5325 No argument is equivalent to zero: just insert braces and leave point
5326 between.
5327
5328 If there is an active region, ARG will be ignored, braces will be
5329 inserted around the region, and point will be left after the
5330 closing brace."
5331 (interactive "P")
5332 (if (TeX-active-mark)
5333 (progn
5334 (if (< (point) (mark))
5335 (exchange-point-and-mark))
5336 (insert TeX-grcl)
5337 (save-excursion
5338 (goto-char (mark))
5339 (insert TeX-grop)))
5340 (insert TeX-grop)
5341 (save-excursion
5342 (if arg (forward-sexp (prefix-numeric-value arg)))
5343 (insert TeX-grcl))))
5344
5345 ;;;###autoload
5346 (defun TeX-submit-bug-report ()
5347 "Submit a bug report on AUCTeX via mail.
5348
5349 Don't hesitate to report any problems or inaccurate documentation.
5350
5351 If you don't have setup sending mail from (X)Emacs, please copy the
5352 output buffer into your mail program, as it gives us important
5353 information about your AUCTeX version and AUCTeX configuration."
5354 (interactive)
5355 (require 'reporter)
5356 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
5357 (reporter-submit-bug-report
5358 "bug-auctex@gnu.org"
5359 AUCTeX-version
5360 (list 'AUCTeX-date
5361 'window-system
5362 'LaTeX-version
5363 'TeX-style-path
5364 'TeX-auto-save
5365 'TeX-parse-self
5366 'TeX-master
5367 'TeX-command-list)
5368 nil nil
5369 "Remember to cover the basics, that is, what you expected to happen and
5370 what in fact did happen.
5371
5372 Be sure to consult the FAQ section in the manual before submitting
5373 a bug report. In addition check if the bug is reproducable with an
5374 up-to-date version of AUCTeX. So please upgrade to the version
5375 available from http://www.gnu.org/software/auctex/ if your
5376 installation is older than the one available from the web site.
5377
5378 If the bug is triggered by a specific \(La\)TeX file, you should try
5379 to produce a minimal sample file showing the problem and include it
5380 in your report.
5381
5382 Your bug report will be posted to the AUCTeX bug reporting list.
5383 ------------------------------------------------------------------------")))
5384
5385
5386 ;;; Documentation
5387
5388 (defun TeX-goto-info-page ()
5389 "Read documentation for AUCTeX in the info system."
5390 (interactive)
5391 (info "auctex"))
5392
5393 (autoload 'info-lookup->completions "info-look")
5394
5395 (defvar TeX-doc-backend-alist
5396 '((texdoc (plain-tex-mode latex-mode doctex-mode ams-tex-mode context-mode)
5397 (lambda ()
5398 (when (executable-find "texdoc")
5399 (TeX-search-files
5400 ;; Explicitely supply doc directory for
5401 ;; non-kpathsea-based TeX systems.
5402 (unless (stringp TeX-kpathsea-path-delimiter)
5403 (or (TeX-tree-expand
5404 '("$SYSTEXMF" "$TEXMFLOCAL" "$TEXMFMAIN" "$TEXMFDIST")
5405 "latex" '("/doc/"))
5406 `(,@TeX-macro-global ,@TeX-macro-private)))
5407 '("dvi" "pdf" "ps" "txt" "html") t t)))
5408 (lambda (doc)
5409 ;; texdoc in MiKTeX requires --view in order to start
5410 ;; the viewer instead of an intermediate web page.
5411 (call-process "texdoc" nil 0 nil "--view" doc)))
5412 (latex-info (latex-mode)
5413 (lambda ()
5414 (when (condition-case nil
5415 (save-window-excursion
5416 (let ((buf (generate-new-buffer-name "*info*")))
5417 (info "latex" buf)
5418 (kill-buffer buf))
5419 t)
5420 (error nil))
5421 (mapcar (lambda (x)
5422 (let ((x (car x)))
5423 (if (string-match "\\`\\\\" x)
5424 (substring x 1) x)))
5425 (info-lookup->completions 'symbol 'latex-mode))))
5426 (lambda (doc)
5427 (info-lookup-symbol (concat "\\" doc) 'latex-mode)))
5428 (texinfo-info (texinfo-mode)
5429 (lambda ()
5430 (when (condition-case nil
5431 (save-window-excursion
5432 (let ((buf (generate-new-buffer-name "*info*")))
5433 (info "texinfo" buf)
5434 (kill-buffer buf))
5435 t)
5436 (error nil))
5437 (mapcar (lambda (x)
5438 (let ((x (car x)))
5439 (if (string-match "\\`@" x)
5440 (substring x 1) x)))
5441 (info-lookup->completions 'symbol
5442 'texinfo-mode))))
5443 (lambda (doc)
5444 (info-lookup-symbol (concat "@" doc) 'texinfo-mode))))
5445 "Alist of backends used for looking up documentation.
5446 Each item consists of four elements.
5447
5448 The first is a symbol describing the backend's name.
5449
5450 The second is a list of modes the backend should be activated in.
5451
5452 The third is a function returning a list of documents available
5453 to the backend. It should return nil if the backend is not
5454 available, e.g. if a required executable is not present on the
5455 system in question.
5456
5457 The fourth is a function for displaying the documentation. The
5458 function should accept a single argument, the chosen package,
5459 command, or document name.")
5460
5461 (defun TeX-doc (&optional name)
5462 "Display documentation for string NAME.
5463 NAME may be a package, a command, or a document."
5464 (interactive)
5465 (let (docs)
5466 ;; Build the lists of available documentation used for completion.
5467 (dolist (elt TeX-doc-backend-alist)
5468 (when (memq major-mode (nth 1 elt))
5469 (let ((completions (funcall (nth 2 elt))))
5470 (unless (null completions)
5471 (add-to-list 'docs (cons completions (nth 0 elt)))))))
5472 (if (null docs)
5473 (progn
5474 (if (executable-find "texdoc")
5475 ;; Fallback if we did not find anything via the backend list.
5476 (let ((doc (read-from-minibuffer "Input for `texdoc': ")))
5477 (when doc (call-process "texdoc" nil 0 nil "--view" doc)))
5478 ;; Give up.
5479 (message "No documentation found")))
5480 ;; Ask the user about the package, command, or document.
5481 (when (and (interactive-p)
5482 (or (not name) (string= name "")))
5483 (let ((symbol (thing-at-point 'symbol))
5484 contained completions doc)
5485 ;; Is the symbol at point contained in the lists of available
5486 ;; documentation?
5487 (setq contained (catch 'found
5488 (dolist (elt docs)
5489 (when (member symbol (car elt))
5490 (throw 'found t)))))
5491 ;; Setup completion list in a format suitable for `completing-read'.
5492 (dolist (elt docs)
5493 (setq completions (nconc (mapcar 'list (car elt)) completions)))
5494 ;; Query user.
5495 (setq doc (completing-read
5496 (if contained
5497 (format "Package, command, or document (default %s): "
5498 symbol)
5499 "Package, command, or document: ")
5500 completions))
5501 (setq name (if (string= doc "") symbol doc))))
5502 (if (not name)
5503 (message "No documentation specified")
5504 ;; XXX: Provide way to choose in case a symbol can be found in
5505 ;; more than one backend.
5506 (let* ((backend (catch 'found
5507 (dolist (elt docs)
5508 (when (member name (car elt))
5509 (throw 'found (cdr elt)))))))
5510 (if backend
5511 (funcall (nth 3 (assoc backend TeX-doc-backend-alist)) name)
5512 (message "Documentation not found")))))))
5513
5514
5515 ;;; Ispell Support
5516
5517 ;; FIXME: Document those functions and variables. -- rs
5518
5519 ;; The FSF ispell.el use this.
5520 (defun ispell-tex-buffer-p ()
5521 (and (boundp 'ispell-tex-p) ispell-tex-p))
5522
5523 ;; The FSF ispell.el might one day use this.
5524 (setq ispell-enable-tex-parser t)
5525
5526 (defun TeX-run-ispell (command string file)
5527 "Run ispell on current TeX buffer."
5528 (cond ((and (string-equal file (TeX-region-file))
5529 (fboundp 'ispell-region))
5530 (call-interactively 'ispell-region))
5531 ((string-equal file (TeX-region-file))
5532 (call-interactively 'spell-region))
5533 ((fboundp 'ispell-buffer)
5534 (ispell-buffer))
5535 ((fboundp 'ispell)
5536 (ispell))
5537 (t
5538 (spell-buffer))))
5539
5540 (defun TeX-ispell-document (name)
5541 "Run ispell on all open files belonging to the current document."
5542 (interactive (list (TeX-master-file)))
5543 (if (string-equal name "")
5544 (setq name (TeX-master-file)))
5545
5546 (let ((found nil)
5547 (regexp (concat "\\`\\("
5548 (mapconcat (lambda (dir)
5549 (regexp-quote
5550 (expand-file-name
5551 (file-name-as-directory dir))))
5552 (append (when (file-name-directory name)
5553 (list (file-name-directory name)))
5554 TeX-check-path)
5555 "\\|")
5556 "\\).*\\("
5557 (mapconcat 'regexp-quote
5558 (cons (file-name-nondirectory name)
5559 (TeX-style-list)) "\\|")
5560 "\\)\\.\\("
5561 (mapconcat 'regexp-quote TeX-file-extensions "\\|")
5562 "\\)\\'"))
5563 (buffers (buffer-list)))
5564 (while buffers
5565 (let* ((buffer (car buffers))
5566 (name (buffer-file-name buffer)))
5567 (setq buffers (cdr buffers))
5568 (if (and name (string-match regexp name))
5569 (progn
5570 (save-excursion (switch-to-buffer buffer) (ispell-buffer))
5571 (setq found t)))))))
5572
5573 ;; Some versions of ispell 3 use this.
5574 (defvar ispell-tex-major-modes nil)
5575 (setq ispell-tex-major-modes
5576 (append '(plain-tex-mode ams-tex-mode latex-mode doctex-mode)
5577 ispell-tex-major-modes))
5578
5579
5580 ;;; Special provisions for other modes and libraries
5581
5582 ;; desktop-locals-to-save is broken by design. Don't have
5583 ;; buffer-local values of it.
5584 (eval-after-load "desktop"
5585 '(progn
5586 (dolist (elt '(TeX-master))
5587 (unless (member elt (default-value 'desktop-locals-to-save))
5588 (setq-default desktop-locals-to-save
5589 (cons elt (default-value 'desktop-locals-to-save)))))
5590 (add-hook 'desktop-after-read-hook '(lambda ()
5591 (TeX-set-mode-name t)))))
5592
5593 ;; delsel.el, `delete-selection-mode'
5594 (put 'TeX-newline 'delete-selection t)
5595 (put 'TeX-insert-dollar 'delete-selection t)
5596 (put 'TeX-insert-quote 'delete-selection t)
5597 (put 'TeX-insert-backslash 'delete-selection t)
5598
5599 (provide 'tex)
5600
5601 ;; Local Variables:
5602 ;; coding: iso-8859-1
5603 ;; End:
5604
5605 ;;; tex.el ends here