*** empty log message ***
[emacs.git] / lisp / textmodes / texnfo-upd.el
blobc236d79184661df7e54f02051ed51d2db337c5ee
1 ;;;; texnfo-upd.el
2 ;;;; A utility for updating nodes and menus in Texinfo files.
4 ;;;; Version 2.00 14 Dec 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
201 (save-excursion
202 (goto-char (texinfo-update-menu-region-beginning level))
203 (end-of-line)
204 (point)))
205 (end (texinfo-update-menu-region-end level))
206 (first (texinfo-menu-first-node beginning end))
207 (node-name (progn
208 (goto-char beginning)
209 (texinfo-copy-node-name)))
210 (new-menu-list (texinfo-make-menu-list beginning end level)))
211 (if (texinfo-old-menu-p beginning first)
212 (progn
213 (texinfo-incorporate-descriptions new-menu-list)
214 (texinfo-delete-old-menu beginning first)))
215 (texinfo-insert-menu new-menu-list node-name)))
217 (defun texinfo-all-menus-update (&optional update-all-nodes-p)
218 "Update every regular menu in a Texinfo file.
219 You must remove the detailed part of a pre-existing master menu before
220 running this command, lest it be partly duplicated.
222 If called with a non-nil argument, this function first updates all the
223 nodes in the buffer before updating the menus."
224 (interactive "P")
225 (save-excursion
226 (mark-whole-buffer)
227 (message "Checking for a master menu... ")
228 (save-excursion
229 (if (re-search-forward texinfo-master-menu-header nil t)
230 (error
231 "Please remove existing master menu, lest it be partly duplicated!")))
233 (if update-all-nodes-p
234 (progn
235 (message "First updating all nodes... ")
236 (sleep-for 2)
237 (mark-whole-buffer)
238 (texinfo-update-node t)))
240 (message "Updating all menus... ")
241 (sleep-for 2)
242 (texinfo-make-menu t)
243 (message "Done...updated all the menus. You may save the buffer.")))
245 (defun texinfo-find-lower-level-node (level region-end)
246 "Search forward from point for node at any level lower than LEVEL.
247 Search is limited to the end of the marked region, REGION-END,
248 and to the end of the menu region for the level.
250 Return t if the node is found, else nil. Leave point at the beginning
251 of the node if one is found; else do not move point."
253 (if (and (< (point) region-end)
254 (re-search-forward
255 (concat
256 "\\(^@node\\).*\n" ; match node line
257 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
258 "\\|" ; or
259 "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
260 (eval (cdr (assoc level texinfo-update-menu-lower-regexps))))
261 ;; the next higher level node marks the end of this
262 ;; section, and no lower level node will be found beyond
263 ;; this position even if region-end is farther off
264 (texinfo-update-menu-region-end level)
266 (goto-char (match-beginning 1))))
268 (defun texinfo-find-higher-level-node (level region-end)
269 "Search forward from point for node at any higher level than argument LEVEL.
270 Search is limited to the end of the marked region, REGION-END.
272 Return t if the node is found, else nil. Leave point at the beginning
273 of the node if one is found; else do not move point."
275 (if (and (< (point) region-end)
276 (re-search-forward
277 (concat
278 "\\(^@node\\).*\n" ; match node line
279 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
280 "\\|" ; or
281 "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
282 (eval ; (won't ever find a `top' node)
283 (cdr (assoc level texinfo-update-menu-higher-regexps))))
286 (goto-char (match-beginning 1))))
289 ;;;; Making the list of new menu entries
291 (defun texinfo-make-menu-list (beginning end level)
292 "Make a list of node names and their descriptions.
293 Point is left at the end of the menu region, but the menu is not inserted.
295 First argument is position from which to start making menu list;
296 second argument is end of region in which to try to locate entries;
297 third argument is the level of the nodes that are the entries.
299 Node names and descriptions are dotted pairs of strings. Each pair is
300 an element of the list. If the description does not exist, the
301 element consists only of the node name."
302 (goto-char beginning)
303 (let (new-menu-list)
304 (while (texinfo-menu-locate-entry-p level end)
305 (setq new-menu-list
306 (cons (cons
307 (texinfo-copy-node-name)
308 (texinfo-copy-section-title))
309 new-menu-list)))
310 (reverse new-menu-list)))
312 (defun texinfo-menu-locate-entry-p (level search-end)
313 "Find a node that will be part of menu for this section.
314 First argument is a string such as \"section\" specifying the general
315 hierarchical level of the menu; second argument is a postion
316 specifying the end of the search.
318 The function returns t if the node is found, else nil. It searches
319 forward from point, and leaves point at the beginning of the node.
321 The function finds entries of the same type. Thus `subsections' and
322 `unnumberedsubsecs' will appear in the same menu."
323 (if (re-search-forward
324 (concat
325 "\\(^@node\\).*\n" ; match node line
326 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
327 "\\|" ; or
328 "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
329 (eval
330 (cdr (assoc level texinfo-update-menu-same-level-regexps))))
331 search-end
333 (goto-char (match-beginning 1))))
335 (defun texinfo-copy-node-name ()
336 "Return the node name as a string.
338 Start with point at the beginning of the node line; copy the text
339 after the node command up to the first comma on the line, if any, and
340 return the text as a string. Leaves point at the beginning of the
341 line. If there is no node name, returns an empty string."
343 (save-excursion
344 (buffer-substring
345 (progn (forward-word 1) ; skip over node command
346 (skip-chars-forward " \t") ; and over spaces
347 (point))
348 (if (search-forward
350 (save-excursion (end-of-line) (point)) t) ; bound search
351 (1- (point))
352 (end-of-line) (point)))))
354 (defun texinfo-copy-section-title ()
355 "Return the title of the section as a string.
356 The title is used as a description line in the menu when one does not
357 already exist.
359 Move point to the beginning of the appropriate section line by going
360 to the start of the text matched by last regexp searched for, which
361 must have been done by `texinfo-menu-locate-entry-p'."
363 ;; could use the same re-search as in `texinfo-menu-locate-entry-p'
364 ;; instead of using `match-beginning'; such a variation would be
365 ;; more general, but would waste information already collected
367 (goto-char (match-beginning 7)) ; match section name
369 (buffer-substring
370 (progn (forward-word 1) ; skip over section type
371 (skip-chars-forward " \t") ; and over spaces
372 (point))
373 (progn (end-of-line) (point))))
376 ;;;; Handling the old menu
378 (defun texinfo-old-menu-p (beginning first)
379 "Move point to the beginning of the menu for this section, if any.
380 Otherwise move point to the end of the first node of this section.
381 Return t if a menu is found, nil otherwise.
383 First argument is the position of the beginning of the section in which
384 the menu will be located; second argument is the position of the first
385 node within the section.
387 If no menu is found, the function inserts two newlines just before the
388 end of the section, and leaves point there where a menu ought to be."
389 (goto-char beginning)
390 (if (not (re-search-forward "^@menu" first 'goto-end))
391 (progn (insert "\n\n") (forward-line -2) nil)
394 (defun texinfo-incorporate-descriptions (new-menu-list)
395 "Copy the old menu line descriptions that exist to the new menu.
397 Point must be at beginning of old menu.
399 If the node-name of the new menu entry cannot be found in the old
400 menu, use the new section title for the description, but if the
401 node-name of the new menu is found in the old menu, replace the
402 section title with the old description, whatever it may be.
404 For this function, the new menu is a list made up of lists of dotted
405 pairs in which the first element of the pair is the node name and the
406 second element the description. The new menu is changed destructively.
407 The old menu is the menu as it appears in the texinfo file."
409 (let ((new-menu-list-pointer new-menu-list)
410 (end-of-menu (texinfo-menu-end)))
411 (while new-menu-list
412 (save-excursion ; keep point at beginning of menu
413 (if (search-forward
414 (concat "\* " ; so only menu entries are found
415 (car (car new-menu-list))
416 ":") ; so only complete entries are found
417 end-of-menu
419 (setcdr (car new-menu-list)
420 (texinfo-menu-copy-old-description end-of-menu))))
421 (setq new-menu-list (cdr new-menu-list)))
422 (setq new-menu-list new-menu-list-pointer)))
424 (defun texinfo-menu-copy-old-description (end-of-menu)
425 "Return description field of old menu line as string.
426 Point must be located just after the node name. Point left before description.
427 Single argument, END-OF-MENU, is position limiting search."
428 (skip-chars-forward "[:.,\t\n ]+")
429 ;; don't copy a carriage return at line beginning with asterisk!
430 ;; do copy a description that begins with an `@'!
431 (if (and (looking-at "\\(\\w+\\|@\\)")
432 (not (looking-at "\\(^\\* \\|^@end menu\\)")))
433 (buffer-substring
434 (point)
435 (save-excursion
436 (re-search-forward "\\(^\\* \\|^@end menu\\)" end-of-menu t)
437 (forward-line -1)
438 (end-of-line) ; go to end of last description line
439 (point)))
440 ""))
442 (defun texinfo-menu-end ()
443 "Return position of end of menu. Does not change location of point.
444 Signal an error if not end of menu."
445 (save-excursion
446 (if (re-search-forward "^@end menu" nil t)
447 (point)
448 (error "Menu does not have an end."))))
450 (defun texinfo-delete-old-menu (beginning first)
451 "Delete the old menu. Point must be in or after menu.
452 First argument is position of the beginning of the section in which
453 the menu will be located; second argument is the position of the first
454 node within the section."
455 ;; No third arg to search, so error if search fails.
456 (re-search-backward "^@menu" beginning)
457 (delete-region (point)
458 (save-excursion
459 (re-search-forward "^@end menu" first)
460 (point))))
463 ;;;; Inserting new menu
465 ;; try 32, but perhaps 24 is better
466 (defvar texinfo-column-for-description 32
467 "*Column at which descriptions start in a Texinfo menu.")
469 (defun texinfo-insert-menu (menu-list node-name)
470 "Insert formatted menu at point.
471 Indents the first line of the description, if any, to the value of
472 texinfo-column-for-description.
474 MENU-LIST has form:
476 \(\(\"node-name1\" . \"description\"\)
477 \(\"node-name\" . \"description\"\) ... \)
479 However, there does not need to be a description field."
481 (insert "@menu\n")
482 (while menu-list
483 (if (cdr (car menu-list)) ; menu-list has description entry
484 (progn
485 (insert
486 (format "* %s::" (car (car menu-list)))) ; node-name entry
487 (indent-to texinfo-column-for-description 2)
488 (insert
489 (format "%s\n" (cdr (car menu-list))))) ; description entry
490 ;; else menu-list lacks description entry
491 (insert
492 (format "* %s::\n" (car (car menu-list))))) ; node-name entry
493 (setq menu-list (cdr menu-list)))
494 (insert "@end menu")
495 (message
496 "Updated \"%s\" level menu following node: %s ... "
497 level node-name))
500 ;;;; Handling description indentation
502 ; Since the make-menu functions indent descriptions, these functions
503 ; are useful primarily for indenting a single menu specially.
505 (defun texinfo-indent-menu-description (column &optional region-p)
506 "Indent every description in menu following point to COLUMN.
507 Non-nil argument (prefix, if interactive) means indent every
508 description in every menu in the region. Does not indent second and
509 subsequent lines of a multi-line description."
511 (interactive
512 "nIndent menu descriptions to (column number): \nP")
513 (save-excursion
514 (save-restriction
515 (widen)
516 (if (not region-p)
517 (progn
518 (re-search-forward "^@menu")
519 (texinfo-menu-indent-description column)
520 (message
521 "Indented descriptions in menu. You may save the buffer."))
522 ;;else
523 (message "Indenting every menu description in region... ")
524 (goto-char (region-beginning))
525 (while (and (< (point) (region-end))
526 (texinfo-locate-menu-p))
527 (forward-line 1)
528 (texinfo-menu-indent-description column))
529 (message "Indenting done. You may save the buffer.")))))
531 (defun texinfo-menu-indent-description (to-column-number)
532 "Indent the Texinfo file menu description to TO-COLUMN-NUMBER.
533 Start with point just after the word `menu' in the `@menu' line and
534 leave point on the line before the `@end menu' line. Does not indent
535 second and subsequent lines of a multi-line description."
536 (let* ((beginning-of-next-line (point)))
537 (while (< beginning-of-next-line
538 (save-excursion ; beginning of end menu line
539 (goto-char (texinfo-menu-end))
540 (beginning-of-line)
541 (point)))
542 (if (search-forward "::" (texinfo-menu-end) t)
543 (progn
544 (let ((beginning-white-space (point)))
545 (skip-chars-forward " \t") ; skip over spaces
546 (if (looking-at "\\(@\\|\\w\\)+") ; if there is text
547 (progn
548 ;; remove pre-existing indentation
549 (delete-region beginning-white-space (point))
550 (indent-to-column to-column-number))))))
551 ;; position point at beginning of next line
552 (forward-line 1)
553 (setq beginning-of-next-line (point)))))
556 ;;;; Making the master menu
558 (defun texinfo-master-menu (update-all-nodes-menus-p)
559 "Make a master menu for a whole Texinfo file.
560 Non-nil argument (prefix, if interactive) means first update all
561 existing nodes and menus. Remove pre-existing master menu, if there is one.
563 This function creates a master menu that follows the top node. The
564 master menu includes every entry from all the other menus. It
565 replaces any existing ordinary menu that follows the top node.
567 If called with a non-nil argument, this function first updates all the
568 menus in the buffer (incorporating descriptions from pre-existing
569 menus) before it constructs the master menu.
571 The function removes the detailed part of an already existing master
572 menu. This action depends on the pre-exisitng master menu using the
573 standard `texinfo-master-menu-header'.
575 The master menu has the following format, which is adapted from the
576 recommendation in the Texinfo Manual:
578 * The first part contains the major nodes in the Texinfo file: the
579 nodes for the chapters, chapter-like sections, and the major
580 appendices. This includes the indices, so long as they are in
581 chapter-like sections, such as unnumbered sections.
583 * The second and subsequent parts contain a listing of the other,
584 lower level menus, in order. This way, an inquirer can go
585 directly to a particular node if he or she is searching for
586 specific information.
588 Each of the menus in the detailed node listing is introduced by the
589 title of the section containing the menu."
591 (interactive "P")
592 (widen)
593 (goto-char (point-min))
595 ;; Move point to location after `top'.
596 (if (not (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t))
597 (error "This buffer needs a Top node!"))
599 (let ((first-chapter
600 (save-excursion (re-search-forward "^@node") (point))))
601 (if (re-search-forward texinfo-master-menu-header first-chapter t)
602 ;; Remove detailed master menu listing
603 (progn
604 (goto-char (match-beginning 0))
605 (let ((end-of-detailed-menu-descriptions
606 (save-excursion ; beginning of end menu line
607 (goto-char (texinfo-menu-end))
608 (beginning-of-line) (forward-char -1)
609 (point))))
610 (delete-region (point) end-of-detailed-menu-descriptions)))))
612 (if update-all-nodes-menus-p
613 (progn
614 (message "Making a master menu...first updating all nodes... ")
615 (sleep-for 2)
616 (mark-whole-buffer)
617 (texinfo-update-node t)
619 (message "Updating all menus... ")
620 (sleep-for 2)
621 (mark-whole-buffer)
622 (texinfo-make-menu t)))
624 (message "Now making the master menu... ")
625 (sleep-for 2)
626 (goto-char (point-min))
627 (texinfo-insert-master-menu-list
628 (texinfo-master-menu-list))
630 ;; Remove extra newlines that texinfo-insert-master-menu-list
631 ;; may have inserted.
633 (save-excursion
634 (goto-char (point-min))
636 (re-search-forward texinfo-master-menu-header)
637 (goto-char (match-beginning 0))
638 (insert "\n")
639 (delete-blank-lines)
641 (re-search-backward "^@menu")
642 (forward-line -1)
643 (delete-blank-lines)
645 (re-search-forward "^@end menu")
646 (forward-line 1)
647 (delete-blank-lines))
649 (message "Done...completed making master menu. You may save the buffer."))
651 (defun texinfo-master-menu-list ()
652 "Return a list of menu entries and header lines for the master menu.
654 Start with the menu for chapters and indices and then find each
655 following menu and the title of the node preceding that menu.
657 The master menu list has this form:
659 \(\(\(... \"entry-1-2\" \"entry-1\"\) \"title-1\"\)
660 \(\(... \"entry-2-2\" \"entry-2-1\"\) \"title-2\"\)
661 ...\)
663 However, there does not need to be a title field."
665 (let (master-menu-list)
666 (while (texinfo-locate-menu-p)
667 (setq master-menu-list
668 (cons (list
669 (texinfo-copy-menu)
670 (texinfo-copy-menu-title))
671 master-menu-list)))
672 (reverse master-menu-list)))
674 (defun texinfo-insert-master-menu-list (master-menu-list)
675 "Format and insert the master menu in the current buffer."
676 (goto-char (point-min))
677 (re-search-forward "^@menu")
678 (beginning-of-line)
679 (delete-region (point) ; buffer must have ordinary top menu
680 (save-excursion
681 (re-search-forward "^@end menu")
682 (point)))
684 (save-excursion ; leave point at beginning of menu
685 ;; Handle top of menu
686 (insert "\n@menu\n")
687 ;; Insert chapter menu entries
688 (setq this-very-menu-list (reverse (car (car master-menu-list))))
689 ;;; Tell user what is going on.
690 (message "Inserting chapter menu entry: %s ... " this-very-menu-list)
691 (while this-very-menu-list
692 (insert "* " (car this-very-menu-list) "\n")
693 (setq this-very-menu-list (cdr this-very-menu-list)))
695 (setq master-menu-list (cdr master-menu-list))
697 (insert texinfo-master-menu-header)
699 ;; Now, insert all the other menus
701 ;; The menu master-menu-list has a form like this:
702 ;; ((("beta" "alpha") "title-A")
703 ;; (("delta" "gamma") "title-B"))
705 (while master-menu-list
707 (message
708 "Inserting menu for %s .... " (car (cdr (car master-menu-list))))
709 ;; insert title of menu section
710 (insert "\n" (car (cdr (car master-menu-list))) "\n\n")
712 ;; insert each menu entry
713 (setq this-very-menu-list (reverse (car (car master-menu-list))))
714 (while this-very-menu-list
715 (insert "* " (car this-very-menu-list) "\n")
716 (setq this-very-menu-list (cdr this-very-menu-list)))
718 (setq master-menu-list (cdr master-menu-list)))
720 ;; Finish menu
721 (insert "@end menu\n\n")))
723 (defvar texinfo-master-menu-header
724 "\n --- The Detailed Node Listing ---\n"
725 "String inserted before lower level entries in Texinfo master menu.
726 It comes after the chapter-level menu entries.")
728 (defun texinfo-locate-menu-p ()
729 "Find the next menu in the texinfo file.
730 If found, leave point after word `menu' on the `@menu' line, and return t.
731 If a menu is not found, do not move point and return nil."
732 (re-search-forward "\\(^@menu\\)" nil t))
734 (defun texinfo-copy-menu-title ()
735 "Return the title of the section preceding the menu as a string.
736 If such a title cannot be found, return an empty string. Do not move
737 point."
738 (save-excursion
739 (if (re-search-backward
740 (concat
741 "\\(^@node\\).*\n" ; match node line
742 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
743 "\\|" ; or
744 "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
745 (eval
746 (cdr
747 (assoc (texinfo-hierarchic-level)
748 texinfo-update-menu-higher-regexps))))
751 (texinfo-copy-section-title)
752 " ")))
754 (defun texinfo-copy-menu ()
755 "Return the entries of an existing menu as a list.
756 Start with point just after the word `menu' in the `@menu' line
757 and leave point on the line before the `@end menu' line."
758 (let* (this-menu-list
759 (end-of-menu (texinfo-menu-end)) ; position of end of `@end menu'
760 (last-entry (save-excursion ; position of beginning of
761 ; last `* ' entry
762 (goto-char end-of-menu)
763 (re-search-backward "^\* ") ; handle multi-line desc.
764 (point))))
765 (while (< (point) last-entry)
766 (if (re-search-forward "^\* " end-of-menu t)
767 (progn
768 (setq this-menu-list
769 (cons
770 (buffer-substring
771 (point)
772 ;; copy multi-line descriptions
773 (save-excursion
774 (re-search-forward "\\(^\* \\|^@e\\)" nil t)
775 (- (point) 3)))
776 this-menu-list)))))
777 this-menu-list))
780 ;;;; Determining the hierarchical level in the texinfo file
782 (defun texinfo-specific-section-type ()
783 "Return the specific type of next section, as a string.
784 For example, \"unnumberedsubsec\". Return \"top\" for top node.
786 Searches forward for a section. Hence, point must be before the
787 section whose type will be found. Does not move point. Signal an
788 error if the node is not the top node and a section is not found."
789 (save-excursion
790 (cond
791 ((re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)"
792 (save-excursion
793 (end-of-line)
794 (point))
796 "top")
797 ((re-search-forward texinfo-section-types-regexp nil t)
798 (buffer-substring (progn (beginning-of-line) ; copy its name
799 (1+ (point)))
800 (progn (forward-word 1)
801 (point))))
803 (error
804 "texinfo-specific-section-type: Chapter or section not found.")))))
806 (defun texinfo-hierarchic-level ()
807 "Return the general hierarchal level of the next node in a texinfo file.
808 Thus, a subheading or appendixsubsec is of type subsection."
809 (cdr (assoc
810 (texinfo-specific-section-type)
811 texinfo-section-to-generic-alist)))
814 ;;;; Locating the major positions
816 (defun texinfo-update-menu-region-beginning (level)
817 "Locate beginning of higher level section this section is within.
818 Return position of the beginning of the node line; do not move point.
819 Thus, if this level is subsection, searches backwards for section node.
820 Only argument is a string of the general type of section."
822 (cond
823 ((or (string-equal "top" level)
824 (string-equal "chapter" level))
825 (save-excursion
826 (goto-char (point-min))
827 (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t)
828 (beginning-of-line)
829 (point)))
831 (save-excursion
832 (re-search-backward
833 (concat
834 "\\(^@node\\).*\n" ; match node line
835 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
836 "\\|" ; or
837 "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
838 (eval
839 (cdr (assoc level texinfo-update-menu-higher-regexps))))
841 'goto-beginning)
842 (point)))))
844 (defun texinfo-update-menu-region-end (level)
845 "Locate end of higher level section this section is within.
846 Return position; do not move point. Thus, if this level is a
847 subsection, find the node for the section this subsection is within.
848 If level is top or chapter, returns end of file. Only argument is a
849 string of the general type of section."
851 (save-excursion
852 (if (re-search-forward
853 (concat
854 "\\(^@node\\).*\n" ; match node line
855 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
856 "\\|" ; or
857 "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
858 (eval
859 (cdr (assoc level texinfo-update-menu-higher-regexps))))
861 'goto-end)
862 (match-beginning 1)
863 (point-max))))
865 (defun texinfo-menu-first-node (beginning end)
866 "Locate first node of the section the menu will be placed in.
867 Return position; do not move point.
868 The menu will be located just before this position.
870 First argument is the position of the beginning of the section in
871 which the menu will be located; second argument is the position of the
872 end of that region; it limits the search."
874 (save-excursion
875 (goto-char beginning)
876 (forward-line 1)
877 (re-search-forward "^@node" end t)
878 (beginning-of-line)
879 (point)))
882 ;;;; Alists and regular expressions for defining hierarchical levels
884 (defvar texinfo-section-to-generic-alist
885 '(("top" . "top")
887 ("chapter" . "chapter")
888 ("unnumbered" . "chapter")
889 ("majorheading" . "chapter")
890 ("chapheading" . "chapter")
891 ("appendix" . "chapter")
893 ("section" . "section")
894 ("unnumberedsec" . "section")
895 ("heading" . "section")
896 ("appendixsec" . "section")
898 ("subsection" . "subsection")
899 ("unnumberedsubsec" . "subsection")
900 ("subheading" . "subsection")
901 ("appendixsubsec" . "subsection")
903 ("subsubsection" . "subsubsection")
904 ("unnumberedsubsubsec" . "subsubsection")
905 ("subsubheading" . "subsubsection")
906 ("appendixsubsubsec" . "subsubsection"))
907 "*An alist of specific and corresponding generic Texinfo section types.
908 The keys are strings specifying specific types of section; the values
909 are strings of their corresponding general types.")
911 (defvar texinfo-section-types-regexp
912 "^@\\(chapter \\|sect\\|sub\\|unnum\\|major\\|chapheading \\|heading \\|appendix\\)"
913 "Regexp matching chapter, section, other headings (but not the top node).")
915 (defvar texinfo-chapter-level-regexp
916 "chapter\\|unnumbered \\|appendix \\|majorheading\\|chapheading"
917 "Regular expression matching just the Texinfo chapter level headings.")
919 (defvar texinfo-section-level-regexp
920 "section\\|unnumberedsec\\|heading \\|appendixsec"
921 "Regular expression matching just the Texinfo section level headings.")
923 (defvar texinfo-subsection-level-regexp
924 "subsection\\|unnumberedsubsec\\|subheading\\|appendixsubsec"
925 "Regular expression matching just the Texinfo subsection level headings.")
927 (defvar texinfo-subsubsection-level-regexp
928 "subsubsection\\|unnumberedsubsubsec\\|subsubheading\\|appendixsubsubsec"
929 "Regular expression matching just the Texinfo subsubsection level headings.")
931 (defvar texinfo-update-menu-same-level-regexps
932 '(("top" . "top[ \t]+")
933 ("chapter" .
934 (concat "\\(^@\\)\\(" texinfo-chapter-level-regexp "\\)[ \t]*"))
935 ("section" .
936 (concat "\\(^@\\)\\(" texinfo-section-level-regexp "\\)[ \t]*"))
937 ("subsection" .
938 (concat "\\(^@\\)\\(" texinfo-subsection-level-regexp "\\)[ \t]+"))
939 ("subsubsection" .
940 (concat "\\(^@\\)\\(" texinfo-subsubsection-level-regexp "\\)[ \t]+")))
941 "*Regexps for searching for same level sections in a Texinfo file.
942 The keys are strings specifying the general hierarchical level in the
943 document; the values are regular expressions.")
945 (defvar texinfo-update-menu-higher-regexps
946 '(("top" . "^@node [ \t]*DIR")
947 ("chapter" . "^@node [ \t]*top[ \t]*\\(,\\|$\\)")
948 ("section" .
949 (concat
950 "\\(^@\\("
951 texinfo-chapter-level-regexp
952 "\\)[ \t]*\\)"))
953 ("subsection" .
954 (concat
955 "\\(^@\\("
956 texinfo-section-level-regexp
957 "\\|"
958 texinfo-chapter-level-regexp
959 "\\)[ \t]*\\)"))
960 ("subsubsection" .
961 (concat
962 "\\(^@\\("
963 texinfo-subsection-level-regexp
964 "\\|"
965 texinfo-section-level-regexp
966 "\\|"
967 texinfo-chapter-level-regexp
968 "\\)[ \t]*\\)")))
969 "*Regexps for searching for higher level sections in a Texinfo file.
970 The keys are strings specifying the general hierarchical level in the
971 document; the values are regular expressions.")
973 (defvar texinfo-update-menu-lower-regexps
974 '(("top" .
975 (concat
976 "\\(^@\\("
977 texinfo-chapter-level-regexp
978 "\\|"
979 texinfo-section-level-regexp
980 "\\|"
981 texinfo-subsection-level-regexp
982 "\\|"
983 texinfo-subsubsection-level-regexp
984 "\\)[ \t]*\\)"))
985 ("chapter" .
986 (concat
987 "\\(^@\\("
988 texinfo-section-level-regexp
989 "\\|"
990 texinfo-subsection-level-regexp
991 "\\|"
992 texinfo-subsubsection-level-regexp
993 "\\)[ \t]*\\)"))
994 ("section" .
995 (concat
996 "\\(^@\\("
997 texinfo-subsection-level-regexp
998 "\\|"
999 texinfo-subsubsection-level-regexp
1000 "\\)[ \t]+\\)"))
1001 ("subsection" .
1002 (concat
1003 "\\(^@\\("
1004 texinfo-subsubsection-level-regexp
1005 "\\)[ \t]+\\)"))
1006 ("subsubsection" . "nothing lower"))
1007 "*Regexps for searching for lower level sections in a Texinfo file.
1008 The keys are strings specifying the general hierarchical level in the
1009 document; the values are regular expressions.")
1012 ;;;; Updating a Node
1014 (defun texinfo-update-node (&optional region-p)
1015 "Without any prefix argument, update the node in which point is located.
1016 Non-nil argument (prefix, if interactive) means update the nodes in the
1017 marked region.
1019 The functions for creating or updating nodes and menus, and their
1020 keybindings, are:
1022 texinfo-update-node (&optional region-p) \\[texinfo-update-node]
1023 texinfo-every-node-update () \\[texinfo-every-node-update]
1024 texinfo-sequential-node-update (&optional region-p)
1026 texinfo-make-menu (&optional region-p) \\[texinfo-make-menu]
1027 texinfo-all-menus-update () \\[texinfo-all-menus-update]
1028 texinfo-master-menu ()
1030 texinfo-indent-menu-description (column &optional region-p)
1032 The `texinfo-column-for-description' variable specifies the column to
1033 which menu descriptions are indented. Its default value is 24."
1035 (interactive "P")
1036 (if (not region-p)
1037 (let ((auto-fill-hook nil)) ; update a single node
1038 (if (not (re-search-backward "^@node" (point-min) t))
1039 (error "Node line not found before this position."))
1040 (texinfo-update-the-node)
1041 (message "Done...updated the node. You may save the buffer."))
1042 ;; else
1043 (let ((auto-fill-hook nil)
1044 (beginning (region-beginning))
1045 (end (region-end)))
1046 (if (= end beginning)
1047 (error "Please mark a region!"))
1048 (save-restriction
1049 (narrow-to-region beginning end)
1050 (goto-char beginning)
1051 (push-mark)
1052 (while (re-search-forward "^@node" (point-max) t)
1053 (beginning-of-line)
1054 (texinfo-update-the-node))
1055 (message "Done...updated nodes in region. You may save the buffer.")))))
1057 (defun texinfo-every-node-update ()
1058 "Update every node in a Texinfo file."
1059 (interactive)
1060 (save-excursion
1061 (mark-whole-buffer)
1062 (texinfo-update-node t)
1063 (message "Done...updated every node. You may save the buffer.")))
1065 (defun texinfo-update-the-node ()
1066 "Update one node. Point must be at the beginning of node line.
1067 Leave point at the end of the node line."
1068 (texinfo-check-for-node-name)
1069 (texinfo-delete-existing-pointers)
1070 (message "Updating node: %s ... " (texinfo-copy-node-name))
1071 (save-restriction
1072 (widen)
1073 (let*
1074 ((case-fold-search t)
1075 (level (texinfo-hierarchic-level))
1076 (beginning (texinfo-update-menu-region-beginning level))
1077 (end (texinfo-update-menu-region-end level)))
1078 (if (string-equal level "top")
1079 (texinfo-top-pointer-case)
1080 ;; else
1081 (texinfo-insert-pointer beginning end level 'next)
1082 (texinfo-insert-pointer beginning end level 'previous)
1083 (texinfo-insert-pointer beginning end level 'up)
1084 (texinfo-clean-up-node-line)))))
1086 (defun texinfo-top-pointer-case ()
1087 "Insert pointers in the Top node. This is a special case.
1089 The `Next' pointer is a pointer to a chapter or section at a lower
1090 hierarchical level in the file. The `Previous' and `Up' pointers are
1091 to `(dir)'. Point must be at the beginning of the node line, and is
1092 left at the end of the node line."
1094 (texinfo-clean-up-node-line)
1095 (insert ", "
1096 (save-excursion
1097 ;; There may be an @chapter or other such command between
1098 ;; the top node line and the next node line, as a title
1099 ;; for an `ifinfo' section. This @chapter command must
1100 ;; must be skipped. So the procedure is to search for
1101 ;; the next `@node' line, and then copy its name.
1102 (if (re-search-forward "^@node" nil t)
1103 (progn
1104 (beginning-of-line)
1105 (texinfo-copy-node-name))
1106 " "))
1107 ", (dir), (dir)"))
1109 (defun texinfo-check-for-node-name ()
1110 "Determine whether the node has a node name. Prompt for one if not.
1111 Point must be at beginning of node line. Does not move point."
1112 (save-excursion
1113 (forward-word 1) ; skip over node command
1114 (skip-chars-forward " \t") ; and over spaces
1115 (if (not (looking-at "[^,\t\n ]+")) ; regexp based on what info looks for
1116 ; alternatively, use "[a-zA-Z]+"
1117 (let ((node-name (read-from-minibuffer "Node name: ")))
1118 (insert " " node-name)))))
1120 (defun texinfo-delete-existing-pointers ()
1121 "Delete `Next', `Previous', and `Up' pointers.
1122 Starts from the current position of the cursor, and searches forward
1123 on the line for a comma and if one is found, deletes the rest of the
1124 line, including the comma. Leaves point at beginning of line."
1125 (if (search-forward "," (save-excursion (end-of-line) (point)) t)
1126 (progn
1127 (goto-char (1- (point)))
1128 (kill-line nil)))
1129 (beginning-of-line))
1131 (defun texinfo-find-pointer (beginning end level direction)
1132 "Move point to section associated with next, previous, or up pointer.
1133 Return type of pointer (either 'normal or 'no-pointer).
1135 The first and second arguments bound the search for a pointer to the
1136 beginning and end, respectively, of the enclosing higher level
1137 section. The third argument is a string specifying the general kind
1138 of section such as \"chapter\ or \"section\". When looking for the
1139 `Next' pointer, the section found will be at the same hierarchical
1140 level in the Texinfo file; when looking for the `Previous' pointer,
1141 the section found will be at the same or higher hierarchical level in
1142 the Texinfo file; when looking for the `Up' pointer, the section found
1143 will be at some level higher in the Texinfo file. The fourth argument
1144 \(one of 'next, 'previous, or 'up\) specifies whether to find the
1145 `Next', `Previous', or `Up' pointer."
1147 (cond ((eq direction 'next)
1148 (forward-line 3) ; skip over current node
1149 (if (re-search-forward
1150 (eval
1151 (cdr (assoc level texinfo-update-menu-same-level-regexps)))
1154 'normal
1155 'no-pointer))
1156 ((eq direction 'previous)
1157 (if (re-search-backward
1158 (concat
1159 "\\("
1160 (eval
1161 (cdr (assoc level texinfo-update-menu-same-level-regexps)))
1162 "\\|"
1163 (eval
1164 (cdr (assoc level texinfo-update-menu-higher-regexps)))
1165 "\\)")
1166 beginning
1168 'normal
1169 'no-pointer))
1170 ((eq direction 'up)
1171 (if (re-search-backward
1172 (eval (cdr (assoc level texinfo-update-menu-higher-regexps)))
1173 (save-excursion
1174 (goto-char beginning)
1175 (beginning-of-line)
1176 (point))
1178 'normal
1179 'no-pointer))
1181 (error "texinfo-find-pointer: lack proper arguments"))))
1183 (defun texinfo-pointer-name (kind)
1184 "Return the node name preceding the section command.
1185 The argument is the kind of section, either normal or no-pointer."
1186 (let (name)
1187 (cond ((eq kind 'normal)
1188 (end-of-line) ; this handles prev node top case
1189 (re-search-backward ; when point is already
1190 "^@node" ; at the beginning of @node line
1191 (save-excursion (forward-line -3))
1193 (setq name (texinfo-copy-node-name)))
1194 ((eq kind 'no-pointer)
1195 (setq name " "))) ; put a blank in the pointer slot
1196 name))
1198 (defun texinfo-insert-pointer (beginning end level direction)
1199 "Insert the `Next', `Previous' or `Up' node name at point.
1200 Move point forward.
1202 The first and second arguments bound the search for a pointer to the
1203 beginning and end, respectively, of the enclosing higher level
1204 section. The third argument is the hierarchical level of the Texinfo
1205 file, a string such as \"section\". The fourth argument is direction
1206 towards which the pointer is directed, one of `next, `previous, or
1207 'up."
1209 (end-of-line)
1210 (insert
1211 ", "
1212 (save-excursion
1213 (texinfo-pointer-name
1214 (texinfo-find-pointer beginning end level direction)))))
1216 (defun texinfo-clean-up-node-line ()
1217 "Remove extra commas, if any, at end of node line."
1218 (end-of-line)
1219 (skip-chars-backward ", ")
1220 (delete-region (point) (save-excursion (end-of-line) (point))))
1223 ;;;; Updating nodes sequentially
1224 ; These sequential update functions insert `Next' or `Previous'
1225 ; pointers that point to the following or preceding nodes even if they
1226 ; are at higher or lower hierarchical levels. This means that if a
1227 ; section contains one or more subsections, the section's `Next'
1228 ; pointer will point to the subsection and not the following section.
1229 ; (The subsection to which `Next' points will most likely be the first
1230 ; item on the section's menu.)
1232 (defun texinfo-sequential-node-update (&optional region-p)
1233 "Update one node (or many) in a Texinfo file with sequential pointers.
1235 This function causes the `Next' or `Previous' pointer to point to the
1236 immediately preceding or following node, even if it is at a higher or
1237 lower hierarchical level in the document. Continually pressing `n' or
1238 `p' takes you straight through the file.
1240 Without any prefix argument, update the node in which point is located.
1241 Non-nil argument (prefix, if interactive) means update the nodes in the
1242 marked region.
1244 This command makes it awkward to navigate among sections and
1245 subsections; it should be used only for those documents that are meant
1246 to be read like a novel rather than a reference, and for which the
1247 Info `g*' command is inadequate."
1249 (interactive "P")
1250 (if (not region-p)
1251 (let ((auto-fill-hook nil)) ; update a single node
1252 (if (not (re-search-backward "^@node" (point-min) t))
1253 (error "Node line not found before this position."))
1254 (texinfo-sequentially-update-the-node)
1255 (message
1256 "Done...sequentially updated the node . You may save the buffer."))
1257 ;; else
1258 (let ((auto-fill-hook nil)
1259 (beginning (region-beginning))
1260 (end (region-end)))
1261 (if (= end beginning)
1262 (error "Please mark a region!"))
1263 (save-restriction
1264 (narrow-to-region beginning end)
1265 (goto-char beginning)
1266 (push-mark)
1267 (while (re-search-forward "^@node" (point-max) t)
1268 (beginning-of-line)
1269 (texinfo-sequentially-update-the-node))
1270 (message
1271 "Done...updated the nodes in sequence. You may save the buffer.")))))
1273 (defun texinfo-sequentially-update-the-node ()
1274 "Update one node such that the pointers are sequential.
1275 A `Next' or `Previous' pointer points to any preceding or following node,
1276 regardless of its hierarchical level."
1278 (texinfo-check-for-node-name)
1279 (texinfo-delete-existing-pointers)
1280 (message
1281 "Sequentially updating node: %s ... " (texinfo-copy-node-name))
1282 (save-restriction
1283 (widen)
1284 (let*
1285 ((case-fold-search t)
1286 (level (texinfo-hierarchic-level)))
1287 (if (string-equal level "top")
1288 (texinfo-top-pointer-case)
1289 ;; else
1290 (texinfo-sequentially-insert-pointer level 'next)
1291 (texinfo-sequentially-insert-pointer level 'previous)
1292 (texinfo-sequentially-insert-pointer level 'up)
1293 (texinfo-clean-up-node-line)))))
1295 (defun texinfo-sequentially-find-pointer (level direction)
1296 "Find next or previous pointer sequentially in Texinfo file, or up pointer.
1297 Move point to section associated with the pointer. Find point even if
1298 it is in a different section.
1300 Return type of pointer (either 'normal or 'no-pointer).
1302 The first argument is a string specifying the general kind of section
1303 such as \"chapter\ or \"section\". The section found will be at the
1304 same hierarchical level in the Texinfo file, or, in the case of the up
1305 pointer, some level higher. The second argument (one of 'next,
1306 'previous, or 'up) specifies whether to find the `Next', `Previous',
1307 or `Up' pointer."
1309 (cond ((eq direction 'next)
1310 (forward-line 3) ; skip over current node
1311 (if (re-search-forward
1312 texinfo-section-types-regexp
1313 (point-max)
1315 'normal
1316 'no-pointer))
1317 ((eq direction 'previous)
1318 (if (re-search-backward
1319 texinfo-section-types-regexp
1320 (point-min)
1322 'normal
1323 'no-pointer))
1324 ((eq direction 'up)
1325 (if (re-search-backward
1326 (eval (cdr (assoc level texinfo-update-menu-higher-regexps)))
1327 beginning
1329 'normal
1330 'no-pointer))
1332 (error "texinfo-sequential-find-pointer: lack proper arguments"))))
1334 (defun texinfo-sequentially-insert-pointer (level direction)
1335 "Insert the `Next', `Previous' or `Up' node name at point.
1336 Move point forward.
1338 The first argument is the hierarchical level of the Texinfo file, a
1339 string such as \"section\". The second argument is direction, one of
1340 `next, `previous, or 'up."
1342 (end-of-line)
1343 (insert
1344 ", "
1345 (save-excursion
1346 (texinfo-pointer-name
1347 (texinfo-sequentially-find-pointer level direction)))))
1350 ;;;; Inserting `@node' lines
1351 ; The `texinfo-insert-node-lines' function inserts `@node' lines as needed
1352 ; before the `@chapter', `@section', and such like lines of a region
1353 ; in a Texinfo file.
1355 (defun texinfo-insert-node-lines (&optional title-p)
1356 "Insert missing `@node' lines in region of Texinfo file.
1357 Non-nil argument (prefix, if interactive) means also to insert the
1358 section titles as node names; and also to insert the section titles as
1359 node names in pre-existing @node lines that lack names."
1360 (interactive "P")
1361 (save-excursion
1362 (let ((begin-region (region-beginning))
1363 (end-region (region-end)))
1364 (goto-char begin-region)
1365 (while (< (point) end-region)
1366 (re-search-forward texinfo-section-types-regexp nil 'end)
1367 ;; copy title, since most often, we will need it
1368 (let ((title
1369 (progn
1370 (beginning-of-line)
1371 (forward-word 1)
1372 (skip-chars-forward " \t")
1373 (buffer-substring
1374 (point)
1375 (save-excursion (end-of-line) (point))))))
1376 ;; insert a node if necessary
1377 (if (re-search-backward
1378 "^@node"
1379 (save-excursion
1380 (forward-line -3)
1381 (point))
1383 ;; @node present, and point at beginning of that line
1384 (forward-word 1)
1385 ;; else @node missing, insert one
1386 (progn
1387 (beginning-of-line) ; beginning of `@section' line
1388 (insert "@node\n")
1389 (backward-char 1))) ; leave point just after `@node'
1390 ;; insert a title if warranted
1391 (if title-p
1392 (progn
1393 (skip-chars-forward " \t")
1394 ;; use regexp based on what info looks for
1395 ;; (alternatively, use "[a-zA-Z]+")
1396 (if (not (looking-at "[^,\t\n ]+"))
1397 (progn
1398 (beginning-of-line)
1399 (forward-word 1)
1400 (insert " " title)
1401 (message "Inserted title %s ... " title)))))
1402 ;; in any case, go forward beyond current section title
1403 (forward-line 3)))))
1404 (if title-p
1405 (message
1406 "Done inserting node lines and titles. You may save the buffer.")
1407 (message "Done inserting node lines. You may save the buffer.")))
1410 ;;;; Update and create menus for multi-file Texinfo sources
1412 ;; 1. M-x texinfo-multiple-files-update
1414 ;; Read the include file list of an outer Texinfo file and
1415 ;; update all highest level nodes in the files listed and insert a
1416 ;; main menu in the outer file after its top node.
1418 ;; 2. C-u M-x texinfo-multiple-files-update
1420 ;; Same as 1, but insert a master menu. (Saves reupdating lower
1421 ;; level menus and nodes.) This command simply reads every menu,
1422 ;; so if the menus are wrong, the master menu will be wrong.
1423 ;; Similarly, if the lower level node pointers are wrong, they
1424 ;; will stay wrong.
1426 ;; 3. C-u 2 M-x texinfo-multiple-files-update
1428 ;; Read the include file list of an outer Texinfo file and
1429 ;; update all nodes and menus in the files listed and insert a
1430 ;; master menu in the outer file after its top node.
1432 ;;; Note: these functions:
1434 ;;; * Do not save or delete any buffers. You may fill up your memory.
1435 ;;; * Do not handle any pre-existing nodes in outer file.
1436 ;;; Hence, you may need a file for indices.
1439 ;;;; Auxiliary functions for multiple file updating
1441 (defun texinfo-multi-file-included-list (outer-file)
1442 "Return a list of the included files in OUTER-FILE."
1443 (let ((included-file-list (list outer-file))
1444 start)
1445 (save-excursion
1446 (switch-to-buffer (find-file-noselect outer-file))
1447 (widen)
1448 (goto-char (point-min))
1449 (while (re-search-forward "^@include" nil t)
1450 (skip-chars-forward " \t")
1451 (setq start (point))
1452 (end-of-line)
1453 (skip-chars-backward " \t")
1454 (setq included-file-list
1455 (cons (buffer-substring start (point))
1456 included-file-list)))
1457 (nreverse included-file-list))))
1459 (defun texinfo-copy-next-section-title ()
1460 "Return the name of the immediately following section as a string.
1462 Start with point at the beginning of the node line. Leave point at the
1463 same place. If there is no title, returns an empty string."
1465 (save-excursion
1466 (end-of-line)
1467 (let ((section-end (or
1468 (save-excursion
1469 (re-search-forward "\\(^@node\\)" nil t)
1470 (match-beginning 0))
1471 (point-max))))
1472 (if (re-search-forward texinfo-section-types-regexp section-end t)
1473 ;; copy title
1474 (let ((title
1475 (buffer-substring
1476 (progn (forward-word 1) ; skip over section type
1477 (skip-chars-forward " \t") ; and over spaces
1478 (point))
1479 (progn (end-of-line) (point)))))
1480 title)
1481 ""))))
1483 (defun texinfo-multi-file-update (files &optional update-everything)
1484 "Update first node pointers in each file in FILES.
1485 Return a list of the node names and the title immediate following them.
1487 The first file in the list is an outer file; the remaining are
1488 files included in the outer file with `@include' commands.
1490 If optional arg UPDATE-EVERYTHING non-nil, update every menu and
1491 pointer in each of the included files.
1493 Also update the `Top' level node pointers of the outer file.
1495 Requirements:
1497 * the first file in the FILES list must be the outer file,
1498 * each of the included files must contain exactly one highest
1499 hierarchical level node,
1500 * this node must be the first node in the included file,
1501 * each highest hierarchical level node must be of the same type.
1503 Thus, normally, each included file contains one, and only one,
1504 chapter.
1506 The menu-list has the form:
1508 \(\(\"node-name1\" . \"title1\"\)
1509 \(\"node-name2\" . \"title2\"\) ... \)
1511 However, there does not need to be a title field."
1513 (let (menu-list)
1515 ;; Find the name of the first node of the first included file.
1516 (switch-to-buffer (find-file-noselect (car (cdr files))))
1517 (widen)
1518 (goto-char (point-min))
1519 (if (not (re-search-forward "^@node" nil t))
1520 (error "No `@node' line found in %s !" (buffer-name)))
1521 (beginning-of-line)
1522 (texinfo-check-for-node-name)
1523 (setq next-node-name (texinfo-copy-node-name))
1525 (setq menu-list
1526 (cons (cons
1527 next-node-name
1528 (texinfo-copy-next-section-title))
1529 menu-list))
1531 ;; Go to outer file
1532 (switch-to-buffer (find-file-noselect (car files)))
1533 (goto-char (point-min))
1534 (if (not (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t))
1535 (error "This buffer needs a Top node!"))
1536 (beginning-of-line)
1537 (texinfo-delete-existing-pointers)
1538 (end-of-line)
1539 (insert ", " next-node-name ", (dir), (dir)")
1540 (beginning-of-line)
1541 (setq previous-node-name "Top")
1542 (setq files (cdr files))
1544 (while files
1546 (if (not (cdr files))
1547 ;; No next file
1548 (setq next-node-name "")
1549 ;; Else,
1550 ;; find the name of the first node in the next file.
1551 (switch-to-buffer (find-file-noselect (car (cdr files))))
1552 (widen)
1553 (goto-char (point-min))
1554 (if (not (re-search-forward "^@node" nil t))
1555 (error "No `@node' line found in %s !" (buffer-name)))
1556 (beginning-of-line)
1557 (texinfo-check-for-node-name)
1558 (setq next-node-name (texinfo-copy-node-name))
1559 (setq menu-list
1560 (cons (cons
1561 next-node-name
1562 (texinfo-copy-next-section-title))
1563 menu-list)))
1565 ;; Go to node to be updated.
1566 (switch-to-buffer (find-file-noselect (car files)))
1567 (goto-char (point-min))
1568 (if (not (re-search-forward "^@node" nil t))
1569 (error "No `@node' line found in %s !" (buffer-name)))
1570 (beginning-of-line)
1571 (texinfo-delete-existing-pointers)
1572 (end-of-line)
1573 (insert ", " next-node-name ", " previous-node-name ", " up-node-name)
1575 (beginning-of-line)
1576 (setq previous-node-name (texinfo-copy-node-name))
1578 ;; Update other menus and nodes if requested.
1579 (if update-everything (texinfo-all-menus-update t))
1581 (setq files (cdr files)))
1582 (nreverse menu-list)))
1584 (defun texinfo-multi-files-insert-main-menu (menu-list)
1585 "Insert formatted main menu at point.
1586 Indents the first line of the description, if any, to the value of
1587 texinfo-column-for-description."
1589 (insert "@menu\n")
1590 (while menu-list
1591 (if (cdr (car menu-list)) ; menu-list has description entry
1592 (progn
1593 (insert
1594 (format "* %s::" (car (car menu-list)))) ; node-name entry
1595 (indent-to texinfo-column-for-description 2)
1596 (insert
1597 (format "%s\n" (cdr (car menu-list))))) ; description entry
1598 ;; else menu-list lacks description entry
1599 (insert
1600 (format "* %s::\n" (car (car menu-list))))) ; node-name entry
1601 (setq menu-list (cdr menu-list)))
1602 (insert "@end menu"))
1605 (defun texinfo-multi-file-master-menu-list (files-list)
1606 "Return master menu list from files in FILES-LIST.
1607 Menu entries in each file collected using `texinfo-master-menu-list'.
1609 The first file in FILES-LIST must be the outer file; the others must
1610 be the files included within it. A main menu must already exist."
1611 (save-excursion
1612 (let (master-menu-list)
1613 (while files-list
1614 (switch-to-buffer (find-file-noselect (car files-list)))
1615 (message "Working on: %s " (current-buffer))
1616 (goto-char (point-min))
1617 (setq master-menu-list
1618 (append master-menu-list (texinfo-master-menu-list)))
1619 (setq files-list (cdr files-list)))
1620 master-menu-list)))
1623 ;;;; The multiple-file update function
1625 (defun texinfo-multiple-files-update
1626 (outer-file &optional update-everything make-master-menu)
1627 "Update first node pointers in each file included in OUTER-FILE;
1628 create or update main menu in the outer file that refers to such nodes.
1629 This does not create or update menus or pointers within the included files.
1631 With optional MAKE-MASTER-MENU argument (prefix arg, if interactive),
1632 insert a master menu in OUTER-FILE. This does not create or update
1633 menus or pointers within the included files.
1635 With optional UPDATE-EVERYTHING argument (numeric prefix arg, if
1636 interactive), update all the menus and all the `Next', `Previous', and
1637 `Up' pointers of all the files included in OUTER-FILE before inserting
1638 a master menu in OUTER-FILE.
1640 The command also updates the `Top' level node pointers of OUTER-FILE.
1642 Notes:
1644 * this command does NOT save any files--you must save the
1645 outer file and any modified, included files.
1647 * except for the `Top' node, this command does NOT handle any
1648 pre-existing nodes in the outer file; hence, indices must be
1649 enclosed in an included file.
1651 Requirements:
1653 * each of the included files must contain exactly one highest
1654 hierarchical level node,
1655 * this highest node must be the first node in the included file,
1656 * each highest hierarchical level node must be of the same type.
1658 Thus, normally, each included file contains one, and only one,
1659 chapter."
1661 (interactive "fName of outer `include' file: ")
1663 (cond (current-prefix-arg
1664 (setq make-master-menu (listp current-prefix-arg))
1665 (setq update-everything (numberp current-prefix-arg))))
1667 (let* ((included-file-list (texinfo-multi-file-included-list outer-file))
1668 (files included-file-list)
1669 main-menu-list
1670 next-node-name
1671 previous-node-name
1672 (up-node-name "Top"))
1674 ;;; Update the pointers
1675 ;;; and collect the names of the nodes and titles
1676 (setq main-menu-list (texinfo-multi-file-update files update-everything))
1678 ;;; Insert main menu
1680 ;; Go to outer file
1681 (switch-to-buffer (find-file-noselect (car included-file-list)))
1682 (if (texinfo-old-menu-p
1683 (point-min)
1684 (save-excursion
1685 (re-search-forward "^@include")
1686 (beginning-of-line)
1687 (point)))
1689 ;; If found, leave point after word `menu' on the `@menu' line.
1690 (progn
1691 (texinfo-incorporate-descriptions main-menu-list)
1692 ;; Delete existing menu.
1693 (beginning-of-line)
1694 (delete-region
1695 (point)
1696 (save-excursion (re-search-forward "^@end menu") (point)))
1697 ;; Insert main menu
1698 (texinfo-multi-files-insert-main-menu main-menu-list))
1700 ;; Else no current menu; insert it before `@include'
1701 (texinfo-multi-files-insert-main-menu main-menu-list))
1703 ;;; Insert master menu
1705 (if make-master-menu
1706 (progn
1707 ;; First, removing detailed part of any pre-existing master menu
1708 (goto-char (point-min))
1709 (if (re-search-forward texinfo-master-menu-header nil t)
1710 ;; Remove detailed master menu listing
1711 (progn
1712 (goto-char (match-beginning 0))
1713 (let ((end-of-detailed-menu-descriptions
1714 (save-excursion ; beginning of end menu line
1715 (goto-char (texinfo-menu-end))
1716 (beginning-of-line) (forward-char -1)
1717 (point))))
1718 (delete-region (point) end-of-detailed-menu-descriptions))))
1720 ;; Create a master menu and insert it
1721 (texinfo-insert-master-menu-list
1722 (texinfo-multi-file-master-menu-list
1723 included-file-list)))))
1724 (message "Multiple files updated."))
1726 ;;;;;;;;;;;;;;;; end texnfo-upd.el ;;;;;;;;;;;;;;;;