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