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