1;;; texinfo.el --- major mode for editing Texinfo files
2
3;; Copyright (C) 1985, '88, '89, '90, '91,
4;;                '92, '93, '96, '97 Free Software Foundation, Inc.
5
6;; Author: Robert J. Chassell
7;; Date:   [Set date below for texinfo-version]
8;; Maintainer: bug-texinfo@prep.ai.mit.edu
9;; Keywords: maint, tex, docs
10
11;; This file is part of GNU Emacs.
12
13;; GNU Emacs 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;; GNU Emacs 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 GNU Emacs; see the file COPYING.  If not, write to the
25;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26;; Boston, MA 02111-1307, USA.
27
28;;; Code:
29
30(defgroup texinfo nil
31  "Texinfo Mode"
32  :group 'docs)
33
34(defvar texinfo-version "2.37 of 24 May 1997")
35
36(defun texinfo-version (&optional here)
37  "Show the version of texinfo.el in the minibuffer.
38If optional argument HERE is non-nil, insert info at point."
39  (interactive "P")
40  (let ((version-string
41         (format "Version of \`texinfo.el\': %s" texinfo-version)))
42    (if here
43        (insert version-string)
44      (if (interactive-p)
45          (message "%s" version-string)
46        version-string))))
47
48
49;;; Autoloads:
50
51(autoload 'makeinfo-region
52  "makeinfo"
53  "Make Info file from region of current Texinfo file, and switch to it.
54
55This command does not offer the `next-error' feature since it would
56apply to a temporary file, not the original; use the `makeinfo-buffer'
57command to gain use of `next-error'."
58  t nil)
59
60(autoload 'makeinfo-buffer
61  "makeinfo"
62  "Make Info file from current buffer.
63
64Use the \\[next-error] command to move to the next error
65\(if there are errors\)."
66  t nil)
67
68(autoload 'kill-compilation
69  "compile"
70  "Kill the process made by the \\[compile] command."
71  t nil)
72
73(autoload 'makeinfo-recenter-compilation-buffer
74  "makeinfo"
75  "Redisplay `*compilation*' buffer so most recent output can be seen.
76The last line of the buffer is displayed on
77line LINE of the window, or centered if LINE is nil."
78  t nil)
79
80(autoload 'texinfo-update-node
81  "texnfo-upd"
82  "Without any prefix argument, update the node in which point is located.
83Non-nil argument (prefix, if interactive) means update the nodes in the
84marked region.
85
86The functions for creating or updating nodes and menus, and their
87keybindings, are:
88
89    texinfo-update-node (&optional region-p)    \\[texinfo-update-node]
90    texinfo-every-node-update ()                \\[texinfo-every-node-update]
91    texinfo-sequential-node-update (&optional region-p)
92
93    texinfo-make-menu (&optional region-p)      \\[texinfo-make-menu]
94    texinfo-all-menus-update ()                 \\[texinfo-all-menus-update]
95    texinfo-master-menu ()
96
97    texinfo-indent-menu-description (column &optional region-p)
98
99The `texinfo-column-for-description' variable specifies the column to
100which menu descriptions are indented. Its default value is 32."
101  t nil)
102
103(autoload 'texinfo-every-node-update
104  "texnfo-upd"
105  "Update every node in a Texinfo file."
106  t nil)
107
108(autoload 'texinfo-sequential-node-update
109  "texnfo-upd"
110  "Update one node (or many) in a Texinfo file with sequential pointers.
111
112This function causes the `Next' or `Previous' pointer to point to the
113immediately preceding or following node, even if it is at a higher or
114lower hierarchical level in the document.  Continually pressing `n' or
115`p' takes you straight through the file.
116
117Without any prefix argument, update the node in which point is located.
118Non-nil argument (prefix, if interactive) means update the nodes in the
119marked region.
120
121This command makes it awkward to navigate among sections and
122subsections; it should be used only for those documents that are meant
123to be read like a novel rather than a reference, and for which the
124Info `g*' command is inadequate."
125  t nil)
126
127(autoload 'texinfo-make-menu
128  "texnfo-upd"
129  "Without any prefix argument, make or update a menu.
130Make the menu for the section enclosing the node found following point.
131
132Non-nil argument (prefix, if interactive) means make or update menus
133for nodes within or part of the marked region.
134
135Whenever a menu exists, and is being updated, the descriptions that
136are associated with node names in the pre-existing menu are
137incorporated into the new menu.  Otherwise, the nodes' section titles
138are inserted as descriptions."
139  t nil)
140
141(autoload 'texinfo-all-menus-update
142  "texnfo-upd"
143  "Update every regular menu in a Texinfo file.
144Remove pre-existing master menu, if there is one.
145
146If called with a non-nil argument, this function first updates all the
147nodes in the buffer before updating the menus."
148  t nil)
149
150(autoload 'texinfo-master-menu
151  "texnfo-upd"
152  "Make a master menu for a whole Texinfo file.
153Non-nil argument (prefix, if interactive) means first update all
154existing nodes and menus.  Remove pre-existing master menu, if there is one.
155
156This function creates a master menu that follows the top node.  The
157master menu includes every entry from all the other menus.  It
158replaces any existing ordinary menu that follows the top node.
159
160If called with a non-nil argument, this function first updates all the
161menus in the buffer (incorporating descriptions from pre-existing
162menus) before it constructs the master menu.
163
164The function removes the detailed part of an already existing master
165menu.  This action depends on the pre-existing master menu using the
166standard `texinfo-master-menu-header'.
167
168The master menu has the following format, which is adapted from the
169recommendation in the Texinfo Manual:
170
171   * The first part contains the major nodes in the Texinfo file: the
172     nodes for the chapters, chapter-like sections, and the major
173     appendices.  This includes the indices, so long as they are in
174     chapter-like sections, such as unnumbered sections.
175
176   * The second and subsequent parts contain a listing of the other,
177     lower level menus, in order.  This way, an inquirer can go
178     directly to a particular node if he or she is searching for
179     specific information.
180
181Each of the menus in the detailed node listing is introduced by the
182title of the section containing the menu."
183  t nil)
184
185(autoload 'texinfo-indent-menu-description
186  "texnfo-upd"
187  "Indent every description in menu following point to COLUMN.
188Non-nil argument (prefix, if interactive) means indent every
189description in every menu in the region.  Does not indent second and
190subsequent lines of a multi-line description."
191  t nil)
192
193(autoload 'texinfo-insert-node-lines
194  "texnfo-upd"
195  "Insert missing `@node' lines in region of Texinfo file.
196Non-nil argument (prefix, if interactive) means also to insert the
197section titles as node names; and also to insert the section titles as
198node names in pre-existing @node lines that lack names."
199  t nil)
200
201(autoload 'texinfo-start-menu-description
202  "texnfo-upd"
203  "In this menu entry, insert the node's section title as a description.
204Position point at beginning of description ready for editing.
205Do not insert a title if the line contains an existing description.
206
207You will need to edit the inserted text since a useful description
208complements the node name rather than repeats it as a title does."
209  t nil)
210
211(autoload 'texinfo-multiple-files-update
212  "texnfo-upd"
213  "Update first node pointers in each file included in OUTER-FILE;
214create or update main menu in the outer file that refers to such nodes.
215This does not create or update menus or pointers within the included files.
216
217With optional MAKE-MASTER-MENU argument (prefix arg, if interactive),
218insert a master menu in OUTER-FILE.  This does not create or update
219menus or pointers within the included files.
220
221With optional UPDATE-EVERYTHING argument (numeric prefix arg, if
222interactive), update all the menus and all the `Next', `Previous', and
223`Up' pointers of all the files included in OUTER-FILE before inserting
224a master menu in OUTER-FILE.
225
226The command also updates the `Top' level node pointers of OUTER-FILE.
227
228Notes:
229
230  * this command does NOT save any files--you must save the
231    outer file and any modified, included files.
232
233  * except for the `Top' node, this command does NOT handle any
234    pre-existing nodes in the outer file; hence, indices must be
235    enclosed in an included file.
236
237Requirements:
238
239  * each of the included files must contain exactly one highest
240    hierarchical level node,
241  * this highest node must be the first node in the included file,
242  * each highest hierarchical level node must be of the same type.
243
244Thus, normally, each included file contains one, and only one,
245chapter."
246  t nil)
247
248
249;;; Code:
250
251;;; Don't you dare insert any `require' calls at top level in this file--rms.
252
253;;; Syntax table
254
255(defvar texinfo-mode-syntax-table nil)
256
257(if texinfo-mode-syntax-table
258    nil
259  (setq texinfo-mode-syntax-table (make-syntax-table))
260  (modify-syntax-entry ?\" " " texinfo-mode-syntax-table)
261  (modify-syntax-entry ?\\ " " texinfo-mode-syntax-table)
262  (modify-syntax-entry ?@ "\\" texinfo-mode-syntax-table)
263  (modify-syntax-entry ?\^q "\\" texinfo-mode-syntax-table)
264  (modify-syntax-entry ?\[ "(]" texinfo-mode-syntax-table)
265  (modify-syntax-entry ?\] ")[" texinfo-mode-syntax-table)
266  (modify-syntax-entry ?{ "(}" texinfo-mode-syntax-table)
267  (modify-syntax-entry ?} "){" texinfo-mode-syntax-table)
268  (modify-syntax-entry ?\' "w" texinfo-mode-syntax-table))
269
270;; Written by Wolfgang Bangerth <zcg51122@rpool1.rus.uni-stuttgart.de>
271;; To override this example, set either `imenu-generic-expression'
272;; or `imenu-create-index-function'.
273(defvar texinfo-imenu-generic-expression
274  '((nil "^@node[ \t]+\\([^,\n]*\\)" 1)
275    ("Chapters" "^@chapter[ \t]+\\(.*\\)$" 1))
276
277  "Imenu generic expression for TexInfo mode.  See `imenu-generic-expression'.")
278
279(defvar texinfo-font-lock-keywords
280  '(;; All but the first 2 had an OVERRIDE of t.
281    ;; It didn't seem to be any better, and it's slower--simon.
282    ("^\\(@c\\|@comment\\)\\>.*" . font-lock-comment-face) ;comments
283    ;; Robert J. Chassell <bob@gnu.ai.mit.edu> says remove this line.
284    ;;("\\$\\([^$]*\\)\\$" 1 font-lock-string-face t)
285    ("@\\([a-zA-Z]+\\|[^ \t\n]\\)" 1 font-lock-keyword-face) ;commands
286    ("^\\*\\(.*\\)[\t ]*$" 1 font-lock-function-name-face t) ;menu items
287    ("@\\(emph\\|strong\\|b\\|i\\){\\([^}]+\\)" 2 font-lock-comment-face)
288    ("@\\(file\\|kbd\\|key\\|url\\|email\\){\\([^}]+\\)" 2 font-lock-string-face)
289    ("@\\(samp\\|code\\|var\\|math\\){\\([^}]+\\)"
290     2 font-lock-variable-name-face)
291    ("@\\(cite\\|xref\\|pxref\\){\\([^}]+\\)" 2 font-lock-reference-face)
292    ("@\\(end\\|itemx?\\) +\\(.+\\)" 2 font-lock-function-name-face keep)
293    )
294  "Additional expressions to highlight in TeXinfo mode.")
295
296(defvar texinfo-section-list
297  '(("top" 1)
298    ("majorheading" 1)
299    ("chapter" 2)
300    ("unnumbered" 2)
301    ("appendix" 2)
302    ("chapheading" 2)
303    ("section" 3)
304    ("unnumberedsec" 3)
305    ("appendixsec" 3)
306    ("heading" 3)
307    ("subsection" 4)
308    ("unnumberedsubsec" 4)
309    ("appendixsubsec" 4)
310    ("subheading" 4)
311    ("subsubsection" 5)
312    ("unnumberedsubsubsec" 5)
313    ("appendixsubsubsec" 5)
314    ("subsubheading" 5))
315  "Alist of sectioning commands and their relative level.")
316
317(defun texinfo-outline-level ()
318  ;; Calculate level of current texinfo outline heading.
319  (save-excursion
320    (if (bobp)
321        0
322      (forward-char 1)
323      (let* ((word (buffer-substring-no-properties
324                    (point) (progn (forward-word 1) (point))))
325             (entry (assoc word texinfo-section-list)))
326        (if entry
327            (nth 1 entry)
328          5)))))
329
330
331;;; Keybindings
332(defvar texinfo-mode-map nil)
333
334;;; Keys common both to Texinfo mode and to TeX shell.
335
336(defun texinfo-define-common-keys (keymap)
337  "Define the keys both in Texinfo mode and in the texinfo-tex-shell."
338  (define-key keymap "\C-c\C-t\C-k"    'tex-kill-job)
339  (define-key keymap "\C-c\C-t\C-x"    'texinfo-quit-job)
340  (define-key keymap "\C-c\C-t\C-l"    'tex-recenter-output-buffer)
341  (define-key keymap "\C-c\C-t\C-d"    'texinfo-delete-from-print-queue)
342  (define-key keymap "\C-c\C-t\C-q"    'tex-show-print-queue)
343  (define-key keymap "\C-c\C-t\C-p"    'texinfo-tex-print)
344  (define-key keymap "\C-c\C-t\C-v"    'texinfo-tex-view)
345  (define-key keymap "\C-c\C-t\C-i"    'texinfo-texindex)
346
347  (define-key keymap "\C-c\C-t\C-r"    'texinfo-tex-region)
348  (define-key keymap "\C-c\C-t\C-b"    'texinfo-tex-buffer))
349
350;; Mode documentation displays commands in reverse order
351;; from how they are listed in the texinfo-mode-map.
352
353(if texinfo-mode-map
354    nil
355  (setq texinfo-mode-map (make-sparse-keymap))
356
357  ;; bindings for `texnfo-tex.el'
358  (texinfo-define-common-keys texinfo-mode-map)
359
360  ;; bindings for `makeinfo.el'
361  (define-key texinfo-mode-map "\C-c\C-m\C-k" 'kill-compilation)
362  (define-key texinfo-mode-map "\C-c\C-m\C-l"
363    'makeinfo-recenter-compilation-buffer)
364  (define-key texinfo-mode-map "\C-c\C-m\C-r" 'makeinfo-region)
365  (define-key texinfo-mode-map "\C-c\C-m\C-b" 'makeinfo-buffer)
366
367  ;; bindings for `texinfmt.el'
368  (define-key texinfo-mode-map "\C-c\C-e\C-r"    'texinfo-format-region)
369  (define-key texinfo-mode-map "\C-c\C-e\C-b"    'texinfo-format-buffer)
370
371  ;; bindings for updating nodes and menus
372
373  (define-key texinfo-mode-map "\C-c\C-um"   'texinfo-master-menu)
374
375  (define-key texinfo-mode-map "\C-c\C-u\C-m"   'texinfo-make-menu)
376  (define-key texinfo-mode-map "\C-c\C-u\C-n"   'texinfo-update-node)
377  (define-key texinfo-mode-map "\C-c\C-u\C-e"   'texinfo-every-node-update)
378  (define-key texinfo-mode-map "\C-c\C-u\C-a"   'texinfo-all-menus-update)
379
380  (define-key texinfo-mode-map "\C-c\C-s"     'texinfo-show-structure)
381
382  (define-key texinfo-mode-map "\C-c}"          'up-list)
383  (define-key texinfo-mode-map "\C-c]"          'up-list)
384  (define-key texinfo-mode-map "\C-c{"          'texinfo-insert-braces)
385
386  ;; bindings for inserting strings
387
388  (define-key texinfo-mode-map "\C-c\C-c\C-d" 'texinfo-start-menu-description)
389
390  (define-key texinfo-mode-map "\C-c\C-cv"    'texinfo-insert-@var)
391  (define-key texinfo-mode-map "\C-c\C-ct"    'texinfo-insert-@table)
392  (define-key texinfo-mode-map "\C-c\C-cs"    'texinfo-insert-@samp)
393  (define-key texinfo-mode-map "\C-c\C-co"    'texinfo-insert-@noindent)
394  (define-key texinfo-mode-map "\C-c\C-cn"    'texinfo-insert-@node)
395  (define-key texinfo-mode-map "\C-c\C-ck"    'texinfo-insert-@kbd)
396  (define-key texinfo-mode-map "\C-c\C-ci"    'texinfo-insert-@item)
397  (define-key texinfo-mode-map "\C-c\C-cf"    'texinfo-insert-@file)
398  (define-key texinfo-mode-map "\C-c\C-cx"    'texinfo-insert-@example)
399  (define-key texinfo-mode-map "\C-c\C-ce"    'texinfo-insert-@end)
400  (define-key texinfo-mode-map "\C-c\C-cd"    'texinfo-insert-@dfn)
401  (define-key texinfo-mode-map "\C-c\C-cc"    'texinfo-insert-@code))
402
403
404;;; Texinfo mode
405
406(defvar texinfo-chapter-level-regexp
407  "chapter\\|unnumbered \\|appendix \\|majorheading\\|chapheading"
408  "Regular expression matching Texinfo chapter-level headings.
409This does not match `@node' and does not match the `@top' command.")
410
411;;;###autoload
412(defun texinfo-mode ()
413  "Major mode for editing Texinfo files.
414
415  It has these extra commands:
416\\{texinfo-mode-map}
417
418  These are files that are used as input for TeX to make printed manuals
419and also to be turned into Info files with \\[makeinfo-buffer] or
420the `makeinfo' program.  These files must be written in a very restricted and
421modified version of TeX input format.
422
423  Editing commands are like text-mode except that the syntax table is
424set up so expression commands skip Texinfo bracket groups.  To see
425what the Info version of a region of the Texinfo file will look like,
426use \\[makeinfo-region], which runs `makeinfo' on the current region.
427
428  You can show the structure of a Texinfo file with \\[texinfo-show-structure].
429This command shows the structure of a Texinfo file by listing the
430lines with the @-sign commands for @chapter, @section, and the like.
431These lines are displayed in another window called the *Occur* window.
432In that window, you can position the cursor over one of the lines and
433use \\[occur-mode-goto-occurrence], to jump to the corresponding spot
434in the Texinfo file.
435
436  In addition, Texinfo mode provides commands that insert various
437frequently used @-sign commands into the buffer.  You can use these
438commands to save keystrokes.  And you can insert balanced braces with
439\\[texinfo-insert-braces] and later use the command \\[up-list] to
440move forward past the closing brace.
441
442Also, Texinfo mode provides functions for automatically creating or
443updating menus and node pointers.  These functions
444
445  * insert the `Next', `Previous' and `Up' pointers of a node,
446  * insert or update the menu for a section, and
447  * create a master menu for a Texinfo source file.
448
449Here are the functions:
450
451    texinfo-update-node                \\[texinfo-update-node]
452    texinfo-every-node-update          \\[texinfo-every-node-update]
453    texinfo-sequential-node-update
454
455    texinfo-make-menu                  \\[texinfo-make-menu]
456    texinfo-all-menus-update           \\[texinfo-all-menus-update]
457    texinfo-master-menu
458
459    texinfo-indent-menu-description (column &optional region-p)
460
461The `texinfo-column-for-description' variable specifies the column to
462which menu descriptions are indented.
463
464Passed an argument (a prefix argument, if interactive), the
465`texinfo-update-node' and `texinfo-make-menu' functions do their jobs
466in the region.
467
468To use the updating commands, you must structure your Texinfo file
469hierarchically, such that each `@node' line, with the exception of the
470Top node, is accompanied by some kind of section line, such as an
471`@chapter' or `@section' line.
472
473If the file has a `top' node, it must be called `top' or `Top' and
474be the first node in the file.
475
476Entering Texinfo mode calls the value of text-mode-hook, and then the
477value of texinfo-mode-hook."
478  (interactive)
479  (text-mode)
480  (setq mode-name "Texinfo")
481  (setq major-mode 'texinfo-mode)
482  (use-local-map texinfo-mode-map)
483  (set-syntax-table texinfo-mode-syntax-table)
484  (make-local-variable 'page-delimiter)
485  (setq page-delimiter
486        (concat
487         "^@node [ \t]*[Tt]op\\|^@\\("
488         texinfo-chapter-level-regexp
489         "\\)"))
490  (make-local-variable 'require-final-newline)
491  (setq require-final-newline t)
492  (make-local-variable 'indent-tabs-mode)
493  (setq indent-tabs-mode nil)
494  (make-local-variable 'paragraph-separate)
495  (setq paragraph-separate
496	(concat "\b\\|@[a-zA-Z]*[ \n]\\|" paragraph-separate))
497  (make-local-variable 'paragraph-start)
498  (setq paragraph-start (concat "\b\\|@[a-zA-Z]*[ \n]\\|" paragraph-start))
499  (make-local-variable 'fill-column)
500  (setq fill-column 72)
501  (make-local-variable 'comment-start)
502  (setq comment-start "@c ")
503  (make-local-variable 'comment-start-skip)
504  (setq comment-start-skip "@c +")
505  (make-local-variable 'words-include-escapes)
506  (setq words-include-escapes t)
507  (make-local-variable 'imenu-generic-expression)
508  (setq imenu-generic-expression texinfo-imenu-generic-expression)
509  (make-local-variable 'font-lock-defaults)
510  (setq font-lock-defaults '(texinfo-font-lock-keywords t))
511  (make-local-variable 'outline-regexp)
512  (setq outline-regexp
513        (concat "@\\("
514                (mapconcat 'car texinfo-section-list "\\>\\|")
515                "\\>\\)"))
516  (make-local-variable 'outline-level)
517  (setq outline-level 'texinfo-outline-level)
518  (make-local-variable 'tex-start-of-header)
519  (setq tex-start-of-header "%\\*\\*start")
520  (make-local-variable 'tex-end-of-header)
521  (setq tex-end-of-header "%\\*\\*end")
522  (make-local-variable 'tex-first-line-header-regexp)
523  (setq tex-first-line-header-regexp "^\\\\input")
524  (make-local-variable 'tex-trailer)
525  (setq tex-trailer "@bye\n")
526  (run-hooks 'text-mode-hook 'texinfo-mode-hook))
527
528
529;;; Insert string commands
530
531;; Keep as concatinated lists for ease of maintenance
532(defconst texinfo-environment-regexp
533  (concat
534   "^@"
535   "\\("
536   "cartouche\\|"
537   "display\\|"
538   "end\\|"
539   "enumerate\\|"
540   "example\\|"
541   "f?table\\|"
542   "flushleft\\|"
543   "flushright\\|"
544   "format\\|"
545   "group\\|"
546   "ifhtml\\|"
547   "ifinfo\\|"
548   "iftex\\|"
549   "ignore\\|"
550   "itemize\\|"
551   "lisp\\|"
552   "macro\\|"
553   "multitable\\|"
554   "quotation\\|"
555   "smallexample\\|"
556   "smalllisp\\|"
557   "tex"
558   "\\)")
559  "Regexp for environment-like TexInfo list commands.
560   Subexpression 1 is what goes into the corresponding `@end' statement.")
561
562;; The following texinfo-insert-@end command not only inserts a SPC
563;; after the @end, but tries to find out what belongs there.  It is
564;; not very smart: it does not understand nested lists.
565
566(defun texinfo-insert-@end ()
567  "Insert the matching `@end' for the last Texinfo command that needs one."
568  (interactive)
569  (let ((depth 1) string)
570    (save-excursion
571      (while (and (> depth 0)
572                  (re-search-backward texinfo-environment-regexp nil t))
573	(if (looking-at "@end")
574	    (setq depth (1+ depth))
575	  (setq depth (1- depth))))
576      (looking-at texinfo-environment-regexp)
577      (if (zerop depth)
578          (setq string
579                (buffer-substring (match-beginning 1)
580                                  (match-end 1)))))
581    (insert "@end ")
582    (if string (insert string "\n"))))
583
584;; The following insert commands accept a prefix arg N, which is the
585;; number of words (actually s-exprs) that should be surrounded by
586;; braces.  Thus you can first paste a variable name into a .texinfo
587;; buffer, then say C-u 1 C-c C-c v at the beginning of the just
588;; pasted variable name to put @var{...} *around* the variable name.
589;; Operate on previous word or words with negative arg.
590
591;; These commands use texinfo-insert-@-with-arg
592(defun texinfo-insert-@-with-arg (string &optional arg)
593  (if arg
594      (progn
595        (setq arg (prefix-numeric-value arg))
596        (if (< arg 0)
597            (progn
598              (skip-chars-backward " \t\n\r\f")
599              (save-excursion
600                (forward-sexp arg)
601                (insert "@" string "{"))
602              (insert "}"))
603          (skip-chars-forward " \t\n\r\f")
604          (insert "@" string "{")
605          (forward-sexp arg)
606          (insert "}")))
607    (insert "@" string "{}")
608    (backward-char)))
609
610(defun texinfo-insert-braces ()
611  "Make a pair of braces and be poised to type inside of them.
612Use \\[up-list] to move forward out of the braces."
613  (interactive)
614  (insert "{}")
615  (backward-char))
616
617(defun texinfo-insert-@code (&optional arg)
618  "Insert a `@code{...}' command in a Texinfo buffer.
619A numeric argument says how many words the braces should surround.
620The default is not to surround any existing words with the braces."
621  (interactive "P")
622  (texinfo-insert-@-with-arg "code" arg))
623
624(defun texinfo-insert-@dfn (&optional arg)
625  "Insert a `@dfn{...}' command in a Texinfo buffer.
626A numeric argument says how many words the braces should surround.
627The default is not to surround any existing words with the braces."
628  (interactive "P")
629  (texinfo-insert-@-with-arg "dfn" arg))
630
631(defun texinfo-insert-@example ()
632  "Insert the string `@example' in a Texinfo buffer."
633  (interactive)
634  (insert "@example\n"))
635
636(defun texinfo-insert-@file (&optional arg)
637  "Insert a `@file{...}' command in a Texinfo buffer.
638A numeric argument says how many words the braces should surround.
639The default is not to surround any existing words with the braces."
640  (interactive "P")
641  (texinfo-insert-@-with-arg "file" arg))
642
643(defun texinfo-insert-@item ()
644  "Insert the string `@item' in a Texinfo buffer."
645  (interactive)
646  (insert "@item")
647  (newline))
648
649(defun texinfo-insert-@kbd (&optional arg)
650  "Insert a `@kbd{...}' command in a Texinfo buffer.
651A numeric argument says how many words the braces should surround.
652The default is not to surround any existing words with the braces."
653  (interactive "P")
654  (texinfo-insert-@-with-arg "kbd" arg))
655
656(defun texinfo-insert-@node ()
657  "Insert the string `@node' in a Texinfo buffer.
658This also inserts on the following line a comment indicating
659the order of arguments to @node."
660  (interactive)
661  (insert "@node \n@comment  node-name,  next,  previous,  up")
662  (forward-line -1)
663  (forward-char 6))
664
665(defun texinfo-insert-@noindent ()
666  "Insert the string `@noindent' in a Texinfo buffer."
667  (interactive)
668  (insert "@noindent\n"))
669
670(defun texinfo-insert-@samp (&optional arg)
671  "Insert a `@samp{...}' command in a Texinfo buffer.
672A numeric argument says how many words the braces should surround.
673The default is not to surround any existing words with the braces."
674  (interactive "P")
675  (texinfo-insert-@-with-arg "samp" arg))
676
677(defun texinfo-insert-@table (&optional arg)
678  "Insert the string `@table' in a Texinfo buffer."
679  (interactive "P")
680  (insert "@table "))
681
682(defun texinfo-insert-@var (&optional arg)
683  "Insert a `@var{}' command in a Texinfo buffer.
684A numeric argument says how many words the braces should surround.
685The default is not to surround any existing words with the braces."
686  (interactive "P")
687  (texinfo-insert-@-with-arg "var" arg))
688
689;;; Texinfo file structure
690
691;; These are defined in texnfo-upd.el.  defvars here avoid warnings.
692(defvar texinfo-section-types-regexp)
693(defvar texinfo-section-level-regexp)
694(defvar texinfo-subsection-level-regexp)
695(defvar texinfo-subsubsection-level-regexp)
696
697(defun texinfo-show-structure (&optional nodes-too)
698  "Show the structure of a Texinfo file.
699List the lines in the file that begin with the @-sign commands for
700@chapter, @section, and the like.
701
702With optional argument (prefix if interactive), list both the lines
703with @-sign commands for @chapter, @section, and the like, and list
704@node lines.
705
706Lines with structuring commands beginning in them are displayed in
707another buffer named `*Occur*'.  In that buffer, you can move point to
708one of those lines and then use \\\\[occur-mode-goto-occurrence],
709to jump to the corresponding spot in the Texinfo source file."
710
711  (interactive "P")
712  (require 'texnfo-upd)
713  (save-excursion
714    (goto-char (point-min))
715    (if nodes-too
716        (occur (concat "\\(^@node\\)\\|" texinfo-section-types-regexp))
717      (occur texinfo-section-types-regexp)))
718  (pop-to-buffer "*Occur*")
719  (goto-char (point-min))
720  (flush-lines "-----")
721  ;; Now format the "*Occur*" buffer to show the structure.
722  ;; Thanks to ceder@signum.se (Per Cederqvist)
723  (goto-char (point-max))
724  (let ((margin 5))
725    (while (re-search-backward "^ *[0-9]*:" nil 0)
726      (re-search-forward ":")
727      (setq margin
728            (cond
729             ((looking-at
730               (concat "@\\(" texinfo-chapter-level-regexp "\\)")) 5)
731             ;; ((looking-at "@chapter ") 5)
732             ;; ((looking-at "@unnumbered ") 5)
733             ;; ((looking-at "@appendix ") 5)
734             ;; ((looking-at "@majorheading ") 5)
735             ;; ((looking-at "@chapheading ") 5)
736
737             ((looking-at
738               (concat "@\\(" texinfo-section-level-regexp "\\)")) 9)
739             ;; ((looking-at "@section ") 9)
740             ;; ((looking-at "@unnumberedsec ") 9)
741             ;; ((looking-at "@appendixsec ") 9)
742             ;; ((looking-at "@heading ") 9)
743
744             ((looking-at
745               (concat "@\\(" texinfo-subsection-level-regexp "\\)")) 13)
746             ;; ((looking-at "@subsection ") 13)
747             ;; ((looking-at "@unnumberedsubsec ") 13)
748             ;; ((looking-at "@appendixsubsec ") 13)
749             ;; ((looking-at "@subheading ") 13)
750
751             ((looking-at
752               (concat "@\\(" texinfo-subsubsection-level-regexp "\\)")) 17)
753             ;; ((looking-at "@subsubsection ") 17)
754             ;; ((looking-at "@unnumberedsubsubsec ") 17)
755             ;; ((looking-at "@appendixsubsubsec ") 17)
756             ;; ((looking-at "@subsubheading ") 17)
757             (t margin)))
758      (indent-to-column margin)
759      (beginning-of-line))))
760
761;;; The  tex  and  print  function definitions:
762
763(defcustom texinfo-texi2dvi-command "texi2dvi"
764  "*Command used by `texinfo-tex-buffer' to run TeX and texindex on a buffer."
765  :type 'string
766  :group 'texinfo)
767
768(defcustom texinfo-tex-command "tex"
769  "*Command used by `texinfo-tex-region' to run TeX on a region."
770  :type 'string
771  :group 'texinfo)
772
773(defcustom texinfo-texindex-command "texindex"
774  "*Command used by `texinfo-texindex' to sort unsorted index files."
775  :type 'string
776  :group 'texinfo)
777
778(defcustom texinfo-delete-from-print-queue-command "lprm"
779  "*Command string used to delete a job from the line printer queue.
780Command is used by \\[texinfo-delete-from-print-queue] based on
781number provided by a previous \\[tex-show-print-queue]
782command."
783  :type 'string
784  :group 'texinfo)
785
786(defvar texinfo-tex-trailer "@bye"
787  "String appended after a region sent to TeX by `texinfo-tex-region'.")
788
789(defun texinfo-tex-region (beg end)
790  "Run TeX on the current region.
791This works by writing a temporary file (`tex-zap-file') in the directory
792that is the value of `tex-directory', then running TeX on that file.
793
794The first line of the buffer is copied to the
795temporary file; and if the buffer has a header, it is written to the
796temporary file before the region itself.  The buffer's header is all lines
797between the strings defined by `tex-start-of-header' and `tex-end-of-header'
798inclusive.  The header must start in the first 100 lines.
799
800The value of `texinfo-tex-trailer' is appended to the temporary file after the region."
801  (interactive "r")
802  (require 'tex-mode)
803  (let ((tex-command texinfo-tex-command)
804	(tex-trailer "@bye\n"))
805    (tex-region beg end)))
806
807(defun texinfo-tex-buffer ()
808  "Run TeX on visited file, once or twice, to make a correct `.dvi' file."
809  (interactive)
810  (require 'tex-mode)
811  (let ((tex-command texinfo-texi2dvi-command))
812    (tex-buffer)))
813
814(defun texinfo-texindex ()
815  "Run `texindex' on unsorted index files.
816The index files are made by \\[texinfo-tex-region] or \\[texinfo-tex-buffer].
817This runs the shell command defined by `texinfo-texindex-command'."
818  (interactive)
819  (require 'tex-mode)
820  (tex-send-command texinfo-texindex-command (concat tex-zap-file ".??"))
821  ;; alternatively
822  ;; (send-string "tex-shell"
823  ;;              (concat texinfo-texindex-command
824  ;;                      " " tex-zap-file ".??" "\n"))
825  (tex-recenter-output-buffer nil))
826
827(defun texinfo-tex-print ()
828  "Print `.dvi' file made by \\[texinfo-tex-region] or \\[texinfo-tex-buffer].
829This runs the shell command defined by `tex-dvi-print-command'."
830  (interactive)
831  (require 'tex-mode)
832  (tex-print))
833
834(defun texinfo-tex-view ()
835  "View `.dvi' file made by \\[texinfo-tex-region] or \\[texinfo-tex-buffer].
836This runs the shell command defined by `tex-dvi-view-command'."
837  (interactive)
838  (require 'tex-mode)
839  (tex-view))
840
841(defun texinfo-quit-job ()
842  "Quit currently running TeX job, by sending an `x' to it."
843  (interactive)
844  (if (not (get-process "tex-shell"))
845      (error "No TeX shell running"))
846  (tex-send-command "x"))
847;; alternatively:
848;; save-excursion
849;;   (set-buffer (get-buffer "*tex-shell*"))
850;;   (goto-char (point-max))
851;;   (insert "x")
852;;   (comint-send-input)
853
854(defun texinfo-delete-from-print-queue (job-number)
855  "Delete job from the line printer spooling queue.
856You are prompted for the job number (use a number shown by a previous
857\\[tex-show-print-queue] command)."
858  (interactive "nPrinter job number for deletion: ")
859  (require 'tex-mode)
860  (if (tex-shell-running)
861      (tex-kill-job)
862    (tex-start-shell))
863  (tex-send-command texinfo-delete-from-print-queue-command job-number)
864  ;; alternatively
865  ;; (send-string "tex-shell"
866  ;;              (concat
867  ;;               texinfo-delete-from-print-queue-command
868  ;;               " "
869  ;;               job-number"\n"))
870  (tex-recenter-output-buffer nil))
871
872(provide 'texinfo)
873
874;;; texinfo.el ends here
875