home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / texinfo-3.1-src.lha / src / amiga / texinfo-3.1 / emacs / texnfo-upd.el < prev   
Text File  |  1993-03-26  |  78KB  |  2,030 lines

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