Initial Commit
[packages] / xemacs-packages / texinfo / texnfo-upd.el
1 ;;; texnfo-upd.el --- utilities for updating nodes and menus in Texinfo files
2
3 ;; Copyright (C) 1989, 1990, 1991, 1992, 2001, 2002 Free Software Foundation, Inc.
4
5 ;; Author: Robert J. Chassell
6 ;; Maintainer: bug-texinfo@gnu.org
7 ;; Keywords: maint, tex, docs
8
9 ;; This file is part of XEmacs.
10
11 ;; XEmacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; XEmacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Synched up with: FSF 21.3.
27
28 ;;; Commentary:
29
30 ;; Known bug: update commands fail to ignore @ignore.
31
32 ;; Summary: how to use the updating commands
33
34 ;; The node and menu updating functions automatically
35
36 ;;   * insert missing `@node' lines,
37 ;;   * insert the `Next', `Previous' and `Up' pointers of a node,
38 ;;   * insert or update the menu for a section,
39 ;;   * create a master menu for a Texinfo source file.
40 ;;
41 ;; With a prefix argument, the `texinfo-update-node' and
42 ;; `texinfo-make-menu' functions do their jobs in the region.
43 ;;
44 ;; In brief, the functions for creating or updating nodes and menus, are:
45 ;;
46 ;;     texinfo-update-node (&optional beginning end)
47 ;;     texinfo-every-node-update ()
48 ;;     texinfo-sequential-node-update (&optional region-p)
49 ;;
50 ;;     texinfo-make-menu (&optional beginning end)
51 ;;     texinfo-all-menus-update ()
52 ;;     texinfo-master-menu ()
53 ;;
54 ;;     texinfo-insert-node-lines  (&optional title-p)
55 ;;
56 ;;     texinfo-indent-menu-description (column &optional region-p)
57
58 ;; The `texinfo-column-for-description' variable specifies the column to
59 ;; which menu descriptions are indented.
60
61 ;; Texinfo file structure
62 ;; ----------------------
63
64 ;; To use the updating commands, you must structure your Texinfo file
65 ;; hierarchically.  Each `@node' line, with the exception of the top
66 ;; node, must be accompanied by some kind of section line, such as an
67 ;; `@chapter' or `@section' line.  Each node-line/section-line
68 ;; combination must look like this:
69
70 ;;      @node    Lists and Tables, Cross References, Structuring, Top
71 ;;      @comment node-name,        next,             previous,    up
72 ;;      @chapter Making Lists and Tables
73
74 ;; or like this (without the `@comment' line):
75
76 ;;      @node    Lists and Tables, Cross References, Structuring, Top
77 ;;      @chapter Making Lists and Tables
78
79 ;; If the file has a `top' node, it must be called `top' or `Top' and
80 ;; be the first node in the file.
81
82 \f
83 ;;; The update node functions described in detail
84
85 ;; The `texinfo-update-node' command with no prefix argument inserts
86 ;; the correct next, previous and up pointers for the node in which
87 ;; point is located (i.e., for the node preceding point).
88
89 ;; With prefix argument, the `texinfo-update-node' function inserts the
90 ;; correct next, previous and up pointers for the nodes inside the
91 ;; region.
92
93 ;; It does not matter whether the  `@node' line has pre-existing
94 ;; `Next', `Previous', or `Up' pointers in it.  They are removed.
95
96 ;; The `texinfo-every-node-update' function runs `texinfo-update-node'
97 ;; on the whole buffer.
98
99 ;; The `texinfo-sequential-node-update' function inserts the
100 ;; immediately following and preceding node into the `Next' or
101 ;; `Previous' pointers regardless of their hierarchical level.  This is
102 ;; only useful for certain kinds of text, like a novel, which you go
103 ;; through sequentially.
104
105 \f
106 ;;; The menu making functions described in detail
107
108 ;; The `texinfo-make-menu' function without an argument creates or
109 ;; updates a menu for the section encompassing the node that follows
110 ;; point.  With an argument, it makes or updates menus for the nodes
111 ;; within or part of the marked region.
112
113 ;; Whenever an existing menu is updated, the descriptions from
114 ;; that menu are incorporated into the new menu.  This is done by copying
115 ;; descriptions from the existing menu to the entries in the new menu
116 ;; that have the same node names.  If the node names are different, the
117 ;; descriptions are not copied to the new menu.
118
119 ;; Menu entries that refer to other Info files are removed since they
120 ;; are not a node within current buffer.  This is a deficiency.
121
122 ;; The `texinfo-all-menus-update' function runs `texinfo-make-menu'
123 ;; on the whole buffer.
124
125 ;; The `texinfo-master-menu' function creates an extended menu located
126 ;; after the top node.  (The file must have a top node.)  The function
127 ;; first updates all the regular menus in the buffer (incorporating the
128 ;; descriptions from pre-existing menus), and then constructs a master
129 ;; menu that includes every entry from every other menu.  (However, the
130 ;; function cannot update an already existing master menu; if one
131 ;; exists, it must be removed before calling the function.)
132
133 ;; The `texinfo-indent-menu-description' function indents every
134 ;; description in the menu following point, to the specified column.
135 ;; Non-nil argument (prefix, if interactive) means indent every
136 ;; description in every menu in the region.  This function does not
137 ;; indent second and subsequent lines of a multi-line description.
138
139 ;; The `texinfo-insert-node-lines' function inserts `@node' before the
140 ;; `@chapter', `@section', and such like lines of a region in a Texinfo
141 ;; file where the `@node' lines are missing.
142 ;;
143 ;; With a non-nil argument (prefix, if interactive), the function not
144 ;; only inserts `@node' lines but also inserts the chapter or section
145 ;; titles as the names of the corresponding nodes; and inserts titles
146 ;; as node names in pre-existing `@node' lines that lack names.
147 ;;
148 ;; Since node names should be more concise than section or chapter
149 ;; titles, node names so inserted will need to be edited manually.
150
151 \f
152 ;;; Code:
153
154 (require 'texinfo)
155 (eval-when-compile (require 'regexp-opt))
156
157 (defvar texinfo-master-menu-header
158   " --- The Detailed Node Listing ---\n"
159   "String inserted before lower level entries in Texinfo master menu.
160 It comes after the chapter-level menu entries.")
161
162 ;; We used to look for just sub, but that found @subtitle.
163 (defvar texinfo-section-types-regexp
164   "^@\\(chapter \\|sect\\|subs\\|subh\\|unnum\\|major\\|chapheading \\|heading \\|appendix\\)"
165   "Regexp matching chapter, section, other headings (but not the top node).")
166
167 (defvar texinfo-section-level-regexp
168   (regexp-opt (texinfo-filter 3 texinfo-section-list))
169   "Regular expression matching just the Texinfo section level headings.")
170
171 (defvar texinfo-subsection-level-regexp
172   (regexp-opt (texinfo-filter 4 texinfo-section-list))
173   "Regular expression matching just the Texinfo subsection level headings.")
174
175 (defvar texinfo-subsubsection-level-regexp
176   (regexp-opt (texinfo-filter 5 texinfo-section-list))
177   "Regular expression matching just the Texinfo subsubsection level headings.")
178
179 (defvar texinfo-update-menu-same-level-regexps
180   '((1 . "top[ \t]+")
181     (2 . (concat "\\(^@\\)\\(" texinfo-chapter-level-regexp "\\)\\>[ \t]*"))
182     (3 . (concat "\\(^@\\)\\(" texinfo-section-level-regexp "\\)\\>[ \t]*"))
183     (4 . (concat "\\(^@\\)\\(" texinfo-subsection-level-regexp "\\)\\>[ \t]+"))
184     (5 . (concat "\\(^@\\)\\(" texinfo-subsubsection-level-regexp "\\)\\>[ \t]+")))
185   "*Regexps for searching for same level sections in a Texinfo file.
186 The keys are strings specifying the general hierarchical level in the
187 document; the values are regular expressions.")
188
189 (defvar texinfo-update-menu-higher-regexps
190   '((1 . "^@node [ \t]*DIR")
191     (2 . "^@node [ \t]*top[ \t]*\\(,\\|$\\)")
192     (3 .
193      (concat
194       "\\(^@\\("
195       texinfo-chapter-level-regexp
196       "\\)\\>[ \t]*\\)"))
197     (4 .
198      (concat
199       "\\(^@\\("
200       texinfo-section-level-regexp
201       "\\|"
202       texinfo-chapter-level-regexp
203       "\\)\\>[ \t]*\\)"))
204     (5 .
205      (concat
206       "\\(^@\\("
207       texinfo-subsection-level-regexp
208       "\\|"
209       texinfo-section-level-regexp
210       "\\|"
211       texinfo-chapter-level-regexp
212       "\\)\\>[ \t]*\\)")))
213   "*Regexps for searching for higher level sections in a Texinfo file.
214 The keys are strings specifying the general hierarchical level in the
215 document; the values are regular expressions.")
216
217 (defvar texinfo-update-menu-lower-regexps
218   '((1 .
219      (concat
220       "\\(^@\\("
221       texinfo-chapter-level-regexp
222       "\\|"
223       texinfo-section-level-regexp
224       "\\|"
225       texinfo-subsection-level-regexp
226       "\\|"
227       texinfo-subsubsection-level-regexp
228       "\\)\\>[ \t]*\\)"))
229     (2 .
230      (concat
231       "\\(^@\\("
232       texinfo-section-level-regexp
233       "\\|"
234       texinfo-subsection-level-regexp
235       "\\|"
236       texinfo-subsubsection-level-regexp
237       "\\)\\>[ \t]*\\)"))
238     (3 .
239      (concat
240       "\\(^@\\("
241       texinfo-subsection-level-regexp
242       "\\|"
243       texinfo-subsubsection-level-regexp
244       "\\)\\>[ \t]+\\)"))
245     (4 .
246      (concat
247       "\\(^@\\("
248       texinfo-subsubsection-level-regexp
249       "\\)\\>[ \t]+\\)"))
250     ;; There's nothing below 5, use a bogus regexp that can't match.
251     (5 . "a\\(^\\)"))
252   "*Regexps for searching for lower level sections in a Texinfo file.
253 The keys are strings specifying the general hierarchical level in the
254 document; the values are regular expressions.")
255
256 \f
257 (defun texinfo-make-menu (&optional beginning end)
258   "Without any prefix argument, make or update a menu.
259 Make the menu for the section enclosing the node found following point.
260
261 A prefix argument means make or update menus
262 for nodes within or part of the marked region.
263
264 Whenever a menu exists, and is being updated, the descriptions that
265 are associated with node names in the pre-existing menu are
266 incorporated into the new menu.
267
268 Leaves trailing whitespace in a menu that lacks descriptions, so
269 descriptions will format well.  In general, a menu should contain
270 descriptions, because node names and section titles are often too
271 short to explain a node well."
272
273   (interactive
274    (if prefix-arg
275        (list (point) (mark))))
276   (if (null beginning)
277       (let ((level (texinfo-hierarchic-level)))
278         (texinfo-make-one-menu level)
279         (message "Menu updated"))
280     ;; else
281     (message "Making or updating menus in %s... " (buffer-name))
282     (save-excursion
283       (goto-char (min beginning end))
284       ;; find section type following point
285       (let ((level (texinfo-hierarchic-level))
286             (region-end-marker (make-marker)))
287         (set-marker region-end-marker (max beginning end))
288         (save-restriction
289           (widen)
290
291           (while (texinfo-find-lower-level-node
292                   level (marker-position region-end-marker))
293             (setq level (texinfo-hierarchic-level)) ; new, lower level
294             (texinfo-make-one-menu level))
295
296           (while (and (< (point) (marker-position region-end-marker))
297                       (texinfo-find-higher-level-node
298                        level (marker-position region-end-marker)))
299             (setq level (texinfo-hierarchic-level))
300             ;; Don't allow texinfo-find-higher-level-node
301             ;; to find the same node again.
302             (forward-line 1)
303             (while (texinfo-find-lower-level-node
304                     level (marker-position region-end-marker))
305               (setq level (texinfo-hierarchic-level)) ; new, lower level
306               (texinfo-make-one-menu level))))))
307     (message "Making or updating menus in %s...done" (buffer-name))))
308
309 (defun texinfo-make-one-menu (level)
310   "Make a menu of all the appropriate nodes in this section.
311 `Appropriate nodes' are those associated with sections that are
312 at the level specified by LEVEL.  Point is left at the end of menu."
313   (let*
314       ((case-fold-search t)
315        (beginning
316         (save-excursion
317           (goto-char (texinfo-update-menu-region-beginning level))
318           (end-of-line)
319           (point)))
320        (end (texinfo-update-menu-region-end level))
321        (first (texinfo-menu-first-node beginning end))
322        (node-name (progn
323                     (goto-char beginning)
324                     (beginning-of-line)
325                     (texinfo-copy-node-name)))
326        (new-menu-list (texinfo-make-menu-list beginning end level)))
327     (when (texinfo-old-menu-p beginning first)
328       (texinfo-incorporate-descriptions new-menu-list)
329       (texinfo-incorporate-menu-entry-names new-menu-list)
330       (texinfo-delete-old-menu beginning first))
331     (texinfo-insert-menu new-menu-list node-name)))
332
333 (defun texinfo-all-menus-update (&optional update-all-nodes-p)
334   "Update every regular menu in a Texinfo file.
335 Update pre-existing master menu, if there is one.
336
337 If called with a non-nil argument, this function first updates all the
338 nodes in the buffer before updating the menus.
339
340 Indents the first line of descriptions, and leaves trailing whitespace
341 in a menu that lacks descriptions, so descriptions will format well.
342 In general, a menu should contain descriptions, because node names and
343 section titles are often too short to explain a node well."
344   (interactive "P")
345   (let ((case-fold-search t)
346         master-menu-p)
347     (save-excursion
348       (push-mark (point-max) t)
349       (goto-char (point-min))
350       (message "Checking for a master menu in %s ... "(buffer-name))
351       (save-excursion
352         (when (search-forward texinfo-master-menu-header nil t)
353           ;; Check if @detailmenu kludge is used;
354           ;; if so, leave point before @detailmenu.
355           (search-backward "\n@detailmenu"
356                            (save-excursion (forward-line -3) (point))
357                            t)
358           ;; Remove detailed master menu listing
359           (setq master-menu-p t)
360           (goto-char (match-beginning 0))
361           (let ((end-of-detailed-menu-descriptions
362                  (save-excursion     ; beginning of end menu line
363                    (goto-char (texinfo-menu-end))
364                    (beginning-of-line) (forward-char -1)
365                    (point))))
366             (delete-region (point) end-of-detailed-menu-descriptions))))
367
368       (when update-all-nodes-p
369         (message "Updating all nodes in %s ... " (buffer-name))
370         (texinfo-update-node (point-min) (point-max)))
371
372       (message "Updating all menus in %s ... " (buffer-name))
373       (texinfo-make-menu (point-max) (point-min))
374
375       (when master-menu-p
376         (message "Updating the master menu in %s... " (buffer-name))
377         (texinfo-master-menu nil)))
378
379     (message "Done...updated all the menus.  You may save the buffer.")))
380
381 (defun texinfo-find-lower-level-node (level region-end)
382   "Search forward from point for node at any level lower than LEVEL.
383 Search is limited to the end of the marked region, REGION-END,
384 and to the end of the menu region for the level.
385
386 Return t if the node is found, else nil.  Leave point at the beginning
387 of the node if one is found; else do not move point."
388   (let ((case-fold-search t))
389     (if (and (< (point) region-end)
390              (re-search-forward
391               (concat
392                "\\(^@node\\).*\n"         ; match node line
393                "\\(\\(\\(^@c\\).*\n\\)"   ; match comment line, if any
394                "\\|"                      ; or
395                "\\(^@ifinfo[ ]*\n\\)"     ; ifinfo line, if any
396                "\\|"                      ; or
397                "\\(^@ifnottex[ ]*\n\\)"   ; ifnottex line, if any
398                "\\)?"                     ; end of expression
399                (eval (cdr (assoc level texinfo-update-menu-lower-regexps))))
400               ;; the next higher level node marks the end of this
401               ;; section, and no lower level node will be found beyond
402               ;; this position even if region-end is farther off
403               (texinfo-update-menu-region-end level)
404               t))
405         (goto-char (match-beginning 1)))))
406
407 (defun texinfo-find-higher-level-node (level region-end)
408   "Search forward from point for node at any higher level than argument LEVEL.
409 Search is limited to the end of the marked region, REGION-END.
410
411 Return t if the node is found, else nil.  Leave point at the beginning
412 of the node if one is found; else do not move point.
413
414 A `@node' line starting at point does count as a match;
415 if the match is found there, the value is t and point does not move."
416
417   (let ((case-fold-search t))
418     (cond
419      ((< level 3)
420       (if (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" region-end t)
421           (progn (beginning-of-line) t)))
422      (t
423       (when (re-search-forward
424              (concat
425               "\\(^@node\\).*\n"              ; match node line
426               "\\(\\(\\(^@c\\).*\n\\)"        ; match comment line, if any
427               "\\|"                           ; or
428               "\\(^@ifinfo[ ]*\n\\)"          ; ifinfo line, if any
429               "\\|"                           ; or
430               "\\(^@ifnottex[ ]*\n\\)"        ; ifnottex line, if any
431               "\\)?"                          ; end of expression
432               (eval (cdr (assoc level texinfo-update-menu-higher-regexps))))
433              region-end t)
434         (beginning-of-line) t)))))
435
436 \f
437 ;;; Making the list of new menu entries
438
439 (defun texinfo-make-menu-list (beginning end level)
440   "Make a list of node names and their descriptions.
441 Point is left at the end of the menu region, but the menu is not inserted.
442
443 First argument is position from which to start making menu list;
444 second argument is end of region in which to try to locate entries;
445 third argument is the level of the nodes that are the entries.
446
447 Node names and descriptions are dotted pairs of strings.  Each pair is
448 an element of the list.  If the description does not exist, the
449 element consists only of the node name."
450   (goto-char beginning)
451   (let (new-menu-list)
452     (while (texinfo-menu-locate-entry-p level end)
453       (push (cons
454              (texinfo-copy-node-name)
455              (prog1 "" (forward-line 1)))
456             ;; Use following to insert section titles automatically.
457             ;; (texinfo-copy-section-title))
458             new-menu-list))
459     (nreverse new-menu-list)))
460
461 (defun texinfo-menu-locate-entry-p (level search-end)
462   "Find a node that will be part of menu for this section.
463 First argument is a string such as \"section\" specifying the general
464 hierarchical level of the menu; second argument is a position
465 specifying the end of the search.
466
467 The function returns t if the node is found, else nil.  It searches
468 forward from point, and leaves point at the beginning of the node.
469
470 The function finds entries of the same type.  Thus `subsections' and
471 `unnumberedsubsecs' will appear in the same menu."
472   (let ((case-fold-search t))
473     (if (re-search-forward
474          (concat
475           "\\(^@node\\).*\n"              ; match node line
476           "\\(\\(\\(^@c\\).*\n\\)"        ; match comment line, if any
477           "\\|"                           ; or
478           "\\(^@ifinfo[ ]*\n\\)"          ; ifinfo line, if any
479           "\\|"                           ; or
480           "\\(^@ifnottex[ ]*\n\\)"        ; ifnottex line, if any
481           "\\)?"                          ; end of expression
482           (eval
483            (cdr (assoc level texinfo-update-menu-same-level-regexps))))
484          search-end
485          t)
486         (goto-char (match-beginning 1)))))
487
488 (defun texinfo-copy-node-name ()
489   "Return the node name as a string.
490
491 Start with point at the beginning of the node line; copy the text
492 after the node command up to the first comma on the line, if any, and
493 return the text as a string.  Leaves point at the beginning of the
494 line.  If there is no node name, returns an empty string."
495   (save-excursion
496     (buffer-substring
497      (progn (forward-word 1)              ; skip over node command
498             (skip-chars-forward " \t")    ; and over spaces
499             (point))
500      (if (search-forward "," (point-at-eol) t) ; bound search 
501          (1- (point))
502        (end-of-line) (point)))))
503
504 (defun texinfo-copy-section-title ()
505   "Return the title of the section as a string.
506 The title is used as a description line in the menu when one does not
507 already exist.
508
509 Move point to the beginning of the appropriate section line by going
510 to the start of the text matched by last regexp searched for, which
511 must have been done by `texinfo-menu-locate-entry-p'."
512
513   ;; could use the same re-search as in `texinfo-menu-locate-entry-p'
514   ;; instead of using `match-beginning'; such a variation would be
515   ;; more general, but would waste information already collected
516
517   (goto-char (match-beginning 7))       ; match section name
518
519   (buffer-substring
520    (progn (forward-word 1)              ; skip over section type
521           (skip-chars-forward " \t")    ; and over spaces
522           (point))
523    (progn (end-of-line) (point))))
524
525 \f
526 ;;; Handling the old menu
527
528 (defun texinfo-old-menu-p (beginning first)
529   "Move point to the beginning of the menu for this section, if any.
530 Otherwise move point to the end of the first node of this section.
531 Return t if a menu is found, nil otherwise.
532
533 First argument is the position of the beginning of the section in which
534 the menu will be located; second argument is the position of the first
535 node within the section.
536
537 If no menu is found, the function inserts two newlines just before the
538 end of the section, and leaves point there where a menu ought to be."
539   (goto-char beginning)
540   (if (re-search-forward "^@menu" first 'goto-end)
541       t
542     (insert "\n\n") (forward-line -2) nil))
543
544 (defun texinfo-incorporate-descriptions (new-menu-list)
545   "Copy the old menu line descriptions that exist to the new menu.
546
547 Point must be at beginning of old menu.
548
549 If the node-name of the new menu is found in the old menu, insert the
550 old description into the new entry.
551
552 For this function, the new menu is a list made up of lists of dotted
553 pairs in which the first element of the pair is the node name and the
554 second element the description.  The new menu is changed destructively.
555 The old menu is the menu as it appears in the Texinfo file."
556
557   (let ((end-of-menu (texinfo-menu-end)))
558     (dolist (new-menu new-menu-list new-menu-list)
559       (save-excursion                   ; keep point at beginning of menu
560         (when (re-search-forward
561                ;; Existing nodes can have the form
562                ;;     * NODE NAME:: DESCRIPTION
563                ;; or
564                ;;     * MENU ITEM: NODE NAME.     DESCRIPTION.
565                ;;
566                ;; Recognize both when looking for the description.
567                (concat "\\* \\("        ; so only menu entries are found
568                        (regexp-quote (car new-menu)) "::"
569                        "\\|"
570                        ".*: " (regexp-quote (car new-menu)) "[.,\t\n]"
571                        "\\)"
572                        )                ; so only complete entries are found
573                end-of-menu
574                t)
575           (setcdr new-menu (texinfo-menu-copy-old-description end-of-menu)))))))
576
577 (defun texinfo-incorporate-menu-entry-names (new-menu-list)
578   "Copy any old menu entry names to the new menu.
579
580 Point must be at beginning of old menu.
581
582 If the node-name of the new menu entry cannot be found in the old
583 menu, do nothing.
584
585 For this function, the new menu is a list made up of lists of dotted
586 pairs in which the first element of the pair is the node name and the
587 second element is the description (or nil).
588
589 If we find an existing menu entry name, we change the first element of
590 the pair to be another dotted pair in which the car is the menu entry
591 name and the cdr is the node name.
592
593 NEW-MENU-LIST is changed destructively.  The old menu is the menu as it
594 appears in the texinfo file."
595
596   (let ((end-of-menu (texinfo-menu-end)))
597     (dolist (new-menu new-menu-list new-menu-list)
598       (save-excursion                   ; keep point at beginning of menu
599         (if (re-search-forward
600              ;; Existing nodes can have the form
601              ;;     * NODE NAME:: DESCRIPTION
602              ;; or
603              ;;     * MENU ITEM: NODE NAME.     DESCRIPTION.
604              ;;
605              ;; We're interested in the second case.
606              (concat "\\* "              ; so only menu entries are found
607                      "\\(.*\\): " (regexp-quote (car new-menu))
608                      "[.,\t\n]")
609              end-of-menu
610              t)
611             (setcar
612               new-menu  ; replace the node name
613               (cons (buffer-substring (match-beginning 1) (match-end 1))
614                     (car new-menu))))))))
615
616 (defun texinfo-menu-copy-old-description (end-of-menu)
617   "Return description field of old menu line as string.
618 Point must be located just after the node name.  Point left before description.
619 Single argument, END-OF-MENU, is position limiting search."
620   (skip-chars-forward "[:.,\t\n ]+")
621   ;; don't copy a carriage return at line beginning with asterisk!
622   ;; do copy a description that begins with an `@'!
623   ;; !! Known bug: does not copy descriptions starting with ^|\{?* etc.
624   (if (and (looking-at "\\(\\w+\\|@\\)")
625            (not (looking-at "\\(^\\* \\|^@end menu\\)")))
626       (buffer-substring
627        (point)
628        (save-excursion
629          (re-search-forward "\\(^\\* \\|^@end menu\\)" end-of-menu t)
630          (forward-line -1)
631          (end-of-line)                  ; go to end of last description line
632          (point)))
633     ""))
634
635 (defun texinfo-menu-end ()
636   "Return position of end of menu, but don't move point.
637 Signal an error if not end of menu."
638   (save-excursion
639     (if (re-search-forward "^@end menu" nil t)
640         (point)
641       (error "Menu does not have an end"))))
642
643 (defun texinfo-delete-old-menu (beginning first)
644   "Delete the old menu.  Point must be in or after menu.
645 First argument is position of the beginning of the section in which
646 the menu will be located; second argument is the position of the first
647 node within the section."
648   ;; No third arg to search, so error if search fails.
649   (re-search-backward "^@menu" beginning)
650   (delete-region (point)
651                  (save-excursion
652                    (re-search-forward "^@end menu" first)
653                    (point))))
654
655 \f
656 ;;; Inserting new menu
657
658 ;; try 32, but perhaps 24 is better
659 (defvar texinfo-column-for-description 32
660   "*Column at which descriptions start in a Texinfo menu.")
661
662 (defun texinfo-insert-menu (menu-list node-name)
663   "Insert formatted menu at point.
664 Indents the first line of descriptions, if any, to the value of
665 texinfo-column-for-description.  Indenting leaves trailing whitespace
666 in a menu that lacks descriptions, so descriptions will format well.
667 In general, a menu should contain descriptions, because node names and
668 section titles are often too short to explain a node well.
669
670 MENU-LIST has form:
671
672     \(\(\"node-name1\" . \"description\"\)
673     \(\"node-name2\" . \"description\"\) ... \)
674
675 However, the description field might be nil.
676
677 Also, the node-name field might itself be a dotted pair (call it P) of
678 strings instead of just a string.  In that case, the car of P
679 is the menu entry name, and the cdr of P is the node name."
680
681   (insert "@menu\n")
682   (dolist (menu menu-list)
683     ;; Every menu entry starts with a star and a space.
684     (insert "* ")
685
686     ;; Insert the node name (and menu entry name, if present).
687     (let ((node-part (car menu)))
688       (if (stringp node-part)
689           ;; "Double colon" entry line; menu entry and node name are the same,
690           (insert (format "%s::" node-part))
691         ;; "Single colon" entry line; menu entry and node name are different.
692         (insert (format "%s: %s." (car node-part) (cdr node-part)))))
693
694     ;; Insert the description, if present.
695     (when (cdr menu)
696       ;; Move to right place.
697       (indent-to texinfo-column-for-description 2)
698       ;; Insert description.
699       (insert (format "%s" (cdr menu))))
700
701     (insert "\n")) ; end this menu entry
702   (insert "@end menu")
703   (let ((level (texinfo-hierarchic-level)))
704     (message
705      "Updated level \"%s\" menu following node: %s ... " level node-name)))
706
707 \f
708 ;;; Starting menu descriptions by inserting titles
709
710 (defun texinfo-start-menu-description ()
711   "In this menu entry, insert the node's section title as a description.
712 Position point at beginning of description ready for editing.
713 Do not insert a title if the line contains an existing description.
714
715 You will need to edit the inserted text since a useful description
716 complements the node name rather than repeats it as a title does."
717
718   (interactive)
719   (let (beginning end node-name title)
720     (save-excursion
721       (beginning-of-line)
722       (if (search-forward "* " (save-excursion (end-of-line) (point)) t)
723           (progn (skip-chars-forward " \t")
724                  (setq beginning (point)))
725         (error "This is not a line in a menu"))
726
727       (cond
728        ;; "Double colon" entry line; menu entry and node name are the same,
729        ((search-forward "::" (save-excursion (end-of-line) (point)) t)
730         (if (looking-at "[ \t]*[^ \t\n]+")
731             (error "Descriptive text already exists"))
732         (skip-chars-backward ": \t")
733         (setq node-name (buffer-substring beginning (point))))
734
735        ;; "Single colon" entry line; menu entry and node name are different.
736        ((search-forward ":" (save-excursion (end-of-line) (point)) t)
737         (skip-chars-forward " \t")
738         (setq beginning (point))
739         ;; Menu entry line ends in a period, comma, or tab.
740         (if (re-search-forward "[.,\t]"
741                                (save-excursion (forward-line 1) (point)) t)
742             (progn
743               (if (looking-at "[ \t]*[^ \t\n]+")
744                   (error "Descriptive text already exists"))
745               (skip-chars-backward "., \t")
746               (setq node-name (buffer-substring beginning (point))))
747           ;; Menu entry line ends in a return.
748           (re-search-forward ".*\n"
749                              (save-excursion (forward-line 1) (point)) t)
750           (skip-chars-backward " \t\n")
751           (setq node-name (buffer-substring beginning (point)))
752           (if (= 0 (length node-name))
753               (error "No node name on this line")
754             (insert "."))))
755        (t (error "No node name on this line")))
756       ;; Search for node that matches node name, and copy the section title.
757       (if (re-search-forward
758            (concat
759             "^@node[ \t]+"
760             (regexp-quote node-name)
761             ".*\n"                      ; match node line
762             "\\("
763             "\\(\\(^@c \\|^@comment\\).*\n\\)" ; match comment line, if any
764             "\\|"                              ; or
765             "\\(^@ifinfo[ ]*\n\\)"             ; ifinfo line, if any
766             "\\|"                              ; or
767             "\\(^@ifnottex[ ]*\n\\)"           ; ifnottex line, if any
768             "\\)?"                             ; end of expression
769             )
770            nil t)
771           (setq title
772                 (buffer-substring
773                  ;; skip over section type
774                  (progn (forward-word 1)
775                         ;; and over spaces
776                         (skip-chars-forward " \t")
777                         (point))
778                  (progn (end-of-line)
779                         (skip-chars-backward " \t")
780                         (point))))
781         (error "Cannot find node to match node name in menu entry")))
782     ;; Return point to the menu and insert the title.
783     (end-of-line)
784     (delete-region
785      (point)
786      (save-excursion (skip-chars-backward " \t") (point)))
787     (indent-to texinfo-column-for-description 2)
788     (save-excursion (insert title))))
789
790 \f
791 ;;; Handling description indentation
792
793 ;; Since the make-menu functions indent descriptions, these functions
794 ;; are useful primarily for indenting a single menu specially.
795
796 (defun texinfo-indent-menu-description (column &optional region-p)
797   "Indent every description in menu following point to COLUMN.
798 Non-nil argument (prefix, if interactive) means indent every
799 description in every menu in the region.  Does not indent second and
800 subsequent lines of a multi-line description."
801
802   (interactive
803    "nIndent menu descriptions to (column number): \nP")
804   (save-excursion
805     (save-restriction
806       (widen)
807       (if (not region-p)
808           (progn
809             (re-search-forward "^@menu")
810             (texinfo-menu-indent-description column)
811             (message
812              "Indented descriptions in menu.  You may save the buffer."))
813         ;;else
814         (message "Indenting every menu description in region... ")
815         (goto-char (region-beginning))
816         (while (and (< (point) (region-end))
817                     (texinfo-locate-menu-p))
818           (forward-line 1)
819           (texinfo-menu-indent-description column))
820         (message "Indenting done.  You may save the buffer.")))))
821
822 (defun texinfo-menu-indent-description (to-column-number)
823   "Indent the Texinfo file menu description to TO-COLUMN-NUMBER.
824 Start with point just after the word `menu' in the `@menu' line and
825 leave point on the line before the `@end menu' line.  Does not indent
826 second and subsequent lines of a multi-line description."
827   (let* ((beginning-of-next-line (point)))
828     (while (< beginning-of-next-line
829               (save-excursion           ; beginning of end menu line
830                 (goto-char (texinfo-menu-end))
831                 (beginning-of-line)
832                 (point)))
833
834       (when (re-search-forward "\\* \\(.*::\\|.*: [^.,\t\n]+[.,\t]\\)"
835                                (texinfo-menu-end)
836                                t)
837         (let ((beginning-white-space (point)))
838           (skip-chars-forward " \t")    ; skip over spaces
839           (if (looking-at "\\(@\\|\\w\\)+") ; if there is text
840               (progn
841                 ;; remove pre-existing indentation
842                 (delete-region beginning-white-space (point))
843                 (indent-to-column to-column-number)))))
844       ;; position point at beginning of next line
845       (forward-line 1)
846       (setq beginning-of-next-line (point)))))
847
848 \f
849 ;;; Making the master menu
850
851 (defun texinfo-master-menu (update-all-nodes-menus-p)
852   "Make a master menu for a whole Texinfo file.
853 Non-nil argument (prefix, if interactive) means first update all
854 existing nodes and menus.  Remove pre-existing master menu, if there is one.
855
856 This function creates a master menu that follows the top node.  The
857 master menu includes every entry from all the other menus.  It
858 replaces any existing ordinary menu that follows the top node.
859
860 If called with a non-nil argument, this function first updates all the
861 menus in the buffer (incorporating descriptions from pre-existing
862 menus) before it constructs the master menu.
863
864 The function removes the detailed part of an already existing master
865 menu.  This action depends on the pre-existing master menu using the
866 standard `texinfo-master-menu-header'.
867
868 The master menu has the following format, which is adapted from the
869 recommendation in the Texinfo Manual:
870
871    * The first part contains the major nodes in the Texinfo file: the
872      nodes for the chapters, chapter-like sections, and the major
873      appendices.  This includes the indices, so long as they are in
874      chapter-like sections, such as unnumbered sections.
875
876    * The second and subsequent parts contain a listing of the other,
877      lower level menus, in order.  This way, an inquirer can go
878      directly to a particular node if he or she is searching for
879      specific information.
880
881 Each of the menus in the detailed node listing is introduced by the
882 title of the section containing the menu.
883
884 Indents the first line of descriptions, and leaves trailing whitespace
885 in a menu that lacks descriptions, so descriptions will format well.
886 In general, a menu should contain descriptions, because node names and
887 section titles are often too short to explain a node well."
888
889   (interactive "P")
890   (let ((case-fold-search t))
891     (widen)
892     (goto-char (point-min))
893
894     ;; Move point to location after `top'.
895     (if (not (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t))
896         (error "This buffer needs a Top node"))
897
898     (let ((first-chapter
899            (save-excursion
900              (or (re-search-forward "^@node" nil t)
901                  (error "Too few nodes for a master menu"))
902              (point))))
903       (if (search-forward texinfo-master-menu-header first-chapter t)
904           (progn
905             ;; Check if @detailmenu kludge is used;
906             ;; if so, leave point before @detailmenu.
907             (search-backward "\n@detailmenu"
908                              (save-excursion (forward-line -3) (point))
909                              t)
910             ;; Remove detailed master menu listing
911             (goto-char (match-beginning 0))
912             (let ((end-of-detailed-menu-descriptions
913                    (save-excursion     ; beginning of end menu line
914                      (goto-char (texinfo-menu-end))
915                      (beginning-of-line) (forward-char -1)
916                      (point))))
917               (delete-region (point) end-of-detailed-menu-descriptions)))))
918
919     (if update-all-nodes-menus-p
920         (progn
921           (message "Making a master menu in %s ...first updating all nodes... "
922                    (buffer-name))
923           (texinfo-update-node (point-min) (point-max))
924
925           (message "Updating all menus in %s ... " (buffer-name))
926           (texinfo-make-menu (point-min) (point-max))))
927
928     (message "Now making the master menu in %s... " (buffer-name))
929     (goto-char (point-min))
930     (texinfo-insert-master-menu-list
931      (texinfo-master-menu-list))
932
933     ;; Remove extra newlines that texinfo-insert-master-menu-list
934     ;; may have inserted.
935
936     (save-excursion
937       (goto-char (point-min))
938
939       (if (search-forward texinfo-master-menu-header nil t)
940           (progn
941             (goto-char (match-beginning 0))
942             ;; Check if @detailmenu kludge is used;
943             ;; if so, leave point before @detailmenu.
944             (search-backward "\n@detailmenu"
945                              (save-excursion (forward-line -3) (point))
946                              t)
947             (insert "\n")
948             (delete-blank-lines)
949             (goto-char (point-min))))
950
951       (re-search-forward "^@menu")
952       (forward-line -1)
953       (delete-blank-lines)
954
955       (re-search-forward "^@end menu")
956       (forward-line 1)
957       (delete-blank-lines))
958
959     (message
960      "Done...completed making master menu.  You may save the buffer.")))
961
962 (defun texinfo-master-menu-list ()
963   "Return a list of menu entries and header lines for the master menu.
964
965 Start with the menu for chapters and indices and then find each
966 following menu and the title of the node preceding that menu.
967
968 The master menu list has this form:
969
970     \(\(\(... \"entry-1-2\"  \"entry-1\"\) \"title-1\"\)
971       \(\(... \"entry-2-2\"  \"entry-2-1\"\) \"title-2\"\)
972       ...\)
973
974 However, there does not need to be a title field."
975
976   (let (master-menu-list)
977     (while (texinfo-locate-menu-p)
978       (push (list (texinfo-copy-menu) (texinfo-copy-menu-title))
979             master-menu-list))
980     (nreverse master-menu-list)))
981
982 (defun texinfo-insert-master-menu-list (master-menu-list)
983   "Format and insert the master menu in the current buffer."
984   (goto-char (point-min))
985   ;; Insert a master menu only after `Top' node and before next node
986   ;; \(or include file if there is no next node\).
987   (unless (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t)
988     (error "This buffer needs a Top node"))
989   (let ((first-chapter
990          (save-excursion (re-search-forward "^@node\\|^@include") (point))))
991     (unless (re-search-forward "^@menu" first-chapter t)
992       (error "Buffer lacks ordinary `Top' menu in which to insert master")))
993   (beginning-of-line)
994   (delete-region      ; buffer must have ordinary top menu
995    (point)
996    (save-excursion (re-search-forward "^@end menu") (point)))
997
998   (save-excursion
999     ;; `master-menu-inserted-p' is a kludge to tell
1000     ;; whether to insert @end detailmenu (see bleow)
1001     (let (master-menu-inserted-p)
1002       ;; Handle top of menu
1003       (insert "\n@menu\n")
1004       ;; Insert chapter menu entries.  Tell user what is going on.
1005       (message "Inserting chapter menu entry: %s ... "
1006                (car (car master-menu-list)))
1007       (dolist (entry (reverse (car (car master-menu-list))))
1008         (insert "* " entry "\n"))
1009
1010       (setq master-menu-list (cdr master-menu-list))
1011
1012       ;; Only insert detailed master menu if there is one....
1013       (if (car (car master-menu-list))
1014           (progn (setq master-menu-inserted-p t)
1015                  (insert (concat "\n@detailmenu\n"
1016                                  texinfo-master-menu-header))))
1017
1018       ;; @detailmenu added 5 Sept 1996 to `texinfo-master-menu-header'
1019       ;; at Karl Berry's request to avert a bug in `makeinfo';
1020       ;; all agree this is a bad kludge and should eventually be removed.
1021       ;; @detailmenu ... @end detailmenu is a noop in `texinfmt.el'.
1022       ;; See @end detailmenu below;
1023       ;; also see `texinfo-all-menus-update' above, `texinfo-master-menu',
1024       ;; `texinfo-multiple-files-update'.
1025
1026       ;; Now, insert all the other menus
1027
1028       ;; The menu master-menu-list has a form like this:
1029       ;; ((("beta"  "alpha") "title-A")
1030       ;;  (("delta" "gamma") "title-B"))
1031
1032       (dolist (menu master-menu-list)
1033
1034         (message "Inserting menu for %s .... " (cadr menu))
1035         ;; insert title of menu section
1036         (insert "\n" (cadr menu) "\n\n")
1037
1038         ;; insert each menu entry
1039         (dolist (entry (reverse (car menu)))
1040           (insert "* " entry "\n")))
1041
1042       ;; Finish menu
1043
1044       ;; @detailmenu (see note above)
1045       ;; Only insert @end detailmenu if a master menu was inserted.
1046       (if master-menu-inserted-p
1047           (insert "\n@end detailmenu"))
1048       (insert "\n@end menu\n\n"))))
1049
1050 (defun texinfo-locate-menu-p ()
1051   "Find the next menu in the texinfo file.
1052 If found, leave point after word `menu' on the `@menu' line, and return t.
1053 If a menu is not found, do not move point and return nil."
1054   (re-search-forward "\\(^@menu\\)" nil t))
1055
1056 (defun texinfo-copy-menu-title ()
1057   "Return the title of the section preceding the menu as a string.
1058 If such a title cannot be found, return an empty string.  Do not move
1059 point."
1060   (let ((case-fold-search t))
1061     (save-excursion
1062       (if (re-search-backward
1063            (concat
1064             "\\(^@top"
1065             "\\|"                         ; or
1066             texinfo-section-types-regexp  ; all other section types
1067             "\\)")
1068            nil
1069            t)
1070           (progn
1071             (beginning-of-line)
1072             (forward-word 1)              ; skip over section type
1073             (skip-chars-forward " \t")    ; and over spaces
1074             (buffer-substring
1075              (point)
1076              (progn (end-of-line) (point))))
1077         ""))))
1078
1079 (defun texinfo-copy-menu ()
1080   "Return the entries of an existing menu as a list.
1081 Start with point just after the word `menu' in the `@menu' line
1082 and leave point on the line before the `@end menu' line."
1083   (let* (this-menu-list
1084          (end-of-menu (texinfo-menu-end)) ; position of end of `@end menu'
1085          (last-entry (save-excursion      ; position of beginning of
1086                                           ; last `* ' entry
1087                       (goto-char end-of-menu)
1088                       ;; handle multi-line description
1089                       (if (not (re-search-backward "^\\* " nil t))
1090                           (error "No entries in menu"))
1091                       (point))))
1092     (while (< (point) last-entry)
1093       (if (re-search-forward  "^\\* " end-of-menu t)
1094           (push (buffer-substring
1095                  (point)
1096                  ;; copy multi-line descriptions
1097                  (save-excursion
1098                    (re-search-forward "\\(^\\* \\|^@e\\)" nil t)
1099                    (- (point) 3)))
1100                 this-menu-list)))
1101     this-menu-list))
1102
1103 \f
1104 ;;; Determining the hierarchical level in the texinfo file
1105
1106 (defun texinfo-specific-section-type ()
1107   "Return the specific type of next section, as a string.
1108 For example, \"unnumberedsubsec\".  Return \"top\" for top node.
1109
1110 Searches forward for a section.  Hence, point must be before the
1111 section whose type will be found.  Does not move point.  Signal an
1112 error if the node is not the top node and a section is not found."
1113   (let ((case-fold-search t))
1114     (save-excursion
1115       (cond
1116        ((re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)"
1117                            ;; Following search limit by cph but causes a bug
1118                            ;;(line-end-position)
1119                            nil
1120                            t)
1121         "top")
1122        ((re-search-forward texinfo-section-types-regexp nil t)
1123         (buffer-substring-no-properties
1124          (progn (beginning-of-line) ; copy its name
1125                 (1+ (point)))
1126          (progn (forward-word 1)
1127                 (point))))
1128        (t
1129         (error
1130          "texinfo-specific-section-type: Chapter or section not found"))))))
1131
1132 (defun texinfo-hierarchic-level ()
1133   "Return the general hierarchal level of the next node in a texinfo file.
1134 Thus, a subheading or appendixsubsec is of type subsection."
1135   (let ((case-fold-search t))
1136     (cadr (assoc
1137            (texinfo-specific-section-type)
1138            texinfo-section-list))))
1139
1140 \f
1141 ;;; Locating the major positions
1142
1143 (defun texinfo-update-menu-region-beginning (level)
1144   "Locate beginning of higher level section this section is within.
1145 Return position of the beginning of the node line; do not move point.
1146 Thus, if this level is subsection, searches backwards for section node.
1147 Only argument is a string of the general type of section."
1148   (let ((case-fold-search t))
1149     ;; !! Known bug: if section immediately follows top node, this
1150     ;; returns the beginning of the buffer as the beginning of the
1151     ;; higher level section.
1152     (cond
1153      ((< level 3)
1154       (save-excursion
1155         (goto-char (point-min))
1156         (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t)
1157         (beginning-of-line)
1158         (point)))
1159      (t
1160       (save-excursion
1161         (re-search-backward
1162          (concat
1163           "\\(^@node\\).*\n"              ; match node line
1164           "\\(\\(\\(^@c\\).*\n\\)"        ; match comment line, if any
1165           "\\|"                           ; or
1166           "\\(^@ifinfo[ ]*\n\\)"          ; ifinfo line, if any
1167           "\\|"                           ; or
1168           "\\(^@ifnottex[ ]*\n\\)"        ; ifnottex line, if any
1169           "\\)?"                          ; end of expression
1170           (eval
1171            (cdr (assoc level texinfo-update-menu-higher-regexps))))
1172          nil
1173          'goto-beginning)
1174         (point))))))
1175
1176 (defun texinfo-update-menu-region-end (level)
1177   "Locate end of higher level section this section is within.
1178 Return position; do not move point.  Thus, if this level is a
1179 subsection, find the node for the section this subsection is within.
1180 If level is top or chapter, returns end of file.  Only argument is a
1181 string of the general type of section."
1182   (let ((case-fold-search t))
1183     (save-excursion
1184       (if (re-search-forward
1185            (concat
1186             "\\(^@node\\).*\n"            ; match node line
1187             "\\(\\(\\(^@c\\).*\n\\)"      ; match comment line, if any
1188             "\\|"                         ; or
1189             "\\(^@ifinfo[ ]*\n\\)"        ; ifinfo line, if any
1190             "\\|"                         ; or
1191             "\\(^@ifnottex[ ]*\n\\)"      ; ifnottex line, if any
1192             "\\)?"                        ; end of expression
1193             (eval
1194              ;; Never finds end of level above chapter so goes to end.
1195              (cdr (assoc level texinfo-update-menu-higher-regexps))))
1196            nil
1197            'goto-end)
1198           (match-beginning 1)
1199         (point-max)))))
1200
1201 (defun texinfo-menu-first-node (beginning end)
1202   "Locate first node of the section the menu will be placed in.
1203 Return position; do not move point.
1204 The menu will be located just before this position.
1205
1206 First argument is the position of the beginning of the section in
1207 which the menu will be located; second argument is the position of the
1208 end of that region; it limits the search."
1209
1210   (save-excursion
1211     (goto-char beginning)
1212     (forward-line 1)
1213     (re-search-forward "^@node" end t)
1214     (beginning-of-line)
1215     (point)))
1216
1217 \f
1218 ;;; Updating a node
1219
1220 (defun texinfo-update-node (&optional beginning end)
1221   "Without any prefix argument, update the node in which point is located.
1222 Interactively, a prefix argument means to operate on the region.
1223
1224 The functions for creating or updating nodes and menus, and their
1225 keybindings, are:
1226
1227     texinfo-update-node (&optional beginning end)    \\[texinfo-update-node]
1228     texinfo-every-node-update ()                \\[texinfo-every-node-update]
1229     texinfo-sequential-node-update (&optional region-p)
1230
1231     texinfo-make-menu (&optional region-p)      \\[texinfo-make-menu]
1232     texinfo-all-menus-update ()                 \\[texinfo-all-menus-update]
1233     texinfo-master-menu ()
1234
1235     texinfo-indent-menu-description (column &optional region-p)
1236
1237 The `texinfo-column-for-description' variable specifies the column to
1238 which menu descriptions are indented. Its default value is 32."
1239
1240   (interactive
1241    (if prefix-arg
1242        (list (point) (mark))))
1243   (if (null beginning)
1244       ;; Update a single node.
1245       (let ((auto-fill-function nil))
1246         (if (not (re-search-backward "^@node" (point-min) t))
1247             (error "Node line not found before this position"))
1248         (texinfo-update-the-node)
1249         (message "Done...updated the node.  You may save the buffer."))
1250     ;; else
1251     (let ((auto-fill-function nil))
1252       (save-excursion
1253         (save-restriction
1254           (narrow-to-region beginning end)
1255           (goto-char (point-min))
1256           (while (re-search-forward "^@node" (point-max) t)
1257             (beginning-of-line)
1258             (texinfo-update-the-node))
1259           (goto-char (point-max))
1260           (message "Done...nodes updated in region.  You may save the buffer."))))))
1261
1262 (defun texinfo-every-node-update ()
1263   "Update every node in a Texinfo file."
1264   (interactive)
1265   (save-excursion
1266     (texinfo-update-node (point-min) (point-max))
1267     (message "Done...updated every node.  You may save the buffer.")))
1268
1269 (defun texinfo-update-the-node ()
1270   "Update one node.  Point must be at the beginning of node line.
1271 Leave point at the end of the node line."
1272   (texinfo-check-for-node-name)
1273   (texinfo-delete-existing-pointers)
1274   (message "Updating node: %s ... " (texinfo-copy-node-name))
1275   (save-restriction
1276     (widen)
1277     (let*
1278         ((case-fold-search t)
1279          (level (texinfo-hierarchic-level))
1280          (beginning (texinfo-update-menu-region-beginning level))
1281          (end (texinfo-update-menu-region-end level)))
1282       (if (eq level 1)
1283           (texinfo-top-pointer-case)
1284         ;; else
1285         (texinfo-insert-pointer beginning end level 'next)
1286         (texinfo-insert-pointer beginning end level 'previous)
1287         (texinfo-insert-pointer beginning end level 'up)
1288         (texinfo-clean-up-node-line)))))
1289
1290 (defun texinfo-top-pointer-case ()
1291   "Insert pointers in the Top node.  This is a special case.
1292
1293 The `Next' pointer is a pointer to a chapter or section at a lower
1294 hierarchical level in the file.  The `Previous' and `Up' pointers are
1295 to `(dir)'.  Point must be at the beginning of the node line, and is
1296 left at the end of the node line."
1297
1298   (texinfo-clean-up-node-line)
1299   (insert ", "
1300           (save-excursion
1301             ;; There may be an @chapter or other such command between
1302             ;; the top node line and the next node line, as a title
1303             ;; for an `ifinfo' section. This @chapter command must
1304             ;; must be skipped.  So the procedure is to search for
1305             ;; the next `@node' line, and then copy its name.
1306             (if (re-search-forward "^@node" nil t)
1307                 (progn
1308                   (beginning-of-line)
1309                   (texinfo-copy-node-name))
1310               " "))
1311           ", (dir), (dir)"))
1312
1313 (defun texinfo-check-for-node-name ()
1314   "Determine whether the node has a node name.  Prompt for one if not.
1315 Point must be at beginning of node line.  Does not move point."
1316   (save-excursion
1317     (let ((initial (texinfo-copy-next-section-title)))
1318       ;; This is not clean.  Use `interactive' to read the arg.
1319       (forward-word 1)                    ; skip over node command
1320       (skip-chars-forward " \t")          ; and over spaces
1321       (if (not (looking-at "[^,\t\n ]+")) ; regexp based on what Info looks for
1322                                           ; alternatively, use "[a-zA-Z]+"
1323         (let ((node-name
1324                (read-from-minibuffer
1325                 "Node name (use no @, commas, colons, or apostrophes): "
1326                 initial)))
1327           (insert " " node-name))))))
1328
1329 (defun texinfo-delete-existing-pointers ()
1330   "Delete `Next', `Previous', and `Up' pointers.
1331 Starts from the current position of the cursor, and searches forward
1332 on the line for a comma and if one is found, deletes the rest of the
1333 line, including the comma.  Leaves point at beginning of line."
1334   (let ((eol-point (save-excursion (end-of-line) (point))))
1335     (if (search-forward "," eol-point t)
1336         (delete-region (1- (point)) eol-point)))
1337   (beginning-of-line))
1338
1339 (defun texinfo-find-pointer (beginning end level direction)
1340   "Move point to section associated with next, previous, or up pointer.
1341 Return type of pointer (either `normal' or `no-pointer').
1342
1343 The first and second arguments bound the search for a pointer to the
1344 beginning and end, respectively, of the enclosing higher level
1345 section.  The third argument is a string specifying the general kind
1346 of section such as \"chapter\" or \"section\".  When looking for the
1347 `Next' pointer, the section found will be at the same hierarchical
1348 level in the Texinfo file; when looking for the `Previous' pointer,
1349 the section found will be at the same or higher hierarchical level in
1350 the Texinfo file; when looking for the `Up' pointer, the section found
1351 will be at some level higher in the Texinfo file.  The fourth argument
1352 \(one of 'next, 'previous, or 'up\) specifies whether to find the
1353 `Next', `Previous', or `Up' pointer."
1354   (let ((case-fold-search t))
1355     (cond ((eq direction 'next)
1356            (forward-line 3)             ; skip over current node
1357            ;; Search for section commands accompanied by node lines;
1358            ;; ignore section commands in the middle of nodes.
1359            (if (re-search-forward
1360                 ;; A `Top' node is never a next pointer, so won't find it.
1361                 (concat
1362                  ;; Match node line.
1363                  "\\(^@node\\).*\n"
1364                  ;; Match comment, ifinfo, ifnottex line, if any
1365                  (concat
1366                   "\\(\\("
1367                   "\\(^@c\\).*\n\\)"
1368                   "\\|"
1369                   "\\(^@ifinfo[ ]*\n\\)"
1370                   "\\|"
1371                   "\\(^@ifnottex[ ]*\n\\)"
1372                   "\\)?")
1373                  (eval
1374                   (cdr (assoc level texinfo-update-menu-same-level-regexps))))
1375                 end
1376                 t)
1377                'normal
1378              'no-pointer))
1379           ((eq direction 'previous)
1380            (if (re-search-backward
1381                 (concat
1382                  "\\("
1383                  ;; Match node line.
1384                  "\\(^@node\\).*\n"
1385                  ;; Match comment, ifinfo, ifnottex line, if any
1386                  (concat
1387                   "\\(\\("
1388                   "\\(^@c\\).*\n\\)"
1389                   "\\|"
1390                   "\\(^@ifinfo[ ]*\n\\)"
1391                   "\\|"
1392                   "\\(^@ifnottex[ ]*\n\\)"
1393                   "\\)?")
1394                  (eval
1395                   (cdr (assoc level texinfo-update-menu-same-level-regexps)))
1396                  "\\|"
1397                  ;; Match node line.
1398                  "\\(^@node\\).*\n"
1399                  ;; Match comment, ifinfo, ifnottex line, if any
1400                  (concat
1401                   "\\(\\("
1402                   "\\(^@c\\).*\n\\)"
1403                   "\\|"
1404                   "\\(^@ifinfo[ ]*\n\\)"
1405                   "\\|"
1406                   "\\(^@ifnottex[ ]*\n\\)"
1407                   "\\)?")
1408                  (eval
1409                   (cdr (assoc level texinfo-update-menu-higher-regexps)))
1410                  "\\|"
1411                  ;; Handle `Top' node specially.
1412                  "^@node [ \t]*top[ \t]*\\(,\\|$\\)"
1413                  "\\)")
1414                 beginning
1415                 t)
1416                'normal
1417              'no-pointer))
1418           ((eq direction 'up)
1419            (if (re-search-backward
1420                 (concat
1421                  "\\("
1422                  ;; Match node line.
1423                  "\\(^@node\\).*\n"
1424                  ;; Match comment, ifinfo, ifnottex line, if any
1425                  (concat
1426                   "\\(\\("
1427                   "\\(^@c\\).*\n\\)"
1428                   "\\|"
1429                   "\\(^@ifinfo[ ]*\n\\)"
1430                   "\\|"
1431                   "\\(^@ifnottex[ ]*\n\\)"
1432                   "\\)?")
1433                  (eval (cdr (assoc level texinfo-update-menu-higher-regexps)))
1434                  "\\|"
1435                  ;; Handle `Top' node specially.
1436                  "^@node [ \t]*top[ \t]*\\(,\\|$\\)"
1437                  "\\)")
1438                 (save-excursion
1439                   (goto-char beginning)
1440                   (beginning-of-line)
1441                   (point))
1442                 t)
1443                'normal
1444              'no-pointer))
1445           (t
1446            (error "texinfo-find-pointer: lack proper arguments")))))
1447
1448 (defun texinfo-pointer-name (kind)
1449   "Return the node name preceding the section command.
1450 The argument is the kind of section, either `normal' or `no-pointer'."
1451   (let (name)
1452     (cond ((eq kind 'normal)
1453            (end-of-line)                ; this handles prev node top case
1454            (re-search-backward          ; when point is already
1455             "^@node"                    ; at the beginning of @node line
1456             (save-excursion (forward-line -3))
1457             t)
1458            (setq name (texinfo-copy-node-name)))
1459           ((eq kind 'no-pointer)
1460            ;; Don't need to put a blank in the pointer slot,
1461            ;; since insert "' " always has a space
1462            (setq name " ")))    ; put a blank in the pointer slot
1463     name))
1464
1465 (defun texinfo-insert-pointer (beginning end level direction)
1466   "Insert the `Next', `Previous' or `Up' node name at point.
1467 Move point forward.
1468
1469 The first and second arguments bound the search for a pointer to the
1470 beginning and end, respectively, of the enclosing higher level
1471 section.  The third argument is the hierarchical level of the Texinfo
1472 file, a string such as \"section\".  The fourth argument is direction
1473 towards which the pointer is directed, one of `next', `previous', or `up'."
1474
1475   (end-of-line)
1476   (insert
1477    ", "
1478    (save-excursion
1479      (texinfo-pointer-name
1480       (texinfo-find-pointer beginning end level direction)))))
1481
1482 (defun texinfo-clean-up-node-line ()
1483   "Remove extra commas, if any, at end of node line."
1484   (end-of-line)
1485   (skip-chars-backward ", ")
1486   (delete-region (point) (save-excursion (end-of-line) (point))))
1487
1488 \f
1489 ;;; Updating nodes sequentially
1490 ;; These sequential update functions insert `Next' or `Previous'
1491 ;; pointers that point to the following or preceding nodes even if they
1492 ;; are at higher or lower hierarchical levels.  This means that if a
1493 ;; section contains one or more subsections, the section's `Next'
1494 ;; pointer will point to the subsection and not the following section.
1495 ;; (The subsection to which `Next' points will most likely be the first
1496 ;; item on the section's menu.)
1497
1498 (defun texinfo-sequential-node-update (&optional region-p)
1499   "Update one node (or many) in a Texinfo file with sequential pointers.
1500
1501 This function causes the `Next' or `Previous' pointer to point to the
1502 immediately preceding or following node, even if it is at a higher or
1503 lower hierarchical level in the document.  Continually pressing `n' or
1504 `p' takes you straight through the file.
1505
1506 Without any prefix argument, update the node in which point is located.
1507 Non-nil argument (prefix, if interactive) means update the nodes in the
1508 marked region.
1509
1510 This command makes it awkward to navigate among sections and
1511 subsections; it should be used only for those documents that are meant
1512 to be read like a novel rather than a reference, and for which the
1513 Info `g*' command is inadequate."
1514
1515   (interactive "P")
1516   (if (not region-p)
1517       ;; update a single node
1518       (let ((auto-fill-function nil))
1519         (if (not (re-search-backward "^@node" (point-min) t))
1520             (error "Node line not found before this position"))
1521         (texinfo-sequentially-update-the-node)
1522         (message
1523          "Done...sequentially updated the node .  You may save the buffer."))
1524     ;; else
1525     (let ((auto-fill-function nil)
1526           (beginning (region-beginning))
1527           (end (region-end)))
1528       (if (= end beginning)
1529           (error "Please mark a region"))
1530       (save-restriction
1531         (narrow-to-region beginning end)
1532         (goto-char beginning)
1533         (push-mark (point) t)
1534         (while (re-search-forward "^@node" (point-max) t)
1535           (beginning-of-line)
1536           (texinfo-sequentially-update-the-node))
1537         (message
1538          "Done...updated the nodes in sequence.  You may save the buffer.")))))
1539
1540 (defun texinfo-sequentially-update-the-node ()
1541   "Update one node such that the pointers are sequential.
1542 A `Next' or `Previous' pointer points to any preceding or following node,
1543 regardless of its hierarchical level."
1544
1545   (texinfo-check-for-node-name)
1546   (texinfo-delete-existing-pointers)
1547   (message
1548    "Sequentially updating node: %s ... " (texinfo-copy-node-name))
1549   (save-restriction
1550     (widen)
1551     (let* ((case-fold-search t)
1552            (level (texinfo-hierarchic-level)))
1553       (if (eq level 1)
1554           (texinfo-top-pointer-case)
1555         ;; else
1556         (texinfo-sequentially-insert-pointer level 'next)
1557         (texinfo-sequentially-insert-pointer level 'previous)
1558         (texinfo-sequentially-insert-pointer level 'up)
1559         (texinfo-clean-up-node-line)))))
1560
1561 (defun texinfo-sequentially-insert-pointer (level direction)
1562   "Insert the `Next', `Previous' or `Up' node name at point.
1563 Move point forward.
1564
1565 The first argument is the hierarchical level of the Texinfo file, a
1566 string such as \"section\".  The second argument is direction, one of
1567 `next', `previous', or `up'."
1568
1569   (end-of-line)
1570   (insert
1571    ", "
1572    (save-excursion
1573      (texinfo-pointer-name
1574       (texinfo-sequentially-find-pointer level direction)))))
1575
1576 (defun texinfo-sequentially-find-pointer (level direction)
1577   "Find next or previous pointer sequentially in Texinfo file, or up pointer.
1578 Move point to section associated with the pointer.  Find point even if
1579 it is in a different section.
1580
1581 Return type of pointer (either `normal' or `no-pointer').
1582
1583 The first argument is a string specifying the general kind of section
1584 such as \"chapter\" or \"section\".  The section found will be at the
1585 same hierarchical level in the Texinfo file, or, in the case of the up
1586 pointer, some level higher.  The second argument (one of `next',
1587 `previous', or `up') specifies whether to find the `Next', `Previous',
1588 or `Up' pointer."
1589   (let ((case-fold-search t))
1590     (cond ((eq direction 'next)
1591            (forward-line 3)             ; skip over current node
1592            (if (re-search-forward
1593                 texinfo-section-types-regexp
1594                 (point-max)
1595                 t)
1596                'normal
1597              'no-pointer))
1598           ((eq direction 'previous)
1599            (if (re-search-backward
1600                 texinfo-section-types-regexp
1601                 (point-min)
1602                 t)
1603                'normal
1604              'no-pointer))
1605           ((eq direction 'up)
1606            (if (re-search-backward
1607                 (eval (cdr (assoc level texinfo-update-menu-higher-regexps)))
1608                 (point-min)
1609                 t)
1610                'normal
1611              'no-pointer))
1612           (t
1613            (error "texinfo-sequential-find-pointer: lack proper arguments")))))
1614
1615 \f
1616 ;;; Inserting `@node' lines
1617 ;; The `texinfo-insert-node-lines' function inserts `@node' lines as needed
1618 ;; before the `@chapter', `@section', and such like lines of a region
1619 ;; in a Texinfo file.
1620
1621 (defun texinfo-insert-node-lines (beginning end &optional title-p)
1622   "Insert missing `@node' lines in region of Texinfo file.
1623 Non-nil argument (prefix, if interactive) means also to insert the
1624 section titles as node names; and also to insert the section titles as
1625 node names in pre-existing `@node' lines that lack names."
1626   (interactive "r\nP")
1627
1628   ;; Use marker; after inserting node lines, leave point at end of
1629   ;; region and mark at beginning.
1630
1631   (let (beginning-marker end-marker title last-section-position)
1632
1633     ;; Save current position on mark ring and set mark to end.
1634     (push-mark end t)
1635     (setq end-marker (mark-marker))
1636
1637     (goto-char beginning)
1638     (while (re-search-forward
1639             texinfo-section-types-regexp
1640             end-marker
1641             'end)
1642       ;; Copy title if desired.
1643       (if title-p
1644           (progn
1645             (beginning-of-line)
1646             (forward-word 1)
1647             (skip-chars-forward " \t")
1648             (setq title (buffer-substring
1649                          (point)
1650                          (save-excursion (end-of-line) (point))))))
1651       ;; Insert node line if necessary.
1652       (if (re-search-backward
1653            "^@node"
1654            ;; Avoid finding previous node line if node lines are close.
1655            (or last-section-position
1656                (save-excursion (forward-line -2) (point))) t)
1657           ;;  @node is present, and point at beginning of that line
1658           (forward-word 1)          ; Leave point just after @node.
1659         ;; Else @node missing; insert one.
1660         (beginning-of-line)         ; Beginning of `@section' line.
1661         (insert "@node\n")
1662         (backward-char 1))          ; Leave point just after `@node'.
1663       ;; Insert title if desired.
1664       (if title-p
1665           (progn
1666             (skip-chars-forward " \t")
1667             ;; Use regexp based on what info looks for
1668             ;; (alternatively, use "[a-zA-Z]+");
1669             ;; this means we only insert a title if none exists.
1670             (if (not (looking-at "[^,\t\n ]+"))
1671                 (progn
1672                   (beginning-of-line)
1673                   (forward-word 1)
1674                   (insert " " title)
1675                   (message "Inserted title %s ... " title)))))
1676       ;; Go forward beyond current section title.
1677       (re-search-forward texinfo-section-types-regexp
1678                          (save-excursion (forward-line 3) (point)) t)
1679       (setq last-section-position (point))
1680       (forward-line 1))
1681
1682     ;; Leave point at end of region, mark at beginning.
1683     (set-mark beginning)
1684
1685     (if title-p
1686       (message
1687        "Done inserting node lines and titles.  You may save the buffer.")
1688     (message "Done inserting node lines.  You may save the buffer."))))
1689
1690 \f
1691 ;;; Update and create menus for multi-file Texinfo sources
1692
1693 ;;  1. M-x texinfo-multiple-files-update
1694 ;;
1695 ;;     Read the include file list of an outer Texinfo file and
1696 ;;     update all highest level nodes in the files listed and insert a
1697 ;;     main menu in the outer file after its top node.
1698
1699 ;;  2. C-u M-x texinfo-multiple-files-update
1700 ;;
1701 ;;     Same as 1, but insert a master menu.  (Saves reupdating lower
1702 ;;     level menus and nodes.)  This command simply reads every menu,
1703 ;;     so if the menus are wrong, the master menu will be wrong.
1704 ;;     Similarly, if the lower level node pointers are wrong, they
1705 ;;     will stay wrong.
1706
1707 ;;  3. C-u 2 M-x texinfo-multiple-files-update
1708 ;;
1709 ;;     Read the include file list of an outer Texinfo file and
1710 ;;     update all nodes and menus in the files listed and insert a
1711 ;;     master menu in the outer file after its top node.
1712
1713 ;;; Note: these functions:
1714 ;;;
1715 ;;;   * Do not save or delete any buffers.  You may fill up your memory.
1716 ;;;   * Do not handle any pre-existing nodes in outer file.
1717 ;;;     Hence, you may need a file for indices.
1718
1719 \f
1720 ;;; Auxiliary functions for multiple file updating
1721
1722 (defun texinfo-multi-file-included-list (outer-file)
1723   "Return a list of the included files in OUTER-FILE."
1724   (let ((included-file-list (list outer-file))
1725         start)
1726     (save-excursion
1727       (set-buffer (find-file-noselect outer-file))
1728       (widen)
1729       (goto-char (point-min))
1730       (while (re-search-forward "^@include" nil t)
1731         (skip-chars-forward " \t")
1732         (setq start (point))
1733         (end-of-line)
1734         (skip-chars-backward " \t")
1735         (setq included-file-list
1736               (cons (buffer-substring start (point))
1737                     included-file-list)))
1738       (nreverse included-file-list))))
1739
1740 (defun texinfo-copy-next-section-title ()
1741   "Return the name of the immediately following section as a string.
1742
1743 Start with point at the beginning of the node line.  Leave point at the
1744 same place.  If there is no title, returns an empty string."
1745
1746   (save-excursion
1747     (end-of-line)
1748     (let ((node-end (or
1749                         (save-excursion
1750                           (if (re-search-forward "\\(^@node\\)" nil t)
1751                               (match-beginning 0)))
1752                         (point-max))))
1753       (if (re-search-forward texinfo-section-types-regexp node-end t)
1754           (progn
1755             (beginning-of-line)
1756             ;; copy title
1757             (let ((title
1758                    (buffer-substring
1759                     (progn (forward-word 1)           ; skip over section type
1760                            (skip-chars-forward " \t") ; and over spaces
1761                            (point))
1762                     (progn (end-of-line) (point)))))
1763               title))
1764         ""))))
1765
1766 (defun texinfo-multi-file-update (files &optional update-everything)
1767   "Update first node pointers in each file in FILES.
1768 Return a list of the node names.
1769
1770 The first file in the list is an outer file; the remaining are
1771 files included in the outer file with `@include' commands.
1772
1773 If optional arg UPDATE-EVERYTHING non-nil, update every menu and
1774 pointer in each of the included files.
1775
1776 Also update the `Top' level node pointers of the outer file.
1777
1778 Requirements:
1779
1780   * the first file in the FILES list must be the outer file,
1781   * each of the included files must contain exactly one highest
1782     hierarchical level node,
1783   * this node must be the first node in the included file,
1784   * each highest hierarchical level node must be of the same type.
1785
1786 Thus, normally, each included file contains one, and only one, chapter."
1787
1788 ;; The menu-list has the form:
1789 ;;
1790 ;;     \(\(\"node-name1\" . \"title1\"\)
1791 ;;       \(\"node-name2\" . \"title2\"\) ... \)
1792 ;;
1793 ;; However, there does not need to be a title field and this function
1794 ;; does not fill it; however a comment tells you how to do so.
1795 ;; You would use the title field if you wanted to insert titles in the
1796 ;; description slot of a menu as a description.
1797
1798   (let ((case-fold-search t)
1799         menu-list next-node-name previous-node-name)
1800
1801     ;; Find the name of the first node of the first included file.
1802     (set-buffer (find-file-noselect (car (cdr files))))
1803     (widen)
1804     (goto-char (point-min))
1805     (if (not (re-search-forward "^@node" nil t))
1806         (error "No `@node' line found in %s" (buffer-name)))
1807     (beginning-of-line)
1808     (texinfo-check-for-node-name)
1809     (setq next-node-name (texinfo-copy-node-name))
1810
1811     (push (cons next-node-name (prog1 "" (forward-line 1)))
1812           ;; Use following to insert section titles automatically.
1813           ;; (texinfo-copy-next-section-title)
1814           menu-list)
1815
1816     ;; Go to outer file
1817     (set-buffer (find-file-noselect (pop files)))
1818     (goto-char (point-min))
1819     (if (not (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t))
1820         (error "This buffer needs a Top node"))
1821     (beginning-of-line)
1822     (texinfo-delete-existing-pointers)
1823     (end-of-line)
1824     (insert ", " next-node-name ", (dir), (dir)")
1825     (beginning-of-line)
1826     (setq previous-node-name "Top")
1827
1828     (while files
1829
1830       (if (not (cdr files))
1831           ;; No next file
1832           (setq next-node-name "")
1833         ;; Else,
1834         ;; find the name of the first node in the next file.
1835         (set-buffer (find-file-noselect (car (cdr files))))
1836         (widen)
1837         (goto-char (point-min))
1838         (if (not (re-search-forward "^@node" nil t))
1839             (error "No `@node' line found in %s" (buffer-name)))
1840         (beginning-of-line)
1841         (texinfo-check-for-node-name)
1842         (setq next-node-name (texinfo-copy-node-name))
1843         (push (cons next-node-name (prog1 "" (forward-line 1)))
1844               ;; Use following to insert section titles automatically.
1845               ;; (texinfo-copy-next-section-title)
1846               menu-list))
1847
1848       ;; Go to node to be updated.
1849       (set-buffer (find-file-noselect (car files)))
1850       (goto-char (point-min))
1851       (if (not (re-search-forward "^@node" nil t))
1852           (error "No `@node' line found in %s" (buffer-name)))
1853       (beginning-of-line)
1854
1855       ;; Update other menus and nodes if requested.
1856       (if update-everything (texinfo-all-menus-update t))
1857
1858       (beginning-of-line)
1859       (texinfo-delete-existing-pointers)
1860       (end-of-line)
1861       (insert ", " next-node-name ", " previous-node-name ", Top")
1862
1863       (beginning-of-line)
1864       (setq previous-node-name (texinfo-copy-node-name))
1865
1866       (setq files (cdr files)))
1867     (nreverse menu-list)))
1868
1869 (defun texinfo-multi-files-insert-main-menu (menu-list)
1870   "Insert formatted main menu at point.
1871 Indents the first line of the description, if any, to the value of
1872 `texinfo-column-for-description'."
1873
1874   (insert "@menu\n")
1875   (dolist (entry menu-list)
1876     ;; Every menu entry starts with a star and a space.
1877     (insert "* ")
1878
1879     ;; Insert the node name (and menu entry name, if present).
1880     (let ((node-part (car entry)))
1881       (if (stringp node-part)
1882           ;; "Double colon" entry line; menu entry and node name are the same,
1883           (insert (format "%s::" node-part))
1884         ;; "Single colon" entry line; menu entry and node name are different.
1885         (insert (format "%s: %s." (car node-part) (cdr node-part)))))
1886
1887     ;; Insert the description, if present.
1888     (when (cdr entry)
1889       ;; Move to right place.
1890       (indent-to texinfo-column-for-description 2)
1891       ;; Insert description.
1892       (insert (format "%s" (cdr entry))))
1893
1894     (insert "\n")) ; end this menu entry
1895   (insert "@end menu"))
1896
1897 (defun texinfo-multi-file-master-menu-list (files-list)
1898   "Return master menu list from files in FILES-LIST.
1899 Menu entries in each file collected using `texinfo-master-menu-list'.
1900
1901 The first file in FILES-LIST must be the outer file; the others must
1902 be the files included within it.  A main menu must already exist."
1903   (save-excursion
1904     (let (master-menu-list)
1905       (dolist (file files-list)
1906         (set-buffer (find-file-noselect file))
1907         (message "Working on: %s " (current-buffer))
1908         (goto-char (point-min))
1909         (setq master-menu-list
1910               (append master-menu-list (texinfo-master-menu-list))))
1911       master-menu-list)))
1912
1913 \f
1914 ;;; The multiple-file update function
1915
1916 (defun texinfo-multiple-files-update
1917   (outer-file &optional update-everything make-master-menu)
1918   "Update first node pointers in each file included in OUTER-FILE;
1919 create or update the `Top' level node pointers and the main menu in
1920 the outer file that refers to such nodes.  This does not create or
1921 update menus or pointers within the included files.
1922
1923 With optional MAKE-MASTER-MENU argument (prefix arg, if interactive),
1924 insert a master menu in OUTER-FILE in addition to creating or updating
1925 pointers in the first @node line in each included file and creating or
1926 updating the `Top' level node pointers of the outer file.  This does
1927 not create or update other menus and pointers within the included
1928 files.
1929
1930 With optional UPDATE-EVERYTHING argument (numeric prefix arg, if
1931 interactive), update all the menus and all the `Next', `Previous', and
1932 `Up' pointers of all the files included in OUTER-FILE before inserting
1933 a master menu in OUTER-FILE.  Also, update the `Top' level node
1934 pointers of OUTER-FILE.
1935
1936 Notes:
1937
1938   * this command does NOT save any files--you must save the
1939     outer file and any modified, included files.
1940
1941   * except for the `Top' node, this command does NOT handle any
1942     pre-existing nodes in the outer file; hence, indices must be
1943     enclosed in an included file.
1944
1945 Requirements:
1946
1947   * each of the included files must contain exactly one highest
1948     hierarchical level node,
1949   * this highest node must be the first node in the included file,
1950   * each highest hierarchical level node must be of the same type.
1951
1952 Thus, normally, each included file contains one, and only one,
1953 chapter."
1954
1955   (interactive (cons
1956                 (read-string
1957                  "Name of outer `include' file: "
1958                  (buffer-file-name))
1959                 (cond
1960                  ((not current-prefix-arg)      '(nil nil))
1961                  ((listp current-prefix-arg)    '(t nil)) ; make-master-menu
1962                  ((numberp current-prefix-arg)  '(t t))))) ; update-everything
1963
1964   (let* ((included-file-list (texinfo-multi-file-included-list outer-file))
1965          (files included-file-list)
1966          next-node-name
1967          previous-node-name
1968          ;; Update the pointers and collect the names of the nodes and titles
1969          (main-menu-list (texinfo-multi-file-update files update-everything)))
1970
1971     ;; Insert main menu
1972
1973     ;; Go to outer file
1974     (set-buffer (find-file-noselect (car included-file-list)))
1975     (if (texinfo-old-menu-p
1976          (point-min)
1977          (save-excursion
1978            (re-search-forward "^@include")
1979            (beginning-of-line)
1980            (point)))
1981
1982         ;; If found, leave point after word `menu' on the `@menu' line.
1983         (progn
1984           (texinfo-incorporate-descriptions main-menu-list)
1985           ;; Delete existing menu.
1986           (beginning-of-line)
1987           (delete-region
1988            (point)
1989            (save-excursion (re-search-forward "^@end menu") (point)))
1990           ;; Insert main menu
1991           (texinfo-multi-files-insert-main-menu main-menu-list))
1992
1993       ;; Else no current menu; insert it before `@include'
1994       (texinfo-multi-files-insert-main-menu main-menu-list))
1995
1996     ;; Insert master menu
1997
1998     (if make-master-menu
1999         (progn
2000           ;; First, removing detailed part of any pre-existing master menu
2001           (goto-char (point-min))
2002           (if (search-forward texinfo-master-menu-header nil t)
2003               (progn
2004                 (goto-char (match-beginning 0))
2005                 ;; Check if @detailmenu kludge is used;
2006                 ;; if so, leave point before @detailmenu.
2007                 (search-backward "\n@detailmenu"
2008                                  (save-excursion (forward-line -3) (point))
2009                                  t)
2010                 ;; Remove detailed master menu listing
2011                 (let ((end-of-detailed-menu-descriptions
2012                        (save-excursion  ; beginning of end menu line
2013                          (goto-char (texinfo-menu-end))
2014                          (beginning-of-line) (forward-char -1)
2015                          (point))))
2016                   (delete-region (point) end-of-detailed-menu-descriptions))))
2017
2018           ;; Create a master menu and insert it
2019           (texinfo-insert-master-menu-list
2020            (texinfo-multi-file-master-menu-list
2021             included-file-list)))))
2022
2023   ;; Remove unwanted extra lines.
2024   (save-excursion
2025     (goto-char (point-min))
2026
2027     (re-search-forward "^@menu")
2028     (forward-line -1)
2029     (insert  "\n")                      ; Ensure at least one blank line.
2030     (delete-blank-lines)
2031
2032     (re-search-forward "^@end menu")
2033     (forward-line 1)
2034     (insert  "\n")                      ; Ensure at least one blank line.
2035     (delete-blank-lines))
2036
2037   (message "Multiple files updated."))
2038
2039 \f
2040 ;; Place `provide' at end of file.
2041 (provide 'texnfo-upd)
2042
2043 ;;; texnfo-upd.el ends here