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