1 ;;; texnfo-upd.el --- utilities for updating nodes and menus in Texinfo files
3 ;; Copyright (C) 1989-1992, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: Robert J. Chassell
6 ;; Maintainer: bug-texinfo@gnu.org
7 ;; Keywords: maint, tex, docs
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Known bug: update commands fail to ignore @ignore, and fail to DTRT
27 ;; with the @if... directives (so expect trouble when the manual uses
28 ;; different @node lines or @menu items in @iftex and in @ifnottex).
30 ;; Summary: how to use the updating commands
32 ;; The node and menu updating functions automatically
34 ;; * insert missing `@node' lines,
35 ;; * insert the `Next', `Previous' and `Up' pointers of a node,
36 ;; * insert or update the menu for a section,
37 ;; * create a master menu for a Texinfo source file.
39 ;; With a prefix argument, the `texinfo-update-node' and
40 ;; `texinfo-make-menu' functions do their jobs in the region.
42 ;; Important note: We do NOT recommend use of these commands to update
43 ;; the `Next', `Previous' and `Up' pointers on @node lines. Most
44 ;; manuals, including those whose Texinfo files adhere to the structure
45 ;; described below, don't need these pointers, because makeinfo will
46 ;; generate them automatically (see the node "makeinfo Pointer
47 ;; Creation" in the Texinfo manual). By contrast, due to known bugs
48 ;; described above, texinfo-update-node etc. could produce incorrect
49 ;; pointers, and thus make a perfectly valid Texinfo file into an
50 ;; invalid one. You _have_ been warned!
52 ;; In brief, the functions for creating or updating nodes and menus, are:
54 ;; texinfo-update-node (&optional beginning end)
55 ;; texinfo-every-node-update ()
56 ;; texinfo-sequential-node-update (&optional region-p)
58 ;; texinfo-make-menu (&optional beginning end)
59 ;; texinfo-all-menus-update ()
60 ;; texinfo-master-menu ()
62 ;; texinfo-insert-node-lines (&optional title-p)
64 ;; texinfo-indent-menu-description (column &optional region-p)
66 ;; The `texinfo-column-for-description' variable specifies the column to
67 ;; which menu descriptions are indented.
69 ;; Texinfo file structure
70 ;; ----------------------
72 ;; To use the updating commands, you must structure your Texinfo file
73 ;; hierarchically. Each `@node' line, with the exception of the top
74 ;; node, must be accompanied by some kind of section line, such as an
75 ;; `@chapter' or `@section' line. Each node-line/section-line
76 ;; combination must look like this:
78 ;; @node Lists and Tables, Cross References, Structuring, Top
79 ;; @comment node-name, next, previous, up
80 ;; @chapter Making Lists and Tables
82 ;; or like this (without the `@comment' line):
84 ;; @node Lists and Tables, Cross References, Structuring, Top
85 ;; @chapter Making Lists and Tables
87 ;; If the file has a `top' node, it must be called `top' or `Top' and
88 ;; be the first node in the file.
91 ;;; The update node functions described in detail
93 ;; The `texinfo-update-node' command with no prefix argument inserts
94 ;; the correct next, previous and up pointers for the node in which
95 ;; point is located (i.e., for the node preceding point).
97 ;; With prefix argument, the `texinfo-update-node' function inserts the
98 ;; correct next, previous and up pointers for the nodes inside the
101 ;; It does not matter whether the `@node' line has pre-existing
102 ;; `Next', `Previous', or `Up' pointers in it. They are removed.
104 ;; Warning: Since the pre-existing pointers are replaced with the ones
105 ;; computed by `texinfo-update-node', and since this function has
106 ;; known bugs with the more advanced Texinfo features (see above), it
107 ;; could produce an invalid Texinfo file. You are well advised not to
108 ;; use this function, except if you know what you are doing and
109 ;; exercise extreme caution. Keep in mind that most manuals do not
110 ;; need the `Next', `Previous', and `Up' pointers to be present on the
111 ;; @node lines; makeinfo will automatically generate them when it
112 ;; produces the Info or HTML versions of the manual.
114 ;; The `texinfo-every-node-update' function runs `texinfo-update-node'
115 ;; on the whole buffer.
117 ;; The `texinfo-sequential-node-update' function inserts the
118 ;; immediately following and preceding node into the `Next' or
119 ;; `Previous' pointers regardless of their hierarchical level. This is
120 ;; only useful for certain kinds of text, like a novel, which you go
121 ;; through sequentially.
124 ;;; The menu making functions described in detail
126 ;; The `texinfo-make-menu' function without an argument creates or
127 ;; updates a menu for the section encompassing the node that follows
128 ;; point. With an argument, it makes or updates menus for the nodes
129 ;; within or part of the marked region.
131 ;; Whenever an existing menu is updated, the descriptions from
132 ;; that menu are incorporated into the new menu. This is done by copying
133 ;; descriptions from the existing menu to the entries in the new menu
134 ;; that have the same node names. If the node names are different, the
135 ;; descriptions are not copied to the new menu.
137 ;; Menu entries that refer to other Info files are removed since they
138 ;; are not a node within current buffer. This is a deficiency.
140 ;; The `texinfo-all-menus-update' function runs `texinfo-make-menu'
141 ;; on the whole buffer.
143 ;; The `texinfo-master-menu' function creates an extended menu located
144 ;; after the top node. (The file must have a top node.) This
145 ;; function works only on Texinfo files all of whose menus are
146 ;; present in a single file; use `texinfo-multiple-files-update' for
147 ;; multi-file manuals. The function constructs a master menu that
148 ;; includes every entry from every other menu. Use this command to
149 ;; create or update the @detailmenu menu after you've created or
150 ;; updated all the menus in the file, including the menu in the Top
151 ;; node, using the `texinfo-make-menu' or the `texinfo-all-menus-update'
154 ;; The `texinfo-indent-menu-description' function indents every
155 ;; description in the menu following point, to the specified column.
156 ;; Non-nil argument (prefix, if interactive) means indent every
157 ;; description in every menu in the region. This function does not
158 ;; indent second and subsequent lines of a multi-line description.
160 ;; The `texinfo-insert-node-lines' function inserts `@node' before the
161 ;; `@chapter', `@section', and such like lines of a region in a Texinfo
162 ;; file where the `@node' lines are missing.
164 ;; With a non-nil argument (prefix, if interactive), the function not
165 ;; only inserts `@node' lines but also inserts the chapter or section
166 ;; titles as the names of the corresponding nodes; and inserts titles
167 ;; as node names in pre-existing `@node' lines that lack names.
169 ;; Since node names should be more concise than section or chapter
170 ;; titles, you will usually want to manually edit node names so inserted.
178 (defvar texinfo-master-menu-header
179 " --- The Detailed Node Listing ---\n"
180 "String inserted before lower level entries in Texinfo master menu.
181 It comes after the chapter-level menu entries.")
183 ;; We used to look for just sub, but that found @subtitle.
184 (defvar texinfo-section-types-regexp
185 "^@\\(chapter \\|sect\\|subs\\|subh\\|unnum\\|major\\|chapheading \\|heading \\|appendix\\)"
186 "Regexp matching chapter, section, other headings (but not the top node).")
188 (defvar texinfo-section-level-regexp
189 (regexp-opt (texinfo-filter 3 texinfo-section-list
))
190 "Regular expression matching just the Texinfo section level headings.")
192 (defvar texinfo-subsection-level-regexp
193 (regexp-opt (texinfo-filter 4 texinfo-section-list
))
194 "Regular expression matching just the Texinfo subsection level headings.")
196 (defvar texinfo-subsubsection-level-regexp
197 (regexp-opt (texinfo-filter 5 texinfo-section-list
))
198 "Regular expression matching just the Texinfo subsubsection level headings.")
200 (defvar texinfo-update-menu-same-level-regexps
202 (2 .
(concat "\\(^@\\)\\(" texinfo-chapter-level-regexp
"\\)\\>[ \t]*"))
203 (3 .
(concat "\\(^@\\)\\(" texinfo-section-level-regexp
"\\)\\>[ \t]*"))
204 (4 .
(concat "\\(^@\\)\\(" texinfo-subsection-level-regexp
"\\)\\>[ \t]+"))
205 (5 .
(concat "\\(^@\\)\\(" texinfo-subsubsection-level-regexp
"\\)\\>[ \t]+")))
206 "Regexps for searching for same level sections in a Texinfo file.
207 The keys are strings specifying the general hierarchical level in the
208 document; the values are regular expressions.")
210 (defvar texinfo-update-menu-higher-regexps
211 '((1 .
"^@node [ \t]*DIR")
212 (2 .
"^@node [ \t]*top[ \t]*\\(,\\|$\\)")
216 texinfo-chapter-level-regexp
221 texinfo-section-level-regexp
223 texinfo-chapter-level-regexp
228 texinfo-subsection-level-regexp
230 texinfo-section-level-regexp
232 texinfo-chapter-level-regexp
234 "Regexps for searching for higher level sections in a Texinfo file.
235 The keys are strings specifying the general hierarchical level in the
236 document; the values are regular expressions.")
238 (defvar texinfo-update-menu-lower-regexps
242 texinfo-chapter-level-regexp
244 texinfo-section-level-regexp
246 texinfo-subsection-level-regexp
248 texinfo-subsubsection-level-regexp
253 texinfo-section-level-regexp
255 texinfo-subsection-level-regexp
257 texinfo-subsubsection-level-regexp
262 texinfo-subsection-level-regexp
264 texinfo-subsubsection-level-regexp
269 texinfo-subsubsection-level-regexp
271 ;; There's nothing below 5, use a bogus regexp that can't match.
273 "Regexps for searching for lower level sections in a Texinfo file.
274 The keys are strings specifying the general hierarchical level in the
275 document; the values are regular expressions.")
278 (defun texinfo-make-menu (&optional beginning end
)
279 "Without any prefix argument, make or update a menu.
280 Make the menu for the section enclosing the node found following point.
282 A prefix argument means make or update menus
283 for nodes within or part of the marked region.
285 Whenever a menu exists, and is being updated, the descriptions that
286 are associated with node names in the pre-existing menu are
287 incorporated into the new menu.
289 Leaves trailing whitespace in a menu that lacks descriptions, so
290 descriptions will format well. In general, a menu should contain
291 descriptions, because node names and section titles are often too
292 short to explain a node well."
296 (list (point) (mark))))
298 (let ((level (texinfo-hierarchic-level)))
299 (texinfo-make-one-menu level
)
300 (message "Menu updated"))
302 (message "Making or updating menus in %s... " (buffer-name))
304 (goto-char (min beginning end
))
305 ;; find section type following point
306 (let ((level (texinfo-hierarchic-level))
307 (region-end-marker (make-marker)))
308 (set-marker region-end-marker
(max beginning end
))
312 (while (texinfo-find-lower-level-node
313 level
(marker-position region-end-marker
))
314 (setq level
(texinfo-hierarchic-level)) ; new, lower level
315 (texinfo-make-one-menu level
))
317 (while (and (< (point) (marker-position region-end-marker
))
318 (texinfo-find-higher-level-node
319 level
(marker-position region-end-marker
)))
320 (setq level
(texinfo-hierarchic-level))
321 ;; Don't allow texinfo-find-higher-level-node
322 ;; to find the same node again.
324 (while (texinfo-find-lower-level-node
325 level
(marker-position region-end-marker
))
326 (setq level
(texinfo-hierarchic-level)) ; new, lower level
327 (texinfo-make-one-menu level
))))))
328 (message "Making or updating menus in %s...done" (buffer-name))))
330 (defun texinfo-make-one-menu (level)
331 "Make a menu of all the appropriate nodes in this section.
332 `Appropriate nodes' are those associated with sections that are
333 at the level specified by LEVEL. Point is left at the end of menu."
335 ((case-fold-search t
)
338 (goto-char (texinfo-update-menu-region-beginning level
))
341 (end (texinfo-update-menu-region-end level
))
342 (first (texinfo-menu-first-node beginning end
))
344 (goto-char beginning
)
346 (texinfo-copy-node-name)))
347 (new-menu-list (texinfo-make-menu-list beginning end level
)))
348 (when (texinfo-old-menu-p beginning first
)
349 (texinfo-incorporate-descriptions new-menu-list
)
350 (texinfo-incorporate-menu-entry-names new-menu-list
)
351 (texinfo-delete-old-menu beginning first
))
352 (texinfo-insert-menu new-menu-list node-name
)))
354 (defun texinfo-all-menus-update (&optional update-all-nodes-p
)
355 "Update every regular menu in a Texinfo file.
356 Update pre-existing master menu, if there is one.
358 Only single-file manuals are supported by this function. For
359 multi-file manuals, use `texinfo-multiple-files-update'.
361 If called with a non-nil argument, this function first updates all the
362 nodes in the buffer before updating the menus. Do NOT invoke this
363 command with an argument if your Texinfo file uses @node lines without
364 the `Next', `Previous', and `Up' pointers!
366 Indents the first line of descriptions, and leaves trailing whitespace
367 in a menu that lacks descriptions, so descriptions will format well.
368 In general, a menu should contain descriptions, because node names and
369 section titles are often too short to explain a node well."
371 (let ((case-fold-search t
)
374 (push-mark (point-max) t
)
375 (goto-char (point-min))
376 (message "Checking for a master menu in %s ... "(buffer-name))
378 (when (search-forward texinfo-master-menu-header nil t
)
379 ;; Check if @detailmenu kludge is used;
380 ;; if so, leave point before @detailmenu.
381 (search-backward "\n@detailmenu" (line-beginning-position -
2) t
)
382 ;; Remove detailed master menu listing
383 (setq master-menu-p t
)
384 (goto-char (match-beginning 0))
385 (let ((end-of-detailed-menu-descriptions
386 (save-excursion ; beginning of end menu line
387 (goto-char (texinfo-menu-end))
388 (beginning-of-line) (forward-char -
1)
390 (delete-region (point) end-of-detailed-menu-descriptions
))))
392 (when update-all-nodes-p
393 (message "Updating all nodes in %s ... " (buffer-name))
394 (texinfo-update-node (point-min) (point-max)))
396 (message "Updating all menus in %s ... " (buffer-name))
397 (texinfo-make-menu (point-max) (point-min))
400 (message "Updating the master menu in %s... " (buffer-name))
401 (texinfo-master-menu nil
)))
403 (message "Done...updated all the menus. You may save the buffer.")))
405 (defun texinfo-find-lower-level-node (level region-end
)
406 "Search forward from point for node at any level lower than LEVEL.
407 Search is limited to the end of the marked region, REGION-END,
408 and to the end of the menu region for the level.
410 Return t if the node is found, else nil. Leave point at the beginning
411 of the node if one is found; else do not move point."
412 (let ((case-fold-search t
))
413 (if (and (< (point) region-end
)
416 "\\(^@node\\).*\n" ; match node line
417 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
419 "\\(^@ifinfo[ ]*\n\\)" ; ifinfo line, if any
421 "\\(^@ifnottex[ ]*\n\\)" ; ifnottex line, if any
422 "\\)?" ; end of expression
423 (eval (cdr (assoc level texinfo-update-menu-lower-regexps
))))
424 ;; the next higher level node marks the end of this
425 ;; section, and no lower level node will be found beyond
426 ;; this position even if region-end is farther off
427 (texinfo-update-menu-region-end level
)
429 (goto-char (match-beginning 1)))))
431 (defun texinfo-find-higher-level-node (level region-end
)
432 "Search forward from point for node at any higher level than argument LEVEL.
433 Search is limited to the end of the marked region, REGION-END.
435 Return t if the node is found, else nil. Leave point at the beginning
436 of the node if one is found; else do not move point.
438 A `@node' line starting at point does count as a match;
439 if the match is found there, the value is t and point does not move."
441 (let ((case-fold-search t
))
444 (if (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" region-end t
)
445 (progn (beginning-of-line) t
)))
447 (when (re-search-forward
449 "\\(^@node\\).*\n" ; match node line
450 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
452 "\\(^@ifinfo[ ]*\n\\)" ; ifinfo line, if any
454 "\\(^@ifnottex[ ]*\n\\)" ; ifnottex line, if any
455 "\\)?" ; end of expression
456 (eval (cdr (assoc level texinfo-update-menu-higher-regexps
))))
458 (beginning-of-line) t
)))))
461 ;;; Making the list of new menu entries
463 (defun texinfo-make-menu-list (beginning end level
)
464 "Make a list of node names and their descriptions.
465 Point is left at the end of the menu region, but the menu is not inserted.
467 First argument is position from which to start making menu list;
468 second argument is end of region in which to try to locate entries;
469 third argument is the level of the nodes that are the entries.
471 Node names and descriptions are dotted pairs of strings. Each pair is
472 an element of the list. If the description does not exist, the
473 element consists only of the node name."
474 (goto-char beginning
)
476 (while (texinfo-menu-locate-entry-p level end
)
478 (texinfo-copy-node-name)
479 (prog1 "" (forward-line 1)))
480 ;; Use following to insert section titles automatically.
481 ;; (texinfo-copy-section-title))
483 (nreverse new-menu-list
)))
485 (defun texinfo-menu-locate-entry-p (level search-end
)
486 "Find a node that will be part of menu for this section.
487 First argument is a string such as \"section\" specifying the general
488 hierarchical level of the menu; second argument is a position
489 specifying the end of the search.
491 The function returns t if the node is found, else nil. It searches
492 forward from point, and leaves point at the beginning of the node.
494 The function finds entries of the same type. Thus `subsections' and
495 `unnumberedsubsecs' will appear in the same menu."
496 (let ((case-fold-search t
))
497 (if (re-search-forward
499 "\\(^@node\\).*\n" ; match node line
500 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
502 "\\(^@ifinfo[ ]*\n\\)" ; ifinfo line, if any
504 "\\(^@ifnottex[ ]*\n\\)" ; ifnottex line, if any
505 "\\)?" ; end of expression
507 (cdr (assoc level texinfo-update-menu-same-level-regexps
))))
510 (goto-char (match-beginning 1)))))
512 (defun texinfo-copy-node-name ()
513 "Return the node name as a string.
515 Start with point at the beginning of the node line; copy the text
516 after the node command up to the first comma on the line, if any, and
517 return the text as a string. Leaves point at the beginning of the
518 line. If there is no node name, returns an empty string."
522 (progn (forward-word 1) ; skip over node command
523 (skip-chars-forward " \t") ; and over spaces
525 (if (search-forward "," (line-end-position) t
) ; bound search
527 (end-of-line) (point)))))
529 (defun texinfo-copy-section-title ()
530 "Return the title of the section as a string.
531 The title is used as a description line in the menu when one does not
534 Move point to the beginning of the appropriate section line by going
535 to the start of the text matched by last regexp searched for, which
536 must have been done by `texinfo-menu-locate-entry-p'."
538 ;; could use the same re-search as in `texinfo-menu-locate-entry-p'
539 ;; instead of using `match-beginning'; such a variation would be
540 ;; more general, but would waste information already collected
542 (goto-char (match-beginning 7)) ; match section name
545 (progn (forward-word 1) ; skip over section type
546 (skip-chars-forward " \t") ; and over spaces
548 (progn (end-of-line) (point))))
551 ;;; Handling the old menu
553 (defun texinfo-old-menu-p (beginning first
)
554 "Move point to the beginning of the menu for this section, if any.
555 Otherwise move point to the end of the first node of this section.
556 Return t if a menu is found, nil otherwise.
558 First argument is the position of the beginning of the section in which
559 the menu will be located; second argument is the position of the first
560 node within the section.
562 If no menu is found, the function inserts two newlines just before the
563 end of the section, and leaves point there where a menu ought to be."
564 (goto-char beginning
)
565 (if (re-search-forward "^@menu" first
'goto-end
)
567 (insert "\n\n") (forward-line -
2) nil
))
569 (defun texinfo-incorporate-descriptions (new-menu-list)
570 "Copy the old menu line descriptions that exist to the new menu.
572 Point must be at beginning of old menu.
574 If the node-name of the new menu is found in the old menu, insert the
575 old description into the new entry.
577 For this function, the new menu is a list made up of lists of dotted
578 pairs in which the first element of the pair is the node name and the
579 second element the description. The new menu is changed destructively.
580 The old menu is the menu as it appears in the Texinfo file."
582 (let ((end-of-menu (texinfo-menu-end)))
583 (dolist (new-menu new-menu-list new-menu-list
)
584 (save-excursion ; keep point at beginning of menu
585 (when (re-search-forward
586 ;; Existing nodes can have the form
587 ;; * NODE NAME:: DESCRIPTION
589 ;; * MENU ITEM: NODE NAME. DESCRIPTION.
591 ;; Recognize both when looking for the description.
592 (concat "\\* \\(" ; so only menu entries are found
593 (regexp-quote (car new-menu
)) "::"
595 ".*: " (regexp-quote (car new-menu
)) "[.,\t\n]"
597 ) ; so only complete entries are found
600 (setcdr new-menu
(texinfo-menu-copy-old-description end-of-menu
)))))))
602 (defun texinfo-incorporate-menu-entry-names (new-menu-list)
603 "Copy any old menu entry names to the new menu.
605 Point must be at beginning of old menu.
607 If the node-name of the new menu entry cannot be found in the old
610 For this function, the new menu is a list made up of lists of dotted
611 pairs in which the first element of the pair is the node name and the
612 second element is the description (or nil).
614 If we find an existing menu entry name, we change the first element of
615 the pair to be another dotted pair in which the car is the menu entry
616 name and the cdr is the node name.
618 NEW-MENU-LIST is changed destructively. The old menu is the menu as it
619 appears in the texinfo file."
621 (let ((end-of-menu (texinfo-menu-end)))
622 (dolist (new-menu new-menu-list new-menu-list
)
623 (save-excursion ; keep point at beginning of menu
624 (if (re-search-forward
625 ;; Existing nodes can have the form
626 ;; * NODE NAME:: DESCRIPTION
628 ;; * MENU ITEM: NODE NAME. DESCRIPTION.
630 ;; We're interested in the second case.
631 (concat "\\* " ; so only menu entries are found
632 "\\(.*\\): " (regexp-quote (car new-menu
))
637 new-menu
; replace the node name
638 (cons (buffer-substring (match-beginning 1) (match-end 1))
639 (car new-menu
))))))))
641 (defun texinfo-menu-copy-old-description (end-of-menu)
642 "Return description field of old menu line as string.
643 Point must be located just after the node name. Point left before description.
644 Single argument, END-OF-MENU, is position limiting search."
645 (skip-chars-forward "[:.,\t\n ]+")
646 ;; don't copy a carriage return at line beginning with asterisk!
647 ;; don't copy @detailmenu or @end menu or @ignore as descriptions!
648 ;; do copy a description that begins with an `@'!
649 ;; !! Known bug: does not copy descriptions starting with ^|\{?* etc.
650 (if (and (looking-at "\\(\\w+\\|@\\)")
652 "\\(^\\* \\|^@detailmenu\\|^@end menu\\|^@ignore\\)")))
656 (re-search-forward "\\(^\\* \\|^@ignore\\|^@end menu\\)" end-of-menu t
)
657 (line-end-position 0))) ; end of last description line
660 (defun texinfo-menu-end ()
661 "Return position of end of menu, but don't move point.
662 Signal an error if not end of menu."
664 (if (re-search-forward "^@end menu" nil t
)
666 (error "Menu does not have an end"))))
668 (defun texinfo-delete-old-menu (beginning first
)
669 "Delete the old menu. Point must be in or after menu.
670 First argument is position of the beginning of the section in which
671 the menu will be located; second argument is the position of the first
672 node within the section."
673 ;; No third arg to search, so error if search fails.
674 (re-search-backward "^@menu" beginning
)
675 (delete-region (point)
677 (re-search-forward "^@end menu" first
)
681 ;;; Inserting new menu
683 ;; try 32, but perhaps 24 is better
684 (defvar texinfo-column-for-description
32
685 "Column at which descriptions start in a Texinfo menu.")
687 (defun texinfo-insert-menu (menu-list node-name
)
688 "Insert formatted menu at point.
689 Indents the first line of descriptions, if any, to the value of
690 texinfo-column-for-description. Indenting leaves trailing whitespace
691 in a menu that lacks descriptions, so descriptions will format well.
692 In general, a menu should contain descriptions, because node names and
693 section titles are often too short to explain a node well.
697 \(\(\"node-name1\" . \"description\"\)
698 \(\"node-name2\" . \"description\"\) ... \)
700 However, the description field might be nil.
702 Also, the node-name field might itself be a dotted pair (call it P) of
703 strings instead of just a string. In that case, the car of P
704 is the menu entry name, and the cdr of P is the node name."
707 (dolist (menu menu-list
)
708 ;; Every menu entry starts with a star and a space.
711 ;; Insert the node name (and menu entry name, if present).
712 (let ((node-part (car menu
)))
713 (if (stringp node-part
)
714 ;; "Double colon" entry line; menu entry and node name are the same,
715 (insert (format "%s::" node-part
))
716 ;; "Single colon" entry line; menu entry and node name are different.
717 (insert (format "%s: %s." (car node-part
) (cdr node-part
)))))
719 ;; Insert the description, if present.
720 (when (> (length (cdr menu
)) 0)
721 ;; Move to right place.
722 (indent-to texinfo-column-for-description
2)
723 ;; Insert description.
724 (insert (format "%s" (cdr menu
))))
726 (insert "\n")) ; end this menu entry
728 (let ((level (texinfo-hierarchic-level)))
730 "Updated level \"%s\" menu following node: %s ... " level node-name
)))
733 ;;; Starting menu descriptions by inserting titles
735 (defun texinfo-start-menu-description ()
736 "In this menu entry, insert the node's section title as a description.
737 Position point at beginning of description ready for editing.
738 Do not insert a title if the line contains an existing description.
740 You will need to edit the inserted text since a useful description
741 complements the node name rather than repeats it as a title does."
744 (let (beginning end node-name title
)
747 (if (search-forward "* " (line-end-position) t
)
748 (progn (skip-chars-forward " \t")
749 (setq beginning
(point)))
750 (error "This is not a line in a menu"))
753 ;; "Double colon" entry line; menu entry and node name are the same,
754 ((search-forward "::" (line-end-position) t
)
755 (if (looking-at "[ \t]*[^ \t\n]+")
756 (error "Descriptive text already exists"))
757 (skip-chars-backward ": \t")
758 (setq node-name
(buffer-substring beginning
(point))))
760 ;; "Single colon" entry line; menu entry and node name are different.
761 ((search-forward ":" (line-end-position) t
)
762 (skip-chars-forward " \t")
763 (setq beginning
(point))
764 ;; Menu entry line ends in a period, comma, or tab.
765 (if (re-search-forward "[.,\t]" (line-beginning-position 2) t
)
767 (if (looking-at "[ \t]*[^ \t\n]+")
768 (error "Descriptive text already exists"))
769 (skip-chars-backward "., \t")
770 (setq node-name
(buffer-substring beginning
(point))))
771 ;; Menu entry line ends in a return.
772 (re-search-forward ".*\n" (line-beginning-position 2) t
)
773 (skip-chars-backward " \t\n")
774 (setq node-name
(buffer-substring beginning
(point)))
775 (if (= 0 (length node-name
))
776 (error "No node name on this line")
778 (t (error "No node name on this line")))
779 ;; Search for node that matches node name, and copy the section title.
780 (if (re-search-forward
783 (regexp-quote node-name
)
784 ".*\n" ; match node line
786 "\\(\\(^@c \\|^@comment\\).*\n\\)" ; match comment line, if any
788 "\\(^@ifinfo[ ]*\n\\)" ; ifinfo line, if any
790 "\\(^@ifnottex[ ]*\n\\)" ; ifnottex line, if any
791 "\\)?" ; end of expression
796 ;; skip over section type
797 (progn (forward-word 1)
799 (skip-chars-forward " \t")
802 (skip-chars-backward " \t")
804 (error "Cannot find node to match node name in menu entry")))
805 ;; Return point to the menu and insert the title.
809 (save-excursion (skip-chars-backward " \t") (point)))
810 (indent-to texinfo-column-for-description
2)
811 (save-excursion (insert title
))))
814 ;;; Handling description indentation
816 ;; Since the make-menu functions indent descriptions, these functions
817 ;; are useful primarily for indenting a single menu specially.
819 (defun texinfo-indent-menu-description (column &optional region-p
)
820 "Indent every description in menu following point to COLUMN.
821 Non-nil argument (prefix, if interactive) means indent every
822 description in every menu in the region. Does not indent second and
823 subsequent lines of a multi-line description."
826 "nIndent menu descriptions to (column number): \nP")
832 (re-search-forward "^@menu")
833 (texinfo-menu-indent-description column
)
835 "Indented descriptions in menu. You may save the buffer."))
837 (message "Indenting every menu description in region... ")
838 (goto-char (region-beginning))
839 (while (and (< (point) (region-end))
840 (texinfo-locate-menu-p))
842 (texinfo-menu-indent-description column
))
843 (message "Indenting done. You may save the buffer.")))))
845 (defun texinfo-menu-indent-description (to-column-number)
846 "Indent the Texinfo file menu description to TO-COLUMN-NUMBER.
847 Start with point just after the word `menu' in the `@menu' line and
848 leave point on the line before the `@end menu' line. Does not indent
849 second and subsequent lines of a multi-line description."
850 (let* ((beginning-of-next-line (point)))
851 (while (< beginning-of-next-line
852 (save-excursion ; beginning of end menu line
853 (goto-char (texinfo-menu-end))
857 (when (re-search-forward "\\* \\(.*::\\|.*: [^.,\t\n]+[.,\t]\\)"
860 (let ((beginning-white-space (point)))
861 (skip-chars-forward " \t") ; skip over spaces
862 (if (looking-at "\\(@\\|\\w\\)+") ; if there is text
864 ;; remove pre-existing indentation
865 (delete-region beginning-white-space
(point))
866 (indent-to-column to-column-number
)))))
867 ;; position point at beginning of next line
869 (setq beginning-of-next-line
(point)))))
872 ;;; Making the master menu
874 (defun texinfo-master-menu (update-all-nodes-menus-p)
875 "Make a master menu for a whole Texinfo file.
876 Remove pre-existing master menu, if there is one.
878 This function supports only single-file manuals. For multi-file
879 manuals, use `texinfo-multiple-files-update'.
881 This function creates or updates the @detailmenu section of a
882 master menu that follows the Top node. It replaces any existing
883 detailed menu that follows the top node. The detailed menu
884 includes every entry from all the other menus. By default, the
885 existing menus, including the menu in the Top node, are not
886 updated according to the buffer contents, so all the menus should
887 be updated first using `texinfo-make-menu' or
888 `texinfo-all-menus-update', which see. Alternatively, invoke
889 this function with a prefix argument, see below.
891 Non-nil, non-numeric argument (C-u prefix, if interactive) means
892 first update all existing menus in the buffer (incorporating
893 descriptions from pre-existing menus) before it constructs the
894 master menu. If the argument is numeric (e.g., \"C-u 2\"),
895 update all existing nodes as well, by calling
896 \`texinfo-update-node' on the entire file. Warning: do NOT
897 invoke with a numeric argument if your Texinfo file uses @node
898 lines without the `Next', `Previous', `Up' pointers, as the
899 result could be an invalid Texinfo file!
901 The function removes and recreates the detailed part of an already
902 existing master menu. This action assumes that the pre-existing
903 master menu uses the standard `texinfo-master-menu-header' for the
906 The master menu has the following format, which is adapted from the
907 recommendation in the Texinfo Manual:
909 * The first part contains the major nodes in the Texinfo file: the
910 nodes for the chapters, chapter-like sections, and the major
911 appendices. This includes the indices, so long as they are in
912 chapter-like sections, such as unnumbered sections.
914 * The second and subsequent parts contain a listing of the other,
915 lower level menus, in order. This way, an inquirer can go
916 directly to a particular node if he or she is searching for
917 specific information.
919 Each of the menus in the detailed node listing is introduced by the
920 title of the section containing the menu.
922 Indents the first line of descriptions, and leaves trailing whitespace
923 in a menu that lacks descriptions, so descriptions will format well.
924 In general, a menu should contain descriptions, because node names and
925 section titles are often too short to explain a node well."
928 (let ((case-fold-search t
))
930 (goto-char (point-min))
932 ;; Move point to location after `top'.
933 (if (not (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t
))
934 (error "This buffer needs a Top node"))
938 (or (re-search-forward "^@node" nil t
)
939 (error "Too few nodes for a master menu"))
941 (if (search-forward texinfo-master-menu-header first-chapter t
)
943 ;; Check if @detailmenu kludge is used;
944 ;; if so, leave point before @detailmenu.
945 (search-backward "\n@detailmenu" (line-beginning-position -
2) t
)
946 ;; Remove detailed master menu listing
947 (goto-char (match-beginning 0))
948 (let ((end-of-detailed-menu-descriptions
949 (save-excursion ; beginning of end menu line
950 (goto-char (texinfo-menu-end))
951 (beginning-of-line) (forward-char -
1)
953 (delete-region (point) end-of-detailed-menu-descriptions
)))))
955 (if update-all-nodes-menus-p
957 (when (numberp update-all-nodes-menus-p
)
959 "Making a master menu in %s ...first updating all nodes... "
961 (texinfo-update-node (point-min) (point-max)))
962 (message "Updating all menus in %s ... " (buffer-name))
963 (texinfo-make-menu (point-min) (point-max))))
965 (message "Now making the master menu in %s... " (buffer-name))
966 (goto-char (point-min))
967 (texinfo-insert-master-menu-list
968 (texinfo-master-menu-list))
970 ;; Remove extra newlines that texinfo-insert-master-menu-list
971 ;; may have inserted.
974 (goto-char (point-min))
976 (if (search-forward texinfo-master-menu-header nil t
)
978 (goto-char (match-beginning 0))
979 ;; Check if @detailmenu kludge is used;
980 ;; if so, leave point before @detailmenu.
981 (search-backward "\n@detailmenu" (line-beginning-position -
2) t
)
984 (goto-char (point-min))))
986 (re-search-forward "^@menu")
990 (re-search-forward "^@end menu")
992 (delete-blank-lines))
995 "Done...completed making master menu. You may save the buffer.")))
997 (defun texinfo-master-menu-list ()
998 "Return a list of menu entries and header lines for the master menu.
1000 Start with the menu for chapters and indices and then find each
1001 following menu and the title of the node preceding that menu.
1003 The master menu list has this form:
1005 \(\(\(... \"entry-1-2\" \"entry-1\"\) \"title-1\"\)
1006 \(\(... \"entry-2-2\" \"entry-2-1\"\) \"title-2\"\)
1009 However, there does not need to be a title field."
1011 (let (master-menu-list)
1012 (while (texinfo-locate-menu-p)
1013 (push (list (texinfo-copy-menu) (texinfo-copy-menu-title))
1015 (nreverse master-menu-list
)))
1017 (defun texinfo-insert-master-menu-list (master-menu-list)
1018 "Format and insert the master menu in the current buffer."
1019 (goto-char (point-min))
1020 ;; Insert a master menu only after `Top' node and before next node
1021 ;; \(or include file if there is no next node\).
1022 (unless (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t
)
1023 (error "This buffer needs a Top node"))
1024 (let ((first-chapter
1025 (save-excursion (re-search-forward "^@node\\|^@include") (point))))
1026 (unless (re-search-forward "^@menu" first-chapter t
)
1027 (error "Buffer lacks a menu in its first node; create it, then run me again")))
1029 (delete-region ; buffer must have ordinary top menu
1031 (save-excursion (re-search-forward "^@end menu") (point)))
1034 ;; `master-menu-inserted-p' is a kludge to tell
1035 ;; whether to insert @end detailmenu (see bleow)
1036 (let (master-menu-inserted-p)
1037 ;; Handle top of menu
1038 (insert "\n@menu\n")
1039 ;; Insert chapter menu entries. Tell user what is going on.
1040 (message "Inserting chapter menu entry: %s ... "
1041 (car (car master-menu-list
)))
1042 (dolist (entry (reverse (car (car master-menu-list
))))
1043 (insert "* " entry
"\n"))
1045 (setq master-menu-list
(cdr master-menu-list
))
1047 ;; Only insert detailed master menu if there is one....
1048 (if (car (car master-menu-list
))
1049 (progn (setq master-menu-inserted-p t
)
1050 (insert (concat "\n@detailmenu\n"
1051 texinfo-master-menu-header
))))
1053 ;; @detailmenu added 5 Sept 1996 to `texinfo-master-menu-header'
1054 ;; at Karl Berry's request to avert a bug in `makeinfo';
1055 ;; all agree this is a bad kludge and should eventually be removed.
1056 ;; @detailmenu ... @end detailmenu is a noop in `texinfmt.el'.
1057 ;; See @end detailmenu below;
1058 ;; also see `texinfo-all-menus-update' above, `texinfo-master-menu',
1059 ;; `texinfo-multiple-files-update'.
1061 ;; Now, insert all the other menus
1063 ;; The menu master-menu-list has a form like this:
1064 ;; ((("beta" "alpha") "title-A")
1065 ;; (("delta" "gamma") "title-B"))
1067 (dolist (menu master-menu-list
)
1069 (message "Inserting menu for %s .... " (cadr menu
))
1070 ;; insert title of menu section
1071 (insert "\n" (cadr menu
) "\n\n")
1073 ;; insert each menu entry
1074 (dolist (entry (reverse (car menu
)))
1075 (insert "* " entry
"\n")))
1079 ;; @detailmenu (see note above)
1080 ;; Only insert @end detailmenu if a master menu was inserted.
1081 (if master-menu-inserted-p
1082 (insert "\n@end detailmenu"))
1083 (insert "\n@end menu\n\n"))))
1085 (defun texinfo-locate-menu-p ()
1086 "Find the next menu in the texinfo file.
1087 If found, leave point after word `menu' on the `@menu' line, and return t.
1088 If a menu is not found, do not move point and return nil."
1089 (re-search-forward "\\(^@menu\\)" nil t
))
1091 (defun texinfo-copy-menu-title ()
1092 "Return the title of the section preceding the menu as a string.
1093 If such a title cannot be found, return an empty string. Do not move
1095 (let ((case-fold-search t
))
1097 (if (re-search-backward
1101 texinfo-section-types-regexp
; all other section types
1107 (forward-word 1) ; skip over section type
1108 (skip-chars-forward " \t") ; and over spaces
1111 (progn (end-of-line) (point))))
1114 (defun texinfo-copy-menu ()
1115 "Return the entries of an existing menu as a list.
1116 Start with point just after the word `menu' in the `@menu' line
1117 and leave point on the line before the `@end menu' line."
1118 (let* (this-menu-list
1119 (end-of-menu (texinfo-menu-end)) ; position of end of `@end menu'
1120 (last-entry (save-excursion ; position of beginning of
1122 (goto-char end-of-menu
)
1123 ;; handle multi-line description
1124 (if (not (re-search-backward "^\\* " nil t
))
1125 (error "No entries in menu"))
1127 (while (< (point) last-entry
)
1128 (if (re-search-forward "^\\* " end-of-menu t
)
1129 (push (buffer-substring
1131 ;; copy multi-line descriptions
1133 (re-search-forward "\\(^\\* \\|^@e\\)" nil t
)
1139 ;;; Determining the hierarchical level in the texinfo file
1141 (defun texinfo-specific-section-type ()
1142 "Return the specific type of next section, as a string.
1143 For example, \"unnumberedsubsec\". Return \"top\" for top node.
1145 Searches forward for a section. Hence, point must be before the
1146 section whose type will be found. Does not move point. Signal an
1147 error if the node is not the top node and a section is not found."
1148 (let ((case-fold-search t
))
1151 ((re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)"
1152 ;; Following search limit by cph but causes a bug
1153 ;;(line-end-position)
1157 ((re-search-forward texinfo-section-types-regexp nil t
)
1158 (buffer-substring-no-properties
1159 (progn (beginning-of-line) ; copy its name
1161 (progn (forward-word 1)
1165 "texinfo-specific-section-type: Chapter or section not found"))))))
1167 (defun texinfo-hierarchic-level ()
1168 "Return the general hierarchical level of the next node in a texinfo file.
1169 Thus, a subheading or appendixsubsec is of type subsection."
1170 (let ((case-fold-search t
))
1172 (texinfo-specific-section-type)
1173 texinfo-section-list
))))
1176 ;;; Locating the major positions
1178 (defun texinfo-update-menu-region-beginning (level)
1179 "Locate beginning of higher level section this section is within.
1180 Return position of the beginning of the node line; do not move point.
1181 Thus, if this level is subsection, searches backwards for section node.
1182 Only argument is a string of the general type of section."
1183 (let ((case-fold-search t
))
1184 ;; !! Known bug: if section immediately follows top node, this
1185 ;; returns the beginning of the buffer as the beginning of the
1186 ;; higher level section.
1190 (goto-char (point-min))
1191 (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t
)
1192 (line-beginning-position)))
1197 "\\(^@node\\).*\n" ; match node line
1198 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
1200 "\\(^@ifinfo[ ]*\n\\)" ; ifinfo line, if any
1202 "\\(^@ifnottex[ ]*\n\\)" ; ifnottex line, if any
1203 "\\)?" ; end of expression
1205 (cdr (assoc level texinfo-update-menu-higher-regexps
))))
1210 (defun texinfo-update-menu-region-end (level)
1211 "Locate end of higher level section this section is within.
1212 Return position; do not move point. Thus, if this level is a
1213 subsection, find the node for the section this subsection is within.
1214 If level is top or chapter, returns end of file. Only argument is a
1215 string of the general type of section."
1216 (let ((case-fold-search t
))
1218 (if (re-search-forward
1220 "\\(^@node\\).*\n" ; match node line
1221 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
1223 "\\(^@ifinfo[ ]*\n\\)" ; ifinfo line, if any
1225 "\\(^@ifnottex[ ]*\n\\)" ; ifnottex line, if any
1226 "\\)?" ; end of expression
1228 ;; Never finds end of level above chapter so goes to end.
1229 (cdr (assoc level texinfo-update-menu-higher-regexps
))))
1235 (defun texinfo-menu-first-node (beginning end
)
1236 "Locate first node of the section the menu will be placed in.
1237 Return position; do not move point.
1238 The menu will be located just before this position.
1240 First argument is the position of the beginning of the section in
1241 which the menu will be located; second argument is the position of the
1242 end of that region; it limits the search."
1244 (goto-char beginning
)
1246 (re-search-forward "^@node" end t
)
1247 (line-beginning-position)))
1252 (defun texinfo-update-node (&optional beginning end
)
1253 "Without any prefix argument, update the node in which point is located.
1254 Interactively, a prefix argument means to operate on the region.
1256 Warning: do NOT use this function if your Texinfo file uses @node
1257 lines without the `Next', `Previous', `Up' pointers, because the
1258 result could be an invalid Texinfo file due to known deficiencies
1259 in this command: it does not support @ignore and @if* directives.
1261 The functions for creating or updating nodes and menus, and their
1264 texinfo-update-node (&optional beginning end) \\[texinfo-update-node]
1265 texinfo-every-node-update () \\[texinfo-every-node-update]
1266 texinfo-sequential-node-update (&optional region-p)
1268 texinfo-make-menu (&optional region-p) \\[texinfo-make-menu]
1269 texinfo-all-menus-update () \\[texinfo-all-menus-update]
1270 texinfo-master-menu ()
1272 texinfo-indent-menu-description (column &optional region-p)
1274 The `texinfo-column-for-description' variable specifies the column to
1275 which menu descriptions are indented. Its default value is 32."
1279 (list (point) (mark))))
1280 (if (null beginning
)
1281 ;; Update a single node.
1282 (let ((auto-fill-function nil
))
1283 (if (not (re-search-backward "^@node" (point-min) t
))
1284 (error "Node line not found before this position"))
1285 (texinfo-update-the-node)
1286 (message "Done...updated the node. You may save the buffer."))
1288 (let ((auto-fill-function nil
))
1291 (narrow-to-region beginning end
)
1292 (goto-char (point-min))
1293 (while (re-search-forward "^@node" (point-max) t
)
1295 (texinfo-update-the-node))
1296 (goto-char (point-max))
1297 (message "Done...nodes updated in region. You may save the buffer."))))))
1299 (defun texinfo-every-node-update ()
1300 "Update every node in a Texinfo file.
1302 Warning: do NOT use this function if your Texinfo file uses @node
1303 lines without the `Next', `Previous', `Up' pointers, because the
1304 result could be an invalid Texinfo file due to known deficiencies
1305 in this command: it does not support @ignore and @if* directives."
1308 (texinfo-update-node (point-min) (point-max))
1309 (message "Done...updated every node. You may save the buffer.")))
1311 (defun texinfo-update-the-node ()
1312 "Update one node. Point must be at the beginning of node line.
1313 Leave point at the end of the node line."
1314 (texinfo-check-for-node-name)
1315 (texinfo-delete-existing-pointers)
1316 (message "Updating node: %s ... " (texinfo-copy-node-name))
1320 ((case-fold-search t
)
1321 (level (texinfo-hierarchic-level))
1322 (beginning (texinfo-update-menu-region-beginning level
))
1323 (end (texinfo-update-menu-region-end level
)))
1325 (texinfo-top-pointer-case)
1327 (texinfo-insert-pointer beginning end level
'next
)
1328 (texinfo-insert-pointer beginning end level
'previous
)
1329 (texinfo-insert-pointer beginning end level
'up
)
1330 (texinfo-clean-up-node-line)))))
1332 (defun texinfo-top-pointer-case ()
1333 "Insert pointers in the Top node. This is a special case.
1335 The `Next' pointer is a pointer to a chapter or section at a lower
1336 hierarchical level in the file. The `Previous' and `Up' pointers are
1337 to `(dir)'. Point must be at the beginning of the node line, and is
1338 left at the end of the node line."
1340 (texinfo-clean-up-node-line)
1343 ;; There may be an @chapter or other such command between
1344 ;; the top node line and the next node line, as a title
1345 ;; for an `ifinfo' section. This @chapter command must
1346 ;; must be skipped. So the procedure is to search for
1347 ;; the next `@node' line, and then copy its name.
1348 (if (re-search-forward "^@node" nil t
)
1351 (texinfo-copy-node-name))
1355 (defun texinfo-check-for-node-name ()
1356 "Determine whether the node has a node name. Prompt for one if not.
1357 Point must be at beginning of node line. Does not move point."
1359 (let ((initial (texinfo-copy-next-section-title)))
1360 ;; This is not clean. Use `interactive' to read the arg.
1361 (forward-word 1) ; skip over node command
1362 (skip-chars-forward " \t") ; and over spaces
1363 (if (not (looking-at "[^,\t\n ]+")) ; regexp based on what Info looks for
1364 ; alternatively, use "[a-zA-Z]+"
1366 (read-from-minibuffer
1367 "Node name (use no @, commas, colons, or apostrophes): "
1369 (insert " " node-name
))))))
1371 (defun texinfo-delete-existing-pointers ()
1372 "Delete `Next', `Previous', and `Up' pointers.
1373 Starts from the current position of the cursor, and searches forward
1374 on the line for a comma and if one is found, deletes the rest of the
1375 line, including the comma. Leaves point at beginning of line."
1376 (let ((eol-point (line-end-position)))
1377 (if (search-forward "," eol-point t
)
1378 (delete-region (1- (point)) eol-point
)))
1379 (beginning-of-line))
1381 (defun texinfo-find-pointer (beginning end level direction
)
1382 "Move point to section associated with next, previous, or up pointer.
1383 Return type of pointer (either `normal' or `no-pointer').
1385 The first and second arguments bound the search for a pointer to the
1386 beginning and end, respectively, of the enclosing higher level
1387 section. The third argument is a string specifying the general kind
1388 of section such as \"chapter\" or \"section\". When looking for the
1389 `Next' pointer, the section found will be at the same hierarchical
1390 level in the Texinfo file; when looking for the `Previous' pointer,
1391 the section found will be at the same or higher hierarchical level in
1392 the Texinfo file; when looking for the `Up' pointer, the section found
1393 will be at some level higher in the Texinfo file. The fourth argument
1394 \(one of 'next, 'previous, or 'up\) specifies whether to find the
1395 `Next', `Previous', or `Up' pointer."
1396 (let ((case-fold-search t
))
1397 (cond ((eq direction
'next
)
1398 (forward-line 3) ; skip over current node
1399 ;; Search for section commands accompanied by node lines;
1400 ;; ignore section commands in the middle of nodes.
1401 (if (re-search-forward
1402 ;; A `Top' node is never a next pointer, so won't find it.
1406 ;; Match comment, ifinfo, ifnottex line, if any
1411 "\\(^@ifinfo[ ]*\n\\)"
1413 "\\(^@ifnottex[ ]*\n\\)"
1416 (cdr (assoc level texinfo-update-menu-same-level-regexps
))))
1421 ((eq direction
'previous
)
1422 (if (re-search-backward
1427 ;; Match comment, ifinfo, ifnottex line, if any
1432 "\\(^@ifinfo[ ]*\n\\)"
1434 "\\(^@ifnottex[ ]*\n\\)"
1437 (cdr (assoc level texinfo-update-menu-same-level-regexps
)))
1441 ;; Match comment, ifinfo, ifnottex line, if any
1446 "\\(^@ifinfo[ ]*\n\\)"
1448 "\\(^@ifnottex[ ]*\n\\)"
1451 (cdr (assoc level texinfo-update-menu-higher-regexps
)))
1453 ;; Handle `Top' node specially.
1454 "^@node [ \t]*top[ \t]*\\(,\\|$\\)"
1461 (if (re-search-backward
1466 ;; Match comment, ifinfo, ifnottex line, if any
1471 "\\(^@ifinfo[ ]*\n\\)"
1473 "\\(^@ifnottex[ ]*\n\\)"
1475 (eval (cdr (assoc level texinfo-update-menu-higher-regexps
)))
1477 ;; Handle `Top' node specially.
1478 "^@node [ \t]*top[ \t]*\\(,\\|$\\)"
1481 (goto-char beginning
)
1482 (line-beginning-position))
1487 (error "texinfo-find-pointer: lack proper arguments")))))
1489 (defun texinfo-pointer-name (kind)
1490 "Return the node name preceding the section command.
1491 The argument is the kind of section, either `normal' or `no-pointer'."
1493 (cond ((eq kind
'normal
)
1494 (end-of-line) ; this handles prev node top case
1495 (re-search-backward ; when point is already
1496 "^@node" ; at the beginning of @node line
1497 (line-beginning-position -
2)
1499 (setq name
(texinfo-copy-node-name)))
1500 ((eq kind
'no-pointer
)
1501 ;; Don't need to put a blank in the pointer slot,
1502 ;; since insert "' " always has a space
1503 (setq name
" "))) ; put a blank in the pointer slot
1506 (defun texinfo-insert-pointer (beginning end level direction
)
1507 "Insert the `Next', `Previous' or `Up' node name at point.
1510 The first and second arguments bound the search for a pointer to the
1511 beginning and end, respectively, of the enclosing higher level
1512 section. The third argument is the hierarchical level of the Texinfo
1513 file, a string such as \"section\". The fourth argument is direction
1514 towards which the pointer is directed, one of `next', `previous', or `up'."
1520 (texinfo-pointer-name
1521 (texinfo-find-pointer beginning end level direction
)))))
1523 (defun texinfo-clean-up-node-line ()
1524 "Remove extra commas, if any, at end of node line."
1526 (skip-chars-backward ", ")
1527 (delete-region (point) (line-end-position)))
1530 ;;; Updating nodes sequentially
1531 ;; These sequential update functions insert `Next' or `Previous'
1532 ;; pointers that point to the following or preceding nodes even if they
1533 ;; are at higher or lower hierarchical levels. This means that if a
1534 ;; section contains one or more subsections, the section's `Next'
1535 ;; pointer will point to the subsection and not the following section.
1536 ;; (The subsection to which `Next' points will most likely be the first
1537 ;; item on the section's menu.)
1539 (defun texinfo-sequential-node-update (&optional region-p
)
1540 "Update one node (or many) in a Texinfo file with sequential pointers.
1542 This function causes the `Next' or `Previous' pointer to point to the
1543 immediately preceding or following node, even if it is at a higher or
1544 lower hierarchical level in the document. Continually pressing `n' or
1545 `p' takes you straight through the file.
1547 Without any prefix argument, update the node in which point is located.
1548 Non-nil argument (prefix, if interactive) means update the nodes in the
1551 This command makes it awkward to navigate among sections and
1552 subsections; it should be used only for those documents that are meant
1553 to be read like a novel rather than a reference, and for which the
1554 Info `g*' command is inadequate."
1558 ;; update a single node
1559 (let ((auto-fill-function nil
))
1560 (if (not (re-search-backward "^@node" (point-min) t
))
1561 (error "Node line not found before this position"))
1562 (texinfo-sequentially-update-the-node)
1564 "Done...sequentially updated the node . You may save the buffer."))
1566 (let ((auto-fill-function nil
)
1567 (beginning (region-beginning))
1569 (if (= end beginning
)
1570 (error "Please mark a region"))
1572 (narrow-to-region beginning end
)
1573 (goto-char beginning
)
1574 (push-mark (point) t
)
1575 (while (re-search-forward "^@node" (point-max) t
)
1577 (texinfo-sequentially-update-the-node))
1579 "Done...updated the nodes in sequence. You may save the buffer.")))))
1581 (defun texinfo-sequentially-update-the-node ()
1582 "Update one node such that the pointers are sequential.
1583 A `Next' or `Previous' pointer points to any preceding or following node,
1584 regardless of its hierarchical level."
1586 (texinfo-check-for-node-name)
1587 (texinfo-delete-existing-pointers)
1589 "Sequentially updating node: %s ... " (texinfo-copy-node-name))
1592 (let* ((case-fold-search t
)
1593 (level (texinfo-hierarchic-level)))
1595 (texinfo-top-pointer-case)
1597 (texinfo-sequentially-insert-pointer level
'next
)
1598 (texinfo-sequentially-insert-pointer level
'previous
)
1599 (texinfo-sequentially-insert-pointer level
'up
)
1600 (texinfo-clean-up-node-line)))))
1602 (defun texinfo-sequentially-insert-pointer (level direction
)
1603 "Insert the `Next', `Previous' or `Up' node name at point.
1606 The first argument is the hierarchical level of the Texinfo file, a
1607 string such as \"section\". The second argument is direction, one of
1608 `next', `previous', or `up'."
1614 (texinfo-pointer-name
1615 (texinfo-sequentially-find-pointer level direction
)))))
1617 (defun texinfo-sequentially-find-pointer (level direction
)
1618 "Find next or previous pointer sequentially in Texinfo file, or up pointer.
1619 Move point to section associated with the pointer. Find point even if
1620 it is in a different section.
1622 Return type of pointer (either `normal' or `no-pointer').
1624 The first argument is a string specifying the general kind of section
1625 such as \"chapter\" or \"section\". The section found will be at the
1626 same hierarchical level in the Texinfo file, or, in the case of the up
1627 pointer, some level higher. The second argument (one of `next',
1628 `previous', or `up') specifies whether to find the `Next', `Previous',
1630 (let ((case-fold-search t
))
1631 (cond ((eq direction
'next
)
1632 (forward-line 3) ; skip over current node
1633 (if (re-search-forward
1634 texinfo-section-types-regexp
1639 ((eq direction
'previous
)
1640 (if (re-search-backward
1641 texinfo-section-types-regexp
1647 (if (re-search-backward
1648 (eval (cdr (assoc level texinfo-update-menu-higher-regexps
)))
1654 (error "texinfo-sequential-find-pointer: lack proper arguments")))))
1657 ;;; Inserting `@node' lines
1658 ;; The `texinfo-insert-node-lines' function inserts `@node' lines as needed
1659 ;; before the `@chapter', `@section', and such like lines of a region
1660 ;; in a Texinfo file.
1662 (defun texinfo-insert-node-lines (beginning end
&optional title-p
)
1663 "Insert missing `@node' lines in region of Texinfo file.
1664 Non-nil argument (prefix, if interactive) means also to insert the
1665 section titles as node names; and also to insert the section titles as
1666 node names in pre-existing `@node' lines that lack names."
1667 (interactive "r\nP")
1669 ;; Use marker; after inserting node lines, leave point at end of
1670 ;; region and mark at beginning.
1672 (let (beginning-marker end-marker title last-section-position
)
1674 ;; Save current position on mark ring and set mark to end.
1676 (setq end-marker
(mark-marker))
1678 (goto-char beginning
)
1679 (while (re-search-forward
1680 texinfo-section-types-regexp
1683 ;; Copy title if desired.
1688 (skip-chars-forward " \t")
1689 (setq title
(buffer-substring
1691 (line-end-position)))))
1692 ;; Insert node line if necessary.
1693 (if (re-search-backward
1695 ;; Avoid finding previous node line if node lines are close.
1696 (or last-section-position
1697 (line-beginning-position -
1))
1699 ;; @node is present, and point at beginning of that line
1700 (forward-word 1) ; Leave point just after @node.
1701 ;; Else @node missing; insert one.
1702 (beginning-of-line) ; Beginning of `@section' line.
1704 (backward-char 1)) ; Leave point just after `@node'.
1705 ;; Insert title if desired.
1708 (skip-chars-forward " \t")
1709 ;; Use regexp based on what info looks for
1710 ;; (alternatively, use "[a-zA-Z]+");
1711 ;; this means we only insert a title if none exists.
1712 (if (not (looking-at "[^,\t\n ]+"))
1717 (message "Inserted title %s ... " title
)))))
1718 ;; Go forward beyond current section title.
1719 (re-search-forward texinfo-section-types-regexp
1720 (line-beginning-position 4) t
)
1721 (setq last-section-position
(point))
1724 ;; Leave point at end of region, mark at beginning.
1725 (set-mark beginning
)
1729 "Done inserting node lines and titles. You may save the buffer.")
1730 (message "Done inserting node lines. You may save the buffer."))))
1733 ;;; Update and create menus for multi-file Texinfo sources
1735 ;; 1. M-x texinfo-multiple-files-update
1737 ;; Read the include file list of an outer Texinfo file and
1738 ;; update all highest level nodes in the files listed and insert a
1739 ;; main menu in the outer file after its top node.
1741 ;; 2. C-u M-x texinfo-multiple-files-update
1743 ;; Same as 1, but insert a master menu. (Saves reupdating lower
1744 ;; level menus and nodes.) This command simply reads every menu,
1745 ;; so if the menus are wrong, the master menu will be wrong.
1746 ;; Similarly, if the lower level node pointers are wrong, they
1749 ;; 3. C-u 2 M-x texinfo-multiple-files-update
1751 ;; Read the include file list of an outer Texinfo file and
1752 ;; update all nodes and menus in the files listed and insert a
1753 ;; master menu in the outer file after its top node.
1755 ;;; Note: these functions:
1757 ;;; * Do not save or delete any buffers. You may fill up your memory.
1758 ;;; * Do not handle any pre-existing nodes in outer file.
1759 ;;; Hence, you may need a file for indices.
1762 ;;; Auxiliary functions for multiple file updating
1764 (defun texinfo-multi-file-included-list (outer-file)
1765 "Return a list of the included files in OUTER-FILE."
1766 (let ((included-file-list (list outer-file
))
1768 (with-current-buffer (find-file-noselect outer-file
)
1770 (goto-char (point-min))
1771 (while (re-search-forward "^@include" nil t
)
1772 (skip-chars-forward " \t")
1773 (setq start
(point))
1775 (skip-chars-backward " \t")
1776 (setq included-file-list
1777 (cons (buffer-substring start
(point))
1778 included-file-list
)))
1779 (nreverse included-file-list
))))
1781 (defun texinfo-copy-next-section-title ()
1782 "Return the name of the immediately following section as a string.
1784 Start with point at the beginning of the node line. Leave point at the
1785 same place. If there is no title, returns an empty string."
1791 (if (re-search-forward "\\(^@node\\)" nil t
)
1792 (match-beginning 0)))
1794 (if (re-search-forward texinfo-section-types-regexp node-end t
)
1800 (progn (forward-word 1) ; skip over section type
1801 (skip-chars-forward " \t") ; and over spaces
1803 (progn (end-of-line) (point)))))
1807 (defun texinfo-multi-file-update (files &optional update-everything
)
1808 "Update first node pointers in each file in FILES.
1809 Return a list of the node names.
1811 The first file in the list is an outer file; the remaining are
1812 files included in the outer file with `@include' commands.
1814 If optional arg UPDATE-EVERYTHING non-nil, update every menu and
1815 pointer in each of the included files.
1817 Also update the `Top' level node pointers of the outer file.
1821 * the first file in the FILES list must be the outer file,
1822 * each of the included files must contain exactly one highest
1823 hierarchical level node,
1824 * this node must be the first node in the included file,
1825 * each highest hierarchical level node must be of the same type.
1827 Thus, normally, each included file contains one, and only one, chapter.
1829 However, when an included file does not have any node lines in
1830 it, this command does not try to create a menu entry for it.
1831 Consequently, you can include any file, such as a version or an
1832 update file without node lines, not just files that are
1835 ;; The menu-list has the form:
1837 ;; \(\(\"node-name1\" . \"title1\"\)
1838 ;; \(\"node-name2\" . \"title2\"\) ... \)
1840 ;; However, there does not need to be a title field and this function
1841 ;; does not fill it; however a comment tells you how to do so.
1842 ;; You would use the title field if you wanted to insert titles in the
1843 ;; description slot of a menu as a description.
1845 (let ((case-fold-search t
)
1846 menu-list next-node-name previous-node-name files-with-node-lines
)
1848 ;; Create a new list of included files that only have node lines
1850 (set-buffer (find-file-noselect (car files
)))
1852 (goto-char (point-min))
1853 (when (re-search-forward "^@node" nil t
)
1854 (setq files-with-node-lines
(cons (car files
) files-with-node-lines
)))
1855 (setq files
(cdr files
)))
1856 (setq files-with-node-lines
(nreverse files-with-node-lines
))
1858 ;; Find the name of the first node in a subsequent file
1859 ;; and copy it into the variable next-node-name
1860 (set-buffer (find-file-noselect (car (cdr files-with-node-lines
))))
1862 (goto-char (point-min))
1863 ;; The following search _must_ succeed, since we verified above
1864 ;; that this file does have a @node line.
1865 (re-search-forward "^@node" nil t
)
1867 (texinfo-check-for-node-name)
1868 (setq next-node-name
(texinfo-copy-node-name))
1869 (push (cons next-node-name
(prog1 "" (forward-line 1)))
1870 ;; Use following to insert section titles automatically.
1871 ;; (texinfo-copy-next-section-title)
1875 ;; `pop' is analogous to (prog1 (car PLACE) (setf PLACE (cdr PLACE)))
1876 (set-buffer (find-file-noselect (pop files-with-node-lines
)))
1877 (goto-char (point-min))
1878 (if (not (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t
))
1879 (error "This buffer needs a Top node"))
1881 (texinfo-delete-existing-pointers)
1883 (insert ", " next-node-name
", (dir), (dir)")
1885 (setq previous-node-name
"Top")
1887 (while files-with-node-lines
1889 (if (not (cdr files-with-node-lines
))
1891 (setq next-node-name
"")
1893 ;; find the name of the first node in the next file.
1894 (set-buffer (find-file-noselect (car (cdr files-with-node-lines
))))
1896 (goto-char (point-min))
1897 ;; The following search _must_ succeed, since we verified
1898 ;; above that files in files-with-node-lines do have a @node
1900 (re-search-forward "^@node" nil t
)
1902 (texinfo-check-for-node-name)
1903 (setq next-node-name
(texinfo-copy-node-name))
1904 (push (cons next-node-name
(prog1 "" (forward-line 1)))
1905 ;; Use following to insert section titles automatically.
1906 ;; (texinfo-copy-next-section-title)
1909 ;; Go to node to be updated.
1910 (set-buffer (find-file-noselect (car files-with-node-lines
)))
1911 (goto-char (point-min))
1914 ;; Update other menus and nodes if requested.
1915 (if update-everything
(texinfo-all-menus-update t
))
1918 (texinfo-delete-existing-pointers)
1920 (insert ", " next-node-name
", " previous-node-name
", Top")
1923 (setq previous-node-name
(texinfo-copy-node-name))
1925 (setq files-with-node-lines
(cdr files-with-node-lines
)))
1926 (nreverse menu-list
)))
1928 (defun texinfo-multi-files-insert-main-menu (menu-list)
1929 "Insert formatted main menu at point.
1930 Indents the first line of the description, if any, to the value of
1931 `texinfo-column-for-description'."
1934 (dolist (entry menu-list
)
1935 ;; Every menu entry starts with a star and a space.
1938 ;; Insert the node name (and menu entry name, if present).
1939 (let ((node-part (car entry
)))
1940 (if (stringp node-part
)
1941 ;; "Double colon" entry line; menu entry and node name are the same,
1942 (insert (format "%s::" node-part
))
1943 ;; "Single colon" entry line; menu entry and node name are different.
1944 (insert (format "%s: %s." (car node-part
) (cdr node-part
)))))
1946 ;; Insert the description, if present.
1948 ;; Move to right place.
1949 (indent-to texinfo-column-for-description
2)
1950 ;; Insert description.
1951 (insert (format "%s" (cdr entry
))))
1953 (insert "\n")) ; end this menu entry
1954 (insert "@end menu"))
1956 (defun texinfo-multi-file-master-menu-list (files-list)
1957 "Return master menu list from files in FILES-LIST.
1958 Menu entries in each file collected using `texinfo-master-menu-list'.
1960 The first file in FILES-LIST must be the outer file; the others must
1961 be the files included within it. A main menu must already exist."
1963 (let (master-menu-list)
1964 (dolist (file files-list
)
1965 (set-buffer (find-file-noselect file
))
1966 (message "Working on: %s " (current-buffer))
1967 (goto-char (point-min))
1968 (setq master-menu-list
1969 (append master-menu-list
(texinfo-master-menu-list))))
1973 ;;; The multiple-file update function
1975 (defun texinfo-multiple-files-update
1976 (outer-file &optional make-master-menu update-everything
)
1977 "Update first node pointers in each file included in OUTER-FILE;
1978 create or update the `Top' level node pointers and the main menu in
1979 the outer file that refers to such nodes. This does not create or
1980 update menus or pointers within the included files.
1982 With optional MAKE-MASTER-MENU argument (prefix arg, if interactive),
1983 insert a master menu in OUTER-FILE in addition to creating or updating
1984 pointers in the first @node line in each included file and creating or
1985 updating the `Top' level node pointers of the outer file. This does
1986 not create or update other menus and pointers within the included
1989 With optional UPDATE-EVERYTHING argument (numeric prefix arg, if
1990 interactive), update all the menus and all the `Next', `Previous', and
1991 `Up' pointers of all the files included in OUTER-FILE before inserting
1992 a master menu in OUTER-FILE. Also, update the `Top' level node
1993 pointers of OUTER-FILE. Do NOT invoke this command with a numeric prefix
1994 arg, if your files use @node lines without the `Next', `Previous', `Up'
1995 pointers, because this could produce invalid Texinfo files due to known
1996 deficiencies in `texinfo-update-node': it does not support the @ignore
1997 and @if... directives.
2001 * this command does NOT save any files--you must save the
2002 outer file and any modified, included files.
2004 * except for the `Top' node, this command does NOT handle any
2005 pre-existing nodes in the outer file; hence, indices must be
2006 enclosed in an included file.
2010 * each of the included files must contain exactly one highest
2011 hierarchical level node,
2012 * this highest node must be the first node in the included file,
2013 * each highest hierarchical level node must be of the same type.
2015 Thus, normally, each included file contains one, and only one,
2020 "Name of outer `include' file: "
2023 ((not current-prefix-arg
) '(nil nil
))
2024 ((listp current-prefix-arg
) '(t nil
)) ; make-master-menu
2025 ((numberp current-prefix-arg
) '(t t
))))) ; update-everything
2027 (let* ((included-file-list (texinfo-multi-file-included-list outer-file
))
2028 (files included-file-list
)
2031 ;; Update the pointers and collect the names of the nodes and titles
2032 (main-menu-list (texinfo-multi-file-update files update-everything
)))
2037 (set-buffer (find-file-noselect (car included-file-list
)))
2038 (if (texinfo-old-menu-p
2041 (re-search-forward "^@include")
2042 (line-beginning-position)))
2043 ;; If found, leave point after word `menu' on the `@menu' line.
2045 (texinfo-incorporate-descriptions main-menu-list
)
2046 ;; Delete existing menu.
2050 (save-excursion (re-search-forward "^@end menu") (point)))
2052 (texinfo-multi-files-insert-main-menu main-menu-list
))
2054 ;; Else no current menu; insert it before `@include'
2055 (texinfo-multi-files-insert-main-menu main-menu-list
))
2057 ;; Insert master menu
2059 (if make-master-menu
2061 ;; First, removing detailed part of any pre-existing master menu
2062 (goto-char (point-min))
2063 (if (search-forward texinfo-master-menu-header nil t
)
2065 (goto-char (match-beginning 0))
2066 ;; Check if @detailmenu kludge is used;
2067 ;; if so, leave point before @detailmenu.
2068 (search-backward "\n@detailmenu" (line-beginning-position -
2) t
)
2069 ;; Remove detailed master menu listing
2070 (let ((end-of-detailed-menu-descriptions
2071 (save-excursion ; beginning of end menu line
2072 (goto-char (texinfo-menu-end))
2073 (beginning-of-line) (forward-char -
1)
2075 (delete-region (point) end-of-detailed-menu-descriptions
))))
2077 ;; Create a master menu and insert it
2078 (texinfo-insert-master-menu-list
2079 (texinfo-multi-file-master-menu-list
2080 included-file-list
)))))
2082 ;; Remove unwanted extra lines.
2084 (goto-char (point-min))
2086 (re-search-forward "^@menu")
2088 (insert "\n") ; Ensure at least one blank line.
2089 (delete-blank-lines)
2091 (re-search-forward "^@end menu")
2093 (insert "\n") ; Ensure at least one blank line.
2094 (delete-blank-lines))
2096 (message "Multiple files updated."))
2099 ;; Place `provide' at end of file.
2100 (provide 'texnfo-upd
)
2102 ;;; texnfo-upd.el ends here