1
;;; tex.el --- Support for TeX documents.
3
;; Maintainer: Per Abrahamsen <auc-tex@sunsite.dk>
6
;; X-URL: http://sunsite.dk/auctex
8
;; Copyright (C) 1985, 1986, 2000, 2001 Free Software Foundation, Inc.
9
;; Copyright (C) 1987 Lars Peter Fischer
10
;; Copyright (C) 1991 Kresten Krab Thorup
11
;; Copyright (C) 1993, 1994, 1996, 1997, 1999 Per Abrahamsen
13
;; This program is free software; you can redistribute it and/or modify
14
;; it under the terms of the GNU General Public License as published by
15
;; the Free Software Foundation; either version 2, or (at your option)
18
;; This program is distributed in the hope that it will be useful,
19
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
;; GNU General Public License for more details.
23
;; You should have received a copy of the GNU General Public License
24
;; along with this program; if not, write to the Free Software
25
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29
(when (< emacs-major-version 20)
30
(error "AUC TeX requires Emacs 20 or later"))
33
(eval-when-compile (require 'cl))
36
"A (La)TeX environment."
38
:link '(custom-manual "(auctex)Top")
39
:link '(url-link :tag "Home Page" "http://sunsite.dk/auctex/")
43
(defgroup TeX-file nil
44
"Files used by AUC TeX."
47
(defgroup TeX-command nil
48
"Calling external commands from AUC TeX."
52
"LaTeX support in AUC TeX."
56
;;; Site Customization
58
;; The following variables are likely to need to be changed for your
59
;; site. It is suggested that you do this by *not* changing this
60
;; file, but instead copy those definitions you need to change to
63
(defcustom TeX-lisp-directory (or (and (fboundp 'locate-data-directory)
64
(locate-data-directory "auctex"))
65
(and (fboundp 'locate-library)
66
(let ((f (locate-library "tex")))
67
(and f (file-name-directory f))))
68
(concat data-directory "auctex/"))
69
"The directory where the AUC TeX Lisp files are located."
73
;; Change this to point to the place where the TeX macros are stored
75
(defcustom TeX-macro-global '("/usr/local/lib/texmf/tex/")
76
"Directories containing the sites TeX macro files and style files.
77
The directory names *must* end with a slash."
79
:type '(repeat (directory :format "%v")))
83
(defcustom TeX-print-command "dvips %s -P%p"
84
"*Command used to print a file.
86
First %p is expanded to the printer name, then ordinary expansion is
87
performed as specified in TeX-expand-list."
91
(defcustom TeX-queue-command "lpq -P%p"
92
"*Command used to show the status of a printer queue.
94
First %p is expanded to the printer name, then ordinary expansion is
95
performed as specified in TeX-expand-list."
99
;; This is the major configuration variable. Most sites will only
100
;; need to change the second string in each entry, which is the name
101
;; of a command to send to the shell. If you use other formatters
102
;; like AMSLaTeX or AMSTeX, you can add those to the list. See
103
;; TeX-expand-list for a description of the % escapes
105
(defcustom TeX-command-list
106
;; You may have to remove the single quotes around the command
107
;; arguments if you use DOS.
108
(list (list "TeX" "tex '\\nonstopmode\\input %t'" 'TeX-run-TeX nil t)
109
(list "TeX Interactive" "tex %t" 'TeX-run-interactive nil t)
110
(list "LaTeX" "%l '\\nonstopmode\\input{%t}'"
111
'TeX-run-LaTeX nil t)
112
(list "LaTeX Interactive" "%l %t" 'TeX-run-interactive nil t)
113
(list "LaTeX2e" "latex2e '\\nonstopmode\\input{%t}'"
114
'TeX-run-LaTeX nil t)
115
(if (or window-system (getenv "DISPLAY"))
116
(list "View" "%v " 'TeX-run-silent t nil)
117
(list "View" "dvi2tty -q -w 132 %s " 'TeX-run-command t nil))
118
(list "Print" "%p %r " 'TeX-run-command t nil)
119
(list "Queue" "%q" 'TeX-run-background nil nil)
120
(list "File" "dvips %d -o %f " 'TeX-run-command t nil)
121
(list "BibTeX" "bibtex %s" 'TeX-run-BibTeX nil nil)
122
(list "Index" "makeindex %s" 'TeX-run-command nil t)
123
;; (list "Check" "chktex -v3 %s" 'TeX-run-compile nil t)
124
;; Uncomment the above line and comment out the next line to
125
;; use `chktex' instead of `lacheck'.
126
(list "Check" "lacheck %s" 'TeX-run-compile nil t)
127
(list "Spell" "<ignored>" 'TeX-run-ispell-on-document nil nil)
128
(list "Other" "" 'TeX-run-command t t)
129
;; Not part of standard TeX.
130
(list "LaTeX PDF" "pdflatex '\\nonstopmode\\input{%t}'"
131
'TeX-run-LaTeX nil t)
132
(list "Makeinfo" "makeinfo %t" 'TeX-run-compile nil t)
133
(list "Makeinfo HTML" "makeinfo --html %t" 'TeX-run-compile nil t)
134
(list "AmSTeX" "amstex '\\nonstopmode\\input %t'"
136
"List of commands to execute on the current document.
138
Each element is a list, whose first element is the name of the command
139
as it will be presented to the user.
141
The second element is the string handed to the shell after being
142
expanded. The expansion is done using the information found in
145
The third element is the function which actually start the process.
146
Several such hooks has been defined:
148
TeX-run-command: Start up the process and show the output in a
149
separate buffer. Check that there is not two commands running for the
150
same file. Return the process object.
152
TeX-run-format: As TeX-run-command, but assume the output is created
153
by a TeX macro package. Return the process object.
155
TeX-run-TeX: For TeX output.
157
TeX-run-LaTeX: For LaTeX output.
159
TeX-run-interactive: Run TeX or LaTeX interactively.
161
TeX-run-BibTeX: For BibTeX output.
163
TeX-run-compile: Use `compile' to run the process.
165
TeX-run-shell: Use `shell-command' to run the process.
167
TeX-run-discard: Start the process in the background, discarding its
170
TeX-run-background: Start the process in the background, show output
173
TeX-run-silent: Start the process in the background.
175
TeX-run-dviout: Special hook for the Japanese dviout previewer for
178
To create your own hook, define a function taking three arguments: The
179
name of the command, the command string, and the name of the file to
180
process. It might be useful to use TeX-run-command in order to
181
create an asynchronous process.
183
If the fourth element is non-nil, the user will get a chance to
184
modify the expanded string.
186
The fifth element is obsolete and ignored."
188
:type '(repeat (group (string :tag "Name")
189
(string :tag "Command")
191
:value TeX-run-command
192
(function-item TeX-run-command)
193
(function-item TeX-run-format)
194
(function-item TeX-run-TeX)
195
(function-item TeX-run-LaTeX)
196
(function-item TeX-run-interactive)
197
(function-item TeX-run-BibTeX)
198
(function-item TeX-run-compile)
199
(function-item TeX-run-shell)
200
(function-item TeX-run-discard)
201
(function-item TeX-run-background)
202
(function-item TeX-run-silent)
203
(function-item TeX-run-dviout)
204
(function :tag "Other"))
205
(boolean :tag "Prompt")
206
(sexp :format "End\n"))))
209
;; You may want to change the default LaTeX version for your site.
210
(defcustom LaTeX-version "2e"
211
"Default LaTeX version. Currently recognized is \"2\" and \"2e\"."
213
:type '(radio (const :format "%v\n%h"
215
The executable `latex' is LaTeX version 2."
217
(const :format "%v\n%h"
219
The executable `latex' is LaTeX version 2e.
220
Do *not* select this if you need to run `latex2e' in order to get
223
(string :tag "Other")))
226
;; You may want special options to the view command depending on the
227
;; style options. Only works if parsing is enabled.
229
(defcustom LaTeX-command-style
230
(if (string-equal LaTeX-version "2")
231
;; There is a lot of different LaTeX 2 based formats.
232
'(("^latex2e$" "latex2e")
233
("^foils$" "foiltex")
235
("^slides$" "slitex")
236
("^plfonts\\|plhb$" "platex")
238
;; They have all been combined in LaTeX 2e.
240
"List of style options and LaTeX commands.
242
If the first element (a regular expresion) matches the name of one of
243
the style files, any occurrence of the string %l in a command in
244
TeX-command-list will be replaced with the second element. The first
245
match is used, if no match is found the %l is replaced with the empty
248
:type '(repeat (group :value ("" "")
249
regexp (string :tag "Style"))))
251
;; Enter the names of the printers available at your site, or nil if
252
;; you only have one printer.
254
(defcustom TeX-printer-list
255
'(("Local" "dvips -f %s | lpr" "lpq")
257
"List of available printers.
259
The first element of each entry is the printer name.
261
The second element is the command used to print to this
262
printer. It defaults to the value of TeX-print-command.
264
The third element is the command used to examine the print queue for
265
this printer. It defaults to the value of TeX-queue-command.
267
Any occurence of `%p' in the second or third element is expanded to
268
the printer name given in the first element, then ordinary expansion
269
is performed as specified in TeX-expand-list."
271
:type '(repeat (group (string :tag "Name")
272
(option (group :inline t
275
(const :tag "default")
276
(string :format "%v"))
277
(option (choice :tag "Queue"
278
(const :tag "default")
282
;; The name of the most used printer.
284
(defcustom TeX-printer-default (or (getenv "PRINTER")
285
(and TeX-printer-list
286
(car (car TeX-printer-list)))
288
"*Default printer to use with TeX-command."
292
;; You may want special options to the view command depending on the
293
;; style options. Only works if parsing is enabled.
295
(defcustom TeX-view-style '(("^a5$" "xdvi %d -paper a5")
296
("^landscape$" "xdvi %d -paper a4r -s 4")
297
;; The latest xdvi can show embedded postscript.
298
;; If you don't have that, uncomment next line.
299
;; ("^epsf$" "ghostview %f")
301
"List of style options and view options.
303
If the first element (a regular expresion) matches the name of one of
304
the style files, any occurrence of the string %v in a command in
305
TeX-command-list will be replaced with the second element. The first
306
match is used, if no match is found the %v is replaced with the empty
309
:type '(repeat (group regexp (string :tag "Command"))))
313
(defcustom TeX-print-style '(("^landscape$" "-t landscape"))
314
"List of style options and print options.
316
If the first element (a regular expresion) matches the name of one of
317
the style files, any occurrence of the string %r in a command in
318
TeX-command-list will be replaced with the second element. The first
319
match is used, if no match is found the %r is replaced with the empty
322
:type '(repeat (group regexp (string :tag "Command"))))
324
;; This is the list of expansion for the commands in
325
;; TeX-command-list. Not likely to be changed, but you may e.g. want
326
;; to handle .ps files.
328
(defcustom TeX-expand-list
329
(list (list "%p" 'TeX-printer-query) ;%p must be the first entry
330
(list "%q" (function (lambda ()
331
(TeX-printer-query TeX-queue-command 2))))
332
(list "%v" (lambda ()
333
(TeX-style-check TeX-view-style)))
334
(list "%r" (lambda ()
335
(TeX-style-check TeX-print-style)))
336
(list "%l" (lambda ()
337
(TeX-style-check LaTeX-command-style)))
338
(list "%s" 'file nil t)
339
(list "%t" 'file 't t)
340
(list "%n" 'TeX-current-line)
341
(list "%d" 'file "dvi" t)
342
(list "%f" 'file "ps" t)
343
(list "%b" 'TeX-current-file-name-nondirectory))
344
"List of expansion strings for TeX command names.
346
Each entry is a list with two or more elements. The first element is
347
the string to be expanded. The second element is the name of a
348
function returning the expanded string when called with the remaining
349
elements as arguments. The special value `file' will be expanded to
350
the name of the file being processed, with an optional extension."
352
:type '(repeat (group (string :tag "Key")
353
(sexp :tag "Expander")
356
(sexp :format "%v")))))
358
;; End of Site Customization.
362
;;(or (assoc TeX-lisp-directory (mapcar 'list load-path)) ;No `member' yet.
363
;; (setq load-path (cons TeX-lisp-directory load-path)))
366
"This function is part of AUC TeX, but has not yet been loaded.
367
Full documentation will be available after autoloading the function."
368
"Documentation for autoload functions.")
370
;; This hook will store bibitems when you save a BibTeX buffer.
371
(add-hook 'bibtex-mode-hook 'BibTeX-auto-store)
373
(autoload 'BibTeX-auto-store "latex" no-doc t)
375
(autoload 'LaTeX-math-mode "latex" no-doc t)
376
(autoload 'japanese-plain-tex-mode "tex-jp" no-doc t)
377
(autoload 'japanese-latex-mode "tex-jp" no-doc t)
378
(autoload 'japanese-slitex-mode "tex-jp" no-doc t)
379
(autoload 'texinfo-mode "tex-info" no-doc t)
380
(autoload 'latex-mode "latex" no-doc t)
382
(autoload 'multi-prompt "multi-prompt" no-doc nil)
383
(autoload 'texmathp "texmathp" no-doc nil)
389
;;; Special support for XEmacs
391
(when (featurep 'xemacs)
393
(defadvice popup-mode-menu (before LaTeX-update activate)
394
"Run `LaTeX-menu-update' before showing menu."
395
(and (fboundp 'LaTeX-menu-update)
396
(LaTeX-menu-update)))
398
(defun TeX-mark-active ()
399
;; In Lucid (mark) returns nil when not active.
404
(defun TeX-active-mark ()
405
(and zmacs-regions (mark)))
407
(fset 'TeX-activate-region (symbol-function 'zmacs-activate-region))
411
;;; Special support for GNU Emacs
413
(unless (featurep 'xemacs)
415
(defun TeX-mark-active ()
416
;; In FSF 19 mark-active indicates if mark is active.
419
(defun TeX-active-mark ()
420
(and transient-mark-mode mark-active))
422
(defun TeX-activate-region ()
429
;; These two variables are automatically updated with "make dist", so
430
;; be careful before changing anything.
432
(defconst AUC-TeX-version "11.06"
433
"AUC TeX version number.")
435
(defconst AUC-TeX-date "Mon Oct 15 17:10:13 CEST 2001"
436
"AUC TeX release date.")
440
(defgroup TeX-output nil
441
"Parsing TeX output."
445
(defcustom TeX-display-help t
446
"*Non-nil means popup help when stepping thrugh errors with \\[TeX-next-error]."
450
(defcustom TeX-debug-bad-boxes nil
451
"*Non-nil means also find overfull/underfull boxes warnings with \\[TeX-next-error]."
455
(defgroup TeX-command-name nil
456
"Names for external commands in AUC TeX."
459
(defcustom TeX-command-BibTeX "BibTeX"
460
"*The name of the BibTeX entry in TeX-command-list."
461
:group 'TeX-command-name
463
(make-variable-buffer-local 'TeX-command-BibTeX)
465
(defcustom TeX-command-Show "View"
466
"*The default command to show (view or print) a TeX file.
467
Must be the car of an entry in TeX-command-list."
468
:group 'TeX-command-name
470
(make-variable-buffer-local 'TeX-command-Show)
472
(defcustom TeX-command-Print "Print"
473
"The name of the Print entry in TeX-command-Print."
474
:group 'TeX-command-name
477
(defcustom TeX-command-Queue "Queue"
478
"The name of the Queue entry in TeX-command-Queue."
479
:group 'TeX-command-name
482
(autoload 'TeX-region-create "tex-buf" no-doc nil)
483
(autoload 'TeX-save-document "tex-buf" no-doc t)
484
(autoload 'TeX-home-buffer "tex-buf" no-doc t)
485
(autoload 'TeX-command-region "tex-buf" no-doc t)
486
(autoload 'TeX-command-buffer "tex-buf" no-doc t)
487
(autoload 'TeX-command-master "tex-buf" no-doc t)
488
(autoload 'TeX-command "tex-buf" no-doc nil)
489
(autoload 'TeX-kill-job "tex-buf" no-doc t)
490
(autoload 'TeX-recenter-output-buffer "tex-buf" no-doc t)
491
(autoload 'TeX-next-error "tex-buf" no-doc t)
492
(autoload 'TeX-toggle-debug-boxes "tex-buf" no-doc t)
493
(autoload 'TeX-region-file "tex-buf" no-doc nil)
494
(autoload 'TeX-current-offset "tex-buf" no-doc nil)
496
(defvar TeX-trailer-start nil
497
"Regular expression delimiting start of trailer in a TeX file.")
499
(make-variable-buffer-local 'TeX-trailer-start)
501
(defvar TeX-header-end nil
502
"Regular expression delimiting end of header in a TeX file.")
504
(make-variable-buffer-local 'TeX-header-end)
506
(defvar TeX-command-default nil
507
"The default command for TeX-command in the current major mode.")
509
(make-variable-buffer-local 'TeX-command-default)
514
(defcustom TeX-one-master "\\.tex$"
515
"*Regular expression matching ordinary TeX files.
517
You should set this variable to match the name of all files, where
518
automatically adding a file variable with the name of the master file
519
is a good idea. When AUC TeX add the name of the master file as a
520
file variable, it does not need to ask next time you edit the file.
522
If you dislike AUC TeX automatically modifying your files, you can set
523
this variable to \"<none>\"."
527
(defun TeX-dwim-master ()
528
"Find a likely `TeX-master'."
529
(let ((dir default-directory))
530
(dolist (buf (buffer-list))
531
(when (with-current-buffer buf
532
(and (equal dir default-directory)
533
(stringp TeX-master)))
534
(return (with-current-buffer buf TeX-master))))))
536
(defun TeX-master-file (&optional extension nondirectory)
537
"Return the name of the master file for the current document.
539
If optional argument EXTENSION is non-nil, add that file extension to
540
the name. Special value t means use `TeX-default-extension'.
542
If optional second argument NONDIRECTORY is non-nil, do not include
545
Currently is will check for the presence of a ``Master:'' line in
546
the beginning of the file, but that feature will be phased out."
548
(setq extension TeX-default-extension))
549
(let ((my-name (if (buffer-file-name)
550
(TeX-strip-extension nil (list TeX-default-extension) t)
555
(goto-char (point-min))
557
;; Special value 't means it is own master (a free file).
558
((equal TeX-master my-name)
561
;; For files shared between many documents.
562
((eq 'shared TeX-master)
565
(let ((default (or (TeX-dwim-master) "this file")))
566
(read-file-name (format "Master file: (default %s) " default)
568
(list TeX-default-extension)
570
(if (or (string-equal TeX-master "this file")
571
(string-equal TeX-master ""))
572
(setq TeX-master t)))
574
;; We might already know the name.
575
((or (eq TeX-master t) (stringp TeX-master)) TeX-master)
577
;; Support the ``Master:'' line (under protest!)
579
"^%% *[Mm]aster:?[ \t]*\\([^ \t\n]+\\)" 500 t)
581
(TeX-strip-extension (TeX-match-buffer 1)
582
(list TeX-default-extension)))
583
(if TeX-convert-master
587
(TeX-add-local-master))))
589
;; Is this a master file?
590
((re-search-forward TeX-header-end 10000 t)
591
(setq TeX-master my-name))
593
;; Ask the user (but add it as a local variable).
596
(let ((default (TeX-dwim-master)))
598
(and (eq 'dwim TeX-master) default)
601
(read-file-name (format "Master file: (default %s) "
602
(or default "this file"))
603
nil (or default "<default>"))
605
(list TeX-default-extension)
607
(cond ((string-equal TeX-master "<quit>")
609
((or (string-equal TeX-master "<default>")
610
(string-equal TeX-master ""))
612
(TeX-add-local-master))
614
(TeX-add-local-master)))))))
616
(let ((name (if (eq TeX-master t)
620
(if (TeX-match-extension name)
621
;; If it already have an extension...
622
(if (equal extension TeX-default-extension)
623
;; Use instead of the default extension
625
;; Otherwise drop it.
626
(setq name (TeX-strip-extension name))))
628
;; Remove directory if needed.
630
(setq name (file-name-nondirectory name)))
633
(concat name "." extension)
636
(defun TeX-master-directory ()
637
"Directory of master file."
638
(abbreviate-file-name
640
(substitute-in-file-name
641
(concat (file-name-directory buffer-file-name)
642
(file-name-directory (TeX-master-file)))))))
644
(defcustom TeX-master t
645
"*The master file associated with the current buffer.
646
If the file being edited is actually included from another file, you
647
can tell AUC TeX the name of the master file by setting this variable.
648
If there are multiple levels of nesting, specify the top level file.
650
If this variable is nil, AUC TeX will query you for the name.
652
If the variable is t, AUC TeX will assume the file is a master file
655
If the variable is 'shared, AUC TeX will query for the name, but not
658
If the variable is 'dwim, AUC TeX will try to avoid querying by
659
attempting to `do what I mean'; and then change the file.
661
It is suggested that you use the File Variables (see the info node in
662
the Emacs manual) to set this variable permanently for each file."
665
:type '(choice (const :tag "Query" nil)
666
(const :tag "This file" t)
667
(const :tag "Shared" shared)
668
(const :tag "Dwim" dwim)
669
(string :format "%v")))
671
(make-variable-buffer-local 'TeX-master)
673
(defvar TeX-convert-master t
674
"*If not nil, automatically convert ``Master:'' lines to file variables.
675
This will be done when AUC TeX first try to use the master file.")
677
(defun TeX-add-local-master ()
678
"Add local variable for `TeX-master'."
680
(if (and (buffer-file-name)
681
(string-match TeX-one-master
682
(file-name-nondirectory (buffer-file-name)))
683
(not buffer-read-only))
685
(goto-char (point-max))
686
(if (re-search-backward (concat "^\\([^\n]+\\)Local " "Variables:")
687
(- (point-max) 3000) t)
688
(let ((prefix (TeX-match-buffer 1)))
689
(re-search-forward (regexp-quote (concat prefix
691
(beginning-of-line 1)
692
(insert prefix "TeX-master: " (prin1-to-string TeX-master) "\n"))
693
(insert "\n%%% Local " "Variables: \n"
694
"%%% mode: " (substring (symbol-name major-mode) 0 -5)
696
"%%% TeX-master: " (prin1-to-string TeX-master) "\n"
701
(or (string-match "/\\'" TeX-lisp-directory)
702
(setq TeX-lisp-directory (concat TeX-lisp-directory "/")))
704
(defcustom TeX-auto-global (concat TeX-lisp-directory "auto/")
705
"*Directory containing automatically generated information.
706
Must end with a slash.
708
For storing automatic extracted information about the TeX macros
709
shared by all users of a site."
713
(defcustom TeX-style-global (concat TeX-lisp-directory "style/")
714
"*Directory containing hand generated TeX information.
715
Must end with a slash.
717
These correspond to TeX macros shared by all users of a site."
721
(defcustom TeX-auto-local "auto/"
722
"*Directory containing automatically generated TeX information.
723
Must end with a slash.
725
This correspond to TeX macros found in the current directory, and must
726
be relative to that."
730
(defcustom TeX-style-local "style/"
731
"*Directory containing hand generated TeX information.
732
Must end with a slash.
734
These correspond to TeX macros found in the current directory, and must
735
be relative to that."
739
(defun TeX-split-string (regexp string)
740
"Returns a list of strings.
741
Given REGEXP the STRING is split into sections which in string was
746
(TeX-split-string \"\:\" \"abc:def:ghi\")
747
-> (\"abc\" \"def\" \"ghi\")
749
(TeX-split-string \" *\" \"dvips -Plw -p3 -c4 testfile.dvi\")
751
-> (\"dvips\" \"-Plw\" \"-p3\" \"-c4\" \"testfile.dvi\")
753
If REGEXP is nil, or \"\", an error will occur."
757
(while (string-match regexp string start)
758
(let ((match (string-match regexp string start)))
759
(setq result (cons (substring string start match) result))
760
(setq start (match-end 0))))
761
(setq result (cons (substring string start nil) result))
764
(defun TeX-directory-absolute-p (dir)
765
;; Non-nil iff DIR is the name of an absolute directory.
766
(if (memq system-type '(ms-dos emx windows-nt))
767
(string-match "^\\([A-Za-z]:\\)?/" dir)
768
(string-match "^/" dir)))
770
(defun TeX-parse-path (env)
771
;; Return a list if private TeX directories found in environment
773
(let* ((value (getenv env))
774
(entries (and value (TeX-split-string ":" value)))
778
(setq entry (car entries))
779
(setq entries (cdr entries))
780
(or (string-match "/$" entry)
781
(setq entry (concat entry "/")))
782
(and (string-match "//$" entry)
783
(setq entry (substring entry 0 -1)))
784
(or (not (TeX-directory-absolute-p entry))
785
(member entry TeX-macro-global)
786
(string-equal "/" entry)
787
(setq answers (cons entry answers))))
790
(defcustom TeX-macro-private (append (TeX-parse-path "TEXINPUTS")
791
(TeX-parse-path "BIBINPUTS"))
792
"Directories where you store your personal TeX macros.
793
Each must end with a slash."
795
:type '(repeat (file :format "%v")))
797
(defcustom TeX-auto-private (mapcar (function (lambda (entry)
798
(concat entry TeX-auto-local)))
800
"List of directories containing automatically generated information.
801
Must end with a slash.
803
These correspond to the personal TeX macros."
805
:type '(repeat (file :format "%v")))
807
(if (stringp TeX-auto-private) ;Backward compatibility
808
(setq TeX-auto-private (list TeX-auto-private)))
810
(defcustom TeX-style-private (mapcar (function (lambda (entry)
814
"List of directories containing hand generated information.
815
Must end with a slash.
817
These correspond to the personal TeX macros."
819
:type '(repeat (file :format "%v")))
821
(if (stringp TeX-style-private) ;Backward compatibility
822
(setq TeX-style-private (list TeX-style-private)))
824
(defcustom TeX-style-path
826
(mapcar (function (lambda (file) (if file (setq path (cons file path)))))
827
(append (list TeX-auto-global TeX-style-global)
828
TeX-auto-private TeX-style-private
829
(list TeX-auto-local TeX-style-local)))
831
"List of directories to search for AUC TeX style files."
833
:type '(repeat (file :format "%v")))
835
(defcustom TeX-check-path
836
(append (list "./") TeX-macro-private TeX-macro-global)
837
"Directory path to search for dependencies.
839
If nil, just check the current file.
840
Used when checking if any files have changed."
842
:type '(repeat (file :format "%v")))
846
(defvar TeX-style-hook-list nil
847
"List of TeX style hooks currently loaded.
849
Each entry is a list where the first element is the name of the style,
850
and the remaining elements are hooks to be run when that style is
853
(defcustom TeX-byte-compile nil
854
"*Not nil means try to byte compile auto files before loading."
858
(defun TeX-load-style (style)
859
"Search for and load each definition for STYLE in TeX-style-path."
860
(cond ((assoc style TeX-style-hook-list)) ; We already found it
861
((string-match "\\`\\(.+/\\)\\([^/]*\\)\\'" style) ;Complex path
862
(let* ((dir (substring style (match-beginning 1) (match-end 1)))
863
(style (substring style (match-beginning 2) (match-end 2)))
864
(master-dir (if (stringp TeX-master)
866
(file-relative-name TeX-master))
868
(TeX-style-path (append (list (concat dir TeX-auto-local)
871
(concat dir TeX-style-local)
875
(TeX-load-style style)))
877
;; Insert empty list to mark the fact that we have searched.
878
(setq TeX-style-hook-list (cons (list style) TeX-style-hook-list))
879
;; Now check each element of the path
880
(mapcar (function (lambda (name)
881
(TeX-load-style-file (if (string-match "/$" name)
883
(concat name "/" style)))))
886
(defun TeX-load-style-file (file)
887
;; Load FILE checking for a lisp extensions.
888
(let ((el (concat file ".el"))
889
(elc (concat file ".elc")))
890
(cond ((file-newer-than-file-p el elc)
891
(if (file-readable-p el)
892
(if (and TeX-byte-compile
893
(file-writable-p elc)
895
;; `byte-compile-file' switches buffer in Emacs 20.3.
896
(byte-compile-file el))
897
(file-readable-p elc))
900
((file-readable-p elc)
902
((file-readable-p el)
905
(defun TeX-add-style-hook (style hook)
906
"Give STYLE yet another HOOK to run."
907
(let ((entry (assoc style TeX-style-hook-list)))
909
;; New style, add entry.
910
(setq TeX-style-hook-list (cons (list style hook)
911
TeX-style-hook-list)))
913
;; Old style, hook already there, do nothing.
916
;; Old style, new hook.
917
(setcdr entry (cons hook (cdr entry)))))))
919
(defun TeX-unload-style (style)
920
"Forget that we once loaded STYLE."
921
(cond ((null (assoc style TeX-style-hook-list)))
922
((equal (car (car TeX-style-hook-list)) style)
923
(setq TeX-style-hook-list (cdr TeX-style-hook-list)))
925
(let ((entry TeX-style-hook-list))
926
(while (not (equal (car (car (cdr entry))) style))
927
(setq entry (cdr entry)))
928
(setcdr entry (cdr (cdr entry)))))))
930
(defcustom TeX-virgin-style (if (and TeX-auto-global
931
(file-directory-p TeX-auto-global))
934
"Style all documents use."
938
(defvar TeX-active-styles nil
939
"List of styles currently active in the document.")
941
(make-variable-buffer-local 'TeX-active-styles)
943
(defun TeX-run-style-hooks (&rest styles)
944
"Run the TeX following style hooks."
947
(if (TeX-member style TeX-active-styles 'string-equal)
949
(setq TeX-active-styles
950
(cons style TeX-active-styles))
951
(TeX-load-style style)
952
(if (string-match "\\`\\(.+/\\)\\([^/]*\\)\\'" style)
953
(setq style ; Complex path
954
(substring style (match-beginning 2) (match-end 2))))
956
(cdr-safe (assoc style TeX-style-hook-list))))))
959
(defcustom TeX-parse-self nil
960
"Parse file after loading it if no style hook is found for it."
964
(defvar TeX-style-hook-applied-p nil
965
"Nil, unless the style specific hooks have been applied.")
966
(make-variable-buffer-local 'TeX-style-hook-applied-p)
968
(defun TeX-update-style (&optional force)
969
"Run style specific hooks for the current document.
971
Only do this if it has not been done before, or if optional argument
974
(if (or (and (boundp 'TeX-auto-update)
975
(eq TeX-auto-update 'BibTeX)) ; Not a real TeX buffer
976
(and (not force) TeX-style-hook-applied-p))
978
(setq TeX-style-hook-applied-p t)
979
(message "Applying style hooks...")
980
(TeX-run-style-hooks (TeX-strip-extension nil nil t))
981
;; Run parent style hooks if it has a single parent that isn't itself.
982
(if (or (not (memq TeX-master '(nil t)))
983
(and (buffer-file-name)
984
(string-match TeX-one-master
985
(file-name-nondirectory (buffer-file-name)))))
986
(TeX-run-style-hooks (TeX-master-file)))
988
(if (and TeX-parse-self
989
(null (cdr-safe (assoc (TeX-strip-extension nil nil t)
990
TeX-style-hook-list))))
993
(message "Applying style hooks... done")))
995
(defvar TeX-remove-style-hook nil
996
"List of hooks to call when we remove the style specific information.")
997
(make-variable-buffer-local 'TeX-remove-style-hook)
999
(defun TeX-remove-style ()
1000
"Remnove all style specific information."
1001
(setq TeX-style-hook-applied-p nil)
1002
(run-hooks 'TeX-remove-style-hooks)
1003
(setq TeX-active-styles (list TeX-virgin-style)))
1005
(defun TeX-style-list ()
1006
"Return a list of all styles (subfils) use by the current document."
1010
;;; Special Characters
1012
(defvar TeX-esc "\\" "The TeX escape character.")
1013
(make-variable-buffer-local 'TeX-esc)
1015
(defvar TeX-grop "{" "The TeX group opening character.")
1016
(make-variable-buffer-local 'TeX-grop)
1018
(defvar TeX-grcl "}" "The TeX group closing character.")
1019
(make-variable-buffer-local 'TeX-grcl)
1023
;; Must be before keymaps.
1025
(defgroup TeX-macro nil
1026
"Support for TeX macros in AUC TeX."
1030
(defcustom TeX-complete-word 'ispell-complete-word
1031
"*Function to call for completing non-macros in tex-mode."
1034
(defvar TeX-complete-list nil
1035
"List of ways to complete the preceding text.
1037
Each entry is a list with the following elements:
1039
0. Regexp matching the preceding text.
1040
1. A number indicating the subgroup in the regexp containing the text.
1041
2. A function returning an alist of possible completions.
1042
3. Text to append after a succesful completion.
1046
0. Regexp matching the preceding text.
1047
1. Function to do the actual completion.")
1049
(defun TeX-complete-symbol ()
1050
"Perform completion on TeX/LaTeX symbol preceding point."
1052
(let ((list TeX-complete-list)
1055
(setq entry (car list)
1057
(if (TeX-looking-at-backward (car entry) 250)
1059
(if (numberp (nth 1 entry))
1060
(let* ((sub (nth 1 entry))
1061
(close (nth 3 entry))
1062
(begin (match-beginning sub))
1063
(end (match-end sub))
1064
(pattern (TeX-match-buffer 0))
1065
(symbol (buffer-substring begin end))
1066
(list (funcall (nth 2 entry)))
1067
(completion (try-completion symbol list)))
1068
(cond ((eq completion t)
1070
(not (looking-at (regexp-quote close)))
1073
(error "Can't find completion for \"%s\"" pattern))
1074
((not (string-equal symbol completion))
1075
(delete-region begin end)
1078
(eq (try-completion completion list) t)
1079
(not (looking-at (regexp-quote close)))
1082
(message "Making completion list...")
1083
(let ((list (all-completions symbol list nil)))
1084
(with-output-to-temp-buffer "*Completions*"
1085
(display-completion-list list)))
1086
(message "Making completion list...done"))))
1087
(funcall (nth 1 entry)))))
1089
(defcustom TeX-default-macro "ref"
1090
"*The default macro when creating new ones with TeX-insert-macro."
1094
(make-variable-buffer-local 'TeX-default-macro)
1096
(defcustom TeX-insert-braces t
1097
"*If non-nil, append a empty pair of braces after inserting a macro."
1101
(defun TeX-insert-macro (symbol)
1102
"Insert TeX macro with completion.
1104
AUC TeX knows of some macros, and may query for extra arguments."
1105
(interactive (list (completing-read (concat "Macro (default "
1109
(TeX-symbol-list))))
1110
(cond ((string-equal symbol "")
1111
(setq symbol TeX-default-macro))
1113
(setq TeX-default-macro symbol)))
1114
(TeX-parse-macro symbol (cdr-safe (assoc symbol (TeX-symbol-list)))))
1116
(defvar TeX-electric-macro-map nil)
1118
(if TeX-electric-macro-map
1120
(setq TeX-electric-macro-map (copy-keymap minibuffer-local-completion-map))
1121
(define-key TeX-electric-macro-map " " 'minibuffer-complete-and-exit))
1123
(defun TeX-electric-macro ()
1124
"Insert TeX macro with completion.
1126
AUC TeX knows of some macros, and may query for extra arguments.
1127
Space will complete and exit."
1129
(cond ((eq (preceding-char) ?\\)
1130
(call-interactively 'self-insert-command))
1131
((eq (preceding-char) ?.)
1132
(let ((TeX-default-macro " ")
1133
(minibuffer-local-completion-map TeX-electric-macro-map))
1134
(call-interactively 'TeX-insert-macro)))
1136
(let ((minibuffer-local-completion-map TeX-electric-macro-map))
1137
(call-interactively 'TeX-insert-macro)))))
1139
(defun TeX-parse-macro (symbol args)
1140
"How to parse TeX macros which takes one or more arguments."
1142
;; First argument is the name of the macro.
1144
;; If called with no additional arguments, insert macro with point
1145
;; inside braces. Otherwise, each argument of this function should
1146
;; match an argument to the TeX macro. What is done depend on the
1149
;; string: Use the string as a prompt to prompt for the argument.
1151
;; number: Insert that many braces, leave point inside the first.
1153
;; nil: Insert empty braces.
1155
;; t: Insert empty braces, leave point between the braces.
1157
;; other symbols: Call the symbol as a function. You can define
1158
;; your own hook, or use one of the predefined argument hooks. If
1159
;; you add new hooks, you can assume that point is placed directly
1160
;; after the previous argument, or after the macro name if this is
1161
;; the first argument. Please leave point located efter the
1162
;; argument you are inserting. If you want point to be located
1163
;; somewhere else after all hooks have been processed, set the value
1164
;; of `exit-mark'. It will point nowhere, until the argument hook
1165
;; set it. By convention, these hook all start with `TeX-arg-'.
1167
;; list: If the car is a string, insert it as a prompt and the next
1168
;; element as initial input. Otherwise, call the car of the list
1169
;; with the remaining elements as arguments.
1171
;; vector: Optional argument. If it has more than one element,
1172
;; parse it as a list, otherwise parse the only element as above.
1173
;; Use square brackets instead of curly braces, and is not inserted
1174
;; on empty user input.
1176
(if (and (TeX-active-mark)
1178
(exchange-point-and-mark))
1179
(insert TeX-esc symbol)
1180
(let ((exit-mark (make-marker))
1182
(TeX-parse-arguments args)
1183
(cond ((marker-position exit-mark)
1184
(goto-char (marker-position exit-mark))
1185
(set-marker exit-mark nil))
1186
((and TeX-insert-braces
1187
(equal position (point))
1188
(string-match "[a-zA-Z]+" symbol)
1191
(if (TeX-active-mark)
1193
(exchange-point-and-mark)
1196
(backward-char))))))
1198
(defun TeX-arg-string (optional &optional prompt input)
1199
"Prompt for a string."
1200
(TeX-argument-insert
1201
(if (and (not optional) (TeX-active-mark))
1202
(let ((TeX-argument (buffer-substring (point) (mark))))
1203
(delete-region (point) (mark))
1205
(read-string (TeX-argument-prompt optional prompt "Text") input))
1208
(defun TeX-parse-arguments (args)
1209
"Parse TeX macro arguments.
1211
See TeX-parse-macro for details."
1212
(let ((last-optional-rejected nil))
1214
(if (vectorp (car args))
1215
(if last-optional-rejected
1217
(let ((< LaTeX-optop)
1219
(TeX-parse-argument t (if (equal (length (car args)) 1)
1221
(append (car args) nil)))))
1224
(setq last-optional-rejected nil)
1225
(TeX-parse-argument nil (car args))))
1226
(setq args (cdr args)))))
1228
(defun TeX-parse-argument (optional arg)
1229
"Depending on OPTIONAL, insert TeX macro argument ARG in curly braces.
1230
If OPTIONAL is set, only insert if there is anything to insert, and
1231
then use square brackets.
1233
See TeX-parse-macro for details."
1235
(cond ((stringp arg)
1236
(TeX-arg-string optional arg))
1240
(TeX-parse-argument optional t)
1242
(TeX-parse-argument optional nil)
1243
(setq arg (- arg 1)))))
1246
(if (and (not optional) (TeX-active-mark))
1247
(exchange-point-and-mark))
1251
(if (and (not optional) (TeX-active-mark))
1252
(exchange-point-and-mark)
1253
(set-marker exit-mark (point)))
1256
(funcall arg optional))
1258
(let ((head (car arg))
1260
(cond ((stringp head)
1261
(apply 'TeX-arg-string optional arg))
1263
(apply head optional tail))
1264
(t (error "Unknown list argument type %s"
1265
(prin1-to-string head))))))
1266
(t (error "Unknown argument type %s" (prin1-to-string arg)))))
1268
(defun TeX-argument-insert (name optional &optional prefix)
1269
"Insert NAME surrounded by curly braces.
1271
If OPTIONAL, only insert it if not empty, and then use square brackets."
1272
(if (and optional (string-equal name ""))
1273
(setq last-optional-rejected t)
1277
(if (and (string-equal name "")
1278
(null (marker-position exit-mark)))
1279
(set-marker exit-mark (point))
1283
(defun TeX-argument-prompt (optional prompt default &optional complete)
1284
"Return a argument prompt.
1286
If OPTIONAL is not nil then the prompt will start with ``(Optional) ''.
1288
PROMPT will be used if not nil, otherwise use DEFAULT.
1290
Unless optional argument COMPLETE is non-nil, ``: '' will be appended."
1291
(concat (if optional "(Optional) " "")
1292
(if prompt prompt default)
1293
(if complete "" ": ")))
1297
(defvar TeX-format-list
1298
'(("JLATEX" japanese-latex-mode
1299
"\\\\\\(documentstyle[^%\n]*{j\\|documentclass[^%\n]*{j\\)")
1300
("JTEX" japanese-plain-tex-mode
1301
"-- string likely in Japanese TeX --")
1302
("AMSTEX" ams-tex-mode
1305
"\\\\\\(begin\\|section\\|chapter\\|documentstyle\\|documentclass\\)\\b")
1306
("TEX" plain-tex-mode "."))
1307
"*List of format packages to consider when choosing a TeX mode.
1309
A list with a entry for each format package available at the site.
1311
Each entry is a list with three elements.
1313
1. The name of the format package.
1314
2. The name of the major mode.
1315
3. A regexp typically matched in the beginning of the file.
1317
When entering tex-mode, each regexp is tried in turn in order to find
1318
when major mode to enter.")
1320
(defcustom TeX-default-mode 'latex-mode
1321
"*Mode to enter for a new file when it can't be determined whether
1322
the file is plain TeX or LaTeX or what."
1324
:type '(radio (function-item latex-mode)
1325
(function-item plain-tex-mode)
1326
(function :tag "Other")))
1328
(defcustom TeX-force-default-mode nil
1329
"*If set to nil, try to infer the mode of the file from its
1334
;; Do not ;;;###autoload because of conflict with standard tex-mode.el.
1336
"Major mode for editing files of input for TeX or LaTeX.
1337
Tries to guess whether this file is for plain TeX or LaTeX.
1339
The algorithm is as follows:
1341
1) if the file is empty or TeX-force-default-mode is not set to nil,
1342
TeX-default-mode is chosen
1343
2) If \\documentstyle or \\begin{, \\section{, \\part{ or \\chapter{ is
1344
found, latex-mode is selected.
1345
3) Otherwise, use plain-tex-mode "
1348
(funcall (if (or (equal (buffer-size) 0)
1349
TeX-force-default-mode)
1352
(goto-char (point-min))
1353
(let ((comment-start-skip ;Used by TeX-in-comment
1355
"\\(\\(^\\|[^\\]\\)\\("
1356
(regexp-quote TeX-esc)
1357
(regexp-quote TeX-esc)
1358
"\\)*\\)\\(%+ *\\)"))
1359
(entry TeX-format-list)
1361
(while (and entry (not answer))
1362
(if (re-search-forward (nth 2 (car entry))
1364
(if (not (TeX-in-comment))
1365
(setq answer (nth 1 (car entry))))
1366
(setq entry (cdr entry))))
1369
TeX-default-mode))))))
1371
;; Do not ;;;###autoload because of conflict with standard tex-mode.el.
1372
(defun plain-tex-mode ()
1373
"Major mode for editing files of input for plain TeX.
1374
See info under AUC TeX for documentation.
1379
Entering plain-tex-mode calls the value of text-mode-hook,
1380
then the value of TeX-mode-hook, and then the value
1381
of plain-TeX-mode-hook."
1383
(plain-TeX-common-initialization)
1384
(setq mode-name "TeX")
1385
(setq major-mode 'plain-tex-mode)
1386
(setq TeX-command-default "TeX")
1387
(run-hooks 'text-mode-hook 'TeX-mode-hook 'plain-TeX-mode-hook))
1390
(defun ams-tex-mode ()
1391
"Major mode for editing files of input for AmS TeX.
1392
See info under AUC TeX for documentation.
1397
Entering AmS-tex-mode calls the value of text-mode-hook,
1398
then the value of TeX-mode-hook, and then the value
1399
of AmS-TeX-mode-hook."
1401
(plain-TeX-common-initialization)
1402
(setq mode-name "AmS TeX")
1403
(setq major-mode 'ams-tex-mode)
1404
(setq TeX-command-default "AmSTeX")
1405
(run-hooks 'text-mode-hook 'TeX-mode-hook 'AmS-TeX-mode-hook))
1407
(autoload 'font-latex-setup "font-latex"
1408
"Font locking optimized for LaTeX.
1409
Should work with all Emacsen." t)
1410
(autoload 'tex-font-setup "tex-font"
1411
"Copy of Emacs 21 standard tex-mode font lock support.
1412
This only works with Emacs 21." t)
1414
(defcustom TeX-install-font-lock 'font-latex-setup
1415
"Function to call to install font lock support.
1416
Choose `ignore' if you don't want AUC TeX to install support for font locking."
1418
:type '(radio (function-item font-latex-setup)
1419
(function-item tex-font-setup)
1420
(function-item ignore)
1421
(function :tag "Other")))
1423
(defun VirTeX-common-initialization ()
1425
(kill-all-local-variables)
1426
(setq local-abbrev-table text-mode-abbrev-table)
1427
(setq indent-tabs-mode nil)
1430
(make-local-variable 'ispell-parser)
1431
(setq ispell-parser 'tex)
1432
(make-local-variable 'ispell-tex-p)
1433
(setq ispell-tex-p t)
1435
;; Redefine some standard varaibles
1436
(make-local-variable 'paragraph-start)
1437
(make-local-variable 'paragraph-separate)
1438
(make-local-variable 'comment-start)
1439
(setq comment-start "%")
1440
(make-local-variable 'comment-start-skip)
1441
(setq comment-start-skip
1443
"\\(\\(^\\|[^\\]\\)\\("
1444
(regexp-quote TeX-esc)
1445
(regexp-quote TeX-esc)
1446
"\\)*\\)\\(%+ *\\)"))
1447
(make-local-variable 'comment-indent-hook)
1448
(setq comment-indent-hook 'TeX-comment-indent)
1449
(make-local-variable 'comment-multi-line)
1450
(setq comment-multi-line nil)
1451
(make-local-variable 'compile-command)
1452
(if (boundp 'compile-command)
1454
(setq compile-command "make"))
1455
(make-local-variable 'words-include-escapes)
1456
(setq words-include-escapes nil)
1458
;; Make TAB stand out
1459
;; (make-local-variable 'buffer-display-table)
1460
;; (setq buffer-display-table (if standard-display-table
1461
;; (copy-sequence standard-display-table)
1462
;; (make-display-table)))
1463
;; (aset buffer-display-table ?\t (apply 'vector (append "<TAB>" nil)))
1465
;; Symbol completion.
1466
(make-local-variable 'TeX-complete-list)
1467
(setq TeX-complete-list
1468
(list (list "\\\\\\([a-zA-Z]*\\)"
1469
1 'TeX-symbol-list (if TeX-insert-braces "{}"))
1470
(list "" TeX-complete-word)))
1472
(funcall TeX-install-font-lock)
1474
;; We want this to be early in the list, so we do not add it before
1475
;; we enter TeX mode the first time.
1476
(if (boundp 'local-write-file-hooks)
1477
(add-hook 'local-write-file-hooks 'TeX-safe-auto-write)
1478
(add-hook 'write-file-hooks 'TeX-safe-auto-write))
1479
(make-local-variable 'TeX-auto-update)
1480
(setq TeX-auto-update t))
1482
(defun plain-TeX-common-initialization ()
1483
;; Common initialization for plain TeX like modes.
1484
(VirTeX-common-initialization)
1485
(use-local-map plain-TeX-mode-map)
1486
(easy-menu-add TeX-mode-menu plain-TeX-mode-map)
1487
(easy-menu-add plain-TeX-mode-menu plain-TeX-mode-map)
1488
(set-syntax-table TeX-mode-syntax-table)
1489
(setq paragraph-start
1492
"\\|" (regexp-quote TeX-esc) "par\\|"
1494
(regexp-quote TeX-esc)
1496
"begin\\|end\\|part\\|chapter\\|"
1497
"section\\|subsection\\|subsubsection\\|"
1498
"paragraph\\|include\\|includeonly\\|"
1499
"tableofcontents\\|appendix\\|label\\|caption\\|"
1500
"\\[\\|\\]" ; display math delimitors
1503
"^[ \t]*\\$\\$" ; display math delimitor
1505
(setq paragraph-separate
1508
(regexp-quote TeX-esc)
1512
(regexp-quote TeX-esc)
1514
"begin\\|end\\|label\\|caption\\|part\\|chapter\\|"
1515
"section\\|subsection\\|subsubsection\\|"
1516
"paragraph\\|include\\|includeonly\\|"
1517
"tableofcontents\\|appendix\\|" (regexp-quote TeX-esc)
1520
(setq TeX-header-end (regexp-quote "%**end of header"))
1521
(setq TeX-trailer-start (regexp-quote (concat TeX-esc "bye")))
1522
(TeX-run-style-hooks "TEX"))
1526
(if (boundp 'hilit-patterns-alist)
1527
(let ((latex-patterns (cdr-safe (assq 'latex-mode hilit-patterns-alist)))
1528
(plain-tex-patterns (cdr-safe (assq 'plain-tex-mode
1529
hilit-patterns-alist))))
1530
(if (and latex-patterns plain-tex-patterns)
1531
(setq hilit-patterns-alist
1532
(append (list (cons 'ams-tex-mode plain-tex-patterns))
1533
hilit-patterns-alist)))))
1537
(defgroup TeX-parse nil
1538
"Parsing TeX files from AUC TeX."
1541
(defvar TeX-auto-parser '((styles TeX-auto-file TeX-run-style-hooks)))
1542
;; Alist of parsed information.
1543
;; Each entry is a list with the following elements:
1545
;; 0. Name of information type.
1546
;; 1. Name of temporary variable used when parsing.
1547
;; 2. Name of function to add information to add to #3.
1548
;; 3. Name of variable holding buffer local information.
1549
;; 4. Name of variable indicating that #3 has changed.
1552
(defconst TeX-auto-parser-temporary 1)
1553
(defconst TeX-auto-parser-add 2)
1554
(defconst TeX-auto-parser-local 3)
1555
(defconst TeX-auto-parser-change 4)
1557
(defun TeX-auto-add-type (name prefix &optional plural)
1558
"Add information about name to the parser using PREFIX.
1560
Optional third argument PLURAL is the plural form of TYPE.
1561
By default just add a `s'.
1563
This function create a set of variables and functions to maintain a
1564
separate type of information in the parser."
1565
(let* ((names (or plural (concat name "s")))
1566
(tmp (intern (concat prefix "-auto-" name)))
1567
(add (intern (concat prefix "-add-" names)))
1568
(local (intern (concat prefix "-" name "-list")))
1569
(change (intern (concat prefix "-" name "-changed"))))
1570
(setq TeX-auto-parser
1571
(cons (list name tmp add local change) TeX-auto-parser))
1573
(make-variable-buffer-local local)
1575
(make-variable-buffer-local change)
1576
(fset add (list 'lambda '(&rest entries)
1577
(concat "Add information about " (upcase name)
1578
" to the current buffer.")
1579
(list 'TeX-auto-add-information name 'entries)))
1580
(fset local (list 'lambda nil
1581
(concat "List of " names
1582
" active in the current buffer.")
1583
(list 'TeX-auto-list-information name)))
1584
(add-hook 'TeX-remove-style-hook
1585
(list 'lambda nil (list 'setq (symbol-name local) nil)))))
1587
(defun TeX-auto-add-information (name entries)
1588
;; For NAME in `TeX-auto-parser' add ENTRIES.
1589
(let* ((entry (assoc name TeX-auto-parser))
1590
(change (nth TeX-auto-parser-change entry))
1591
(change-value (symbol-value change))
1592
(local (nth TeX-auto-parser-local entry))
1593
(local-value (symbol-value local)))
1595
(set local (cons entries local-value))
1597
(set local (list entries local-value)))))
1599
(defun TeX-auto-list-information (name)
1600
;; Return information in `TeX-auto-parser' about NAME.
1602
(let* ((entry (assoc name TeX-auto-parser))
1603
(change (nth TeX-auto-parser-change entry))
1604
(change-value (symbol-value change))
1605
(local (nth TeX-auto-parser-local entry)))
1606
(if (not change-value)
1610
(message "Sorting " name "...")
1612
(sort (mapcar 'TeX-listify (apply 'append (symbol-value local)))
1613
'TeX-car-string-lessp))
1615
(message "Removing duplicates...")
1616
(let ((entry (symbol-value local)))
1617
(while (and entry (cdr entry))
1618
(let ((this (car entry))
1619
(next (car (cdr entry))))
1620
(if (not (string-equal (car this) (car next)))
1621
(setq entry (cdr entry))
1622
;; We have two equal symbols. Use the one with
1624
(if (> (length next) (length this))
1625
(setcdr this (cdr next)))
1626
(setcdr entry (cdr (cdr entry)))))))
1627
(message "Removing duplicates... done"))
1628
(symbol-value local)))
1630
(TeX-auto-add-type "symbol" "TeX")
1632
(defvar TeX-auto-apply-hook nil
1633
"Hook run when a buffer is parsed and the information is applied.")
1635
(defun TeX-auto-apply ()
1636
;; Parse and apply TeX information in the current buffer.
1638
(run-hooks 'TeX-auto-apply-hook)
1639
(mapcar 'TeX-auto-apply-entry TeX-auto-parser))
1641
(defun TeX-auto-apply-entry (entry)
1642
;; Apply the information in an entry in `TeX-auto-parser'.
1643
(let ((value (symbol-value (nth TeX-auto-parser-temporary entry)))
1644
(add (nth TeX-auto-parser-add entry)))
1645
(if value (apply add value))))
1647
(defun TeX-safe-auto-write ()
1648
;; Call TeX-auto-write safely
1649
(condition-case name
1650
(and (boundp 'TeX-auto-update)
1654
;; Continue with the other write file hooks.
1657
(defcustom TeX-auto-save nil
1658
"*Automatically save style information when saving the buffer."
1662
(defcustom TeX-auto-untabify t
1663
"*Automatically untabify when saving the buffer."
1667
(defun TeX-auto-write ()
1668
;; Save all relevant TeX information from the current buffer.
1669
(if TeX-auto-untabify
1670
(untabify (point-min) (point-max)))
1671
(if (and TeX-auto-save TeX-auto-local)
1672
(let* ((file (concat (TeX-master-directory)
1674
(if (string-match "/$" TeX-auto-local) "" "/")
1675
(TeX-strip-extension nil TeX-all-extensions t)
1677
(dir (file-name-directory file)))
1678
;; Create auto directory if possible.
1679
(if (not (file-exists-p dir))
1680
(condition-case name
1681
(make-directory (substring dir 0 -1))
1683
(if (file-writable-p file)
1686
(TeX-auto-store file))
1687
(message "Can't write style information.")))))
1689
(defcustom TeX-macro-default (car-safe TeX-macro-private)
1690
"*Default directory to search for TeX macros."
1694
(defcustom TeX-auto-default (car-safe TeX-auto-private)
1695
"*Default directory to place automatically generated TeX information."
1700
(defun TeX-auto-generate (tex auto)
1701
"Generate style file for TEX and store it in AUTO.
1702
If TEX is a directory, generate style files for all files in the directory."
1703
(interactive (list (setq TeX-macro-default
1704
(expand-file-name (read-file-name
1705
"TeX file or directory: "
1707
TeX-macro-default 'confirm)))
1708
(setq TeX-auto-default
1709
(expand-file-name (read-file-name
1710
"AUTO lisp directory: "
1712
TeX-auto-default 'confirm)))))
1713
(cond ((not (file-readable-p tex)))
1714
((string-match TeX-ignore-file tex))
1715
((file-directory-p tex)
1716
(let ((files (directory-files tex))
1717
(default-directory (concat (if (TeX-directory-absolute-p tex)
1720
(if (string-match "/$" tex)
1723
(TeX-file-recurse (cond ((symbolp TeX-file-recurse)
1725
((zerop TeX-file-recurse)
1727
((1- TeX-file-recurse)))))
1728
(mapcar (function (lambda (file)
1729
(if (or TeX-file-recurse
1730
(not (file-directory-p file)))
1731
(TeX-auto-generate file auto))))
1733
((not (file-newer-than-file-p tex
1734
(concat auto (if (string-match "/$" auto) "" "/")
1735
(TeX-strip-extension tex TeX-all-extensions t) ".el"))))
1736
((TeX-match-extension tex (append TeX-file-extensions
1737
BibTeX-file-extensions))
1739
(set-buffer (find-file-noselect tex))
1740
(message "Parsing %s..." tex)
1741
(TeX-auto-store (concat auto
1742
(if (string-match "/$" auto) "" "/")
1743
(TeX-strip-extension tex
1747
(kill-buffer (current-buffer))
1748
(message "Parsing %s... done" tex)))))
1751
(defun TeX-auto-generate-global ()
1752
"Create global auto directory for global TeX macro definitions."
1754
(if (file-directory-p
1755
(if (string-match "/$" TeX-auto-global)
1756
(substring TeX-auto-global 0 -1)
1759
(make-directory (if (string-match "/$" TeX-auto-global)
1760
(substring TeX-auto-global 0 -1)
1762
(mapcar (function (lambda (macro) (TeX-auto-generate macro TeX-auto-global)))
1764
(byte-recompile-directory TeX-auto-global 0))
1766
(defun TeX-auto-store (file)
1767
;; Extract information for auc tex from current buffer and store it in FILE.
1770
(if (member nil (mapcar 'TeX-auto-entry-clear-p TeX-auto-parser))
1771
(let ((style (TeX-strip-extension nil TeX-all-extensions t)))
1772
(TeX-unload-style style)
1774
(set-buffer (generate-new-buffer file))
1776
(insert "(TeX-add-style-hook \"" style "\"\n"
1779
(mapcar 'TeX-auto-insert TeX-auto-parser)
1781
(write-region (point-min) (point-max) file nil 'silent)
1782
(kill-buffer (current-buffer))))
1783
(if (file-exists-p (concat file "c"))
1784
(delete-file (concat file "c")))
1785
(if (file-exists-p file)
1786
(delete-file file))))
1788
(defun TeX-auto-entry-clear-p (entry)
1789
;; Check if the temporary for `TeX-auto-parser' entry ENTRY is clear.
1790
(null (symbol-value (nth TeX-auto-parser-temporary entry))))
1792
(defun TeX-auto-insert (entry)
1793
;; Insert code to initialize ENTRY from `TeX-auto-parser'.
1794
(let ((name (symbol-name (nth TeX-auto-parser-add entry)))
1795
(list (symbol-value (nth TeX-auto-parser-temporary entry))))
1798
(insert "\n (" name)
1801
(if (stringp (car list))
1802
(insert (prin1-to-string (car list)))
1803
(insert "'" (prin1-to-string (car list))))
1804
(setq list (cdr list)))
1807
(defvar TeX-auto-ignore
1808
'("csname" "filedate" "fileversion" "docdate" "next" "labelitemi"
1809
"labelitemii" "labelitemiii" "labelitemiv" "labelitemv"
1810
"labelenumi" "labelenumii" "labelenumiii" "labelenumiv"
1811
"labelenumv" "theenumi" "theenumii" "theenumiii" "theenumiv"
1812
"theenumv" "document" "par" "do" "expandafter")
1813
"List of symbols to ignore when scanning a TeX style file.")
1815
(defun TeX-auto-add-regexp (regexp)
1816
"Add REGEXP to TeX-auto-regexp-list if not already a member."
1817
(if (symbolp TeX-auto-regexp-list)
1818
(setq TeX-auto-regexp-list (symbol-value TeX-auto-regexp-list)))
1819
(or (memq regexp TeX-auto-regexp-list)
1820
(setq TeX-auto-regexp-list (cons regexp TeX-auto-regexp-list))))
1822
(defvar TeX-auto-empty-regexp-list
1823
'(("<IMPOSSIBLE>\\(\\'\\`\\)" 1 ignore))
1824
"List of regular expressions guaranteed to match nothing.")
1826
(defvar plain-TeX-auto-regexp-list
1827
'(("\\\\def\\\\\\([a-zA-Z]+\\)[^a-zA-Z@]" 1 TeX-auto-symbol-check)
1828
("\\\\let\\\\\\([a-zA-Z]+\\)[^a-zA-Z@]" 1 TeX-auto-symbol-check)
1829
("\\\\font\\\\\\([a-zA-Z]+\\)[^a-zA-Z@]" 1 TeX-auto-symbol)
1830
("\\\\chardef\\\\\\([a-zA-Z]+\\)[^a-zA-Z@]" 1 TeX-auto-symbol)
1831
("\\\\new\\(count\\|dimen\\|muskip\\|skip\\)\\\\\\([a-z]+\\)[^a-zA-Z@]"
1833
("\\\\newfont{?\\\\\\([a-zA-Z]+\\)}?" 1 TeX-auto-symbol)
1834
("\\\\typein\\[\\\\\\([a-zA-Z]+\\)\\]" 1 TeX-auto-symbol)
1835
("\\\\input +\\(\\.*[^#%\\\\\\.\n\r]+\\)\\(\\.[^#%\\\\\\.\n\r]+\\)?"
1837
("\\\\mathchardef\\\\\\([a-zA-Z]+\\)[^a-zA-Z@]" 1 TeX-auto-symbol))
1838
"List of regular expression matching common LaTeX macro definitions.")
1840
(defvar TeX-auto-full-regexp-list plain-TeX-auto-regexp-list
1841
"Full list of regular expression matching TeX macro definitions.")
1843
(defvar TeX-auto-prepare-hook nil
1844
"List of hooks to be called before parsing a TeX file.")
1846
(defvar TeX-auto-cleanup-hook nil
1847
"List of hooks to be called after parsing a TeX file.")
1849
(defcustom TeX-auto-parse-length 999999
1850
"*Maximal length of TeX file that will be parsed."
1853
(make-variable-buffer-local 'TeX-auto-parse-length)
1855
(defcustom TeX-auto-x-parse-length 0
1856
"*Maximum length of TeX file that will be parse additionally.
1857
Use `TeX-auto-x-regexp-list' for parsing the region between
1858
`TeX-auto-parse-length' and this value."
1861
(make-variable-buffer-local 'TeX-auto-x-parse-length)
1863
(defcustom TeX-auto-x-regexp-list 'LaTeX-auto-label-regexp-list
1864
"*List of regular expresions used for additional parsing.
1865
See `TeX-auto-x-parse-length'."
1866
:type '(radio (variable-item TeX-auto-empty-regexp-list)
1867
(variable-item TeX-auto-full-regexp-list)
1868
(variable-item plain-TeX-auto-regexp-list)
1869
(variable-item LaTeX-auto-minimal-regexp-list)
1870
(variable-item LaTeX-auto-label-regexp-list)
1871
(variable-item LaTeX-auto-regexp-list)
1872
(symbol :tag "Other")
1873
(repeat :tag "Specify"
1874
(group (regexp :tag "Match")
1875
(sexp :tag "Groups")
1878
(make-variable-buffer-local 'TeX-auto-x-regexp-list)
1880
(defun TeX-auto-parse-region (regexp-list beg end)
1881
"Parse TeX information in region between BEG and END.
1882
Parse according to REGEXP-LIST."
1883
(if (symbolp regexp-list)
1884
(setq regexp-list (and (boundp regexp-list) (symbol-value regexp-list))))
1886
;; Extract the information.
1887
(let ((regexp (concat "\\("
1888
(mapconcat 'car regexp-list "\\)\\|\\(")
1890
(goto-char (if end (min end (point-max)) (point-max)))
1891
(while (re-search-backward regexp beg t)
1892
(if (TeX-in-comment)
1894
(let* ((entry (TeX-member nil regexp-list
1895
(function (lambda (a b)
1896
(looking-at (nth 0 b))))))
1897
(symbol (nth 2 entry))
1898
(match (nth 1 entry)))
1899
(if (fboundp symbol)
1900
(funcall symbol match)
1901
(set symbol (cons (if (listp match)
1902
(mapcar 'TeX-match-buffer match)
1903
(TeX-match-buffer match))
1904
(symbol-value symbol))))))))))
1906
(defun TeX-auto-parse ()
1907
"Parse TeX information in current buffer.
1909
Call the functions in TeX-auto-prepare-hook before parsing, and the
1910
functions in TeX-auto-cleanup-hook after parsing."
1912
(let ((case-fold-search nil))
1914
(mapcar 'TeX-auto-clear-entry TeX-auto-parser)
1915
(run-hooks 'TeX-auto-prepare-hook)
1918
(and (> TeX-auto-x-parse-length TeX-auto-parse-length)
1919
(> (point-max) TeX-auto-parse-length)
1920
(TeX-auto-parse-region TeX-auto-x-regexp-list
1921
TeX-auto-parse-length
1922
TeX-auto-x-parse-length))
1923
(TeX-auto-parse-region TeX-auto-regexp-list
1924
nil TeX-auto-parse-length))
1926
;; Cleanup ignored symbols.
1928
;; NOTE: This is O(N M) where it could be O(N log N + M log M) if we
1929
;; sorted the lists first.
1930
(while (member (car TeX-auto-symbol) TeX-auto-ignore)
1931
(setq TeX-auto-symbol (cdr TeX-auto-symbol)))
1932
(let ((list TeX-auto-symbol))
1933
(while (and list (cdr list))
1934
(if (member (car (cdr list)) TeX-auto-ignore)
1935
(setcdr list (cdr (cdr list)))
1936
(setq list (cdr list)))))
1938
(run-hooks 'TeX-auto-cleanup-hook)))
1940
(defun TeX-auto-clear-entry (entry)
1941
;; Set the temporary variable in ENTRY to nil.
1942
(set (nth TeX-auto-parser-temporary entry) nil))
1944
(defvar LaTeX-auto-end-symbol nil)
1946
(defun TeX-auto-symbol-check (match)
1947
"Add MATCH to TeX-auto-symbols.
1948
Check for potential LaTeX environments."
1949
(let ((symbol (if (listp match)
1950
(mapcar 'TeX-match-buffer match)
1951
(TeX-match-buffer match))))
1952
(if (and (stringp symbol)
1953
(string-match "^end\\(.+\\)$" symbol))
1954
(setq LaTeX-auto-end-symbol
1955
(cons (substring symbol (match-beginning 1) (match-end 1))
1956
LaTeX-auto-end-symbol))
1957
(setq TeX-auto-symbol (cons symbol TeX-auto-symbol)))))
1961
;; Some of these functions has little to do with TeX, but nonetheless we
1962
;; should use the "TeX-" prefix to avoid name clashes.
1964
(defcustom TeX-auto-regexp-list 'TeX-auto-full-regexp-list
1965
"*List of regular expresions used for parsing the current file."
1966
:type '(radio (variable-item TeX-auto-empty-regexp-list)
1967
(variable-item TeX-auto-full-regexp-list)
1968
(variable-item plain-TeX-auto-regexp-list)
1969
(variable-item LaTeX-auto-minimal-regexp-list)
1970
(variable-item LaTeX-auto-label-regexp-list)
1971
(variable-item LaTeX-auto-regexp-list)
1972
(symbol :tag "Other")
1973
(repeat :tag "Specify"
1974
(group (regexp :tag "Match")
1975
(sexp :tag "Groups")
1978
(make-variable-buffer-local 'TeX-auto-regexp-list)
1980
(defgroup TeX-file-extension nil
1981
"File extensions recognized by AUC TeX."
1984
(defcustom TeX-file-extensions '("tex" "sty" "cls" "ltx" "texi" "texinfo")
1985
"*File extensions used by manually generated TeX files."
1986
:group 'TeX-file-extension
1987
:type '(repeat (string :format "%v")))
1989
(defcustom TeX-all-extensions '("[^.\n]+")
1990
"All possible file extensions."
1991
:group 'TeX-file-extension
1992
:type '(repeat (regexp :format "%v")))
1994
(defcustom TeX-default-extension "tex"
1995
"*Default extension for TeX files."
1996
:group 'TeX-file-extension
1999
(make-variable-buffer-local 'TeX-default-extension)
2001
(defcustom BibTeX-file-extensions '("bib")
2002
"Valid file extensions for BibTeX files."
2003
:group 'TeX-file-extension
2004
:type '(repeat (string :format "%v")))
2006
(defcustom BibTeX-style-extensions '("bst")
2007
"Valid file extensions for BibTeX styles."
2008
:group 'TeX-file-extension
2009
:type '(repeat (string :format "%v")))
2011
(defcustom TeX-ignore-file "\\(^\\|/\\)\\(\\.\\|\\.\\.\\|RCS\\|SCCS\\|CVS\\|babel\\..*\\)$"
2012
"Regular expression matching file names to ignore.
2014
These files or directories will not be considered when searching for
2015
TeX files in a directory."
2019
(defcustom TeX-file-recurse t
2020
"*Whether to search TeX directories recursively: 'nil' means do not
2021
recurse, a positive integer means go that far deep in the directory
2022
hierarchy, 't' means recurse indefinitely."
2024
:type '(choice (const :tag "On" t)
2025
(const :tag "Off" nil)
2026
(integer :tag "Depth" :value 1)))
2028
(defun TeX-match-extension (file &optional extensions)
2029
"Return non-nil if FILE has an one of EXTENSIONS.
2031
If EXTENSIONS is not specified or nil, the value of
2032
TeX-file-extensions is used instead."
2034
(if (null extensions)
2035
(setq extensions TeX-file-extensions))
2037
(let ((regexp (concat "\\.\\("
2038
(mapconcat 'identity extensions "\\|")
2040
(case-fold-search t))
2041
(string-match regexp file)))
2043
(defun TeX-strip-extension (&optional string extensions nodir nostrip)
2044
"Return STRING without any trailing extension in EXTENSIONS.
2045
If NODIR is `t', also remove directory part of STRING.
2046
If NODIR is `path', remove directory part of STRING if it is equal to
2047
the current directory, TeX-macro-private or TeX-macro-global.
2048
If NOSTRIP is set, do not remove extension after all.
2049
STRING defaults to the name of the current buffer.
2050
EXTENSIONS defaults to TeX-file-extensions."
2053
(setq string (or (buffer-file-name) "<none>")))
2055
(if (null extensions)
2056
(setq extensions TeX-file-extensions))
2058
(let* ((strip (if (and (not nostrip)
2059
(TeX-match-extension string extensions))
2060
(substring string 0 (match-beginning 0))
2062
(dir (file-name-directory (expand-file-name strip))))
2063
(if (or (eq nodir t)
2064
(string-equal dir (expand-file-name "./"))
2065
(member dir TeX-macro-global)
2066
(member dir TeX-macro-private))
2067
(file-name-nondirectory strip)
2070
(defun TeX-search-files (&optional directories extensions nodir strip)
2071
"Return a list of all reachable files in DIRECTORIES ending with EXTENSIONS.
2072
If optional argument NODIR is set, remove directory part.
2073
If optional argument STRIP is set, remove file extension.
2074
If optional argument DIRECTORIES is set, search in those directories.
2075
Otherwise, search in all TeX macro directories.
2076
If optional argument EXTENSIONS is not set, use TeX-file-extensions"
2078
(if (null extensions)
2079
(setq extensions TeX-file-extensions))
2081
(if (null directories)
2083
(cons "./" (append TeX-macro-private TeX-macro-global))))
2086
(TeX-file-recurse (cond ((symbolp TeX-file-recurse)
2088
((zerop TeX-file-recurse)
2090
((1- TeX-file-recurse)))))
2092
(let* ((directory (car directories))
2093
(content (and directory
2094
(file-readable-p directory)
2095
(file-directory-p directory)
2096
(directory-files directory))))
2098
(setq directories (cdr directories))
2101
(let ((file (concat directory (car content))))
2103
(setq content (cdr content))
2104
(cond ((string-match TeX-ignore-file file))
2105
((not (file-readable-p file)))
2106
((file-directory-p file)
2107
(if TeX-file-recurse
2110
(TeX-search-files (list (concat file "/"))
2113
((TeX-match-extension file extensions)
2114
(setq match (cons (TeX-strip-extension file
2122
(defun TeX-car-string-lessp (a b)
2123
(string-lessp (car a) (car b)))
2125
(defun TeX-listify (a)
2126
(if (listp a) a (list a)))
2128
(defun TeX-member (elt list how)
2129
"Returns the member ELT in LIST. Comparison done with HOW.
2131
Return nil if ELT is not a member of LIST."
2132
(while (and list (not (funcall how elt (car list))))
2133
(setq list (cdr list)))
2136
(defun TeX-assoc (elem list)
2137
"Like assoc, except case incentive."
2138
(let ((case-fold-search t))
2139
(TeX-member elem list
2140
(function (lambda (a b)
2141
(string-match (concat "^" (regexp-quote a) "$")
2144
(defun TeX-match-buffer (n)
2145
"Return the substring corresponding to the N'th match.
2146
See match-data for details."
2147
(if (match-beginning n)
2148
(let ((str (buffer-substring (match-beginning n) (match-end n))))
2149
(set-text-properties 0 (length str) nil str)
2150
(copy-sequence str))
2153
(defun TeX-function-p (arg)
2154
"Return non-nil if ARG is callable as a function."
2155
(or (and (fboundp 'byte-code-function-p)
2156
(byte-code-function-p arg))
2158
(eq (car arg) 'lambda))
2162
(defun TeX-looking-at-backward (regexp &optional limit)
2163
;; Return non-nil if the text before point matches REGEXP.
2164
;; Optional second argument LIMIT gives a max number of characters
2165
;; to look backward for.
2166
(let ((pos (point)))
2168
(and (re-search-backward regexp
2169
(if limit (max (point-min) (- (point) limit)))
2171
(eq (match-end 0) pos)))))
2173
(defun TeX-current-line ()
2174
"The current line number."
2175
(format "%d" (1+ (TeX-current-offset))))
2177
(defun TeX-current-file-name-nondirectory ()
2178
"Return current filename, without path."
2179
(file-name-nondirectory buffer-file-name))
2184
(defvar TeX-mode-syntax-table (make-syntax-table)
2185
"Syntax table used while in TeX mode.")
2187
(make-variable-buffer-local 'TeX-mode-syntax-table)
2189
(progn ; Define TeX-mode-syntax-table.
2190
(modify-syntax-entry (string-to-char TeX-esc)
2191
"\\" TeX-mode-syntax-table)
2192
(modify-syntax-entry ?\f ">" TeX-mode-syntax-table)
2193
(modify-syntax-entry ?\n ">" TeX-mode-syntax-table)
2194
(modify-syntax-entry (string-to-char TeX-grop)
2195
(concat "(" TeX-grcl)
2196
TeX-mode-syntax-table)
2197
(modify-syntax-entry (string-to-char TeX-grcl)
2198
(concat ")" TeX-grop)
2199
TeX-mode-syntax-table)
2200
(modify-syntax-entry ?% "<" TeX-mode-syntax-table)
2201
(modify-syntax-entry ?\" "." TeX-mode-syntax-table)
2202
(modify-syntax-entry ?& "." TeX-mode-syntax-table)
2203
(modify-syntax-entry ?_ "." TeX-mode-syntax-table)
2204
(modify-syntax-entry ?@ "_" TeX-mode-syntax-table)
2205
(modify-syntax-entry ?~ " " TeX-mode-syntax-table)
2206
(modify-syntax-entry ?$ "$" TeX-mode-syntax-table)
2207
(modify-syntax-entry ?' "w" TeX-mode-syntax-table))
2211
(defvar TeX-command-current 'TeX-command-master)
2212
;; Function used to run external command.
2214
(defun TeX-command-select-master ()
2216
(message "Next command will be on the master file")
2217
(setq TeX-command-current 'TeX-command-master))
2219
(defun TeX-command-select-buffer ()
2221
(message "Next command will be on the buffer")
2222
(setq TeX-command-current 'TeX-command-buffer))
2224
(defun TeX-command-select-region ()
2226
(message "Next command will be on the region")
2227
(setq TeX-command-current 'TeX-command-region))
2229
(defvar TeX-command-force nil)
2230
;; If non-nil, TeX-command-query will return the value of this
2231
;; variable instead of quering the user.
2233
(defun TeX-command-menu (name)
2234
;; Execute TeX-command-list NAME from a menu.
2235
(let ((TeX-command-force name))
2236
(funcall TeX-command-current)))
2238
(defun TeX-command-menu-print (printer command name)
2239
;; Print on PRINTER using method COMMAND to run NAME.
2240
(let ((TeX-printer-default printer)
2241
(TeX-printer-list nil)
2242
(TeX-print-command command))
2243
(TeX-command-menu name)))
2245
(defun TeX-command-menu-printer-entry (entry)
2246
;; Return TeX-printer-list ENTRY as a menu item.
2247
(vector (nth 0 entry)
2248
(list 'TeX-command-menu-print
2250
(or (nth lookup entry) command)
2254
;; Begin fix part 1 by Ulrik Dickow <dickow@nbi.dk> 16-Feb-1996,
2255
;; to make queue command usable. Easy but ugly code duplication again.
2257
(defun TeX-command-menu-queue (printer command name)
2258
;; Show queue for PRINTER using method COMMAND to run NAME.
2259
(let ((TeX-printer-default printer)
2260
(TeX-printer-list nil)
2261
(TeX-queue-command command))
2262
(TeX-command-menu name)))
2264
(defun TeX-command-menu-queue-entry (entry)
2265
;; Return TeX-printer-list ENTRY as a menu item.
2266
(vector (nth 0 entry)
2267
(list 'TeX-command-menu-queue
2269
(or (nth lookup entry) command)
2275
(defun TeX-command-menu-entry (entry)
2276
;; Return TeX-command-list ENTRY as a menu item.
2277
(let ((name (car entry)))
2278
(cond ((and (string-equal name TeX-command-Print)
2280
(let ((command TeX-print-command)
2282
(append (list TeX-command-Print)
2283
(mapcar 'TeX-command-menu-printer-entry
2284
TeX-printer-list))))
2285
((and (string-equal name TeX-command-Queue)
2287
(let ((command TeX-queue-command)
2289
(append (list TeX-command-Queue)
2290
(mapcar 'TeX-command-menu-queue-entry ; dickow fix part 2.
2291
TeX-printer-list))))
2293
(vector name (list 'TeX-command-menu name) t)))))
2297
(defcustom TeX-electric-escape nil
2298
"If this is non-nil when AUC TeX is loaded, the TeX escape
2299
character ``\\'' will be bound to `TeX-electric-macro'."
2303
(defvar TeX-mode-map nil
2304
"Keymap for common TeX and LaTeX commands.")
2308
(setq TeX-mode-map (make-sparse-keymap))
2311
;; (define-key TeX-mode-map "\177" 'backward-delete-char-untabify)
2312
(define-key TeX-mode-map "\C-c}" 'up-list)
2313
(define-key TeX-mode-map "\C-c#" 'TeX-normal-mode)
2314
(define-key TeX-mode-map "\C-c\C-n" 'TeX-normal-mode)
2315
(define-key TeX-mode-map "\C-c?" 'describe-mode)
2316
(define-key TeX-mode-map "\C-c\C-i" 'TeX-goto-info-page)
2319
(define-key TeX-mode-map "\"" 'TeX-insert-quote)
2320
(define-key TeX-mode-map "$" 'TeX-insert-dollar)
2321
;; Removed because LaTeX 2e have a better solution to italic correction.
2322
;; (define-key TeX-mode-map "." 'TeX-insert-punctuation)
2323
;; (define-key TeX-mode-map "," 'TeX-insert-punctuation)
2324
(define-key TeX-mode-map "\C-c{" 'TeX-insert-braces)
2325
(define-key TeX-mode-map "\C-c\C-f" 'TeX-font)
2326
(define-key TeX-mode-map "\C-c\C-m" 'TeX-insert-macro)
2327
(if TeX-electric-escape
2328
(define-key TeX-mode-map "\\" 'TeX-electric-macro))
2329
(define-key TeX-mode-map "\e\t" 'TeX-complete-symbol) ;*** Emacs 19 way
2331
(define-key TeX-mode-map "\C-c;" 'TeX-comment-region)
2332
(define-key TeX-mode-map "\C-c%" 'TeX-comment-paragraph)
2334
(define-key TeX-mode-map "\C-c'" 'TeX-comment-paragraph) ;*** Old way
2335
(define-key TeX-mode-map "\C-c:" 'TeX-un-comment-region) ;*** Old way
2336
(define-key TeX-mode-map "\C-c\"" 'TeX-un-comment) ;*** Old way
2339
(define-key TeX-mode-map "\C-c\C-d" 'TeX-save-document)
2340
(define-key TeX-mode-map "\C-c\C-r" 'TeX-command-region)
2341
(define-key TeX-mode-map "\C-c\C-b" 'TeX-command-buffer)
2342
(define-key TeX-mode-map "\C-c\C-c" 'TeX-command-master)
2343
(define-key TeX-mode-map "\C-c\C-k" 'TeX-kill-job)
2344
(define-key TeX-mode-map "\C-c\C-l" 'TeX-recenter-output-buffer)
2345
(define-key TeX-mode-map "\C-c^" 'TeX-home-buffer)
2346
(define-key TeX-mode-map "\C-c`" 'TeX-next-error)
2347
(define-key TeX-mode-map "\C-c\C-w" 'TeX-toggle-debug-boxes))
2349
(easy-menu-define TeX-mode-menu
2351
"Menu used in TeX mode."
2352
(append '("Command")
2354
[ "Master File" TeX-command-select-master
2355
:keys "C-c C-c" :style radio
2356
:selected (eq TeX-command-current 'TeX-command-master) ]
2357
[ "Buffer" TeX-command-select-buffer
2358
:keys "C-c C-b" :style radio
2359
:selected (eq TeX-command-current 'TeX-command-buffer) ]
2360
[ "Region" TeX-command-select-region
2361
:keys "C-c C-r" :style radio
2362
:selected (eq TeX-command-current 'TeX-command-region) ]))
2363
(let ((file 'TeX-command-on-current))
2364
(mapcar 'TeX-command-menu-entry TeX-command-list))))
2366
(defvar plain-TeX-mode-map (copy-keymap TeX-mode-map)
2367
"Keymap used in plain TeX mode.")
2369
(easy-menu-define plain-TeX-mode-menu
2371
"Menu used in plain TeX mode."
2373
["Macro..." TeX-insert-macro t]
2374
["Complete" TeX-complete-symbol t]
2375
["Save Document" TeX-save-document t]
2376
["Next Error" TeX-next-error t]
2377
["Kill Job" TeX-kill-job t]
2378
["Debug Bad Boxes" TeX-toggle-debug-boxes
2379
:style toggle :selected TeX-debug-bad-boxes ]
2380
["Switch to Original File" TeX-home-buffer t]
2381
["Recenter Output Buffer" TeX-recenter-output-buffer t]
2382
;; ["Uncomment" TeX-un-comment t]
2383
["Uncomment Region" TeX-un-comment-region t]
2384
;; ["Comment Paragraph" TeX-comment-paragraph t]
2385
["Comment Region" TeX-comment-region t]
2386
["Switch to Master file" TeX-home-buffer t]
2387
["Documentation" TeX-goto-info-page t]
2388
["Submit bug report" TeX-submit-bug-report t]
2389
["Reset Buffer" TeX-normal-mode t]
2390
["Reset AUC TeX" (TeX-normal-mode t) :keys "C-u C-c C-n"]))
2394
(defun TeX-un-comment-region (start end level)
2395
"Remove up to LEVEL comment characters from each line in the region."
2396
(interactive "*r\np")
2397
(comment-region start end (- level)))
2399
(defun TeX-un-comment (level)
2400
"Delete up to LEVEL %'s from the beginning of each line in a comment."
2403
; Find first comment line
2404
(re-search-backward (concat "^[^" comment-start "]") nil 'limit)
2405
(let ((beg (point)))
2407
; Find last comment line
2408
(re-search-forward (concat "^[^" comment-start "]") nil 'limit)
2410
(comment-region beg (point) (- level)))))
2412
(fset 'TeX-comment-region 'comment-region)
2414
(defun TeX-comment-paragraph (level)
2415
"Inserts LEVEL %'s at the beginning of every line in the current paragraph."
2418
(TeX-un-comment (- level))
2421
(comment-region (point) (mark) level))))
2423
(defun TeX-in-comment ()
2424
;; Return non-nil if point is in a comment.
2426
(null comment-start-skip)
2427
(eq (preceding-char) ?\r))
2430
(let ((pos (point)))
2431
(re-search-backward "^\\|\r" nil t)
2432
(or (looking-at comment-start-skip)
2433
(re-search-forward comment-start-skip pos t))))))
2437
(defgroup TeX-indentation nil
2438
"Indentation of TeX buffers in AUC TeX."
2441
(defun TeX-brace-count-line ()
2442
"Count number of open/closed braces."
2446
(narrow-to-region (point)
2448
(re-search-forward "[^\\\\]%\\|\n\\|\\'")
2452
(while (re-search-forward "\\({\\|}\\|\\\\.\\)" nil t)
2454
((string= "{" (TeX-match-buffer 1))
2455
(setq count (+ count TeX-brace-indent-level)))
2456
((string= "}" (TeX-match-buffer 1))
2457
(setq count (- count TeX-brace-indent-level)))))
2460
(defcustom TeX-brace-indent-level 2
2461
"*The level of indentation produced by a open brace."
2462
:group 'TeX-indentation
2465
(defun TeX-comment-indent ()
2466
(if (looking-at "%%%")
2468
(skip-chars-backward " \t")
2469
(max (if (bolp) 0 (1+ (current-column)))
2474
(defcustom TeX-font-list '((?\C-b "{\\bf " "}")
2475
(?\C-c "{\\sc " "}")
2476
(?\C-e "{\\em " "\\/}")
2477
(?\C-i "{\\it " "\\/}")
2478
(?\C-r "{\\rm " "}")
2479
(?\C-s "{\\sl " "\\/}")
2480
(?\C-t "{\\tt " "}")
2482
"List of fonts used by TeX-font.
2484
Each entry is a list.
2485
The first element is the key to activate the font.
2486
The second element is the string to insert before point, and the third
2487
element is the string to insert after point.
2488
If the fourth and fifth element are strings, they specify the prefix and
2489
suffix to be used in math mode.
2490
An optional fourth (or sixth) element means always replace if t."
2494
:value (?\C-a "" "")
2495
(character :tag "Key")
2496
(string :tag "Prefix")
2497
(string :tag "Suffix")
2500
(string :tag "Math Prefix")
2501
(string :tag "Math Suffix")))
2502
(option (sexp :format "Replace\n" :value t)))))
2504
(defvar TeX-font-replace-function 'TeX-font-replace
2505
"Determines the function which is called when a font should be replaced.")
2507
(defun TeX-describe-font-entry (entry)
2508
;; A textual description of an ENTRY in TeX-font-list.
2509
(concat (format "%16s " (key-description (char-to-string (nth 0 entry))))
2510
(if (or (eq t (nth 3 entry)) (eq t (nth 5 entry)))
2512
(format "%14s %-3s %14s %-3s"
2513
(nth 1 entry) (nth 2 entry)
2514
(if (stringp (nth 3 entry)) (nth 3 entry) "")
2515
(if (stringp (nth 4 entry)) (nth 4 entry) "")))))
2517
(defun TeX-font (replace what)
2518
"Insert template for font change command.
2519
If REPLACE is not nil, replace current font. WHAT determines the font
2520
to use, as specified by TeX-font-list."
2521
(interactive "*P\nc")
2523
(let* ((entry (assoc what TeX-font-list))
2524
(in-math (texmathp))
2525
(before (nth 1 entry))
2526
(after (nth 2 entry)))
2527
(setq replace (or replace (eq t (nth 3 entry)) (eq t (nth 5 entry))))
2528
(if (and in-math (stringp (nth 3 entry)))
2529
(setq before (nth 3 entry)
2530
after (nth 4 entry)))
2534
"KEY TEXTFONT MATHFONT\n\n"
2535
(mapconcat 'TeX-describe-font-entry
2536
TeX-font-list "\n"))))
2537
(with-output-to-temp-buffer "*Help*"
2538
(set-buffer "*Help*")
2541
(funcall TeX-font-replace-function before after))
2544
(cond ((> (mark) (point))
2557
(defun TeX-font-replace (start end)
2558
"Replace font specification around point with START and END.
2559
For modes with font specifications like `{\\font text}'.
2560
See also `TeX-font-replace-macro' and `TeX-font-replace-function'."
2562
(while (not (looking-at "{\\\\[a-zA-Z]+ "))
2566
(replace-match start t t))
2569
(if (looking-at (regexp-quote "\\/}"))
2574
(delete-backward-char 1))
2577
(defun TeX-font-replace-macro (start end)
2578
"Replace font specification around point with START and END.
2579
For modes with font specifications like `\\font{text}'.
2580
See also `TeX-font-replace' and `TeX-font-replace-function'."
2581
(let ((font-list TeX-font-list)
2582
cmds strings regexp)
2584
(setq strings (cdr (car font-list))
2585
font-list (cdr font-list))
2586
(and (stringp (car strings)) (null (string= (car strings) ""))
2587
(setq cmds (cons (car strings) cmds)))
2588
(setq strings (cdr (cdr strings)))
2589
(and (stringp (car strings)) (null (string= (car strings) ""))
2590
(setq cmds (cons (car strings) cmds))))
2591
(setq regexp (mapconcat 'regexp-quote cmds "\\|"))
2595
(if (/= ?\\ (following-char))
2596
(skip-chars-backward "a-zA-Z "))
2597
(skip-chars-backward (regexp-quote TeX-esc))
2598
(if (looking-at regexp)
2603
(replace-match start t t))
2604
(delete-backward-char 1)
2609
;; Rewritten from scratch with use of `texmathp' by
2610
;; Carsten Dominik <dominik@strw.leidenuniv.nl>
2612
(defvar TeX-symbol-marker nil)
2614
(defvar TeX-symbol-marker-pos 0)
2616
;; The following constants are no longer used, but kept in case some
2617
;; foreign code uses any of them.
2618
(defvar TeX-dollar-sign ?$
2619
"*Character used to enter and leave math mode in TeX.")
2620
(defconst TeX-dollar-string (char-to-string TeX-dollar-sign))
2621
(defconst TeX-dollar-regexp
2622
(concat "^" (regexp-quote TeX-dollar-string) "\\|[^" TeX-esc "]"
2623
(regexp-quote TeX-dollar-string)))
2625
(defun TeX-insert-dollar (&optional arg)
2626
"Insert dollar sign.
2628
If current math mode was not entered with a dollar, refuse to insert one.
2629
Show matching dollar sign if this dollar sign ends the TeX math mode.
2630
Ensure double dollar signs match up correctly by inserting extra
2631
dollar signs when needed.
2633
With raw C-u prefix, insert exactly one dollar sign.
2634
With optional ARG, insert that many dollar signs."
2637
((and arg (listp arg))
2638
;; C-u always inserts one
2641
;; Numerical arg inserts that many
2642
(insert (make-string (prefix-numeric-value arg) ?\$)))
2643
((TeX-point-is-escaped)
2644
;; This is escaped with `\', so just insert one.
2647
;; We are inside math mode
2648
(if (and (stringp (car texmathp-why))
2649
(string-equal (substring (car texmathp-why) 0 1) "\$"))
2650
;; Math mode was turned on with $ or $$ - so finish it accordingly.
2652
(insert (car texmathp-why))
2654
(goto-char (cdr texmathp-why))
2655
(if (pos-visible-in-window-p)
2657
(message "Matches %s"
2658
(buffer-substring (point)
2659
(progn (end-of-line) (point)))))))
2660
;; Math mode was not entered with dollar - we cannot finish it with one.
2661
(error "Math mode because of `%s'. Use `C-q $' to force a dollar."
2662
(car texmathp-why))))
2664
;; Just somewhere in the text.
2667
(defun TeX-point-is-escaped ()
2668
;; Count backslashes before point and return t if number is odd.
2671
(while (equal (preceding-char) ?\\)
2674
(setq odd (not odd)))))
2679
(defun TeX-normal-mode (arg)
2680
"Remove all information about this buffer, and apply the style hooks again.
2681
Save buffer first including style information.
2682
With optional argument, also reload the style hooks."
2685
(setq TeX-style-hook-list nil
2686
BibTeX-global-style-files nil
2687
BibTeX-global-files nil
2688
TeX-global-input-files nil))
2689
(let ((TeX-auto-save t))
2690
(if (buffer-modified-p)
2696
(defgroup TeX-quote nil
2697
"Quoting in AUC TeX."
2700
(defcustom TeX-open-quote "``"
2701
"*String inserted by typing \\[TeX-insert-quote] to open a quotation."
2705
(defcustom TeX-close-quote "''"
2706
"*String inserted by typing \\[TeX-insert-quote] to close a quotation."
2710
(defcustom TeX-quote-after-quote nil
2711
"*Behaviour of \\[TeX-insert-quote]. Nil means standard behaviour;
2712
when non-nil, opening and closing quotes are inserted only after \"."
2717
(defun TeX-insert-quote (arg)
2718
"Insert the appropriate quote marks for TeX.
2719
Inserts the value of `TeX-open-quote' (normally ``) or `TeX-close-quote'
2720
\(normally '') depending on the context. If `TeX-quote-after-quote'
2721
is non-nil, this insertion works only after \".
2722
With prefix argument, always inserts \" characters."
2725
(self-insert-command (prefix-numeric-value arg))
2727
(if TeX-quote-after-quote
2728
(insert (cond ((bobp)
2730
((not (= (preceding-char) ?\"))
2735
(delete-backward-char 1)
2738
(forward-char -2) ;;; at -1 there is double quote
2739
(looking-at "[ \t\n]\\|\\s("))
2740
(delete-backward-char 1)
2743
(delete-backward-char 1)
2745
(insert (cond ((bobp)
2747
((= (preceding-char) (string-to-char TeX-esc))
2749
((= (preceding-char) ?\")
2752
(forward-char (- (length TeX-open-quote)))
2753
(looking-at (regexp-quote TeX-open-quote)))
2754
(delete-backward-char (length TeX-open-quote))
2757
(forward-char (- (length TeX-close-quote)))
2758
(looking-at (regexp-quote TeX-close-quote)))
2759
(delete-backward-char (length TeX-close-quote))
2763
(looking-at "[ \t\n]\\|\\s("))
2766
TeX-close-quote))))))
2768
;; For the sake of BibTeX...
2769
;;; Do not ;;;###autoload because of conflict with standard tex-mode.el.
2770
(fset 'tex-insert-quote 'TeX-insert-quote)
2772
(defun TeX-insert-punctuation ()
2773
"Insert point or comma, cleaning up preceding space."
2776
(if (TeX-looking-at-backward "\\\\/\\(}+\\)" 50)
2777
(replace-match "\\1" t))
2778
(call-interactively 'self-insert-command))
2780
(defun TeX-insert-braces (arg)
2781
"Make a pair of braces around next ARG sexps and leave point inside.
2782
No argument is equivalent to zero: just insert braces and leave point
2787
(if arg (forward-sexp (prefix-numeric-value arg)))
2790
(defun TeX-goto-info-page ()
2791
"Read documentation for AUC TeX in the info system."
2794
(Info-goto-node "(auctex)"))
2797
(defun TeX-submit-bug-report ()
2798
"Submit via mail a bug report on AUC TeX"
2801
(reporter-submit-bug-report
2802
"auc-tex@sunsite.dk"
2803
(concat "AUC TeX " AUC-TeX-version)
2804
(list 'window-system
2811
"Remember to cover the basics, that is, what you expected to happen and
2812
what in fact did happen."))
2816
;; The FSF ispell.el use this.
2817
(defun ispell-tex-buffer-p ()
2818
(and (boundp 'ispell-tex-p) ispell-tex-p))
2820
;; The FSF ispell.el might one day use this.
2821
(setq ispell-enable-tex-parser t)
2823
(defun TeX-run-ispell (command string file)
2824
"Run ispell on current TeX buffer."
2825
(cond ((and (string-equal file (TeX-region-file))
2826
(fboundp 'ispell-region))
2827
(call-interactively 'ispell-region))
2828
((string-equal file (TeX-region-file))
2829
(call-interactively 'spell-region))
2830
((fboundp 'ispell-buffer)
2837
;; Some versions of ispell 3 use this.
2838
(defvar ispell-tex-major-modes nil)
2839
(setq ispell-tex-major-modes
2840
(append '(plain-tex-mode ams-tex-mode latex-mode)
2841
ispell-tex-major-modes))
2845
;;; tex.el ends here