home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / packages / LaTeXinfo.shar.10 / latexinfo.el
Encoding:
Text File  |  1990-07-22  |  49.1 KB  |  1,439 lines

  1. ;; Convert latexinfo files to info files.
  2. ;; Copyright (C) 1985, 1986, 1988 Free Software Foundation, Inc.
  3.  
  4. ;; This file is not yet a part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is distributed in the hope that it will be useful,
  7. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  8. ;; accepts responsibility to anyone for the consequences of using it
  9. ;; or for whether it serves any particular purpose or works at all,
  10. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  11. ;; License for full details.
  12.  
  13. ;; Everyone is granted permission to copy, modify and redistribute
  14. ;; GNU Emacs, but only under the conditions described in the
  15. ;; GNU Emacs General Public License.   A copy of this license is
  16. ;; supposed to have been given to you along with GNU Emacs so you
  17. ;; can know your rights and responsibilities.  It should be in a
  18. ;; file named COPYING.  Among other things, the copyright notice
  19. ;; and this notice must be preserved on all copies.
  20.  
  21.  
  22. (defvar latexinfo-formats-directory
  23.   (file-name-as-directory (getenv "LATEXINFO")))
  24.  
  25. (if (not (memq latexinfo-formats-directory
  26.            load-path))
  27.     (setq load-path (cons
  28.              latexinfo-formats-directory
  29.              load-path)))
  30.  
  31. (defvar latexinfo-known-document-styles
  32.       '(latexinfo 11pt 12pt twoside titlepage psfonts smallverb format))
  33.  
  34. (autoload 'latexinfo-mode "latexinfo-mode"
  35.         "Major mode for editing latexinfo files." t)
  36.  
  37. (autoload 'Info-tagify "informat" "Info Tagify" t)
  38. (autoload 'Info-split "informat" "Info Split" t)
  39.  
  40. (put 'latexinfoversion 'latexinfo-format 'latexinfo-format-latexinfoversion)
  41. (defun latexinfo-format-latexinfoversion ()
  42.   (latexinfo-parse-noarg)
  43.   (insert "1.2"))
  44.  
  45. (defvar latexinfo-format-syntax-table nil)
  46.  
  47. (defvar latexinfo-vindex)
  48. (defvar latexinfo-findex)
  49. (defvar latexinfo-cindex)
  50. (defvar latexinfo-cpsubindex)
  51. (defvar latexinfo-pindex)
  52. (defvar latexinfo-tindex)
  53. (defvar latexinfo-kindex)
  54. (defvar latexinfo-last-node)
  55. (defvar latexinfo-node-names)
  56.  
  57. (if latexinfo-format-syntax-table
  58.     nil
  59.   (setq latexinfo-format-syntax-table (make-syntax-table))
  60.   (modify-syntax-entry ?\" " " latexinfo-format-syntax-table)
  61.   (modify-syntax-entry ?* "w" latexinfo-format-syntax-table)
  62.   (modify-syntax-entry ?\\ "\\" latexinfo-format-syntax-table)
  63.   (modify-syntax-entry ?\[ "(]" latexinfo-format-syntax-table)
  64.   (modify-syntax-entry ?\] ")[" latexinfo-format-syntax-table)
  65.   (modify-syntax-entry ?\( "." latexinfo-format-syntax-table)
  66.   (modify-syntax-entry ?\) "." latexinfo-format-syntax-table)
  67.   (modify-syntax-entry ?{ "(}" latexinfo-format-syntax-table)
  68.   (modify-syntax-entry ?} "){" latexinfo-format-syntax-table)
  69.   (modify-syntax-entry ?\' "." latexinfo-format-syntax-table))
  70.  
  71. (defun latexinfo-format-buffer (&optional notagify)
  72.   "Process the current buffer as latexinfo code, into an Info file.
  73. The Info file output is generated in a buffer visiting the Info file
  74. names specified in the \\setfilename command.
  75.  
  76. Non-nil argument (prefix, if interactive) means don't make tag table
  77. and don't split the file if large.  You can use Info-tagify and
  78. Info-split to do these manually."
  79.   (interactive "P")
  80.   (let ((lastmessage "Formatting Info file..."))
  81.     (message lastmessage)
  82.     (latexinfo-format-buffer-1)
  83.     (if notagify
  84.     nil
  85.       (progn
  86.     (if (> (buffer-size) 30000)
  87.         (progn
  88.           (message (setq lastmessage "Making tags table for Info file..."))
  89.           (Info-tagify)))
  90.     (if (> (buffer-size) 100000)
  91.         (progn
  92.           (message (setq lastmessage "Splitting Info file..."))
  93.           (Info-split)))))
  94.     (message (concat lastmessage
  95.              (if (interactive-p) "done.  Now save it." "done.")))))
  96.  
  97. (defun latexinfo-format-buffer-1 ()
  98.   (let (latexinfo-format-filename
  99.     latexinfo-example-start
  100.     latexinfo-command-start
  101.     latexinfo-command-end
  102.     latexinfo-command-name
  103.     latexinfo-last-node
  104.     latexinfo-vindex
  105.     latexinfo-findex
  106.     latexinfo-cindex
  107.     latexinfo-cpsubindex
  108.     latexinfo-pindex
  109.     latexinfo-tindex
  110.     latexinfo-kindex
  111.     latexinfo-stack
  112.     latexinfo-node-names
  113.     outfile
  114.     (fill-column fill-column)
  115.     (input-buffer (current-buffer))
  116.     (input-directory default-directory))
  117.     (save-excursion
  118.       (goto-char (point-min))
  119.       (search-forward "\\setfilename")
  120.       (setq latexinfo-command-end (point))
  121.       (setq outfile (latexinfo-parse-line-arg)))
  122.     (find-file outfile)
  123.     (latexinfo-mode)
  124.     (set-syntax-table latexinfo-format-syntax-table)
  125.     (erase-buffer)
  126.     (insert-buffer-substring input-buffer)
  127.     (goto-char (point-min))
  128.     (latexinfo-run-documentstyle-hooks)
  129.     ;; Run this after latexinfo-run-documentstyle-hooks
  130.     (goto-char (point-min))
  131.     (search-forward "\\setfilename")
  132.     (beginning-of-line)
  133.     (delete-region (point-min) (point))
  134.     ;; Remove \end{document} at end of file, if it is there.
  135.     (goto-char (point-max))
  136.     (if (search-backward "\\end{document}" nil t)
  137.     (delete-region (point) (point-max))
  138.       (error "Missing \\end{document}"))
  139.     ;; Make sure buffer ends in a newline.
  140.     (or (= (preceding-char) ?\n)
  141.     (insert "\n"))
  142.     ;; Scan the whole buffer, converting to Info format.
  143.     (goto-char (point-min))
  144.     (latexinfo-format-scan-noverbatim)
  145.     ;; Return data for indices.
  146.     (goto-char (point-min))
  147.     (list outfile
  148.       latexinfo-vindex latexinfo-findex latexinfo-cindex
  149.       latexinfo-pindex latexinfo-tindex latexinfo-kindex)))
  150.  
  151. (defvar latexinfo-region-buffer-name "*Info Region*"
  152.   "*Name of the temporary buffer used by \\[latexinfo-format-region].")
  153.  
  154. (defun latexinfo-format-region (region-beginning region-ending)
  155.   "Convert the the current region of the Latexinfo file to Info format.
  156. This lets you see what that part of the file will look like in Info.
  157. The command is bound to \\[latexinfo-format-region].  The text that is
  158. converted to Info is stored in a temporary buffer."
  159.   (interactive "r")
  160.   (message "Converting region to Info format...")
  161.   (let (latexinfo-command-start
  162.     latexinfo-command-end
  163.     latexinfo-command-name
  164.     latexinfo-vindex
  165.     latexinfo-findex
  166.     latexinfo-cindex
  167.     latexinfo-pindex
  168.     latexinfo-tindex
  169.     latexinfo-kindex
  170.     latexinfo-stack
  171.     latexinfo-format-filename
  172.     latexinfo-example-start
  173.     latexinfo-last-node
  174.     latexinfo-node-names
  175.     (fill-column fill-column)
  176.     (input-buffer (current-buffer))
  177.     (input-directory default-directory)
  178.     filename-beginning
  179.     filename-ending)
  180.  
  181. ;;; Find a buffer to use.
  182.  
  183.     (switch-to-buffer (get-buffer-create latexinfo-region-buffer-name))
  184.  
  185.     ;; Insert the region into the buffer.
  186.     (erase-buffer)
  187.  
  188.     (save-excursion
  189.       (set-buffer input-buffer)
  190.       (save-excursion
  191.     (save-restriction
  192.       (widen)
  193.       (goto-char (point-min))
  194.       ;; Initialize the buffer with the filename
  195.       ;; or else explain that a filename is needed.
  196.       (or (search-forward "\\setfilename"
  197.                   (save-excursion (forward-line 100) (point)) t)
  198.           (error "The latexinfo file needs a line saying: \\setfilename <name>"))
  199.       (beginning-of-line)
  200.       (setq filename-beginning (point))
  201.       (forward-line 1)
  202.       (setq filename-ending (point)))))
  203.  
  204.     ;; Insert the \\setfilename line into the buffer.
  205.     (insert-buffer-substring input-buffer
  206.                  (min filename-beginning region-beginning)  
  207.                  filename-ending)
  208.     
  209.     ;; Insert the region into the buffer.
  210.     (insert-buffer-substring input-buffer
  211.                  (max region-beginning filename-ending)
  212.                  region-ending)
  213.  
  214.     (latexinfo-mode)
  215.  
  216.     ;; Install a syntax table useful for scanning command operands.
  217.     (set-syntax-table latexinfo-format-syntax-table)
  218.     
  219.     ;; If the region includes the effective end of the data,
  220.     ;; discard everything after that.
  221.     (goto-char (point-max))
  222.     (if (search-backward "\\end{document}" nil t)
  223.     (delete-region (point) (point-max)))
  224.     ;; Make sure buffer ends in a newline.
  225.     (or (= (preceding-char) ?\n)
  226.     (insert "\n"))
  227.  
  228.     (goto-char (point-max))
  229.     ;; Now convert for real.
  230.     (goto-char (point-min))
  231.     (latexinfo-format-scan-noverbatim)
  232.     (goto-char (point-min)))
  233.  
  234.   (message "Done."))
  235.  
  236. (defun latexinfo-format-scan-noverbatim ()
  237.   (if (re-search-forward "^\\\\begin{verbatim}\\|\\\\input{" nil t)
  238.       (let ((start (point-min))
  239.         end
  240.         str)
  241.     (goto-char start)
  242.     (while (re-search-forward "^\\\\begin{verbatim}\\|\\\\input{" nil t)
  243.       (setq str (buffer-substring (match-beginning 0) (match-end 0)))
  244.       ;; Handle LaTeX \input{filename} commands by inserting them now.
  245.       ;; Only look at the beginning of the line to avoid \c \input{foo}
  246.       (cond ((string-equal str "\\input{")
  247.          (save-excursion
  248.            (skip-chars-forward "     {")
  249.            (let ((file-name
  250.               (buffer-substring 
  251.                (point)
  252.                (progn
  253.                  (skip-chars-forward "^     }\n")
  254.                  (point)))))
  255.              (setq file-name
  256.                (expand-file-name
  257.                 (if (file-readable-p (expand-file-name file-name))
  258.                 file-name
  259.                   (concat file-name ".tex"))))
  260.              (beginning-of-line 1)
  261.              (delete-region (point) (progn (forward-line 1) (point)))
  262.              (message "Inserting file %s..." file-name) (sit-for 1)
  263.              (insert-file file-name)
  264.              (message "Inserting file %s...done" file-name)
  265.              )))
  266.         ((string-equal str "\\begin{verbatim}")
  267.          (delete-region (point) (progn (beginning-of-line 1) (point)))
  268. ;;         (message "texinfo-format-scan-noverbatim %d" (point))
  269. ;;         (sit-for 1)
  270.          (latexinfo-format-expand-region start (point))
  271.          (setq start
  272.                (progn
  273.              (goto-char start)
  274.              (if (re-search-forward "^\\\\end{verbatim}" nil t)
  275.                  (progn
  276.                    (delete-region (point)
  277.                           (progn (beginning-of-line 1)
  278.                              (forward-char -1)
  279.                              (point)))
  280.                    (point))
  281.                (error "Missing \\end{verbatim}"))))
  282.          )))
  283.     (latexinfo-format-expand-region start (point-max))
  284.     )
  285.     (latexinfo-format-scan)))
  286.  
  287. (put 'verbatim 'latexinfo-format 'latexinfo-format-verbatim)
  288. (defun latexinfo-format-verbatim ()
  289.   (error "Nested verbatim environments are not supported"))
  290.  
  291. (defun latexinfo-format-scan ()
  292.   ;; LaTeX sometimes uses \\ to force a new-line
  293.   (goto-char (point-min))
  294.   (replace-regexp "\\\\\\\\$" "")
  295.   ;; Convert left and right quotes to typewriter font quotes.
  296.   (goto-char (point-min))
  297.   (while (search-forward "``" nil t)
  298.     (replace-match "\""))
  299.   (goto-char (point-min))
  300.   (while (search-forward "''" nil t)
  301.     (replace-match "\""))
  302.   ;; Scan for \\-commands.
  303.   (goto-char (point-min))
  304.   (while (search-forward "\\" nil t)
  305.     (if (looking-at "[@{}'` *%]")
  306.     ;; Handle a few special \-followed-by-one-char commands.
  307.     (if (= (following-char) ?*)
  308.         ;; \* has no effect, since we are not filling.
  309.         (delete-region (1- (point)) (1+ (point)))
  310.       ;; The other characters are simply quoted.  Delete the \.
  311.       (delete-char -1)
  312.       (forward-char 1))
  313.       ;; \ is followed by a command-word; find the end of the word.
  314.       (setq latexinfo-command-start (1- (point)))
  315.       (if (= (char-syntax (following-char)) ?w)
  316.       (forward-word 1)
  317.     (forward-char 1))
  318.       (setq latexinfo-command-end (point))
  319.       ;; Call the handler for this command.
  320.       (setq latexinfo-command-name
  321.         (intern (buffer-substring (1+ latexinfo-command-start)
  322.                       latexinfo-command-end)))
  323.       (let ((cmd (get latexinfo-command-name 'latexinfo-format)))
  324.     (if cmd (funcall cmd)
  325.       (latexinfo-unsupported)))))
  326.   (cond (latexinfo-stack
  327.      (goto-char (nth 2 (car latexinfo-stack)))
  328.      (error "Unterminated \begin{%s}" (car (car latexinfo-stack))))))
  329.  
  330. (put 'begin 'latexinfo-format 'latexinfo-format-begin)
  331. (defun latexinfo-format-begin ()
  332.   (latexinfo-format-begin-end 'latexinfo-format))
  333.  
  334. (put 'end 'latexinfo-format 'latexinfo-format-end)
  335. (defun latexinfo-format-end ()
  336.   (latexinfo-format-begin-end 'latexinfo-end))
  337.  
  338. (defun latexinfo-format-begin-end (prop)
  339.   (setq latexinfo-command-name (intern (latexinfo-parse-line-arg)))
  340.   (setq cmd (get latexinfo-command-name prop))
  341.   (if cmd (funcall cmd)
  342.     (latexinfo-unsupported)))
  343.  
  344. (defun latexinfo-parse-line-arg ()
  345.   (goto-char latexinfo-command-end)
  346.   (let ((start (point)))
  347.     (cond ((looking-at " ")
  348.        (skip-chars-forward " ")
  349.        (setq start (point))
  350.        (end-of-line)
  351.        (setq latexinfo-command-end (1+ (point))))
  352.       ((looking-at "[{\\[]")
  353.        (setq start (1+ (point)))
  354.        (forward-list 1)
  355.        (setq latexinfo-command-end (point))
  356.        (forward-char -1))
  357.       (t
  358.        (error "Invalid latexinfo command arg format")))
  359.     (prog1 (buffer-substring start (point))
  360.        (if (eolp) (forward-char 1)))))
  361.  
  362. (defun latexinfo-parse-expanded-arg ()
  363.   (goto-char latexinfo-command-end)
  364.   (let ((start (point))
  365.     marker)
  366.     (cond ((looking-at " ")
  367.        (skip-chars-forward " ")
  368.        (setq start (point))
  369.        (end-of-line)
  370.        (setq latexinfo-command-end (1+ (point))))
  371.       ((looking-at "[{\\[]")
  372.        (setq start (1+ (point)))
  373.        (forward-list 1)
  374.        (setq latexinfo-command-end (point))
  375.        (forward-char -1))
  376.       (t
  377.        (error "Invalid latexinfo command arg format")))
  378.     (setq marker (move-marker (make-marker) latexinfo-command-end))
  379.     (latexinfo-format-expand-region start (point))
  380.     (setq latexinfo-command-end (marker-position marker))
  381.     (move-marker marker nil)
  382.     (prog1 (buffer-substring start (point))
  383.        (if (eolp) (forward-char 1)))))
  384.  
  385. (defun latexinfo-format-expand-region (start end)
  386.   (save-restriction
  387.     (narrow-to-region start end)
  388.     (let (latexinfo-command-start
  389.       latexinfo-command-end
  390.       latexinfo-command-name
  391.       latexinfo-stack)
  392.       (latexinfo-format-scan))
  393.     (goto-char (point-max))))
  394.  
  395. (defun latexinfo-parse-arg-discard ()
  396.   (prog1 (latexinfo-parse-line-arg)
  397.      (latexinfo-discard-command)))
  398.  
  399. (defun latexinfo-discard-command ()
  400.   (delete-region latexinfo-command-start latexinfo-command-end))
  401.  
  402. (defun latexinfo-format-parse-line-args ()
  403.   (let ((start (1- (point)))
  404.     next beg end
  405.     args)
  406.     (skip-chars-forward " ")
  407.     (while (not (eolp))
  408.       (setq beg (point))
  409.       (re-search-forward "[\n,]")
  410.       (setq next (point))
  411.       (if (bolp) (setq next (1- next)))
  412.       (forward-char -1)
  413.       (skip-chars-backward " ")
  414.       (setq end (point))
  415.       (setq args (cons (if (> end beg) (buffer-substring beg end))
  416.                args))
  417.       (goto-char next)
  418.       (skip-chars-forward " "))
  419.     (if (eolp) (forward-char 1))
  420.     (setq latexinfo-command-end (point))
  421.     (nreverse args)))
  422.  
  423. (defun latexinfo-format-parse-args ()
  424.   (let ((start (1- (point)))
  425.     next beg end
  426.     args)
  427.     (search-forward "{")
  428.     (while (/= (preceding-char) ?\})
  429.       (skip-chars-forward " \t\n")
  430.       (setq beg (point))
  431.       (re-search-forward "[},]")
  432.       (setq next (point))
  433.       (forward-char -1)
  434.       (skip-chars-backward " \t\n")
  435.       (setq end (point))
  436.       (cond ((< beg end)
  437.          (goto-char beg)
  438.          (while (search-forward "\n" end t)
  439.            (replace-match " "))))
  440.       (setq args (cons (if (> end beg) (buffer-substring beg end))
  441.                args))
  442.       (goto-char next))
  443.     (if (eolp) (forward-char 1))
  444.     (setq latexinfo-command-end (point))
  445.     (nreverse args)))
  446.  
  447. (defun latexinfo-format-parse-defun-args ()
  448.   (goto-char latexinfo-command-end)
  449.   (let ((start (point)))
  450.     (end-of-line)
  451.     (setq latexinfo-command-end (1+ (point)))
  452.     (let ((marker (move-marker (make-marker) latexinfo-command-end)))
  453.       (latexinfo-format-expand-region start (point))
  454.       (setq latexinfo-command-end (marker-position marker))
  455.       (move-marker marker nil))
  456.     (goto-char start)
  457.     (let ((args '())
  458.       beg end)
  459.       (skip-chars-forward " ")
  460.       (while (not (eolp))
  461.     (cond ((looking-at "[{\\[]")
  462.            (setq beg (1+ (point)))
  463.            (forward-list 1)
  464.            (setq end (1- (point))))
  465.           (t
  466.            (setq beg (point))
  467.            (re-search-forward "[\n ]")
  468.            (forward-char -1)
  469.            (setq end (point))))
  470.     (setq args (cons (buffer-substring beg end) args))
  471.     (skip-chars-forward " "))
  472.       (forward-char 1)
  473.       (nreverse args))))
  474.  
  475. (put 'setfilename 'latexinfo-format 'latexinfo-format-setfilename)
  476. (defun latexinfo-format-setfilename ()
  477.   (let ((arg (latexinfo-parse-arg-discard)))
  478.     (setq latexinfo-format-filename
  479.       (file-name-nondirectory (expand-file-name arg)))
  480.     (insert "Info file: "
  481.         latexinfo-format-filename ",    -*-Text-*-\n"
  482.         "produced by latexinfo-format-buffer\nfrom "
  483.         (if (buffer-file-name input-buffer)
  484.         (concat "file: "
  485.             (file-name-sans-versions
  486.              (file-name-nondirectory
  487.               (buffer-file-name input-buffer))))
  488.           (concat "buffer " (buffer-name input-buffer)))
  489.         "\n\n")))
  490.  
  491. (put 'node 'latexinfo-format 'latexinfo-format-node)
  492. (defun latexinfo-format-node ()
  493.   (let* ((args (latexinfo-format-parse-line-args))
  494.      (name (nth 0 args))
  495.      (next (nth 1 args))
  496.      (prev (nth 2 args))
  497.      (up (nth 3 args)))
  498.     (latexinfo-discard-command)
  499.     (setq latexinfo-last-node name)
  500.     (let ((tem (downcase name)))
  501.       (if (assoc tem latexinfo-node-names)
  502.       (error "Duplicate node name: %s" name)
  503.     (setq latexinfo-node-names (cons tem latexinfo-node-names))))
  504.     (or (bolp)
  505.     (insert ?\n))
  506.     (insert "\^_\nFile: " latexinfo-format-filename
  507.         "  Node: " name)
  508.     (if prev
  509.     (insert ", Prev: " prev))
  510.     (if up
  511.     (insert ", Up: " up))
  512.     (if next
  513.     (insert ", Next: " next))
  514.     (insert ?\n)))
  515.  
  516. (put 'menu 'latexinfo-format 'latexinfo-format-menu)
  517. (defun latexinfo-format-menu ()
  518.   (latexinfo-discard-line)
  519.   (insert "* Menu:\n\n"))
  520.  
  521. (put 'menu 'latexinfo-end 'latexinfo-discard-command)
  522. (defun latexinfo-discard-line ()
  523.   (goto-char latexinfo-command-end)
  524.   (skip-chars-forward " \t")
  525.   (or (eolp)
  526.       (error "Extraneous text at end of command line."))
  527.   (goto-char latexinfo-command-start)
  528.   (or (bolp)
  529.       (error "Extraneous text at beginning of command line."))
  530.   (delete-region (point) (progn (forward-line 1) (point))))
  531.  
  532. ; \xref {NODE, FNAME, NAME, FILE, DOCUMENT}
  533. ; -> *Note FNAME: (FILE)NODE
  534. ;   If FILE is missing,
  535. ;    *Note FNAME: NODE
  536. ;   If FNAME is empty and NAME is present
  537. ;    *Note NAME: Node
  538. ;   If both NAME and FNAME are missing
  539. ;    *Note NODE::
  540. ;   latexinfo ignores the DOCUMENT argument.
  541. ; -> See section <xref to NODE> [NAME, else NODE], page <xref to NODE>
  542. ;   If FILE is specified, (FILE)NODE is used for xrefs.
  543. ;   If fifth argument DOCUMENT is specified, produces
  544. ;    See section <xref to NODE> [NAME, else NODE], page <xref to NODE>
  545. ;    of DOCUMENT
  546. (put 'xref 'latexinfo-format 'latexinfo-format-xref)
  547. (defun latexinfo-format-xref ()
  548.   (let ((args (latexinfo-format-parse-args)))
  549.     (latexinfo-discard-command)
  550.     (insert "*Note ")
  551.     (let ((fname (or (nth 1 args) (nth 2 args))))
  552.       (if (null (or fname (nth 3 args)))
  553.       (insert (car args) "::")
  554.     (insert (or fname (car args)) ": ")
  555.     (if (nth 3 args)
  556.         (insert "(" (nth 3 args) ")"))
  557.     (insert (car args))))))
  558.  
  559. (put 'pxref 'latexinfo-format 'latexinfo-format-pxref)
  560. (defun latexinfo-format-pxref ()
  561.   (latexinfo-format-xref)
  562.   (or (save-excursion
  563.     (forward-char -2)
  564.     (looking-at "::"))
  565.       (insert ".")))
  566.  
  567. ;\inforef{NODE, FNAME, FILE}
  568. ;Like \xref{NODE, FNAME,,FILE} in latexinfo.
  569. ;In LaTeX, generates "See Info file FILE, node NODE"
  570. (put 'inforef 'latexinfo-format 'latexinfo-format-inforef)
  571. (defun latexinfo-format-inforef ()
  572.   (let ((args (latexinfo-format-parse-args)))
  573.     (latexinfo-discard-command)
  574.     (insert "*Note " (nth 1 args) ": (" (nth 2 args) ")" (car args))))
  575.  
  576. (put 'appendix 'latexinfo-format 'latexinfo-parse-noarg)
  577.  
  578. (put 'chapter 'latexinfo-format 'latexinfo-format-chapter)
  579. (put 'chapter* 'latexinfo-format 'latexinfo-format-chapter)
  580. (defun latexinfo-format-chapter ()
  581.   (latexinfo-format-chapter-1 ?*))
  582.  
  583. (put 'section 'latexinfo-format 'latexinfo-format-section)
  584. (put 'section* 'latexinfo-format 'latexinfo-format-section)
  585. (defun latexinfo-format-section ()
  586.   (latexinfo-format-chapter-1 ?=))
  587.  
  588. (put 'subsection 'latexinfo-format 'latexinfo-format-subsection)
  589. (put 'subsection* 'latexinfo-format 'latexinfo-format-subsection)
  590. (defun latexinfo-format-subsection ()
  591.   (latexinfo-format-chapter-1 ?-))
  592.  
  593. (put 'subsubsection 'latexinfo-format 'latexinfo-format-subsubsection)
  594. (put 'subsubsection* 'latexinfo-format 'latexinfo-format-subsubsection)
  595. (defun latexinfo-format-subsubsection ()
  596.   (latexinfo-format-chapter-1 ?.))
  597.  
  598. (defun latexinfo-format-chapter-1 (belowchar)
  599.   (let ((arg (latexinfo-parse-arg-discard)))
  600.     (insert ?\n arg ?\n "\\SectionPAD " belowchar ?\n)
  601.     (forward-line -2)))
  602.  
  603. (put 'SectionPAD 'latexinfo-format 'latexinfo-format-sectionpad)
  604. (defun latexinfo-format-sectionpad ()
  605.   (let ((str (latexinfo-parse-arg-discard)))
  606.     (forward-char -1)
  607.     (let ((column (current-column)))
  608.       (forward-char 1)
  609.       (while (> column 0)
  610.     (insert str)
  611.     (setq column (1- column))))
  612.     (insert ?\n)))
  613.  
  614. (put '\. 'latexinfo-format 'latexinfo-format-\.)
  615. (defun latexinfo-format-\. ()
  616.   (latexinfo-discard-command)
  617.   (insert "."))
  618.  
  619. (put '\: 'latexinfo-format 'latexinfo-format-\:)
  620. (defun latexinfo-format-\: ()
  621.   (latexinfo-discard-command))
  622.  
  623.  
  624. ;; \begin{itemize} pushes (itemize "COMMANDS" STARTPOS) on latexinfo-stack.
  625. ;; \begin{enumerate} pushes (enumerate 0 STARTPOS).
  626. ;; \item dispatches to the latexinfo-item prop of the first elt of the list.
  627. ;; For itemize, this puts in and rescans the COMMANDS.
  628. ;; For enumerate, this increments the number and puts it in.
  629. ;; In either case, it puts a Backspace at the front of the line
  630. ;; which marks it not to be indented later.
  631. ;; All other lines get indented by 5 when the \end is reached.
  632.  
  633. (defun latexinfo-push-stack (check arg)
  634.   (setq latexinfo-stack
  635.     (cons (list check arg latexinfo-command-start)
  636.           latexinfo-stack)))
  637.  
  638. (defun latexinfo-pop-stack (check)
  639.   (if (null latexinfo-stack)
  640.       (error "Unmatched \\end{%s}" check))
  641.   (if (not (eq (car (car latexinfo-stack)) check))
  642.       (error "\\end{%s} matches \\begin{%s}"
  643.          check (car (car latexinfo-stack))))
  644.   (prog1 (cdr (car latexinfo-stack))
  645.      (setq latexinfo-stack (cdr latexinfo-stack))))
  646.  
  647. (put 'itemize 'latexinfo-format 'latexinfo-itemize)
  648. (defun latexinfo-itemize ()
  649.   (latexinfo-push-stack 'itemize "*")
  650.   (setq fill-column (- fill-column 5))
  651.   (latexinfo-discard-line))
  652.  
  653. (put 'itemize 'latexinfo-end 'latexinfo-end-itemize)
  654. (defun latexinfo-end-itemize ()
  655.   (setq fill-column (+ fill-column 5))
  656.   (latexinfo-discard-command)
  657.   (let ((stacktop
  658.      (latexinfo-pop-stack 'itemize)))
  659.     (latexinfo-do-itemize (nth 1 stacktop))))
  660.  
  661. (put 'enumerate 'latexinfo-format 'latexinfo-enumerate)
  662. (defun latexinfo-enumerate ()
  663.   (latexinfo-push-stack 'enumerate 0)
  664.   (setq fill-column (- fill-column 5))
  665.   (latexinfo-discard-line))
  666.  
  667. (put 'enumerate 'latexinfo-end 'latexinfo-end-enumerate)
  668. (defun latexinfo-end-enumerate ()
  669.   (setq fill-column (+ fill-column 5))
  670.   (latexinfo-discard-command)
  671.   (let ((stacktop
  672.      (latexinfo-pop-stack 'enumerate)))
  673.     (latexinfo-do-itemize (nth 1 stacktop))))
  674.  
  675. (put 'center 'latexinfo-format 'latexinfo-format-center)
  676. (defun latexinfo-format-center ()
  677.   (latexinfo-push-stack 'center 0)
  678.   (latexinfo-discard-line))
  679.  
  680. (put 'center 'latexinfo-end 'latexinfo-end-center)
  681. (defun latexinfo-end-center ()
  682.   (latexinfo-discard-command)
  683.   (let ((stacktop
  684.      (latexinfo-pop-stack 'center)))
  685.     (latexinfo-do-center (nth 1 stacktop))))
  686.  
  687. (defun latexinfo-do-center (from)
  688.   (let ((indent-tabs-mode nil))
  689.     (while (progn (forward-line -1)
  690.           (>= (point) from))
  691.       (save-excursion
  692.     (center-line)))))
  693.  
  694. (put 'table 'latexinfo-format 'latexinfo-table)
  695. (defun latexinfo-table ()
  696.   (latexinfo-push-stack 'table (latexinfo-parse-arg-discard))
  697.   (setq fill-column (- fill-column 5)))
  698.  
  699. (put 'ftable 'latexinfo-format 'latexinfo-ftable)
  700. (defun latexinfo-ftable ()
  701.   (latexinfo-push-stack 'table "\\code")
  702.   (setq fill-column (- fill-column 5))
  703.   (latexinfo-discard-line))
  704.  
  705. (put 'description 'latexinfo-format 'latexinfo-description)
  706. (defun latexinfo-description ()
  707.   (latexinfo-push-stack 'table "\\asis")
  708.   (setq fill-column (- fill-column 5))
  709.   (latexinfo-discard-line))
  710.  
  711. (put 'table 'latexinfo-end 'latexinfo-end-table)
  712. (put 'ftable 'latexinfo-end 'latexinfo-end-table)
  713. (put 'description 'latexinfo-end 'latexinfo-end-table)
  714. (defun latexinfo-end-table ()
  715.   (setq fill-column (+ fill-column 5))
  716.   (latexinfo-discard-command)
  717.   (let ((stacktop
  718.      (latexinfo-pop-stack 'table)))
  719.     (latexinfo-do-itemize (nth 1 stacktop))))
  720.  
  721. ;; At the \end, indent all the lines within the construct
  722. ;; except those marked with backspace.  FROM says where
  723. ;; construct started.
  724. (defun latexinfo-do-itemize (from)
  725.   (save-excursion
  726.    (while (progn (forward-line -1)
  727.          (>= (point) from))
  728.      (if (= (following-char) ?\b)
  729.      (save-excursion
  730.        (delete-char 1)
  731.        (end-of-line)
  732.        (delete-char 6))
  733.        (save-excursion (insert "     "))))))
  734.  
  735. (put 'item 'latexinfo-format 'latexinfo-item)
  736. (defun latexinfo-item ()
  737.   (funcall (get (car (car latexinfo-stack)) 'latexinfo-item)))
  738.  
  739. (put 'itemize 'latexinfo-item 'latexinfo-itemize-item)
  740. (defun latexinfo-itemize-item ()
  741.   (latexinfo-parse-noarg)
  742.   (insert "\b   " (nth 1 (car latexinfo-stack)) " \n")
  743.   (forward-line -1))
  744.  
  745. (put 'enumerate 'latexinfo-item 'latexinfo-enumerate-item)
  746. (defun latexinfo-enumerate-item ()
  747.   (latexinfo-parse-noarg)
  748.   (let ((next (1+ (car (cdr (car latexinfo-stack))))))
  749.     (setcar (cdr (car latexinfo-stack)) next)
  750.     (insert ?\b (format "%3d. " next) ?\n))
  751.   (forward-line -1))
  752.  
  753. (put 'table 'latexinfo-item 'latexinfo-table-item)
  754. (defun latexinfo-table-item ()
  755.   (let ((arg (latexinfo-parse-arg-discard))
  756.     (itemfont (car (cdr (car latexinfo-stack)))))
  757.     (insert ?\b itemfont ?\{ arg "}\n     \n"))
  758.   (forward-line -2))
  759.  
  760. (put 'ifinfo 'latexinfo-format 'latexinfo-discard-line)
  761. (put 'ifinfo 'latexinfo-end 'latexinfo-discard-command)
  762.  
  763. (put 'iftex 'latexinfo-format 'latexinfo-format-iftex)
  764. (defun latexinfo-format-iftex ()
  765.   (delete-region latexinfo-command-start
  766.          (progn (re-search-forward "\\\\end[ {]iftex[ }\n]")
  767.             (point))))
  768.  
  769. (put 'tex 'latexinfo-format 'latexinfo-format-tex)
  770. (defun latexinfo-format-tex ()
  771.   (delete-region latexinfo-command-start
  772.          (progn (re-search-forward "\\end[ {]tex[     }\n]")
  773.             (point))))
  774.  
  775. (put 'ignore 'latexinfo-format 'latexinfo-format-ignore)
  776. (defun latexinfo-format-ignore ()
  777.   (delete-region latexinfo-command-start
  778.          (progn (re-search-forward "\\\\end[ {]ignore[     }\n]")
  779.             (point))))
  780.  
  781. (put 'endignore 'latexinfo-format 'latexinfo-discard-line)
  782.  
  783. (put 'var 'latexinfo-format 'latexinfo-format-var)
  784. (defun latexinfo-format-var ()
  785.   (insert (upcase (latexinfo-parse-arg-discard)))
  786.   (goto-char latexinfo-command-start))
  787.  
  788. (put 'asis 'latexinfo-format 'latexinfo-format-noop)
  789. (put 'b 'latexinfo-format 'latexinfo-format-noop)
  790. (put 't 'latexinfo-format 'latexinfo-format-noop)
  791. (put 'i 'latexinfo-format 'latexinfo-format-noop)
  792. (put 'key 'latexinfo-format 'latexinfo-format-noop)
  793. (put 'w 'latexinfo-format 'latexinfo-format-noop)
  794. (defun latexinfo-format-noop ()
  795.   (insert (latexinfo-parse-arg-discard))
  796.   (goto-char latexinfo-command-start))
  797.  
  798. (put 'code 'latexinfo-format 'latexinfo-format-code)
  799. (put 'samp 'latexinfo-format 'latexinfo-format-code)
  800. (put 'file 'latexinfo-format 'latexinfo-format-code)
  801. (put 'kbd 'latexinfo-format 'latexinfo-format-code)
  802. (defun latexinfo-format-code ()
  803.   (insert "`" (latexinfo-parse-arg-discard) "'")
  804.   (goto-char latexinfo-command-start))
  805.  
  806. (put 'emph 'latexinfo-format 'latexinfo-format-emph)
  807. (put 'strong 'latexinfo-format 'latexinfo-format-emph)
  808. (defun latexinfo-format-emph ()
  809.   (insert "*" (latexinfo-parse-arg-discard) "*"))
  810.  
  811. (put 'defn 'latexinfo-format 'latexinfo-format-defn)
  812. (put 'dfn 'latexinfo-format 'latexinfo-format-defn)
  813. (defun latexinfo-format-defn ()
  814.   (insert "\"" (latexinfo-parse-arg-discard) "\"")
  815.   (goto-char latexinfo-command-start))
  816.  
  817. (put 'bullet 'latexinfo-format 'latexinfo-format-bullet)
  818. (defun latexinfo-format-bullet ()
  819.   (latexinfo-discard-command)
  820.   (insert "*"))
  821.  
  822. (put 'smallexample 'latexinfo-format 'latexinfo-format-example)
  823. (put 'example 'latexinfo-format 'latexinfo-format-example)
  824. (put 'quotation 'latexinfo-format 'latexinfo-format-example)
  825. (put 'lisp 'latexinfo-format 'latexinfo-format-example)
  826. (put 'display 'latexinfo-format 'latexinfo-format-example)
  827. (put 'format 'latexinfo-format 'latexinfo-format-example)
  828. (put 'flushleft 'latexinfo-format 'latexinfo-format-example)
  829. (defun latexinfo-format-example ()
  830.   (latexinfo-push-stack 'example nil)
  831.   (setq fill-column (- fill-column 5))
  832.   (latexinfo-discard-line))
  833.  
  834. (put 'smallexample 'latexinfo-end 'latexinfo-end-example)
  835. (put 'example 'latexinfo-end 'latexinfo-end-example)
  836. (put 'quotation 'latexinfo-end 'latexinfo-end-example)
  837. (put 'lisp 'latexinfo-end 'latexinfo-end-example)
  838. (put 'display 'latexinfo-end 'latexinfo-end-example)
  839. (put 'format 'latexinfo-end 'latexinfo-end-example)
  840. (put 'flushleft 'latexinfo-end 'latexinfo-end-example)
  841. (defun latexinfo-end-example ()
  842.   (setq fill-column (+ fill-column 5))
  843.   (latexinfo-discard-command)
  844.   (let ((stacktop
  845.      (latexinfo-pop-stack 'example)))
  846.     (latexinfo-do-itemize (nth 1 stacktop))))
  847.  
  848. (put 'exdent 'latexinfo-format 'latexinfo-format-exdent)
  849. (defun latexinfo-format-exdent ()
  850.   (latexinfo-discard-command)
  851.   (delete-region (point)
  852.          (progn
  853.           (skip-chars-forward " ")
  854.           (point)))
  855.   (insert ?\b)
  856.   ;; Cancel out the deletion that latexinfo-do-itemize
  857.   ;; is going to do at the end of this line.
  858.   (save-excursion
  859.     (end-of-line)
  860.     (insert "\n     ")))
  861.  
  862. (put 'ctrl 'latexinfo-format 'latexinfo-format-ctrl)
  863. (defun latexinfo-format-ctrl ()
  864.   (let ((str (latexinfo-parse-arg-discard)))
  865.     (insert "^" str)))
  866.     ;;    (insert (logand 31 (aref str 0)))))
  867.  
  868. (put 'TeX 'latexinfo-format 'latexinfo-format-TeX)
  869. (defun latexinfo-format-TeX ()
  870.   (latexinfo-parse-noarg)
  871.   (insert "TeX"))
  872.  
  873. (put 'copyright 'latexinfo-format 'latexinfo-format-copyright)
  874. (defun latexinfo-format-copyright ()
  875.   (latexinfo-parse-noarg)
  876.   (insert "(C)"))
  877.  
  878. (put 'minus 'latexinfo-format 'latexinfo-format-minus)
  879. (defun latexinfo-format-minus ()
  880.   (latexinfo-parse-arg-discard)
  881.   (insert "-"))
  882.  
  883. (put 'dots 'latexinfo-format 'latexinfo-format-dots)
  884. (defun latexinfo-format-dots ()
  885.   (latexinfo-discard-command)
  886.   (insert "..."))
  887.  
  888. (put 'refill 'latexinfo-format 'latexinfo-format-refill)
  889. (defun latexinfo-format-refill ()
  890.   (latexinfo-discard-command)
  891.   (fill-paragraph nil))
  892.  
  893. ;; Index generation
  894.  
  895. (put 'vindex 'latexinfo-format 'latexinfo-format-vindex)
  896. (defun latexinfo-format-vindex ()
  897.   (latexinfo-index 'latexinfo-vindex))
  898.  
  899. (put 'cindex 'latexinfo-format 'latexinfo-format-cindex)
  900. (defun latexinfo-format-cindex ()
  901.   (latexinfo-index 'latexinfo-cindex))
  902.  
  903. (put 'findex 'latexinfo-format 'latexinfo-format-findex)
  904. (defun latexinfo-format-findex ()
  905.   (latexinfo-index 'latexinfo-findex))
  906.  
  907. (put 'pindex 'latexinfo-format 'latexinfo-format-pindex)
  908. (defun latexinfo-format-pindex ()
  909.   (latexinfo-index 'latexinfo-pindex))
  910.  
  911. (put 'tindex 'latexinfo-format 'latexinfo-format-tindex)
  912. (defun latexinfo-format-tindex ()
  913.   (latexinfo-index 'latexinfo-tindex))
  914.  
  915. (put 'kindex 'latexinfo-format 'latexinfo-format-kindex)
  916. (defun latexinfo-format-kindex ()
  917.   (latexinfo-index 'latexinfo-kindex))
  918.  
  919. (defun latexinfo-index (indexvar)
  920.   (let ((arg (latexinfo-parse-expanded-arg)))
  921.     (latexinfo-discard-command)
  922.     (set indexvar
  923.      (cons (list arg latexinfo-last-node)
  924.            (symbol-value indexvar)))))
  925.  
  926. (defconst latexinfo-indexvar-alist
  927.   '(("cp" . latexinfo-cindex)
  928.     ("fn" . latexinfo-findex)
  929.     ("vr" . latexinfo-vindex)
  930.     ("tp" . latexinfo-tindex)
  931.     ("pg" . latexinfo-pindex)
  932.     ("ky" . latexinfo-kindex)))
  933.  
  934. (put 'printindex 'latexinfo-format 'latexinfo-format-printindex)
  935. (defun latexinfo-format-printindex ()
  936.   (let ((indexelts (symbol-value
  937.             (cdr (assoc (latexinfo-parse-arg-discard)
  938.                 latexinfo-indexvar-alist))))
  939.     opoint)
  940.     (insert "\n* Menu:\n\n")
  941.     (setq opoint (point))
  942.     (latexinfo-print-index nil indexelts)
  943.     (if (eq system-type 'vax-vms) 
  944.     (latexinfo-sort-region opoint (point))
  945.       (shell-command-on-region opoint (point) "sort -fd" 1))))
  946.  
  947. (defun latexinfo-print-index (file indexelts)
  948.   (while indexelts
  949.     (if (stringp (car (car indexelts)))
  950.     (insert "* " (car (car indexelts))
  951.         ": " (if file (concat "(" file ")") "")
  952.         (nth 1 (car indexelts)) ".\n")
  953.       ;; index entries from \include'd file
  954.       (latexinfo-print-index (nth 1 (car indexelts))
  955.                (nth 2 (car indexelts))))
  956.     (setq indexelts (cdr indexelts))))
  957.  
  958.  
  959. ;;;; Lisp Definitions
  960.  
  961. (defun latexinfo-format-defun ()
  962.   (latexinfo-push-stack 'defun nil)
  963.   (setq fill-column (- fill-column 5))
  964.   (latexinfo-format-defun-1 t))
  965.  
  966. (defun latexinfo-format-defunx ()
  967.   (latexinfo-format-defun-1 nil))
  968.  
  969. (defun latexinfo-format-defun-1 (first-p)
  970.   (let ((args (latexinfo-format-parse-defun-args))
  971.     (type (get latexinfo-command-name 'latexinfo-defun-type)))
  972.     (latexinfo-discard-command)
  973.     (if (eq type 'arg)
  974.     (progn (setq type (car args))
  975.            (setq args (cdr args))))
  976.     (let ((formatter (get latexinfo-command-name 'latexinfo-defun-format-type)))
  977.       (if formatter
  978.       (setq type (funcall formatter type args))))
  979.     ;; Delete extra newline inserted after previous header line.
  980.     (if (not first-p)
  981.     (delete-char -1))
  982.     (insert "* " type ": " (car args))
  983.     (let ((args (cdr args)))
  984.       (while args
  985.     (insert " " (upcase (car args)))
  986.     (setq args (cdr args))))
  987.     ;; Insert extra newline so that paragraph filling does not mess
  988.     ;; with header line.
  989.     (insert "\n\n")
  990.     (rplaca (cdr (cdr (car latexinfo-stack))) (point))
  991.     (let ((indexvar (get latexinfo-command-name 'latexinfo-defun-index))
  992.       (formatter (get latexinfo-command-name 'latexinfo-defun-format-index)))
  993.       (set indexvar
  994.        (cons (list (if formatter (funcall formatter type args) (car args))
  995.                latexinfo-last-node)
  996.          (symbol-value indexvar))))))
  997.  
  998. (defun latexinfo-end-defun ()
  999.   (setq fill-column (+ fill-column 5))
  1000.   (latexinfo-discard-command)
  1001.   (let ((start (nth 1 (latexinfo-pop-stack 'defun))))
  1002.     (latexinfo-do-itemize start)
  1003.     ;; Delete extra newline inserted after header.
  1004.     (save-excursion
  1005.       (goto-char start)
  1006.       (delete-char -1))))
  1007.  
  1008. (put 'deffn 'latexinfo-format 'latexinfo-format-defun)
  1009. (put 'deffnx 'latexinfo-format 'latexinfo-format-defunx)
  1010. (put 'deffn 'latexinfo-end 'latexinfo-end-defun)
  1011. (put 'deffn 'latexinfo-defun-type 'arg)
  1012. (put 'deffnx 'latexinfo-defun-type 'arg)
  1013. (put 'deffn 'latexinfo-defun-index 'latexinfo-findex)
  1014. (put 'deffnx 'latexinfo-defun-index 'latexinfo-findex)
  1015.  
  1016. (put 'defun 'latexinfo-format 'latexinfo-format-defun)
  1017. (put 'defunx 'latexinfo-format 'latexinfo-format-defunx)
  1018. (put 'defun 'latexinfo-end 'latexinfo-end-defun)
  1019. (put 'defun 'latexinfo-defun-type "Function")
  1020. (put 'defunx 'latexinfo-defun-type "Function")
  1021. (put 'defun 'latexinfo-defun-index 'latexinfo-findex)
  1022. (put 'defunx 'latexinfo-defun-index 'latexinfo-findex)
  1023.  
  1024. (put 'defmac 'latexinfo-format 'latexinfo-format-defun)
  1025. (put 'defmacx 'latexinfo-format 'latexinfo-format-defunx)
  1026. (put 'defmac 'latexinfo-end 'latexinfo-end-defun)
  1027. (put 'defmac 'latexinfo-defun-type "Macro")
  1028. (put 'defmacx 'latexinfo-defun-type "Macro")
  1029. (put 'defmac 'latexinfo-defun-index 'latexinfo-findex)
  1030. (put 'defmacx 'latexinfo-defun-index 'latexinfo-findex)
  1031.  
  1032. (put 'defspec 'latexinfo-format 'latexinfo-format-defun)
  1033. (put 'defspecx 'latexinfo-format 'latexinfo-format-defunx)
  1034. (put 'defspec 'latexinfo-end 'latexinfo-end-defun)
  1035. (put 'defspec 'latexinfo-defun-type "Special form")
  1036. (put 'defspecx 'latexinfo-defun-type "Special form")
  1037. (put 'defspec 'latexinfo-defun-index 'latexinfo-findex)
  1038. (put 'defspecx 'latexinfo-defun-index 'latexinfo-findex)
  1039.  
  1040. (put 'defvr 'latexinfo-format 'latexinfo-format-defun)
  1041. (put 'defvrx 'latexinfo-format 'latexinfo-format-defunx)
  1042. (put 'defvr 'latexinfo-end 'latexinfo-end-defun)
  1043. (put 'defvr 'latexinfo-defun-type 'arg)
  1044. (put 'defvrx 'latexinfo-defun-type 'arg)
  1045. (put 'defvr 'latexinfo-defun-index 'latexinfo-vindex)
  1046. (put 'defvrx 'latexinfo-defun-index 'latexinfo-vindex)
  1047.  
  1048. (put 'defvar 'latexinfo-format 'latexinfo-format-defun)
  1049. (put 'defvarx 'latexinfo-format 'latexinfo-format-defunx)
  1050. (put 'defvar 'latexinfo-end 'latexinfo-end-defun)
  1051. (put 'defvar 'latexinfo-defun-type "Variable")
  1052. (put 'defvarx 'latexinfo-defun-type "Variable")
  1053. (put 'defvar 'latexinfo-defun-index 'latexinfo-vindex)
  1054. (put 'defvarx 'latexinfo-defun-index 'latexinfo-vindex)
  1055.  
  1056. (put 'defopt 'latexinfo-format 'latexinfo-format-defun)
  1057. (put 'defoptx 'latexinfo-format 'latexinfo-format-defunx)
  1058. (put 'defopt 'latexinfo-end 'latexinfo-end-defun)
  1059. (put 'defopt 'latexinfo-defun-type "User Option")
  1060. (put 'defoptx 'latexinfo-defun-type "User Option")
  1061. (put 'defopt 'latexinfo-defun-index 'latexinfo-vindex)
  1062. (put 'defoptx 'latexinfo-defun-index 'latexinfo-vindex)
  1063.  
  1064. (put 'deftp 'latexinfo-format 'latexinfo-format-defun)
  1065. (put 'deftpx 'latexinfo-format 'latexinfo-format-defunx)
  1066. (put 'deftp 'latexinfo-end 'latexinfo-end-defun)
  1067. (put 'deftp 'latexinfo-defun-type 'arg)
  1068. (put 'deftpx 'latexinfo-defun-type 'arg)
  1069. (put 'deftp 'latexinfo-defun-index 'latexinfo-tindex)
  1070. (put 'deftpx 'latexinfo-defun-index 'latexinfo-tindex)
  1071.  
  1072. ;;; Object-oriented stuff is a little hairier.
  1073.  
  1074. (put 'defop 'latexinfo-format 'latexinfo-format-defun)
  1075. (put 'defopx 'latexinfo-format 'latexinfo-format-defunx)
  1076. (put 'defop 'latexinfo-end 'latexinfo-end-defun)
  1077. (put 'defop 'latexinfo-defun-type 'arg)
  1078. (put 'defopx 'latexinfo-defun-type 'arg)
  1079. (put 'defop 'latexinfo-defun-format-type 'latexinfo-format-defop-type)
  1080. (put 'defopx 'latexinfo-defun-format-type 'latexinfo-format-defop-type)
  1081. (put 'defop 'latexinfo-defun-index 'latexinfo-findex)
  1082. (put 'defopx 'latexinfo-defun-index 'latexinfo-findex)
  1083. (put 'defop 'latexinfo-defun-format-index 'latexinfo-format-defop-index)
  1084. (put 'defopx 'latexinfo-defun-format-index 'latexinfo-format-defop-index)
  1085.  
  1086. (put 'defmethod 'latexinfo-format 'latexinfo-format-defun)
  1087. (put 'defmethodx 'latexinfo-format 'latexinfo-format-defunx)
  1088. (put 'defmethod 'latexinfo-end 'latexinfo-end-defun)
  1089. (put 'defmethod 'latexinfo-defun-type "Operation")
  1090. (put 'defmethodx 'latexinfo-defun-type "Operation")
  1091. (put 'defmethod 'latexinfo-defun-format-type 'latexinfo-format-defop-type)
  1092. (put 'defmethodx 'latexinfo-defun-format-type 'latexinfo-format-defop-type)
  1093. (put 'defmethod 'latexinfo-defun-index 'latexinfo-findex)
  1094. (put 'defmethodx 'latexinfo-defun-index 'latexinfo-findex)
  1095. (put 'defmethod 'latexinfo-defun-format-index 'latexinfo-format-defop-index)
  1096. (put 'defmethodx 'latexinfo-defun-format-index 'latexinfo-format-defop-index)
  1097.  
  1098. (defun latexinfo-format-defop-type (type args)
  1099.   (format "%s on %s" type (car args)))
  1100.  
  1101. (defun latexinfo-format-defop-index (type args)
  1102.   (format "%s on %s" (car (cdr args)) (car args)))
  1103.  
  1104. (put 'defcv 'latexinfo-format 'latexinfo-format-defun)
  1105. (put 'defcvx 'latexinfo-format 'latexinfo-format-defunx)
  1106. (put 'defcv 'latexinfo-end 'latexinfo-end-defun)
  1107. (put 'defcv 'latexinfo-defun-type 'arg)
  1108. (put 'defcvx 'latexinfo-defun-type 'arg)
  1109. (put 'defcv 'latexinfo-defun-format-type 'latexinfo-format-defcv-type)
  1110. (put 'defcvx 'latexinfo-defun-format-type 'latexinfo-format-defcv-type)
  1111. (put 'defcv 'latexinfo-defun-index 'latexinfo-vindex)
  1112. (put 'defcvx 'latexinfo-defun-index 'latexinfo-vindex)
  1113. (put 'defcv 'latexinfo-defun-format-index 'latexinfo-format-defcv-index)
  1114. (put 'defcvx 'latexinfo-defun-format-index 'latexinfo-format-defcv-index)
  1115.  
  1116. (put 'defivar 'latexinfo-format 'latexinfo-format-defun)
  1117. (put 'defivarx 'latexinfo-format 'latexinfo-format-defunx)
  1118. (put 'defivar 'latexinfo-end 'latexinfo-end-defun)
  1119. (put 'defivar 'latexinfo-defun-type "Instance variable")
  1120. (put 'defivarx 'latexinfo-defun-type "Instance variable")
  1121. (put 'defivar 'latexinfo-defun-format-type 'latexinfo-format-defcv-type)
  1122. (put 'defivarx 'latexinfo-defun-format-type 'latexinfo-format-defcv-type)
  1123. (put 'defivar 'latexinfo-defun-index 'latexinfo-vindex)
  1124. (put 'defivarx 'latexinfo-defun-index 'latexinfo-vindex)
  1125. (put 'defivar 'latexinfo-defun-format-index 'latexinfo-format-defcv-index)
  1126. (put 'defivarx 'latexinfo-defun-format-index 'latexinfo-format-defcv-index)
  1127.  
  1128. (defun latexinfo-format-defcv-type (type args)
  1129.   (format "%s of %s" type (car args)))
  1130.  
  1131. (defun latexinfo-format-defcv-index (type args)
  1132.   (format "%s of %s" (car (cdr args)) (car args)))
  1133.  
  1134. ;; process included files
  1135. (put 'include 'latexinfo-format 'latexinfo-format-include)
  1136. (defun latexinfo-format-include ()
  1137.   (let ((filename (latexinfo-parse-arg-discard))
  1138.     (default-directory input-directory)
  1139.     subindex)
  1140.     (setq subindex
  1141.       (save-excursion
  1142.         (progn (find-file
  1143.             (cond ((file-readable-p (concat filename ".texinfo"))
  1144.                (concat filename ".texinfo"))
  1145.               ((file-readable-p (concat filename ".tex"))
  1146.                (concat filename ".tex"))
  1147.               ((file-readable-p filename)
  1148.                filename)
  1149.               (t (error "\\include'd file %s not found"
  1150.                     filename))))
  1151.            (latexinfo-format-buffer-1))))
  1152.     (latexinfo-subindex 'latexinfo-vindex (car subindex) (nth 1 subindex))
  1153.     (latexinfo-subindex 'latexinfo-findex (car subindex) (nth 2 subindex))
  1154.     (latexinfo-subindex 'latexinfo-cindex (car subindex) (nth 3 subindex))
  1155.     (latexinfo-subindex 'latexinfo-pindex (car subindex) (nth 4 subindex))
  1156.     (latexinfo-subindex 'latexinfo-tindex (car subindex) (nth 5 subindex))
  1157.     (latexinfo-subindex 'latexinfo-kindex (car subindex) (nth 6 subindex))))
  1158.  
  1159. (defun latexinfo-subindex (indexvar file content)
  1160.   (set indexvar (cons (list 'recurse file content)
  1161.               (symbol-value indexvar))))
  1162.  
  1163.  
  1164. ;; Lots of bolio constructs do nothing in latexinfo.
  1165.  
  1166. (put 'c 'latexinfo-format 'latexinfo-discard-line-with-args)
  1167. (put 'comment 'latexinfo-format 'latexinfo-discard-line-with-args)
  1168. ;;(put 'setchapternewpage 'latexinfo-format 'latexinfo-discard-line-with-args)
  1169. (put 'nopara 'latexinfo-format 'latexinfo-discard-line-with-args)
  1170. (put 'noindent 'latexinfo-format 'latexinfo-discard-line-with-args)
  1171. ;;(put 'setx 'latexinfo-format 'latexinfo-discard-line-with-args)
  1172. ;;(put 'setq 'latexinfo-format 'latexinfo-discard-line-with-args)
  1173. ;;(put 'settitle 'latexinfo-format 'latexinfo-discard-line-with-args)
  1174. ;;(put 'defindex 'latexinfo-format 'latexinfo-discard-line-with-args)
  1175. (put 'synindex 'latexinfo-format 'latexinfo-discard-line-with-args)
  1176. (put 'hsize 'latexinfo-format 'latexinfo-discard-line-with-args)
  1177. (put 'parindent 'latexinfo-format 'latexinfo-discard-line-with-args)
  1178. (put 'lispnarrowing 'latexinfo-format 'latexinfo-discard-line-with-args)
  1179. (put 'itemindent 'latexinfo-format 'latexinfo-discard-line-with-args)
  1180. (put 'headings 'latexinfo-format 'latexinfo-discard-line-with-args)
  1181. (put 'smallbook 'latexinfo-format 'latexinfo-discard-line)
  1182.  
  1183. (defun latexinfo-discard-line-with-args ()
  1184.   (goto-char latexinfo-command-start)
  1185.   (delete-region (point) (progn (forward-line 1) (point))))
  1186.  
  1187. ;; Sort an index which is in the current buffer between START and END.
  1188. ;; Used on VMS, where the `sort' utility is not available.
  1189. (defun latexinfo-sort-region (start end)
  1190.   (require 'sort)
  1191.   (save-restriction
  1192.     (narrow-to-region start end)
  1193.     (sort-subr nil 'forward-line 'end-of-line 'latexinfo-sort-startkeyfun)))
  1194.  
  1195. ;; Subroutine for sorting an index.
  1196. ;; At start of a line, return a string to sort the line under.
  1197. (defun latexinfo-sort-startkeyfun ()
  1198.   (let ((line
  1199.      (buffer-substring (point) (save-excursion (end-of-line) (point)))))
  1200.     ;; Canonicalize whitespace and eliminate funny chars.
  1201.     (while (string-match "[ \t][ \t]+\\|[^a-z0-9 ]+" line)
  1202.       (setq line (concat (substring line 0 (match-beginning 0))
  1203.              " "
  1204.              (substring line (match-end 0) (length line)))))
  1205.     line))
  1206.  
  1207. ;; Some cannot be handled - just ignore them and junk the line
  1208.  
  1209. (defun latexinfo-unsupported ()
  1210.   (latexinfo-parse-noarg)
  1211.   )
  1212.  
  1213. (defun batch-latexinfo-format ()
  1214.   "Runs  latexinfo-format-buffer  on the files remaining on the command line.
  1215. Must be used only with -batch, and kills emacs on completion.
  1216. Each file will be processed even if an error occurred previously.
  1217. For example, invoke
  1218.   \"emacs -batch -funcall batch-latexinfo-format $docs/ ~/*.tex\"."
  1219.   (if (not noninteractive)
  1220.       (error "batch-latexinfo-format may only be used -batch."))
  1221.   (let ((version-control t)
  1222.     (auto-save-default nil)
  1223.     (find-file-run-dired nil)
  1224.     (kept-old-versions 259259)
  1225.     (kept-new-versions 259259))
  1226.     (let ((error 0)
  1227.       file
  1228.       (files ()))
  1229.       (while command-line-args-left
  1230.     (setq file (expand-file-name (car command-line-args-left)))
  1231.     (cond ((not (file-exists-p file))
  1232.            (message ">> %s does not exist!" file)
  1233.            (setq error 1
  1234.              command-line-args-left (cdr command-line-args-left)))
  1235.           ((file-directory-p file)
  1236.            (setq command-line-args-left
  1237.              (nconc (directory-files file)
  1238.                 (cdr command-line-args-left))))
  1239.           (t
  1240.            (setq files (cons file files)
  1241.              command-line-args-left (cdr command-line-args-left)))))
  1242.       (while files
  1243.     (setq file (car files)
  1244.           files (cdr files))
  1245.     (condition-case err
  1246.         (progn
  1247.           (if buffer-file-name (kill-buffer (current-buffer)))
  1248.           (find-file file)
  1249.           (buffer-flush-undo (current-buffer))
  1250.           (set-buffer-modified-p nil)
  1251.           (latexinfo-mode)
  1252.           (message "Latexinfo formatting %s..." file)
  1253.           (latexinfo-format-buffer nil)
  1254.           (if (buffer-modified-p)
  1255.           (progn (message "Saving modified %s" (buffer-file-name))
  1256.              (save-buffer))))
  1257.       (error
  1258.        (message ">> Error: %s" (prin1-to-string err))
  1259.        (message ">>  point at")
  1260.        (let ((s (buffer-substring (point)
  1261.                       (min (+ (point) 100)
  1262.                        (point-max))))
  1263.          (tem 0))
  1264.          (while (setq tem (string-match "\n+" s tem))
  1265.            (setq s (concat (substring s 0 (match-beginning 0))
  1266.                    "\n>>  "
  1267.                    (substring s (match-end 0)))
  1268.              tem (1+ tem)))
  1269.          (message ">>  %s" s)))
  1270.       (setq error 1)))
  1271.       (kill-emacs error))))
  1272.  
  1273. ;;; LaTeX additions
  1274.  
  1275. (defun latexinfo-run-documentstyle-hooks ()
  1276.   (goto-char (point-min))
  1277.   (search-forward "\\documentstyle")
  1278.   (if (looking-at "\\[")
  1279.       (let ((begin (1+ (point)))
  1280.         (end (save-excursion (search-forward "]") (point)))
  1281.         (options-list nil))
  1282.     (if (null latexinfo-formats-directory)
  1283.         (setq latexinfo-formats-directory default-directory))
  1284.     (while (re-search-forward ",\\|]" end t)
  1285.       (setq options-list (cons (buffer-substring begin (1- (point)))
  1286.                    options-list))
  1287.       (setq begin (point)))
  1288.     (setq options-list (nreverse options-list))
  1289.     (while options-list
  1290.       (let ((option (car options-list))
  1291.         (filename nil))
  1292.         (if (not (memq (intern option) latexinfo-known-document-styles))
  1293.         (progn
  1294.           (message "Checking formatting option %s" option) (sit-for 2)
  1295.           (setq filename (concat 
  1296.                   (file-name-as-directory
  1297.                    latexinfo-formats-directory)
  1298.                   option "-fmt"))
  1299.           (setq filename
  1300.              (cond
  1301.               ((file-readable-p (concat filename ".elc"))
  1302.                (concat filename ".elc"))
  1303.               ((file-readable-p (concat filename ".el"))
  1304.                (concat filename ".el"))
  1305.               (t nil)))
  1306.           (if filename
  1307.               (progn
  1308.             (message "Loading file %s..." filename) (sit-for 1)
  1309.             (load-file filename)
  1310.             (let ((option-symbol
  1311.                    (intern (concat option "-fmt-hook"))))
  1312.               (if (fboundp option-symbol)
  1313.                 (progn
  1314.                     (message
  1315.                  (concat "Running " option
  1316.                      "formatting hooks..."))
  1317.                  (sit-for 1)
  1318.                  (funcall option-symbol))))
  1319.             (message "Done loading file %s" filename) (sit-for 1))
  1320.             ))))
  1321.       (setq options-list (cdr options-list))))))
  1322.  
  1323. (defun latexinfo-parse-noarg ()
  1324.    (delete-region latexinfo-command-start latexinfo-command-end)
  1325.    (cond ((looking-at "{")
  1326.       (delete-region (point)
  1327.              (save-excursion
  1328.                (forward-sexp 1) (point))))
  1329.     ;; TeX gobbles the next whitespace.
  1330.      ((or (eolp) (looking-at "[ \t\n]"))
  1331.       (delete-region (point)
  1332.              (save-excursion
  1333.                (skip-chars-forward " \t\n") (point)))))
  1334.     )
  1335.  
  1336. (put 'back 'latexinfo-format 'latexinfo-format-backslash)
  1337. (defun latexinfo-format-backslash ()
  1338.   (latexinfo-discard-command)
  1339.   ;; TeX gobbles the next whitespace.
  1340.   (if (looking-at "[ \t\n]")
  1341.       (delete-region (point)
  1342.              (save-excursion
  1343.                (skip-chars-forward " \t\n")
  1344.                (point))))
  1345.   (insert ?\\))
  1346.  
  1347. (put 'same 'latexinfo-format 'latexinfo-discard-command)
  1348. (put 'same 'latexinfo-end 'latexinfo-discard-command)
  1349.  
  1350. (put 'cite 'latexinfo-format 'latexinfo-format-cite)
  1351. (defun latexinfo-format-cite ()
  1352.   (if (looking-at "[\\[]")
  1353.       (save-excursion
  1354.     (let ((here (point)) (str nil))
  1355.           (forward-sexp 1)
  1356.           (setq str (buffer-substring (1+ here) (- (point) 1)))
  1357.           (delete-region here (point))
  1358.           (if (eolp) (delete-char 1))
  1359.           (insert "[" (latexinfo-parse-arg-discard) ", " str "]")))
  1360.     (insert "[" (latexinfo-parse-arg-discard) "]"))
  1361.   (goto-char latexinfo-command-start))
  1362.  
  1363. (put 'hfill 'latexinfo-format 'latexinfo-format-hfill)
  1364. (defun latexinfo-format-hfill ()
  1365.   (latexinfo-parse-arg-discard)
  1366.   (insert-char ?\  (- fill-column
  1367.               (save-excursion
  1368.             (end-of-line 1)
  1369.             (current-column)))))
  1370.  
  1371.  
  1372. (put 'S 'latexinfo-format 'latexinfo-format-S)
  1373. (defun latexinfo-format-S ()
  1374.   (latexinfo-parse-noarg)
  1375.   (insert "Section"))
  1376.  
  1377. (put 'LaTeX 'latexinfo-format 'latexinfo-format-LaTeX)
  1378. (defun latexinfo-format-LaTeX ()
  1379.   (latexinfo-parse-noarg)
  1380.   (insert "LaTeX"))
  1381.  
  1382. (put 'arrow 'latexinfo-format 'latexinfo-format-arrow)
  1383. (defun latexinfo-format-arrow ()
  1384.   (latexinfo-parse-noarg)
  1385.   (insert "=> "))
  1386.  
  1387. (put 'leq 'latexinfo-format 'latexinfo-format-leq)
  1388. (defun latexinfo-format-leq ()
  1389.   (latexinfo-parse-noarg)
  1390.   (insert "<="))
  1391.  
  1392. (put 'geq 'latexinfo-format 'latexinfo-format-geq)
  1393. (defun latexinfo-format-geq ()
  1394.   (latexinfo-parse-noarg)
  1395.   (insert "<="))
  1396.  
  1397. (put 'pi 'latexinfo-format 'latexinfo-format-pi)
  1398. (defun latexinfo-format-pi ()
  1399.   (latexinfo-parse-noarg)
  1400.   (insert "pi"))
  1401.  
  1402. (put 'quad 'latexinfo-format 'latexinfo-format-quad)
  1403. (defun latexinfo-format-quad ()
  1404.   (latexinfo-parse-noarg)
  1405.   (insert "    "))
  1406.  
  1407. (put 'qquad 'latexinfo-format 'latexinfo-format-qquad)
  1408. (defun latexinfo-format-qquad ()
  1409.   (latexinfo-parse-noarg)
  1410.   (insert "      "))
  1411.  
  1412. (put 'pm 'latexinfo-format 'latexinfo-format-pm)
  1413. (defun latexinfo-format-pm ()
  1414.   (latexinfo-parse-noarg)
  1415.   (insert "+/-"))
  1416.  
  1417. (put '& 'latexinfo-format 'latexinfo-format-&)
  1418. (defun latexinfo-format-& ()
  1419.   (latexinfo-parse-noarg)
  1420.   (insert "&"))
  1421.  
  1422. ;; LaTeX noops
  1423. (put 'DOTS 'latexinfo-format 'latexinfo-format-dots)
  1424. (put 'clearpage 'latexinfo-format 'latexinfo-discard-line-with-args)
  1425. (put 'cleardoublepage 'latexinfo-format 'latexinfo-discard-line-with-args)
  1426. (put 'tableofcontents 'latexinfo-format 'latexinfo-discard-line-with-args)
  1427. (put 'protect 'latexinfo-format 'latexinfo-parse-noarg)
  1428. (put 'vspace 'latexinfo-format 'latexinfo-format-noop)
  1429. (put 'pagestyle 'latexinfo-format 'latexinfo-discard-line-with-args)
  1430. (put 'pagenumbering 'latexinfo-format 'latexinfo-discard-line-with-args)
  1431. (put 'markboth 'latexinfo-format 'latexinfo-discard-line-with-args)
  1432. (put 'label 'latexinfo-format 'latexinfo-discard-line-with-args)
  1433. (put 'index 'latexinfo-format 'latexinfo-format-noop)
  1434. (put 'cpsubindex 'latexinfo-format 'latexinfo-discard-line-with-args)
  1435.  
  1436. (provide 'latexinfo)
  1437.  
  1438.  
  1439.