]> code.delx.au - gnu-emacs/blob - lisp/textmodes/texinfo.el
Merge from origin/emacs-25
[gnu-emacs] / lisp / textmodes / texinfo.el
1 ;;; texinfo.el --- major mode for editing Texinfo files
2
3 ;; Copyright (C) 1985, 1988-1993, 1996-1997, 2000-2016 Free Software
4 ;; Foundation, Inc.
5
6 ;; Author: Robert J. Chassell
7 ;; Date: [See date below for texinfo-version]
8 ;; Maintainer: emacs-devel@gnu.org
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 3 of the License, or
16 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Todo:
27
28 ;; - facemenu support.
29 ;; - command completion.
30
31 ;;; Commentary:
32
33 ;;; Code:
34
35 (eval-when-compile (require 'tex-mode))
36 (declare-function tex-buffer "tex-mode" ())
37 (declare-function tex-region "tex-mode" (beg end))
38 (declare-function tex-send-command "tex-mode")
39 (declare-function tex-recenter-output-buffer "tex-mode" (linenum))
40 (declare-function tex-print "tex-mode" (&optional alt))
41 (declare-function tex-view "tex-mode" ())
42 (declare-function tex-shell-running "tex-mode" ())
43 (declare-function tex-kill-job "tex-mode" ())
44
45 (defvar outline-heading-alist)
46
47 (defvar skeleton-end-newline)
48
49 (defgroup texinfo nil
50 "Texinfo Mode."
51 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
52 :group 'docs)
53
54 ;;;###autoload
55 (defcustom texinfo-open-quote (purecopy "``")
56 "String inserted by typing \\[texinfo-insert-quote] to open a quotation."
57 :type 'string
58 :group 'texinfo)
59
60 ;;;###autoload
61 (defcustom texinfo-close-quote (purecopy "''")
62 "String inserted by typing \\[texinfo-insert-quote] to close a quotation."
63 :type 'string
64 :group 'texinfo)
65
66 (defcustom texinfo-mode-hook nil
67 "Normal hook run when entering Texinfo mode."
68 :type 'hook
69 :options '(turn-on-auto-fill flyspell-mode)
70 :group 'texinfo)
71
72 \f
73 ;;; Autoloads:
74
75 (autoload 'makeinfo-region
76 "makeinfo"
77 "Make Info file from region of current Texinfo file, and switch to it.
78
79 This command does not offer the `next-error' feature since it would
80 apply to a temporary file, not the original; use the `makeinfo-buffer'
81 command to gain use of `next-error'."
82 t nil)
83
84 (autoload 'makeinfo-buffer
85 "makeinfo"
86 "Make Info file from current buffer.
87
88 Use the \\[next-error] command to move to the next error
89 \(if there are errors)."
90 t nil)
91
92 (autoload 'kill-compilation
93 "compile"
94 "Kill the process made by the \\[compile] command."
95 t nil)
96
97 (autoload 'makeinfo-recenter-compilation-buffer
98 "makeinfo"
99 "Redisplay `*compilation*' buffer so most recent output can be seen.
100 The last line of the buffer is displayed on
101 line LINE of the window, or centered if LINE is nil."
102 t nil)
103
104 (autoload 'texinfo-update-node
105 "texnfo-upd"
106 "Without any prefix argument, update the node in which point is located.
107 Non-nil argument (prefix, if interactive) means update the nodes in the
108 marked region.
109
110 The functions for creating or updating nodes and menus, and their
111 keybindings, are:
112
113 `texinfo-update-node' (&optional region-p) \\[texinfo-update-node]
114 `texinfo-every-node-update' () \\[texinfo-every-node-update]
115 `texinfo-sequential-node-update' (&optional region-p)
116
117 `texinfo-make-menu' (&optional region-p) \\[texinfo-make-menu]
118 `texinfo-all-menus-update' () \\[texinfo-all-menus-update]
119 `texinfo-master-menu' ()
120
121 `texinfo-indent-menu-description' (column &optional region-p)
122
123 The `texinfo-column-for-description' variable specifies the column to
124 which menu descriptions are indented. Its default value is 32."
125 t nil)
126
127 (autoload 'texinfo-every-node-update
128 "texnfo-upd"
129 "Update every node in a Texinfo file."
130 t nil)
131
132 (autoload 'texinfo-sequential-node-update
133 "texnfo-upd"
134 "Update one node (or many) in a Texinfo file with sequential pointers.
135
136 This function causes the `Next' or `Previous' pointer to point to the
137 immediately preceding or following node, even if it is at a higher or
138 lower hierarchical level in the document. Continually pressing `n' or
139 `p' takes you straight through the file.
140
141 Without any prefix argument, update the node in which point is located.
142 Non-nil argument (prefix, if interactive) means update the nodes in the
143 marked region.
144
145 This command makes it awkward to navigate among sections and
146 subsections; it should be used only for those documents that are meant
147 to be read like a novel rather than a reference, and for which the
148 Info `g*' command is inadequate."
149 t nil)
150
151 (autoload 'texinfo-make-menu
152 "texnfo-upd"
153 "Without any prefix argument, make or update a menu.
154 Make the menu for the section enclosing the node found following point.
155
156 Non-nil argument (prefix, if interactive) means make or update menus
157 for nodes within or part of the marked region.
158
159 Whenever a menu exists, and is being updated, the descriptions that
160 are associated with node names in the pre-existing menu are
161 incorporated into the new menu. Otherwise, the nodes' section titles
162 are inserted as descriptions."
163 t nil)
164
165 (autoload 'texinfo-all-menus-update
166 "texnfo-upd"
167 "Update every regular menu in a Texinfo file.
168 Remove pre-existing master menu, if there is one.
169
170 If called with a non-nil argument, this function first updates all the
171 nodes in the buffer before updating the menus."
172 t nil)
173
174 (autoload 'texinfo-master-menu
175 "texnfo-upd"
176 "Make a master menu for a whole Texinfo file.
177 Non-nil argument (prefix, if interactive) means first update all
178 existing nodes and menus. Remove pre-existing master menu, if there is one.
179
180 This function creates a master menu that follows the top node. The
181 master menu includes every entry from all the other menus. It
182 replaces any existing ordinary menu that follows the top node.
183
184 If called with a non-nil argument, this function first updates all the
185 menus in the buffer (incorporating descriptions from pre-existing
186 menus) before it constructs the master menu.
187
188 The function removes the detailed part of an already existing master
189 menu. This action depends on the pre-existing master menu using the
190 standard `texinfo-master-menu-header'.
191
192 The master menu has the following format, which is adapted from the
193 recommendation in the Texinfo Manual:
194
195 * The first part contains the major nodes in the Texinfo file: the
196 nodes for the chapters, chapter-like sections, and the major
197 appendices. This includes the indices, so long as they are in
198 chapter-like sections, such as unnumbered sections.
199
200 * The second and subsequent parts contain a listing of the other,
201 lower level menus, in order. This way, an inquirer can go
202 directly to a particular node if he or she is searching for
203 specific information.
204
205 Each of the menus in the detailed node listing is introduced by the
206 title of the section containing the menu."
207 t nil)
208
209 (autoload 'texinfo-indent-menu-description
210 "texnfo-upd"
211 "Indent every description in menu following point to COLUMN.
212 Non-nil argument (prefix, if interactive) means indent every
213 description in every menu in the region. Does not indent second and
214 subsequent lines of a multi-line description."
215 t nil)
216
217 (autoload 'texinfo-insert-node-lines
218 "texnfo-upd"
219 "Insert missing `@node' lines in region of Texinfo file.
220 Non-nil argument (prefix, if interactive) means also to insert the
221 section titles as node names; and also to insert the section titles as
222 node names in pre-existing @node lines that lack names."
223 t nil)
224
225 (autoload 'texinfo-start-menu-description
226 "texnfo-upd"
227 "In this menu entry, insert the node's section title as a description.
228 Position point at beginning of description ready for editing.
229 Do not insert a title if the line contains an existing description.
230
231 You will need to edit the inserted text since a useful description
232 complements the node name rather than repeats it as a title does."
233 t nil)
234
235 (autoload 'texinfo-multiple-files-update
236 "texnfo-upd"
237 "Update first node pointers in each file included in OUTER-FILE;
238 create or update main menu in the outer file that refers to such nodes.
239 This does not create or update menus or pointers within the included files.
240
241 With optional MAKE-MASTER-MENU argument (prefix arg, if interactive),
242 insert a master menu in OUTER-FILE. This does not create or update
243 menus or pointers within the included files.
244
245 With optional UPDATE-EVERYTHING argument (numeric prefix arg, if
246 interactive), update all the menus and all the `Next', `Previous', and
247 `Up' pointers of all the files included in OUTER-FILE before inserting
248 a master menu in OUTER-FILE.
249
250 The command also updates the `Top' level node pointers of OUTER-FILE.
251
252 Notes:
253
254 * this command does NOT save any files--you must save the
255 outer file and any modified, included files.
256
257 * except for the `Top' node, this command does NOT handle any
258 pre-existing nodes in the outer file; hence, indices must be
259 enclosed in an included file.
260
261 Requirements:
262
263 * each of the included files must contain exactly one highest
264 hierarchical level node,
265 * this highest node must be the first node in the included file,
266 * each highest hierarchical level node must be of the same type.
267
268 Thus, normally, each included file contains one, and only one,
269 chapter."
270 t nil)
271
272 \f
273 ;;; Code:
274
275 ;;; Don't you dare insert any `require' calls at top level in this file--rms.
276
277 (defvar texinfo-section-list
278 '(("top" 1)
279 ("chapter" 2)
280 ("section" 3)
281 ("subsection" 4)
282 ("subsubsection" 5)
283 ("unnumbered" 2)
284 ("unnumberedsec" 3)
285 ("unnumberedsubsec" 4)
286 ("unnumberedsubsubsec" 5)
287 ("appendix" 2)
288 ("appendixsec" 3)
289 ("appendixsection" 3)
290 ("appendixsubsec" 4)
291 ("appendixsubsubsec" 5)
292 ("majorheading" 2)
293 ("chapheading" 2)
294 ("heading" 3)
295 ("subheading" 4)
296 ("subsubheading" 5))
297 "Alist of sectioning commands and their relative level.")
298
299 ;;; Syntax table
300
301 (defvar texinfo-mode-syntax-table
302 (let ((st (make-syntax-table)))
303 (modify-syntax-entry ?\" "." st)
304 (modify-syntax-entry ?\\ "." st)
305 (modify-syntax-entry ?@ "\\" st)
306 (modify-syntax-entry ?\^q "\\" st)
307 (modify-syntax-entry ?\[ "(]" st)
308 (modify-syntax-entry ?\] ")[" st)
309 (modify-syntax-entry ?{ "(}" st)
310 (modify-syntax-entry ?} "){" st)
311 (modify-syntax-entry ?\n ">" st)
312 (modify-syntax-entry ?\' "w" st)
313 st))
314
315 ;; Written by Wolfgang Bangerth <zcg51122@rpool1.rus.uni-stuttgart.de>
316 ;; To override this example, set either `imenu-generic-expression'
317 ;; or `imenu-create-index-function'.
318 (defvar texinfo-imenu-generic-expression
319 '((nil "^@\\(node\\|anchor\\)[ \t]+\\([^,\n]*\\)" 2)
320 ("Chapters" "^@chapter[ \t]+\\(.*\\)$" 1))
321 "Imenu generic expression for Texinfo mode. See `imenu-generic-expression'.")
322
323 (defconst texinfo-syntax-propertize-function
324 (syntax-propertize-rules
325 ("\\(@\\)c\\(omment\\)?\\>" (1 "<"))
326 ("^\\(@\\)ignore\\>" (1 "< b"))
327 ("^@end ignore\\(\n\\)" (1 "> b")))
328 "Syntactic keywords to catch comment delimiters in `texinfo-mode'.")
329
330 (defconst texinfo-environments
331 '("cartouche" "copying" "defcv" "deffn" "defivar" "defmac"
332 "defmethod" "defop" "defopt" "defspec" "deftp" "deftypecv"
333 "deftypefn" "deftypefun" "deftypeivar" "deftypemethod"
334 "deftypeop" "deftypevar" "deftypevr" "defun" "defvar"
335 "defvr" "description" "detailmenu" "direntry" "display"
336 "documentdescription" "enumerate" "example" "flushleft"
337 "flushright" "format" "ftable" "group" "html" "ifclear" "ifset"
338 "ifhtml" "ifinfo" "ifnothtml" "ifnotinfo" "ifnotplaintext"
339 "ifnottex" "ifplaintext" "iftex" "ignore" "itemize" "lisp"
340 "macro" "menu" "multitable" "quotation" "smalldisplay"
341 "smallexample" "smallformat" "smalllisp" "table" "tex"
342 "titlepage" "verbatim" "vtable")
343 "List of Texinfo environments.")
344
345 (defconst texinfo-environment-regexp
346 (concat "^@" (regexp-opt (cons "end" texinfo-environments) t) "\\>")
347 "Regexp for environment-like Texinfo list commands.
348 Subexpression 1 is what goes into the corresponding `@end' statement.")
349
350 (defface texinfo-heading
351 '((t (:inherit font-lock-function-name-face)))
352 "Face used for section headings in `texinfo-mode'."
353 :group 'texinfo)
354 (define-obsolete-face-alias 'texinfo-heading-face 'texinfo-heading "22.1")
355 (defvar texinfo-heading-face 'texinfo-heading)
356
357 (defvar texinfo-font-lock-keywords
358 `(;; All but the first had an OVERRIDE of t.
359 ;; It didn't seem to be any better, and it's slower--simon.
360 ;; Robert J. Chassell <bob@gnu.org> says remove this line.
361 ;;("\\$\\([^$]*\\)\\$" 1 font-lock-string-face t)
362 ("@\\([a-zA-Z]+\\|[^ \t\n]\\)" 1 font-lock-keyword-face) ;commands
363 ("^\\*\\([^\n:]*\\)" 1 font-lock-function-name-face t) ;menu items
364 ("@\\(emph\\|i\\|sc\\){\\([^}]+\\)" 2 'italic)
365 ("@\\(strong\\|b\\){\\([^}]+\\)" 2 'bold)
366 ("@\\(kbd\\|key\\|url\\|uref\\){\\([^}]+\\)" 2 font-lock-string-face)
367 ;; The following two groups have an OVERRIDE of `keep' because
368 ;; their arguments frequently include a @@, and we don't want that
369 ;; to overwrite the normal fontification of the argument.
370 ("@\\(file\\|email\\){\\([^}]+\\)" 2 font-lock-string-face keep)
371 ("@\\(samp\\|code\\|var\\|env\\|command\\|option\\){\\([^}]+\\)"
372 2 font-lock-variable-name-face keep)
373 ;; @math allows nested braces like @math{2^{12}}
374 ("@math{\\([^{}]*{?[^{}]*}?[^{}]*\\)}" 1 font-lock-variable-name-face)
375 ("@\\(cite\\|x?ref\\|pxref\\|dfn\\|inforef\\){\\([^}]+\\)"
376 2 font-lock-constant-face)
377 ("@\\(anchor\\){\\([^}]+\\)" 2 font-lock-type-face)
378 ("@\\(dmn\\|acronym\\|value\\){\\([^}]+\\)" 2 font-lock-builtin-face)
379 ("@\\(end\\|itemx?\\) +\\(.+\\)" 2 font-lock-keyword-face keep)
380 ;; (,texinfo-environment-regexp
381 ;; 1 (texinfo-clone-environment (match-beginning 1) (match-end 1)) keep)
382 (,(concat "^@" (regexp-opt (mapcar 'car texinfo-section-list) t)
383 ".*\n") 0 texinfo-heading-face t))
384 "Additional expressions to highlight in Texinfo mode.")
385
386 (defun texinfo-clone-environment (start end)
387 (let ((endp nil))
388 (save-excursion
389 (ignore-errors
390 (goto-char start)
391 (when (looking-at "end\\Sw+\\(\\sw+\\)")
392 (setq endp t start (match-beginning 1) end (match-end 1)))
393 (unless (get-char-property start 'text-clones)
394 (if endp
395 (texinfo-last-unended-begin)
396 (forward-word-strictly 1)
397 (texinfo-next-unmatched-end))
398 (skip-syntax-forward "^w")
399 (when (looking-at
400 (concat (regexp-quote (buffer-substring start end)) "\\>"))
401 (text-clone-create start end 'spread "\\w*")))))))
402
403 \f
404 ;;; Keybindings
405
406 ;;; Keys common both to Texinfo mode and to TeX shell.
407
408 (defun texinfo-define-common-keys (keymap)
409 "Define the keys both in Texinfo mode and in the texinfo-tex-shell."
410 (define-key keymap "\C-c\C-t\C-k" 'tex-kill-job)
411 (define-key keymap "\C-c\C-t\C-x" 'texinfo-quit-job)
412 (define-key keymap "\C-c\C-t\C-l" 'tex-recenter-output-buffer)
413 (define-key keymap "\C-c\C-t\C-d" 'texinfo-delete-from-print-queue)
414 (define-key keymap "\C-c\C-t\C-q" 'tex-show-print-queue)
415 (define-key keymap "\C-c\C-t\C-p" 'texinfo-tex-print)
416 (define-key keymap "\C-c\C-t\C-v" 'texinfo-tex-view)
417 (define-key keymap "\C-c\C-t\C-i" 'texinfo-texindex)
418
419 (define-key keymap "\C-c\C-t\C-r" 'texinfo-tex-region)
420 (define-key keymap "\C-c\C-t\C-b" 'texinfo-tex-buffer))
421
422 ;; Mode documentation displays commands in reverse order
423 ;; from how they are listed in the texinfo-mode-map.
424
425 (defvar texinfo-mode-map
426 (let ((map (make-sparse-keymap)))
427
428 ;; bindings for `texnfo-tex.el'
429 (texinfo-define-common-keys map)
430
431 (define-key map "\"" 'texinfo-insert-quote)
432
433 ;; bindings for `makeinfo.el'
434 (define-key map "\C-c\C-m\C-k" 'kill-compilation)
435 (define-key map "\C-c\C-m\C-l"
436 'makeinfo-recenter-compilation-buffer)
437 (define-key map "\C-c\C-m\C-r" 'makeinfo-region)
438 (define-key map "\C-c\C-m\C-b" 'makeinfo-buffer)
439
440 ;; bindings for `texinfmt.el'
441 (define-key map "\C-c\C-e\C-r" 'texinfo-format-region)
442 (define-key map "\C-c\C-e\C-b" 'texinfo-format-buffer)
443
444 ;; AUCTeX-like bindings
445 (define-key map "\e\r" 'texinfo-insert-@item)
446
447 ;; bindings for updating nodes and menus
448
449 (define-key map "\C-c\C-um" 'texinfo-master-menu)
450
451 (define-key map "\C-c\C-u\C-m" 'texinfo-make-menu)
452 (define-key map "\C-c\C-u\C-n" 'texinfo-update-node)
453 (define-key map "\C-c\C-u\C-e" 'texinfo-every-node-update)
454 (define-key map "\C-c\C-u\C-a" 'texinfo-all-menus-update)
455
456 (define-key map "\C-c\C-s" 'texinfo-show-structure)
457
458 (define-key map "\C-c}" 'up-list)
459 ;; FIXME: This is often used for "close block" aka texinfo-insert-@end.
460 (define-key map "\C-c]" 'up-list)
461 (define-key map "\C-c/" 'texinfo-insert-@end)
462 (define-key map "\C-c{" 'texinfo-insert-braces)
463
464 ;; bindings for inserting strings
465 (define-key map "\C-c\C-o" 'texinfo-insert-block)
466 (define-key map "\C-c\C-c\C-d" 'texinfo-start-menu-description)
467 (define-key map "\C-c\C-c\C-s" 'texinfo-insert-@strong)
468 (define-key map "\C-c\C-c\C-e" 'texinfo-insert-@emph)
469
470 (define-key map "\C-c\C-cv" 'texinfo-insert-@var)
471 (define-key map "\C-c\C-cu" 'texinfo-insert-@uref)
472 (define-key map "\C-c\C-ct" 'texinfo-insert-@table)
473 (define-key map "\C-c\C-cs" 'texinfo-insert-@samp)
474 (define-key map "\C-c\C-cq" 'texinfo-insert-@quotation)
475 (define-key map "\C-c\C-co" 'texinfo-insert-@noindent)
476 (define-key map "\C-c\C-cn" 'texinfo-insert-@node)
477 (define-key map "\C-c\C-cm" 'texinfo-insert-@email)
478 (define-key map "\C-c\C-ck" 'texinfo-insert-@kbd)
479 (define-key map "\C-c\C-ci" 'texinfo-insert-@item)
480 (define-key map "\C-c\C-cf" 'texinfo-insert-@file)
481 (define-key map "\C-c\C-cx" 'texinfo-insert-@example)
482 (define-key map "\C-c\C-ce" 'texinfo-insert-@end)
483 (define-key map "\C-c\C-cd" 'texinfo-insert-@dfn)
484 (define-key map "\C-c\C-cc" 'texinfo-insert-@code)
485 map))
486
487 (easy-menu-define texinfo-mode-menu
488 texinfo-mode-map
489 "Menu used for `texinfo-mode'."
490 '("Texinfo"
491 ["Insert block" texinfo-insert-block t]
492 ;; ["Insert node" texinfo-insert-@node t]
493 "----"
494 ["Update All" (lambda () (interactive) (texinfo-master-menu t))
495 :keys "\\[universal-argument] \\[texinfo-master-menu]"]
496 ["Update every node" texinfo-every-node-update t]
497 ["Update node" texinfo-update-node t]
498 ["Make Master menu" texinfo-master-menu t]
499 ["Make menu" texinfo-make-menu t]
500 ["Update all menus" texinfo-all-menus-update t]
501 "----"
502 ["Show structure" texinfo-show-structure t]
503 ["Format region" texinfo-format-region t]
504 ["Format buffer" texinfo-format-buffer t]
505 ["Makeinfo region" makeinfo-region t]
506 ["Makeinfo buffer" makeinfo-buffer t]))
507
508 \f
509 (defun texinfo-filter (section list)
510 (let (res)
511 (dolist (x list) (if (eq section (cadr x)) (push (car x) res)))
512 res))
513
514 (defvar texinfo-chapter-level-regexp
515 (regexp-opt (texinfo-filter 2 texinfo-section-list))
516 "Regular expression matching just the Texinfo chapter level headings.")
517
518 (defun texinfo-current-defun-name ()
519 "Return the name of the Texinfo node at point, or nil."
520 (save-excursion
521 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
522 (match-string-no-properties 1))))
523
524 ;;; Texinfo mode
525
526 ;;;###autoload
527 (define-derived-mode texinfo-mode text-mode "Texinfo"
528 "Major mode for editing Texinfo files.
529
530 It has these extra commands:
531 \\{texinfo-mode-map}
532
533 These are files that are used as input for TeX to make printed manuals
534 and also to be turned into Info files with \\[makeinfo-buffer] or
535 the `makeinfo' program. These files must be written in a very restricted and
536 modified version of TeX input format.
537
538 Editing commands are like text-mode except that the syntax table is
539 set up so expression commands skip Texinfo bracket groups. To see
540 what the Info version of a region of the Texinfo file will look like,
541 use \\[makeinfo-region], which runs `makeinfo' on the current region.
542
543 You can show the structure of a Texinfo file with \\[texinfo-show-structure].
544 This command shows the structure of a Texinfo file by listing the
545 lines with the @-sign commands for @chapter, @section, and the like.
546 These lines are displayed in another window called the *Occur* window.
547 In that window, you can position the cursor over one of the lines and
548 use \\[occur-mode-goto-occurrence], to jump to the corresponding spot
549 in the Texinfo file.
550
551 In addition, Texinfo mode provides commands that insert various
552 frequently used @-sign commands into the buffer. You can use these
553 commands to save keystrokes. And you can insert balanced braces with
554 \\[texinfo-insert-braces] and later use the command \\[up-list] to
555 move forward past the closing brace.
556
557 Also, Texinfo mode provides functions for automatically creating or
558 updating menus and node pointers. These functions
559
560 * insert the `Next', `Previous' and `Up' pointers of a node,
561 * insert or update the menu for a section, and
562 * create a master menu for a Texinfo source file.
563
564 Here are the functions:
565
566 texinfo-update-node \\[texinfo-update-node]
567 texinfo-every-node-update \\[texinfo-every-node-update]
568 texinfo-sequential-node-update
569
570 texinfo-make-menu \\[texinfo-make-menu]
571 texinfo-all-menus-update \\[texinfo-all-menus-update]
572 texinfo-master-menu
573
574 texinfo-indent-menu-description (column &optional region-p)
575
576 The `texinfo-column-for-description' variable specifies the column to
577 which menu descriptions are indented.
578
579 Passed an argument (a prefix argument, if interactive), the
580 `texinfo-update-node' and `texinfo-make-menu' functions do their jobs
581 in the region.
582
583 To use the updating commands, you must structure your Texinfo file
584 hierarchically, such that each `@node' line, with the exception of the
585 Top node, is accompanied by some kind of section line, such as an
586 `@chapter' or `@section' line.
587
588 If the file has a `top' node, it must be called `top' or `Top' and
589 be the first node in the file.
590
591 Entering Texinfo mode calls the value of `text-mode-hook', and then the
592 value of `texinfo-mode-hook'."
593 (setq-local page-delimiter
594 (concat "^@node [ \t]*[Tt]op\\|^@\\("
595 texinfo-chapter-level-regexp
596 "\\)\\>"))
597 (setq-local require-final-newline mode-require-final-newline)
598 (setq-local indent-tabs-mode nil)
599 (setq-local paragraph-separate
600 (concat "\b\\|@[a-zA-Z]*[ \n]\\|"
601 paragraph-separate))
602 (setq-local paragraph-start (concat "\b\\|@[a-zA-Z]*[ \n]\\|"
603 paragraph-start))
604 (setq-local sentence-end-base "\\(@\\(end\\)?dots{}\\|[.?!]\\)[]\"'”)}]*")
605 (setq-local fill-column 70)
606 (setq-local comment-start "@c ")
607 (setq-local comment-start-skip "@c +\\|@comment +")
608 (setq-local words-include-escapes t)
609 (setq-local imenu-generic-expression texinfo-imenu-generic-expression)
610 (setq imenu-case-fold-search nil)
611 (setq font-lock-defaults
612 '(texinfo-font-lock-keywords nil nil nil backward-paragraph))
613 (setq-local syntax-propertize-function texinfo-syntax-propertize-function)
614 (setq-local parse-sexp-lookup-properties t)
615 (setq-local add-log-current-defun-function #'texinfo-current-defun-name)
616
617 ;; Outline settings.
618 (setq-local outline-heading-alist
619 ;; We should merge `outline-heading-alist' and
620 ;; `texinfo-section-list'. But in the mean time, let's
621 ;; just generate one from the other.
622 (mapcar (lambda (x) (cons (concat "@" (car x)) (cadr x)))
623 texinfo-section-list))
624 (setq-local outline-regexp
625 (concat (regexp-opt (mapcar 'car outline-heading-alist) t)
626 "\\>"))
627
628 (setq-local tex-start-of-header "%\\*\\*start")
629 (setq-local tex-end-of-header "%\\*\\*end")
630 (setq-local tex-first-line-header-regexp "^\\\\input")
631 (setq-local tex-trailer "@bye\n")
632
633 ;; Prevent skeleton.el from adding a newline to each inserted
634 ;; skeleton. Those which do want a newline do that explicitly in
635 ;; their define-skeleton form.
636 (setq-local skeleton-end-newline nil)
637
638 ;; Prevent filling certain lines, in addition to ones specified by
639 ;; the user.
640 (setq-local auto-fill-inhibit-regexp
641 (let ((prevent-filling "^@\\(def\\|multitable\\)"))
642 (if (null auto-fill-inhibit-regexp)
643 prevent-filling
644 (concat auto-fill-inhibit-regexp "\\|" prevent-filling)))))
645
646
647 \f
648 ;;; Insert string commands
649
650 (defvar texinfo-block-default "example")
651
652 (define-skeleton texinfo-insert-block
653 "Create a matching pair @<cmd> .. @end <cmd> at point.
654 Puts point on a blank line between them."
655 (setq texinfo-block-default
656 (completing-read (format "Block name [%s]: " texinfo-block-default)
657 texinfo-environments
658 nil nil nil nil texinfo-block-default))
659 \n "@" str
660 ;; Blocks that take parameters: all the def* blocks take parameters,
661 ;; plus a few others.
662 (if (or (string-match "\\`def" str)
663 (member str '("table" "ftable" "vtable")))
664 '(nil " " -))
665 \n _ \n "@end " str \n \n)
666
667 (defun texinfo-inside-macro-p (macro &optional bound)
668 "Non-nil if inside a macro matching the regexp MACRO."
669 (condition-case nil
670 (save-excursion
671 (save-restriction
672 (narrow-to-region bound (point))
673 (while (progn
674 (up-list -1)
675 (not (condition-case nil
676 (save-excursion
677 (backward-sexp 1)
678 (looking-at macro))
679 (scan-error nil)))))
680 t))
681 (scan-error nil)))
682
683 (defun texinfo-inside-env-p (env &optional bound)
684 "Non-nil if inside an environment matching the regexp @ENV."
685 (save-excursion
686 (and (re-search-backward (concat "@\\(end\\s +\\)?" env) bound t)
687 (not (match-end 1)))))
688
689 (defvar texinfo-enable-quote-macros "@\\(code\\|samp\\|kbd\\)\\>")
690 (defvar texinfo-enable-quote-envs
691 '("example\\>" "smallexample\\>" "lisp\\>"))
692 (defun texinfo-insert-quote (&optional arg)
693 "Insert the appropriate quote mark for Texinfo.
694 Usually inserts the value of `texinfo-open-quote' (normally \\=`\\=`) or
695 `texinfo-close-quote' (normally \\='\\='), depending on the context.
696 With prefix argument or inside @code or @example, inserts a plain \"."
697 (interactive "*P")
698 (let ((top (or (save-excursion (re-search-backward "@node\\>" nil t))
699 (point-min))))
700 (if (or arg
701 (= (preceding-char) ?\\)
702 (save-excursion
703 ;; Might be near the start of a (narrowed) buffer.
704 (ignore-errors (backward-char (length texinfo-open-quote)))
705 (when (or (looking-at texinfo-open-quote)
706 (looking-at texinfo-close-quote))
707 (delete-char (length texinfo-open-quote))
708 t))
709 (texinfo-inside-macro-p texinfo-enable-quote-macros top)
710 (let ((in-env nil))
711 (dolist (env texinfo-enable-quote-envs in-env)
712 (if (texinfo-inside-env-p env top)
713 (setq in-env t)))))
714 (self-insert-command (prefix-numeric-value arg))
715 (insert
716 (if (or (bobp)
717 (memq (char-syntax (preceding-char)) '(?\( ?> ?\s)))
718 texinfo-open-quote
719 texinfo-close-quote)))))
720
721 ;; The following texinfo-insert-@end command not only inserts a SPC
722 ;; after the @end, but tries to find out what belongs there. It is
723 ;; not very smart: it does not understand nested lists.
724
725 (defun texinfo-last-unended-begin ()
726 (while (and (re-search-backward texinfo-environment-regexp)
727 (looking-at "@end"))
728 (texinfo-last-unended-begin)))
729
730 (defun texinfo-next-unmatched-end ()
731 (while (and (re-search-forward texinfo-environment-regexp)
732 (save-excursion
733 (goto-char (match-beginning 0))
734 (not (looking-at "@end"))))
735 (texinfo-next-unmatched-end)))
736
737 (define-skeleton texinfo-insert-@end
738 "Insert the matching `@end' for the last Texinfo command that needs one."
739 (ignore-errors
740 (save-excursion
741 (backward-word-strictly 1)
742 (texinfo-last-unended-begin)
743 (or (match-string 1) '-)))
744 \n "@end " str \n \n)
745
746 (define-skeleton texinfo-insert-braces
747 "Make a pair of braces and be poised to type inside of them.
748 Use \\[up-list] to move forward out of the braces."
749 nil
750 "{" _ "}")
751
752 (define-skeleton texinfo-insert-@code
753 "Insert a `@code{...}' command in a Texinfo buffer.
754 A numeric argument says how many words the braces should surround.
755 The default is not to surround any existing words with the braces."
756 nil
757 "@code{" _ "}")
758
759 (define-skeleton texinfo-insert-@dfn
760 "Insert a `@dfn{...}' command in a Texinfo buffer.
761 A numeric argument says how many words the braces should surround.
762 The default is not to surround any existing words with the braces."
763 nil
764 "@dfn{" _ "}")
765
766 (define-skeleton texinfo-insert-@email
767 "Insert a `@email{...}' command in a Texinfo buffer.
768 A numeric argument says how many words the braces should surround.
769 The default is not to surround any existing words with the braces."
770 nil
771 "@email{" _ "}")
772
773 (define-skeleton texinfo-insert-@emph
774 "Insert a `@emph{...}' command in a Texinfo buffer.
775 A numeric argument says how many words the braces should surround.
776 The default is not to surround any existing words with the braces."
777 nil
778 "@emph{" _ "}")
779
780 (define-skeleton texinfo-insert-@example
781 "Insert the string `@example' in a Texinfo buffer."
782 nil
783 \n "@example" \n \n)
784
785 (define-skeleton texinfo-insert-@file
786 "Insert a `@file{...}' command in a Texinfo buffer.
787 A numeric argument says how many words the braces should surround.
788 The default is not to surround any existing words with the braces."
789 nil
790 "@file{" _ "}")
791
792 (define-skeleton texinfo-insert-@item
793 "Insert the string `@item' in a Texinfo buffer.
794 If in a table defined by @table, follow said string with a space.
795 Otherwise, follow with a newline."
796 nil
797 \n "@item"
798 (if (equal (ignore-errors
799 (save-excursion
800 (texinfo-last-unended-begin)
801 (match-string 1)))
802 "table")
803 " " '\n)
804 _ \n)
805
806 (define-skeleton texinfo-insert-@kbd
807 "Insert a `@kbd{...}' command in a Texinfo buffer.
808 A numeric argument says how many words the braces should surround.
809 The default is not to surround any existing words with the braces."
810 nil
811 "@kbd{" _ "}")
812
813 (define-skeleton texinfo-insert-@node
814 "Insert the string `@node' in a Texinfo buffer.
815 Insert a comment on the following line indicating the order of
816 arguments to @node. Insert a carriage return after the comment line.
817 Leave point after `@node'."
818 nil
819 \n "@node " _ \n)
820
821 (define-skeleton texinfo-insert-@noindent
822 "Insert the string `@noindent' in a Texinfo buffer."
823 nil
824 \n "@noindent" \n)
825
826 (define-skeleton texinfo-insert-@quotation
827 "Insert the string `@quotation' in a Texinfo buffer."
828 \n "@quotation" \n _ \n)
829
830 (define-skeleton texinfo-insert-@samp
831 "Insert a `@samp{...}' command in a Texinfo buffer.
832 A numeric argument says how many words the braces should surround.
833 The default is not to surround any existing words with the braces."
834 nil
835 "@samp{" _ "}")
836
837 (define-skeleton texinfo-insert-@strong
838 "Insert a `@strong{...}' command in a Texinfo buffer.
839 A numeric argument says how many words the braces should surround.
840 The default is not to surround any existing words with the braces."
841 nil
842 "@strong{" _ "}")
843
844 (define-skeleton texinfo-insert-@table
845 "Insert the string `@table' in a Texinfo buffer."
846 nil
847 \n "@table " _ \n)
848
849 (define-skeleton texinfo-insert-@var
850 "Insert a `@var{}' command in a Texinfo buffer.
851 A numeric argument says how many words the braces should surround.
852 The default is not to surround any existing words with the braces."
853 nil
854 "@var{" _ "}")
855
856 (define-skeleton texinfo-insert-@uref
857 "Insert a `@uref{}' command in a Texinfo buffer.
858 A numeric argument says how many words the braces should surround.
859 The default is not to surround any existing words with the braces."
860 nil
861 "@uref{" _ "}")
862 (defalias 'texinfo-insert-@url 'texinfo-insert-@uref)
863 \f
864 ;;; Texinfo file structure
865
866 (defun texinfo-show-structure (&optional nodes-too)
867 "Show the structure of a Texinfo file.
868 List the lines in the file that begin with the @-sign commands for
869 @chapter, @section, and the like.
870
871 With optional argument (prefix if interactive), list both the lines
872 with @-sign commands for @chapter, @section, and the like, and list
873 @node lines.
874
875 Lines with structuring commands beginning in them are displayed in
876 another buffer named `*Occur*'. In that buffer, you can move point to
877 one of those lines and then use
878 \\<occur-mode-map>\\[occur-mode-goto-occurrence],
879 to jump to the corresponding spot in the Texinfo source file."
880
881 (interactive "P")
882 ;; First, remember current location
883 (let (current-location)
884 (save-excursion
885 (end-of-line) ; so as to find section on current line
886 (if (re-search-backward
887 ;; do not require `texinfo-section-types-regexp' in texnfo-upd.el
888 "^@\\(chapter \\|sect\\|subs\\|subh\\|unnum\\|major\\|chapheading \\|heading \\|appendix\\)"
889 nil t)
890 (setq current-location
891 (progn
892 (beginning-of-line)
893 (buffer-substring (point) (progn (end-of-line) (point)))))
894 ;; else point is located before any section command.
895 (setq current-location "tex")))
896 ;; Second, create and format an *Occur* buffer
897 (save-excursion
898 (goto-char (point-min))
899 (occur (concat "^\\(?:" (if nodes-too "@node\\>\\|")
900 outline-regexp "\\)")))
901 (pop-to-buffer "*Occur*")
902 (goto-char (point-min))
903 (let ((inhibit-read-only t))
904 (flush-lines "-----")
905 ;; Now format the "*Occur*" buffer to show the structure.
906 ;; Thanks to ceder@signum.se (Per Cederqvist)
907 (goto-char (point-max))
908 (let (level)
909 (while (re-search-backward "^ *[0-9]*:@\\(\\sw+\\)" nil 0)
910 (goto-char (1- (match-beginning 1)))
911 (setq level
912 (or (cadr (assoc (match-string 1) texinfo-section-list)) 2))
913 (indent-to-column (+ (current-column) (* 4 (- level 2))))
914 (beginning-of-line))))
915 ;; Third, go to line corresponding to location in source file
916 ;; potential bug: two exactly similar `current-location' lines ...
917 (goto-char (point-min))
918 (re-search-forward current-location nil t)
919 (beginning-of-line)
920 ))
921
922 \f
923 ;;; The tex and print function definitions:
924
925 (defcustom texinfo-texi2dvi-command "texi2dvi"
926 "Command used by `texinfo-tex-buffer' to run TeX and texindex on a buffer."
927 :type 'string
928 :group 'texinfo)
929
930 (defcustom texinfo-tex-command "tex"
931 "Command used by `texinfo-tex-region' to run TeX on a region."
932 :type 'string
933 :group 'texinfo)
934
935 (defcustom texinfo-texindex-command "texindex"
936 "Command used by `texinfo-texindex' to sort unsorted index files."
937 :type 'string
938 :group 'texinfo)
939
940 (defcustom texinfo-delete-from-print-queue-command "lprm"
941 "Command string used to delete a job from the line printer queue.
942 Command is used by \\[texinfo-delete-from-print-queue] based on
943 number provided by a previous \\[tex-show-print-queue]
944 command."
945 :type 'string
946 :group 'texinfo)
947
948 (defvar texinfo-tex-trailer "@bye"
949 "String appended after a region sent to TeX by `texinfo-tex-region'.")
950
951 (defun texinfo-tex-region (beg end)
952 "Run TeX on the current region.
953 This works by writing a temporary file (`tex-zap-file') in the directory
954 that is the value of `tex-directory', then running TeX on that file.
955
956 The first line of the buffer is copied to the
957 temporary file; and if the buffer has a header, it is written to the
958 temporary file before the region itself. The buffer's header is all lines
959 between the strings defined by `tex-start-of-header' and `tex-end-of-header'
960 inclusive. The header must start in the first 100 lines.
961
962 The value of `texinfo-tex-trailer' is appended to the temporary file after the region."
963 (interactive "r")
964 (require 'tex-mode)
965 (let ((tex-command texinfo-tex-command)
966 (tex-trailer texinfo-tex-trailer))
967 (tex-region beg end)))
968
969 (defun texinfo-tex-buffer ()
970 "Run TeX on visited file, once or twice, to make a correct `.dvi' file."
971 (interactive)
972 (require 'tex-mode)
973 (let ((tex-command texinfo-texi2dvi-command)
974 ;; Disable tex-start-options-string. texi2dvi would not
975 ;; understand anything specified here.
976 (tex-start-options-string ""))
977 (tex-buffer)))
978
979 (defun texinfo-texindex ()
980 "Run `texindex' on unsorted index files.
981 The index files are made by \\[texinfo-tex-region] or \\[texinfo-tex-buffer].
982 This runs the shell command defined by `texinfo-texindex-command'."
983 (interactive)
984 (require 'tex-mode)
985 (tex-send-command texinfo-texindex-command (concat tex-zap-file ".??"))
986 ;; alternatively
987 ;; (send-string "tex-shell"
988 ;; (concat texinfo-texindex-command
989 ;; " " tex-zap-file ".??" "\n"))
990 (tex-recenter-output-buffer nil))
991
992 (defun texinfo-tex-print ()
993 "Print `.dvi' file made by \\[texinfo-tex-region] or \\[texinfo-tex-buffer].
994 This runs the shell command defined by `tex-dvi-print-command'."
995 (interactive)
996 (require 'tex-mode)
997 (tex-print))
998
999 (defun texinfo-tex-view ()
1000 "View `.dvi' file made by \\[texinfo-tex-region] or \\[texinfo-tex-buffer].
1001 This runs the shell command defined by `tex-dvi-view-command'."
1002 (interactive)
1003 (require 'tex-mode)
1004 (tex-view))
1005
1006 (defun texinfo-quit-job ()
1007 "Quit currently running TeX job, by sending an `x' to it."
1008 (interactive)
1009 (if (not (get-process "tex-shell"))
1010 (error "No TeX shell running"))
1011 (tex-send-command "x"))
1012 ;; alternatively:
1013 ;; save-excursion
1014 ;; (set-buffer (get-buffer "*tex-shell*"))
1015 ;; (goto-char (point-max))
1016 ;; (insert "x")
1017 ;; (comint-send-input)
1018
1019 (defun texinfo-delete-from-print-queue (job-number)
1020 "Delete job from the line printer spooling queue.
1021 You are prompted for the job number (use a number shown by a previous
1022 \\[tex-show-print-queue] command)."
1023 (interactive "nPrinter job number for deletion: ")
1024 (require 'tex-mode)
1025 (if (tex-shell-running)
1026 (tex-kill-job)
1027 (tex-start-shell))
1028 (tex-send-command texinfo-delete-from-print-queue-command job-number)
1029 ;; alternatively
1030 ;; (send-string "tex-shell"
1031 ;; (concat
1032 ;; texinfo-delete-from-print-queue-command
1033 ;; " "
1034 ;; job-number"\n"))
1035 (tex-recenter-output-buffer nil))
1036
1037 (provide 'texinfo)
1038
1039 ;;; texinfo.el ends here