texnfo-upd.el 76 KB

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