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