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