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