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