Initial revision
[emacs.git] / lisp / textmodes / texnfo-upd.el
blob1a46a02e5202116383f3dd90ab1fb8057ba625c4
1 ;;;; texnfo-upd.el
2 ;;;; A utility for updating nodes and menus in Texinfo files.
4 ;;;; Robert J. Chassell, 23 September 1989; 15 May 1990
6 ;;;; Copyright 1989, 1990 Free Software Foundation
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 1, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 (provide 'texnfo-upd)
27 ;;;; Summary
29 ; (Much of the following commentary ought eventually be incorporated
30 ; into the Texinfo Manual.)
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 ; Passed an argument, the `texinfo-update-node' and
40 ; `texinfo-make-menu' functions do their jobs in the region.
42 ; These functions replace doing these jobs by hand.
43 ; You may find them helpful.
45 ; In brief, the functions for creating or updating nodes and menus, are:
47 ; texinfo-update-node (&optional region-p)
48 ; texinfo-every-node-update ()
49 ; texinfo-sequential-node-update (&optional region-p)
51 ; texinfo-make-menu (&optional region-p)
52 ; texinfo-all-menus-update ()
53 ; texinfo-master-menu ()
55 ; texinfo-insert-node-lines (&optional title-p)
57 ; texinfo-indent-menu-description (column &optional region-p)
59 ; The `texinfo-column-for-description' variable specifies the column to
60 ; which menu descriptions are indented.
62 ; Texinfo file structure
63 ; ----------------------
65 ; To use the updating commands, you must structure your Texinfo file
66 ; hierarchically. Each `@node' line, with the exception of the top
67 ; node, must be accompanied by some kind of section line, such as an
68 ; `@chapter' or `@section' line. Each node-line/section-line
69 ; combination must look like this:
71 ; @node Lists and Tables, Cross References, Structuring, Top
72 ; @comment node-name, next, previous, up
73 ; @chapter Making Lists and Tables
75 ; or like this (without the `@comment' line):
77 ; @node Lists and Tables, Cross References, Structuring, Top
78 ; @chapter Making Lists and Tables
80 ; If the file has a `top' node, it must be called `top' or `Top' and
81 ; be the first node in the file.
84 ;;;; The updating functions in detail
85 ; --------------------------------
87 ; The `texinfo-update-node' function without an argument inserts
88 ; the correct next, previous and up pointers for the node in which
89 ; point is located (i.e., for the node preceding point).
91 ; With an argument, the `texinfo-update-node' function inserts the
92 ; correct next, previous and up pointers for the nodes inside the
93 ; region.
95 ; It does not matter whether the `@node' line has pre-existing
96 ; `Next', `Previous', or `Up' pointers in it. They are removed.
98 ; The `texinfo-every-node-update' function runs `texinfo-update-node'
99 ; on the whole buffer.
101 ; The `texinfo-update-node' function inserts the immediately following
102 ; and preceding node into the `Next' or `Previous' pointers regardless
103 ; of their hierarchical level. This is only useful for certain kinds
104 ; of text, like a novel, which you go through sequentially.
106 ; The `texinfo-make-menu' function without an argument creates or
107 ; updates a menu for the section encompassing the node that follows
108 ; point. With an argument, it makes or updates menus for the nodes
109 ; within or part of the marked region.
111 ; Whenever an existing menu is updated, the descriptions from
112 ; that menu are incorporated into the new menu. This is done by copying
113 ; descriptions from the existing menu to the entries in the new menu
114 ; that have the same node names. If the node names are different, the
115 ; descriptions are not copied to the new menu.
117 ; Menu entries that refer to other Info files are removed since they
118 ; are not a node within current buffer. This is a deficiency.
120 ; The `texinfo-all-menus-update' function runs `texinfo-make-menu'
121 ; on the whole buffer.
123 ; The `texinfo-master-menu' function creates an extended menu located
124 ; after the top node. (The file must have a top node.) The function
125 ; first updates all the regular menus in the buffer (incorporating the
126 ; descriptions from pre-existing menus), and then constructs a master
127 ; menu that includes every entry from every other menu. (However, the
128 ; function cannot update an already existing master menu; if one
129 ; exists, it must be removed before calling the function.)
131 ; The `texinfo-indent-menu-description' function indents every
132 ; description in the menu following point, to the specified column.
133 ; Non-nil argument (prefix, if interactive) means indent every
134 ; description in every menu in the region. This function does not
135 ; indent second and subsequent lines of a multi-line description.
137 ; The `texinfo-insert-node-lines' function inserts `@node' before the
138 ; `@chapter', `@section', and such like lines of a region in a Texinfo
139 ; file where the `@node' lines are missing.
141 ; With a non-nil argument (prefix, if interactive), the function not
142 ; only inserts `@node' lines but also inserts the chapter or section
143 ; titles as the names of the corresponding nodes; and inserts titles
144 ; as node names in pre-existing `@node' lines that lack names.
146 ; Since node names should be more concise than section or chapter
147 ; titles, node names so inserted will need to be edited manually.
150 ;;;; Menu Making Functions
152 (defun texinfo-make-menu (&optional region-p)
153 "Without any prefix argument, make or update a menu.
154 Make the menu for the section enclosing the node found following point.
156 Non-nil argument (prefix, if interactive) means make or update menus
157 for nodes within or part of the marked region.
159 Whenever a menu exists, and is being updated, the descriptions that
160 are associated with node names in the pre-existing menu are
161 incorporated into the new menu. Otherwise, the nodes' section titles
162 are inserted as descriptions."
164 (interactive "P")
165 (if (not region-p)
166 (let ((level (texinfo-hierarchic-level)))
167 (texinfo-make-one-menu level)
168 (message "Done...updated the menu. You may save the buffer."))
169 ;; else
170 (message "Making or updating menus... ")
171 (let ((beginning (region-beginning))
172 (region-end (region-end))
173 (level (progn ; find section type following point
174 (goto-char (region-beginning))
175 (texinfo-hierarchic-level))))
176 (if (= region-end beginning)
177 (error "Please mark a region!"))
178 (save-excursion
179 (save-restriction
180 (widen)
182 (while (texinfo-find-lower-level-node level region-end)
183 (setq level (texinfo-hierarchic-level)) ; new, lower level
184 (texinfo-make-one-menu level))
186 (while (and (< (point) region-end)
187 (texinfo-find-higher-level-node level region-end))
188 (setq level (texinfo-hierarchic-level))
189 (while (texinfo-find-lower-level-node level region-end)
190 (setq level (texinfo-hierarchic-level)) ; new, lower level
191 (texinfo-make-one-menu level))))))
192 (message "Done...updated menus. You may save the buffer.")))
194 (defun texinfo-make-one-menu (level)
195 "Make a menu of all the appropriate nodes in this section.
196 `Appropriate nodes' are those associated with sections that are
197 at the level specified by LEVEL. Point is left at the end of menu."
198 (let*
199 ((case-fold-search t)
200 (beginning (texinfo-update-menu-region-beginning level))
201 (end (texinfo-update-menu-region-end level))
202 (first (texinfo-menu-first-node beginning end))
203 (node-name (progn
204 (goto-char beginning)
205 (texinfo-copy-node-name)))
206 (new-menu-list (texinfo-make-menu-list beginning end level)))
207 (if (texinfo-old-menu-p beginning first)
208 (progn
209 (texinfo-incorporate-descriptions new-menu-list)
210 (texinfo-delete-old-menu beginning first)))
211 (texinfo-insert-menu new-menu-list node-name)))
213 (defun texinfo-all-menus-update (&optional update-all-nodes-p)
214 "Update every regular menu in a Texinfo file.
215 You must remove the detailed part of a pre-existing master menu before
216 running this command, lest it be partly duplicated.
218 If called with a non-nil argument, this function first updates all the
219 nodes in the buffer before updating the menus."
220 (interactive "P")
221 (save-excursion
222 (mark-whole-buffer)
223 (message "Checking for a master menu... ")
224 (save-excursion
225 (if (re-search-forward texinfo-master-menu-header nil t)
226 (error
227 "Please remove existing master menu, lest it be partly duplicated!")))
229 (if update-all-nodes-p
230 (progn
231 (message "First updating all nodes... ")
232 (sleep-for 2)
233 (mark-whole-buffer)
234 (texinfo-update-node t)))
236 (message "Updating all menus... ")
237 (sleep-for 2)
238 (texinfo-make-menu t)
239 (message "Done...updated all the menus. You may save the buffer.")))
241 (defun texinfo-find-lower-level-node (level region-end)
242 "Search forward from point for node at any level lower than LEVEL.
243 Search is limited to the end of the marked region, REGION-END,
244 and to the end of the menu region for the level.
246 Return t if the node is found, else nil. Leave point at the beginning
247 of the node if one is found; else do not move point."
249 (if (and (< (point) region-end)
250 (re-search-forward
251 (concat
252 "\\(^@node\\).*\n" ; match node line
253 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
254 "\\|" ; or
255 "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
256 (eval (cdr (assoc level texinfo-update-menu-lower-regexps))))
257 ;; the next higher level node marks the end of this
258 ;; section, and no lower level node will be found beyond
259 ;; this position even if region-end is farther off
260 (texinfo-update-menu-region-end level)
262 (goto-char (match-beginning 1))))
264 (defun texinfo-find-higher-level-node (level region-end)
265 "Search forward from point for node at any higher level than argument LEVEL.
266 Search is limited to the end of the marked region, REGION-END.
268 Return t if the node is found, else nil. Leave point at the beginning
269 of the node if one is found; else do not move point."
271 (if (and (< (point) region-end)
272 (re-search-forward
273 (concat
274 "\\(^@node\\).*\n" ; match node line
275 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
276 "\\|" ; or
277 "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
278 (eval ; (won't ever find a `top' node)
279 (cdr (assoc level texinfo-update-menu-higher-regexps))))
282 (goto-char (match-beginning 1))))
285 ;;;; Making the list of new menu entries
287 (defun texinfo-make-menu-list (beginning end level)
288 "Make a list of node names and their descriptions.
289 Point is left at the end of the menu region, but the menu is not inserted.
291 First argument is position from which to start making menu list;
292 second argument is end of region in which to try to locate entries;
293 third argument is the level of the nodes that are the entries.
295 Node names and descriptions are dotted pairs of strings. Each pair is
296 an element of the list. If the description does not exist, the
297 element consists only of the node name."
298 (goto-char beginning)
299 (let (new-menu-list)
300 (while (texinfo-menu-locate-entry-p level end)
301 (setq new-menu-list
302 (cons (cons
303 (texinfo-copy-node-name)
304 (texinfo-copy-section-title))
305 new-menu-list)))
306 (reverse new-menu-list)))
308 (defun texinfo-menu-locate-entry-p (level search-end)
309 "Find a node that will be part of menu for this section.
310 First argument is a string such as \"section\" specifying the general
311 hierarchical level of the menu; second argument is a postion
312 specifying the end of the search.
314 The function returns t if the node is found, else nil. It searches
315 forward from point, and leaves point at the beginning of the node.
317 The function finds entries of the same type. Thus `subsections' and
318 `unnumberedsubsecs' will appear in the same menu."
319 (if (re-search-forward
320 (concat
321 "\\(^@node\\).*\n" ; match node line
322 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
323 "\\|" ; or
324 "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
325 (eval
326 (cdr (assoc level texinfo-update-menu-same-level-regexps))))
327 search-end
329 (goto-char (match-beginning 1))))
331 (defun texinfo-copy-node-name ()
332 "Return the node name as a string.
334 Start with point at the beginning of the node line; copy the text
335 after the node command up to the first comma on the line, if any, and
336 return the text as a string. Leaves point at the beginning of the
337 line. If there is no node name, returns an empty string."
339 (save-excursion
340 (buffer-substring
341 (progn (forward-word 1) ; skip over node command
342 (skip-chars-forward " \t") ; and over spaces
343 (point))
344 (if (search-forward
346 (save-excursion (end-of-line) (point)) t) ; bound search
347 (1- (point))
348 (end-of-line) (point)))))
350 (defun texinfo-copy-section-title ()
351 "Return the title of the section as a string.
352 The title is used as a description line in the menu when one does not
353 already exist.
355 Move point to the beginning of the appropriate section line by going
356 to the start of the text matched by last regexp searched for, which
357 must have been done by `texinfo-menu-locate-entry-p'."
359 ;; could use the same re-search as in `texinfo-menu-locate-entry-p'
360 ;; instead of using `match-beginning'; such a variation would be
361 ;; more general, but would waste information already collected
363 (goto-char (match-beginning 7)) ; match section name
365 (buffer-substring
366 (progn (forward-word 1) ; skip over section type
367 (skip-chars-forward " \t") ; and over spaces
368 (point))
369 (progn (end-of-line) (point))))
372 ;;;; Handling the old menu
374 (defun texinfo-old-menu-p (beginning first)
375 "Move point to the beginning of the menu for this section, if any.
376 Otherwise move point to the end of the first node of this section.
377 Return t if a menu is found, nil otherwise.
379 First argument is the position of the beginning of the section in which
380 the menu will be located; second argument is the position of the first
381 node within the section.
383 If no menu is found, the function inserts two newlines just before the
384 end of the section, and leaves point there where a menu ought to be."
385 (goto-char beginning)
386 (if (not (re-search-forward "^@menu" first 'goto-end))
387 (progn (insert "\n\n") (forward-line -2) nil)
390 (defun texinfo-incorporate-descriptions (new-menu-list)
391 "Copy the old menu line descriptions that exist to the new menu.
393 Point must be at beginning of old menu.
395 If the node-name of the new menu entry cannot be found in the old
396 menu, use the new section title for the description, but if the
397 node-name of the new menu is found in the old menu, replace the
398 section title with the old description, whatever it may be.
400 For this function, the new menu is a list made up of lists of dotted
401 pairs in which the first element of the pair is the node name and the
402 second element the description. The new menu is changed destructively.
403 The old menu is the menu as it appears in the texinfo file."
405 (let ((new-menu-list-pointer new-menu-list)
406 (end-of-menu (texinfo-menu-end)))
407 (while new-menu-list
408 (save-excursion ; keep point at beginning of menu
409 (if (search-forward
410 (concat "\* " ; so only menu entries are found
411 (car (car new-menu-list))
412 ":") ; so only complete entries are found
413 end-of-menu
415 (setcdr (car new-menu-list)
416 (texinfo-menu-copy-old-description end-of-menu))))
417 (setq new-menu-list (cdr new-menu-list)))
418 (setq new-menu-list new-menu-list-pointer)))
420 (defun texinfo-menu-copy-old-description (end-of-menu)
421 "Return description field of old menu line as string.
422 Point must be located just after the node name. Point left before description.
423 Single argument, END-OF-MENU, is position limiting search."
424 (skip-chars-forward "[:.,\t\n ]+")
425 ;; don't copy a carriage return at line beginning with asterisk!
426 ;; do copy a description that begins with an `@'!
427 (if (and (looking-at "\\(\\w+\\|@\\)")
428 (not (looking-at "\\(^\\* \\|^@end menu\\)")))
429 (buffer-substring
430 (point)
431 (save-excursion
432 (re-search-forward "\\(^\\* \\|^@end menu\\)" end-of-menu t)
433 (forward-line -1)
434 (end-of-line) ; go to end of last description line
435 (point)))
436 ""))
438 (defun texinfo-menu-end ()
439 "Return position of end of menu. Does not change location of point.
440 Signal an error if not end of menu."
441 (save-excursion
442 (if (re-search-forward "^@end menu" nil t)
443 (point)
444 (error "Menu does not have an end."))))
446 (defun texinfo-delete-old-menu (beginning first)
447 "Delete the old menu. Point must be in or after menu.
448 First argument is position of the beginning of the section in which
449 the menu will be located; second argument is the position of the first
450 node within the section."
451 ;; No third arg to search, so error if search fails.
452 (re-search-backward "^@menu" beginning)
453 (delete-region (point)
454 (save-excursion
455 (re-search-forward "^@end menu" first)
456 (point))))
459 ;;;; Inserting new menu
461 ;; try 32, but perhaps 24 is better
462 (defvar texinfo-column-for-description 32
463 "*Column at which descriptions start in a Texinfo menu.")
465 (defun texinfo-insert-menu (menu-list node-name)
466 "Insert formatted menu at point.
467 Indents the first line of the description, if any, to the value of
468 texinfo-column-for-description.
470 MENU-LIST has form:
472 \(\(\"node-name1\" . \"description\"\)
473 \(\"node-name\" . \"description\"\) ... \)
475 However, there does not need to be a description field."
477 (insert "@menu\n")
478 (while menu-list
479 (if (cdr (car menu-list)) ; menu-list has description entry
480 (progn
481 (insert
482 (format "* %s::" (car (car menu-list)))) ; node-name entry
483 (indent-to texinfo-column-for-description 2)
484 (insert
485 (format "%s\n" (cdr (car menu-list))))) ; description entry
486 ;; else menu-list lacks description entry
487 (insert
488 (format "* %s::\n" (car (car menu-list))))) ; node-name entry
489 (setq menu-list (cdr menu-list)))
490 (insert "@end menu")
491 (message
492 "Updated \"%s\" level menu following node: %s ... "
493 level node-name))
496 ;;;; Handling description indentation
498 ; Since the make-menu functions indent descriptions, these functions
499 ; are useful primarily for indenting a single menu specially.
501 (defun texinfo-indent-menu-description (column &optional region-p)
502 "Indent every description in menu following point to COLUMN.
503 Non-nil argument (prefix, if interactive) means indent every
504 description in every menu in the region. Does not indent second and
505 subsequent lines of a multi-line description."
507 (interactive
508 "nIndent menu descriptions to (column number): \nP")
509 (save-excursion
510 (save-restriction
511 (widen)
512 (if (not region-p)
513 (progn
514 (re-search-forward "^@menu")
515 (texinfo-menu-indent-description column)
516 (message
517 "Indented descriptions in menu. You may save the buffer."))
518 ;;else
519 (message "Indenting every menu description in region... ")
520 (goto-char (region-beginning))
521 (while (and (< (point) (region-end))
522 (texinfo-locate-menu-p))
523 (forward-line 1)
524 (texinfo-menu-indent-description column))
525 (message "Indenting done. You may save the buffer.")))))
527 (defun texinfo-menu-indent-description (to-column-number)
528 "Indent the Texinfo file menu description to TO-COLUMN-NUMBER.
529 Start with point just after the word `menu' in the `@menu' line and
530 leave point on the line before the `@end menu' line. Does not indent
531 second and subsequent lines of a multi-line description."
532 (let* ((beginning-of-next-line (point)))
533 (while (< beginning-of-next-line
534 (save-excursion ; beginning of end menu line
535 (goto-char (texinfo-menu-end))
536 (beginning-of-line)
537 (point)))
538 (if (search-forward "::" (texinfo-menu-end) t)
539 (progn
540 (let ((beginning-white-space (point)))
541 (skip-chars-forward " \t") ; skip over spaces
542 (if (looking-at "\\(@\\|\\w\\)+") ; if there is text
543 (progn
544 ;; remove pre-existing indentation
545 (delete-region beginning-white-space (point))
546 (indent-to-column to-column-number))))))
547 ;; position point at beginning of next line
548 (forward-line 1)
549 (setq beginning-of-next-line (point)))))
552 ;;;; Making the master menu
554 (defun texinfo-master-menu (update-all-nodes-menus-p)
555 "Make a master menu for a whole Texinfo file.
556 Non-nil argument (prefix, if interactive) means first update all
557 existing nodes and menus. Remove pre-existing master menu, if there is one.
559 This function creates a master menu that follows the top node. The
560 master menu includes every entry from all the other menus. It
561 replaces any existing ordinary menu that follows the top node.
563 If called with a non-nil argument, this function first updates all the
564 menus in the buffer (incorporating descriptions from pre-existing
565 menus) before it constructs the master menu.
567 The function removes the detailed part of an already existing master
568 menu. This action depends on the pre-exisitng master menu using the
569 standard `texinfo-master-menu-header'.
571 The master menu has the following format, which is adapted from the
572 recommendation in the Texinfo Manual:
574 * The first part contains the major nodes in the Texinfo file: the
575 nodes for the chapters, chapter-like sections, and the major
576 appendices. This includes the indices, so long as they are in
577 chapter-like sections, such as unnumbered sections.
579 * The second and subsequent parts contain a listing of the other,
580 lower level menus, in order. This way, an inquirer can go
581 directly to a particular node if he or she is searching for
582 specific information.
584 Each of the menus in the detailed node listing is introduced by the
585 title of the section containing the menu."
587 (interactive "P")
588 (widen)
589 (goto-char (point-min))
591 ;; Move point to location after `top'.
592 (if (not (re-search-forward "^@node [ \t]*top" nil t))
593 (error "This buffer needs a Top node!"))
595 (let ((first-chapter
596 (save-excursion (re-search-forward "^@node") (point))))
597 (if (re-search-forward texinfo-master-menu-header first-chapter t)
598 ;; Remove detailed master menu listing
599 (progn
600 (goto-char (match-beginning 0))
601 (let ((end-of-detailed-menu-descriptions
602 (save-excursion ; beginning of end menu line
603 (goto-char (texinfo-menu-end))
604 (beginning-of-line) (forward-char -1)
605 (point))))
606 (delete-region (point) end-of-detailed-menu-descriptions)))))
608 (if update-all-nodes-menus-p
609 (progn
610 (message "Making a master menu...first updating all nodes... ")
611 (sleep-for 2)
612 (mark-whole-buffer)
613 (texinfo-update-node t)
615 (message "Updating all menus... ")
616 (sleep-for 2)
617 (mark-whole-buffer)
618 (texinfo-make-menu t)))
620 (message "Now making the master menu... ")
621 (sleep-for 2)
622 (goto-char (point-min))
623 (texinfo-insert-master-menu-list
624 (texinfo-master-menu-list))
626 ;; Remove extra newlines that texinfo-insert-master-menu-list
627 ;; may have inserted.
629 (save-excursion
630 (goto-char (point-min))
632 (re-search-forward texinfo-master-menu-header)
633 (goto-char (match-beginning 0))
634 (insert "\n")
635 (delete-blank-lines)
637 (re-search-backward "^@menu")
638 (forward-line -1)
639 (delete-blank-lines)
641 (re-search-forward "^@end menu")
642 (forward-line 1)
643 (delete-blank-lines))
645 (message "Done...completed making master menu. You may save the buffer."))
647 (defun texinfo-master-menu-list ()
648 "Return a list of menu entries and header lines for the master menu.
650 Start with the menu for chapters and indices and then find each
651 following menu and the title of the node preceding that menu.
653 The master menu list has this form:
655 \(\(\(... \"entry-1-2\" \"entry-1\"\) \"title-1\"\)
656 \(\(... \"entry-2-2\" \"entry-2-1\"\) \"title-2\"\)
657 ...\)
659 However, there does not need to be a title field."
661 (let (master-menu-list)
662 (while (texinfo-locate-menu-p)
663 (setq master-menu-list
664 (cons (list
665 (texinfo-copy-menu)
666 (texinfo-copy-menu-title))
667 master-menu-list)))
668 (reverse master-menu-list)))
670 (defun texinfo-insert-master-menu-list (master-menu-list)
671 "Format and insert the master menu in the current buffer."
672 (goto-char (point-min))
673 (re-search-forward "^@menu")
674 (beginning-of-line)
675 (delete-region (point) ; buffer must have ordinary top menu
676 (save-excursion
677 (re-search-forward "^@end menu")
678 (point)))
680 (save-excursion ; leave point at beginning of menu
681 ;; Handle top of menu
682 (insert "\n@menu\n")
683 ;; Insert chapter menu entries
684 (setq this-very-menu-list (reverse (car (car master-menu-list))))
685 ;;; Tell user what is going on.
686 (message "Inserting chapter menu entry: %s ... " this-very-menu-list)
687 (while this-very-menu-list
688 (insert "* " (car this-very-menu-list) "\n")
689 (setq this-very-menu-list (cdr this-very-menu-list)))
691 (setq master-menu-list (cdr master-menu-list))
693 (insert texinfo-master-menu-header)
695 ;; Now, insert all the other menus
697 ;; The menu master-menu-list has a form like this:
698 ;; ((("beta" "alpha") "title-A")
699 ;; (("delta" "gamma") "title-B"))
701 (while master-menu-list
703 (message
704 "Inserting menu for %s .... " (car (cdr (car master-menu-list))))
705 ;; insert title of menu section
706 (insert "\n" (car (cdr (car master-menu-list))) "\n\n")
708 ;; insert each menu entry
709 (setq this-very-menu-list (reverse (car (car master-menu-list))))
710 (while this-very-menu-list
711 (insert "* " (car this-very-menu-list) "\n")
712 (setq this-very-menu-list (cdr this-very-menu-list)))
714 (setq master-menu-list (cdr master-menu-list)))
716 ;; Finish menu
717 (insert "@end menu\n\n")))
719 (defvar texinfo-master-menu-header
720 "\n --- The Detailed Node Listing ---\n"
721 "String inserted before lower level entries in Texinfo master menu.
722 It comes after the chapter-level menu entries.")
724 (defun texinfo-locate-menu-p ()
725 "Find the next menu in the texinfo file.
726 If found, leave point after word `menu' on the `@menu' line, and return t.
727 If a menu is not found, do not move point and return nil."
728 (re-search-forward "\\(^@menu\\)" nil t))
730 (defun texinfo-copy-menu-title ()
731 "Return the title of the section preceding the menu as a string.
732 If such a title cannot be found, return an empty string. Do not move
733 point."
734 (save-excursion
735 (if (re-search-backward
736 (concat
737 "\\(^@node\\).*\n" ; match node line
738 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
739 "\\|" ; or
740 "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
741 (eval
742 (cdr
743 (assoc (texinfo-hierarchic-level)
744 texinfo-update-menu-higher-regexps))))
747 (texinfo-copy-section-title)
748 " ")))
750 (defun texinfo-copy-menu ()
751 "Return the entries of an existing menu as a list.
752 Start with point just after the word `menu' in the `@menu' line
753 and leave point on the line before the `@end menu' line."
754 (let* (this-menu-list
755 (end-of-menu (texinfo-menu-end)) ; position of end of `@end menu'
756 (last-entry (save-excursion ; position of beginning of
757 ; last `* ' entry
758 (goto-char end-of-menu)
759 (re-search-backward "^\* ") ; handle multi-line desc.
760 (point))))
761 (while (< (point) last-entry)
762 (if (re-search-forward "^\* " end-of-menu t)
763 (progn
764 (setq this-menu-list
765 (cons
766 (buffer-substring
767 (point)
768 ;; copy multi-line descriptions
769 (save-excursion
770 (re-search-forward "\\(^\* \\|^@e\\)" nil t)
771 (- (point) 3)))
772 this-menu-list)))))
773 this-menu-list))
776 ;;;; Determining the hierarchical level in the texinfo file
778 (defun texinfo-specific-section-type ()
779 "Return the specific type of next section, as a string.
780 For example, \"unnumberedsubsec\". Return \"top\" for top node.
782 Searches forward for a section. Hence, point must be before the
783 section whose type will be found. Does not move point. Signal an
784 error if the node is not the top node and a section is not found."
785 (save-excursion
786 (cond
787 ((re-search-forward "^@node [ \t]*top" nil t)
788 "top")
789 ((re-search-forward texinfo-section-types-regexp nil t)
790 (buffer-substring (progn (beginning-of-line) ; copy its name
791 (1+ (point)))
792 (progn (forward-word 1)
793 (point))))
795 (error
796 "texinfo-specific-section-type: Chapter or section not found.")))))
798 (defun texinfo-hierarchic-level ()
799 "Return the general hierarchal level of the next node in a texinfo file.
800 Thus, a subheading or appendixsubsec is of type subsection."
801 (cdr (assoc
802 (texinfo-specific-section-type)
803 texinfo-section-to-generic-alist)))
806 ;;;; Locating the major positions
808 (defun texinfo-update-menu-region-beginning (level)
809 "Locate beginning of higher level section this section is within.
810 Return position of the beginning of the node line; do not move point.
811 Thus, if this level is subsection, searches backwards for section node.
812 Only argument is a string of the general type of section."
814 (cond
815 ((string-equal "top" level)
816 (save-excursion
817 (re-search-forward "^@node [ \t]*top" nil t) (point)))
818 ((string-equal "chapter" level)
819 (save-excursion
820 (re-search-backward "^@node [ \t]*top" nil t)
821 ;; Leave point at end of line so texinfo-menu-locate-entry-p does not
822 ;; accidentally copy an info-only title for the top node into
823 ;; the main or master menu
824 (end-of-line)
825 (point)))
827 (save-excursion
828 (re-search-backward
829 (concat
830 "\\(^@node\\).*\n" ; match node line
831 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
832 "\\|" ; or
833 "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
834 (eval
835 (cdr (assoc level texinfo-update-menu-higher-regexps))))
837 'goto-beginning)
838 (point)))))
840 (defun texinfo-update-menu-region-end (level)
841 "Locate end of higher level section this section is within.
842 Return position; do not move point. Thus, if this level is a
843 subsection, find the node for the section this subsection is within.
844 If level is top or chapter, returns end of file. Only argument is a
845 string of the general type of section."
847 (save-excursion
848 (if (re-search-forward
849 (concat
850 "\\(^@node\\).*\n" ; match node line
851 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
852 "\\|" ; or
853 "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
854 (eval
855 (cdr (assoc level texinfo-update-menu-higher-regexps))))
857 'goto-end)
858 (match-beginning 1)
859 (point-max))))
861 (defun texinfo-menu-first-node (beginning end)
862 "Locate first node of the section the menu will be placed in.
863 Return position; do not move point.
864 The menu will be located just before this position.
866 First argument is the position of the beginning of the section in
867 which the menu will be located; second argument is the position of the
868 end of that region; it limits the search."
870 (save-excursion
871 (goto-char beginning)
872 (forward-line 1)
873 (re-search-forward "^@node" end t)
874 (beginning-of-line)
875 (point)))
878 ;;;; Alists and regular expressions for defining hierarchical levels
880 (defvar texinfo-section-to-generic-alist
881 '(("top" . "top")
883 ("chapter" . "chapter")
884 ("unnumbered" . "chapter")
885 ("majorheading" . "chapter")
886 ("chapheading" . "chapter")
887 ("appendix" . "chapter")
889 ("section" . "section")
890 ("unnumberedsec" . "section")
891 ("heading" . "section")
892 ("appendixsec" . "section")
894 ("subsection" . "subsection")
895 ("unnumberedsubsec" . "subsection")
896 ("subheading" . "subsection")
897 ("appendixsubsec" . "subsection")
899 ("subsubsection" . "subsubsection")
900 ("unnumberedsubsubsec" . "subsubsection")
901 ("subsubheading" . "subsubsection")
902 ("appendixsubsubsec" . "subsubsection"))
903 "*An alist of specific and corresponding generic Texinfo section types.
904 The keys are strings specifying specific types of section; the values
905 are strings of their corresponding general types.")
907 (defvar texinfo-section-types-regexp
908 "^@\\(chapter \\|sect\\|sub\\|unnum\\|major\\|chapheading \\|heading \\|appendix\\)"
909 "Regexp matching chapter, section, other headings (but not the top node).")
911 (defvar texinfo-chapter-level-regexp
912 "chapter\\|unnumbered \\|appendix \\|majorheading\\|chapheading"
913 "Regular expression matching just the Texinfo chapter level headings.")
915 (defvar texinfo-section-level-regexp
916 "section\\|unnumberedsec\\|heading \\|appendixsec"
917 "Regular expression matching just the Texinfo section level headings.")
919 (defvar texinfo-subsection-level-regexp
920 "subsection\\|unnumberedsubsec\\|subheading\\|appendixsubsec"
921 "Regular expression matching just the Texinfo subsection level headings.")
923 (defvar texinfo-subsubsection-level-regexp
924 "subsubsection\\|unnumberedsubsubsec\\|subsubheading\\|appendixsubsubsec"
925 "Regular expression matching just the Texinfo subsubsection level headings.")
927 (defvar texinfo-update-menu-same-level-regexps
928 '(("top" . "top[ \t]+")
929 ("chapter" .
930 (concat "\\(^@\\)\\(" texinfo-chapter-level-regexp "\\)[ \t]*"))
931 ("section" .
932 (concat "\\(^@\\)\\(" texinfo-section-level-regexp "\\)[ \t]*"))
933 ("subsection" .
934 (concat "\\(^@\\)\\(" texinfo-subsection-level-regexp "\\)[ \t]+"))
935 ("subsubsection" .
936 (concat "\\(^@\\)\\(" texinfo-subsubsection-level-regexp "\\)[ \t]+")))
937 "*Regexps for searching for same level sections in a Texinfo file.
938 The keys are strings specifying the general hierarchical level in the
939 document; the values are regular expressions.")
941 (defvar texinfo-update-menu-higher-regexps
942 '(("top" . "^@node [ \t]*DIR")
943 ("chapter" . "^@node [ \t]*top")
944 ("section" .
945 (concat
946 "\\(^@\\("
947 texinfo-chapter-level-regexp
948 "\\)[ \t]*\\)"))
949 ("subsection" .
950 (concat
951 "\\(^@\\("
952 texinfo-section-level-regexp
953 "\\|"
954 texinfo-chapter-level-regexp
955 "\\)[ \t]*\\)"))
956 ("subsubsection" .
957 (concat
958 "\\(^@\\("
959 texinfo-subsection-level-regexp
960 "\\|"
961 texinfo-section-level-regexp
962 "\\|"
963 texinfo-chapter-level-regexp
964 "\\)[ \t]*\\)")))
965 "*Regexps for searching for higher level sections in a Texinfo file.
966 The keys are strings specifying the general hierarchical level in the
967 document; the values are regular expressions.")
969 (defvar texinfo-update-menu-lower-regexps
970 '(("top" .
971 (concat
972 "\\(^@\\("
973 texinfo-chapter-level-regexp
974 "\\|"
975 texinfo-section-level-regexp
976 "\\|"
977 texinfo-subsection-level-regexp
978 "\\|"
979 texinfo-subsubsection-level-regexp
980 "\\)[ \t]*\\)"))
981 ("chapter" .
982 (concat
983 "\\(^@\\("
984 texinfo-section-level-regexp
985 "\\|"
986 texinfo-subsection-level-regexp
987 "\\|"
988 texinfo-subsubsection-level-regexp
989 "\\)[ \t]*\\)"))
990 ("section" .
991 (concat
992 "\\(^@\\("
993 texinfo-subsection-level-regexp
994 "\\|"
995 texinfo-subsubsection-level-regexp
996 "\\)[ \t]+\\)"))
997 ("subsection" .
998 (concat
999 "\\(^@\\("
1000 texinfo-subsubsection-level-regexp
1001 "\\)[ \t]+\\)"))
1002 ("subsubsection" . "nothing lower"))
1003 "*Regexps for searching for lower level sections in a Texinfo file.
1004 The keys are strings specifying the general hierarchical level in the
1005 document; the values are regular expressions.")
1008 ;;;; Updating a Node
1010 (defun texinfo-update-node (&optional region-p)
1011 "Without any prefix argument, update the node in which point is located.
1012 Non-nil argument (prefix, if interactive) means update the nodes in the
1013 marked region.
1015 The functions for creating or updating nodes and menus, and their
1016 keybindings, are:
1018 texinfo-update-node (&optional region-p) \\[texinfo-update-node]
1019 texinfo-every-node-update () \\[texinfo-every-node-update]
1020 texinfo-sequential-node-update (&optional region-p)
1022 texinfo-make-menu (&optional region-p) \\[texinfo-make-menu]
1023 texinfo-all-menus-update () \\[texinfo-all-menus-update]
1024 texinfo-master-menu ()
1026 texinfo-indent-menu-description (column &optional region-p)
1028 The `texinfo-column-for-description' variable specifies the column to
1029 which menu descriptions are indented. Its default value is 24."
1031 (interactive "P")
1032 (if (not region-p)
1033 (let ((auto-fill-hook nil)) ; update a single node
1034 (if (not (re-search-backward "^@node" (point-min) t))
1035 (error "Node line not found before this position."))
1036 (texinfo-update-the-node)
1037 (message "Done...updated the node. You may save the buffer."))
1038 ;; else
1039 (let ((auto-fill-hook nil)
1040 (beginning (region-beginning))
1041 (end (region-end)))
1042 (if (= end beginning)
1043 (error "Please mark a region!"))
1044 (save-restriction
1045 (narrow-to-region beginning end)
1046 (goto-char beginning)
1047 (push-mark)
1048 (while (re-search-forward "^@node" (point-max) t)
1049 (beginning-of-line)
1050 (texinfo-update-the-node))
1051 (message "Done...updated nodes in region. You may save the buffer.")))))
1053 (defun texinfo-every-node-update ()
1054 "Update every node in a Texinfo file."
1055 (interactive)
1056 (save-excursion
1057 (mark-whole-buffer)
1058 (texinfo-update-node t)
1059 (message "Done...updated every node. You may save the buffer.")))
1061 (defun texinfo-update-the-node ()
1062 "Update one node. Point must be at the beginning of node line.
1063 Leave point at the end of the node line."
1064 (texinfo-check-for-node-name)
1065 (texinfo-delete-existing-pointers)
1066 (message "Updating node: %s ... " (texinfo-copy-node-name))
1067 (save-restriction
1068 (widen)
1069 (let*
1070 ((case-fold-search t)
1071 (level (texinfo-hierarchic-level))
1072 (beginning (texinfo-update-menu-region-beginning level))
1073 (end (texinfo-update-menu-region-end level)))
1074 (if (string-equal level "top")
1075 (texinfo-top-pointer-case)
1076 ;; else
1077 (texinfo-insert-pointer beginning end level 'next)
1078 (texinfo-insert-pointer beginning end level 'previous)
1079 (texinfo-insert-pointer beginning end level 'up)
1080 (texinfo-clean-up-node-line)))))
1082 (defun texinfo-top-pointer-case ()
1083 "Insert pointers in the Top node. This is a special case.
1085 The `Next' pointer is a pointer to a chapter or section at a lower
1086 hierarchical level in the file. The `Previous' and `Up' pointers are
1087 to `(dir)'. Point must be at the beginning of the node line, and is
1088 left at the end of the node line."
1090 (texinfo-clean-up-node-line)
1091 (insert ", "
1092 (save-excursion
1093 ;; There may be an @chapter or other such command between
1094 ;; the top node line and the next node line, as a title
1095 ;; for an `ifinfo' section. This @chapter command must
1096 ;; must be skipped. So the procedure is to search for
1097 ;; the next `@node' line, and then copy its name.
1098 (if (re-search-forward "^@node" nil t)
1099 (progn
1100 (beginning-of-line)
1101 (texinfo-copy-node-name))
1102 " "))
1103 ", (dir), (dir)"))
1105 (defun texinfo-check-for-node-name ()
1106 "Determine whether the node has a node name. Prompt for one if not.
1107 Point must be at beginning of node line. Does not move point."
1108 (save-excursion
1109 (forward-word 1) ; skip over node command
1110 (skip-chars-forward " \t") ; and over spaces
1111 (if (not (looking-at "[^,\t\n ]+")) ; regexp based on what info looks for
1112 ; alternatively, use "[a-zA-Z]+"
1113 (let ((node-name (read-from-minibuffer "Node name: ")))
1114 (insert " " node-name)))))
1116 (defun texinfo-delete-existing-pointers ()
1117 "Delete `Next', `Previous', and `Up' pointers.
1118 Starts from the current position of the cursor, and searches forward
1119 on the line for a comma and if one is found, deletes the rest of the
1120 line, including the comma. Leaves point at beginning of line."
1121 (if (search-forward "," (save-excursion (end-of-line) (point)) t)
1122 (progn
1123 (goto-char (1- (point)))
1124 (kill-line nil)))
1125 (beginning-of-line))
1127 (defun texinfo-find-pointer (beginning end level direction)
1128 "Move point to section associated with next, previous, or up pointer.
1129 Return type of pointer (either 'normal or 'no-pointer).
1131 The first and second arguments bound the search for a pointer to the
1132 beginning and end, respectively, of the enclosing higher level
1133 section. The third argument is a string specifying the general kind
1134 of section such as \"chapter\ or \"section\". When looking for the
1135 `Next' pointer, the section found will be at the same hierarchical
1136 level in the Texinfo file; when looking for the `Previous' pointer,
1137 the section found will be at the same or higher hierarchical level in
1138 the Texinfo file; when looking for the `Up' pointer, the section found
1139 will be at some level higher in the Texinfo file. The fourth argument
1140 \(one of 'next, 'previous, or 'up\) specifies whether to find the
1141 `Next', `Previous', or `Up' pointer."
1143 (cond ((eq direction 'next)
1144 (forward-line 3) ; skip over current node
1145 (if (re-search-forward
1146 (eval
1147 (cdr (assoc level texinfo-update-menu-same-level-regexps)))
1150 'normal
1151 'no-pointer))
1152 ((eq direction 'previous)
1153 (if (re-search-backward
1154 (concat
1155 "\\("
1156 (eval
1157 (cdr (assoc level texinfo-update-menu-same-level-regexps)))
1158 "\\|"
1159 (eval
1160 (cdr (assoc level texinfo-update-menu-higher-regexps)))
1161 "\\)")
1162 beginning
1164 'normal
1165 'no-pointer))
1166 ((eq direction 'up)
1167 (if (re-search-backward
1168 (eval (cdr (assoc level texinfo-update-menu-higher-regexps)))
1169 (save-excursion
1170 (goto-char beginning)
1171 (beginning-of-line)
1172 (point))
1174 'normal
1175 'no-pointer))
1177 (error "texinfo-find-pointer: lack proper arguments"))))
1179 (defun texinfo-pointer-name (kind)
1180 "Return the node name preceding the section command.
1181 The argument is the kind of section, either normal or no-pointer."
1182 (let (name)
1183 (cond ((eq kind 'normal)
1184 (end-of-line) ; this handles prev node top case
1185 (re-search-backward ; when point is already
1186 "^@node" ; at the beginning of @node line
1187 (save-excursion (forward-line -3))
1189 (setq name (texinfo-copy-node-name)))
1190 ((eq kind 'no-pointer)
1191 (setq name " "))) ; put a blank in the pointer slot
1192 name))
1194 (defun texinfo-insert-pointer (beginning end level direction)
1195 "Insert the `Next', `Previous' or `Up' node name at point.
1196 Move point forward.
1198 The first and second arguments bound the search for a pointer to the
1199 beginning and end, respectively, of the enclosing higher level
1200 section. The third argument is the hierarchical level of the Texinfo
1201 file, a string such as \"section\". The fourth argument is direction
1202 towards which the pointer is directed, one of `next, `previous, or
1203 'up."
1205 (end-of-line)
1206 (insert
1207 ", "
1208 (save-excursion
1209 (texinfo-pointer-name
1210 (texinfo-find-pointer beginning end level direction)))))
1212 (defun texinfo-clean-up-node-line ()
1213 "Remove extra commas, if any, at end of node line."
1214 (end-of-line)
1215 (skip-chars-backward ", ")
1216 (delete-region (point) (save-excursion (end-of-line) (point))))
1219 ;;;; Updating nodes sequentially
1220 ; These sequential update functions insert `Next' or `Previous'
1221 ; pointers that point to the following or preceding nodes even if they
1222 ; are at higher or lower hierarchical levels. This means that if a
1223 ; section contains one or more subsections, the section's `Next'
1224 ; pointer will point to the subsection and not the following section.
1225 ; (The subsection to which `Next' points will most likely be the first
1226 ; item on the section's menu.)
1228 (defun texinfo-sequential-node-update (&optional region-p)
1229 "Update one node (or many) in a Texinfo file with sequential pointers.
1231 This function causes the `Next' or `Previous' pointer to point to the
1232 immediately preceding or following node, even if it is at a higher or
1233 lower hierarchical level in the document. Continually pressing `n' or
1234 `p' takes you straight through the file.
1236 Without any prefix argument, update the node in which point is located.
1237 Non-nil argument (prefix, if interactive) means update the nodes in the
1238 marked region.
1240 This command makes it awkward to navigate among sections and
1241 subsections; it should be used only for those documents that are meant
1242 to be read like a novel rather than a reference, and for which the
1243 Info `g*' command is inadequate."
1245 (interactive "P")
1246 (if (not region-p)
1247 (let ((auto-fill-hook nil)) ; update a single node
1248 (if (not (re-search-backward "^@node" (point-min) t))
1249 (error "Node line not found before this position."))
1250 (texinfo-sequentially-update-the-node)
1251 (message
1252 "Done...sequentially updated the node . You may save the buffer."))
1253 ;; else
1254 (let ((auto-fill-hook nil)
1255 (beginning (region-beginning))
1256 (end (region-end)))
1257 (if (= end beginning)
1258 (error "Please mark a region!"))
1259 (save-restriction
1260 (narrow-to-region beginning end)
1261 (goto-char beginning)
1262 (push-mark)
1263 (while (re-search-forward "^@node" (point-max) t)
1264 (beginning-of-line)
1265 (texinfo-sequentially-update-the-node))
1266 (message
1267 "Done...updated the nodes in sequence. You may save the buffer.")))))
1269 (defun texinfo-sequentially-update-the-node ()
1270 "Update one node such that the pointers are sequential.
1271 A `Next' or `Previous' pointer points to any preceding or following node,
1272 regardless of its hierarchical level."
1274 (texinfo-check-for-node-name)
1275 (texinfo-delete-existing-pointers)
1276 (message
1277 "Sequentially updating node: %s ... " (texinfo-copy-node-name))
1278 (save-restriction
1279 (widen)
1280 (let*
1281 ((case-fold-search t)
1282 (level (texinfo-hierarchic-level)))
1283 (if (string-equal level "top")
1284 (texinfo-top-pointer-case)
1285 ;; else
1286 (texinfo-sequentially-insert-pointer level 'next)
1287 (texinfo-sequentially-insert-pointer level 'previous)
1288 (texinfo-sequentially-insert-pointer level 'up)
1289 (texinfo-clean-up-node-line)))))
1291 (defun texinfo-sequentially-find-pointer (level direction)
1292 "Find next or previous pointer sequentially in Texinfo file, or up pointer.
1293 Move point to section associated with the pointer. Find point even if
1294 it is in a different section.
1296 Return type of pointer (either 'normal or 'no-pointer).
1298 The first argument is a string specifying the general kind of section
1299 such as \"chapter\ or \"section\". The section found will be at the
1300 same hierarchical level in the Texinfo file, or, in the case of the up
1301 pointer, some level higher. The second argument (one of 'next,
1302 'previous, or 'up) specifies whether to find the `Next', `Previous',
1303 or `Up' pointer."
1305 (cond ((eq direction 'next)
1306 (forward-line 3) ; skip over current node
1307 (if (re-search-forward
1308 texinfo-section-types-regexp
1309 (point-max)
1311 'normal
1312 'no-pointer))
1313 ((eq direction 'previous)
1314 (if (re-search-backward
1315 texinfo-section-types-regexp
1316 (point-min)
1318 'normal
1319 'no-pointer))
1320 ((eq direction 'up)
1321 (if (re-search-backward
1322 (eval (cdr (assoc level texinfo-update-menu-higher-regexps)))
1323 beginning
1325 'normal
1326 'no-pointer))
1328 (error "texinfo-sequential-find-pointer: lack proper arguments"))))
1330 (defun texinfo-sequentially-insert-pointer (level direction)
1331 "Insert the `Next', `Previous' or `Up' node name at point.
1332 Move point forward.
1334 The first argument is the hierarchical level of the Texinfo file, a
1335 string such as \"section\". The second argument is direction, one of
1336 `next, `previous, or 'up."
1338 (end-of-line)
1339 (insert
1340 ", "
1341 (save-excursion
1342 (texinfo-pointer-name
1343 (texinfo-sequentially-find-pointer level direction)))))
1346 ;;;; Inserting `@node' lines
1347 ; The `texinfo-insert-node-lines' function inserts `@node' lines as needed
1348 ; before the `@chapter', `@section', and such like lines of a region
1349 ; in a Texinfo file.
1351 (defun texinfo-insert-node-lines (&optional title-p)
1352 "Insert missing `@node' lines in region of Texinfo file.
1353 Non-nil argument (prefix, if interactive) means also to insert the
1354 section titles as node names; and also to insert the section titles as
1355 node names in pre-existing @node lines that lack names."
1356 (interactive "P")
1357 (save-excursion
1358 (let ((begin-region (region-beginning))
1359 (end-region (region-end)))
1360 (goto-char begin-region)
1361 (while (< (point) end-region)
1362 (re-search-forward texinfo-section-types-regexp nil 'end)
1363 ;; copy title, since most often, we will need it
1364 (let ((title
1365 (progn
1366 (beginning-of-line)
1367 (forward-word 1)
1368 (skip-chars-forward " \t")
1369 (buffer-substring
1370 (point)
1371 (save-excursion (end-of-line) (point))))))
1372 ;; insert a node if necessary
1373 (if (re-search-backward
1374 "^@node"
1375 (save-excursion
1376 (forward-line -3)
1377 (point))
1379 ;; @node present, and point at beginning of that line
1380 (forward-word 1)
1381 ;; else @node missing, insert one
1382 (progn
1383 (beginning-of-line) ; beginning of `@section' line
1384 (insert "@node\n")
1385 (backward-char 1))) ; leave point just after `@node'
1386 ;; insert a title if warranted
1387 (if title-p
1388 (progn
1389 (skip-chars-forward " \t")
1390 ;; use regexp based on what info looks for
1391 ;; (alternatively, use "[a-zA-Z]+")
1392 (if (not (looking-at "[^,\t\n ]+"))
1393 (progn
1394 (beginning-of-line)
1395 (forward-word 1)
1396 (insert " " title)
1397 (message "Inserted title %s ... " title)))))
1398 ;; in any case, go forward beyond current section title
1399 (forward-line 3)))))
1400 (if title-p
1401 (message
1402 "Done inserting node lines and titles. You may save the buffer.")
1403 (message "Done inserting node lines. You may save the buffer.")))
1406 ;;;; Update and create menus for multi-file Texinfo sources
1408 ;; 1. M-x texinfo-multiple-files-update
1410 ;; Read the include file list of an outer Texinfo file and
1411 ;; update all highest level nodes in the files listed and insert a
1412 ;; main menu in the outer file after its top node.
1414 ;; 2. C-u M-x texinfo-multiple-files-update
1416 ;; Same as 1, but insert a master menu. (Saves reupdating lower
1417 ;; level menus and nodes.) This command simply reads every menu,
1418 ;; so if the menus are wrong, the master menu will be wrong.
1419 ;; Similarly, if the lower level node pointers are wrong, they
1420 ;; will stay wrong.
1422 ;; 3. C-u 2 M-x texinfo-multiple-files-update
1424 ;; Read the include file list of an outer Texinfo file and
1425 ;; update all nodes and menus in the files listed and insert a
1426 ;; master menu in the outer file after its top node.
1428 ;;; Note: these functions:
1430 ;;; * Do not save or delete any buffers. You may fill up your memory.
1431 ;;; * Do not handle any pre-existing nodes in outer file.
1432 ;;; Hence, you may need a file for indices.
1435 ;;;; Auxiliary functions for multiple file updating
1437 (defun texinfo-multi-file-included-list (outer-file)
1438 "Return a list of the included files in OUTER-FILE."
1439 (let ((included-file-list (list outer-file))
1440 start)
1441 (save-excursion
1442 (switch-to-buffer (find-file-noselect outer-file))
1443 (widen)
1444 (goto-char (point-min))
1445 (while (re-search-forward "^@include" nil t)
1446 (skip-chars-forward " \t")
1447 (setq start (point))
1448 (end-of-line)
1449 (skip-chars-backward " \t")
1450 (setq included-file-list
1451 (cons (buffer-substring start (point))
1452 included-file-list)))
1453 (nreverse included-file-list))))
1455 (defun texinfo-copy-next-section-title ()
1456 "Return the name of the immediately following section as a string.
1458 Start with point at the beginning of the node line. Leave point at the
1459 same place. If there is no title, returns an empty string."
1461 (save-excursion
1462 (end-of-line)
1463 (let ((section-end (or
1464 (save-excursion
1465 (re-search-forward "\\(^@node\\)" nil t)
1466 (match-beginning 0))
1467 (point-max))))
1468 (if (re-search-forward texinfo-section-types-regexp section-end t)
1469 ;; copy title
1470 (let ((title
1471 (buffer-substring
1472 (progn (forward-word 1) ; skip over section type
1473 (skip-chars-forward " \t") ; and over spaces
1474 (point))
1475 (progn (end-of-line) (point)))))
1476 title)
1477 ""))))
1479 (defun texinfo-multi-file-update (files &optional update-everything)
1480 "Update first node pointers in each file in FILES.
1481 Return a list of the node names and the title immediate following them.
1483 The first file in the list is an outer file; the remaining are
1484 files included in the outer file with `@include' commands.
1486 If optional arg UPDATE-EVERYTHING non-nil, update every menu and
1487 pointer in each of the included files.
1489 Also update the `Top' level node pointers of the outer file.
1491 Requirements:
1493 * the first file in the FILES list must be the outer file,
1494 * each of the included files must contain exactly one highest
1495 hierarchical level node,
1496 * this node must be the first node in the included file,
1497 * each highest hierarchical level node must be of the same type.
1499 Thus, normally, each included file contains one, and only one,
1500 chapter.
1502 The menu-list has the form:
1504 \(\(\"node-name1\" . \"title1\"\)
1505 \(\"node-name2\" . \"title2\"\) ... \)
1507 However, there does not need to be a title field."
1509 (let (menu-list)
1511 ;; Find the name of the first node of the first included file.
1512 (switch-to-buffer (find-file-noselect (car (cdr files))))
1513 (widen)
1514 (goto-char (point-min))
1515 (if (not (re-search-forward "^@node" nil t))
1516 (error "No `@node' line found in %s !" (buffer-name)))
1517 (beginning-of-line)
1518 (texinfo-check-for-node-name)
1519 (setq next-node-name (texinfo-copy-node-name))
1521 (setq menu-list
1522 (cons (cons
1523 next-node-name
1524 (texinfo-copy-next-section-title))
1525 menu-list))
1527 ;; Go to outer file
1528 (switch-to-buffer (find-file-noselect (car files)))
1529 (goto-char (point-min))
1530 (if (not (re-search-forward "^@node [ \t]*top" nil t))
1531 (error "This buffer needs a Top node!"))
1532 (beginning-of-line)
1533 (texinfo-delete-existing-pointers)
1534 (end-of-line)
1535 (insert ", " next-node-name ", (dir), (dir)")
1536 (beginning-of-line)
1537 (setq previous-node-name "Top")
1538 (setq files (cdr files))
1540 (while files
1542 (if (not (cdr files))
1543 ;; No next file
1544 (setq next-node-name "")
1545 ;; Else,
1546 ;; find the name of the first node in the next file.
1547 (switch-to-buffer (find-file-noselect (car (cdr files))))
1548 (widen)
1549 (goto-char (point-min))
1550 (if (not (re-search-forward "^@node" nil t))
1551 (error "No `@node' line found in %s !" (buffer-name)))
1552 (beginning-of-line)
1553 (texinfo-check-for-node-name)
1554 (setq next-node-name (texinfo-copy-node-name))
1555 (setq menu-list
1556 (cons (cons
1557 next-node-name
1558 (texinfo-copy-next-section-title))
1559 menu-list)))
1561 ;; Go to node to be updated.
1562 (switch-to-buffer (find-file-noselect (car files)))
1563 (goto-char (point-min))
1564 (if (not (re-search-forward "^@node" nil t))
1565 (error "No `@node' line found in %s !" (buffer-name)))
1566 (beginning-of-line)
1567 (texinfo-delete-existing-pointers)
1568 (end-of-line)
1569 (insert ", " next-node-name ", " previous-node-name ", " up-node-name)
1571 (beginning-of-line)
1572 (setq previous-node-name (texinfo-copy-node-name))
1574 ;; Update other menus and nodes if requested.
1575 (if update-everything (texinfo-all-menus-update t))
1577 (setq files (cdr files)))
1578 (nreverse menu-list)))
1580 (defun texinfo-multi-files-insert-main-menu (menu-list)
1581 "Insert formatted main menu at point.
1582 Indents the first line of the description, if any, to the value of
1583 texinfo-column-for-description."
1585 (insert "@menu\n")
1586 (while menu-list
1587 (if (cdr (car menu-list)) ; menu-list has description entry
1588 (progn
1589 (insert
1590 (format "* %s::" (car (car menu-list)))) ; node-name entry
1591 (indent-to texinfo-column-for-description 2)
1592 (insert
1593 (format "%s\n" (cdr (car menu-list))))) ; description entry
1594 ;; else menu-list lacks description entry
1595 (insert
1596 (format "* %s::\n" (car (car menu-list))))) ; node-name entry
1597 (setq menu-list (cdr menu-list)))
1598 (insert "@end menu"))
1601 (defun texinfo-multi-file-master-menu-list (files-list)
1602 "Return master menu list from files in FILES-LIST.
1603 Menu entries in each file collected using `texinfo-master-menu-list'.
1605 The first file in FILES-LIST must be the outer file; the others must
1606 be the files included within it. A main menu must already exist."
1607 (save-excursion
1608 (let (master-menu-list)
1609 (while files-list
1610 (switch-to-buffer (find-file-noselect (car files-list)))
1611 (message "Working on: %s " (current-buffer))
1612 (goto-char (point-min))
1613 (setq master-menu-list
1614 (append master-menu-list (texinfo-master-menu-list)))
1615 (setq files-list (cdr files-list)))
1616 master-menu-list)))
1619 ;;;; The multiple-file update function
1621 (defun texinfo-multiple-files-update
1622 (outer-file &optional update-everything make-master-menu)
1623 "Update first node pointers in each file included in OUTER-FILE;
1624 create or update main menu in the outer file that refers to such nodes.
1625 This does not create or update menus or pointers within the included files.
1627 With optional MAKE-MASTER-MENU argument (prefix arg, if interactive),
1628 insert a master menu in OUTER-FILE. This does not create or update
1629 menus or pointers within the included files.
1631 With optional UPDATE-EVERYTHING argument (numeric prefix arg, if
1632 interactive), update all the menus and all the `Next', `Previous', and
1633 `Up' pointers of all the files included in OUTER-FILE before inserting
1634 a master menu in OUTER-FILE.
1636 The command also updates the `Top' level node pointers of OUTER-FILE.
1638 Notes:
1640 * this command does NOT save any files--you must save the
1641 outer file and any modified, included files.
1643 * except for the `Top' node, this command does NOT handle any
1644 pre-existing nodes in the outer file; hence, indices must be
1645 enclosed in an included file.
1647 Requirements:
1649 * each of the included files must contain exactly one highest
1650 hierarchical level node,
1651 * this highest node must be the first node in the included file,
1652 * each highest hierarchical level node must be of the same type.
1654 Thus, normally, each included file contains one, and only one,
1655 chapter."
1657 (interactive "fName of outer `include' file: ")
1659 (cond (current-prefix-arg
1660 (setq make-master-menu (listp current-prefix-arg))
1661 (setq update-everything (numberp current-prefix-arg))))
1663 (let* ((included-file-list (texinfo-multi-file-included-list outer-file))
1664 (files included-file-list)
1665 main-menu-list
1666 next-node-name
1667 previous-node-name
1668 (up-node-name "Top"))
1670 ;;; Update the pointers
1671 ;;; and collect the names of the nodes and titles
1672 (setq main-menu-list (texinfo-multi-file-update files update-everything))
1674 ;;; Insert main menu
1676 ;; Go to outer file
1677 (switch-to-buffer (find-file-noselect (car included-file-list)))
1678 (if (texinfo-old-menu-p
1679 (point-min)
1680 (save-excursion
1681 (re-search-forward "^@include")
1682 (beginning-of-line)
1683 (point)))
1685 ;; If found, leave point after word `menu' on the `@menu' line.
1686 (progn
1687 (texinfo-incorporate-descriptions main-menu-list)
1688 ;; Delete existing menu.
1689 (beginning-of-line)
1690 (delete-region
1691 (point)
1692 (save-excursion (re-search-forward "^@end menu") (point)))
1693 ;; Insert main menu
1694 (texinfo-multi-files-insert-main-menu main-menu-list))
1696 ;; Else no current menu; insert it before `@include'
1697 (texinfo-multi-files-insert-main-menu main-menu-list))
1699 ;;; Insert master menu
1701 (if make-master-menu
1702 (progn
1703 ;; First, removing detailed part of any pre-existing master menu
1704 (goto-char (point-min))
1705 (if (re-search-forward texinfo-master-menu-header nil t)
1706 ;; Remove detailed master menu listing
1707 (progn
1708 (goto-char (match-beginning 0))
1709 (let ((end-of-detailed-menu-descriptions
1710 (save-excursion ; beginning of end menu line
1711 (goto-char (texinfo-menu-end))
1712 (beginning-of-line) (forward-char -1)
1713 (point))))
1714 (delete-region (point) end-of-detailed-menu-descriptions))))
1716 ;; Create a master menu and insert it
1717 (texinfo-insert-master-menu-list
1718 (texinfo-multi-file-master-menu-list
1719 included-file-list)))))
1720 (message "Multiple files updated."))
1722 ;;;;;;;;;;;;;;;; end texnfo-upd.el ;;;;;;;;;;;;;;;;