texinfo.el 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. ;;; texinfo.el --- major mode for editing Texinfo files -*- coding: utf-8 -*-
  2. ;; Copyright (C) 1985, 1988-1993, 1996-1997, 2000-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Robert J. Chassell
  5. ;; Date: [See date below for texinfo-version]
  6. ;; Maintainer: FSF
  7. ;; Keywords: maint, tex, docs
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Todo:
  20. ;; - facemenu support.
  21. ;; - command completion.
  22. ;;; Commentary:
  23. ;;; Code:
  24. (eval-when-compile (require 'tex-mode) (require 'cl))
  25. (defvar outline-heading-alist)
  26. (defgroup texinfo nil
  27. "Texinfo Mode."
  28. :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
  29. :group 'docs)
  30. ;;;###autoload
  31. (defcustom texinfo-open-quote (purecopy "``")
  32. "String inserted by typing \\[texinfo-insert-quote] to open a quotation."
  33. :type 'string
  34. :group 'texinfo)
  35. ;;;###autoload
  36. (defcustom texinfo-close-quote (purecopy "''")
  37. "String inserted by typing \\[texinfo-insert-quote] to close a quotation."
  38. :type 'string
  39. :group 'texinfo)
  40. (defcustom texinfo-mode-hook nil
  41. "Normal hook run when entering Texinfo mode."
  42. :type 'hook
  43. :options '(turn-on-auto-fill flyspell-mode)
  44. :group 'texinfo)
  45. ;;; Autoloads:
  46. (autoload 'makeinfo-region
  47. "makeinfo"
  48. "Make Info file from region of current Texinfo file, and switch to it.
  49. This command does not offer the `next-error' feature since it would
  50. apply to a temporary file, not the original; use the `makeinfo-buffer'
  51. command to gain use of `next-error'."
  52. t nil)
  53. (autoload 'makeinfo-buffer
  54. "makeinfo"
  55. "Make Info file from current buffer.
  56. Use the \\[next-error] command to move to the next error
  57. \(if there are errors\)."
  58. t nil)
  59. (autoload 'kill-compilation
  60. "compile"
  61. "Kill the process made by the \\[compile] command."
  62. t nil)
  63. (autoload 'makeinfo-recenter-compilation-buffer
  64. "makeinfo"
  65. "Redisplay `*compilation*' buffer so most recent output can be seen.
  66. The last line of the buffer is displayed on
  67. line LINE of the window, or centered if LINE is nil."
  68. t nil)
  69. (autoload 'texinfo-update-node
  70. "texnfo-upd"
  71. "Without any prefix argument, update the node in which point is located.
  72. Non-nil argument (prefix, if interactive) means update the nodes in the
  73. marked region.
  74. The functions for creating or updating nodes and menus, and their
  75. keybindings, are:
  76. `texinfo-update-node' (&optional region-p) \\[texinfo-update-node]
  77. `texinfo-every-node-update' () \\[texinfo-every-node-update]
  78. `texinfo-sequential-node-update' (&optional region-p)
  79. `texinfo-make-menu' (&optional region-p) \\[texinfo-make-menu]
  80. `texinfo-all-menus-update' () \\[texinfo-all-menus-update]
  81. `texinfo-master-menu' ()
  82. `texinfo-indent-menu-description' (column &optional region-p)
  83. The `texinfo-column-for-description' variable specifies the column to
  84. which menu descriptions are indented. Its default value is 32."
  85. t nil)
  86. (autoload 'texinfo-every-node-update
  87. "texnfo-upd"
  88. "Update every node in a Texinfo file."
  89. t nil)
  90. (autoload 'texinfo-sequential-node-update
  91. "texnfo-upd"
  92. "Update one node (or many) in a Texinfo file with sequential pointers.
  93. This function causes the `Next' or `Previous' pointer to point to the
  94. immediately preceding or following node, even if it is at a higher or
  95. lower hierarchical level in the document. Continually pressing `n' or
  96. `p' takes you straight through the file.
  97. Without any prefix argument, update the node in which point is located.
  98. Non-nil argument (prefix, if interactive) means update the nodes in the
  99. marked region.
  100. This command makes it awkward to navigate among sections and
  101. subsections; it should be used only for those documents that are meant
  102. to be read like a novel rather than a reference, and for which the
  103. Info `g*' command is inadequate."
  104. t nil)
  105. (autoload 'texinfo-make-menu
  106. "texnfo-upd"
  107. "Without any prefix argument, make or update a menu.
  108. Make the menu for the section enclosing the node found following point.
  109. Non-nil argument (prefix, if interactive) means make or update menus
  110. for nodes within or part of the marked region.
  111. Whenever a menu exists, and is being updated, the descriptions that
  112. are associated with node names in the pre-existing menu are
  113. incorporated into the new menu. Otherwise, the nodes' section titles
  114. are inserted as descriptions."
  115. t nil)
  116. (autoload 'texinfo-all-menus-update
  117. "texnfo-upd"
  118. "Update every regular menu in a Texinfo file.
  119. Remove pre-existing master menu, if there is one.
  120. If called with a non-nil argument, this function first updates all the
  121. nodes in the buffer before updating the menus."
  122. t nil)
  123. (autoload 'texinfo-master-menu
  124. "texnfo-upd"
  125. "Make a master menu for a whole Texinfo file.
  126. Non-nil argument (prefix, if interactive) means first update all
  127. existing nodes and menus. Remove pre-existing master menu, if there is one.
  128. This function creates a master menu that follows the top node. The
  129. master menu includes every entry from all the other menus. It
  130. replaces any existing ordinary menu that follows the top node.
  131. If called with a non-nil argument, this function first updates all the
  132. menus in the buffer (incorporating descriptions from pre-existing
  133. menus) before it constructs the master menu.
  134. The function removes the detailed part of an already existing master
  135. menu. This action depends on the pre-existing master menu using the
  136. standard `texinfo-master-menu-header'.
  137. The master menu has the following format, which is adapted from the
  138. recommendation in the Texinfo Manual:
  139. * The first part contains the major nodes in the Texinfo file: the
  140. nodes for the chapters, chapter-like sections, and the major
  141. appendices. This includes the indices, so long as they are in
  142. chapter-like sections, such as unnumbered sections.
  143. * The second and subsequent parts contain a listing of the other,
  144. lower level menus, in order. This way, an inquirer can go
  145. directly to a particular node if he or she is searching for
  146. specific information.
  147. Each of the menus in the detailed node listing is introduced by the
  148. title of the section containing the menu."
  149. t nil)
  150. (autoload 'texinfo-indent-menu-description
  151. "texnfo-upd"
  152. "Indent every description in menu following point to COLUMN.
  153. Non-nil argument (prefix, if interactive) means indent every
  154. description in every menu in the region. Does not indent second and
  155. subsequent lines of a multi-line description."
  156. t nil)
  157. (autoload 'texinfo-insert-node-lines
  158. "texnfo-upd"
  159. "Insert missing `@node' lines in region of Texinfo file.
  160. Non-nil argument (prefix, if interactive) means also to insert the
  161. section titles as node names; and also to insert the section titles as
  162. node names in pre-existing @node lines that lack names."
  163. t nil)
  164. (autoload 'texinfo-start-menu-description
  165. "texnfo-upd"
  166. "In this menu entry, insert the node's section title as a description.
  167. Position point at beginning of description ready for editing.
  168. Do not insert a title if the line contains an existing description.
  169. You will need to edit the inserted text since a useful description
  170. complements the node name rather than repeats it as a title does."
  171. t nil)
  172. (autoload 'texinfo-multiple-files-update
  173. "texnfo-upd"
  174. "Update first node pointers in each file included in OUTER-FILE;
  175. create or update main menu in the outer file that refers to such nodes.
  176. This does not create or update menus or pointers within the included files.
  177. With optional MAKE-MASTER-MENU argument (prefix arg, if interactive),
  178. insert a master menu in OUTER-FILE. This does not create or update
  179. menus or pointers within the included files.
  180. With optional UPDATE-EVERYTHING argument (numeric prefix arg, if
  181. interactive), update all the menus and all the `Next', `Previous', and
  182. `Up' pointers of all the files included in OUTER-FILE before inserting
  183. a master menu in OUTER-FILE.
  184. The command also updates the `Top' level node pointers of OUTER-FILE.
  185. Notes:
  186. * this command does NOT save any files--you must save the
  187. outer file and any modified, included files.
  188. * except for the `Top' node, this command does NOT handle any
  189. pre-existing nodes in the outer file; hence, indices must be
  190. enclosed in an included file.
  191. Requirements:
  192. * each of the included files must contain exactly one highest
  193. hierarchical level node,
  194. * this highest node must be the first node in the included file,
  195. * each highest hierarchical level node must be of the same type.
  196. Thus, normally, each included file contains one, and only one,
  197. chapter."
  198. t nil)
  199. ;;; Code:
  200. ;;; Don't you dare insert any `require' calls at top level in this file--rms.
  201. (defvar texinfo-section-list
  202. '(("top" 1)
  203. ("chapter" 2)
  204. ("section" 3)
  205. ("subsection" 4)
  206. ("subsubsection" 5)
  207. ("unnumbered" 2)
  208. ("unnumberedsec" 3)
  209. ("unnumberedsubsec" 4)
  210. ("unnumberedsubsubsec" 5)
  211. ("appendix" 2)
  212. ("appendixsec" 3)
  213. ("appendixsection" 3)
  214. ("appendixsubsec" 4)
  215. ("appendixsubsubsec" 5)
  216. ("majorheading" 2)
  217. ("chapheading" 2)
  218. ("heading" 3)
  219. ("subheading" 4)
  220. ("subsubheading" 5))
  221. "Alist of sectioning commands and their relative level.")
  222. ;;; Syntax table
  223. (defvar texinfo-mode-syntax-table
  224. (let ((st (make-syntax-table)))
  225. (modify-syntax-entry ?\" "." st)
  226. (modify-syntax-entry ?\\ "." st)
  227. (modify-syntax-entry ?@ "\\" st)
  228. (modify-syntax-entry ?\^q "\\" st)
  229. (modify-syntax-entry ?\[ "(]" st)
  230. (modify-syntax-entry ?\] ")[" st)
  231. (modify-syntax-entry ?{ "(}" st)
  232. (modify-syntax-entry ?} "){" st)
  233. (modify-syntax-entry ?\n ">" st)
  234. (modify-syntax-entry ?\' "w" st)
  235. st))
  236. ;; Written by Wolfgang Bangerth <zcg51122@rpool1.rus.uni-stuttgart.de>
  237. ;; To override this example, set either `imenu-generic-expression'
  238. ;; or `imenu-create-index-function'.
  239. (defvar texinfo-imenu-generic-expression
  240. '((nil "^@\\(node\\|anchor\\)[ \t]+\\([^,\n]*\\)" 2)
  241. ("Chapters" "^@chapter[ \t]+\\(.*\\)$" 1))
  242. "Imenu generic expression for Texinfo mode. See `imenu-generic-expression'.")
  243. (defconst texinfo-syntax-propertize-function
  244. (syntax-propertize-rules
  245. ("\\(@\\)c\\(omment\\)?\\>" (1 "<"))
  246. ("^\\(@\\)ignore\\>" (1 "< b"))
  247. ("^@end ignore\\(\n\\)" (1 "> b")))
  248. "Syntactic keywords to catch comment delimiters in `texinfo-mode'.")
  249. (defconst texinfo-environments
  250. '("cartouche" "copying" "defcv" "deffn" "defivar" "defmac"
  251. "defmethod" "defop" "defopt" "defspec" "deftp" "deftypecv"
  252. "deftypefn" "deftypefun" "deftypeivar" "deftypemethod"
  253. "deftypeop" "deftypevar" "deftypevr" "defun" "defvar"
  254. "defvr" "description" "detailmenu" "direntry" "display"
  255. "documentdescription" "enumerate" "example" "flushleft"
  256. "flushright" "format" "ftable" "group" "html" "ifclear" "ifset"
  257. "ifhtml" "ifinfo" "ifnothtml" "ifnotinfo" "ifnotplaintext"
  258. "ifnottex" "ifplaintext" "iftex" "ignore" "itemize" "lisp"
  259. "macro" "menu" "multitable" "quotation" "smalldisplay"
  260. "smallexample" "smallformat" "smalllisp" "table" "tex"
  261. "titlepage" "verbatim" "vtable")
  262. "List of Texinfo environments.")
  263. (defconst texinfo-environment-regexp
  264. (concat "^@" (regexp-opt (cons "end" texinfo-environments) t) "\\>")
  265. "Regexp for environment-like Texinfo list commands.
  266. Subexpression 1 is what goes into the corresponding `@end' statement.")
  267. (defface texinfo-heading
  268. '((t (:inherit font-lock-function-name-face)))
  269. "Face used for section headings in `texinfo-mode'."
  270. :group 'texinfo)
  271. (define-obsolete-face-alias 'texinfo-heading-face 'texinfo-heading "22.1")
  272. (defvar texinfo-heading-face 'texinfo-heading)
  273. (defvar texinfo-font-lock-keywords
  274. `(;; All but the first had an OVERRIDE of t.
  275. ;; It didn't seem to be any better, and it's slower--simon.
  276. ;; Robert J. Chassell <bob@gnu.org> says remove this line.
  277. ;;("\\$\\([^$]*\\)\\$" 1 font-lock-string-face t)
  278. ("@\\([a-zA-Z]+\\|[^ \t\n]\\)" 1 font-lock-keyword-face) ;commands
  279. ("^\\*\\([^\n:]*\\)" 1 font-lock-function-name-face t) ;menu items
  280. ("@\\(emph\\|i\\|sc\\){\\([^}]+\\)" 2 'italic)
  281. ("@\\(strong\\|b\\){\\([^}]+\\)" 2 'bold)
  282. ("@\\(kbd\\|key\\|url\\|uref\\){\\([^}]+\\)" 2 font-lock-string-face)
  283. ;; The following two groups have an OVERRIDE of `keep' because
  284. ;; their arguments frequently include a @@, and we don't want that
  285. ;; to overwrite the normal fontification of the argument.
  286. ("@\\(file\\|email\\){\\([^}]+\\)" 2 font-lock-string-face keep)
  287. ("@\\(samp\\|code\\|var\\|math\\|env\\|command\\|option\\){\\([^}]+\\)"
  288. 2 font-lock-variable-name-face keep)
  289. ("@\\(cite\\|x?ref\\|pxref\\|dfn\\|inforef\\){\\([^}]+\\)"
  290. 2 font-lock-constant-face)
  291. ("@\\(anchor\\){\\([^}]+\\)" 2 font-lock-type-face)
  292. ("@\\(dmn\\|acronym\\|value\\){\\([^}]+\\)" 2 font-lock-builtin-face)
  293. ("@\\(end\\|itemx?\\) +\\(.+\\)" 2 font-lock-keyword-face keep)
  294. ;; (,texinfo-environment-regexp
  295. ;; 1 (texinfo-clone-environment (match-beginning 1) (match-end 1)) keep)
  296. (,(concat "^@" (regexp-opt (mapcar 'car texinfo-section-list) t)
  297. ".*\n") 0 texinfo-heading-face t))
  298. "Additional expressions to highlight in Texinfo mode.")
  299. (defun texinfo-clone-environment (start end)
  300. (let ((endp nil))
  301. (save-excursion
  302. (ignore-errors
  303. (goto-char start)
  304. (when (looking-at "end\\Sw+\\(\\sw+\\)")
  305. (setq endp t start (match-beginning 1) end (match-end 1)))
  306. (unless (get-char-property start 'text-clones)
  307. (if endp
  308. (texinfo-last-unended-begin)
  309. (forward-word 1)
  310. (texinfo-next-unmatched-end))
  311. (skip-syntax-forward "^w")
  312. (when (looking-at
  313. (concat (regexp-quote (buffer-substring start end)) "\\>"))
  314. (text-clone-create start end 'spread "\\w*")))))))
  315. ;;; Keybindings
  316. ;;; Keys common both to Texinfo mode and to TeX shell.
  317. (defun texinfo-define-common-keys (keymap)
  318. "Define the keys both in Texinfo mode and in the texinfo-tex-shell."
  319. (define-key keymap "\C-c\C-t\C-k" 'tex-kill-job)
  320. (define-key keymap "\C-c\C-t\C-x" 'texinfo-quit-job)
  321. (define-key keymap "\C-c\C-t\C-l" 'tex-recenter-output-buffer)
  322. (define-key keymap "\C-c\C-t\C-d" 'texinfo-delete-from-print-queue)
  323. (define-key keymap "\C-c\C-t\C-q" 'tex-show-print-queue)
  324. (define-key keymap "\C-c\C-t\C-p" 'texinfo-tex-print)
  325. (define-key keymap "\C-c\C-t\C-v" 'texinfo-tex-view)
  326. (define-key keymap "\C-c\C-t\C-i" 'texinfo-texindex)
  327. (define-key keymap "\C-c\C-t\C-r" 'texinfo-tex-region)
  328. (define-key keymap "\C-c\C-t\C-b" 'texinfo-tex-buffer))
  329. ;; Mode documentation displays commands in reverse order
  330. ;; from how they are listed in the texinfo-mode-map.
  331. (defvar texinfo-mode-map
  332. (let ((map (make-sparse-keymap)))
  333. ;; bindings for `texnfo-tex.el'
  334. (texinfo-define-common-keys map)
  335. (define-key map "\"" 'texinfo-insert-quote)
  336. ;; bindings for `makeinfo.el'
  337. (define-key map "\C-c\C-m\C-k" 'kill-compilation)
  338. (define-key map "\C-c\C-m\C-l"
  339. 'makeinfo-recenter-compilation-buffer)
  340. (define-key map "\C-c\C-m\C-r" 'makeinfo-region)
  341. (define-key map "\C-c\C-m\C-b" 'makeinfo-buffer)
  342. ;; bindings for `texinfmt.el'
  343. (define-key map "\C-c\C-e\C-r" 'texinfo-format-region)
  344. (define-key map "\C-c\C-e\C-b" 'texinfo-format-buffer)
  345. ;; AUCTeX-like bindings
  346. (define-key map "\e\r" 'texinfo-insert-@item)
  347. ;; bindings for updating nodes and menus
  348. (define-key map "\C-c\C-um" 'texinfo-master-menu)
  349. (define-key map "\C-c\C-u\C-m" 'texinfo-make-menu)
  350. (define-key map "\C-c\C-u\C-n" 'texinfo-update-node)
  351. (define-key map "\C-c\C-u\C-e" 'texinfo-every-node-update)
  352. (define-key map "\C-c\C-u\C-a" 'texinfo-all-menus-update)
  353. (define-key map "\C-c\C-s" 'texinfo-show-structure)
  354. (define-key map "\C-c}" 'up-list)
  355. ;; FIXME: This is often used for "close block" aka texinfo-insert-@end.
  356. (define-key map "\C-c]" 'up-list)
  357. (define-key map "\C-c/" 'texinfo-insert-@end)
  358. (define-key map "\C-c{" 'texinfo-insert-braces)
  359. ;; bindings for inserting strings
  360. (define-key map "\C-c\C-o" 'texinfo-insert-block)
  361. (define-key map "\C-c\C-c\C-d" 'texinfo-start-menu-description)
  362. (define-key map "\C-c\C-c\C-s" 'texinfo-insert-@strong)
  363. (define-key map "\C-c\C-c\C-e" 'texinfo-insert-@emph)
  364. (define-key map "\C-c\C-cv" 'texinfo-insert-@var)
  365. (define-key map "\C-c\C-cu" 'texinfo-insert-@uref)
  366. (define-key map "\C-c\C-ct" 'texinfo-insert-@table)
  367. (define-key map "\C-c\C-cs" 'texinfo-insert-@samp)
  368. (define-key map "\C-c\C-cq" 'texinfo-insert-@quotation)
  369. (define-key map "\C-c\C-co" 'texinfo-insert-@noindent)
  370. (define-key map "\C-c\C-cn" 'texinfo-insert-@node)
  371. (define-key map "\C-c\C-cm" 'texinfo-insert-@email)
  372. (define-key map "\C-c\C-ck" 'texinfo-insert-@kbd)
  373. (define-key map "\C-c\C-ci" 'texinfo-insert-@item)
  374. (define-key map "\C-c\C-cf" 'texinfo-insert-@file)
  375. (define-key map "\C-c\C-cx" 'texinfo-insert-@example)
  376. (define-key map "\C-c\C-ce" 'texinfo-insert-@end)
  377. (define-key map "\C-c\C-cd" 'texinfo-insert-@dfn)
  378. (define-key map "\C-c\C-cc" 'texinfo-insert-@code)
  379. map))
  380. (easy-menu-define texinfo-mode-menu
  381. texinfo-mode-map
  382. "Menu used for `texinfo-mode'."
  383. '("Texinfo"
  384. ["Insert block" texinfo-insert-block t]
  385. ;; ["Insert node" texinfo-insert-@node t]
  386. "----"
  387. ["Update All" (lambda () (interactive) (texinfo-master-menu t))
  388. :keys "\\[universal-argument] \\[texinfo-master-menu]"]
  389. ["Update every node" texinfo-every-node-update t]
  390. ["Update node" texinfo-update-node t]
  391. ["Make Master menu" texinfo-master-menu t]
  392. ["Make menu" texinfo-make-menu t]
  393. ["Update all menus" texinfo-all-menus-update t]
  394. "----"
  395. ["Show structure" texinfo-show-structure t]
  396. ["Format region" texinfo-format-region t]
  397. ["Format buffer" texinfo-format-buffer t]
  398. ["Makeinfo region" makeinfo-region t]
  399. ["Makeinfo buffer" makeinfo-buffer t]))
  400. (defun texinfo-filter (section list)
  401. (let (res)
  402. (dolist (x list) (if (eq section (cadr x)) (push (car x) res)))
  403. res))
  404. (defvar texinfo-chapter-level-regexp
  405. (regexp-opt (texinfo-filter 2 texinfo-section-list))
  406. "Regular expression matching just the Texinfo chapter level headings.")
  407. ;;; Texinfo mode
  408. ;;;###autoload
  409. (define-derived-mode texinfo-mode text-mode "Texinfo"
  410. "Major mode for editing Texinfo files.
  411. It has these extra commands:
  412. \\{texinfo-mode-map}
  413. These are files that are used as input for TeX to make printed manuals
  414. and also to be turned into Info files with \\[makeinfo-buffer] or
  415. the `makeinfo' program. These files must be written in a very restricted and
  416. modified version of TeX input format.
  417. Editing commands are like text-mode except that the syntax table is
  418. set up so expression commands skip Texinfo bracket groups. To see
  419. what the Info version of a region of the Texinfo file will look like,
  420. use \\[makeinfo-region], which runs `makeinfo' on the current region.
  421. You can show the structure of a Texinfo file with \\[texinfo-show-structure].
  422. This command shows the structure of a Texinfo file by listing the
  423. lines with the @-sign commands for @chapter, @section, and the like.
  424. These lines are displayed in another window called the *Occur* window.
  425. In that window, you can position the cursor over one of the lines and
  426. use \\[occur-mode-goto-occurrence], to jump to the corresponding spot
  427. in the Texinfo file.
  428. In addition, Texinfo mode provides commands that insert various
  429. frequently used @-sign commands into the buffer. You can use these
  430. commands to save keystrokes. And you can insert balanced braces with
  431. \\[texinfo-insert-braces] and later use the command \\[up-list] to
  432. move forward past the closing brace.
  433. Also, Texinfo mode provides functions for automatically creating or
  434. updating menus and node pointers. These functions
  435. * insert the `Next', `Previous' and `Up' pointers of a node,
  436. * insert or update the menu for a section, and
  437. * create a master menu for a Texinfo source file.
  438. Here are the functions:
  439. texinfo-update-node \\[texinfo-update-node]
  440. texinfo-every-node-update \\[texinfo-every-node-update]
  441. texinfo-sequential-node-update
  442. texinfo-make-menu \\[texinfo-make-menu]
  443. texinfo-all-menus-update \\[texinfo-all-menus-update]
  444. texinfo-master-menu
  445. texinfo-indent-menu-description (column &optional region-p)
  446. The `texinfo-column-for-description' variable specifies the column to
  447. which menu descriptions are indented.
  448. Passed an argument (a prefix argument, if interactive), the
  449. `texinfo-update-node' and `texinfo-make-menu' functions do their jobs
  450. in the region.
  451. To use the updating commands, you must structure your Texinfo file
  452. hierarchically, such that each `@node' line, with the exception of the
  453. Top node, is accompanied by some kind of section line, such as an
  454. `@chapter' or `@section' line.
  455. If the file has a `top' node, it must be called `top' or `Top' and
  456. be the first node in the file.
  457. Entering Texinfo mode calls the value of `text-mode-hook', and then the
  458. value of `texinfo-mode-hook'."
  459. (set (make-local-variable 'page-delimiter)
  460. (concat
  461. "^@node [ \t]*[Tt]op\\|^@\\("
  462. texinfo-chapter-level-regexp
  463. "\\)\\>"))
  464. (make-local-variable 'require-final-newline)
  465. (setq require-final-newline mode-require-final-newline)
  466. (make-local-variable 'indent-tabs-mode)
  467. (setq indent-tabs-mode nil)
  468. (make-local-variable 'paragraph-separate)
  469. (setq paragraph-separate
  470. (concat "\b\\|@[a-zA-Z]*[ \n]\\|" paragraph-separate))
  471. (make-local-variable 'paragraph-start)
  472. (setq paragraph-start (concat "\b\\|@[a-zA-Z]*[ \n]\\|" paragraph-start))
  473. (set (make-local-variable 'sentence-end-base)
  474. "\\(@\\(end\\)?dots{}\\|[.?!]\\)[]\"'”)}]*")
  475. (make-local-variable 'fill-column)
  476. (setq fill-column 70)
  477. (make-local-variable 'comment-start)
  478. (setq comment-start "@c ")
  479. (make-local-variable 'comment-start-skip)
  480. (setq comment-start-skip "@c +\\|@comment +")
  481. (make-local-variable 'words-include-escapes)
  482. (setq words-include-escapes t)
  483. (make-local-variable 'imenu-generic-expression)
  484. (setq imenu-generic-expression texinfo-imenu-generic-expression)
  485. (setq imenu-case-fold-search nil)
  486. (make-local-variable 'font-lock-defaults)
  487. (setq font-lock-defaults
  488. '(texinfo-font-lock-keywords nil nil nil backward-paragraph))
  489. (set (make-local-variable 'syntax-propertize-function)
  490. texinfo-syntax-propertize-function)
  491. (set (make-local-variable 'parse-sexp-lookup-properties) t)
  492. ;; Outline settings.
  493. (set (make-local-variable 'outline-heading-alist)
  494. ;; We should merge outline-heading-alist and texinfo-section-list
  495. ;; but in the mean time, let's just generate one from the other.
  496. (mapcar (lambda (x) (cons (concat "@" (car x)) (cadr x)))
  497. texinfo-section-list))
  498. (set (make-local-variable 'outline-regexp)
  499. (concat (regexp-opt (mapcar 'car outline-heading-alist) t)
  500. "\\>"))
  501. (make-local-variable 'tex-start-of-header)
  502. (setq tex-start-of-header "%\\*\\*start")
  503. (make-local-variable 'tex-end-of-header)
  504. (setq tex-end-of-header "%\\*\\*end")
  505. (make-local-variable 'tex-first-line-header-regexp)
  506. (setq tex-first-line-header-regexp "^\\\\input")
  507. (make-local-variable 'tex-trailer)
  508. (setq tex-trailer "@bye\n")
  509. ;; Prevent filling certain lines, in addition to ones specified
  510. ;; by the user.
  511. (let ((prevent-filling "^@\\(def\\|multitable\\)"))
  512. (set (make-local-variable 'auto-fill-inhibit-regexp)
  513. (if (null auto-fill-inhibit-regexp)
  514. prevent-filling
  515. (concat auto-fill-inhibit-regexp "\\|" prevent-filling)))))
  516. ;;; Insert string commands
  517. (defvar texinfo-block-default "example")
  518. (define-skeleton texinfo-insert-block
  519. "Create a matching pair @<cmd> .. @end <cmd> at point.
  520. Puts point on a blank line between them."
  521. (setq texinfo-block-default
  522. (completing-read (format "Block name [%s]: " texinfo-block-default)
  523. texinfo-environments
  524. nil nil nil nil texinfo-block-default))
  525. \n "@" str
  526. ;; Blocks that take parameters: all the def* blocks take parameters,
  527. ;; plus a few others.
  528. (if (or (string-match "\\`def" str)
  529. (member str '("table" "ftable" "vtable")))
  530. '(nil " " -))
  531. \n _ \n "@end " str \n)
  532. (defun texinfo-inside-macro-p (macro &optional bound)
  533. "Non-nil if inside a macro matching the regexp MACRO."
  534. (condition-case nil
  535. (save-excursion
  536. (save-restriction
  537. (narrow-to-region bound (point))
  538. (while (progn
  539. (up-list -1)
  540. (not (condition-case nil
  541. (save-excursion
  542. (backward-sexp 1)
  543. (looking-at macro))
  544. (scan-error nil)))))
  545. t))
  546. (scan-error nil)))
  547. (defun texinfo-inside-env-p (env &optional bound)
  548. "Non-nil if inside an environment matching the regexp @ENV."
  549. (save-excursion
  550. (and (re-search-backward (concat "@\\(end\\s +\\)?" env) bound t)
  551. (not (match-end 1)))))
  552. (defvar texinfo-enable-quote-macros "@\\(code\\|samp\\|kbd\\)\\>")
  553. (defvar texinfo-enable-quote-envs '("example\\>" "lisp\\>"))
  554. (defun texinfo-insert-quote (&optional arg)
  555. "Insert the appropriate quote mark for Texinfo.
  556. Usually inserts the value of `texinfo-open-quote' (normally ``) or
  557. `texinfo-close-quote' (normally ''), depending on the context.
  558. With prefix argument or inside @code or @example, inserts a plain \"."
  559. (interactive "*P")
  560. (let ((top (or (save-excursion (re-search-backward "@node\\>" nil t))
  561. (point-min))))
  562. (if (or arg
  563. (= (preceding-char) ?\\)
  564. (save-excursion
  565. ;; Might be near the start of a (narrowed) buffer.
  566. (ignore-errors (backward-char (length texinfo-open-quote)))
  567. (when (or (looking-at texinfo-open-quote)
  568. (looking-at texinfo-close-quote))
  569. (delete-char (length texinfo-open-quote))
  570. t))
  571. (texinfo-inside-macro-p texinfo-enable-quote-macros top)
  572. (let ((in-env nil))
  573. (dolist (env texinfo-enable-quote-envs in-env)
  574. (if (texinfo-inside-env-p env top)
  575. (setq in-env t)))))
  576. (self-insert-command (prefix-numeric-value arg))
  577. (insert
  578. (if (or (bobp)
  579. (memq (char-syntax (preceding-char)) '(?\( ?> ?\s)))
  580. texinfo-open-quote
  581. texinfo-close-quote)))))
  582. ;; The following texinfo-insert-@end command not only inserts a SPC
  583. ;; after the @end, but tries to find out what belongs there. It is
  584. ;; not very smart: it does not understand nested lists.
  585. (defun texinfo-last-unended-begin ()
  586. (while (and (re-search-backward texinfo-environment-regexp)
  587. (looking-at "@end"))
  588. (texinfo-last-unended-begin)))
  589. (defun texinfo-next-unmatched-end ()
  590. (while (and (re-search-forward texinfo-environment-regexp)
  591. (save-excursion
  592. (goto-char (match-beginning 0))
  593. (not (looking-at "@end"))))
  594. (texinfo-next-unmatched-end)))
  595. (define-skeleton texinfo-insert-@end
  596. "Insert the matching `@end' for the last Texinfo command that needs one."
  597. (ignore-errors
  598. (save-excursion
  599. (backward-word 1)
  600. (texinfo-last-unended-begin)
  601. (or (match-string 1) '-)))
  602. \n "@end " str \n)
  603. (define-skeleton texinfo-insert-braces
  604. "Make a pair of braces and be poised to type inside of them.
  605. Use \\[up-list] to move forward out of the braces."
  606. nil
  607. "{" _ "}")
  608. (define-skeleton texinfo-insert-@code
  609. "Insert a `@code{...}' command in a Texinfo buffer.
  610. A numeric argument says how many words the braces should surround.
  611. The default is not to surround any existing words with the braces."
  612. nil
  613. "@code{" _ "}")
  614. (define-skeleton texinfo-insert-@dfn
  615. "Insert a `@dfn{...}' command in a Texinfo buffer.
  616. A numeric argument says how many words the braces should surround.
  617. The default is not to surround any existing words with the braces."
  618. nil
  619. "@dfn{" _ "}")
  620. (define-skeleton texinfo-insert-@email
  621. "Insert a `@email{...}' command in a Texinfo buffer.
  622. A numeric argument says how many words the braces should surround.
  623. The default is not to surround any existing words with the braces."
  624. nil
  625. "@email{" _ "}")
  626. (define-skeleton texinfo-insert-@emph
  627. "Insert a `@emph{...}' command in a Texinfo buffer.
  628. A numeric argument says how many words the braces should surround.
  629. The default is not to surround any existing words with the braces."
  630. nil
  631. "@emph{" _ "}")
  632. (define-skeleton texinfo-insert-@example
  633. "Insert the string `@example' in a Texinfo buffer."
  634. nil
  635. \n "@example" \n)
  636. (define-skeleton texinfo-insert-@file
  637. "Insert a `@file{...}' command in a Texinfo buffer.
  638. A numeric argument says how many words the braces should surround.
  639. The default is not to surround any existing words with the braces."
  640. nil
  641. "@file{" _ "}")
  642. (define-skeleton texinfo-insert-@item
  643. "Insert the string `@item' in a Texinfo buffer.
  644. If in a table defined by @table, follow said string with a space.
  645. Otherwise, follow with a newline."
  646. nil
  647. \n "@item"
  648. (if (equal (ignore-errors
  649. (save-excursion
  650. (texinfo-last-unended-begin)
  651. (match-string 1)))
  652. "table")
  653. " " '\n)
  654. _ \n)
  655. (define-skeleton texinfo-insert-@kbd
  656. "Insert a `@kbd{...}' command in a Texinfo buffer.
  657. A numeric argument says how many words the braces should surround.
  658. The default is not to surround any existing words with the braces."
  659. nil
  660. "@kbd{" _ "}")
  661. (define-skeleton texinfo-insert-@node
  662. "Insert the string `@node' in a Texinfo buffer.
  663. Insert a comment on the following line indicating the order of
  664. arguments to @node. Insert a carriage return after the comment line.
  665. Leave point after `@node'."
  666. nil
  667. \n "@node " _ \n)
  668. (define-skeleton texinfo-insert-@noindent
  669. "Insert the string `@noindent' in a Texinfo buffer."
  670. nil
  671. \n "@noindent" \n)
  672. (define-skeleton texinfo-insert-@quotation
  673. "Insert the string `@quotation' in a Texinfo buffer."
  674. \n "@quotation" \n)
  675. (define-skeleton texinfo-insert-@samp
  676. "Insert a `@samp{...}' command in a Texinfo buffer.
  677. A numeric argument says how many words the braces should surround.
  678. The default is not to surround any existing words with the braces."
  679. nil
  680. "@samp{" _ "}")
  681. (define-skeleton texinfo-insert-@strong
  682. "Insert a `@strong{...}' command in a Texinfo buffer.
  683. A numeric argument says how many words the braces should surround.
  684. The default is not to surround any existing words with the braces."
  685. nil
  686. "@strong{" _ "}")
  687. (define-skeleton texinfo-insert-@table
  688. "Insert the string `@table' in a Texinfo buffer."
  689. nil
  690. \n "@table " _ \n)
  691. (define-skeleton texinfo-insert-@var
  692. "Insert a `@var{}' command in a Texinfo buffer.
  693. A numeric argument says how many words the braces should surround.
  694. The default is not to surround any existing words with the braces."
  695. nil
  696. "@var{" _ "}")
  697. (define-skeleton texinfo-insert-@uref
  698. "Insert a `@uref{}' command in a Texinfo buffer.
  699. A numeric argument says how many words the braces should surround.
  700. The default is not to surround any existing words with the braces."
  701. nil
  702. "@uref{" _ "}")
  703. (defalias 'texinfo-insert-@url 'texinfo-insert-@uref)
  704. ;;; Texinfo file structure
  705. (defun texinfo-show-structure (&optional nodes-too)
  706. "Show the structure of a Texinfo file.
  707. List the lines in the file that begin with the @-sign commands for
  708. @chapter, @section, and the like.
  709. With optional argument (prefix if interactive), list both the lines
  710. with @-sign commands for @chapter, @section, and the like, and list
  711. @node lines.
  712. Lines with structuring commands beginning in them are displayed in
  713. another buffer named `*Occur*'. In that buffer, you can move point to
  714. one of those lines and then use
  715. \\<occur-mode-map>\\[occur-mode-goto-occurrence],
  716. to jump to the corresponding spot in the Texinfo source file."
  717. (interactive "P")
  718. ;; First, remember current location
  719. (let (current-location)
  720. (save-excursion
  721. (end-of-line) ; so as to find section on current line
  722. (if (re-search-backward
  723. ;; do not require `texinfo-section-types-regexp' in texnfo-upd.el
  724. "^@\\(chapter \\|sect\\|subs\\|subh\\|unnum\\|major\\|chapheading \\|heading \\|appendix\\)"
  725. nil t)
  726. (setq current-location
  727. (progn
  728. (beginning-of-line)
  729. (buffer-substring (point) (progn (end-of-line) (point)))))
  730. ;; else point is located before any section command.
  731. (setq current-location "tex")))
  732. ;; Second, create and format an *Occur* buffer
  733. (save-excursion
  734. (goto-char (point-min))
  735. (occur (concat "^\\(?:" (if nodes-too "@node\\>\\|")
  736. outline-regexp "\\)")))
  737. (pop-to-buffer "*Occur*")
  738. (goto-char (point-min))
  739. (let ((inhibit-read-only t))
  740. (flush-lines "-----")
  741. ;; Now format the "*Occur*" buffer to show the structure.
  742. ;; Thanks to ceder@signum.se (Per Cederqvist)
  743. (goto-char (point-max))
  744. (let (level)
  745. (while (re-search-backward "^ *[0-9]*:@\\(\\sw+\\)" nil 0)
  746. (goto-char (1- (match-beginning 1)))
  747. (setq level
  748. (or (cadr (assoc (match-string 1) texinfo-section-list)) 2))
  749. (indent-to-column (+ (current-column) (* 4 (- level 2))))
  750. (beginning-of-line))))
  751. ;; Third, go to line corresponding to location in source file
  752. ;; potential bug: two exactly similar `current-location' lines ...
  753. (goto-char (point-min))
  754. (re-search-forward current-location nil t)
  755. (beginning-of-line)
  756. ))
  757. ;;; The tex and print function definitions:
  758. (defcustom texinfo-texi2dvi-command "texi2dvi"
  759. "Command used by `texinfo-tex-buffer' to run TeX and texindex on a buffer."
  760. :type 'string
  761. :group 'texinfo)
  762. (defcustom texinfo-tex-command "tex"
  763. "Command used by `texinfo-tex-region' to run TeX on a region."
  764. :type 'string
  765. :group 'texinfo)
  766. (defcustom texinfo-texindex-command "texindex"
  767. "Command used by `texinfo-texindex' to sort unsorted index files."
  768. :type 'string
  769. :group 'texinfo)
  770. (defcustom texinfo-delete-from-print-queue-command "lprm"
  771. "Command string used to delete a job from the line printer queue.
  772. Command is used by \\[texinfo-delete-from-print-queue] based on
  773. number provided by a previous \\[tex-show-print-queue]
  774. command."
  775. :type 'string
  776. :group 'texinfo)
  777. (defvar texinfo-tex-trailer "@bye"
  778. "String appended after a region sent to TeX by `texinfo-tex-region'.")
  779. (defun texinfo-tex-region (beg end)
  780. "Run TeX on the current region.
  781. This works by writing a temporary file (`tex-zap-file') in the directory
  782. that is the value of `tex-directory', then running TeX on that file.
  783. The first line of the buffer is copied to the
  784. temporary file; and if the buffer has a header, it is written to the
  785. temporary file before the region itself. The buffer's header is all lines
  786. between the strings defined by `tex-start-of-header' and `tex-end-of-header'
  787. inclusive. The header must start in the first 100 lines.
  788. The value of `texinfo-tex-trailer' is appended to the temporary file after the region."
  789. (interactive "r")
  790. (require 'tex-mode)
  791. (let ((tex-command texinfo-tex-command)
  792. (tex-trailer texinfo-tex-trailer))
  793. (tex-region beg end)))
  794. (defun texinfo-tex-buffer ()
  795. "Run TeX on visited file, once or twice, to make a correct `.dvi' file."
  796. (interactive)
  797. (require 'tex-mode)
  798. (let ((tex-command texinfo-texi2dvi-command)
  799. ;; Disable tex-start-options-string. texi2dvi would not
  800. ;; understand anything specified here.
  801. (tex-start-options-string ""))
  802. (tex-buffer)))
  803. (defun texinfo-texindex ()
  804. "Run `texindex' on unsorted index files.
  805. The index files are made by \\[texinfo-tex-region] or \\[texinfo-tex-buffer].
  806. This runs the shell command defined by `texinfo-texindex-command'."
  807. (interactive)
  808. (require 'tex-mode)
  809. (tex-send-command texinfo-texindex-command (concat tex-zap-file ".??"))
  810. ;; alternatively
  811. ;; (send-string "tex-shell"
  812. ;; (concat texinfo-texindex-command
  813. ;; " " tex-zap-file ".??" "\n"))
  814. (tex-recenter-output-buffer nil))
  815. (defun texinfo-tex-print ()
  816. "Print `.dvi' file made by \\[texinfo-tex-region] or \\[texinfo-tex-buffer].
  817. This runs the shell command defined by `tex-dvi-print-command'."
  818. (interactive)
  819. (require 'tex-mode)
  820. (tex-print))
  821. (defun texinfo-tex-view ()
  822. "View `.dvi' file made by \\[texinfo-tex-region] or \\[texinfo-tex-buffer].
  823. This runs the shell command defined by `tex-dvi-view-command'."
  824. (interactive)
  825. (require 'tex-mode)
  826. (tex-view))
  827. (defun texinfo-quit-job ()
  828. "Quit currently running TeX job, by sending an `x' to it."
  829. (interactive)
  830. (if (not (get-process "tex-shell"))
  831. (error "No TeX shell running"))
  832. (tex-send-command "x"))
  833. ;; alternatively:
  834. ;; save-excursion
  835. ;; (set-buffer (get-buffer "*tex-shell*"))
  836. ;; (goto-char (point-max))
  837. ;; (insert "x")
  838. ;; (comint-send-input)
  839. (defun texinfo-delete-from-print-queue (job-number)
  840. "Delete job from the line printer spooling queue.
  841. You are prompted for the job number (use a number shown by a previous
  842. \\[tex-show-print-queue] command)."
  843. (interactive "nPrinter job number for deletion: ")
  844. (require 'tex-mode)
  845. (if (tex-shell-running)
  846. (tex-kill-job)
  847. (tex-start-shell))
  848. (tex-send-command texinfo-delete-from-print-queue-command job-number)
  849. ;; alternatively
  850. ;; (send-string "tex-shell"
  851. ;; (concat
  852. ;; texinfo-delete-from-print-queue-command
  853. ;; " "
  854. ;; job-number"\n"))
  855. (tex-recenter-output-buffer nil))
  856. (provide 'texinfo)
  857. ;;; texinfo.el ends here