home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / modes / texnfo-upd.el < prev    next >
Encoding:
Text File  |  1992-06-29  |  51.5 KB  |  1,385 lines

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