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