home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / packages / texinfo.el < prev    next >
Encoding:
Text File  |  1992-06-29  |  15.5 KB  |  410 lines

  1. ; Major mode for editing texinfo files.
  2. ;; Copyright (C) 1985, 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 1, or (at your option)
  9. ;; any later version.
  10.  
  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.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. (require 'texnfo-upd)
  21. (require 'tex-mode)
  22.  
  23. (defvar texinfo-mode-syntax-table nil)
  24.  
  25. (if texinfo-mode-syntax-table
  26.     nil
  27.   (setq texinfo-mode-syntax-table (make-syntax-table))
  28.   (modify-syntax-entry ?\" " " texinfo-mode-syntax-table)
  29.   (modify-syntax-entry ?\\ " " texinfo-mode-syntax-table)
  30.   (modify-syntax-entry ?@ "\\" texinfo-mode-syntax-table)
  31.   (modify-syntax-entry ?\^q "\\" texinfo-mode-syntax-table)
  32.   (modify-syntax-entry ?\[ "(]" texinfo-mode-syntax-table)
  33.   (modify-syntax-entry ?\] ")[" texinfo-mode-syntax-table)
  34.   (modify-syntax-entry ?{ "(}" texinfo-mode-syntax-table)
  35.   (modify-syntax-entry ?} "){" texinfo-mode-syntax-table)
  36.   (modify-syntax-entry ?\' "w" texinfo-mode-syntax-table))
  37.  
  38. (defvar texinfo-mode-map nil)
  39.  
  40. ;; Mode documentation displays commands in reverse order 
  41. ;; from how they are listed in the texinfo-mode-map.
  42. (if texinfo-mode-map
  43.     nil
  44.   (setq texinfo-mode-map (make-sparse-keymap))
  45.  
  46.   (define-key texinfo-mode-map "\C-c\C-k"    'tex-kill-job)
  47.   (define-key texinfo-mode-map "\C-c\C-l"    'tex-recenter-output-buffer)
  48.   (define-key texinfo-mode-map "\C-c\C-q"    'tex-show-print-queue)
  49.   (define-key texinfo-mode-map "\C-c\C-p"    'texinfo-tex-print)
  50.   (define-key texinfo-mode-map "\C-c\C-i"    'texinfo-texindex)
  51.   (define-key texinfo-mode-map "\C-c\C-t"    'texinfo-tex-buffer)
  52.   (define-key texinfo-mode-map "\C-c\C-r"    'texinfo-tex-region)
  53.  
  54.   (define-key texinfo-mode-map "\C-c\C-f"    'texinfo-format-region)
  55.   (define-key texinfo-mode-map "\C-c\C-b"    'texinfo-format-buffer)
  56.  
  57.   (define-key texinfo-mode-map "\C-c\C-um"   'texinfo-make-menu)
  58.   (define-key texinfo-mode-map "\C-c\C-un"   'texinfo-update-node)
  59.   (define-key texinfo-mode-map "\C-c\C-ue"   'texinfo-every-node-update)
  60.   (define-key texinfo-mode-map "\C-c\C-ua"   'texinfo-all-menus-update)
  61.  
  62.   (define-key texinfo-mode-map "\C-c\C-s"     'texinfo-show-structure)
  63.  
  64.   (define-key texinfo-mode-map "\""           'tex-insert-quote)
  65.   (define-key texinfo-mode-map "\e}"          'up-list)
  66.   (define-key texinfo-mode-map "\e{"          'texinfo-insert-braces)
  67.  
  68.   (define-key texinfo-mode-map "\C-c\C-cv"    'texinfo-insert-@var)
  69.   (define-key texinfo-mode-map "\C-c\C-cs"    'texinfo-insert-@samp)
  70.   (define-key texinfo-mode-map "\C-c\C-co"    'texinfo-insert-@noindent)
  71.   (define-key texinfo-mode-map "\C-c\C-cn"    'texinfo-insert-@node)
  72.   (define-key texinfo-mode-map "\C-c\C-ck"    'texinfo-insert-@kbd)
  73.   (define-key texinfo-mode-map "\C-c\C-ci"    'texinfo-insert-@item)
  74.   (define-key texinfo-mode-map "\C-c\C-cx"    'texinfo-insert-@example)
  75.   (define-key texinfo-mode-map "\C-c\C-ce"    'texinfo-insert-@end-example)
  76.   (define-key texinfo-mode-map "\C-c\C-cd"    'texinfo-insert-@dfn)
  77.   (define-key texinfo-mode-map "\C-c\C-cc"    'texinfo-insert-@code))
  78.  
  79. (defun texinfo-insert-@var ()
  80.   "Insert the string @var in a texinfo buffer."
  81.   (interactive)
  82.   (insert "@var{}")
  83.   (backward-char))
  84.  
  85. (defun texinfo-insert-@samp ()
  86.   "Insert the string @samp in a texinfo buffer."
  87.   (interactive)
  88.   (insert "@samp{}")
  89.   (backward-char))
  90.  
  91. (defun texinfo-insert-@noindent ()
  92.   "Insert the string @noindent in a texinfo buffer."
  93.   (interactive)
  94.   (insert "@noindent\n"))
  95.  
  96. (defun texinfo-insert-@node ()
  97.   "Insert the string @node in a texinfo buffer, 
  98. along with a comment indicating the arguments to @node."
  99.   (interactive)
  100.   (insert "@node \n@comment  node-name,  next,  previous,  up")
  101.   (forward-line -1)
  102.   (forward-char 6))
  103.  
  104. (defun texinfo-insert-@kbd ()
  105.   "Insert the string @kbd in a texinfo buffer."
  106.   (interactive)
  107.   (insert "@kbd{}")
  108.   (backward-char))
  109.  
  110. (defun texinfo-insert-@item ()
  111.   "Insert the string @item in a texinfo buffer."
  112.   (interactive)
  113.   (insert "@item")
  114.   (newline))
  115.  
  116. (defun texinfo-insert-@example ()
  117.   "Insert the string @example in a texinfo buffer."
  118.   (interactive)
  119.   (insert "@example\n"))
  120.  
  121. (defun texinfo-insert-@end-example ()
  122.   "Insert the string @end example in a texinfo buffer."
  123.   (interactive)
  124.   (insert "@end example\n"))
  125.  
  126. (defun texinfo-insert-@dfn ()
  127.   "Insert the string @dfn in a texinfo buffer."
  128.   (interactive)
  129.   (insert "@dfn{}")
  130.   (backward-char))
  131.  
  132. (defun texinfo-insert-@code ()
  133.   "Insert the string @code in a texinfo buffer."
  134.   (interactive)
  135.   (insert "@code{}")
  136.   (backward-char))
  137.  
  138. (defun texinfo-insert-braces ()
  139.   "Make a pair of braces and be poised to type inside of them.
  140. Use \\[up-list] to move forward out of the braces."
  141.   (interactive)
  142.   (insert "{}")
  143.   (backward-char))
  144.  
  145. (defun texinfo-mode ()
  146.   "Major mode for editing Texinfo files.
  147.  
  148.   It has these extra commands:
  149. \\{texinfo-mode-map}
  150.  
  151.   These are files that are used as input for TeX to make printed manuals
  152. and also to be turned into Info files by \\[texinfo-format-buffer] or
  153. `makeinfo'.  These files must be written in a very restricted and
  154. modified version of TeX input format.
  155.  
  156.   Editing commands are like text-mode except that the syntax table is
  157. set up so expression commands skip Texinfo bracket groups.  To see
  158. what the Info version of a region of the Texinfo file will look like,
  159. use \\[texinfo-format-region].  This command runs Info on the current region
  160. of the Texinfo file and formats it properly.
  161.  
  162.   You can show the structure of a Texinfo file with \\[texinfo-show-structure].
  163. This command shows the structure of a Texinfo file by listing the
  164. lines with the @-sign commands for @chapter, @section, and the like.
  165. These lines are displayed in another window called the *Occur* window.
  166. In that window, you can position the cursor over one of the lines and
  167. use \\[occur-mode-goto-occurrence], to jump to the corresponding spot
  168. in the Texinfo file.
  169.  
  170.   In addition, Texinfo mode provides commands that insert various
  171. frequently used @-sign commands into the buffer.  You can use these
  172. commands to save keystrokes.  And you can insert balanced braces with
  173. \\[texinfo-insert-braces] and later use the command \\[up-list] to
  174. move forward past the closing brace.
  175.  
  176. Also, Texinfo mode provides functions for automatically creating or
  177. updating menus and node pointers.  These functions
  178.  
  179.   * insert the `Next', `Previous' and `Up' pointers of a node,
  180.   * insert or update the menu for a section, and
  181.   * create a master menu for a Texinfo source file.
  182.  
  183. Here are the functions:
  184.  
  185.     texinfo-update-node                \\[texinfo-update-node]
  186.     texinfo-every-node-update          \\[texinfo-every-node-update]
  187.     texinfo-sequential-node-update 
  188.  
  189.     texinfo-make-menu                  \\[texinfo-make-menu]
  190.     texinfo-all-menus-update           \\[texinfo-all-menus-update]
  191.     texinfo-master-menu
  192.  
  193.     texinfo-indent-menu-description (column &optional region-p)
  194.  
  195. The `texinfo-column-for-description' variable specifies the column to
  196. which menu descriptions are indented. 
  197.  
  198. Passed an argument (a prefix argument, if interactive), the
  199. `texinfo-update-node' and `texinfo-make-menu' functions do their jobs
  200. in the region.
  201.  
  202. To use the updating commands, you must structure your Texinfo file
  203. hierarchically, such that each `@node' line, with the exception of the
  204. Top node, is accompanied by some kind of section line, such as an
  205. `@chapter' or `@section' line.
  206.  
  207. If the file has a `top' node, it must be called `top' or `Top' and
  208. be the first node in the file.
  209.  
  210. Entering Texinfo mode calls the value of text-mode-hook, and then the
  211. value of texinfo-mode-hook."
  212.   (interactive)
  213.   (text-mode)
  214.   (setq mode-name "texinfo")
  215.   (setq major-mode 'texinfo-mode)
  216.   (use-local-map texinfo-mode-map)
  217.   (set-syntax-table texinfo-mode-syntax-table)
  218.   (make-local-variable 'require-final-newline)
  219.   (setq require-final-newline t)
  220.   (make-local-variable 'paragraph-separate)
  221.   (setq paragraph-separate (concat "^\b\\|^@[a-zA-Z]*[ \n]\\|" paragraph-separate))
  222.   (make-local-variable 'paragraph-start)
  223.   (setq paragraph-start (concat "^\b\\|^@[a-zA-Z]*[ \n]\\|" paragraph-start))
  224.   (make-local-variable 'fill-column)
  225.   (setq fill-column 72)
  226.   (make-local-variable 'comment-start)
  227.   (setq comment-start "@c ")
  228.   (make-local-variable 'comment-start-skip)
  229.   (setq comment-start-skip "@c +")
  230.   (make-local-variable 'words-include-escapes)
  231.   (setq words-include-escapes t)
  232.   (make-local-variable 'tex-start-of-header)
  233.   (setq tex-start-of-header "%**start of header")
  234.   (make-local-variable 'tex-end-of-header)
  235.   (setq tex-end-of-header "%**end of header")
  236.   (make-local-variable 'tex-trailer)
  237.   (setq tex-trailer "@bye\n")
  238.   (run-hooks 'text-mode-hook 'texinfo-mode-hook))
  239.  
  240.  
  241. ;;; Texinfo file structure
  242.  
  243. ; The following is defined in `texnfo-upd.el'
  244. ; (defvar texinfo-section-types-regexp
  245. ;   "^@\\(chapter \\|sect\\|sub\\|unnum\\|major\\|heading \\|appendix\\)"
  246. ;   "Regexp matching chapter, section, other headings (but not the top node).")
  247.  
  248. (defun texinfo-show-structure (&optional nodes-too) 
  249.   "Show the structure of a Texinfo file.
  250. List the lines in the file that begin with the @-sign commands for
  251. @chapter, @section, and the like.
  252.  
  253. With optional argument (prefix if interactive), list both the lines
  254. with @-sign commands for @chapter, @section, and the like, and list
  255. @node lines.
  256.  
  257. Lines with structuring commands beginning in them are displayed in
  258. another window called the *Occur* window.  In that window, you can
  259. position the cursor over one of the lines and use
  260. \\[occur-mode-goto-occurrence], 
  261. to jump to the corresponding spot in the Texinfo file."
  262.  
  263.   (interactive "P")
  264.   (save-excursion 
  265.     (goto-char (point-min))
  266.     (if nodes-too
  267.         (occur (concat "\\(^@node\\)\\|" texinfo-section-types-regexp))
  268.       (occur texinfo-section-types-regexp)))
  269.   (pop-to-buffer "*Occur*")
  270.   (goto-char (point-min))
  271.   (flush-lines "-----"))
  272.  
  273.  
  274. ;;; texinfo mode tex and hardcopy printing commands.
  275.  
  276. ;; These commands are for running tex on a region of a texinfo file in
  277. ;; GNU Emacs, or on the whole buffer, and for printing the resulting
  278. ;; .dvi file.  The three commands are:
  279.  
  280. ; texinfo-tex-region    to run tex on the current region.
  281. ; texinfo-tex-buffer    to run tex on the current buffer.
  282. ; texinfo-tex-print     to print the .dvi file made by tex
  283.  
  284. ;;; Other useful functions
  285.  
  286. ; These functions are provided by `tex-mode.el' but are bound to keys
  287. ; in texinfo mode.
  288.  
  289. ; tex-kill-job                  to kill the currently running tex job
  290. ; tex-recenter-output-buffer    to redisplay tex job output buffer
  291. ; tex-show-print-queue          to show the print queue
  292.  
  293. ; Various variables are provided by `tex-mode.el'
  294.  
  295. ; tex mode variable         Default Value 
  296.  
  297. ; tex-dvi-print-command         "lpr -d"
  298. ; tex-directory                 "/tmp/"   
  299. ; tex-show-queue-command        "lpq"     
  300. ; tex-shell-cd-command          "cd"      
  301. ; tex-zap-file                  nil   (created  as needed)
  302.  
  303.  
  304. ;;; The  tex  and  print  function definitions:
  305.  
  306. (defvar texinfo-tex-command "tex"
  307.   "*Command used by  texinfo-tex-region  to run tex on a region.")
  308.  
  309. (defvar texinfo-texindex-command "texindex"
  310.   "*Command used by  texinfo-texindex  to sort unsorted index files.")
  311.  
  312. (defun texinfo-tex-region (beg end)
  313.   "Run tex on the current region.  A temporary file (tex-zap-file) is
  314. written in directory tex-directory, and tex is run in that directory.
  315. The first line of the file is copied to the temporary file; and
  316. if the buffer has a header, it is written to the temporary file before
  317. the region itself.  The buffer's header is all lines between the
  318. strings defined by tex-start-of-header and tex-end-of-header
  319. inclusive.  The header must start in the first 100 lines.  The value
  320. of tex-trailer is appended to the temporary file after the region."
  321.   (interactive "r")
  322.   (if (get-buffer "*tex-shell*")
  323.       (tex-kill-job)
  324.     (tex-start-shell))
  325.   (or tex-zap-file (setq tex-zap-file (make-temp-name "#tz")))
  326.   (let ((tex-out-file (concat tex-zap-file ".tex"))
  327.     (temp-buffer (get-buffer-create " tex-Output-Buffer"))
  328.     (zap-directory
  329.          (file-name-as-directory (expand-file-name tex-directory))))
  330.     (save-excursion
  331.       (save-restriction
  332.     (widen)
  333.     (goto-char (point-min))
  334.     (forward-line 100)
  335.     (let ((search-end (point))
  336.           (hbeg (point-min)) (hend (point-min))
  337.           (default-directory zap-directory))
  338.       (goto-char (point-min))
  339.           
  340.           ;; Copy first line, the `\input texinfo' line, to temp file
  341.       (write-region (point) 
  342.                         (save-excursion (end-of-line) (point))
  343.                         tex-out-file nil nil)
  344.           
  345.           ;; Don't copy first line twice if region includes it.
  346.           (forward-line 1)
  347.           (if (< beg (point)) (setq beg (point)))
  348.           
  349.           ;; Initialize the temp file with either the header or nothing
  350.           (if (search-forward tex-start-of-header search-end t)
  351.               (progn
  352.                 (beginning-of-line)
  353.                 (setq hbeg (point))    ; Mark beginning of header.
  354.                 (if (search-forward tex-end-of-header nil t)
  355.                     (progn (beginning-of-line)
  356.                            (setq hend (point)))    ; Mark end of header.
  357.                   (setq hbeg (point-min))))) ; Else no header.
  358.           
  359.           ;; Copy  header  to temp file.
  360.           (write-region (min hbeg beg) hend tex-out-file t nil)
  361.           
  362.           ;; Copy  region  to temp file.
  363.           (write-region (max beg hend) end tex-out-file t nil))
  364.         
  365.         ;; This is  a kludge to insert the tex-trailer into the tex-out-file.
  366.         ;;  We have to create a special buffer in which to insert
  367.         ;;  the tex-trailer first because there is no function with
  368.         ;;  which to append a literal string directly to a file.
  369.         (let ((local-tex-trailer tex-trailer))
  370.           (set-buffer temp-buffer)
  371.           (erase-buffer)
  372.           ;; make sure trailer isn't hidden by a comment
  373.           (insert-string "\n")
  374.           (if local-tex-trailer (insert-string local-tex-trailer))
  375.           (set-buffer-directory temp-buffer zap-directory)
  376.           (write-region (point-min) (point-max) tex-out-file t nil))))
  377.     
  378.     (set-buffer-directory "*tex-shell*" zap-directory)
  379.     (send-string "tex-shell" (concat tex-shell-cd-command " "
  380.                                      zap-directory "\n"))
  381.     (send-string "tex-shell" (concat texinfo-tex-command " "
  382.                                      tex-out-file "\n")))
  383.   (tex-recenter-output-buffer 0))
  384.  
  385. (defun texinfo-tex-buffer ()
  386.   "Run tex on current buffer.  
  387. See \\[texinfo-tex-region] for more information."
  388.   (interactive)
  389.   (texinfo-tex-region (point-min) (point-max)))
  390.  
  391. (defun texinfo-texindex ()
  392.   "Run texindex on unsorted index files.
  393. The index files are made by \\[texinfo-tex-region] or \\[texinfo-tex-buffer].
  394. Runs the shell command defined by texinfo-texindex-command."
  395.   (interactive)
  396.   (send-string "tex-shell"
  397.            (concat texinfo-texindex-command
  398.                        " " tex-zap-file ".??" "\n"))
  399.   (tex-recenter-output-buffer nil))
  400.  
  401. (defun texinfo-tex-print ()
  402.   "Print .dvi file made by \\[texinfo-tex-region] or \\[texinfo-tex-buffer].
  403. Runs the shell command defined by tex-dvi-print-command."
  404.   (interactive)
  405.   (send-string "tex-shell"
  406.            (concat tex-dvi-print-command
  407.                        " " tex-zap-file ".dvi" "\n"))
  408.   (tex-recenter-output-buffer nil))
  409.  
  410.