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