*** empty log message ***
[emacs.git] / lisp / textmodes / texnfo-upd.el
blobdb70a00c8ec6cfc8bdd88db5591bda6dbe461227
1 ;;; texnfo-upd.el --- a utility for updating nodes and menus in Texinfo files.
3 ;; Author: Bob Chassell <bob@gnu.ai.mit.edu>
4 ;; Version: 2.00
5 ;; Last-Modified: 14 Dec 1990
6 ;; Keywords: maint, tex, docs
8 ;;;; Copyright 1989, 1990, 1992 Free Software Foundation
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;;; Commentary:
28 ;;;; Summary
30 ; (Much of the following commentary ought eventually be incorporated
31 ; into the Texinfo Manual.)
33 ; The node and menu updating functions automatically
35 ; * insert missing `@node' lines,
36 ; * insert the `Next', `Previous' and `Up' pointers of a node,
37 ; * insert or update the menu for a section,
38 ; * create a master menu for a Texinfo source file.
40 ; Passed an argument, the `texinfo-update-node' and
41 ; `texinfo-make-menu' functions do their jobs in the region.
43 ; These functions replace doing these jobs by hand.
44 ; You may find them helpful.
46 ; In brief, the functions for creating or updating nodes and menus, are:
48 ; texinfo-update-node (&optional region-p)
49 ; texinfo-every-node-update ()
50 ; texinfo-sequential-node-update (&optional region-p)
52 ; texinfo-make-menu (&optional region-p)
53 ; texinfo-all-menus-update ()
54 ; texinfo-master-menu ()
56 ; texinfo-insert-node-lines (&optional title-p)
58 ; texinfo-indent-menu-description (column &optional region-p)
60 ; The `texinfo-column-for-description' variable specifies the column to
61 ; which menu descriptions are indented.
63 ; Texinfo file structure
64 ; ----------------------
66 ; To use the updating commands, you must structure your Texinfo file
67 ; hierarchically. Each `@node' line, with the exception of the top
68 ; node, must be accompanied by some kind of section line, such as an
69 ; `@chapter' or `@section' line. Each node-line/section-line
70 ; combination must look like this:
72 ; @node Lists and Tables, Cross References, Structuring, Top
73 ; @comment node-name, next, previous, up
74 ; @chapter Making Lists and Tables
76 ; or like this (without the `@comment' line):
78 ; @node Lists and Tables, Cross References, Structuring, Top
79 ; @chapter Making Lists and Tables
81 ; If the file has a `top' node, it must be called `top' or `Top' and
82 ; be the first node in the file.
85 ;;;; The updating functions in detail
86 ; --------------------------------
88 ; The `texinfo-update-node' function without an argument inserts
89 ; the correct next, previous and up pointers for the node in which
90 ; point is located (i.e., for the node preceding point).
92 ; With an argument, the `texinfo-update-node' function inserts the
93 ; correct next, previous and up pointers for the nodes inside the
94 ; region.
96 ; It does not matter whether the `@node' line has pre-existing
97 ; `Next', `Previous', or `Up' pointers in it. They are removed.
99 ; The `texinfo-every-node-update' function runs `texinfo-update-node'
100 ; on the whole buffer.
102 ; The `texinfo-update-node' function inserts the immediately following
103 ; and preceding node into the `Next' or `Previous' pointers regardless
104 ; of their hierarchical level. This is only useful for certain kinds
105 ; of text, like a novel, which you go through sequentially.
107 ; The `texinfo-make-menu' function without an argument creates or
108 ; updates a menu for the section encompassing the node that follows
109 ; point. With an argument, it makes or updates menus for the nodes
110 ; within or part of the marked region.
112 ; Whenever an existing menu is updated, the descriptions from
113 ; that menu are incorporated into the new menu. This is done by copying
114 ; descriptions from the existing menu to the entries in the new menu
115 ; that have the same node names. If the node names are different, the
116 ; descriptions are not copied to the new menu.
118 ; Menu entries that refer to other Info files are removed since they
119 ; are not a node within current buffer. This is a deficiency.
121 ; The `texinfo-all-menus-update' function runs `texinfo-make-menu'
122 ; on the whole buffer.
124 ; The `texinfo-master-menu' function creates an extended menu located
125 ; after the top node. (The file must have a top node.) The function
126 ; first updates all the regular menus in the buffer (incorporating the
127 ; descriptions from pre-existing menus), and then constructs a master
128 ; menu that includes every entry from every other menu. (However, the
129 ; function cannot update an already existing master menu; if one
130 ; exists, it must be removed before calling the function.)
132 ; The `texinfo-indent-menu-description' function indents every
133 ; description in the menu following point, to the specified column.
134 ; Non-nil argument (prefix, if interactive) means indent every
135 ; description in every menu in the region. This function does not
136 ; indent second and subsequent lines of a multi-line description.
138 ; The `texinfo-insert-node-lines' function inserts `@node' before the
139 ; `@chapter', `@section', and such like lines of a region in a Texinfo
140 ; file where the `@node' lines are missing.
142 ; With a non-nil argument (prefix, if interactive), the function not
143 ; only inserts `@node' lines but also inserts the chapter or section
144 ; titles as the names of the corresponding nodes; and inserts titles
145 ; as node names in pre-existing `@node' lines that lack names.
147 ; Since node names should be more concise than section or chapter
148 ; titles, node names so inserted will need to be edited manually.
150 ;;; Code:
152 ;;;; Menu Making Functions
154 (defun texinfo-make-menu (&optional region-p)
155 "Without any prefix argument, make or update a menu.
156 Make the menu for the section enclosing the node found following point.
158 Non-nil argument (prefix, if interactive) means make or update menus
159 for nodes within or part of the marked region.
161 Whenever a menu exists, and is being updated, the descriptions that
162 are associated with node names in the pre-existing menu are
163 incorporated into the new menu. Otherwise, the nodes' section titles
164 are inserted as descriptions."
166 (interactive "P")
167 (if (not region-p)
168 (let ((level (texinfo-hierarchic-level)))
169 (texinfo-make-one-menu level)
170 (message "Done...updated the menu. You may save the buffer."))
171 ;; else
172 (message "Making or updating menus... ")
173 (let ((beginning (region-beginning))
174 (region-end (region-end))
175 (level (progn ; find section type following point
176 (goto-char (region-beginning))
177 (texinfo-hierarchic-level))))
178 (if (= region-end beginning)
179 (error "Please mark a region!"))
180 (save-excursion
181 (save-restriction
182 (widen)
184 (while (texinfo-find-lower-level-node level region-end)
185 (setq level (texinfo-hierarchic-level)) ; new, lower level
186 (texinfo-make-one-menu level))
188 (while (and (< (point) region-end)
189 (texinfo-find-higher-level-node level region-end))
190 (setq level (texinfo-hierarchic-level))
191 (while (texinfo-find-lower-level-node level region-end)
192 (setq level (texinfo-hierarchic-level)) ; new, lower level
193 (texinfo-make-one-menu level))))))
194 (message "Done...updated menus. You may save the buffer.")))
196 (defun texinfo-make-one-menu (level)
197 "Make a menu of all the appropriate nodes in this section.
198 `Appropriate nodes' are those associated with sections that are
199 at the level specified by LEVEL. Point is left at the end of menu."
200 (let*
201 ((case-fold-search t)
202 (beginning
203 (save-excursion
204 (goto-char (texinfo-update-menu-region-beginning level))
205 (end-of-line)
206 (point)))
207 (end (texinfo-update-menu-region-end level))
208 (first (texinfo-menu-first-node beginning end))
209 (node-name (progn
210 (goto-char beginning)
211 (texinfo-copy-node-name)))
212 (new-menu-list (texinfo-make-menu-list beginning end level)))
213 (if (texinfo-old-menu-p beginning first)
214 (progn
215 (texinfo-incorporate-descriptions new-menu-list)
216 (texinfo-delete-old-menu beginning first)))
217 (texinfo-insert-menu new-menu-list node-name)))
219 (defun texinfo-all-menus-update (&optional update-all-nodes-p)
220 "Update every regular menu in a Texinfo file.
221 You must remove the detailed part of a pre-existing master menu before
222 running this command, lest it be partly duplicated.
224 If called with a non-nil argument, this function first updates all the
225 nodes in the buffer before updating the menus."
226 (interactive "P")
227 (save-excursion
228 (mark-whole-buffer)
229 (message "Checking for a master menu... ")
230 (save-excursion
231 (if (re-search-forward texinfo-master-menu-header nil t)
232 (error
233 "Please remove existing master menu, lest it be partly duplicated!")))
235 (if update-all-nodes-p
236 (progn
237 (message "First updating all nodes... ")
238 (sleep-for 2)
239 (mark-whole-buffer)
240 (texinfo-update-node t)))
242 (message "Updating all menus... ")
243 (sleep-for 2)
244 (texinfo-make-menu t)
245 (message "Done...updated all the menus. You may save the buffer.")))
247 (defun texinfo-find-lower-level-node (level region-end)
248 "Search forward from point for node at any level lower than LEVEL.
249 Search is limited to the end of the marked region, REGION-END,
250 and to the end of the menu region for the level.
252 Return t if the node is found, else nil. Leave point at the beginning
253 of the node if one is found; else do not move point."
255 (if (and (< (point) region-end)
256 (re-search-forward
257 (concat
258 "\\(^@node\\).*\n" ; match node line
259 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
260 "\\|" ; or
261 "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
262 (eval (cdr (assoc level texinfo-update-menu-lower-regexps))))
263 ;; the next higher level node marks the end of this
264 ;; section, and no lower level node will be found beyond
265 ;; this position even if region-end is farther off
266 (texinfo-update-menu-region-end level)
268 (goto-char (match-beginning 1))))
270 (defun texinfo-find-higher-level-node (level region-end)
271 "Search forward from point for node at any higher level than argument LEVEL.
272 Search is limited to the end of the marked region, REGION-END.
274 Return t if the node is found, else nil. Leave point at the beginning
275 of the node if one is found; else do not move point."
277 (if (and (< (point) region-end)
278 (re-search-forward
279 (concat
280 "\\(^@node\\).*\n" ; match node line
281 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
282 "\\|" ; or
283 "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
284 (eval ; (won't ever find a `top' node)
285 (cdr (assoc level texinfo-update-menu-higher-regexps))))
288 (goto-char (match-beginning 1))))
291 ;;;; Making the list of new menu entries
293 (defun texinfo-make-menu-list (beginning end level)
294 "Make a list of node names and their descriptions.
295 Point is left at the end of the menu region, but the menu is not inserted.
297 First argument is position from which to start making menu list;
298 second argument is end of region in which to try to locate entries;
299 third argument is the level of the nodes that are the entries.
301 Node names and descriptions are dotted pairs of strings. Each pair is
302 an element of the list. If the description does not exist, the
303 element consists only of the node name."
304 (goto-char beginning)
305 (let (new-menu-list)
306 (while (texinfo-menu-locate-entry-p level end)
307 (setq new-menu-list
308 (cons (cons
309 (texinfo-copy-node-name)
310 (texinfo-copy-section-title))
311 new-menu-list)))
312 (reverse new-menu-list)))
314 (defun texinfo-menu-locate-entry-p (level search-end)
315 "Find a node that will be part of menu for this section.
316 First argument is a string such as \"section\" specifying the general
317 hierarchical level of the menu; second argument is a postion
318 specifying the end of the search.
320 The function returns t if the node is found, else nil. It searches
321 forward from point, and leaves point at the beginning of the node.
323 The function finds entries of the same type. Thus `subsections' and
324 `unnumberedsubsecs' will appear in the same menu."
325 (if (re-search-forward
326 (concat
327 "\\(^@node\\).*\n" ; match node line
328 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
329 "\\|" ; or
330 "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
331 (eval
332 (cdr (assoc level texinfo-update-menu-same-level-regexps))))
333 search-end
335 (goto-char (match-beginning 1))))
337 (defun texinfo-copy-node-name ()
338 "Return the node name as a string.
340 Start with point at the beginning of the node line; copy the text
341 after the node command up to the first comma on the line, if any, and
342 return the text as a string. Leaves point at the beginning of the
343 line. If there is no node name, returns an empty string."
345 (save-excursion
346 (buffer-substring
347 (progn (forward-word 1) ; skip over node command
348 (skip-chars-forward " \t") ; and over spaces
349 (point))
350 (if (search-forward
352 (save-excursion (end-of-line) (point)) t) ; bound search
353 (1- (point))
354 (end-of-line) (point)))))
356 (defun texinfo-copy-section-title ()
357 "Return the title of the section as a string.
358 The title is used as a description line in the menu when one does not
359 already exist.
361 Move point to the beginning of the appropriate section line by going
362 to the start of the text matched by last regexp searched for, which
363 must have been done by `texinfo-menu-locate-entry-p'."
365 ;; could use the same re-search as in `texinfo-menu-locate-entry-p'
366 ;; instead of using `match-beginning'; such a variation would be
367 ;; more general, but would waste information already collected
369 (goto-char (match-beginning 7)) ; match section name
371 (buffer-substring
372 (progn (forward-word 1) ; skip over section type
373 (skip-chars-forward " \t") ; and over spaces
374 (point))
375 (progn (end-of-line) (point))))
378 ;;;; Handling the old menu
380 (defun texinfo-old-menu-p (beginning first)
381 "Move point to the beginning of the menu for this section, if any.
382 Otherwise move point to the end of the first node of this section.
383 Return t if a menu is found, nil otherwise.
385 First argument is the position of the beginning of the section in which
386 the menu will be located; second argument is the position of the first
387 node within the section.
389 If no menu is found, the function inserts two newlines just before the
390 end of the section, and leaves point there where a menu ought to be."
391 (goto-char beginning)
392 (if (not (re-search-forward "^@menu" first 'goto-end))
393 (progn (insert "\n\n") (forward-line -2) nil)
396 (defun texinfo-incorporate-descriptions (new-menu-list)
397 "Copy the old menu line descriptions that exist to the new menu.
399 Point must be at beginning of old menu.
401 If the node-name of the new menu entry cannot be found in the old
402 menu, use the new section title for the description, but if the
403 node-name of the new menu is found in the old menu, replace the
404 section title with the old description, whatever it may be.
406 For this function, the new menu is a list made up of lists of dotted
407 pairs in which the first element of the pair is the node name and the
408 second element the description. The new menu is changed destructively.
409 The old menu is the menu as it appears in the texinfo file."
411 (let ((new-menu-list-pointer new-menu-list)
412 (end-of-menu (texinfo-menu-end)))
413 (while new-menu-list
414 (save-excursion ; keep point at beginning of menu
415 (if (search-forward
416 (concat "\* " ; so only menu entries are found
417 (car (car new-menu-list))
418 ":") ; so only complete entries are found
419 end-of-menu
421 (setcdr (car new-menu-list)
422 (texinfo-menu-copy-old-description end-of-menu))))
423 (setq new-menu-list (cdr new-menu-list)))
424 (setq new-menu-list new-menu-list-pointer)))
426 (defun texinfo-menu-copy-old-description (end-of-menu)
427 "Return description field of old menu line as string.
428 Point must be located just after the node name. Point left before description.
429 Single argument, END-OF-MENU, is position limiting search."
430 (skip-chars-forward "[:.,\t\n ]+")
431 ;; don't copy a carriage return at line beginning with asterisk!
432 ;; do copy a description that begins with an `@'!
433 (if (and (looking-at "\\(\\w+\\|@\\)")
434 (not (looking-at "\\(^\\* \\|^@end menu\\)")))
435 (buffer-substring
436 (point)
437 (save-excursion
438 (re-search-forward "\\(^\\* \\|^@end menu\\)" end-of-menu t)
439 (forward-line -1)
440 (end-of-line) ; go to end of last description line
441 (point)))
442 ""))
444 (defun texinfo-menu-end ()
445 "Return position of end of menu. Does not change location of point.
446 Signal an error if not end of menu."
447 (save-excursion
448 (if (re-search-forward "^@end menu" nil t)
449 (point)
450 (error "Menu does not have an end."))))
452 (defun texinfo-delete-old-menu (beginning first)
453 "Delete the old menu. Point must be in or after menu.
454 First argument is position of the beginning of the section in which
455 the menu will be located; second argument is the position of the first
456 node within the section."
457 ;; No third arg to search, so error if search fails.
458 (re-search-backward "^@menu" beginning)
459 (delete-region (point)
460 (save-excursion
461 (re-search-forward "^@end menu" first)
462 (point))))
465 ;;;; Inserting new menu
467 ;; try 32, but perhaps 24 is better
468 (defvar texinfo-column-for-description 32
469 "*Column at which descriptions start in a Texinfo menu.")
471 (defun texinfo-insert-menu (menu-list node-name)
472 "Insert formatted menu at point.
473 Indents the first line of the description, if any, to the value of
474 texinfo-column-for-description.
476 MENU-LIST has form:
478 \(\(\"node-name1\" . \"description\"\)
479 \(\"node-name\" . \"description\"\) ... \)
481 However, there does not need to be a description field."
483 (insert "@menu\n")
484 (while menu-list
485 (if (cdr (car menu-list)) ; menu-list has description entry
486 (progn
487 (insert
488 (format "* %s::" (car (car menu-list)))) ; node-name entry
489 (indent-to texinfo-column-for-description 2)
490 (insert
491 (format "%s\n" (cdr (car menu-list))))) ; description entry
492 ;; else menu-list lacks description entry
493 (insert
494 (format "* %s::\n" (car (car menu-list))))) ; node-name entry
495 (setq menu-list (cdr menu-list)))
496 (insert "@end menu")
497 (message
498 "Updated \"%s\" level menu following node: %s ... "
499 level node-name))
502 ;;;; Handling description indentation
504 ; Since the make-menu functions indent descriptions, these functions
505 ; are useful primarily for indenting a single menu specially.
507 (defun texinfo-indent-menu-description (column &optional region-p)
508 "Indent every description in menu following point to COLUMN.
509 Non-nil argument (prefix, if interactive) means indent every
510 description in every menu in the region. Does not indent second and
511 subsequent lines of a multi-line description."
513 (interactive
514 "nIndent menu descriptions to (column number): \nP")
515 (save-excursion
516 (save-restriction
517 (widen)
518 (if (not region-p)
519 (progn
520 (re-search-forward "^@menu")
521 (texinfo-menu-indent-description column)
522 (message
523 "Indented descriptions in menu. You may save the buffer."))
524 ;;else
525 (message "Indenting every menu description in region... ")
526 (goto-char (region-beginning))
527 (while (and (< (point) (region-end))
528 (texinfo-locate-menu-p))
529 (forward-line 1)
530 (texinfo-menu-indent-description column))
531 (message "Indenting done. You may save the buffer.")))))
533 (defun texinfo-menu-indent-description (to-column-number)
534 "Indent the Texinfo file menu description to TO-COLUMN-NUMBER.
535 Start with point just after the word `menu' in the `@menu' line and
536 leave point on the line before the `@end menu' line. Does not indent
537 second and subsequent lines of a multi-line description."
538 (let* ((beginning-of-next-line (point)))
539 (while (< beginning-of-next-line
540 (save-excursion ; beginning of end menu line
541 (goto-char (texinfo-menu-end))
542 (beginning-of-line)
543 (point)))
544 (if (search-forward "::" (texinfo-menu-end) t)
545 (progn
546 (let ((beginning-white-space (point)))
547 (skip-chars-forward " \t") ; skip over spaces
548 (if (looking-at "\\(@\\|\\w\\)+") ; if there is text
549 (progn
550 ;; remove pre-existing indentation
551 (delete-region beginning-white-space (point))
552 (indent-to-column to-column-number))))))
553 ;; position point at beginning of next line
554 (forward-line 1)
555 (setq beginning-of-next-line (point)))))
558 ;;;; Making the master menu
560 (defun texinfo-master-menu (update-all-nodes-menus-p)
561 "Make a master menu for a whole Texinfo file.
562 Non-nil argument (prefix, if interactive) means first update all
563 existing nodes and menus. Remove pre-existing master menu, if there is one.
565 This function creates a master menu that follows the top node. The
566 master menu includes every entry from all the other menus. It
567 replaces any existing ordinary menu that follows the top node.
569 If called with a non-nil argument, this function first updates all the
570 menus in the buffer (incorporating descriptions from pre-existing
571 menus) before it constructs the master menu.
573 The function removes the detailed part of an already existing master
574 menu. This action depends on the pre-exisitng master menu using the
575 standard `texinfo-master-menu-header'.
577 The master menu has the following format, which is adapted from the
578 recommendation in the Texinfo Manual:
580 * The first part contains the major nodes in the Texinfo file: the
581 nodes for the chapters, chapter-like sections, and the major
582 appendices. This includes the indices, so long as they are in
583 chapter-like sections, such as unnumbered sections.
585 * The second and subsequent parts contain a listing of the other,
586 lower level menus, in order. This way, an inquirer can go
587 directly to a particular node if he or she is searching for
588 specific information.
590 Each of the menus in the detailed node listing is introduced by the
591 title of the section containing the menu."
593 (interactive "P")
594 (widen)
595 (goto-char (point-min))
597 ;; Move point to location after `top'.
598 (if (not (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t))
599 (error "This buffer needs a Top node!"))
601 (let ((first-chapter
602 (save-excursion (re-search-forward "^@node") (point))))
603 (if (re-search-forward texinfo-master-menu-header first-chapter t)
604 ;; Remove detailed master menu listing
605 (progn
606 (goto-char (match-beginning 0))
607 (let ((end-of-detailed-menu-descriptions
608 (save-excursion ; beginning of end menu line
609 (goto-char (texinfo-menu-end))
610 (beginning-of-line) (forward-char -1)
611 (point))))
612 (delete-region (point) end-of-detailed-menu-descriptions)))))
614 (if update-all-nodes-menus-p
615 (progn
616 (message "Making a master menu...first updating all nodes... ")
617 (sleep-for 2)
618 (mark-whole-buffer)
619 (texinfo-update-node t)
621 (message "Updating all menus... ")
622 (sleep-for 2)
623 (mark-whole-buffer)
624 (texinfo-make-menu t)))
626 (message "Now making the master menu... ")
627 (sleep-for 2)
628 (goto-char (point-min))
629 (texinfo-insert-master-menu-list
630 (texinfo-master-menu-list))
632 ;; Remove extra newlines that texinfo-insert-master-menu-list
633 ;; may have inserted.
635 (save-excursion
636 (goto-char (point-min))
638 (re-search-forward texinfo-master-menu-header)
639 (goto-char (match-beginning 0))
640 (insert "\n")
641 (delete-blank-lines)
643 (re-search-backward "^@menu")
644 (forward-line -1)
645 (delete-blank-lines)
647 (re-search-forward "^@end menu")
648 (forward-line 1)
649 (delete-blank-lines))
651 (message "Done...completed making master menu. You may save the buffer."))
653 (defun texinfo-master-menu-list ()
654 "Return a list of menu entries and header lines for the master menu.
656 Start with the menu for chapters and indices and then find each
657 following menu and the title of the node preceding that menu.
659 The master menu list has this form:
661 \(\(\(... \"entry-1-2\" \"entry-1\"\) \"title-1\"\)
662 \(\(... \"entry-2-2\" \"entry-2-1\"\) \"title-2\"\)
663 ...\)
665 However, there does not need to be a title field."
667 (let (master-menu-list)
668 (while (texinfo-locate-menu-p)
669 (setq master-menu-list
670 (cons (list
671 (texinfo-copy-menu)
672 (texinfo-copy-menu-title))
673 master-menu-list)))
674 (reverse master-menu-list)))
676 (defun texinfo-insert-master-menu-list (master-menu-list)
677 "Format and insert the master menu in the current buffer."
678 (goto-char (point-min))
679 (re-search-forward "^@menu")
680 (beginning-of-line)
681 (delete-region (point) ; buffer must have ordinary top menu
682 (save-excursion
683 (re-search-forward "^@end menu")
684 (point)))
686 (save-excursion ; leave point at beginning of menu
687 ;; Handle top of menu
688 (insert "\n@menu\n")
689 ;; Insert chapter menu entries
690 (setq this-very-menu-list (reverse (car (car master-menu-list))))
691 ;;; Tell user what is going on.
692 (message "Inserting chapter menu entry: %s ... " this-very-menu-list)
693 (while this-very-menu-list
694 (insert "* " (car this-very-menu-list) "\n")
695 (setq this-very-menu-list (cdr this-very-menu-list)))
697 (setq master-menu-list (cdr master-menu-list))
699 (insert texinfo-master-menu-header)
701 ;; Now, insert all the other menus
703 ;; The menu master-menu-list has a form like this:
704 ;; ((("beta" "alpha") "title-A")
705 ;; (("delta" "gamma") "title-B"))
707 (while master-menu-list
709 (message
710 "Inserting menu for %s .... " (car (cdr (car master-menu-list))))
711 ;; insert title of menu section
712 (insert "\n" (car (cdr (car master-menu-list))) "\n\n")
714 ;; insert each menu entry
715 (setq this-very-menu-list (reverse (car (car master-menu-list))))
716 (while this-very-menu-list
717 (insert "* " (car this-very-menu-list) "\n")
718 (setq this-very-menu-list (cdr this-very-menu-list)))
720 (setq master-menu-list (cdr master-menu-list)))
722 ;; Finish menu
723 (insert "@end menu\n\n")))
725 (defvar texinfo-master-menu-header
726 "\n --- The Detailed Node Listing ---\n"
727 "String inserted before lower level entries in Texinfo master menu.
728 It comes after the chapter-level menu entries.")
730 (defun texinfo-locate-menu-p ()
731 "Find the next menu in the texinfo file.
732 If found, leave point after word `menu' on the `@menu' line, and return t.
733 If a menu is not found, do not move point and return nil."
734 (re-search-forward "\\(^@menu\\)" nil t))
736 (defun texinfo-copy-menu-title ()
737 "Return the title of the section preceding the menu as a string.
738 If such a title cannot be found, return an empty string. Do not move
739 point."
740 (save-excursion
741 (if (re-search-backward
742 (concat
743 "\\(^@node\\).*\n" ; match node line
744 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
745 "\\|" ; or
746 "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
747 (eval
748 (cdr
749 (assoc (texinfo-hierarchic-level)
750 texinfo-update-menu-higher-regexps))))
753 (texinfo-copy-section-title)
754 " ")))
756 (defun texinfo-copy-menu ()
757 "Return the entries of an existing menu as a list.
758 Start with point just after the word `menu' in the `@menu' line
759 and leave point on the line before the `@end menu' line."
760 (let* (this-menu-list
761 (end-of-menu (texinfo-menu-end)) ; position of end of `@end menu'
762 (last-entry (save-excursion ; position of beginning of
763 ; last `* ' entry
764 (goto-char end-of-menu)
765 (re-search-backward "^\* ") ; handle multi-line desc.
766 (point))))
767 (while (< (point) last-entry)
768 (if (re-search-forward "^\* " end-of-menu t)
769 (progn
770 (setq this-menu-list
771 (cons
772 (buffer-substring
773 (point)
774 ;; copy multi-line descriptions
775 (save-excursion
776 (re-search-forward "\\(^\* \\|^@e\\)" nil t)
777 (- (point) 3)))
778 this-menu-list)))))
779 this-menu-list))
782 ;;;; Determining the hierarchical level in the texinfo file
784 (defun texinfo-specific-section-type ()
785 "Return the specific type of next section, as a string.
786 For example, \"unnumberedsubsec\". Return \"top\" for top node.
788 Searches forward for a section. Hence, point must be before the
789 section whose type will be found. Does not move point. Signal an
790 error if the node is not the top node and a section is not found."
791 (save-excursion
792 (cond
793 ((re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)"
794 (save-excursion
795 (end-of-line)
796 (point))
798 "top")
799 ((re-search-forward texinfo-section-types-regexp nil t)
800 (buffer-substring (progn (beginning-of-line) ; copy its name
801 (1+ (point)))
802 (progn (forward-word 1)
803 (point))))
805 (error
806 "texinfo-specific-section-type: Chapter or section not found.")))))
808 (defun texinfo-hierarchic-level ()
809 "Return the general hierarchal level of the next node in a texinfo file.
810 Thus, a subheading or appendixsubsec is of type subsection."
811 (cdr (assoc
812 (texinfo-specific-section-type)
813 texinfo-section-to-generic-alist)))
816 ;;;; Locating the major positions
818 (defun texinfo-update-menu-region-beginning (level)
819 "Locate beginning of higher level section this section is within.
820 Return position of the beginning of the node line; do not move point.
821 Thus, if this level is subsection, searches backwards for section node.
822 Only argument is a string of the general type of section."
824 (cond
825 ((or (string-equal "top" level)
826 (string-equal "chapter" level))
827 (save-excursion
828 (goto-char (point-min))
829 (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t)
830 (beginning-of-line)
831 (point)))
833 (save-excursion
834 (re-search-backward
835 (concat
836 "\\(^@node\\).*\n" ; match node line
837 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
838 "\\|" ; or
839 "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
840 (eval
841 (cdr (assoc level texinfo-update-menu-higher-regexps))))
843 'goto-beginning)
844 (point)))))
846 (defun texinfo-update-menu-region-end (level)
847 "Locate end of higher level section this section is within.
848 Return position; do not move point. Thus, if this level is a
849 subsection, find the node for the section this subsection is within.
850 If level is top or chapter, returns end of file. Only argument is a
851 string of the general type of section."
853 (save-excursion
854 (if (re-search-forward
855 (concat
856 "\\(^@node\\).*\n" ; match node line
857 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
858 "\\|" ; or
859 "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
860 (eval
861 (cdr (assoc level texinfo-update-menu-higher-regexps))))
863 'goto-end)
864 (match-beginning 1)
865 (point-max))))
867 (defun texinfo-menu-first-node (beginning end)
868 "Locate first node of the section the menu will be placed in.
869 Return position; do not move point.
870 The menu will be located just before this position.
872 First argument is the position of the beginning of the section in
873 which the menu will be located; second argument is the position of the
874 end of that region; it limits the search."
876 (save-excursion
877 (goto-char beginning)
878 (forward-line 1)
879 (re-search-forward "^@node" end t)
880 (beginning-of-line)
881 (point)))
884 ;;;; Alists and regular expressions for defining hierarchical levels
886 (defvar texinfo-section-to-generic-alist
887 '(("top" . "top")
889 ("chapter" . "chapter")
890 ("unnumbered" . "chapter")
891 ("majorheading" . "chapter")
892 ("chapheading" . "chapter")
893 ("appendix" . "chapter")
895 ("section" . "section")
896 ("unnumberedsec" . "section")
897 ("heading" . "section")
898 ("appendixsec" . "section")
900 ("subsection" . "subsection")
901 ("unnumberedsubsec" . "subsection")
902 ("subheading" . "subsection")
903 ("appendixsubsec" . "subsection")
905 ("subsubsection" . "subsubsection")
906 ("unnumberedsubsubsec" . "subsubsection")
907 ("subsubheading" . "subsubsection")
908 ("appendixsubsubsec" . "subsubsection"))
909 "*An alist of specific and corresponding generic Texinfo section types.
910 The keys are strings specifying specific types of section; the values
911 are strings of their corresponding general types.")
913 (defvar texinfo-section-types-regexp
914 "^@\\(chapter \\|sect\\|sub\\|unnum\\|major\\|chapheading \\|heading \\|appendix\\)"
915 "Regexp matching chapter, section, other headings (but not the top node).")
917 (defvar texinfo-chapter-level-regexp
918 "chapter\\|unnumbered \\|appendix \\|majorheading\\|chapheading"
919 "Regular expression matching just the Texinfo chapter level headings.")
921 (defvar texinfo-section-level-regexp
922 "section\\|unnumberedsec\\|heading \\|appendixsec"
923 "Regular expression matching just the Texinfo section level headings.")
925 (defvar texinfo-subsection-level-regexp
926 "subsection\\|unnumberedsubsec\\|subheading\\|appendixsubsec"
927 "Regular expression matching just the Texinfo subsection level headings.")
929 (defvar texinfo-subsubsection-level-regexp
930 "subsubsection\\|unnumberedsubsubsec\\|subsubheading\\|appendixsubsubsec"
931 "Regular expression matching just the Texinfo subsubsection level headings.")
933 (defvar texinfo-update-menu-same-level-regexps
934 '(("top" . "top[ \t]+")
935 ("chapter" .
936 (concat "\\(^@\\)\\(" texinfo-chapter-level-regexp "\\)[ \t]*"))
937 ("section" .
938 (concat "\\(^@\\)\\(" texinfo-section-level-regexp "\\)[ \t]*"))
939 ("subsection" .
940 (concat "\\(^@\\)\\(" texinfo-subsection-level-regexp "\\)[ \t]+"))
941 ("subsubsection" .
942 (concat "\\(^@\\)\\(" texinfo-subsubsection-level-regexp "\\)[ \t]+")))
943 "*Regexps for searching for same level sections in a Texinfo file.
944 The keys are strings specifying the general hierarchical level in the
945 document; the values are regular expressions.")
947 (defvar texinfo-update-menu-higher-regexps
948 '(("top" . "^@node [ \t]*DIR")
949 ("chapter" . "^@node [ \t]*top[ \t]*\\(,\\|$\\)")
950 ("section" .
951 (concat
952 "\\(^@\\("
953 texinfo-chapter-level-regexp
954 "\\)[ \t]*\\)"))
955 ("subsection" .
956 (concat
957 "\\(^@\\("
958 texinfo-section-level-regexp
959 "\\|"
960 texinfo-chapter-level-regexp
961 "\\)[ \t]*\\)"))
962 ("subsubsection" .
963 (concat
964 "\\(^@\\("
965 texinfo-subsection-level-regexp
966 "\\|"
967 texinfo-section-level-regexp
968 "\\|"
969 texinfo-chapter-level-regexp
970 "\\)[ \t]*\\)")))
971 "*Regexps for searching for higher level sections in a Texinfo file.
972 The keys are strings specifying the general hierarchical level in the
973 document; the values are regular expressions.")
975 (defvar texinfo-update-menu-lower-regexps
976 '(("top" .
977 (concat
978 "\\(^@\\("
979 texinfo-chapter-level-regexp
980 "\\|"
981 texinfo-section-level-regexp
982 "\\|"
983 texinfo-subsection-level-regexp
984 "\\|"
985 texinfo-subsubsection-level-regexp
986 "\\)[ \t]*\\)"))
987 ("chapter" .
988 (concat
989 "\\(^@\\("
990 texinfo-section-level-regexp
991 "\\|"
992 texinfo-subsection-level-regexp
993 "\\|"
994 texinfo-subsubsection-level-regexp
995 "\\)[ \t]*\\)"))
996 ("section" .
997 (concat
998 "\\(^@\\("
999 texinfo-subsection-level-regexp
1000 "\\|"
1001 texinfo-subsubsection-level-regexp
1002 "\\)[ \t]+\\)"))
1003 ("subsection" .
1004 (concat
1005 "\\(^@\\("
1006 texinfo-subsubsection-level-regexp
1007 "\\)[ \t]+\\)"))
1008 ("subsubsection" . "nothing lower"))
1009 "*Regexps for searching for lower level sections in a Texinfo file.
1010 The keys are strings specifying the general hierarchical level in the
1011 document; the values are regular expressions.")
1014 ;;;; Updating a Node
1016 (defun texinfo-update-node (&optional region-p)
1017 "Without any prefix argument, update the node in which point is located.
1018 Non-nil argument (prefix, if interactive) means update the nodes in the
1019 marked region.
1021 The functions for creating or updating nodes and menus, and their
1022 keybindings, are:
1024 texinfo-update-node (&optional region-p) \\[texinfo-update-node]
1025 texinfo-every-node-update () \\[texinfo-every-node-update]
1026 texinfo-sequential-node-update (&optional region-p)
1028 texinfo-make-menu (&optional region-p) \\[texinfo-make-menu]
1029 texinfo-all-menus-update () \\[texinfo-all-menus-update]
1030 texinfo-master-menu ()
1032 texinfo-indent-menu-description (column &optional region-p)
1034 The `texinfo-column-for-description' variable specifies the column to
1035 which menu descriptions are indented. Its default value is 24."
1037 (interactive "P")
1038 (if (not region-p)
1039 (let ((auto-fill-function nil)) ; update a single node
1040 (if (not (re-search-backward "^@node" (point-min) t))
1041 (error "Node line not found before this position."))
1042 (texinfo-update-the-node)
1043 (message "Done...updated the node. You may save the buffer."))
1044 ;; else
1045 (let ((auto-fill-function nil)
1046 (beginning (region-beginning))
1047 (end (region-end)))
1048 (if (= end beginning)
1049 (error "Please mark a region!"))
1050 (save-restriction
1051 (narrow-to-region beginning end)
1052 (goto-char beginning)
1053 (push-mark)
1054 (while (re-search-forward "^@node" (point-max) t)
1055 (beginning-of-line)
1056 (texinfo-update-the-node))
1057 (message "Done...updated nodes in region. You may save the buffer.")))))
1059 (defun texinfo-every-node-update ()
1060 "Update every node in a Texinfo file."
1061 (interactive)
1062 (save-excursion
1063 (mark-whole-buffer)
1064 (texinfo-update-node t)
1065 (message "Done...updated every node. You may save the buffer.")))
1067 (defun texinfo-update-the-node ()
1068 "Update one node. Point must be at the beginning of node line.
1069 Leave point at the end of the node line."
1070 (texinfo-check-for-node-name)
1071 (texinfo-delete-existing-pointers)
1072 (message "Updating node: %s ... " (texinfo-copy-node-name))
1073 (save-restriction
1074 (widen)
1075 (let*
1076 ((case-fold-search t)
1077 (level (texinfo-hierarchic-level))
1078 (beginning (texinfo-update-menu-region-beginning level))
1079 (end (texinfo-update-menu-region-end level)))
1080 (if (string-equal level "top")
1081 (texinfo-top-pointer-case)
1082 ;; else
1083 (texinfo-insert-pointer beginning end level 'next)
1084 (texinfo-insert-pointer beginning end level 'previous)
1085 (texinfo-insert-pointer beginning end level 'up)
1086 (texinfo-clean-up-node-line)))))
1088 (defun texinfo-top-pointer-case ()
1089 "Insert pointers in the Top node. This is a special case.
1091 The `Next' pointer is a pointer to a chapter or section at a lower
1092 hierarchical level in the file. The `Previous' and `Up' pointers are
1093 to `(dir)'. Point must be at the beginning of the node line, and is
1094 left at the end of the node line."
1096 (texinfo-clean-up-node-line)
1097 (insert ", "
1098 (save-excursion
1099 ;; There may be an @chapter or other such command between
1100 ;; the top node line and the next node line, as a title
1101 ;; for an `ifinfo' section. This @chapter command must
1102 ;; must be skipped. So the procedure is to search for
1103 ;; the next `@node' line, and then copy its name.
1104 (if (re-search-forward "^@node" nil t)
1105 (progn
1106 (beginning-of-line)
1107 (texinfo-copy-node-name))
1108 " "))
1109 ", (dir), (dir)"))
1111 (defun texinfo-check-for-node-name ()
1112 "Determine whether the node has a node name. Prompt for one if not.
1113 Point must be at beginning of node line. Does not move point."
1114 (save-excursion
1115 (forward-word 1) ; skip over node command
1116 (skip-chars-forward " \t") ; and over spaces
1117 (if (not (looking-at "[^,\t\n ]+")) ; regexp based on what info looks for
1118 ; alternatively, use "[a-zA-Z]+"
1119 (let ((node-name (read-from-minibuffer "Node name: ")))
1120 (insert " " node-name)))))
1122 (defun texinfo-delete-existing-pointers ()
1123 "Delete `Next', `Previous', and `Up' pointers.
1124 Starts from the current position of the cursor, and searches forward
1125 on the line for a comma and if one is found, deletes the rest of the
1126 line, including the comma. Leaves point at beginning of line."
1127 (if (search-forward "," (save-excursion (end-of-line) (point)) t)
1128 (progn
1129 (goto-char (1- (point)))
1130 (kill-line nil)))
1131 (beginning-of-line))
1133 (defun texinfo-find-pointer (beginning end level direction)
1134 "Move point to section associated with next, previous, or up pointer.
1135 Return type of pointer (either 'normal or 'no-pointer).
1137 The first and second arguments bound the search for a pointer to the
1138 beginning and end, respectively, of the enclosing higher level
1139 section. The third argument is a string specifying the general kind
1140 of section such as \"chapter\ or \"section\". When looking for the
1141 `Next' pointer, the section found will be at the same hierarchical
1142 level in the Texinfo file; when looking for the `Previous' pointer,
1143 the section found will be at the same or higher hierarchical level in
1144 the Texinfo file; when looking for the `Up' pointer, the section found
1145 will be at some level higher in the Texinfo file. The fourth argument
1146 \(one of 'next, 'previous, or 'up\) specifies whether to find the
1147 `Next', `Previous', or `Up' pointer."
1149 (cond ((eq direction 'next)
1150 (forward-line 3) ; skip over current node
1151 (if (re-search-forward
1152 (eval
1153 (cdr (assoc level texinfo-update-menu-same-level-regexps)))
1156 'normal
1157 'no-pointer))
1158 ((eq direction 'previous)
1159 (if (re-search-backward
1160 (concat
1161 "\\("
1162 (eval
1163 (cdr (assoc level texinfo-update-menu-same-level-regexps)))
1164 "\\|"
1165 (eval
1166 (cdr (assoc level texinfo-update-menu-higher-regexps)))
1167 "\\)")
1168 beginning
1170 'normal
1171 'no-pointer))
1172 ((eq direction 'up)
1173 (if (re-search-backward
1174 (eval (cdr (assoc level texinfo-update-menu-higher-regexps)))
1175 (save-excursion
1176 (goto-char beginning)
1177 (beginning-of-line)
1178 (point))
1180 'normal
1181 'no-pointer))
1183 (error "texinfo-find-pointer: lack proper arguments"))))
1185 (defun texinfo-pointer-name (kind)
1186 "Return the node name preceding the section command.
1187 The argument is the kind of section, either normal or no-pointer."
1188 (let (name)
1189 (cond ((eq kind 'normal)
1190 (end-of-line) ; this handles prev node top case
1191 (re-search-backward ; when point is already
1192 "^@node" ; at the beginning of @node line
1193 (save-excursion (forward-line -3))
1195 (setq name (texinfo-copy-node-name)))
1196 ((eq kind 'no-pointer)
1197 (setq name " "))) ; put a blank in the pointer slot
1198 name))
1200 (defun texinfo-insert-pointer (beginning end level direction)
1201 "Insert the `Next', `Previous' or `Up' node name at point.
1202 Move point forward.
1204 The first and second arguments bound the search for a pointer to the
1205 beginning and end, respectively, of the enclosing higher level
1206 section. The third argument is the hierarchical level of the Texinfo
1207 file, a string such as \"section\". The fourth argument is direction
1208 towards which the pointer is directed, one of `next, `previous, or
1209 'up."
1211 (end-of-line)
1212 (insert
1213 ", "
1214 (save-excursion
1215 (texinfo-pointer-name
1216 (texinfo-find-pointer beginning end level direction)))))
1218 (defun texinfo-clean-up-node-line ()
1219 "Remove extra commas, if any, at end of node line."
1220 (end-of-line)
1221 (skip-chars-backward ", ")
1222 (delete-region (point) (save-excursion (end-of-line) (point))))
1225 ;;;; Updating nodes sequentially
1226 ; These sequential update functions insert `Next' or `Previous'
1227 ; pointers that point to the following or preceding nodes even if they
1228 ; are at higher or lower hierarchical levels. This means that if a
1229 ; section contains one or more subsections, the section's `Next'
1230 ; pointer will point to the subsection and not the following section.
1231 ; (The subsection to which `Next' points will most likely be the first
1232 ; item on the section's menu.)
1234 (defun texinfo-sequential-node-update (&optional region-p)
1235 "Update one node (or many) in a Texinfo file with sequential pointers.
1237 This function causes the `Next' or `Previous' pointer to point to the
1238 immediately preceding or following node, even if it is at a higher or
1239 lower hierarchical level in the document. Continually pressing `n' or
1240 `p' takes you straight through the file.
1242 Without any prefix argument, update the node in which point is located.
1243 Non-nil argument (prefix, if interactive) means update the nodes in the
1244 marked region.
1246 This command makes it awkward to navigate among sections and
1247 subsections; it should be used only for those documents that are meant
1248 to be read like a novel rather than a reference, and for which the
1249 Info `g*' command is inadequate."
1251 (interactive "P")
1252 (if (not region-p)
1253 (let ((auto-fill-function nil)) ; update a single node
1254 (if (not (re-search-backward "^@node" (point-min) t))
1255 (error "Node line not found before this position."))
1256 (texinfo-sequentially-update-the-node)
1257 (message
1258 "Done...sequentially updated the node . You may save the buffer."))
1259 ;; else
1260 (let ((auto-fill-function nil)
1261 (beginning (region-beginning))
1262 (end (region-end)))
1263 (if (= end beginning)
1264 (error "Please mark a region!"))
1265 (save-restriction
1266 (narrow-to-region beginning end)
1267 (goto-char beginning)
1268 (push-mark)
1269 (while (re-search-forward "^@node" (point-max) t)
1270 (beginning-of-line)
1271 (texinfo-sequentially-update-the-node))
1272 (message
1273 "Done...updated the nodes in sequence. You may save the buffer.")))))
1275 (defun texinfo-sequentially-update-the-node ()
1276 "Update one node such that the pointers are sequential.
1277 A `Next' or `Previous' pointer points to any preceding or following node,
1278 regardless of its hierarchical level."
1280 (texinfo-check-for-node-name)
1281 (texinfo-delete-existing-pointers)
1282 (message
1283 "Sequentially updating node: %s ... " (texinfo-copy-node-name))
1284 (save-restriction
1285 (widen)
1286 (let*
1287 ((case-fold-search t)
1288 (level (texinfo-hierarchic-level)))
1289 (if (string-equal level "top")
1290 (texinfo-top-pointer-case)
1291 ;; else
1292 (texinfo-sequentially-insert-pointer level 'next)
1293 (texinfo-sequentially-insert-pointer level 'previous)
1294 (texinfo-sequentially-insert-pointer level 'up)
1295 (texinfo-clean-up-node-line)))))
1297 (defun texinfo-sequentially-find-pointer (level direction)
1298 "Find next or previous pointer sequentially in Texinfo file, or up pointer.
1299 Move point to section associated with the pointer. Find point even if
1300 it is in a different section.
1302 Return type of pointer (either 'normal or 'no-pointer).
1304 The first argument is a string specifying the general kind of section
1305 such as \"chapter\ or \"section\". The section found will be at the
1306 same hierarchical level in the Texinfo file, or, in the case of the up
1307 pointer, some level higher. The second argument (one of 'next,
1308 'previous, or 'up) specifies whether to find the `Next', `Previous',
1309 or `Up' pointer."
1311 (cond ((eq direction 'next)
1312 (forward-line 3) ; skip over current node
1313 (if (re-search-forward
1314 texinfo-section-types-regexp
1315 (point-max)
1317 'normal
1318 'no-pointer))
1319 ((eq direction 'previous)
1320 (if (re-search-backward
1321 texinfo-section-types-regexp
1322 (point-min)
1324 'normal
1325 'no-pointer))
1326 ((eq direction 'up)
1327 (if (re-search-backward
1328 (eval (cdr (assoc level texinfo-update-menu-higher-regexps)))
1329 beginning
1331 'normal
1332 'no-pointer))
1334 (error "texinfo-sequential-find-pointer: lack proper arguments"))))
1336 (defun texinfo-sequentially-insert-pointer (level direction)
1337 "Insert the `Next', `Previous' or `Up' node name at point.
1338 Move point forward.
1340 The first argument is the hierarchical level of the Texinfo file, a
1341 string such as \"section\". The second argument is direction, one of
1342 `next, `previous, or 'up."
1344 (end-of-line)
1345 (insert
1346 ", "
1347 (save-excursion
1348 (texinfo-pointer-name
1349 (texinfo-sequentially-find-pointer level direction)))))
1352 ;;;; Inserting `@node' lines
1353 ; The `texinfo-insert-node-lines' function inserts `@node' lines as needed
1354 ; before the `@chapter', `@section', and such like lines of a region
1355 ; in a Texinfo file.
1357 (defun texinfo-insert-node-lines (&optional title-p)
1358 "Insert missing `@node' lines in region of Texinfo file.
1359 Non-nil argument (prefix, if interactive) means also to insert the
1360 section titles as node names; and also to insert the section titles as
1361 node names in pre-existing @node lines that lack names."
1362 (interactive "P")
1363 (save-excursion
1364 (let ((begin-region (region-beginning))
1365 (end-region (region-end)))
1366 (goto-char begin-region)
1367 (while (< (point) end-region)
1368 (re-search-forward texinfo-section-types-regexp nil 'end)
1369 ;; copy title, since most often, we will need it
1370 (let ((title
1371 (progn
1372 (beginning-of-line)
1373 (forward-word 1)
1374 (skip-chars-forward " \t")
1375 (buffer-substring
1376 (point)
1377 (save-excursion (end-of-line) (point))))))
1378 ;; insert a node if necessary
1379 (if (re-search-backward
1380 "^@node"
1381 (save-excursion
1382 (forward-line -3)
1383 (point))
1385 ;; @node present, and point at beginning of that line
1386 (forward-word 1)
1387 ;; else @node missing, insert one
1388 (progn
1389 (beginning-of-line) ; beginning of `@section' line
1390 (insert "@node\n")
1391 (backward-char 1))) ; leave point just after `@node'
1392 ;; insert a title if warranted
1393 (if title-p
1394 (progn
1395 (skip-chars-forward " \t")
1396 ;; use regexp based on what info looks for
1397 ;; (alternatively, use "[a-zA-Z]+")
1398 (if (not (looking-at "[^,\t\n ]+"))
1399 (progn
1400 (beginning-of-line)
1401 (forward-word 1)
1402 (insert " " title)
1403 (message "Inserted title %s ... " title)))))
1404 ;; in any case, go forward beyond current section title
1405 (forward-line 3)))))
1406 (if title-p
1407 (message
1408 "Done inserting node lines and titles. You may save the buffer.")
1409 (message "Done inserting node lines. You may save the buffer.")))
1412 ;;;; Update and create menus for multi-file Texinfo sources
1414 ;; 1. M-x texinfo-multiple-files-update
1416 ;; Read the include file list of an outer Texinfo file and
1417 ;; update all highest level nodes in the files listed and insert a
1418 ;; main menu in the outer file after its top node.
1420 ;; 2. C-u M-x texinfo-multiple-files-update
1422 ;; Same as 1, but insert a master menu. (Saves reupdating lower
1423 ;; level menus and nodes.) This command simply reads every menu,
1424 ;; so if the menus are wrong, the master menu will be wrong.
1425 ;; Similarly, if the lower level node pointers are wrong, they
1426 ;; will stay wrong.
1428 ;; 3. C-u 2 M-x texinfo-multiple-files-update
1430 ;; Read the include file list of an outer Texinfo file and
1431 ;; update all nodes and menus in the files listed and insert a
1432 ;; master menu in the outer file after its top node.
1434 ;;; Note: these functions:
1436 ;;; * Do not save or delete any buffers. You may fill up your memory.
1437 ;;; * Do not handle any pre-existing nodes in outer file.
1438 ;;; Hence, you may need a file for indices.
1441 ;;;; Auxiliary functions for multiple file updating
1443 (defun texinfo-multi-file-included-list (outer-file)
1444 "Return a list of the included files in OUTER-FILE."
1445 (let ((included-file-list (list outer-file))
1446 start)
1447 (save-excursion
1448 (switch-to-buffer (find-file-noselect outer-file))
1449 (widen)
1450 (goto-char (point-min))
1451 (while (re-search-forward "^@include" nil t)
1452 (skip-chars-forward " \t")
1453 (setq start (point))
1454 (end-of-line)
1455 (skip-chars-backward " \t")
1456 (setq included-file-list
1457 (cons (buffer-substring start (point))
1458 included-file-list)))
1459 (nreverse included-file-list))))
1461 (defun texinfo-copy-next-section-title ()
1462 "Return the name of the immediately following section as a string.
1464 Start with point at the beginning of the node line. Leave point at the
1465 same place. If there is no title, returns an empty string."
1467 (save-excursion
1468 (end-of-line)
1469 (let ((section-end (or
1470 (save-excursion
1471 (re-search-forward "\\(^@node\\)" nil t)
1472 (match-beginning 0))
1473 (point-max))))
1474 (if (re-search-forward texinfo-section-types-regexp section-end t)
1475 ;; copy title
1476 (let ((title
1477 (buffer-substring
1478 (progn (forward-word 1) ; skip over section type
1479 (skip-chars-forward " \t") ; and over spaces
1480 (point))
1481 (progn (end-of-line) (point)))))
1482 title)
1483 ""))))
1485 (defun texinfo-multi-file-update (files &optional update-everything)
1486 "Update first node pointers in each file in FILES.
1487 Return a list of the node names and the title immediate following them.
1489 The first file in the list is an outer file; the remaining are
1490 files included in the outer file with `@include' commands.
1492 If optional arg UPDATE-EVERYTHING non-nil, update every menu and
1493 pointer in each of the included files.
1495 Also update the `Top' level node pointers of the outer file.
1497 Requirements:
1499 * the first file in the FILES list must be the outer file,
1500 * each of the included files must contain exactly one highest
1501 hierarchical level node,
1502 * this node must be the first node in the included file,
1503 * each highest hierarchical level node must be of the same type.
1505 Thus, normally, each included file contains one, and only one,
1506 chapter.
1508 The menu-list has the form:
1510 \(\(\"node-name1\" . \"title1\"\)
1511 \(\"node-name2\" . \"title2\"\) ... \)
1513 However, there does not need to be a title field."
1515 (let (menu-list)
1517 ;; Find the name of the first node of the first included file.
1518 (switch-to-buffer (find-file-noselect (car (cdr files))))
1519 (widen)
1520 (goto-char (point-min))
1521 (if (not (re-search-forward "^@node" nil t))
1522 (error "No `@node' line found in %s !" (buffer-name)))
1523 (beginning-of-line)
1524 (texinfo-check-for-node-name)
1525 (setq next-node-name (texinfo-copy-node-name))
1527 (setq menu-list
1528 (cons (cons
1529 next-node-name
1530 (texinfo-copy-next-section-title))
1531 menu-list))
1533 ;; Go to outer file
1534 (switch-to-buffer (find-file-noselect (car files)))
1535 (goto-char (point-min))
1536 (if (not (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t))
1537 (error "This buffer needs a Top node!"))
1538 (beginning-of-line)
1539 (texinfo-delete-existing-pointers)
1540 (end-of-line)
1541 (insert ", " next-node-name ", (dir), (dir)")
1542 (beginning-of-line)
1543 (setq previous-node-name "Top")
1544 (setq files (cdr files))
1546 (while files
1548 (if (not (cdr files))
1549 ;; No next file
1550 (setq next-node-name "")
1551 ;; Else,
1552 ;; find the name of the first node in the next file.
1553 (switch-to-buffer (find-file-noselect (car (cdr files))))
1554 (widen)
1555 (goto-char (point-min))
1556 (if (not (re-search-forward "^@node" nil t))
1557 (error "No `@node' line found in %s !" (buffer-name)))
1558 (beginning-of-line)
1559 (texinfo-check-for-node-name)
1560 (setq next-node-name (texinfo-copy-node-name))
1561 (setq menu-list
1562 (cons (cons
1563 next-node-name
1564 (texinfo-copy-next-section-title))
1565 menu-list)))
1567 ;; Go to node to be updated.
1568 (switch-to-buffer (find-file-noselect (car files)))
1569 (goto-char (point-min))
1570 (if (not (re-search-forward "^@node" nil t))
1571 (error "No `@node' line found in %s !" (buffer-name)))
1572 (beginning-of-line)
1573 (texinfo-delete-existing-pointers)
1574 (end-of-line)
1575 (insert ", " next-node-name ", " previous-node-name ", " up-node-name)
1577 (beginning-of-line)
1578 (setq previous-node-name (texinfo-copy-node-name))
1580 ;; Update other menus and nodes if requested.
1581 (if update-everything (texinfo-all-menus-update t))
1583 (setq files (cdr files)))
1584 (nreverse menu-list)))
1586 (defun texinfo-multi-files-insert-main-menu (menu-list)
1587 "Insert formatted main menu at point.
1588 Indents the first line of the description, if any, to the value of
1589 texinfo-column-for-description."
1591 (insert "@menu\n")
1592 (while menu-list
1593 (if (cdr (car menu-list)) ; menu-list has description entry
1594 (progn
1595 (insert
1596 (format "* %s::" (car (car menu-list)))) ; node-name entry
1597 (indent-to texinfo-column-for-description 2)
1598 (insert
1599 (format "%s\n" (cdr (car menu-list))))) ; description entry
1600 ;; else menu-list lacks description entry
1601 (insert
1602 (format "* %s::\n" (car (car menu-list))))) ; node-name entry
1603 (setq menu-list (cdr menu-list)))
1604 (insert "@end menu"))
1607 (defun texinfo-multi-file-master-menu-list (files-list)
1608 "Return master menu list from files in FILES-LIST.
1609 Menu entries in each file collected using `texinfo-master-menu-list'.
1611 The first file in FILES-LIST must be the outer file; the others must
1612 be the files included within it. A main menu must already exist."
1613 (save-excursion
1614 (let (master-menu-list)
1615 (while files-list
1616 (switch-to-buffer (find-file-noselect (car files-list)))
1617 (message "Working on: %s " (current-buffer))
1618 (goto-char (point-min))
1619 (setq master-menu-list
1620 (append master-menu-list (texinfo-master-menu-list)))
1621 (setq files-list (cdr files-list)))
1622 master-menu-list)))
1625 ;;;; The multiple-file update function
1627 (defun texinfo-multiple-files-update
1628 (outer-file &optional update-everything make-master-menu)
1629 "Update first node pointers in each file included in OUTER-FILE;
1630 create or update main menu in the outer file that refers to such nodes.
1631 This does not create or update menus or pointers within the included files.
1633 With optional MAKE-MASTER-MENU argument (prefix arg, if interactive),
1634 insert a master menu in OUTER-FILE. This does not create or update
1635 menus or pointers within the included files.
1637 With optional UPDATE-EVERYTHING argument (numeric prefix arg, if
1638 interactive), update all the menus and all the `Next', `Previous', and
1639 `Up' pointers of all the files included in OUTER-FILE before inserting
1640 a master menu in OUTER-FILE.
1642 The command also updates the `Top' level node pointers of OUTER-FILE.
1644 Notes:
1646 * this command does NOT save any files--you must save the
1647 outer file and any modified, included files.
1649 * except for the `Top' node, this command does NOT handle any
1650 pre-existing nodes in the outer file; hence, indices must be
1651 enclosed in an included file.
1653 Requirements:
1655 * each of the included files must contain exactly one highest
1656 hierarchical level node,
1657 * this highest node must be the first node in the included file,
1658 * each highest hierarchical level node must be of the same type.
1660 Thus, normally, each included file contains one, and only one,
1661 chapter."
1663 (interactive "fName of outer `include' file: ")
1665 (cond (current-prefix-arg
1666 (setq make-master-menu (listp current-prefix-arg))
1667 (setq update-everything (numberp current-prefix-arg))))
1669 (let* ((included-file-list (texinfo-multi-file-included-list outer-file))
1670 (files included-file-list)
1671 main-menu-list
1672 next-node-name
1673 previous-node-name
1674 (up-node-name "Top"))
1676 ;;; Update the pointers
1677 ;;; and collect the names of the nodes and titles
1678 (setq main-menu-list (texinfo-multi-file-update files update-everything))
1680 ;;; Insert main menu
1682 ;; Go to outer file
1683 (switch-to-buffer (find-file-noselect (car included-file-list)))
1684 (if (texinfo-old-menu-p
1685 (point-min)
1686 (save-excursion
1687 (re-search-forward "^@include")
1688 (beginning-of-line)
1689 (point)))
1691 ;; If found, leave point after word `menu' on the `@menu' line.
1692 (progn
1693 (texinfo-incorporate-descriptions main-menu-list)
1694 ;; Delete existing menu.
1695 (beginning-of-line)
1696 (delete-region
1697 (point)
1698 (save-excursion (re-search-forward "^@end menu") (point)))
1699 ;; Insert main menu
1700 (texinfo-multi-files-insert-main-menu main-menu-list))
1702 ;; Else no current menu; insert it before `@include'
1703 (texinfo-multi-files-insert-main-menu main-menu-list))
1705 ;;; Insert master menu
1707 (if make-master-menu
1708 (progn
1709 ;; First, removing detailed part of any pre-existing master menu
1710 (goto-char (point-min))
1711 (if (re-search-forward texinfo-master-menu-header nil t)
1712 ;; Remove detailed master menu listing
1713 (progn
1714 (goto-char (match-beginning 0))
1715 (let ((end-of-detailed-menu-descriptions
1716 (save-excursion ; beginning of end menu line
1717 (goto-char (texinfo-menu-end))
1718 (beginning-of-line) (forward-char -1)
1719 (point))))
1720 (delete-region (point) end-of-detailed-menu-descriptions))))
1722 ;; Create a master menu and insert it
1723 (texinfo-insert-master-menu-list
1724 (texinfo-multi-file-master-menu-list
1725 included-file-list)))))
1726 (message "Multiple files updated."))
1728 (provide 'texnfo-upd)
1730 ;;; texnfo-upd.el ends here