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