* Makefile.in (change-history-commit): Add missing piece of previous.
[emacs.git] / lisp / textmodes / texnfo-upd.el
Commit [+]AuthorDateLineData
be010748 Richard M. Stallman1995-10-30 17:35:01 +00001;;; texnfo-upd.el --- utilities for updating nodes and menus in Texinfo files
1337cb42 Erik Naggum1995-10-30 22:13:36 +00002
7e09ef09 Paul Eggert2015-01-01 14:26:41 -08003;; Copyright (C) 1989-1992, 2001-2015 Free Software Foundation, Inc.
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00004
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00005;; Author: Robert J. Chassell
64a1ba18 Andreas Schwab2002-03-09 20:11:46 +00006;; Maintainer: bug-texinfo@gnu.org
d7b4d18f Eric S. Raymond1992-07-17 20:24:00 +00007;; Keywords: maint, tex, docs
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00008
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00009;; This file is part of GNU Emacs.
10
1fecc8fe Glenn Morris2008-05-06 04:34:22 +000011;; GNU Emacs is free software: you can redistribute it and/or modify
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +000012;; it under the terms of the GNU General Public License as published by
1fecc8fe
GM
Glenn Morris2008-05-06 04:34:22 +000013;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +000015
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 Glenn Morris2008-05-06 04:34:22 +000022;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +000023
22a89ee8 Eric S. Raymond1992-07-14 21:51:51 +000024;;; Commentary:
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +000025
e5d84bfe
EZ
Eli Zaretskii2011-12-10 12:49:33 +020026;; Known bug: update commands fail to ignore @ignore, and fail to DTRT
27;; with the @if... directives (so expect trouble when the manual uses
28;; different @node lines or @menu items in @iftex and in @ifnottex).
b578f267
EN
Erik Naggum1996-01-14 07:34:30 +000029
30;; Summary: how to use the updating commands
31
32;; The node and menu updating functions automatically
33
34;; * insert missing `@node' lines,
35;; * insert the `Next', `Previous' and `Up' pointers of a node,
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +000036;; * insert or update the menu for a section,
b578f267
EN
Erik Naggum1996-01-14 07:34:30 +000037;; * create a master menu for a Texinfo source file.
38;;
78897d3d Richard M. Stallman1997-04-05 21:59:09 +000039;; With a prefix argument, the `texinfo-update-node' and
b578f267
EN
Erik Naggum1996-01-14 07:34:30 +000040;; `texinfo-make-menu' functions do their jobs in the region.
41;;
e5d84bfe
EZ
Eli Zaretskii2011-12-10 12:49:33 +020042;; Important note: We do NOT recommend use of these commands to update
43;; the `Next', `Previous' and `Up' pointers on @node lines. Most
22bcf204 Paul Eggert2011-12-30 17:27:15 -080044;; manuals, including those whose Texinfo files adhere to the structure
e5d84bfe
EZ
Eli Zaretskii2011-12-10 12:49:33 +020045;; described below, don't need these pointers, because makeinfo will
46;; generate them automatically (see the node "makeinfo Pointer
47;; Creation" in the Texinfo manual). By contrast, due to known bugs
48;; described above, texinfo-update-node etc. could produce incorrect
49;; pointers, and thus make a perfectly valid Texinfo file into an
50;; invalid one. You _have_ been warned!
51;;
b578f267 Erik Naggum1996-01-14 07:34:30 +000052;; In brief, the functions for creating or updating nodes and menus, are:
d1f9f441
RS
Richard M. Stallman1997-08-04 01:11:01 +000053;;
54;; texinfo-update-node (&optional beginning end)
55;; texinfo-every-node-update ()
b578f267 Erik Naggum1996-01-14 07:34:30 +000056;; texinfo-sequential-node-update (&optional region-p)
d1f9f441
RS
Richard M. Stallman1997-08-04 01:11:01 +000057;;
58;; texinfo-make-menu (&optional beginning end)
59;; texinfo-all-menus-update ()
b578f267
EN
Erik Naggum1996-01-14 07:34:30 +000060;; texinfo-master-menu ()
61;;
62;; texinfo-insert-node-lines (&optional title-p)
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +000063;;
b578f267
EN
Erik Naggum1996-01-14 07:34:30 +000064;; texinfo-indent-menu-description (column &optional region-p)
65
66;; The `texinfo-column-for-description' variable specifies the column to
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +000067;; which menu descriptions are indented.
b578f267
EN
Erik Naggum1996-01-14 07:34:30 +000068
69;; Texinfo file structure
70;; ----------------------
71
72;; To use the updating commands, you must structure your Texinfo file
73;; hierarchically. Each `@node' line, with the exception of the top
74;; node, must be accompanied by some kind of section line, such as an
75;; `@chapter' or `@section' line. Each node-line/section-line
76;; combination must look like this:
77
78;; @node Lists and Tables, Cross References, Structuring, Top
79;; @comment node-name, next, previous, up
80;; @chapter Making Lists and Tables
81
82;; or like this (without the `@comment' line):
83
84;; @node Lists and Tables, Cross References, Structuring, Top
85;; @chapter Making Lists and Tables
86
87;; If the file has a `top' node, it must be called `top' or `Top' and
88;; be the first node in the file.
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +000089
90\f
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +000091;;; The update node functions described in detail
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +000092
78897d3d Richard M. Stallman1997-04-05 21:59:09 +000093;; The `texinfo-update-node' command with no prefix argument inserts
b578f267
EN
Erik Naggum1996-01-14 07:34:30 +000094;; the correct next, previous and up pointers for the node in which
95;; point is located (i.e., for the node preceding point).
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +000096
78897d3d Richard M. Stallman1997-04-05 21:59:09 +000097;; With prefix argument, the `texinfo-update-node' function inserts the
b578f267
EN
Erik Naggum1996-01-14 07:34:30 +000098;; correct next, previous and up pointers for the nodes inside the
99;; region.
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000100
b578f267
EN
Erik Naggum1996-01-14 07:34:30 +0000101;; It does not matter whether the `@node' line has pre-existing
102;; `Next', `Previous', or `Up' pointers in it. They are removed.
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000103
e5d84bfe
EZ
Eli Zaretskii2011-12-10 12:49:33 +0200104;; Warning: Since the pre-existing pointers are replaced with the ones
105;; computed by `texinfo-update-node', and since this function has
106;; known bugs with the more advanced Texinfo features (see above), it
107;; could produce an invalid Texinfo file. You are well advised not to
108;; use this function, except if you know what you are doing and
109;; exercise extreme caution. Keep in mind that most manuals do not
110;; need the `Next', `Previous', and `Up' pointers to be present on the
111;; @node lines; makeinfo will automatically generate them when it
112;; produces the Info or HTML versions of the manual.
113
b578f267
EN
Erik Naggum1996-01-14 07:34:30 +0000114;; The `texinfo-every-node-update' function runs `texinfo-update-node'
115;; on the whole buffer.
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000116
b578f267
EN
Erik Naggum1996-01-14 07:34:30 +0000117;; The `texinfo-sequential-node-update' function inserts the
118;; immediately following and preceding node into the `Next' or
119;; `Previous' pointers regardless of their hierarchical level. This is
120;; only useful for certain kinds of text, like a novel, which you go
121;; through sequentially.
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000122
123\f
124;;; The menu making functions described in detail
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000125
b578f267
EN
Erik Naggum1996-01-14 07:34:30 +0000126;; The `texinfo-make-menu' function without an argument creates or
127;; updates a menu for the section encompassing the node that follows
128;; point. With an argument, it makes or updates menus for the nodes
129;; within or part of the marked region.
130
131;; Whenever an existing menu is updated, the descriptions from
132;; that menu are incorporated into the new menu. This is done by copying
133;; descriptions from the existing menu to the entries in the new menu
134;; that have the same node names. If the node names are different, the
135;; descriptions are not copied to the new menu.
136
137;; Menu entries that refer to other Info files are removed since they
138;; are not a node within current buffer. This is a deficiency.
139
140;; The `texinfo-all-menus-update' function runs `texinfo-make-menu'
141;; on the whole buffer.
142
143;; The `texinfo-master-menu' function creates an extended menu located
e5d84bfe Eli Zaretskii2011-12-10 12:49:33 +0200144;; after the top node. (The file must have a top node.) This
b8830d28 Eli Zaretskii2011-12-10 14:22:25 +0200145;; function works only on Texinfo files all of whose menus are
e5d84bfe Eli Zaretskii2011-12-10 12:49:33 +0200146;; present in a single file; use `texinfo-multiple-files-update' for
b8830d28
EZ
Eli Zaretskii2011-12-10 14:22:25 +0200147;; multi-file manuals. The function constructs a master menu that
148;; includes every entry from every other menu. Use this command to
149;; create or update the @detailmenu menu after you've created or
150;; updated all the menus in the file, including the menu in the Top
151;; node, using the `texinfo-make-menu' or the `texinfo-all-menus-update'
152;; command.
b578f267
EN
Erik Naggum1996-01-14 07:34:30 +0000153
154;; The `texinfo-indent-menu-description' function indents every
155;; description in the menu following point, to the specified column.
156;; Non-nil argument (prefix, if interactive) means indent every
157;; description in every menu in the region. This function does not
158;; indent second and subsequent lines of a multi-line description.
159
160;; The `texinfo-insert-node-lines' function inserts `@node' before the
161;; `@chapter', `@section', and such like lines of a region in a Texinfo
162;; file where the `@node' lines are missing.
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000163;;
b578f267
EN
Erik Naggum1996-01-14 07:34:30 +0000164;; With a non-nil argument (prefix, if interactive), the function not
165;; only inserts `@node' lines but also inserts the chapter or section
166;; titles as the names of the corresponding nodes; and inserts titles
167;; as node names in pre-existing `@node' lines that lack names.
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000168;;
b578f267 Erik Naggum1996-01-14 07:34:30 +0000169;; Since node names should be more concise than section or chapter
e5d84bfe Eli Zaretskii2011-12-10 12:49:33 +0200170;; titles, you will usually want to manually edit node names so inserted.
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000171
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000172\f
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000173;;; Code:
174
6f602bd0 Stefan Monnier2000-11-06 15:53:05 +0000175(require 'texinfo)
80216a47 Richard M. Stallman1997-08-30 19:48:14 +0000176
80216a47 Richard M. Stallman1997-08-30 19:48:14 +0000177
155b48df
RS
Richard M. Stallman1997-07-18 02:57:14 +0000178(defvar texinfo-master-menu-header
179 " --- The Detailed Node Listing ---\n"
180 "String inserted before lower level entries in Texinfo master menu.
181It comes after the chapter-level menu entries.")
182
739da925
SM
Stefan Monnier2001-10-25 03:39:43 +0000183;; We used to look for just sub, but that found @subtitle.
184(defvar texinfo-section-types-regexp
185 "^@\\(chapter \\|sect\\|subs\\|subh\\|unnum\\|major\\|chapheading \\|heading \\|appendix\\)"
186 "Regexp matching chapter, section, other headings (but not the top node).")
187
188(defvar texinfo-section-level-regexp
189 (regexp-opt (texinfo-filter 3 texinfo-section-list))
190 "Regular expression matching just the Texinfo section level headings.")
191
192(defvar texinfo-subsection-level-regexp
193 (regexp-opt (texinfo-filter 4 texinfo-section-list))
194 "Regular expression matching just the Texinfo subsection level headings.")
195
196(defvar texinfo-subsubsection-level-regexp
197 (regexp-opt (texinfo-filter 5 texinfo-section-list))
198 "Regular expression matching just the Texinfo subsubsection level headings.")
199
200(defvar texinfo-update-menu-same-level-regexps
201 '((1 . "top[ \t]+")
202 (2 . (concat "\\(^@\\)\\(" texinfo-chapter-level-regexp "\\)\\>[ \t]*"))
203 (3 . (concat "\\(^@\\)\\(" texinfo-section-level-regexp "\\)\\>[ \t]*"))
204 (4 . (concat "\\(^@\\)\\(" texinfo-subsection-level-regexp "\\)\\>[ \t]+"))
205 (5 . (concat "\\(^@\\)\\(" texinfo-subsubsection-level-regexp "\\)\\>[ \t]+")))
fb7ada5f Chong Yidong2012-04-09 21:05:48 +0800206 "Regexps for searching for same level sections in a Texinfo file.
739da925
SM
Stefan Monnier2001-10-25 03:39:43 +0000207The keys are strings specifying the general hierarchical level in the
208document; the values are regular expressions.")
209
210(defvar texinfo-update-menu-higher-regexps
211 '((1 . "^@node [ \t]*DIR")
212 (2 . "^@node [ \t]*top[ \t]*\\(,\\|$\\)")
213 (3 .
214 (concat
215 "\\(^@\\("
216 texinfo-chapter-level-regexp
217 "\\)\\>[ \t]*\\)"))
218 (4 .
219 (concat
220 "\\(^@\\("
221 texinfo-section-level-regexp
222 "\\|"
223 texinfo-chapter-level-regexp
224 "\\)\\>[ \t]*\\)"))
225 (5 .
226 (concat
227 "\\(^@\\("
228 texinfo-subsection-level-regexp
229 "\\|"
230 texinfo-section-level-regexp
231 "\\|"
232 texinfo-chapter-level-regexp
233 "\\)\\>[ \t]*\\)")))
fb7ada5f Chong Yidong2012-04-09 21:05:48 +0800234 "Regexps for searching for higher level sections in a Texinfo file.
739da925
SM
Stefan Monnier2001-10-25 03:39:43 +0000235The keys are strings specifying the general hierarchical level in the
236document; the values are regular expressions.")
237
238(defvar texinfo-update-menu-lower-regexps
239 '((1 .
240 (concat
241 "\\(^@\\("
242 texinfo-chapter-level-regexp
243 "\\|"
244 texinfo-section-level-regexp
245 "\\|"
246 texinfo-subsection-level-regexp
247 "\\|"
248 texinfo-subsubsection-level-regexp
249 "\\)\\>[ \t]*\\)"))
250 (2 .
251 (concat
252 "\\(^@\\("
253 texinfo-section-level-regexp
254 "\\|"
255 texinfo-subsection-level-regexp
256 "\\|"
257 texinfo-subsubsection-level-regexp
258 "\\)\\>[ \t]*\\)"))
259 (3 .
260 (concat
261 "\\(^@\\("
262 texinfo-subsection-level-regexp
263 "\\|"
264 texinfo-subsubsection-level-regexp
265 "\\)\\>[ \t]+\\)"))
266 (4 .
267 (concat
268 "\\(^@\\("
269 texinfo-subsubsection-level-regexp
270 "\\)\\>[ \t]+\\)"))
271 ;; There's nothing below 5, use a bogus regexp that can't match.
272 (5 . "a\\(^\\)"))
fb7ada5f Chong Yidong2012-04-09 21:05:48 +0800273 "Regexps for searching for lower level sections in a Texinfo file.
739da925
SM
Stefan Monnier2001-10-25 03:39:43 +0000274The keys are strings specifying the general hierarchical level in the
275document; the values are regular expressions.")
276
277\f
78897d3d Richard M. Stallman1997-04-05 21:59:09 +0000278(defun texinfo-make-menu (&optional beginning end)
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000279 "Without any prefix argument, make or update a menu.
280Make the menu for the section enclosing the node found following point.
281
78897d3d Richard M. Stallman1997-04-05 21:59:09 +0000282A prefix argument means make or update menus
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000283for nodes within or part of the marked region.
284
285Whenever a menu exists, and is being updated, the descriptions that
286are associated with node names in the pre-existing menu are
70e2c6a4
RC
Robert J. Chassell2002-02-20 17:39:38 +0000287incorporated into the new menu.
288
289Leaves trailing whitespace in a menu that lacks descriptions, so
290descriptions will format well. In general, a menu should contain
291descriptions, because node names and section titles are often too
292short to explain a node well."
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000293
78897d3d
RS
Richard M. Stallman1997-04-05 21:59:09 +0000294 (interactive
295 (if prefix-arg
296 (list (point) (mark))))
297 (if (null beginning)
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000298 (let ((level (texinfo-hierarchic-level)))
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000299 (texinfo-make-one-menu level)
300 (message "Menu updated"))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000301 ;; else
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000302 (message "Making or updating menus in %s... " (buffer-name))
78897d3d
RS
Richard M. Stallman1997-04-05 21:59:09 +0000303 (save-excursion
304 (goto-char (min beginning end))
305 ;; find section type following point
306 (let ((level (texinfo-hierarchic-level))
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000307 (region-end-marker (make-marker)))
308 (set-marker region-end-marker (max beginning end))
309 (save-restriction
310 (widen)
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000311
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000312 (while (texinfo-find-lower-level-node
313 level (marker-position region-end-marker))
314 (setq level (texinfo-hierarchic-level)) ; new, lower level
315 (texinfo-make-one-menu level))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000316
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000317 (while (and (< (point) (marker-position region-end-marker))
318 (texinfo-find-higher-level-node
319 level (marker-position region-end-marker)))
320 (setq level (texinfo-hierarchic-level))
af584472
RS
Richard M. Stallman1998-10-11 12:47:19 +0000321 ;; Don't allow texinfo-find-higher-level-node
322 ;; to find the same node again.
323 (forward-line 1)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000324 (while (texinfo-find-lower-level-node
325 level (marker-position region-end-marker))
326 (setq level (texinfo-hierarchic-level)) ; new, lower level
327 (texinfo-make-one-menu level))))))
78897d3d Richard M. Stallman1997-04-05 21:59:09 +0000328 (message "Making or updating menus in %s...done" (buffer-name))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000329
330(defun texinfo-make-one-menu (level)
331 "Make a menu of all the appropriate nodes in this section.
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000332`Appropriate nodes' are those associated with sections that are
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000333at the level specified by LEVEL. Point is left at the end of menu."
334 (let*
335 ((case-fold-search t)
82ac4b77
JB
Jim Blandy1990-12-20 20:38:22 +0000336 (beginning
337 (save-excursion
338 (goto-char (texinfo-update-menu-region-beginning level))
339 (end-of-line)
340 (point)))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000341 (end (texinfo-update-menu-region-end level))
342 (first (texinfo-menu-first-node beginning end))
343 (node-name (progn
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000344 (goto-char beginning)
345 (beginning-of-line)
346 (texinfo-copy-node-name)))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000347 (new-menu-list (texinfo-make-menu-list beginning end level)))
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +0000348 (when (texinfo-old-menu-p beginning first)
349 (texinfo-incorporate-descriptions new-menu-list)
350 (texinfo-incorporate-menu-entry-names new-menu-list)
351 (texinfo-delete-old-menu beginning first))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000352 (texinfo-insert-menu new-menu-list node-name)))
353
354(defun texinfo-all-menus-update (&optional update-all-nodes-p)
355 "Update every regular menu in a Texinfo file.
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000356Update pre-existing master menu, if there is one.
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000357
e5d84bfe
EZ
Eli Zaretskii2011-12-10 12:49:33 +0200358Only single-file manuals are supported by this function. For
359multi-file manuals, use `texinfo-multiple-files-update'.
360
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000361If called with a non-nil argument, this function first updates all the
e5d84bfe
EZ
Eli Zaretskii2011-12-10 12:49:33 +0200362nodes in the buffer before updating the menus. Do NOT invoke this
363command with an argument if your Texinfo file uses @node lines without
364the `Next', `Previous', and `Up' pointers!
70e2c6a4
RC
Robert J. Chassell2002-02-20 17:39:38 +0000365
366Indents the first line of descriptions, and leaves trailing whitespace
367in a menu that lacks descriptions, so descriptions will format well.
368In general, a menu should contain descriptions, because node names and
369section titles are often too short to explain a node well."
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000370 (interactive "P")
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000371 (let ((case-fold-search t)
91c6f26e Karl Heuer1999-01-25 17:04:23 +0000372 master-menu-p)
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000373 (save-excursion
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000374 (push-mark (point-max) t)
375 (goto-char (point-min))
376 (message "Checking for a master menu in %s ... "(buffer-name))
377 (save-excursion
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +0000378 (when (search-forward texinfo-master-menu-header nil t)
379 ;; Check if @detailmenu kludge is used;
380 ;; if so, leave point before @detailmenu.
dea53a43 Glenn Morris2010-11-09 20:35:18 -0800381 (search-backward "\n@detailmenu" (line-beginning-position -2) t)
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +0000382 ;; Remove detailed master menu listing
383 (setq master-menu-p t)
384 (goto-char (match-beginning 0))
385 (let ((end-of-detailed-menu-descriptions
386 (save-excursion ; beginning of end menu line
387 (goto-char (texinfo-menu-end))
388 (beginning-of-line) (forward-char -1)
389 (point))))
390 (delete-region (point) end-of-detailed-menu-descriptions))))
391
392 (when update-all-nodes-p
393 (message "Updating all nodes in %s ... " (buffer-name))
394 (texinfo-update-node (point-min) (point-max)))
d1f9f441
RS
Richard M. Stallman1997-08-04 01:11:01 +0000395
396 (message "Updating all menus in %s ... " (buffer-name))
78897d3d Richard M. Stallman1997-04-05 21:59:09 +0000397 (texinfo-make-menu (point-max) (point-min))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000398
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +0000399 (when master-menu-p
400 (message "Updating the master menu in %s... " (buffer-name))
401 (texinfo-master-menu nil)))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000402
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000403 (message "Done...updated all the menus. You may save the buffer.")))
404
405(defun texinfo-find-lower-level-node (level region-end)
406 "Search forward from point for node at any level lower than LEVEL.
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000407Search is limited to the end of the marked region, REGION-END,
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000408and to the end of the menu region for the level.
409
410Return t if the node is found, else nil. Leave point at the beginning
411of the node if one is found; else do not move point."
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000412 (let ((case-fold-search t))
413 (if (and (< (point) region-end)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000414 (re-search-forward
415 (concat
416 "\\(^@node\\).*\n" ; match node line
417 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
418 "\\|" ; or
21324fae
GM
Gerd Moellmann2001-08-16 17:59:38 +0000419 "\\(^@ifinfo[ ]*\n\\)" ; ifinfo line, if any
420 "\\|" ; or
421 "\\(^@ifnottex[ ]*\n\\)" ; ifnottex line, if any
422 "\\)?" ; end of expression
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000423 (eval (cdr (assoc level texinfo-update-menu-lower-regexps))))
424 ;; the next higher level node marks the end of this
425 ;; section, and no lower level node will be found beyond
426 ;; this position even if region-end is farther off
427 (texinfo-update-menu-region-end level)
428 t))
429 (goto-char (match-beginning 1)))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000430
431(defun texinfo-find-higher-level-node (level region-end)
432 "Search forward from point for node at any higher level than argument LEVEL.
433Search is limited to the end of the marked region, REGION-END.
434
435Return t if the node is found, else nil. Leave point at the beginning
af584472
RS
Richard M. Stallman1998-10-11 12:47:19 +0000436of the node if one is found; else do not move point.
437
438A `@node' line starting at point does count as a match;
439if the match is found there, the value is t and point does not move."
440
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000441 (let ((case-fold-search t))
442 (cond
6f602bd0 Stefan Monnier2000-11-06 15:53:05 +0000443 ((< level 3)
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000444 (if (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" region-end t)
91c6f26e Karl Heuer1999-01-25 17:04:23 +0000445 (progn (beginning-of-line) t)))
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000446 (t
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +0000447 (when (re-search-forward
448 (concat
449 "\\(^@node\\).*\n" ; match node line
450 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
451 "\\|" ; or
452 "\\(^@ifinfo[ ]*\n\\)" ; ifinfo line, if any
453 "\\|" ; or
454 "\\(^@ifnottex[ ]*\n\\)" ; ifnottex line, if any
455 "\\)?" ; end of expression
456 (eval (cdr (assoc level texinfo-update-menu-higher-regexps))))
457 region-end t)
458 (beginning-of-line) t)))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000459
460\f
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000461;;; Making the list of new menu entries
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000462
463(defun texinfo-make-menu-list (beginning end level)
464 "Make a list of node names and their descriptions.
465Point is left at the end of the menu region, but the menu is not inserted.
466
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000467First argument is position from which to start making menu list;
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000468second argument is end of region in which to try to locate entries;
469third argument is the level of the nodes that are the entries.
470
471Node names and descriptions are dotted pairs of strings. Each pair is
472an element of the list. If the description does not exist, the
473element consists only of the node name."
474 (goto-char beginning)
475 (let (new-menu-list)
476 (while (texinfo-menu-locate-entry-p level end)
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +0000477 (push (cons
478 (texinfo-copy-node-name)
479 (prog1 "" (forward-line 1)))
480 ;; Use following to insert section titles automatically.
481 ;; (texinfo-copy-section-title))
482 new-menu-list))
483 (nreverse new-menu-list)))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000484
485(defun texinfo-menu-locate-entry-p (level search-end)
486 "Find a node that will be part of menu for this section.
487First argument is a string such as \"section\" specifying the general
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000488hierarchical level of the menu; second argument is a position
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000489specifying the end of the search.
490
491The function returns t if the node is found, else nil. It searches
492forward from point, and leaves point at the beginning of the node.
493
494The function finds entries of the same type. Thus `subsections' and
495`unnumberedsubsecs' will appear in the same menu."
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000496 (let ((case-fold-search t))
497 (if (re-search-forward
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000498 (concat
499 "\\(^@node\\).*\n" ; match node line
500 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
501 "\\|" ; or
21324fae
GM
Gerd Moellmann2001-08-16 17:59:38 +0000502 "\\(^@ifinfo[ ]*\n\\)" ; ifinfo line, if any
503 "\\|" ; or
504 "\\(^@ifnottex[ ]*\n\\)" ; ifnottex line, if any
505 "\\)?" ; end of expression
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000506 (eval
507 (cdr (assoc level texinfo-update-menu-same-level-regexps))))
508 search-end
509 t)
510 (goto-char (match-beginning 1)))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000511
512(defun texinfo-copy-node-name ()
513 "Return the node name as a string.
514
515Start with point at the beginning of the node line; copy the text
516after the node command up to the first comma on the line, if any, and
517return the text as a string. Leaves point at the beginning of the
518line. If there is no node name, returns an empty string."
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000519
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000520 (save-excursion
521 (buffer-substring
522 (progn (forward-word 1) ; skip over node command
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000523 (skip-chars-forward " \t") ; and over spaces
524 (point))
b9f60161 Stefan Monnier2001-10-28 03:24:14 +0000525 (if (search-forward "," (line-end-position) t) ; bound search
91c6f26e Karl Heuer1999-01-25 17:04:23 +0000526 (1- (point))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000527 (end-of-line) (point)))))
528
529(defun texinfo-copy-section-title ()
530 "Return the title of the section as a string.
531The title is used as a description line in the menu when one does not
532already exist.
533
534Move point to the beginning of the appropriate section line by going
535to the start of the text matched by last regexp searched for, which
536must have been done by `texinfo-menu-locate-entry-p'."
537
538 ;; could use the same re-search as in `texinfo-menu-locate-entry-p'
539 ;; instead of using `match-beginning'; such a variation would be
540 ;; more general, but would waste information already collected
541
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000542 (goto-char (match-beginning 7)) ; match section name
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000543
544 (buffer-substring
545 (progn (forward-word 1) ; skip over section type
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000546 (skip-chars-forward " \t") ; and over spaces
547 (point))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000548 (progn (end-of-line) (point))))
549
550\f
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000551;;; Handling the old menu
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000552
553(defun texinfo-old-menu-p (beginning first)
554 "Move point to the beginning of the menu for this section, if any.
555Otherwise move point to the end of the first node of this section.
556Return t if a menu is found, nil otherwise.
557
558First argument is the position of the beginning of the section in which
559the menu will be located; second argument is the position of the first
560node within the section.
561
562If no menu is found, the function inserts two newlines just before the
563end of the section, and leaves point there where a menu ought to be."
564 (goto-char beginning)
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +0000565 (if (re-search-forward "^@menu" first 'goto-end)
566 t
567 (insert "\n\n") (forward-line -2) nil))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000568
569(defun texinfo-incorporate-descriptions (new-menu-list)
570 "Copy the old menu line descriptions that exist to the new menu.
571
572Point must be at beginning of old menu.
573
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000574If the node-name of the new menu is found in the old menu, insert the
575old description into the new entry.
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000576
577For this function, the new menu is a list made up of lists of dotted
578pairs in which the first element of the pair is the node name and the
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000579second element the description. The new menu is changed destructively.
06cc857d Richard M. Stallman1997-06-15 17:39:13 +0000580The old menu is the menu as it appears in the Texinfo file."
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000581
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +0000582 (let ((end-of-menu (texinfo-menu-end)))
583 (dolist (new-menu new-menu-list new-menu-list)
584 (save-excursion ; keep point at beginning of menu
585 (when (re-search-forward
586 ;; Existing nodes can have the form
587 ;; * NODE NAME:: DESCRIPTION
588 ;; or
589 ;; * MENU ITEM: NODE NAME. DESCRIPTION.
590 ;;
591 ;; Recognize both when looking for the description.
592 (concat "\\* \\(" ; so only menu entries are found
593 (regexp-quote (car new-menu)) "::"
594 "\\|"
595 ".*: " (regexp-quote (car new-menu)) "[.,\t\n]"
596 "\\)"
597 ) ; so only complete entries are found
598 end-of-menu
599 t)
600 (setcdr new-menu (texinfo-menu-copy-old-description end-of-menu)))))))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000601
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000602(defun texinfo-incorporate-menu-entry-names (new-menu-list)
603 "Copy any old menu entry names to the new menu.
604
605Point must be at beginning of old menu.
606
607If the node-name of the new menu entry cannot be found in the old
608menu, do nothing.
609
610For this function, the new menu is a list made up of lists of dotted
611pairs in which the first element of the pair is the node name and the
612second element is the description (or nil).
613
614If we find an existing menu entry name, we change the first element of
615the pair to be another dotted pair in which the car is the menu entry
616name and the cdr is the node name.
617
618NEW-MENU-LIST is changed destructively. The old menu is the menu as it
619appears in the texinfo file."
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000620
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +0000621 (let ((end-of-menu (texinfo-menu-end)))
622 (dolist (new-menu new-menu-list new-menu-list)
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000623 (save-excursion ; keep point at beginning of menu
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000624 (if (re-search-forward
625 ;; Existing nodes can have the form
626 ;; * NODE NAME:: DESCRIPTION
627 ;; or
628 ;; * MENU ITEM: NODE NAME. DESCRIPTION.
629 ;;
630 ;; We're interested in the second case.
631 (concat "\\* " ; so only menu entries are found
b9f60161 Stefan Monnier2001-10-28 03:24:14 +0000632 "\\(.*\\): " (regexp-quote (car new-menu))
ca722adb Karl Heuer1998-03-14 09:08:26 +0000633 "[.,\t\n]")
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000634 end-of-menu
635 t)
636 (setcar
b9f60161 Stefan Monnier2001-10-28 03:24:14 +0000637 new-menu ; replace the node name
91c6f26e Karl Heuer1999-01-25 17:04:23 +0000638 (cons (buffer-substring (match-beginning 1) (match-end 1))
b9f60161 Stefan Monnier2001-10-28 03:24:14 +0000639 (car new-menu))))))))
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000640
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000641(defun texinfo-menu-copy-old-description (end-of-menu)
642 "Return description field of old menu line as string.
643Point must be located just after the node name. Point left before description.
644Single argument, END-OF-MENU, is position limiting search."
645 (skip-chars-forward "[:.,\t\n ]+")
646 ;; don't copy a carriage return at line beginning with asterisk!
f6472377 Eli Zaretskii2007-02-03 14:02:45 +0000647 ;; don't copy @detailmenu or @end menu or @ignore as descriptions!
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000648 ;; do copy a description that begins with an `@'!
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000649 ;; !! Known bug: does not copy descriptions starting with ^|\{?* etc.
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000650 (if (and (looking-at "\\(\\w+\\|@\\)")
f6472377
EZ
Eli Zaretskii2007-02-03 14:02:45 +0000651 (not (looking-at
652 "\\(^\\* \\|^@detailmenu\\|^@end menu\\|^@ignore\\)")))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000653 (buffer-substring
654 (point)
655 (save-excursion
f6472377 Eli Zaretskii2007-02-03 14:02:45 +0000656 (re-search-forward "\\(^\\* \\|^@ignore\\|^@end menu\\)" end-of-menu t)
dea53a43 Glenn Morris2010-11-09 20:35:18 -0800657 (line-end-position 0))) ; end of last description line
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000658 ""))
659
660(defun texinfo-menu-end ()
06cc857d Richard M. Stallman1997-06-15 17:39:13 +0000661 "Return position of end of menu, but don't move point.
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000662Signal an error if not end of menu."
663 (save-excursion
664 (if (re-search-forward "^@end menu" nil t)
91c6f26e Karl Heuer1999-01-25 17:04:23 +0000665 (point)
5b02e712 Eli Zaretskii2001-08-26 16:49:59 +0000666 (error "Menu does not have an end"))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000667
668(defun texinfo-delete-old-menu (beginning first)
669 "Delete the old menu. Point must be in or after menu.
670First argument is position of the beginning of the section in which
671the menu will be located; second argument is the position of the first
672node within the section."
673 ;; No third arg to search, so error if search fails.
674 (re-search-backward "^@menu" beginning)
675 (delete-region (point)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000676 (save-excursion
677 (re-search-forward "^@end menu" first)
678 (point))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000679
680\f
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000681;;; Inserting new menu
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000682
683;; try 32, but perhaps 24 is better
684(defvar texinfo-column-for-description 32
fb7ada5f Chong Yidong2012-04-09 21:05:48 +0800685 "Column at which descriptions start in a Texinfo menu.")
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000686
687(defun texinfo-insert-menu (menu-list node-name)
688 "Insert formatted menu at point.
70e2c6a4
RC
Robert J. Chassell2002-02-20 17:39:38 +0000689Indents the first line of descriptions, if any, to the value of
690texinfo-column-for-description. Indenting leaves trailing whitespace
691in a menu that lacks descriptions, so descriptions will format well.
692In general, a menu should contain descriptions, because node names and
693section titles are often too short to explain a node well.
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000694
695MENU-LIST has form:
696
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000697 \(\(\"node-name1\" . \"description\"\)
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000698 \(\"node-name2\" . \"description\"\) ... \)
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000699
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000700However, the description field might be nil.
701
702Also, the node-name field might itself be a dotted pair (call it P) of
703strings instead of just a string. In that case, the car of P
704is the menu entry name, and the cdr of P is the node name."
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000705
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000706 (insert "@menu\n")
b9f60161 Stefan Monnier2001-10-28 03:24:14 +0000707 (dolist (menu menu-list)
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000708 ;; Every menu entry starts with a star and a space.
709 (insert "* ")
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000710
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000711 ;; Insert the node name (and menu entry name, if present).
b9f60161 Stefan Monnier2001-10-28 03:24:14 +0000712 (let ((node-part (car menu)))
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000713 (if (stringp node-part)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000714 ;; "Double colon" entry line; menu entry and node name are the same,
715 (insert (format "%s::" node-part))
716 ;; "Single colon" entry line; menu entry and node name are different.
717 (insert (format "%s: %s." (car node-part) (cdr node-part)))))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000718
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000719 ;; Insert the description, if present.
9a51c6c7 Richard Kim2011-07-03 18:11:37 +0200720 (when (> (length (cdr menu)) 0)
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +0000721 ;; Move to right place.
722 (indent-to texinfo-column-for-description 2)
723 ;; Insert description.
724 (insert (format "%s" (cdr menu))))
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000725
b9f60161 Stefan Monnier2001-10-28 03:24:14 +0000726 (insert "\n")) ; end this menu entry
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000727 (insert "@end menu")
a056c2c7
RC
Robert J. Chassell2002-03-03 17:30:07 +0000728 (let ((level (texinfo-hierarchic-level)))
729 (message
730 "Updated level \"%s\" menu following node: %s ... " level node-name)))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000731
732\f
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000733;;; Starting menu descriptions by inserting titles
734
735(defun texinfo-start-menu-description ()
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000736 "In this menu entry, insert the node's section title as a description.
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000737Position point at beginning of description ready for editing.
738Do not insert a title if the line contains an existing description.
739
740You will need to edit the inserted text since a useful description
741complements the node name rather than repeats it as a title does."
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000742
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000743 (interactive)
744 (let (beginning end node-name title)
745 (save-excursion
b9f60161 Stefan Monnier2001-10-28 03:24:14 +0000746 (beginning-of-line)
5ed619e0 Glenn Morris2010-11-06 13:23:42 -0700747 (if (search-forward "* " (line-end-position) t)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000748 (progn (skip-chars-forward " \t")
749 (setq beginning (point)))
5b02e712 Eli Zaretskii2001-08-26 16:49:59 +0000750 (error "This is not a line in a menu"))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000751
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000752 (cond
b9f60161 Stefan Monnier2001-10-28 03:24:14 +0000753 ;; "Double colon" entry line; menu entry and node name are the same,
5ed619e0 Glenn Morris2010-11-06 13:23:42 -0700754 ((search-forward "::" (line-end-position) t)
91c6f26e Karl Heuer1999-01-25 17:04:23 +0000755 (if (looking-at "[ \t]*[^ \t\n]+")
5b02e712 Eli Zaretskii2001-08-26 16:49:59 +0000756 (error "Descriptive text already exists"))
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000757 (skip-chars-backward ": \t")
758 (setq node-name (buffer-substring beginning (point))))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000759
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000760 ;; "Single colon" entry line; menu entry and node name are different.
5ed619e0 Glenn Morris2010-11-06 13:23:42 -0700761 ((search-forward ":" (line-end-position) t)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000762 (skip-chars-forward " \t")
763 (setq beginning (point))
764 ;; Menu entry line ends in a period, comma, or tab.
dea53a43 Glenn Morris2010-11-09 20:35:18 -0800765 (if (re-search-forward "[.,\t]" (line-beginning-position 2) t)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000766 (progn
767 (if (looking-at "[ \t]*[^ \t\n]+")
5b02e712 Eli Zaretskii2001-08-26 16:49:59 +0000768 (error "Descriptive text already exists"))
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000769 (skip-chars-backward "., \t")
770 (setq node-name (buffer-substring beginning (point))))
771 ;; Menu entry line ends in a return.
dea53a43 Glenn Morris2010-11-09 20:35:18 -0800772 (re-search-forward ".*\n" (line-beginning-position 2) t)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000773 (skip-chars-backward " \t\n")
774 (setq node-name (buffer-substring beginning (point)))
775 (if (= 0 (length node-name))
5b02e712 Eli Zaretskii2001-08-26 16:49:59 +0000776 (error "No node name on this line")
91c6f26e Karl Heuer1999-01-25 17:04:23 +0000777 (insert "."))))
5b02e712 Eli Zaretskii2001-08-26 16:49:59 +0000778 (t (error "No node name on this line")))
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000779 ;; Search for node that matches node name, and copy the section title.
780 (if (re-search-forward
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000781 (concat
782 "^@node[ \t]+"
783 (regexp-quote node-name)
b9f60161 Stefan Monnier2001-10-28 03:24:14 +0000784 ".*\n" ; match node line
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000785 "\\("
786 "\\(\\(^@c \\|^@comment\\).*\n\\)" ; match comment line, if any
787 "\\|" ; or
788 "\\(^@ifinfo[ ]*\n\\)" ; ifinfo line, if any
21324fae
GM
Gerd Moellmann2001-08-16 17:59:38 +0000789 "\\|" ; or
790 "\\(^@ifnottex[ ]*\n\\)" ; ifnottex line, if any
791 "\\)?" ; end of expression
857b864a Andreas Schwab2002-03-01 16:02:42 +0000792 )
91c6f26e Karl Heuer1999-01-25 17:04:23 +0000793 nil t)
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +0000794 (setq title
795 (buffer-substring
796 ;; skip over section type
797 (progn (forward-word 1)
798 ;; and over spaces
799 (skip-chars-forward " \t")
800 (point))
801 (progn (end-of-line)
802 (skip-chars-backward " \t")
803 (point))))
5b02e712 Eli Zaretskii2001-08-26 16:49:59 +0000804 (error "Cannot find node to match node name in menu entry")))
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000805 ;; Return point to the menu and insert the title.
806 (end-of-line)
807 (delete-region
808 (point)
809 (save-excursion (skip-chars-backward " \t") (point)))
810 (indent-to texinfo-column-for-description 2)
811 (save-excursion (insert title))))
812
813\f
814;;; Handling description indentation
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000815
06cc857d
RS
Richard M. Stallman1997-06-15 17:39:13 +0000816;; Since the make-menu functions indent descriptions, these functions
817;; are useful primarily for indenting a single menu specially.
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000818
819(defun texinfo-indent-menu-description (column &optional region-p)
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000820 "Indent every description in menu following point to COLUMN.
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000821Non-nil argument (prefix, if interactive) means indent every
822description in every menu in the region. Does not indent second and
823subsequent lines of a multi-line description."
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000824
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000825 (interactive
826 "nIndent menu descriptions to (column number): \nP")
827 (save-excursion
828 (save-restriction
829 (widen)
830 (if (not region-p)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000831 (progn
832 (re-search-forward "^@menu")
833 (texinfo-menu-indent-description column)
834 (message
835 "Indented descriptions in menu. You may save the buffer."))
836 ;;else
837 (message "Indenting every menu description in region... ")
838 (goto-char (region-beginning))
839 (while (and (< (point) (region-end))
840 (texinfo-locate-menu-p))
841 (forward-line 1)
842 (texinfo-menu-indent-description column))
843 (message "Indenting done. You may save the buffer.")))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000844
845(defun texinfo-menu-indent-description (to-column-number)
846 "Indent the Texinfo file menu description to TO-COLUMN-NUMBER.
847Start with point just after the word `menu' in the `@menu' line and
848leave point on the line before the `@end menu' line. Does not indent
849second and subsequent lines of a multi-line description."
850 (let* ((beginning-of-next-line (point)))
851 (while (< beginning-of-next-line
b9f60161 Stefan Monnier2001-10-28 03:24:14 +0000852 (save-excursion ; beginning of end menu line
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000853 (goto-char (texinfo-menu-end))
854 (beginning-of-line)
855 (point)))
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000856
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +0000857 (when (re-search-forward "\\* \\(.*::\\|.*: [^.,\t\n]+[.,\t]\\)"
858 (texinfo-menu-end)
859 t)
860 (let ((beginning-white-space (point)))
861 (skip-chars-forward " \t") ; skip over spaces
862 (if (looking-at "\\(@\\|\\w\\)+") ; if there is text
863 (progn
864 ;; remove pre-existing indentation
865 (delete-region beginning-white-space (point))
866 (indent-to-column to-column-number)))))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000867 ;; position point at beginning of next line
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000868 (forward-line 1)
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000869 (setq beginning-of-next-line (point)))))
870
871\f
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000872;;; Making the master menu
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000873
874(defun texinfo-master-menu (update-all-nodes-menus-p)
875 "Make a master menu for a whole Texinfo file.
e5d84bfe
EZ
Eli Zaretskii2011-12-10 12:49:33 +0200876Remove pre-existing master menu, if there is one.
877
878This function supports only single-file manuals. For multi-file
879manuals, use `texinfo-multiple-files-update'.
880
b8830d28
EZ
Eli Zaretskii2011-12-10 14:22:25 +0200881This function creates or updates the @detailmenu section of a
882master menu that follows the Top node. It replaces any existing
883detailed menu that follows the top node. The detailed menu
884includes every entry from all the other menus. By default, the
885existing menus, including the menu in the Top node, are not
886updated according to the buffer contents, so all the menus should
887be updated first using `texinfo-make-menu' or
888`texinfo-all-menus-update', which see. Alternatively, invoke
889this function with a prefix argument, see below.
890
e5d84bfe
EZ
Eli Zaretskii2011-12-10 12:49:33 +0200891Non-nil, non-numeric argument (C-u prefix, if interactive) means
892first update all existing menus in the buffer (incorporating
893descriptions from pre-existing menus) before it constructs the
894master menu. If the argument is numeric (e.g., \"C-u 2\"),
895update all existing nodes as well, by calling
896\`texinfo-update-node' on the entire file. Warning: do NOT
897invoke with a numeric argument if your Texinfo file uses @node
898lines without the `Next', `Previous', `Up' pointers, as the
899result could be an invalid Texinfo file!
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000900
b8830d28
EZ
Eli Zaretskii2011-12-10 14:22:25 +0200901The function removes and recreates the detailed part of an already
902existing master menu. This action assumes that the pre-existing
903master menu uses the standard `texinfo-master-menu-header' for the
904detailed menu.
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000905
906The master menu has the following format, which is adapted from the
907recommendation in the Texinfo Manual:
908
909 * The first part contains the major nodes in the Texinfo file: the
910 nodes for the chapters, chapter-like sections, and the major
911 appendices. This includes the indices, so long as they are in
912 chapter-like sections, such as unnumbered sections.
913
914 * The second and subsequent parts contain a listing of the other,
915 lower level menus, in order. This way, an inquirer can go
916 directly to a particular node if he or she is searching for
917 specific information.
918
919Each of the menus in the detailed node listing is introduced by the
70e2c6a4
RC
Robert J. Chassell2002-02-20 17:39:38 +0000920title of the section containing the menu.
921
922Indents the first line of descriptions, and leaves trailing whitespace
923in a menu that lacks descriptions, so descriptions will format well.
924In general, a menu should contain descriptions, because node names and
925section titles are often too short to explain a node well."
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000926
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000927 (interactive "P")
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000928 (let ((case-fold-search t))
929 (widen)
930 (goto-char (point-min))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000931
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000932 ;; Move point to location after `top'.
933 (if (not (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t))
5b02e712 Eli Zaretskii2001-08-26 16:49:59 +0000934 (error "This buffer needs a Top node"))
d1f9f441
RS
Richard M. Stallman1997-08-04 01:11:01 +0000935
936 (let ((first-chapter
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000937 (save-excursion
938 (or (re-search-forward "^@node" nil t)
5b02e712 Eli Zaretskii2001-08-26 16:49:59 +0000939 (error "Too few nodes for a master menu"))
91c6f26e Karl Heuer1999-01-25 17:04:23 +0000940 (point))))
155b48df Richard M. Stallman1997-07-18 02:57:14 +0000941 (if (search-forward texinfo-master-menu-header first-chapter t)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000942 (progn
943 ;; Check if @detailmenu kludge is used;
944 ;; if so, leave point before @detailmenu.
dea53a43 Glenn Morris2010-11-09 20:35:18 -0800945 (search-backward "\n@detailmenu" (line-beginning-position -2) t)
06cc857d Richard M. Stallman1997-06-15 17:39:13 +0000946 ;; Remove detailed master menu listing
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000947 (goto-char (match-beginning 0))
948 (let ((end-of-detailed-menu-descriptions
949 (save-excursion ; beginning of end menu line
950 (goto-char (texinfo-menu-end))
951 (beginning-of-line) (forward-char -1)
952 (point))))
953 (delete-region (point) end-of-detailed-menu-descriptions)))))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000954
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000955 (if update-all-nodes-menus-p
91c6f26e Karl Heuer1999-01-25 17:04:23 +0000956 (progn
e5d84bfe
EZ
Eli Zaretskii2011-12-10 12:49:33 +0200957 (when (numberp update-all-nodes-menus-p)
958 (message
959 "Making a master menu in %s ...first updating all nodes... "
960 (buffer-name))
961 (texinfo-update-node (point-min) (point-max)))
91c6f26e Karl Heuer1999-01-25 17:04:23 +0000962 (message "Updating all menus in %s ... " (buffer-name))
91c6f26e Karl Heuer1999-01-25 17:04:23 +0000963 (texinfo-make-menu (point-min) (point-max))))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000964
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +0000965 (message "Now making the master menu in %s... " (buffer-name))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +0000966 (goto-char (point-min))
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000967 (texinfo-insert-master-menu-list
968 (texinfo-master-menu-list))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000969
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000970 ;; Remove extra newlines that texinfo-insert-master-menu-list
971 ;; may have inserted.
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000972
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000973 (save-excursion
974 (goto-char (point-min))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000975
155b48df Richard M. Stallman1997-07-18 02:57:14 +0000976 (if (search-forward texinfo-master-menu-header nil t)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000977 (progn
978 (goto-char (match-beginning 0))
979 ;; Check if @detailmenu kludge is used;
980 ;; if so, leave point before @detailmenu.
dea53a43 Glenn Morris2010-11-09 20:35:18 -0800981 (search-backward "\n@detailmenu" (line-beginning-position -2) t)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +0000982 (insert "\n")
983 (delete-blank-lines)
984 (goto-char (point-min))))
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000985
986 (re-search-forward "^@menu")
987 (forward-line -1)
988 (delete-blank-lines)
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000989
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000990 (re-search-forward "^@end menu")
991 (forward-line 1)
992 (delete-blank-lines))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +0000993
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +0000994 (message
995 "Done...completed making master menu. You may save the buffer.")))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +0000996
997(defun texinfo-master-menu-list ()
998 "Return a list of menu entries and header lines for the master menu.
999
1000Start with the menu for chapters and indices and then find each
1001following menu and the title of the node preceding that menu.
1002
1003The master menu list has this form:
1004
1005 \(\(\(... \"entry-1-2\" \"entry-1\"\) \"title-1\"\)
1006 \(\(... \"entry-2-2\" \"entry-2-1\"\) \"title-2\"\)
1007 ...\)
1008
1009However, there does not need to be a title field."
1010
1011 (let (master-menu-list)
1012 (while (texinfo-locate-menu-p)
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +00001013 (push (list (texinfo-copy-menu) (texinfo-copy-menu-title))
1014 master-menu-list))
1015 (nreverse master-menu-list)))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001016
1017(defun texinfo-insert-master-menu-list (master-menu-list)
1018 "Format and insert the master menu in the current buffer."
1019 (goto-char (point-min))
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00001020 ;; Insert a master menu only after `Top' node and before next node
1021 ;; \(or include file if there is no next node\).
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +00001022 (unless (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t)
1023 (error "This buffer needs a Top node"))
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001024 (let ((first-chapter
91c6f26e Karl Heuer1999-01-25 17:04:23 +00001025 (save-excursion (re-search-forward "^@node\\|^@include") (point))))
b9f60161 Stefan Monnier2001-10-28 03:24:14 +00001026 (unless (re-search-forward "^@menu" first-chapter t)
b8830d28 Eli Zaretskii2011-12-10 14:22:25 +02001027 (error "Buffer lacks a menu in its first node; create it, then run me again")))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001028 (beginning-of-line)
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001029 (delete-region ; buffer must have ordinary top menu
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001030 (point)
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001031 (save-excursion (re-search-forward "^@end menu") (point)))
d1f9f441
RS
Richard M. Stallman1997-08-04 01:11:01 +00001032
1033 (save-excursion
1034 ;; `master-menu-inserted-p' is a kludge to tell
06cc857d
RS
Richard M. Stallman1997-06-15 17:39:13 +00001035 ;; whether to insert @end detailmenu (see bleow)
1036 (let (master-menu-inserted-p)
1037 ;; Handle top of menu
1038 (insert "\n@menu\n")
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +00001039 ;; Insert chapter menu entries. Tell user what is going on.
1040 (message "Inserting chapter menu entry: %s ... "
1041 (car (car master-menu-list)))
1042 (dolist (entry (reverse (car (car master-menu-list))))
1043 (insert "* " entry "\n"))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001044
06cc857d Richard M. Stallman1997-06-15 17:39:13 +00001045 (setq master-menu-list (cdr master-menu-list))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001046
06cc857d
RS
Richard M. Stallman1997-06-15 17:39:13 +00001047 ;; Only insert detailed master menu if there is one....
1048 (if (car (car master-menu-list))
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001049 (progn (setq master-menu-inserted-p t)
1050 (insert (concat "\n@detailmenu\n"
eae582fe Richard M. Stallman1998-06-08 06:12:50 +00001051 texinfo-master-menu-header))))
06cc857d
RS
Richard M. Stallman1997-06-15 17:39:13 +00001052
1053 ;; @detailmenu added 5 Sept 1996 to `texinfo-master-menu-header'
1054 ;; at Karl Berry's request to avert a bug in `makeinfo';
1055 ;; all agree this is a bad kludge and should eventually be removed.
1056 ;; @detailmenu ... @end detailmenu is a noop in `texinfmt.el'.
1057 ;; See @end detailmenu below;
1058 ;; also see `texinfo-all-menus-update' above, `texinfo-master-menu',
1059 ;; `texinfo-multiple-files-update'.
1060
1061 ;; Now, insert all the other menus
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001062
06cc857d
RS
Richard M. Stallman1997-06-15 17:39:13 +00001063 ;; The menu master-menu-list has a form like this:
1064 ;; ((("beta" "alpha") "title-A")
1065 ;; (("delta" "gamma") "title-B"))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001066
b9f60161 Stefan Monnier2001-10-28 03:24:14 +00001067 (dolist (menu master-menu-list)
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001068
b9f60161 Stefan Monnier2001-10-28 03:24:14 +00001069 (message "Inserting menu for %s .... " (cadr menu))
91c6f26e Karl Heuer1999-01-25 17:04:23 +00001070 ;; insert title of menu section
b9f60161 Stefan Monnier2001-10-28 03:24:14 +00001071 (insert "\n" (cadr menu) "\n\n")
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001072
91c6f26e Karl Heuer1999-01-25 17:04:23 +00001073 ;; insert each menu entry
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +00001074 (dolist (entry (reverse (car menu)))
1075 (insert "* " entry "\n")))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001076
06cc857d
RS
Richard M. Stallman1997-06-15 17:39:13 +00001077 ;; Finish menu
1078
1079 ;; @detailmenu (see note above)
1080 ;; Only insert @end detailmenu if a master menu was inserted.
1081 (if master-menu-inserted-p
91c6f26e Karl Heuer1999-01-25 17:04:23 +00001082 (insert "\n@end detailmenu"))
06cc857d Richard M. Stallman1997-06-15 17:39:13 +00001083 (insert "\n@end menu\n\n"))))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001084
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001085(defun texinfo-locate-menu-p ()
1086 "Find the next menu in the texinfo file.
1087If found, leave point after word `menu' on the `@menu' line, and return t.
1088If a menu is not found, do not move point and return nil."
1089 (re-search-forward "\\(^@menu\\)" nil t))
1090
b9f60161 Stefan Monnier2001-10-28 03:24:14 +00001091(defun texinfo-copy-menu-title ()
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001092 "Return the title of the section preceding the menu as a string.
1093If such a title cannot be found, return an empty string. Do not move
1094point."
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00001095 (let ((case-fold-search t))
1096 (save-excursion
1097 (if (re-search-backward
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001098 (concat
1099 "\\(^@top"
1100 "\\|" ; or
1101 texinfo-section-types-regexp ; all other section types
1102 "\\)")
1103 nil
1104 t)
1105 (progn
1106 (beginning-of-line)
1107 (forward-word 1) ; skip over section type
1108 (skip-chars-forward " \t") ; and over spaces
1109 (buffer-substring
1110 (point)
1111 (progn (end-of-line) (point))))
1112 ""))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001113
1114(defun texinfo-copy-menu ()
1115 "Return the entries of an existing menu as a list.
1116Start with point just after the word `menu' in the `@menu' line
1117and leave point on the line before the `@end menu' line."
1118 (let* (this-menu-list
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001119 (end-of-menu (texinfo-menu-end)) ; position of end of `@end menu'
1120 (last-entry (save-excursion ; position of beginning of
1121 ; last `* ' entry
1122 (goto-char end-of-menu)
1123 ;; handle multi-line description
1124 (if (not (re-search-backward "^\\* " nil t))
5b02e712 Eli Zaretskii2001-08-26 16:49:59 +00001125 (error "No entries in menu"))
91c6f26e Karl Heuer1999-01-25 17:04:23 +00001126 (point))))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001127 (while (< (point) last-entry)
c0fbcada Karl Heuer1996-06-09 13:42:55 +00001128 (if (re-search-forward "^\\* " end-of-menu t)
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +00001129 (push (buffer-substring
1130 (point)
1131 ;; copy multi-line descriptions
1132 (save-excursion
1133 (re-search-forward "\\(^\\* \\|^@e\\)" nil t)
1134 (- (point) 3)))
1135 this-menu-list)))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001136 this-menu-list))
1137
1138\f
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001139;;; Determining the hierarchical level in the texinfo file
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001140
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001141(defun texinfo-specific-section-type ()
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001142 "Return the specific type of next section, as a string.
1143For example, \"unnumberedsubsec\". Return \"top\" for top node.
1144
1145Searches forward for a section. Hence, point must be before the
1146section whose type will be found. Does not move point. Signal an
1147error if the node is not the top node and a section is not found."
8ab2588c
EZ
Eli Zaretskii2014-08-21 18:07:13 +03001148 (let* ((case-fold-search t)
1149 ;; The Texinfo manual has a second Top node inside @verbatim
1150 ;; near the end, which dupes us into thinking we are at top
1151 ;; level, no matter where we are when invoked. We don't
1152 ;; really grok @verbatim, so we cheat: only consider us to be
1153 ;; at top level if the position of the Top node we found is
1154 ;; before any other sectioning command.
1155 (top-pos (save-excursion
1156 (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)"
1157 ;; Following search limit causes a bug
1158 ;;(line-end-position)
1159 nil
1160 t)))
1161 (sec-pos (save-excursion
1162 (re-search-forward texinfo-section-types-regexp nil t)))
1163 sec-name)
1164 (if sec-pos
1165 (save-excursion
1166 (goto-char sec-pos)
1167 (setq sec-name (buffer-substring-no-properties
1168 (progn (beginning-of-line) ; copy its name
1169 (1+ (point)))
1170 (progn (forward-word 1)
1171 (point))))))
1172 (cond
1173 ((or sec-pos top-pos)
1174 (if (and top-pos sec-pos)
1175 (if (< top-pos sec-pos)
1176 "top"
1177 sec-name)
1178 (or sec-name "top")))
1179 (t
1180 (error
1181 "texinfo-specific-section-type: Chapter or section not found")))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001182
1183(defun texinfo-hierarchic-level ()
99d99081 Paul Eggert2011-12-18 22:21:24 -08001184 "Return the general hierarchical level of the next node in a texinfo file.
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001185Thus, a subheading or appendixsubsec is of type subsection."
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001186 (let ((case-fold-search t))
6f602bd0 Stefan Monnier2000-11-06 15:53:05 +00001187 (cadr (assoc
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +00001188 (texinfo-specific-section-type)
1189 texinfo-section-list))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001190
1191\f
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001192;;; Locating the major positions
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001193
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001194(defun texinfo-update-menu-region-beginning (level)
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001195 "Locate beginning of higher level section this section is within.
1196Return position of the beginning of the node line; do not move point.
1197Thus, if this level is subsection, searches backwards for section node.
1198Only argument is a string of the general type of section."
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00001199 (let ((case-fold-search t))
1200 ;; !! Known bug: if section immediately follows top node, this
1201 ;; returns the beginning of the buffer as the beginning of the
1202 ;; higher level section.
1203 (cond
6f602bd0 Stefan Monnier2000-11-06 15:53:05 +00001204 ((< level 3)
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001205 (save-excursion
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001206 (goto-char (point-min))
1207 (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t)
5ed619e0 Glenn Morris2010-11-06 13:23:42 -07001208 (line-beginning-position)))
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00001209 (t
1210 (save-excursion
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001211 (re-search-backward
1212 (concat
1213 "\\(^@node\\).*\n" ; match node line
1214 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
1215 "\\|" ; or
21324fae
GM
Gerd Moellmann2001-08-16 17:59:38 +00001216 "\\(^@ifinfo[ ]*\n\\)" ; ifinfo line, if any
1217 "\\|" ; or
1218 "\\(^@ifnottex[ ]*\n\\)" ; ifnottex line, if any
1219 "\\)?" ; end of expression
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001220 (eval
1221 (cdr (assoc level texinfo-update-menu-higher-regexps))))
1222 nil
1223 'goto-beginning)
1224 (point))))))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001225
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001226(defun texinfo-update-menu-region-end (level)
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001227 "Locate end of higher level section this section is within.
1228Return position; do not move point. Thus, if this level is a
1229subsection, find the node for the section this subsection is within.
1230If level is top or chapter, returns end of file. Only argument is a
1231string of the general type of section."
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00001232 (let ((case-fold-search t))
1233 (save-excursion
1234 (if (re-search-forward
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001235 (concat
1236 "\\(^@node\\).*\n" ; match node line
1237 "\\(\\(\\(^@c\\).*\n\\)" ; match comment line, if any
1238 "\\|" ; or
21324fae
GM
Gerd Moellmann2001-08-16 17:59:38 +00001239 "\\(^@ifinfo[ ]*\n\\)" ; ifinfo line, if any
1240 "\\|" ; or
1241 "\\(^@ifnottex[ ]*\n\\)" ; ifnottex line, if any
1242 "\\)?" ; end of expression
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001243 (eval
1244 ;; Never finds end of level above chapter so goes to end.
1245 (cdr (assoc level texinfo-update-menu-higher-regexps))))
1246 nil
1247 'goto-end)
1248 (match-beginning 1)
1249 (point-max)))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001250
1251(defun texinfo-menu-first-node (beginning end)
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001252 "Locate first node of the section the menu will be placed in.
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001253Return position; do not move point.
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001254The menu will be located just before this position.
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001255
1256First argument is the position of the beginning of the section in
1257which the menu will be located; second argument is the position of the
1258end of that region; it limits the search."
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001259 (save-excursion
1260 (goto-char beginning)
1261 (forward-line 1)
1262 (re-search-forward "^@node" end t)
5ed619e0 Glenn Morris2010-11-06 13:23:42 -07001263 (line-beginning-position)))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001264
1265\f
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001266;;; Updating a node
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001267
78897d3d Richard M. Stallman1997-04-05 21:59:09 +00001268(defun texinfo-update-node (&optional beginning end)
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001269 "Without any prefix argument, update the node in which point is located.
78897d3d Richard M. Stallman1997-04-05 21:59:09 +00001270Interactively, a prefix argument means to operate on the region.
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001271
e5d84bfe
EZ
Eli Zaretskii2011-12-10 12:49:33 +02001272Warning: do NOT use this function if your Texinfo file uses @node
1273lines without the `Next', `Previous', `Up' pointers, because the
1274result could be an invalid Texinfo file due to known deficiencies
1275in this command: it does not support @ignore and @if* directives.
1276
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001277The functions for creating or updating nodes and menus, and their
1278keybindings, are:
1279
78897d3d Richard M. Stallman1997-04-05 21:59:09 +00001280 texinfo-update-node (&optional beginning end) \\[texinfo-update-node]
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001281 texinfo-every-node-update () \\[texinfo-every-node-update]
1282 texinfo-sequential-node-update (&optional region-p)
1283
1284 texinfo-make-menu (&optional region-p) \\[texinfo-make-menu]
1285 texinfo-all-menus-update () \\[texinfo-all-menus-update]
1286 texinfo-master-menu ()
1287
1288 texinfo-indent-menu-description (column &optional region-p)
1289
1290The `texinfo-column-for-description' variable specifies the column to
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001291which menu descriptions are indented. Its default value is 32."
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001292
78897d3d
RS
Richard M. Stallman1997-04-05 21:59:09 +00001293 (interactive
1294 (if prefix-arg
1295 (list (point) (mark))))
1296 (if (null beginning)
1297 ;; Update a single node.
b9f60161 Stefan Monnier2001-10-28 03:24:14 +00001298 (let ((auto-fill-function nil))
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001299 (if (not (re-search-backward "^@node" (point-min) t))
1300 (error "Node line not found before this position"))
1301 (texinfo-update-the-node)
1302 (message "Done...updated the node. You may save the buffer."))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001303 ;; else
b9f60161 Stefan Monnier2001-10-28 03:24:14 +00001304 (let ((auto-fill-function nil))
78897d3d
RS
Richard M. Stallman1997-04-05 21:59:09 +00001305 (save-excursion
1306 (save-restriction
1307 (narrow-to-region beginning end)
1308 (goto-char (point-min))
1309 (while (re-search-forward "^@node" (point-max) t)
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001310 (beginning-of-line)
78897d3d
RS
Richard M. Stallman1997-04-05 21:59:09 +00001311 (texinfo-update-the-node))
1312 (goto-char (point-max))
1313 (message "Done...nodes updated in region. You may save the buffer."))))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001314
1315(defun texinfo-every-node-update ()
e5d84bfe
EZ
Eli Zaretskii2011-12-10 12:49:33 +02001316 "Update every node in a Texinfo file.
1317
1318Warning: do NOT use this function if your Texinfo file uses @node
1319lines without the `Next', `Previous', `Up' pointers, because the
1320result could be an invalid Texinfo file due to known deficiencies
1321in this command: it does not support @ignore and @if* directives."
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001322 (interactive)
1323 (save-excursion
78897d3d Richard M. Stallman1997-04-05 21:59:09 +00001324 (texinfo-update-node (point-min) (point-max))
8039284a Gerd Moellmann2001-08-20 10:36:41 +00001325 (message "Done...updated every node. You may save the buffer.")))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001326
1327(defun texinfo-update-the-node ()
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001328 "Update one node. Point must be at the beginning of node line.
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001329Leave point at the end of the node line."
1330 (texinfo-check-for-node-name)
1331 (texinfo-delete-existing-pointers)
1332 (message "Updating node: %s ... " (texinfo-copy-node-name))
1333 (save-restriction
1334 (widen)
1335 (let*
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001336 ((case-fold-search t)
1337 (level (texinfo-hierarchic-level))
1338 (beginning (texinfo-update-menu-region-beginning level))
1339 (end (texinfo-update-menu-region-end level)))
6f602bd0 Stefan Monnier2000-11-06 15:53:05 +00001340 (if (eq level 1)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001341 (texinfo-top-pointer-case)
1342 ;; else
1343 (texinfo-insert-pointer beginning end level 'next)
1344 (texinfo-insert-pointer beginning end level 'previous)
1345 (texinfo-insert-pointer beginning end level 'up)
1346 (texinfo-clean-up-node-line)))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001347
1348(defun texinfo-top-pointer-case ()
1349 "Insert pointers in the Top node. This is a special case.
1350
1351The `Next' pointer is a pointer to a chapter or section at a lower
1352hierarchical level in the file. The `Previous' and `Up' pointers are
1353to `(dir)'. Point must be at the beginning of the node line, and is
1354left at the end of the node line."
1355
1356 (texinfo-clean-up-node-line)
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001357 (insert ", "
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001358 (save-excursion
1359 ;; There may be an @chapter or other such command between
1360 ;; the top node line and the next node line, as a title
1361 ;; for an `ifinfo' section. This @chapter command must
1362 ;; must be skipped. So the procedure is to search for
1363 ;; the next `@node' line, and then copy its name.
1364 (if (re-search-forward "^@node" nil t)
1365 (progn
1366 (beginning-of-line)
1367 (texinfo-copy-node-name))
1368 " "))
1369 ", (dir), (dir)"))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001370
1371(defun texinfo-check-for-node-name ()
1372 "Determine whether the node has a node name. Prompt for one if not.
1373Point must be at beginning of node line. Does not move point."
1374 (save-excursion
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00001375 (let ((initial (texinfo-copy-next-section-title)))
1376 ;; This is not clean. Use `interactive' to read the arg.
1377 (forward-word 1) ; skip over node command
1378 (skip-chars-forward " \t") ; and over spaces
1379 (if (not (looking-at "[^,\t\n ]+")) ; regexp based on what Info looks for
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001380 ; alternatively, use "[a-zA-Z]+"
1381 (let ((node-name
1382 (read-from-minibuffer
1383 "Node name (use no @, commas, colons, or apostrophes): "
1384 initial)))
1385 (insert " " node-name))))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001386
1387(defun texinfo-delete-existing-pointers ()
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001388 "Delete `Next', `Previous', and `Up' pointers.
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001389Starts from the current position of the cursor, and searches forward
1390on the line for a comma and if one is found, deletes the rest of the
1391line, including the comma. Leaves point at beginning of line."
5ed619e0 Glenn Morris2010-11-06 13:23:42 -07001392 (let ((eol-point (line-end-position)))
2db10f13 Karl Heuer1995-05-26 04:00:52 +00001393 (if (search-forward "," eol-point t)
91c6f26e Karl Heuer1999-01-25 17:04:23 +00001394 (delete-region (1- (point)) eol-point)))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001395 (beginning-of-line))
1396
1397(defun texinfo-find-pointer (beginning end level direction)
1398 "Move point to section associated with next, previous, or up pointer.
06cc857d Richard M. Stallman1997-06-15 17:39:13 +00001399Return type of pointer (either `normal' or `no-pointer').
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001400
1401The first and second arguments bound the search for a pointer to the
1402beginning and end, respectively, of the enclosing higher level
1403section. The third argument is a string specifying the general kind
d460a763 Karl Heuer1996-06-09 13:40:05 +00001404of section such as \"chapter\" or \"section\". When looking for the
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001405`Next' pointer, the section found will be at the same hierarchical
1406level in the Texinfo file; when looking for the `Previous' pointer,
1407the section found will be at the same or higher hierarchical level in
1408the Texinfo file; when looking for the `Up' pointer, the section found
1409will be at some level higher in the Texinfo file. The fourth argument
1410\(one of 'next, 'previous, or 'up\) specifies whether to find the
1411`Next', `Previous', or `Up' pointer."
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00001412 (let ((case-fold-search t))
1413 (cond ((eq direction 'next)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001414 (forward-line 3) ; skip over current node
1415 ;; Search for section commands accompanied by node lines;
1416 ;; ignore section commands in the middle of nodes.
1417 (if (re-search-forward
21324fae Gerd Moellmann2001-08-16 17:59:38 +00001418 ;; A `Top' node is never a next pointer, so won't find it.
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001419 (concat
1420 ;; Match node line.
1421 "\\(^@node\\).*\n"
21324fae
GM
Gerd Moellmann2001-08-16 17:59:38 +00001422 ;; Match comment, ifinfo, ifnottex line, if any
1423 (concat
1424 "\\(\\("
1425 "\\(^@c\\).*\n\\)"
1426 "\\|"
1427 "\\(^@ifinfo[ ]*\n\\)"
1428 "\\|"
1429 "\\(^@ifnottex[ ]*\n\\)"
1430 "\\)?")
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001431 (eval
1432 (cdr (assoc level texinfo-update-menu-same-level-regexps))))
1433 end
1434 t)
1435 'normal
1436 'no-pointer))
1437 ((eq direction 'previous)
1438 (if (re-search-backward
1439 (concat
1440 "\\("
1441 ;; Match node line.
1442 "\\(^@node\\).*\n"
21324fae
GM
Gerd Moellmann2001-08-16 17:59:38 +00001443 ;; Match comment, ifinfo, ifnottex line, if any
1444 (concat
1445 "\\(\\("
1446 "\\(^@c\\).*\n\\)"
1447 "\\|"
1448 "\\(^@ifinfo[ ]*\n\\)"
1449 "\\|"
1450 "\\(^@ifnottex[ ]*\n\\)"
1451 "\\)?")
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001452 (eval
1453 (cdr (assoc level texinfo-update-menu-same-level-regexps)))
1454 "\\|"
1455 ;; Match node line.
1456 "\\(^@node\\).*\n"
21324fae
GM
Gerd Moellmann2001-08-16 17:59:38 +00001457 ;; Match comment, ifinfo, ifnottex line, if any
1458 (concat
1459 "\\(\\("
1460 "\\(^@c\\).*\n\\)"
1461 "\\|"
1462 "\\(^@ifinfo[ ]*\n\\)"
1463 "\\|"
1464 "\\(^@ifnottex[ ]*\n\\)"
1465 "\\)?")
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001466 (eval
1467 (cdr (assoc level texinfo-update-menu-higher-regexps)))
1468 "\\|"
1469 ;; Handle `Top' node specially.
1470 "^@node [ \t]*top[ \t]*\\(,\\|$\\)"
1471 "\\)")
1472 beginning
1473 t)
1474 'normal
1475 'no-pointer))
1476 ((eq direction 'up)
1477 (if (re-search-backward
1478 (concat
1479 "\\("
1480 ;; Match node line.
1481 "\\(^@node\\).*\n"
21324fae
GM
Gerd Moellmann2001-08-16 17:59:38 +00001482 ;; Match comment, ifinfo, ifnottex line, if any
1483 (concat
1484 "\\(\\("
1485 "\\(^@c\\).*\n\\)"
1486 "\\|"
1487 "\\(^@ifinfo[ ]*\n\\)"
1488 "\\|"
1489 "\\(^@ifnottex[ ]*\n\\)"
1490 "\\)?")
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001491 (eval (cdr (assoc level texinfo-update-menu-higher-regexps)))
1492 "\\|"
1493 ;; Handle `Top' node specially.
1494 "^@node [ \t]*top[ \t]*\\(,\\|$\\)"
1495 "\\)")
1496 (save-excursion
1497 (goto-char beginning)
5ed619e0 Glenn Morris2010-11-06 13:23:42 -07001498 (line-beginning-position))
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001499 t)
1500 'normal
1501 'no-pointer))
1502 (t
1503 (error "texinfo-find-pointer: lack proper arguments")))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001504
1505(defun texinfo-pointer-name (kind)
1506 "Return the node name preceding the section command.
06cc857d Richard M. Stallman1997-06-15 17:39:13 +00001507The argument is the kind of section, either `normal' or `no-pointer'."
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001508 (let (name)
1509 (cond ((eq kind 'normal)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001510 (end-of-line) ; this handles prev node top case
1511 (re-search-backward ; when point is already
1512 "^@node" ; at the beginning of @node line
c026460b Glenn Morris2010-11-09 23:59:00 -08001513 (line-beginning-position -2)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001514 t)
1515 (setq name (texinfo-copy-node-name)))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001516 ((eq kind 'no-pointer)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001517 ;; Don't need to put a blank in the pointer slot,
1518 ;; since insert "' " always has a space
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001519 (setq name " "))) ; put a blank in the pointer slot
1520 name))
1521
1522(defun texinfo-insert-pointer (beginning end level direction)
1523 "Insert the `Next', `Previous' or `Up' node name at point.
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001524Move point forward.
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001525
1526The first and second arguments bound the search for a pointer to the
1527beginning and end, respectively, of the enclosing higher level
1528section. The third argument is the hierarchical level of the Texinfo
1529file, a string such as \"section\". The fourth argument is direction
06cc857d Richard M. Stallman1997-06-15 17:39:13 +00001530towards which the pointer is directed, one of `next', `previous', or `up'."
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001531
1532 (end-of-line)
1533 (insert
1534 ", "
1535 (save-excursion
1536 (texinfo-pointer-name
1537 (texinfo-find-pointer beginning end level direction)))))
1538
1539(defun texinfo-clean-up-node-line ()
1540 "Remove extra commas, if any, at end of node line."
1541 (end-of-line)
1542 (skip-chars-backward ", ")
5ed619e0 Glenn Morris2010-11-06 13:23:42 -07001543 (delete-region (point) (line-end-position)))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001544
1545\f
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001546;;; Updating nodes sequentially
06cc857d
RS
Richard M. Stallman1997-06-15 17:39:13 +00001547;; These sequential update functions insert `Next' or `Previous'
1548;; pointers that point to the following or preceding nodes even if they
1549;; are at higher or lower hierarchical levels. This means that if a
1550;; section contains one or more subsections, the section's `Next'
1551;; pointer will point to the subsection and not the following section.
1552;; (The subsection to which `Next' points will most likely be the first
1553;; item on the section's menu.)
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001554
1555(defun texinfo-sequential-node-update (&optional region-p)
1556 "Update one node (or many) in a Texinfo file with sequential pointers.
1557
1558This function causes the `Next' or `Previous' pointer to point to the
1559immediately preceding or following node, even if it is at a higher or
1560lower hierarchical level in the document. Continually pressing `n' or
1561`p' takes you straight through the file.
1562
1563Without any prefix argument, update the node in which point is located.
1564Non-nil argument (prefix, if interactive) means update the nodes in the
1565marked region.
1566
1567This command makes it awkward to navigate among sections and
1568subsections; it should be used only for those documents that are meant
1569to be read like a novel rather than a reference, and for which the
1570Info `g*' command is inadequate."
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001571
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001572 (interactive "P")
1573 (if (not region-p)
fe3371ef Richard M. Stallman1993-07-07 22:21:19 +00001574 ;; update a single node
b9f60161 Stefan Monnier2001-10-28 03:24:14 +00001575 (let ((auto-fill-function nil))
91c6f26e Karl Heuer1999-01-25 17:04:23 +00001576 (if (not (re-search-backward "^@node" (point-min) t))
5b02e712 Eli Zaretskii2001-08-26 16:49:59 +00001577 (error "Node line not found before this position"))
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001578 (texinfo-sequentially-update-the-node)
1579 (message
1580 "Done...sequentially updated the node . You may save the buffer."))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001581 ;; else
e5d77022 Jim Blandy1992-06-30 13:54:21 +00001582 (let ((auto-fill-function nil)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001583 (beginning (region-beginning))
1584 (end (region-end)))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001585 (if (= end beginning)
5b02e712 Eli Zaretskii2001-08-26 16:49:59 +00001586 (error "Please mark a region"))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001587 (save-restriction
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001588 (narrow-to-region beginning end)
1589 (goto-char beginning)
1590 (push-mark (point) t)
1591 (while (re-search-forward "^@node" (point-max) t)
1592 (beginning-of-line)
1593 (texinfo-sequentially-update-the-node))
1594 (message
1595 "Done...updated the nodes in sequence. You may save the buffer.")))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001596
1597(defun texinfo-sequentially-update-the-node ()
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001598 "Update one node such that the pointers are sequential.
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001599A `Next' or `Previous' pointer points to any preceding or following node,
1600regardless of its hierarchical level."
1601
6f602bd0
SM
Stefan Monnier2000-11-06 15:53:05 +00001602 (texinfo-check-for-node-name)
1603 (texinfo-delete-existing-pointers)
1604 (message
1605 "Sequentially updating node: %s ... " (texinfo-copy-node-name))
1606 (save-restriction
1607 (widen)
1608 (let* ((case-fold-search t)
1609 (level (texinfo-hierarchic-level)))
1610 (if (eq level 1)
1611 (texinfo-top-pointer-case)
1612 ;; else
1613 (texinfo-sequentially-insert-pointer level 'next)
1614 (texinfo-sequentially-insert-pointer level 'previous)
1615 (texinfo-sequentially-insert-pointer level 'up)
1616 (texinfo-clean-up-node-line)))))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001617
a056c2c7
RC
Robert J. Chassell2002-03-03 17:30:07 +00001618(defun texinfo-sequentially-insert-pointer (level direction)
1619 "Insert the `Next', `Previous' or `Up' node name at point.
1620Move point forward.
1621
1622The first argument is the hierarchical level of the Texinfo file, a
1623string such as \"section\". The second argument is direction, one of
1624`next', `previous', or `up'."
1625
1626 (end-of-line)
1627 (insert
1628 ", "
1629 (save-excursion
1630 (texinfo-pointer-name
1631 (texinfo-sequentially-find-pointer level direction)))))
1632
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001633(defun texinfo-sequentially-find-pointer (level direction)
1634 "Find next or previous pointer sequentially in Texinfo file, or up pointer.
1635Move point to section associated with the pointer. Find point even if
1636it is in a different section.
1637
06cc857d Richard M. Stallman1997-06-15 17:39:13 +00001638Return type of pointer (either `normal' or `no-pointer').
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001639
1640The first argument is a string specifying the general kind of section
d460a763 Karl Heuer1996-06-09 13:40:05 +00001641such as \"chapter\" or \"section\". The section found will be at the
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001642same hierarchical level in the Texinfo file, or, in the case of the up
06cc857d
RS
Richard M. Stallman1997-06-15 17:39:13 +00001643pointer, some level higher. The second argument (one of `next',
1644`previous', or `up') specifies whether to find the `Next', `Previous',
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001645or `Up' pointer."
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001646 (let ((case-fold-search t))
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001647 (cond ((eq direction 'next)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001648 (forward-line 3) ; skip over current node
1649 (if (re-search-forward
1650 texinfo-section-types-regexp
1651 (point-max)
1652 t)
1653 'normal
1654 'no-pointer))
1655 ((eq direction 'previous)
1656 (if (re-search-backward
1657 texinfo-section-types-regexp
1658 (point-min)
1659 t)
1660 'normal
1661 'no-pointer))
1662 ((eq direction 'up)
1663 (if (re-search-backward
1664 (eval (cdr (assoc level texinfo-update-menu-higher-regexps)))
a056c2c7 Robert J. Chassell2002-03-03 17:30:07 +00001665 (point-min)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001666 t)
1667 'normal
1668 'no-pointer))
1669 (t
1670 (error "texinfo-sequential-find-pointer: lack proper arguments")))))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001671
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001672\f
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001673;;; Inserting `@node' lines
06cc857d
RS
Richard M. Stallman1997-06-15 17:39:13 +00001674;; The `texinfo-insert-node-lines' function inserts `@node' lines as needed
1675;; before the `@chapter', `@section', and such like lines of a region
1676;; in a Texinfo file.
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001677
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001678(defun texinfo-insert-node-lines (beginning end &optional title-p)
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001679 "Insert missing `@node' lines in region of Texinfo file.
1680Non-nil argument (prefix, if interactive) means also to insert the
1681section titles as node names; and also to insert the section titles as
06cc857d Richard M. Stallman1997-06-15 17:39:13 +00001682node names in pre-existing `@node' lines that lack names."
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00001683 (interactive "r\nP")
1684
1685 ;; Use marker; after inserting node lines, leave point at end of
1686 ;; region and mark at beginning.
1687
1688 (let (beginning-marker end-marker title last-section-position)
1689
1690 ;; Save current position on mark ring and set mark to end.
d1f9f441
RS
Richard M. Stallman1997-08-04 01:11:01 +00001691 (push-mark end t)
1692 (setq end-marker (mark-marker))
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00001693
1694 (goto-char beginning)
1695 (while (re-search-forward
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001696 texinfo-section-types-regexp
1697 end-marker
1698 'end)
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00001699 ;; Copy title if desired.
1700 (if title-p
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001701 (progn
1702 (beginning-of-line)
1703 (forward-word 1)
1704 (skip-chars-forward " \t")
1705 (setq title (buffer-substring
1706 (point)
5ed619e0 Glenn Morris2010-11-06 13:23:42 -07001707 (line-end-position)))))
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00001708 ;; Insert node line if necessary.
1709 (if (re-search-backward
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001710 "^@node"
1711 ;; Avoid finding previous node line if node lines are close.
1712 (or last-section-position
dea53a43
GM
Glenn Morris2010-11-09 20:35:18 -08001713 (line-beginning-position -1))
1714 t)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001715 ;; @node is present, and point at beginning of that line
1716 (forward-word 1) ; Leave point just after @node.
1717 ;; Else @node missing; insert one.
1718 (beginning-of-line) ; Beginning of `@section' line.
1719 (insert "@node\n")
1720 (backward-char 1)) ; Leave point just after `@node'.
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00001721 ;; Insert title if desired.
1722 (if title-p
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001723 (progn
1724 (skip-chars-forward " \t")
1725 ;; Use regexp based on what info looks for
1726 ;; (alternatively, use "[a-zA-Z]+");
1727 ;; this means we only insert a title if none exists.
1728 (if (not (looking-at "[^,\t\n ]+"))
1729 (progn
1730 (beginning-of-line)
1731 (forward-word 1)
1732 (insert " " title)
1733 (message "Inserted title %s ... " title)))))
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001734 ;; Go forward beyond current section title.
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001735 (re-search-forward texinfo-section-types-regexp
dea53a43 Glenn Morris2010-11-09 20:35:18 -08001736 (line-beginning-position 4) t)
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00001737 (setq last-section-position (point))
1738 (forward-line 1))
1739
1740 ;; Leave point at end of region, mark at beginning.
1741 (set-mark beginning)
1742
1743 (if title-p
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001744 (message
1745 "Done inserting node lines and titles. You may save the buffer.")
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001746 (message "Done inserting node lines. You may save the buffer."))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001747
1748\f
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001749;;; Update and create menus for multi-file Texinfo sources
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001750
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001751;; 1. M-x texinfo-multiple-files-update
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001752;;
1753;; Read the include file list of an outer Texinfo file and
1754;; update all highest level nodes in the files listed and insert a
1755;; main menu in the outer file after its top node.
1756
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001757;; 2. C-u M-x texinfo-multiple-files-update
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001758;;
1759;; Same as 1, but insert a master menu. (Saves reupdating lower
1760;; level menus and nodes.) This command simply reads every menu,
1761;; so if the menus are wrong, the master menu will be wrong.
1762;; Similarly, if the lower level node pointers are wrong, they
1763;; will stay wrong.
1764
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001765;; 3. C-u 2 M-x texinfo-multiple-files-update
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001766;;
1767;; Read the include file list of an outer Texinfo file and
1768;; update all nodes and menus in the files listed and insert a
1769;; master menu in the outer file after its top node.
1770
1771;;; Note: these functions:
1772;;;
1773;;; * Do not save or delete any buffers. You may fill up your memory.
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001774;;; * Do not handle any pre-existing nodes in outer file.
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001775;;; Hence, you may need a file for indices.
1776
1777\f
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001778;;; Auxiliary functions for multiple file updating
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001779
1780(defun texinfo-multi-file-included-list (outer-file)
1781 "Return a list of the included files in OUTER-FILE."
1782 (let ((included-file-list (list outer-file))
91c6f26e Karl Heuer1999-01-25 17:04:23 +00001783 start)
9a529312 Stefan Monnier2009-10-31 02:38:34 +00001784 (with-current-buffer (find-file-noselect outer-file)
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001785 (widen)
1786 (goto-char (point-min))
1787 (while (re-search-forward "^@include" nil t)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001788 (skip-chars-forward " \t")
1789 (setq start (point))
1790 (end-of-line)
1791 (skip-chars-backward " \t")
1792 (setq included-file-list
1793 (cons (buffer-substring start (point))
1794 included-file-list)))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001795 (nreverse included-file-list))))
1796
1797(defun texinfo-copy-next-section-title ()
1798 "Return the name of the immediately following section as a string.
1799
1800Start with point at the beginning of the node line. Leave point at the
1801same place. If there is no title, returns an empty string."
1802
1803 (save-excursion
1804 (end-of-line)
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001805 (let ((node-end (or
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001806 (save-excursion
1807 (if (re-search-forward "\\(^@node\\)" nil t)
1808 (match-beginning 0)))
1809 (point-max))))
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001810 (if (re-search-forward texinfo-section-types-regexp node-end t)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001811 (progn
1812 (beginning-of-line)
1813 ;; copy title
1814 (let ((title
1815 (buffer-substring
1816 (progn (forward-word 1) ; skip over section type
1817 (skip-chars-forward " \t") ; and over spaces
1818 (point))
1819 (progn (end-of-line) (point)))))
1820 title))
1821 ""))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001822
1823(defun texinfo-multi-file-update (files &optional update-everything)
1824 "Update first node pointers in each file in FILES.
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001825Return a list of the node names.
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001826
1827The first file in the list is an outer file; the remaining are
1828files included in the outer file with `@include' commands.
1829
1830If optional arg UPDATE-EVERYTHING non-nil, update every menu and
1831pointer in each of the included files.
1832
1833Also update the `Top' level node pointers of the outer file.
1834
1835Requirements:
1836
1837 * the first file in the FILES list must be the outer file,
1838 * each of the included files must contain exactly one highest
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001839 hierarchical level node,
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001840 * this node must be the first node in the included file,
1841 * each highest hierarchical level node must be of the same type.
1842
12b795e1
RC
Robert J. Chassell2004-01-10 15:18:29 +00001843Thus, normally, each included file contains one, and only one, chapter.
1844
1845However, when an included file does not have any node lines in
1846it, this command does not try to create a menu entry for it.
1847Consequently, you can include any file, such as a version or an
1848update file without node lines, not just files that are
1849chapters."
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001850
06cc857d Richard M. Stallman1997-06-15 17:39:13 +00001851;; The menu-list has the form:
d1f9f441
RS
Richard M. Stallman1997-08-04 01:11:01 +00001852;;
1853;; \(\(\"node-name1\" . \"title1\"\)
06cc857d Richard M. Stallman1997-06-15 17:39:13 +00001854;; \(\"node-name2\" . \"title2\"\) ... \)
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001855;;
06cc857d
RS
Richard M. Stallman1997-06-15 17:39:13 +00001856;; However, there does not need to be a title field and this function
1857;; does not fill it; however a comment tells you how to do so.
1858;; You would use the title field if you wanted to insert titles in the
1859;; description slot of a menu as a description.
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001860
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001861 (let ((case-fold-search t)
d7fc69d1 Robert J. Chassell2003-12-25 20:20:50 +00001862 menu-list next-node-name previous-node-name files-with-node-lines)
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001863
d7fc69d1
RC
Robert J. Chassell2003-12-25 20:20:50 +00001864 ;; Create a new list of included files that only have node lines
1865 (while files
1866 (set-buffer (find-file-noselect (car files)))
1867 (widen)
1868 (goto-char (point-min))
1869 (when (re-search-forward "^@node" nil t)
1870 (setq files-with-node-lines (cons (car files) files-with-node-lines)))
1871 (setq files (cdr files)))
1872 (setq files-with-node-lines (nreverse files-with-node-lines))
1873
1874 ;; Find the name of the first node in a subsequent file
1875 ;; and copy it into the variable next-node-name
1876 (set-buffer (find-file-noselect (car (cdr files-with-node-lines))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001877 (widen)
1878 (goto-char (point-min))
f6472377
EZ
Eli Zaretskii2007-02-03 14:02:45 +00001879 ;; The following search _must_ succeed, since we verified above
1880 ;; that this file does have a @node line.
1881 (re-search-forward "^@node" nil t)
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001882 (beginning-of-line)
1883 (texinfo-check-for-node-name)
1884 (setq next-node-name (texinfo-copy-node-name))
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +00001885 (push (cons next-node-name (prog1 "" (forward-line 1)))
1886 ;; Use following to insert section titles automatically.
1887 ;; (texinfo-copy-next-section-title)
1888 menu-list)
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001889
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001890 ;; Go to outer file
d7fc69d1
RC
Robert J. Chassell2003-12-25 20:20:50 +00001891 ;; `pop' is analogous to (prog1 (car PLACE) (setf PLACE (cdr PLACE)))
1892 (set-buffer (find-file-noselect (pop files-with-node-lines)))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001893 (goto-char (point-min))
82ac4b77 Jim Blandy1990-12-20 20:38:22 +00001894 (if (not (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t))
5b02e712 Eli Zaretskii2001-08-26 16:49:59 +00001895 (error "This buffer needs a Top node"))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001896 (beginning-of-line)
1897 (texinfo-delete-existing-pointers)
1898 (end-of-line)
1899 (insert ", " next-node-name ", (dir), (dir)")
1900 (beginning-of-line)
1901 (setq previous-node-name "Top")
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001902
d7fc69d1 Robert J. Chassell2003-12-25 20:20:50 +00001903 (while files-with-node-lines
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001904
d7fc69d1 Robert J. Chassell2003-12-25 20:20:50 +00001905 (if (not (cdr files-with-node-lines))
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001906 ;; No next file
1907 (setq next-node-name "")
1908 ;; Else,
1909 ;; find the name of the first node in the next file.
d7fc69d1 Robert J. Chassell2003-12-25 20:20:50 +00001910 (set-buffer (find-file-noselect (car (cdr files-with-node-lines))))
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001911 (widen)
1912 (goto-char (point-min))
f6472377
EZ
Eli Zaretskii2007-02-03 14:02:45 +00001913 ;; The following search _must_ succeed, since we verified
1914 ;; above that files in files-with-node-lines do have a @node
1915 ;; line.
1916 (re-search-forward "^@node" nil t)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001917 (beginning-of-line)
1918 (texinfo-check-for-node-name)
1919 (setq next-node-name (texinfo-copy-node-name))
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +00001920 (push (cons next-node-name (prog1 "" (forward-line 1)))
1921 ;; Use following to insert section titles automatically.
1922 ;; (texinfo-copy-next-section-title)
1923 menu-list))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001924
1925 ;; Go to node to be updated.
d7fc69d1 Robert J. Chassell2003-12-25 20:20:50 +00001926 (set-buffer (find-file-noselect (car files-with-node-lines)))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001927 (goto-char (point-min))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001928 (beginning-of-line)
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001929
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00001930 ;; Update other menus and nodes if requested.
1931 (if update-everything (texinfo-all-menus-update t))
1932
1933 (beginning-of-line)
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001934 (texinfo-delete-existing-pointers)
1935 (end-of-line)
b9f60161 Stefan Monnier2001-10-28 03:24:14 +00001936 (insert ", " next-node-name ", " previous-node-name ", Top")
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001937
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001938 (beginning-of-line)
1939 (setq previous-node-name (texinfo-copy-node-name))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001940
d7fc69d1 Robert J. Chassell2003-12-25 20:20:50 +00001941 (setq files-with-node-lines (cdr files-with-node-lines)))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001942 (nreverse menu-list)))
1943
1944(defun texinfo-multi-files-insert-main-menu (menu-list)
1945 "Insert formatted main menu at point.
1946Indents the first line of the description, if any, to the value of
06cc857d Richard M. Stallman1997-06-15 17:39:13 +00001947`texinfo-column-for-description'."
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001948
1949 (insert "@menu\n")
b9f60161 Stefan Monnier2001-10-28 03:24:14 +00001950 (dolist (entry menu-list)
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00001951 ;; Every menu entry starts with a star and a space.
1952 (insert "* ")
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001953
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001954 ;; Insert the node name (and menu entry name, if present).
b9f60161 Stefan Monnier2001-10-28 03:24:14 +00001955 (let ((node-part (car entry)))
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001956 (if (stringp node-part)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001957 ;; "Double colon" entry line; menu entry and node name are the same,
1958 (insert (format "%s::" node-part))
1959 ;; "Single colon" entry line; menu entry and node name are different.
1960 (insert (format "%s: %s." (car node-part) (cdr node-part)))))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00001961
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001962 ;; Insert the description, if present.
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +00001963 (when (cdr entry)
1964 ;; Move to right place.
1965 (indent-to texinfo-column-for-description 2)
1966 ;; Insert description.
1967 (insert (format "%s" (cdr entry))))
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001968
b9f60161 Stefan Monnier2001-10-28 03:24:14 +00001969 (insert "\n")) ; end this menu entry
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001970 (insert "@end menu"))
1971
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001972(defun texinfo-multi-file-master-menu-list (files-list)
1973 "Return master menu list from files in FILES-LIST.
1974Menu entries in each file collected using `texinfo-master-menu-list'.
1975
1976The first file in FILES-LIST must be the outer file; the others must
1977be the files included within it. A main menu must already exist."
1978 (save-excursion
1979 (let (master-menu-list)
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +00001980 (dolist (file files-list)
1981 (set-buffer (find-file-noselect file))
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00001982 (message "Working on: %s " (current-buffer))
1983 (goto-char (point-min))
1984 (setq master-menu-list
b9f60161 Stefan Monnier2001-10-28 03:24:14 +00001985 (append master-menu-list (texinfo-master-menu-list))))
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001986 master-menu-list)))
1987
1988\f
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00001989;;; The multiple-file update function
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001990
1991(defun texinfo-multiple-files-update
f6472377 Eli Zaretskii2007-02-03 14:02:45 +00001992 (outer-file &optional make-master-menu update-everything)
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00001993 "Update first node pointers in each file included in OUTER-FILE;
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00001994create or update the `Top' level node pointers and the main menu in
1995the outer file that refers to such nodes. This does not create or
1996update menus or pointers within the included files.
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00001997
1998With optional MAKE-MASTER-MENU argument (prefix arg, if interactive),
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00001999insert a master menu in OUTER-FILE in addition to creating or updating
2000pointers in the first @node line in each included file and creating or
2001updating the `Top' level node pointers of the outer file. This does
2002not create or update other menus and pointers within the included
2003files.
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00002004
2005With optional UPDATE-EVERYTHING argument (numeric prefix arg, if
2006interactive), update all the menus and all the `Next', `Previous', and
2007`Up' pointers of all the files included in OUTER-FILE before inserting
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00002008a master menu in OUTER-FILE. Also, update the `Top' level node
e5d84bfe
EZ
Eli Zaretskii2011-12-10 12:49:33 +02002009pointers of OUTER-FILE. Do NOT invoke this command with a numeric prefix
2010arg, if your files use @node lines without the `Next', `Previous', `Up'
2011pointers, because this could produce invalid Texinfo files due to known
2012deficiencies in `texinfo-update-node': it does not support the @ignore
2013and @if... directives.
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00002014
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00002015Notes:
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00002016
2017 * this command does NOT save any files--you must save the
2018 outer file and any modified, included files.
2019
2020 * except for the `Top' node, this command does NOT handle any
2021 pre-existing nodes in the outer file; hence, indices must be
2022 enclosed in an included file.
2023
2024Requirements:
2025
2026 * each of the included files must contain exactly one highest
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00002027 hierarchical level node,
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00002028 * this highest node must be the first node in the included file,
2029 * each highest hierarchical level node must be of the same type.
2030
2031Thus, normally, each included file contains one, and only one,
2032chapter."
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00002033
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00002034 (interactive (cons
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00002035 (read-string
2036 "Name of outer `include' file: "
2037 (buffer-file-name))
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +00002038 (cond
2039 ((not current-prefix-arg) '(nil nil))
2040 ((listp current-prefix-arg) '(t nil)) ; make-master-menu
2041 ((numberp current-prefix-arg) '(t t))))) ; update-everything
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00002042
2043 (let* ((included-file-list (texinfo-multi-file-included-list outer-file))
91c6f26e Karl Heuer1999-01-25 17:04:23 +00002044 (files included-file-list)
91c6f26e
KH
Karl Heuer1999-01-25 17:04:23 +00002045 next-node-name
2046 previous-node-name
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +00002047 ;; Update the pointers and collect the names of the nodes and titles
2048 (main-menu-list (texinfo-multi-file-update files update-everything)))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00002049
b9f60161 Stefan Monnier2001-10-28 03:24:14 +00002050 ;; Insert main menu
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00002051
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +00002052 ;; Go to outer file
2053 (set-buffer (find-file-noselect (car included-file-list)))
2054 (if (texinfo-old-menu-p
2055 (point-min)
2056 (save-excursion
2057 (re-search-forward "^@include")
5ed619e0 Glenn Morris2010-11-06 13:23:42 -07002058 (line-beginning-position)))
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +00002059 ;; If found, leave point after word `menu' on the `@menu' line.
2060 (progn
2061 (texinfo-incorporate-descriptions main-menu-list)
2062 ;; Delete existing menu.
2063 (beginning-of-line)
2064 (delete-region
2065 (point)
2066 (save-excursion (re-search-forward "^@end menu") (point)))
2067 ;; Insert main menu
2068 (texinfo-multi-files-insert-main-menu main-menu-list))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00002069
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +00002070 ;; Else no current menu; insert it before `@include'
2071 (texinfo-multi-files-insert-main-menu main-menu-list))
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00002072
b9f60161 Stefan Monnier2001-10-28 03:24:14 +00002073 ;; Insert master menu
7e1335cf Robert J. Chassell1990-10-20 02:38:04 +00002074
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +00002075 (if make-master-menu
2076 (progn
2077 ;; First, removing detailed part of any pre-existing master menu
2078 (goto-char (point-min))
2079 (if (search-forward texinfo-master-menu-header nil t)
2080 (progn
2081 (goto-char (match-beginning 0))
2082 ;; Check if @detailmenu kludge is used;
2083 ;; if so, leave point before @detailmenu.
dea53a43 Glenn Morris2010-11-09 20:35:18 -08002084 (search-backward "\n@detailmenu" (line-beginning-position -2) t)
b9f60161
SM
Stefan Monnier2001-10-28 03:24:14 +00002085 ;; Remove detailed master menu listing
2086 (let ((end-of-detailed-menu-descriptions
2087 (save-excursion ; beginning of end menu line
2088 (goto-char (texinfo-menu-end))
2089 (beginning-of-line) (forward-char -1)
2090 (point))))
2091 (delete-region (point) end-of-detailed-menu-descriptions))))
2092
2093 ;; Create a master menu and insert it
2094 (texinfo-insert-master-menu-list
2095 (texinfo-multi-file-master-menu-list
2096 included-file-list)))))
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00002097
2098 ;; Remove unwanted extra lines.
2099 (save-excursion
2100 (goto-char (point-min))
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00002101
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00002102 (re-search-forward "^@menu")
2103 (forward-line -1)
b9f60161 Stefan Monnier2001-10-28 03:24:14 +00002104 (insert "\n") ; Ensure at least one blank line.
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00002105 (delete-blank-lines)
d1f9f441 Richard M. Stallman1997-08-04 01:11:01 +00002106
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00002107 (re-search-forward "^@end menu")
2108 (forward-line 1)
b9f60161 Stefan Monnier2001-10-28 03:24:14 +00002109 (insert "\n") ; Ensure at least one blank line.
daa6abb8
RS
Richard M. Stallman1993-07-07 22:15:20 +00002110 (delete-blank-lines))
2111
7e1335cf
RC
Robert J. Chassell1990-10-20 02:38:04 +00002112 (message "Multiple files updated."))
2113
daa6abb8 Richard M. Stallman1993-07-07 22:15:20 +00002114\f
b9f60161 Stefan Monnier2001-10-28 03:24:14 +00002115;; Place `provide' at end of file.
49116ac0
JB
Jim Blandy1992-03-16 20:39:07 +00002116(provide 'texnfo-upd)
2117
d501f516 Eric S. Raymond1992-05-30 20:24:49 +00002118;;; texnfo-upd.el ends here