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