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