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