~ubuntu-branches/ubuntu/natty/auctex/natty

« back to all changes in this revision

Viewing changes to tex.el

  • Committer: Bazaar Package Importer
  • Author(s): Davide G. M. Salvetti
  • Date: 2002-01-18 16:30:43 UTC
  • Revision ID: james.westby@ubuntu.com-20020118163043-tfgs96q1xrtshsq1
Tags: upstream-11.06
ImportĀ upstreamĀ versionĀ 11.06

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
;;; tex.el --- Support for TeX documents.
 
2
 
 
3
;; Maintainer: Per Abrahamsen <auc-tex@sunsite.dk>
 
4
;; Version: 11.06
 
5
;; Keywords: wp
 
6
;; X-URL: http://sunsite.dk/auctex
 
7
 
 
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 
 
12
;; 
 
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)
 
16
;; any later version.
 
17
;; 
 
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.
 
22
;; 
 
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.
 
26
 
 
27
;;; Code:
 
28
 
 
29
(when (< emacs-major-version 20)
 
30
  (error "AUC TeX requires Emacs 20 or later"))
 
31
 
 
32
(require 'custom)
 
33
(eval-when-compile (require 'cl))
 
34
 
 
35
(defgroup AUC-TeX nil
 
36
  "A (La)TeX environment."
 
37
  :tag "AUC TeX"
 
38
  :link '(custom-manual "(auctex)Top")
 
39
  :link '(url-link :tag "Home Page" "http://sunsite.dk/auctex/")
 
40
  :prefix "TeX-"
 
41
  :group 'tex)
 
42
 
 
43
(defgroup TeX-file nil
 
44
  "Files used by AUC TeX."
 
45
  :group 'AUC-TeX)
 
46
 
 
47
(defgroup TeX-command nil
 
48
  "Calling external commands from AUC TeX."
 
49
  :group 'AUC-TeX)
 
50
 
 
51
(defgroup LaTeX nil
 
52
  "LaTeX support in AUC TeX."
 
53
  :tag "LaTeX"
 
54
  :group 'AUC-TeX)
 
55
 
 
56
;;; Site Customization
 
57
;;
 
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
 
61
;; `tex-site.el'. 
 
62
 
 
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."
 
70
  :group 'TeX-file
 
71
  :type 'directory)
 
72
 
 
73
;; Change this to point to the place where the TeX macros are stored
 
74
;; at yourt site.
 
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."
 
78
  :group 'TeX-file
 
79
  :type '(repeat (directory :format "%v")))
 
80
 
 
81
;; How to print.
 
82
 
 
83
(defcustom TeX-print-command "dvips %s -P%p"
 
84
  "*Command used to print a file. 
 
85
 
 
86
First %p is expanded to the printer name, then ordinary expansion is
 
87
performed as specified in TeX-expand-list."
 
88
  :group 'TeX-command
 
89
  :type 'string)
 
90
 
 
91
(defcustom TeX-queue-command "lpq -P%p"
 
92
  "*Command used to show the status of a printer queue. 
 
93
 
 
94
First %p is expanded to the printer name, then ordinary expansion is
 
95
performed as specified in TeX-expand-list."
 
96
  :group 'TeX-command
 
97
  :type 'string)
 
98
 
 
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
 
104
 
 
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'"
 
135
              'TeX-run-TeX nil t))
 
136
  "List of commands to execute on the current document.
 
137
 
 
138
Each element is a list, whose first element is the name of the command
 
139
as it will be presented to the user.  
 
140
 
 
141
The second element is the string handed to the shell after being
 
142
expanded.  The expansion is done using the information found in
 
143
TeX-expand-list. 
 
144
 
 
145
The third element is the function which actually start the process.
 
146
Several such hooks has been defined:
 
147
 
 
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. 
 
151
 
 
152
TeX-run-format: As TeX-run-command, but assume the output is created
 
153
by a TeX macro package.  Return the process object. 
 
154
 
 
155
TeX-run-TeX: For TeX output.
 
156
 
 
157
TeX-run-LaTeX: For LaTeX output.
 
158
 
 
159
TeX-run-interactive: Run TeX or LaTeX interactively.
 
160
 
 
161
TeX-run-BibTeX: For BibTeX output.
 
162
 
 
163
TeX-run-compile: Use `compile' to run the process.  
 
164
 
 
165
TeX-run-shell: Use `shell-command' to run the process.
 
166
 
 
167
TeX-run-discard: Start the process in the background, discarding its
 
168
output.
 
169
 
 
170
TeX-run-background: Start the process in the background, show output
 
171
in other window.
 
172
 
 
173
TeX-run-silent: Start the process in the background.
 
174
 
 
175
TeX-run-dviout: Special hook for the Japanese dviout previewer for
 
176
PC-9801.
 
177
 
 
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.
 
182
 
 
183
If the fourth element is non-nil, the user will get a chance to
 
184
modify the expanded string.
 
185
 
 
186
The fifth element is obsolete and ignored."
 
187
  :group 'TeX-command
 
188
  :type '(repeat (group (string :tag "Name")
 
189
                        (string :tag "Command")
 
190
                        (choice :tag "How"
 
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"))))
 
207
                       
 
208
 
 
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\"."
 
212
  :group 'LaTeX
 
213
  :type '(radio (const :format "%v\n%h"
 
214
                       :doc "\
 
215
The executable `latex' is LaTeX version 2."
 
216
                       "2")
 
217
                (const :format "%v\n%h"
 
218
                       :doc "\
 
219
The executable `latex' is LaTeX version 2e.
 
220
Do *not* select this if you need to run `latex2e' in order to get
 
221
LaTeX version 2e."
 
222
                       "2e")
 
223
                (string :tag "Other")))
 
224
                
 
225
 
 
226
;; You may want special options to the view command depending on the
 
227
;; style options.  Only works if parsing is enabled.
 
228
 
 
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")
 
234
        ("^ams" "amslatex")
 
235
        ("^slides$" "slitex")
 
236
        ("^plfonts\\|plhb$" "platex")
 
237
        ("." "latex"))
 
238
    ;; They have all been combined in LaTeX 2e.
 
239
    '(("." "latex")))
 
240
  "List of style options and LaTeX commands.
 
241
 
 
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
 
246
string."
 
247
  :group 'TeX-command
 
248
  :type '(repeat (group :value ("" "")
 
249
                        regexp (string :tag "Style"))))
 
250
 
 
251
;; Enter the names of the printers available at your site, or nil if
 
252
;; you only have one printer.
 
253
 
 
254
(defcustom TeX-printer-list
 
255
  '(("Local" "dvips -f %s | lpr" "lpq")
 
256
    ("lw") ("ps"))
 
257
  "List of available printers.
 
258
 
 
259
The first element of each entry is the printer name.
 
260
 
 
261
The second element is the command used to print to this
 
262
printer.  It defaults to the value of TeX-print-command.
 
263
 
 
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.
 
266
 
 
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."
 
270
  :group 'TeX-command
 
271
  :type '(repeat (group (string :tag "Name")
 
272
                        (option (group :inline t
 
273
                                       :extra-offset -4
 
274
                                       (choice :tag "Print"
 
275
                                               (const :tag "default")
 
276
                                               (string :format "%v"))
 
277
                                       (option (choice :tag "Queue"
 
278
                                                       (const :tag "default")
 
279
                                                       (string 
 
280
                                                        :format "%v"))))))))
 
281
 
 
282
;; The name of the most used printer.  
 
283
 
 
284
(defcustom TeX-printer-default (or (getenv "PRINTER")
 
285
                                   (and TeX-printer-list
 
286
                                        (car (car TeX-printer-list)))
 
287
                                   "lw")
 
288
  "*Default printer to use with TeX-command."
 
289
  :group 'TeX-command
 
290
  :type 'string)
 
291
 
 
292
;; You may want special options to the view command depending on the
 
293
;; style options.  Only works if parsing is enabled.
 
294
 
 
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")
 
300
                            ("." "xdvi %d"))
 
301
  "List of style options and view options.
 
302
 
 
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
 
307
string."
 
308
  :group 'TeX-command
 
309
  :type '(repeat (group regexp (string :tag "Command"))))
 
310
 
 
311
;;Same for printing.
 
312
 
 
313
(defcustom TeX-print-style '(("^landscape$" "-t landscape"))
 
314
  "List of style options and print options.
 
315
 
 
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
 
320
string."
 
321
  :group 'TeX-command
 
322
  :type '(repeat (group regexp (string :tag "Command"))))
 
323
 
 
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. 
 
327
 
 
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.
 
345
 
 
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."
 
351
  :group 'TeX-command
 
352
  :type '(repeat (group (string :tag "Key")
 
353
                        (sexp :tag "Expander")
 
354
                        (repeat :inline t
 
355
                                :tag "Arguments"
 
356
                                (sexp :format "%v")))))
 
357
 
 
358
;; End of Site Customization.
 
359
 
 
360
;;; Import
 
361
 
 
362
;;(or (assoc TeX-lisp-directory (mapcar 'list load-path))       ;No `member' yet.
 
363
;;    (setq load-path (cons TeX-lisp-directory load-path)))
 
364
 
 
365
(defvar no-doc
 
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.")
 
369
 
 
370
;; This hook will store bibitems when you save a BibTeX buffer.
 
371
(add-hook 'bibtex-mode-hook 'BibTeX-auto-store)
 
372
 
 
373
(autoload 'BibTeX-auto-store "latex" no-doc t)
 
374
 
 
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)
 
381
 
 
382
(autoload 'multi-prompt "multi-prompt" no-doc nil)
 
383
(autoload 'texmathp "texmathp" no-doc nil)
 
384
 
 
385
;;; Portability.
 
386
 
 
387
(require 'easymenu)
 
388
 
 
389
;;; Special support for XEmacs 
 
390
 
 
391
(when (featurep 'xemacs)
 
392
 
 
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)))
 
397
 
 
398
  (defun TeX-mark-active ()
 
399
    ;; In Lucid (mark) returns nil when not active.
 
400
    (if zmacs-regions 
 
401
        (mark)
 
402
      (mark t)))
 
403
 
 
404
  (defun TeX-active-mark ()
 
405
    (and zmacs-regions (mark)))
 
406
 
 
407
  (fset 'TeX-activate-region (symbol-function 'zmacs-activate-region))
 
408
 
 
409
  )
 
410
 
 
411
;;; Special support for GNU Emacs
 
412
 
 
413
(unless (featurep 'xemacs)
 
414
 
 
415
  (defun TeX-mark-active ()
 
416
    ;; In FSF 19 mark-active indicates if mark is active.
 
417
    mark-active)
 
418
 
 
419
  (defun TeX-active-mark ()
 
420
    (and transient-mark-mode mark-active))
 
421
 
 
422
  (defun TeX-activate-region ()
 
423
    nil)
 
424
 
 
425
  )
 
426
 
 
427
;;; Version 
 
428
 
 
429
;; These two variables are automatically updated with "make dist", so
 
430
;; be careful before changing anything.
 
431
 
 
432
(defconst AUC-TeX-version "11.06"
 
433
  "AUC TeX version number.")
 
434
 
 
435
(defconst AUC-TeX-date "Mon Oct 15 17:10:13 CEST 2001"
 
436
  "AUC TeX release date.")
 
437
 
 
438
;;; Buffer
 
439
 
 
440
(defgroup TeX-output nil
 
441
  "Parsing TeX output."
 
442
  :prefix "TeX-"
 
443
  :group 'AUC-TeX)
 
444
 
 
445
(defcustom TeX-display-help t
 
446
  "*Non-nil means popup help when stepping thrugh errors with \\[TeX-next-error]."
 
447
  :group 'TeX-output
 
448
  :type 'boolean)
 
449
 
 
450
(defcustom TeX-debug-bad-boxes nil
 
451
  "*Non-nil means also find overfull/underfull boxes warnings with \\[TeX-next-error]."
 
452
  :group 'TeX-output
 
453
  :type 'boolean)
 
454
 
 
455
(defgroup TeX-command-name nil
 
456
  "Names for external commands in AUC TeX."
 
457
  :group 'TeX-command)
 
458
 
 
459
(defcustom TeX-command-BibTeX "BibTeX"
 
460
  "*The name of the BibTeX entry in TeX-command-list."
 
461
  :group 'TeX-command-name
 
462
  :type 'string)
 
463
  (make-variable-buffer-local 'TeX-command-BibTeX)
 
464
 
 
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
 
469
  :type 'string)
 
470
  (make-variable-buffer-local 'TeX-command-Show)
 
471
 
 
472
(defcustom TeX-command-Print "Print"
 
473
  "The name of the Print entry in TeX-command-Print."
 
474
  :group 'TeX-command-name
 
475
  :type 'string)
 
476
 
 
477
(defcustom TeX-command-Queue "Queue"
 
478
  "The name of the Queue entry in TeX-command-Queue."
 
479
  :group 'TeX-command-name
 
480
  :type 'string)
 
481
 
 
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)
 
495
 
 
496
(defvar TeX-trailer-start nil
 
497
  "Regular expression delimiting start of trailer in a TeX file.")
 
498
 
 
499
 (make-variable-buffer-local 'TeX-trailer-start)
 
500
 
 
501
(defvar TeX-header-end nil
 
502
  "Regular expression delimiting end of header in a TeX file.")
 
503
 
 
504
 (make-variable-buffer-local 'TeX-header-end)
 
505
 
 
506
(defvar TeX-command-default nil
 
507
  "The default command for TeX-command in the current major mode.")
 
508
 
 
509
 (make-variable-buffer-local 'TeX-command-default)
 
510
 
 
511
 
 
512
;;; Master File
 
513
 
 
514
(defcustom TeX-one-master "\\.tex$"
 
515
  "*Regular expression matching ordinary TeX files.
 
516
 
 
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.  
 
521
 
 
522
If you dislike AUC TeX automatically modifying your files, you can set
 
523
this variable to \"<none>\"."
 
524
  :group 'TeX-command
 
525
  :type 'regexp)
 
526
 
 
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))))))
 
535
 
 
536
(defun TeX-master-file (&optional extension nondirectory)
 
537
  "Return the name of the master file for the current document.
 
538
 
 
539
If optional argument EXTENSION is non-nil, add that file extension to
 
540
the name.  Special value t means use `TeX-default-extension'.
 
541
 
 
542
If optional second argument NONDIRECTORY is non-nil, do not include
 
543
the directory.
 
544
 
 
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."
 
547
  (if (eq extension t)
 
548
      (setq extension TeX-default-extension))
 
549
  (let ((my-name (if (buffer-file-name)
 
550
                     (TeX-strip-extension nil (list TeX-default-extension) t)
 
551
                   "<none>")))
 
552
    (save-excursion
 
553
      (save-restriction
 
554
        (widen)
 
555
        (goto-char (point-min))
 
556
        (cond
 
557
         ;; Special value 't means it is own master (a free file).
 
558
         ((equal TeX-master my-name)
 
559
          (setq TeX-master t))
 
560
 
 
561
         ;; For files shared between many documents.
 
562
         ((eq 'shared TeX-master)
 
563
          (setq TeX-master
 
564
                (TeX-strip-extension
 
565
                 (let ((default (or (TeX-dwim-master) "this file")))
 
566
                   (read-file-name (format "Master file: (default %s) " default)
 
567
                                   nil default)
 
568
                 (list TeX-default-extension)
 
569
                 'path)))
 
570
          (if (or (string-equal TeX-master "this file")
 
571
                  (string-equal TeX-master ""))
 
572
              (setq TeX-master t)))
 
573
 
 
574
         ;; We might already know the name.
 
575
         ((or (eq TeX-master t) (stringp TeX-master)) TeX-master)
 
576
 
 
577
         ;; Support the ``Master:'' line (under protest!)
 
578
         ((re-search-forward
 
579
           "^%% *[Mm]aster:?[ \t]*\\([^ \t\n]+\\)" 500 t)
 
580
          (setq TeX-master
 
581
                (TeX-strip-extension (TeX-match-buffer 1)
 
582
                                     (list TeX-default-extension)))
 
583
          (if TeX-convert-master
 
584
              (progn
 
585
                (beginning-of-line)
 
586
                (kill-line 1)
 
587
                (TeX-add-local-master))))
 
588
 
 
589
         ;; Is this a master file?
 
590
         ((re-search-forward TeX-header-end 10000 t)
 
591
          (setq TeX-master my-name))
 
592
 
 
593
         ;; Ask the user (but add it as a local variable).
 
594
         (t
 
595
          (setq TeX-master
 
596
                (let ((default (TeX-dwim-master)))
 
597
                  (or
 
598
                   (and (eq 'dwim TeX-master) default)
 
599
                   (TeX-strip-extension
 
600
                    (condition-case name
 
601
                        (read-file-name (format "Master file: (default %s) "
 
602
                                                (or default "this file"))
 
603
                                        nil (or default "<default>"))
 
604
                      (quit "<quit>"))
 
605
                    (list TeX-default-extension)
 
606
                    'path))))
 
607
          (cond ((string-equal TeX-master "<quit>")
 
608
                 (setq TeX-master t))
 
609
                ((or (string-equal TeX-master "<default>")
 
610
                     (string-equal TeX-master ""))
 
611
                 (setq TeX-master t)
 
612
                 (TeX-add-local-master))
 
613
                (t
 
614
                 (TeX-add-local-master)))))))
 
615
  
 
616
    (let ((name (if (eq TeX-master t)
 
617
                    my-name
 
618
                  TeX-master)))
 
619
      
 
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
 
624
              (setq extension nil)
 
625
            ;; Otherwise drop it.
 
626
            (setq name (TeX-strip-extension name))))
 
627
      
 
628
      ;; Remove directory if needed.
 
629
      (if nondirectory
 
630
          (setq name (file-name-nondirectory name)))
 
631
 
 
632
      (if extension
 
633
          (concat name "." extension)
 
634
        name))))
 
635
 
 
636
(defun TeX-master-directory ()
 
637
  "Directory of master file."
 
638
  (abbreviate-file-name
 
639
   (expand-file-name
 
640
    (substitute-in-file-name
 
641
     (concat (file-name-directory buffer-file-name)
 
642
             (file-name-directory (TeX-master-file)))))))
 
643
 
 
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. 
 
649
 
 
650
If this variable is nil, AUC TeX will query you for the name.
 
651
 
 
652
If the variable is t, AUC TeX will assume the file is a master file
 
653
itself.
 
654
 
 
655
If the variable is 'shared, AUC TeX will query for the name, but not
 
656
change the file.
 
657
 
 
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.
 
660
 
 
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."
 
663
  :group 'TeX-command
 
664
  :group 'TeX-parse
 
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")))
 
670
 
 
671
 (make-variable-buffer-local 'TeX-master)
 
672
 
 
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.")
 
676
 
 
677
(defun TeX-add-local-master ()
 
678
  "Add local variable for `TeX-master'."
 
679
 
 
680
  (if (and (buffer-file-name)
 
681
           (string-match TeX-one-master
 
682
                         (file-name-nondirectory (buffer-file-name)))
 
683
           (not buffer-read-only))
 
684
      (progn
 
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
 
690
                                                        "End:")))
 
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)
 
695
                  "\n"
 
696
                  "%%% TeX-master: " (prin1-to-string TeX-master) "\n"
 
697
                  "%%% End: \n")))))
 
698
 
 
699
;;; Style Paths
 
700
 
 
701
(or (string-match "/\\'" TeX-lisp-directory)
 
702
    (setq TeX-lisp-directory (concat TeX-lisp-directory "/")))
 
703
 
 
704
(defcustom TeX-auto-global (concat TeX-lisp-directory "auto/")
 
705
  "*Directory containing automatically generated information.
 
706
Must end with a slash.
 
707
 
 
708
For storing automatic extracted information about the TeX macros
 
709
shared by all users of a site."
 
710
  :group 'TeX-file
 
711
  :type 'directory)
 
712
 
 
713
(defcustom TeX-style-global (concat TeX-lisp-directory "style/")
 
714
  "*Directory containing hand generated TeX information.
 
715
Must end with a slash.
 
716
 
 
717
These correspond to TeX macros shared by all users of a site."
 
718
  :group 'TeX-file
 
719
  :type 'directory)
 
720
 
 
721
(defcustom TeX-auto-local "auto/"
 
722
  "*Directory containing automatically generated TeX information.
 
723
Must end with a slash.
 
724
 
 
725
This correspond to TeX macros found in the current directory, and must
 
726
be relative to that."
 
727
  :group 'TeX-file
 
728
  :type 'string)
 
729
 
 
730
(defcustom TeX-style-local "style/"
 
731
  "*Directory containing hand generated TeX information.
 
732
Must end with a slash.
 
733
 
 
734
These correspond to TeX macros found in the current directory, and must
 
735
be relative to that."
 
736
  :group 'TeX-file
 
737
  :type 'string)
 
738
 
 
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
 
742
seperated by REGEXP.
 
743
 
 
744
Examples:
 
745
 
 
746
      (TeX-split-string \"\:\" \"abc:def:ghi\")
 
747
          -> (\"abc\" \"def\" \"ghi\")
 
748
 
 
749
      (TeX-split-string \" *\" \"dvips -Plw -p3 -c4 testfile.dvi\")
 
750
 
 
751
          -> (\"dvips\" \"-Plw\" \"-p3\" \"-c4\" \"testfile.dvi\")
 
752
 
 
753
If REGEXP is nil, or \"\", an error will occur."
 
754
 
 
755
  (let ((start 0)
 
756
        (result '()))
 
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))
 
762
    (nreverse result)))
 
763
 
 
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)))
 
769
 
 
770
(defun TeX-parse-path (env)
 
771
  ;; Return a list if private TeX directories found in environment
 
772
  ;; variable ENV.  
 
773
  (let* ((value (getenv env))
 
774
         (entries (and value (TeX-split-string ":" value)))
 
775
         entry
 
776
         answers) 
 
777
    (while entries
 
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))))
 
788
    answers))
 
789
 
 
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."
 
794
  :group 'TeX-file
 
795
  :type '(repeat (file :format "%v")))
 
796
 
 
797
(defcustom TeX-auto-private (mapcar (function (lambda (entry)
 
798
                                                (concat entry TeX-auto-local)))
 
799
                                    TeX-macro-private)
 
800
  "List of directories containing automatically generated information.
 
801
Must end with a slash.
 
802
 
 
803
These correspond to the personal TeX macros."
 
804
  :group 'TeX-file
 
805
  :type '(repeat (file :format "%v")))
 
806
 
 
807
(if (stringp TeX-auto-private)          ;Backward compatibility
 
808
    (setq TeX-auto-private (list TeX-auto-private)))
 
809
 
 
810
(defcustom TeX-style-private (mapcar (function (lambda (entry)
 
811
                                                 (concat entry
 
812
                                                         TeX-style-local)))
 
813
                                     TeX-macro-private)
 
814
  "List of directories containing hand generated information.
 
815
Must end with a slash.
 
816
 
 
817
These correspond to the personal TeX macros."
 
818
  :group 'TeX-file
 
819
  :type '(repeat (file :format "%v")))
 
820
 
 
821
(if (stringp TeX-style-private)         ;Backward compatibility
 
822
    (setq TeX-style-private (list TeX-style-private)))
 
823
 
 
824
(defcustom TeX-style-path
 
825
  (let ((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)))
 
830
    path)
 
831
  "List of directories to search for AUC TeX style files."
 
832
  :group 'TeX-file
 
833
  :type '(repeat (file :format "%v")))
 
834
 
 
835
(defcustom TeX-check-path
 
836
  (append (list "./") TeX-macro-private TeX-macro-global)
 
837
  "Directory path to search for dependencies.
 
838
 
 
839
If nil, just check the current file.
 
840
Used when checking if any files have changed."
 
841
  :group 'TeX-file
 
842
  :type '(repeat (file :format "%v")))
 
843
 
 
844
;;; Style Files
 
845
 
 
846
(defvar TeX-style-hook-list nil
 
847
  "List of TeX style hooks currently loaded.
 
848
 
 
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
 
851
active.")
 
852
 
 
853
(defcustom TeX-byte-compile nil
 
854
  "*Not nil means try to byte compile auto files before loading."
 
855
  :group 'TeX-parse
 
856
  :type 'boolean)
 
857
 
 
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)
 
865
                                (file-name-directory
 
866
                                 (file-relative-name TeX-master))
 
867
                              "./"))
 
868
                (TeX-style-path (append (list (concat dir TeX-auto-local)
 
869
                                              (concat master-dir
 
870
                                                      TeX-auto-local)  
 
871
                                              (concat dir TeX-style-local)  
 
872
                                              (concat master-dir
 
873
                                                      TeX-style-local))
 
874
                                        TeX-style-path)))
 
875
           (TeX-load-style style)))
 
876
        (t                              ;Relative path
 
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)
 
882
                                             (concat name style)
 
883
                                           (concat name "/" style)))))
 
884
                 TeX-style-path))))
 
885
 
 
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)
 
894
                        (save-excursion
 
895
                          ;; `byte-compile-file' switches buffer in Emacs 20.3.
 
896
                          (byte-compile-file el))
 
897
                        (file-readable-p elc))
 
898
                   (load-file elc)
 
899
                 (load-file el))))
 
900
          ((file-readable-p elc)
 
901
           (load-file elc))
 
902
          ((file-readable-p el)
 
903
           (load-file el)))))
 
904
 
 
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)))
 
908
    (cond ((null entry)
 
909
           ;; New style, add entry.
 
910
           (setq TeX-style-hook-list (cons (list style hook)
 
911
                                           TeX-style-hook-list)))
 
912
          ((member hook entry)
 
913
           ;; Old style, hook already there, do nothing.
 
914
           nil)
 
915
          (t
 
916
           ;; Old style, new hook.
 
917
           (setcdr entry (cons hook (cdr entry)))))))
 
918
 
 
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)))
 
924
        (t
 
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)))))))
 
929
 
 
930
(defcustom TeX-virgin-style (if (and TeX-auto-global
 
931
                                     (file-directory-p TeX-auto-global))
 
932
                                "virtex"
 
933
                              "NoVirtexSymbols")
 
934
  "Style all documents use."
 
935
  :group 'TeX-parse
 
936
  :type 'string)
 
937
 
 
938
(defvar TeX-active-styles nil
 
939
  "List of styles currently active in the document.")
 
940
 
 
941
 (make-variable-buffer-local 'TeX-active-styles)
 
942
 
 
943
(defun TeX-run-style-hooks (&rest styles)
 
944
  "Run the TeX following style hooks."
 
945
  (mapcar (function
 
946
           (lambda (style)
 
947
             (if (TeX-member style TeX-active-styles 'string-equal) 
 
948
                 ()                   ;Avoid recursion.
 
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))))
 
955
               (mapcar 'funcall
 
956
                       (cdr-safe (assoc style TeX-style-hook-list))))))
 
957
          styles))
 
958
 
 
959
(defcustom TeX-parse-self nil
 
960
  "Parse file after loading it if no style hook is found for it."
 
961
  :group 'TeX-parse
 
962
  :type 'boolean)
 
963
 
 
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)
 
967
 
 
968
(defun TeX-update-style (&optional force)
 
969
  "Run style specific hooks for the current document.
 
970
 
 
971
Only do this if it has not been done before, or if optional argument
 
972
FORCE is not nil."
 
973
 
 
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))
 
977
      ()
 
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)))
 
987
 
 
988
    (if (and TeX-parse-self
 
989
             (null (cdr-safe (assoc (TeX-strip-extension nil nil t)
 
990
                                    TeX-style-hook-list))))
 
991
        (TeX-auto-apply))
 
992
    
 
993
    (message "Applying style hooks... done")))
 
994
 
 
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)
 
998
 
 
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)))
 
1004
 
 
1005
(defun TeX-style-list ()
 
1006
  "Return a list of all styles (subfils) use by the current document."
 
1007
  (TeX-update-style)
 
1008
  TeX-active-styles)
 
1009
 
 
1010
;;; Special Characters
 
1011
 
 
1012
(defvar TeX-esc "\\" "The TeX escape character.")
 
1013
 (make-variable-buffer-local 'TeX-esc)
 
1014
 
 
1015
(defvar TeX-grop "{" "The TeX group opening character.")
 
1016
 (make-variable-buffer-local 'TeX-grop)
 
1017
 
 
1018
(defvar TeX-grcl "}" "The TeX group closing character.")
 
1019
 (make-variable-buffer-local 'TeX-grcl)
 
1020
 
 
1021
;;; Symbols
 
1022
 
 
1023
;; Must be before keymaps.
 
1024
 
 
1025
(defgroup TeX-macro nil
 
1026
  "Support for TeX macros in AUC TeX."
 
1027
  :prefix "TeX-"
 
1028
  :group 'TeX)
 
1029
 
 
1030
(defcustom TeX-complete-word 'ispell-complete-word
 
1031
  "*Function to call for completing non-macros in tex-mode."
 
1032
  :group 'TeX-macro)
 
1033
 
 
1034
(defvar TeX-complete-list nil
 
1035
  "List of ways to complete the preceding text.
 
1036
 
 
1037
Each entry is a list with the following elements:
 
1038
 
 
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.
 
1043
 
 
1044
Or alternatively:
 
1045
 
 
1046
0. Regexp matching the preceding text.
 
1047
1. Function to do the actual completion.")
 
1048
 
 
1049
(defun TeX-complete-symbol ()
 
1050
  "Perform completion on TeX/LaTeX symbol preceding point."
 
1051
  (interactive "*")
 
1052
  (let ((list TeX-complete-list)
 
1053
        entry)
 
1054
    (while list
 
1055
      (setq entry (car list)
 
1056
            list (cdr list))
 
1057
      (if (TeX-looking-at-backward (car entry) 250)
 
1058
          (setq list nil)))
 
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)
 
1069
                 (and close
 
1070
                      (not (looking-at (regexp-quote close)))
 
1071
                      (insert close)))
 
1072
                ((null completion)
 
1073
                 (error "Can't find completion for \"%s\"" pattern))
 
1074
                ((not (string-equal symbol completion))
 
1075
                 (delete-region begin end)
 
1076
                 (insert completion)
 
1077
                 (and close
 
1078
                      (eq (try-completion completion list) t)
 
1079
                      (not (looking-at (regexp-quote close)))
 
1080
                      (insert close)))
 
1081
                (t
 
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)))))
 
1088
 
 
1089
(defcustom TeX-default-macro "ref"
 
1090
  "*The default macro when creating new ones with TeX-insert-macro."
 
1091
  :group 'TeX-macro
 
1092
  :type 'string)
 
1093
 
 
1094
 (make-variable-buffer-local 'TeX-default-macro)
 
1095
 
 
1096
(defcustom TeX-insert-braces t
 
1097
  "*If non-nil, append a empty pair of braces after inserting a macro."
 
1098
  :group 'TeX-macro
 
1099
  :type 'boolean)
 
1100
 
 
1101
(defun TeX-insert-macro (symbol)
 
1102
  "Insert TeX macro with completion.
 
1103
 
 
1104
AUC TeX knows of some macros, and may query for extra arguments."
 
1105
  (interactive (list (completing-read (concat "Macro (default "
 
1106
                                              TeX-default-macro
 
1107
                                              "): " 
 
1108
                                              TeX-esc)
 
1109
                                      (TeX-symbol-list))))
 
1110
  (cond ((string-equal symbol "")
 
1111
         (setq symbol TeX-default-macro))
 
1112
        ((interactive-p)
 
1113
         (setq TeX-default-macro symbol)))
 
1114
  (TeX-parse-macro symbol (cdr-safe (assoc symbol (TeX-symbol-list)))))
 
1115
 
 
1116
(defvar TeX-electric-macro-map nil)
 
1117
 
 
1118
(if TeX-electric-macro-map
 
1119
    ()
 
1120
  (setq TeX-electric-macro-map (copy-keymap minibuffer-local-completion-map))
 
1121
  (define-key TeX-electric-macro-map " " 'minibuffer-complete-and-exit))
 
1122
 
 
1123
(defun TeX-electric-macro ()
 
1124
  "Insert TeX macro with completion.
 
1125
 
 
1126
AUC TeX knows of some macros, and may query for extra arguments.
 
1127
Space will complete and exit."
 
1128
  (interactive)
 
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)))
 
1135
        (t
 
1136
         (let ((minibuffer-local-completion-map TeX-electric-macro-map))
 
1137
           (call-interactively 'TeX-insert-macro)))))
 
1138
 
 
1139
(defun TeX-parse-macro (symbol args)
 
1140
  "How to parse TeX macros which takes one or more arguments."
 
1141
 
 
1142
  ;; First argument is the name of the macro.  
 
1143
 
 
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
 
1147
  ;; argument type.
 
1148
 
 
1149
  ;; string: Use the string as a prompt to prompt for the argument.
 
1150
 
 
1151
  ;; number: Insert that many braces, leave point inside the first.
 
1152
 
 
1153
  ;; nil: Insert empty braces.
 
1154
 
 
1155
  ;; t: Insert empty braces, leave point between the braces.
 
1156
 
 
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-'.
 
1166
 
 
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.
 
1170
 
 
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.
 
1175
 
 
1176
  (if (and (TeX-active-mark)
 
1177
           (> (point) (mark)))
 
1178
      (exchange-point-and-mark))
 
1179
  (insert TeX-esc symbol)
 
1180
  (let ((exit-mark (make-marker))
 
1181
        (position (point)))
 
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)
 
1189
                (not (texmathp)))
 
1190
           (insert TeX-grop)
 
1191
           (if (TeX-active-mark)
 
1192
               (progn
 
1193
                 (exchange-point-and-mark)
 
1194
                 (insert TeX-grcl))
 
1195
             (insert TeX-grcl)
 
1196
             (backward-char))))))
 
1197
 
 
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))
 
1204
         TeX-argument)
 
1205
     (read-string (TeX-argument-prompt optional prompt "Text") input))
 
1206
   optional))
 
1207
 
 
1208
(defun TeX-parse-arguments (args)
 
1209
  "Parse TeX macro arguments.
 
1210
 
 
1211
See TeX-parse-macro for details."
 
1212
  (let ((last-optional-rejected nil))
 
1213
    (while args
 
1214
      (if (vectorp (car args))
 
1215
          (if last-optional-rejected
 
1216
              ()
 
1217
            (let ((< LaTeX-optop)
 
1218
                  (> LaTeX-optcl))
 
1219
              (TeX-parse-argument t (if (equal (length (car args)) 1)
 
1220
                                        (aref (car args) 0)
 
1221
                                      (append (car args) nil)))))
 
1222
        (let ((< TeX-grop)
 
1223
              (> TeX-grcl))
 
1224
          (setq last-optional-rejected nil)
 
1225
          (TeX-parse-argument nil (car args))))
 
1226
      (setq args (cdr args)))))
 
1227
 
 
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.
 
1232
 
 
1233
See TeX-parse-macro for details."
 
1234
  
 
1235
  (cond ((stringp arg)
 
1236
         (TeX-arg-string optional arg))
 
1237
        ((numberp arg)
 
1238
         (if (< arg 1)
 
1239
             ()
 
1240
           (TeX-parse-argument optional t)
 
1241
           (while (> arg 1)
 
1242
             (TeX-parse-argument optional nil)
 
1243
             (setq arg (- arg 1)))))
 
1244
        ((null arg)
 
1245
         (insert <)
 
1246
         (if (and (not optional) (TeX-active-mark))
 
1247
             (exchange-point-and-mark))
 
1248
         (insert >))
 
1249
        ((eq arg t)
 
1250
         (insert  < )
 
1251
         (if (and (not optional) (TeX-active-mark))
 
1252
             (exchange-point-and-mark)
 
1253
           (set-marker exit-mark (point)))
 
1254
         (insert >))
 
1255
        ((symbolp arg)
 
1256
         (funcall arg optional))
 
1257
        ((listp arg)
 
1258
         (let ((head (car arg))
 
1259
               (tail (cdr arg)))
 
1260
           (cond ((stringp head)
 
1261
                  (apply 'TeX-arg-string optional arg))
 
1262
                 ((symbolp head)
 
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)))))
 
1267
 
 
1268
(defun TeX-argument-insert (name optional &optional prefix)
 
1269
  "Insert NAME surrounded by curly braces.
 
1270
 
 
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)
 
1274
    (insert <)
 
1275
    (if prefix
 
1276
        (insert prefix))
 
1277
    (if (and (string-equal name "")
 
1278
             (null (marker-position exit-mark)))
 
1279
        (set-marker exit-mark (point))
 
1280
      (insert name))
 
1281
    (insert >)))
 
1282
 
 
1283
(defun TeX-argument-prompt (optional prompt default &optional complete)
 
1284
  "Return a argument prompt.
 
1285
 
 
1286
If OPTIONAL is not nil then the prompt will start with ``(Optional) ''.
 
1287
 
 
1288
PROMPT will be used if not nil, otherwise use DEFAULT.
 
1289
 
 
1290
Unless optional argument COMPLETE is non-nil, ``: '' will be appended."
 
1291
  (concat (if optional "(Optional) " "")
 
1292
          (if prompt prompt default)
 
1293
          (if complete "" ": ")))
 
1294
 
 
1295
;;; The Mode
 
1296
 
 
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
 
1303
     "\\\\document\\b")
 
1304
    ("LATEX" latex-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.
 
1308
 
 
1309
A list with a entry for each format package available at the site.
 
1310
 
 
1311
Each entry is a list with three elements.
 
1312
 
 
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.
 
1316
 
 
1317
When entering tex-mode, each regexp is tried in turn in order to find
 
1318
when major mode to enter.")
 
1319
 
 
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."
 
1323
  :group 'AUC-TeX
 
1324
  :type '(radio (function-item latex-mode)
 
1325
                (function-item plain-tex-mode)
 
1326
                (function :tag "Other")))
 
1327
 
 
1328
(defcustom TeX-force-default-mode nil
 
1329
  "*If set to nil, try to infer the mode of the file from its
 
1330
content."
 
1331
  :group 'AUC-TeX
 
1332
  :type 'boolean)
 
1333
 
 
1334
;; Do not ;;;###autoload because of conflict with standard tex-mode.el.
 
1335
(defun tex-mode ()
 
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.
 
1338
 
 
1339
The algorithm is as follows:
 
1340
 
 
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 "
 
1346
  (interactive)
 
1347
 
 
1348
  (funcall (if (or (equal (buffer-size) 0)
 
1349
                   TeX-force-default-mode)
 
1350
               TeX-default-mode
 
1351
             (save-excursion
 
1352
               (goto-char (point-min))
 
1353
               (let ((comment-start-skip ;Used by TeX-in-comment
 
1354
                      (concat
 
1355
                       "\\(\\(^\\|[^\\]\\)\\("
 
1356
                       (regexp-quote TeX-esc)
 
1357
                       (regexp-quote TeX-esc)
 
1358
                       "\\)*\\)\\(%+ *\\)"))
 
1359
                     (entry TeX-format-list)
 
1360
                     answer)
 
1361
                 (while (and entry (not answer))
 
1362
                   (if (re-search-forward (nth 2 (car entry))
 
1363
                                          10000 t)
 
1364
                       (if (not (TeX-in-comment))
 
1365
                           (setq answer (nth 1 (car entry))))
 
1366
                     (setq entry (cdr entry))))
 
1367
                 (if answer
 
1368
                     answer
 
1369
                   TeX-default-mode))))))
 
1370
 
 
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.
 
1375
 
 
1376
Special commands:
 
1377
\\{TeX-mode-map}
 
1378
 
 
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."
 
1382
  (interactive)
 
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))
 
1388
 
 
1389
;;;###autoload
 
1390
(defun ams-tex-mode ()
 
1391
  "Major mode for editing files of input for AmS TeX.
 
1392
See info under AUC TeX for documentation.
 
1393
 
 
1394
Special commands:
 
1395
\\{TeX-mode-map}
 
1396
 
 
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."
 
1400
  (interactive)
 
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))
 
1406
 
 
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)
 
1413
 
 
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."
 
1417
  :group 'AUC-TeX
 
1418
  :type '(radio (function-item font-latex-setup)
 
1419
                (function-item tex-font-setup)
 
1420
                (function-item ignore)
 
1421
                (function :tag "Other")))
 
1422
 
 
1423
(defun VirTeX-common-initialization ()
 
1424
  ;; Initialize
 
1425
  (kill-all-local-variables)
 
1426
  (setq local-abbrev-table text-mode-abbrev-table)
 
1427
  (setq indent-tabs-mode nil)
 
1428
 
 
1429
  ;; Ispell support
 
1430
  (make-local-variable 'ispell-parser)
 
1431
  (setq ispell-parser 'tex)
 
1432
  (make-local-variable 'ispell-tex-p)
 
1433
  (setq ispell-tex-p t)
 
1434
 
 
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
 
1442
        (concat
 
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)
 
1453
      ()
 
1454
    (setq compile-command "make"))
 
1455
  (make-local-variable 'words-include-escapes)
 
1456
  (setq words-include-escapes nil)
 
1457
 
 
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)))
 
1464
  
 
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)))
 
1471
  
 
1472
  (funcall TeX-install-font-lock)
 
1473
 
 
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))
 
1481
 
 
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
 
1490
        (concat
 
1491
         "\\(^[ \t]*$"
 
1492
         "\\|" (regexp-quote TeX-esc) "par\\|" 
 
1493
         "^[ \t]*"
 
1494
         (regexp-quote TeX-esc)
 
1495
         "\\("
 
1496
         "begin\\|end\\|part\\|chapter\\|"
 
1497
         "section\\|subsection\\|subsubsection\\|"
 
1498
         "paragraph\\|include\\|includeonly\\|"
 
1499
         "tableofcontents\\|appendix\\|label\\|caption\\|"
 
1500
         "\\[\\|\\]"                    ; display math delimitors
 
1501
         "\\)"
 
1502
         "\\|"
 
1503
         "^[ \t]*\\$\\$"                ; display math delimitor
 
1504
         "\\)" ))
 
1505
  (setq paragraph-separate
 
1506
        (concat
 
1507
         "\\("
 
1508
         (regexp-quote TeX-esc)
 
1509
         "par\\|"
 
1510
         "^[ \t]*$\\|"
 
1511
         "^[ \t]*"
 
1512
         (regexp-quote TeX-esc)
 
1513
         "\\("
 
1514
         "begin\\|end\\|label\\|caption\\|part\\|chapter\\|"
 
1515
         "section\\|subsection\\|subsubsection\\|"
 
1516
         "paragraph\\|include\\|includeonly\\|"
 
1517
         "tableofcontents\\|appendix\\|" (regexp-quote TeX-esc)
 
1518
         "\\)"
 
1519
         "\\)"))
 
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"))
 
1523
 
 
1524
;;; Hilighting
 
1525
 
 
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)))))
 
1534
 
 
1535
;;; Parsing
 
1536
 
 
1537
(defgroup TeX-parse nil
 
1538
  "Parsing TeX files from AUC TeX."
 
1539
  :group 'AUC-TeX)
 
1540
 
 
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:
 
1544
;; 
 
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.
 
1550
 
 
1551
 
 
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)
 
1556
 
 
1557
(defun TeX-auto-add-type (name prefix &optional plural)
 
1558
  "Add information about name to the parser using PREFIX.
 
1559
 
 
1560
Optional third argument PLURAL is the plural form of TYPE.  
 
1561
By default just add  a `s'.
 
1562
 
 
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))
 
1572
    (set local nil)
 
1573
    (make-variable-buffer-local local)
 
1574
    (set change nil)
 
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)))))
 
1586
 
 
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)))
 
1594
    (if change-value
 
1595
        (set local (cons entries local-value))
 
1596
      (set change t)
 
1597
      (set local (list entries local-value)))))
 
1598
 
 
1599
(defun TeX-auto-list-information (name)
 
1600
  ;; Return information in `TeX-auto-parser' about NAME.
 
1601
  (TeX-update-style)
 
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)
 
1607
        ()
 
1608
      (set change nil)
 
1609
      ;; Sort it
 
1610
      (message "Sorting " name "...")
 
1611
      (set local
 
1612
           (sort (mapcar 'TeX-listify (apply 'append (symbol-value local)))
 
1613
                 'TeX-car-string-lessp))
 
1614
      ;; Make it unique
 
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
 
1623
              ;; most arguments.
 
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)))
 
1629
 
 
1630
(TeX-auto-add-type "symbol" "TeX")
 
1631
 
 
1632
(defvar TeX-auto-apply-hook nil
 
1633
  "Hook run when a buffer is parsed and the information is applied.")
 
1634
 
 
1635
(defun TeX-auto-apply ()
 
1636
  ;; Parse and apply TeX information in the current buffer.
 
1637
  (TeX-auto-parse)
 
1638
  (run-hooks 'TeX-auto-apply-hook)
 
1639
  (mapcar 'TeX-auto-apply-entry TeX-auto-parser))
 
1640
 
 
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))))
 
1646
 
 
1647
(defun TeX-safe-auto-write ()
 
1648
  ;; Call TeX-auto-write safely
 
1649
  (condition-case name
 
1650
      (and (boundp 'TeX-auto-update)
 
1651
           TeX-auto-update
 
1652
           (TeX-auto-write))
 
1653
    (error nil))
 
1654
  ;; Continue with the other write file hooks.
 
1655
  nil)
 
1656
 
 
1657
(defcustom TeX-auto-save nil
 
1658
  "*Automatically save style information when saving the buffer."
 
1659
  :group 'TeX-parse
 
1660
  :type 'boolean)
 
1661
 
 
1662
(defcustom TeX-auto-untabify t
 
1663
  "*Automatically untabify when saving the buffer."
 
1664
  :group 'TeX-parse
 
1665
  :type 'boolean)
 
1666
 
 
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)
 
1673
                           TeX-auto-local
 
1674
                           (if (string-match "/$" TeX-auto-local) "" "/")
 
1675
                           (TeX-strip-extension nil TeX-all-extensions t)
 
1676
                           ".el"))
 
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))
 
1682
              (error nil)))
 
1683
        (if (file-writable-p file)
 
1684
            (save-excursion
 
1685
              (TeX-update-style)
 
1686
              (TeX-auto-store file))
 
1687
          (message "Can't write style information.")))))
 
1688
 
 
1689
(defcustom TeX-macro-default (car-safe TeX-macro-private)
 
1690
  "*Default directory to search for TeX macros."
 
1691
  :group 'TeX-file
 
1692
  :type 'directory)
 
1693
 
 
1694
(defcustom TeX-auto-default (car-safe TeX-auto-private)
 
1695
  "*Default directory to place automatically generated TeX information."
 
1696
  :group 'TeX-file
 
1697
  :type 'directory)
 
1698
 
 
1699
;;;###autoload
 
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: "
 
1706
                                              TeX-macro-default
 
1707
                                              TeX-macro-default 'confirm)))
 
1708
                     (setq TeX-auto-default
 
1709
                           (expand-file-name (read-file-name
 
1710
                                              "AUTO lisp directory: "
 
1711
                                              TeX-auto-default
 
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)
 
1718
                                              ""
 
1719
                                            default-directory)
 
1720
                                          (if (string-match "/$" tex)
 
1721
                                              tex
 
1722
                                            (concat tex "/"))))
 
1723
               (TeX-file-recurse (cond ((symbolp TeX-file-recurse)
 
1724
                                        TeX-file-recurse)
 
1725
                                       ((zerop TeX-file-recurse)
 
1726
                                        nil)
 
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))))
 
1732
                   files)))
 
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))
 
1738
         (save-excursion
 
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
 
1744
                                                        TeX-all-extensions
 
1745
                                                        t)
 
1746
                                   ".el"))
 
1747
           (kill-buffer (current-buffer))
 
1748
           (message "Parsing %s... done" tex)))))
 
1749
 
 
1750
;;;###autoload
 
1751
(defun TeX-auto-generate-global ()
 
1752
  "Create global auto directory for global TeX macro definitions."
 
1753
  (interactive)
 
1754
  (if (file-directory-p 
 
1755
             (if (string-match "/$" TeX-auto-global)   
 
1756
                   (substring TeX-auto-global 0 -1)
 
1757
                    TeX-auto-global))
 
1758
        nil
 
1759
        (make-directory (if (string-match "/$" TeX-auto-global)
 
1760
                      (substring TeX-auto-global 0 -1)
 
1761
                    TeX-auto-global)))
 
1762
  (mapcar (function (lambda (macro) (TeX-auto-generate macro TeX-auto-global)))
 
1763
          TeX-macro-global)
 
1764
  (byte-recompile-directory TeX-auto-global 0))
 
1765
 
 
1766
(defun TeX-auto-store (file)
 
1767
  ;; Extract information for auc tex from current buffer and store it in FILE.
 
1768
  (TeX-auto-parse)
 
1769
  
 
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)
 
1773
        (save-excursion
 
1774
          (set-buffer (generate-new-buffer file))
 
1775
          (erase-buffer)
 
1776
          (insert "(TeX-add-style-hook \"" style "\"\n"
 
1777
                  " (function\n"
 
1778
                  "  (lambda ()")
 
1779
          (mapcar 'TeX-auto-insert TeX-auto-parser)
 
1780
          (insert ")))\n\n")
 
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))))
 
1787
 
 
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))))
 
1791
 
 
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))))
 
1796
    (if (null list)
 
1797
        ()
 
1798
      (insert "\n    (" name)
 
1799
      (while list
 
1800
        (insert "\n     ")
 
1801
        (if (stringp (car list))
 
1802
            (insert (prin1-to-string (car list)))
 
1803
          (insert "'" (prin1-to-string (car list))))
 
1804
        (setq list (cdr list)))
 
1805
      (insert ")"))))
 
1806
 
 
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.")
 
1814
 
 
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))))
 
1821
 
 
1822
(defvar TeX-auto-empty-regexp-list
 
1823
  '(("<IMPOSSIBLE>\\(\\'\\`\\)" 1 ignore))
 
1824
  "List of regular expressions guaranteed to match nothing.")
 
1825
 
 
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@]"
 
1832
     2 TeX-auto-symbol)
 
1833
    ("\\\\newfont{?\\\\\\([a-zA-Z]+\\)}?" 1 TeX-auto-symbol)
 
1834
    ("\\\\typein\\[\\\\\\([a-zA-Z]+\\)\\]" 1 TeX-auto-symbol)
 
1835
    ("\\\\input +\\(\\.*[^#%\\\\\\.\n\r]+\\)\\(\\.[^#%\\\\\\.\n\r]+\\)?"
 
1836
     1 TeX-auto-file)
 
1837
    ("\\\\mathchardef\\\\\\([a-zA-Z]+\\)[^a-zA-Z@]" 1 TeX-auto-symbol))
 
1838
  "List of regular expression matching common LaTeX macro definitions.")
 
1839
 
 
1840
(defvar TeX-auto-full-regexp-list plain-TeX-auto-regexp-list
 
1841
  "Full list of regular expression matching TeX macro definitions.")
 
1842
 
 
1843
(defvar TeX-auto-prepare-hook nil
 
1844
  "List of hooks to be called before parsing a TeX file.")
 
1845
 
 
1846
(defvar TeX-auto-cleanup-hook nil
 
1847
  "List of hooks to be called after parsing a TeX file.")
 
1848
 
 
1849
(defcustom TeX-auto-parse-length 999999
 
1850
  "*Maximal length of TeX file that will be parsed."
 
1851
  :group 'TeX-parse
 
1852
  :type 'integer)
 
1853
  (make-variable-buffer-local 'TeX-auto-parse-length)
 
1854
 
 
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."
 
1859
  :group 'TeX-parse
 
1860
  :type 'integer)
 
1861
  (make-variable-buffer-local 'TeX-auto-x-parse-length)
 
1862
 
 
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")
 
1876
                               symbol)))
 
1877
  :group 'TeX-parse)
 
1878
  (make-variable-buffer-local 'TeX-auto-x-regexp-list)
 
1879
 
 
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))))
 
1885
   (if regexp-list
 
1886
       ;; Extract the information.
 
1887
       (let ((regexp (concat "\\("
 
1888
                             (mapconcat 'car regexp-list "\\)\\|\\(")
 
1889
                             "\\)")))
 
1890
         (goto-char (if end (min end (point-max)) (point-max)))
 
1891
         (while (re-search-backward regexp beg t)
 
1892
           (if (TeX-in-comment)
 
1893
               ()
 
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))))))))))
 
1905
 
 
1906
(defun TeX-auto-parse ()
 
1907
  "Parse TeX information in current buffer.
 
1908
 
 
1909
Call the functions in TeX-auto-prepare-hook before parsing, and the
 
1910
functions in TeX-auto-cleanup-hook after parsing."
 
1911
 
 
1912
  (let ((case-fold-search nil))
 
1913
    
 
1914
    (mapcar 'TeX-auto-clear-entry TeX-auto-parser)
 
1915
    (run-hooks 'TeX-auto-prepare-hook)
 
1916
    
 
1917
    (save-excursion
 
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))
 
1925
    
 
1926
    ;; Cleanup ignored symbols.
 
1927
    
 
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)))))
 
1937
    
 
1938
    (run-hooks 'TeX-auto-cleanup-hook)))
 
1939
 
 
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))
 
1943
 
 
1944
(defvar LaTeX-auto-end-symbol nil)
 
1945
 
 
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)))))
 
1958
 
 
1959
;;; Utilities
 
1960
;;
 
1961
;; Some of these functions has little to do with TeX, but nonetheless we
 
1962
;; should use the "TeX-" prefix to avoid name clashes.
 
1963
 
 
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")
 
1976
                               symbol)))
 
1977
  :group 'TeX-parse)
 
1978
  (make-variable-buffer-local 'TeX-auto-regexp-list)
 
1979
 
 
1980
(defgroup TeX-file-extension nil
 
1981
  "File extensions recognized by AUC TeX."
 
1982
  :group 'TeX-file)  
 
1983
 
 
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")))
 
1988
 
 
1989
(defcustom TeX-all-extensions '("[^.\n]+")
 
1990
  "All possible file extensions."
 
1991
  :group 'TeX-file-extension
 
1992
  :type '(repeat (regexp :format "%v")))
 
1993
 
 
1994
(defcustom TeX-default-extension "tex"
 
1995
  "*Default extension for TeX files."
 
1996
  :group 'TeX-file-extension
 
1997
  :type 'string)
 
1998
 
 
1999
  (make-variable-buffer-local 'TeX-default-extension)
 
2000
 
 
2001
(defcustom BibTeX-file-extensions '("bib")
 
2002
  "Valid file extensions for BibTeX files."
 
2003
  :group 'TeX-file-extension
 
2004
  :type '(repeat (string :format "%v")))
 
2005
 
 
2006
(defcustom BibTeX-style-extensions '("bst")
 
2007
  "Valid file extensions for BibTeX styles."
 
2008
  :group 'TeX-file-extension
 
2009
  :type '(repeat (string :format "%v")))
 
2010
 
 
2011
(defcustom TeX-ignore-file "\\(^\\|/\\)\\(\\.\\|\\.\\.\\|RCS\\|SCCS\\|CVS\\|babel\\..*\\)$"
 
2012
  "Regular expression matching file names to ignore.
 
2013
 
 
2014
These files or directories will not be considered when searching for
 
2015
TeX files in a directory."
 
2016
  :group 'TeX-parse
 
2017
  :type 'regexp)
 
2018
 
 
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."  
 
2023
  :group 'TeX-parse
 
2024
  :type '(choice (const :tag "On" t)
 
2025
                 (const :tag "Off" nil)
 
2026
                 (integer :tag "Depth" :value 1)))
 
2027
 
 
2028
(defun TeX-match-extension (file &optional extensions)
 
2029
  "Return non-nil if FILE has an one of EXTENSIONS.
 
2030
 
 
2031
If EXTENSIONS is not specified or nil, the value of
 
2032
TeX-file-extensions is used instead."
 
2033
 
 
2034
  (if (null extensions)
 
2035
      (setq extensions TeX-file-extensions))
 
2036
 
 
2037
  (let ((regexp (concat "\\.\\("
 
2038
                        (mapconcat 'identity extensions "\\|")
 
2039
                        "\\)$"))
 
2040
        (case-fold-search t))
 
2041
    (string-match regexp file)))
 
2042
 
 
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."
 
2051
  
 
2052
  (if (null string)
 
2053
      (setq string (or (buffer-file-name) "<none>")))
 
2054
  
 
2055
  (if (null extensions)
 
2056
      (setq extensions TeX-file-extensions))
 
2057
  
 
2058
  (let* ((strip (if (and (not nostrip)
 
2059
                         (TeX-match-extension string extensions))
 
2060
                    (substring string 0 (match-beginning 0))
 
2061
                  string))
 
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)
 
2068
      strip)))
 
2069
 
 
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"
 
2077
 
 
2078
  (if (null extensions)
 
2079
      (setq extensions TeX-file-extensions))
 
2080
  
 
2081
  (if (null directories)
 
2082
      (setq directories
 
2083
            (cons "./" (append TeX-macro-private TeX-macro-global))))
 
2084
  
 
2085
  (let (match
 
2086
        (TeX-file-recurse (cond ((symbolp TeX-file-recurse)
 
2087
                                        TeX-file-recurse)
 
2088
                                       ((zerop TeX-file-recurse)
 
2089
                                        nil)
 
2090
                                       ((1- TeX-file-recurse)))))
 
2091
    (while directories
 
2092
      (let* ((directory (car directories))
 
2093
             (content (and directory
 
2094
                           (file-readable-p directory)
 
2095
                           (file-directory-p directory)
 
2096
                           (directory-files directory))))
 
2097
        
 
2098
        (setq directories (cdr directories))
 
2099
        
 
2100
        (while content
 
2101
          (let ((file (concat directory (car content))))
 
2102
            
 
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
 
2108
                       (setq match 
 
2109
                             (append match 
 
2110
                                     (TeX-search-files (list (concat file "/"))
 
2111
                                                       extensions
 
2112
                                                       nodir strip)))))
 
2113
                  ((TeX-match-extension file extensions)
 
2114
                   (setq match (cons (TeX-strip-extension file
 
2115
                                                          extensions
 
2116
                                                          nodir
 
2117
                                                          (not strip))
 
2118
                                     match))))))))
 
2119
    
 
2120
    match))
 
2121
 
 
2122
(defun TeX-car-string-lessp (a b)
 
2123
  (string-lessp (car a) (car b)))
 
2124
 
 
2125
(defun TeX-listify (a)
 
2126
  (if (listp a) a (list a)))
 
2127
 
 
2128
(defun TeX-member (elt list how)
 
2129
  "Returns the member ELT in LIST.  Comparison done with HOW.
 
2130
 
 
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)))
 
2134
  (car-safe list))
 
2135
 
 
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) "$")
 
2142
                                (car b)))))))
 
2143
 
 
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))
 
2151
    ""))
 
2152
 
 
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))
 
2157
      (and (listp arg)
 
2158
           (eq (car arg) 'lambda))
 
2159
      (and (symbolp arg)
 
2160
           (fboundp arg))))
 
2161
 
 
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)))
 
2167
    (save-excursion
 
2168
      (and (re-search-backward regexp
 
2169
                               (if limit (max (point-min) (- (point) limit)))
 
2170
                               t)
 
2171
           (eq (match-end 0) pos)))))
 
2172
 
 
2173
(defun TeX-current-line ()
 
2174
  "The current line number."
 
2175
  (format "%d" (1+ (TeX-current-offset))))
 
2176
 
 
2177
(defun TeX-current-file-name-nondirectory ()
 
2178
  "Return current filename, without path."
 
2179
  (file-name-nondirectory buffer-file-name))
 
2180
 
 
2181
 
 
2182
;;; Syntax Table
 
2183
 
 
2184
(defvar TeX-mode-syntax-table (make-syntax-table)
 
2185
  "Syntax table used while in TeX mode.")
 
2186
 
 
2187
 (make-variable-buffer-local 'TeX-mode-syntax-table)
 
2188
 
 
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))
 
2208
 
 
2209
;;; Menu Support
 
2210
 
 
2211
(defvar TeX-command-current 'TeX-command-master)
 
2212
;; Function used to run external command.
 
2213
 
 
2214
(defun TeX-command-select-master ()
 
2215
  (interactive)
 
2216
  (message "Next command will be on the master file")
 
2217
  (setq TeX-command-current 'TeX-command-master))
 
2218
 
 
2219
(defun TeX-command-select-buffer ()
 
2220
  (interactive)
 
2221
  (message "Next command will be on the buffer")
 
2222
  (setq TeX-command-current 'TeX-command-buffer))
 
2223
 
 
2224
(defun TeX-command-select-region ()
 
2225
  (interactive)
 
2226
  (message "Next command will be on the region")
 
2227
  (setq TeX-command-current 'TeX-command-region))
 
2228
 
 
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. 
 
2232
 
 
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)))
 
2237
 
 
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)))
 
2244
 
 
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
 
2249
                (nth 0 entry)
 
2250
                (or (nth lookup entry) command)
 
2251
                name)
 
2252
          t))
 
2253
 
 
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.
 
2256
 
 
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)))
 
2263
 
 
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
 
2268
                (nth 0 entry)
 
2269
                (or (nth lookup entry) command)
 
2270
                name)
 
2271
          t))
 
2272
 
 
2273
;; End fix part 1.
 
2274
 
 
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)
 
2279
                TeX-printer-list)
 
2280
           (let ((command TeX-print-command)
 
2281
                 (lookup 1))
 
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)
 
2286
                TeX-printer-list)
 
2287
           (let ((command TeX-queue-command)
 
2288
                 (lookup 2))
 
2289
             (append (list TeX-command-Queue)
 
2290
                     (mapcar 'TeX-command-menu-queue-entry ; dickow fix part 2.
 
2291
                             TeX-printer-list))))
 
2292
          (t
 
2293
           (vector name (list 'TeX-command-menu name) t)))))
 
2294
 
 
2295
;;; Keymap
 
2296
 
 
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'."
 
2300
  :group 'TeX-macro
 
2301
  :type 'boolean)
 
2302
 
 
2303
(defvar TeX-mode-map nil
 
2304
  "Keymap for common TeX and LaTeX commands.")
 
2305
 
 
2306
(if TeX-mode-map 
 
2307
    ()
 
2308
  (setq TeX-mode-map (make-sparse-keymap))
 
2309
 
 
2310
  ;; Standard
 
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)
 
2317
 
 
2318
  ;; From tex.el
 
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
 
2330
  
 
2331
  (define-key TeX-mode-map "\C-c;"    'TeX-comment-region)
 
2332
  (define-key TeX-mode-map "\C-c%"    'TeX-comment-paragraph)
 
2333
 
 
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
 
2337
 
 
2338
  ;; From tex-buf.el
 
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))
 
2348
 
 
2349
(easy-menu-define TeX-mode-menu
 
2350
    TeX-mode-map
 
2351
    "Menu used in TeX mode."
 
2352
  (append '("Command")
 
2353
          '(("Command on"
 
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))))
 
2365
 
 
2366
(defvar plain-TeX-mode-map (copy-keymap TeX-mode-map)
 
2367
  "Keymap used in plain TeX mode.")
 
2368
 
 
2369
(easy-menu-define plain-TeX-mode-menu
 
2370
    plain-TeX-mode-map
 
2371
    "Menu used in plain TeX mode."
 
2372
  (list "TeX"
 
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"]))
 
2391
 
 
2392
;;; Comments
 
2393
 
 
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)))
 
2398
 
 
2399
(defun TeX-un-comment (level)
 
2400
  "Delete up to LEVEL %'s from the beginning of each line in a comment."
 
2401
  (interactive "*p")
 
2402
  (save-excursion
 
2403
    ; Find first comment line
 
2404
    (re-search-backward (concat "^[^" comment-start "]") nil 'limit)
 
2405
    (let ((beg (point)))
 
2406
      (forward-line 1)
 
2407
      ; Find last comment line
 
2408
      (re-search-forward (concat "^[^" comment-start "]") nil 'limit)
 
2409
      ; Uncomment region
 
2410
      (comment-region beg (point) (- level)))))
 
2411
 
 
2412
(fset 'TeX-comment-region 'comment-region)
 
2413
 
 
2414
(defun TeX-comment-paragraph (level)
 
2415
  "Inserts LEVEL %'s at the beginning of every line in the current paragraph."
 
2416
  (interactive "*p")
 
2417
  (if (< level 0)
 
2418
      (TeX-un-comment (- level))
 
2419
    (save-excursion
 
2420
      (mark-paragraph)
 
2421
      (comment-region (point) (mark) level))))
 
2422
 
 
2423
(defun TeX-in-comment ()
 
2424
  ;; Return non-nil if point is in a comment.
 
2425
  (if (or (bolp)
 
2426
          (null comment-start-skip)
 
2427
          (eq (preceding-char) ?\r))
 
2428
      nil
 
2429
    (save-excursion
 
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))))))
 
2434
 
 
2435
;;; Indentation
 
2436
 
 
2437
(defgroup TeX-indentation nil
 
2438
  "Indentation of TeX buffers in AUC TeX."
 
2439
  :group 'AUC-TeX)
 
2440
 
 
2441
(defun TeX-brace-count-line ()
 
2442
  "Count number of open/closed braces."
 
2443
  (save-excursion
 
2444
    (save-restriction
 
2445
      (let ((count 0))
 
2446
        (narrow-to-region (point)
 
2447
                          (save-excursion
 
2448
                            (re-search-forward "[^\\\\]%\\|\n\\|\\'")
 
2449
                            (backward-char)
 
2450
                            (point)))
 
2451
        
 
2452
        (while (re-search-forward "\\({\\|}\\|\\\\.\\)" nil t)
 
2453
          (cond
 
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)))))
 
2458
        count))))
 
2459
 
 
2460
(defcustom TeX-brace-indent-level 2
 
2461
  "*The level of indentation produced by a open brace."
 
2462
  :group 'TeX-indentation
 
2463
  :type 'integer)
 
2464
 
 
2465
(defun TeX-comment-indent ()
 
2466
  (if (looking-at "%%%")
 
2467
      (current-column)
 
2468
    (skip-chars-backward " \t")
 
2469
    (max (if (bolp) 0 (1+ (current-column)))
 
2470
         comment-column)))
 
2471
 
 
2472
;;; Fonts
 
2473
 
 
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 " "}")
 
2481
                           (?\C-d "" "" t))
 
2482
  "List of fonts used by TeX-font.
 
2483
 
 
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."
 
2491
  :group 'TeX-macro
 
2492
  :type '(repeat 
 
2493
           (group 
 
2494
            :value (?\C-a "" "")
 
2495
            (character :tag "Key")
 
2496
            (string :tag "Prefix")
 
2497
            (string :tag "Suffix")
 
2498
            (option (group
 
2499
                     :inline t
 
2500
                     (string :tag "Math Prefix")
 
2501
                     (string :tag "Math Suffix")))
 
2502
            (option (sexp :format "Replace\n" :value t)))))
 
2503
 
 
2504
(defvar TeX-font-replace-function 'TeX-font-replace
 
2505
  "Determines the function which is called when a font should be replaced.")
 
2506
 
 
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)))
 
2511
              "-- delete font"
 
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) "")))))
 
2516
 
 
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")
 
2522
  (TeX-update-style)
 
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)))
 
2531
    (cond ((null entry)
 
2532
           (let ((help (concat 
 
2533
                        "Font list:   "
 
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*")
 
2539
               (insert help))))
 
2540
          (replace
 
2541
           (funcall TeX-font-replace-function before after))
 
2542
          ((TeX-active-mark)
 
2543
           (save-excursion
 
2544
             (cond ((> (mark) (point))
 
2545
                    (insert before)
 
2546
                    (goto-char (mark))
 
2547
                    (insert after))
 
2548
                   (t
 
2549
                    (insert after)
 
2550
                    (goto-char (mark))
 
2551
                    (insert before)))))
 
2552
          (t
 
2553
           (insert before)
 
2554
           (save-excursion
 
2555
             (insert after))))))
 
2556
 
 
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'."
 
2561
  (save-excursion
 
2562
    (while (not (looking-at "{\\\\[a-zA-Z]+ "))
 
2563
      (up-list -1))
 
2564
    (forward-sexp)
 
2565
    (save-excursion
 
2566
      (replace-match start t t))
 
2567
    (if (save-excursion
 
2568
          (backward-char 3)
 
2569
          (if (looking-at (regexp-quote "\\/}"))
 
2570
              (progn
 
2571
                (delete-char 3)
 
2572
                nil)
 
2573
            t))
 
2574
        (delete-backward-char 1))
 
2575
    (insert end)))
 
2576
 
 
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)
 
2583
    (while font-list
 
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 "\\|"))
 
2592
    (save-excursion
 
2593
      (catch 'done
 
2594
        (while t
 
2595
          (if (/= ?\\ (following-char))
 
2596
              (skip-chars-backward "a-zA-Z "))
 
2597
          (skip-chars-backward (regexp-quote TeX-esc))
 
2598
          (if (looking-at regexp)
 
2599
              (throw 'done t)
 
2600
            (up-list -1))))
 
2601
      (forward-sexp 2)
 
2602
      (save-excursion
 
2603
        (replace-match start t t))
 
2604
      (delete-backward-char 1)
 
2605
      (insert end))))
 
2606
 
 
2607
;;; Dollars
 
2608
;;
 
2609
;; Rewritten from scratch with use of `texmathp' by 
 
2610
;; Carsten Dominik <dominik@strw.leidenuniv.nl>
 
2611
 
 
2612
(defvar TeX-symbol-marker nil)
 
2613
 
 
2614
(defvar TeX-symbol-marker-pos 0)
 
2615
 
 
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)))
 
2624
 
 
2625
(defun TeX-insert-dollar (&optional arg)
 
2626
  "Insert dollar sign.
 
2627
 
 
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.
 
2632
 
 
2633
With raw C-u prefix, insert exactly one dollar sign.
 
2634
With optional ARG, insert that many dollar signs."
 
2635
  (interactive "P")
 
2636
  (cond
 
2637
   ((and arg (listp arg))
 
2638
    ;; C-u always inserts one
 
2639
    (insert "$"))
 
2640
   (arg
 
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.
 
2645
    (insert "$"))
 
2646
   ((texmathp)
 
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.
 
2651
        (progn
 
2652
          (insert (car texmathp-why))
 
2653
          (save-excursion
 
2654
            (goto-char (cdr texmathp-why))
 
2655
            (if (pos-visible-in-window-p)
 
2656
                (sit-for 1)
 
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))))
 
2663
   (t
 
2664
    ;; Just somewhere in the text.
 
2665
    (insert "$"))))
 
2666
 
 
2667
(defun TeX-point-is-escaped ()
 
2668
  ;; Count backslashes before point and return t if number is odd.
 
2669
  (let (odd)
 
2670
    (save-excursion
 
2671
      (while (equal (preceding-char) ?\\)
 
2672
        (progn
 
2673
          (forward-char -1)
 
2674
          (setq odd (not odd)))))
 
2675
    odd))
 
2676
 
 
2677
;;; Simple Commands
 
2678
 
 
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."
 
2683
  (interactive "*P")
 
2684
  (if arg
 
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)
 
2691
        (save-buffer)
 
2692
      (TeX-auto-write)))
 
2693
  (normal-mode)
 
2694
  (TeX-update-style))
 
2695
 
 
2696
(defgroup TeX-quote nil
 
2697
  "Quoting in AUC TeX."
 
2698
  :group 'AUC-TeX)
 
2699
 
 
2700
(defcustom TeX-open-quote "``"
 
2701
  "*String inserted by typing \\[TeX-insert-quote] to open a quotation."
 
2702
  :group 'TeX-quote
 
2703
  :type 'string)
 
2704
 
 
2705
(defcustom TeX-close-quote "''"
 
2706
  "*String inserted by typing \\[TeX-insert-quote] to close a quotation."
 
2707
  :group 'TeX-quote
 
2708
  :type 'string)
 
2709
 
 
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 \"."
 
2713
  :group 'TeX-quote
 
2714
  :type 'boolean)
 
2715
 
 
2716
;;;###autoload
 
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."
 
2723
  (interactive "*P")
 
2724
  (if arg
 
2725
      (self-insert-command (prefix-numeric-value arg))
 
2726
    (TeX-update-style)
 
2727
    (if TeX-quote-after-quote
 
2728
        (insert (cond ((bobp)
 
2729
                       ?\")
 
2730
                      ((not (= (preceding-char) ?\"))
 
2731
                       ?\")
 
2732
                      ((save-excursion
 
2733
                         (forward-char -1)
 
2734
                         (bobp))
 
2735
                       (delete-backward-char 1)
 
2736
                       TeX-open-quote)
 
2737
                      ((save-excursion
 
2738
                         (forward-char -2) ;;; at -1 there is double quote
 
2739
                         (looking-at "[ \t\n]\\|\\s("))
 
2740
                       (delete-backward-char 1)
 
2741
                       TeX-open-quote)
 
2742
                      (t
 
2743
                       (delete-backward-char 1)
 
2744
                       TeX-close-quote)))
 
2745
      (insert (cond ((bobp)
 
2746
                     TeX-open-quote)
 
2747
                    ((= (preceding-char) (string-to-char TeX-esc))
 
2748
                     ?\")
 
2749
                    ((= (preceding-char) ?\")
 
2750
                     ?\")
 
2751
                    ((save-excursion
 
2752
                       (forward-char (- (length TeX-open-quote)))
 
2753
                       (looking-at (regexp-quote TeX-open-quote)))
 
2754
                     (delete-backward-char (length TeX-open-quote))
 
2755
                     ?\")
 
2756
                    ((save-excursion
 
2757
                       (forward-char (- (length TeX-close-quote)))
 
2758
                       (looking-at (regexp-quote TeX-close-quote)))
 
2759
                     (delete-backward-char (length TeX-close-quote))
 
2760
                     ?\")
 
2761
                    ((save-excursion
 
2762
                       (forward-char -1)
 
2763
                       (looking-at "[ \t\n]\\|\\s("))
 
2764
                     TeX-open-quote)
 
2765
                    (t
 
2766
                     TeX-close-quote))))))
 
2767
 
 
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)
 
2771
 
 
2772
(defun TeX-insert-punctuation ()
 
2773
  "Insert point or comma, cleaning up preceding space."
 
2774
  (interactive)
 
2775
  (expand-abbrev)
 
2776
  (if (TeX-looking-at-backward "\\\\/\\(}+\\)" 50)
 
2777
      (replace-match "\\1" t))
 
2778
  (call-interactively 'self-insert-command))
 
2779
 
 
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
 
2783
between."
 
2784
  (interactive "P")
 
2785
  (insert TeX-grop)
 
2786
  (save-excursion
 
2787
    (if arg (forward-sexp (prefix-numeric-value arg)))
 
2788
    (insert TeX-grcl)))
 
2789
 
 
2790
(defun TeX-goto-info-page ()
 
2791
  "Read documentation for AUC TeX in the info system."
 
2792
  (interactive)
 
2793
  (require 'info)
 
2794
  (Info-goto-node "(auctex)"))
 
2795
 
 
2796
;;;###autoload
 
2797
(defun TeX-submit-bug-report ()
 
2798
  "Submit via mail a bug report on AUC TeX"
 
2799
  (interactive)
 
2800
  (require 'reporter)
 
2801
  (reporter-submit-bug-report
 
2802
   "auc-tex@sunsite.dk"
 
2803
   (concat "AUC TeX " AUC-TeX-version)
 
2804
   (list 'window-system
 
2805
         'LaTeX-version
 
2806
         'TeX-style-path
 
2807
         'TeX-auto-save
 
2808
         'TeX-parse-self
 
2809
         'TeX-master)
 
2810
   nil nil
 
2811
   "Remember to cover the basics, that is, what you expected to happen and
 
2812
what in fact did happen."))
 
2813
 
 
2814
;;; Ispell Support
 
2815
 
 
2816
;; The FSF ispell.el use this.
 
2817
(defun ispell-tex-buffer-p ()
 
2818
  (and (boundp 'ispell-tex-p) ispell-tex-p))
 
2819
 
 
2820
;; The FSF ispell.el might one day use this.
 
2821
(setq ispell-enable-tex-parser t)
 
2822
 
 
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)
 
2831
         (ispell-buffer))
 
2832
        ((fboundp 'ispell)
 
2833
         (ispell))
 
2834
        (t 
 
2835
         (spell-buffer))))
 
2836
 
 
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))
 
2842
 
 
2843
(provide 'tex)
 
2844
 
 
2845
;;; tex.el ends here