home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / utility / misc / gulamdoc / texinfmt.el < prev   
Lisp/Scheme  |  1987-11-02  |  30KB  |  861 lines

  1. ;; Convert texinfo files to info files.
  2. ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  3.  
  4. ;; This file is 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 texinfo-format-syntax-table nil)
  23.  
  24. (defvar texinfo-vindex)
  25. (defvar texinfo-findex)
  26. (defvar texinfo-cindex)
  27. (defvar texinfo-pindex)
  28. (defvar texinfo-tindex)
  29. (defvar texinfo-kindex)
  30. (defvar texinfo-last-node)
  31. (defvar texinfo-node-names)
  32.  
  33. (if texinfo-format-syntax-table
  34.     nil
  35.   (setq texinfo-format-syntax-table (make-syntax-table))
  36.   (modify-syntax-entry ?\" " " texinfo-format-syntax-table)
  37.   (modify-syntax-entry ?\\ " " texinfo-format-syntax-table)
  38.   (modify-syntax-entry ?@ "\\" texinfo-format-syntax-table)
  39.   (modify-syntax-entry ?\^q "\\" texinfo-format-syntax-table)
  40.   (modify-syntax-entry ?\[ "." texinfo-format-syntax-table)
  41.   (modify-syntax-entry ?\] "." texinfo-format-syntax-table)
  42.   (modify-syntax-entry ?\( "." texinfo-format-syntax-table)
  43.   (modify-syntax-entry ?\) "." texinfo-format-syntax-table)
  44.   (modify-syntax-entry ?{ "(}" texinfo-format-syntax-table)
  45.   (modify-syntax-entry ?} "){" texinfo-format-syntax-table)
  46.   (modify-syntax-entry ?\' "." texinfo-format-syntax-table))
  47.  
  48. (defun texinfo-format-buffer (&optional notagify)
  49.   "Process the current buffer as texinfo code, into an Info file.
  50. The Info file output is generated in a buffer
  51. visiting the Info file names specified in the @setfilename command.
  52.  
  53. Non-nil argument (prefix, if interactive) means don't make tag table
  54. and don't split the file if large.  You can use Info-tagify and
  55. Info-split to do these manually."
  56.   (interactive "P")
  57.   (let ((lastmessage "Formatting Info file..."))
  58.     (message lastmessage)
  59.     (texinfo-format-buffer-1)
  60.     (if notagify
  61.     nil
  62.       (if (> (buffer-size) 30000)
  63.       (progn
  64.         (message (setq lastmessage "Making tags table for Info file..."))
  65.         (Info-tagify)))
  66.       (if (> (buffer-size) 100000)
  67.       (progn
  68.         (message (setq lastmessage "Splitting Info file..."))
  69.         (Info-split))))
  70.     (message (concat lastmessage
  71.              (if (interactive-p) "done.  Now save it." "done.")))))
  72.  
  73. (defun texinfo-format-buffer-1 ()
  74.   (let (texinfo-format-filename
  75.     texinfo-example-start
  76.     texinfo-command-start
  77.     texinfo-command-end
  78.     texinfo-command-name
  79.     texinfo-last-node
  80.     texinfo-vindex
  81.     texinfo-findex
  82.     texinfo-cindex
  83.     texinfo-pindex
  84.     texinfo-tindex
  85.     texinfo-kindex
  86.     texinfo-stack
  87.     texinfo-node-names
  88.     outfile
  89.     (fill-column fill-column)
  90.     (input-buffer (current-buffer))
  91.     (input-directory default-directory))
  92.     (save-excursion
  93.       (goto-char (point-min))
  94.       (search-forward "@setfilename")
  95.       (setq texinfo-command-end (point))
  96.       (setq outfile (texinfo-parse-line-arg)))
  97.     (find-file outfile)
  98.     (texinfo-mode)
  99.     (set-syntax-table texinfo-format-syntax-table)
  100.     (erase-buffer)
  101.     (insert-buffer-substring input-buffer)
  102.     (goto-char (point-min))
  103.     (search-forward "@setfilename")
  104.     (beginning-of-line)
  105.     (delete-region (point-min) (point))
  106.     (while (search-forward "``" nil t)
  107.       (replace-match "\""))
  108.     (goto-char (point-min))
  109.     (while (search-forward "''" nil t)
  110.       (replace-match "\""))
  111.     (goto-char (point-min))
  112.     (while (search-forward "@" nil t)
  113.       ;; If the @ is preceded by an odd number of ^Q's, do nothing,
  114.       (if (and (eq (char-after (- (point) 2)) ?\^Q)
  115.            (save-excursion
  116.          (forward-char -1)
  117.          (let ((opoint (point)))
  118.            (skip-chars-backward "\^Q")
  119.            (= (logand 1 (- opoint (point))) 1))))
  120.       nil
  121.     (if (looking-at "[@{}'` *]")
  122.         (if (= (following-char) ?*)
  123.         (delete-region (1- (point)) (1+ (point)))
  124.           (delete-char -1)
  125.           (forward-char 1))
  126.       (setq texinfo-command-start (1- (point)))
  127.       (if (= (char-syntax (following-char)) ?w)
  128.           (forward-word 1)
  129.         (forward-char 1))
  130.       (setq texinfo-command-end (point))
  131.       (setq texinfo-command-name
  132.         (intern (buffer-substring (1+ texinfo-command-start)
  133.                       texinfo-command-end)))
  134.       (let ((cmd (get texinfo-command-name 'texinfo-format)))
  135.         (if cmd (funcall cmd)
  136.           (texinfo-unsupported))))))
  137.     (cond (texinfo-stack
  138.        (goto-char (nth 2 (car texinfo-stack)))
  139.        (error "Unterminated @%s" (car (car texinfo-stack)))))
  140.     (goto-char (point-min))
  141.     (while (search-forward "\^q" nil t)
  142.       (delete-char -1)
  143.       (forward-char 1))
  144.     (goto-char (point-min))
  145.     (list outfile
  146.       texinfo-vindex texinfo-findex texinfo-cindex
  147.       texinfo-pindex texinfo-tindex texinfo-kindex)))
  148.  
  149. (put 'begin 'texinfo-format 'texinfo-format-begin)
  150. (defun texinfo-format-begin ()
  151.   (texinfo-format-begin-end 'texinfo-format))
  152.  
  153. (put 'begin 'texinfo-format 'texinfo-format-begin)
  154. (defun texinfo-format-begin ()
  155.   (texinfo-format-begin-end 'texinfo-format))
  156.  
  157. (put 'end 'texinfo-format 'texinfo-format-end)
  158. (defun texinfo-format-end ()
  159.   (texinfo-format-begin-end 'texinfo-end))
  160.  
  161. (defun texinfo-format-begin-end (prop)
  162.   (setq texinfo-command-name (intern (texinfo-parse-line-arg)))
  163.   (setq cmd (get texinfo-command-name prop))
  164.   (if cmd (funcall cmd)
  165.     (texinfo-unsupported)))
  166.  
  167. (defun texinfo-parse-line-arg ()
  168.   (goto-char texinfo-command-end)
  169.   (let ((start (point)))
  170.     (cond ((looking-at " ")
  171.        (skip-chars-forward " ")
  172.        (setq start (point))
  173.        (end-of-line)
  174.        (setq texinfo-command-end (1+ (point))))
  175.       ((looking-at "{")
  176.        (setq start (1+ (point)))
  177.        (forward-list 1)
  178.        (setq texinfo-command-end (point))
  179.        (forward-char -1))
  180.       (t
  181.        (error "Invalid texinfo command arg format")))
  182.     (prog1 (buffer-substring start (point))
  183.        (if (eolp) (forward-char 1)))))
  184.  
  185. (defun texinfo-parse-arg-discard ()
  186.   (prog1 (texinfo-parse-line-arg)
  187.      (texinfo-discard-command)))
  188.  
  189. (defun texinfo-discard-command ()
  190.   (delete-region texinfo-command-start texinfo-command-end))
  191.  
  192. (defun texinfo-format-parse-line-args ()
  193.   (let ((start (1- (point)))
  194.     next beg end
  195.     args)
  196.     (skip-chars-forward " ")
  197.     (while (not (eolp))
  198.       (setq beg (point))
  199.       (re-search-forward "[\n,]")
  200.       (setq next (point))
  201.       (if (bolp) (setq next (1- next)))
  202.       (forward-char -1)
  203.       (skip-chars-backward " ")
  204.       (setq end (point))
  205.       (setq args (cons (if (> end beg) (buffer-substring beg end))
  206.                args))
  207.       (goto-char next)
  208.       (skip-chars-forward " "))
  209.     (if (eolp) (forward-char 1))
  210.     (setq texinfo-command-end (point))
  211.     (nreverse args)))
  212.  
  213. (defun texinfo-format-parse-args ()
  214.   (let ((start (1- (point)))
  215.     next beg end
  216.     args)
  217.     (search-forward "{")
  218.     (while (/= (preceding-char) ?\})
  219.       (skip-chars-forward " \t\n")
  220.       (setq beg (point))
  221.       (re-search-forward "[},]")
  222.       (setq next (point))
  223.       (forward-char -1)
  224.       (skip-chars-backward " \t\n")
  225.       (setq end (point))
  226.       (cond ((< beg end)
  227.          (goto-char beg)
  228.          (while (search-forward "\n" end t)
  229.            (replace-match " "))))
  230.       (setq args (cons (if (> end beg) (buffer-substring beg end))
  231.                args))
  232.       (goto-char next))
  233.     (if (eolp) (forward-char 1))
  234.     (setq texinfo-command-end (point))
  235.     (nreverse args)))
  236.  
  237. (put 'setfilename 'texinfo-format 'texinfo-format-setfilename)
  238. (defun texinfo-format-setfilename ()
  239.   (let ((arg (texinfo-parse-arg-discard)))
  240.     (setq texinfo-format-filename (file-name-nondirectory arg))
  241.     (insert "Info file "
  242.         texinfo-format-filename
  243.         ", produced by texinfo-format-buffer   -*-Text-*-\nfrom "
  244.         (if (buffer-file-name input-buffer)
  245.         (concat "file "
  246.             (file-name-nondirectory (buffer-file-name input-buffer)))
  247.           (concat "buffer " (buffer-name input-buffer)))
  248.         ?\n)))
  249.  
  250. (put 'node 'texinfo-format 'texinfo-format-node)
  251. (defun texinfo-format-node ()
  252.   (let* ((args (texinfo-format-parse-line-args))
  253.      (name (nth 0 args))
  254.      (next (nth 1 args))
  255.      (prev (nth 2 args))
  256.      (up (nth 3 args)))
  257.     (texinfo-discard-command)
  258.     (setq texinfo-last-node name)
  259.     (let ((tem (downcase name)))
  260.       (if (assoc tem texinfo-node-names)
  261.       (error "Duplicate node name: %s" name)
  262.     (setq texinfo-node-names (cons tem texinfo-node-names))))
  263.     (or (bolp)
  264.     (insert ?\n))
  265.     (insert "\^_\nFile: " texinfo-format-filename
  266.         "  Node: " name)
  267.     (if prev
  268.     (insert ", Prev: " prev))
  269.     (if up
  270.     (insert ", Up: " up))
  271.     (if next
  272.     (insert ", Next: " next))
  273.     (insert ?\n)))
  274.  
  275. (put 'menu 'texinfo-format 'texinfo-format-menu)
  276. (defun texinfo-format-menu ()
  277.   (texinfo-discard-line)
  278.   (insert "* Menu:\n\n"))
  279.  
  280. (put 'menu 'texinfo-end 'texinfo-discard-command)
  281. (defun texinfo-discard-line ()
  282.   (goto-char texinfo-command-end)
  283.   (skip-chars-forward " \t")
  284.   (or (eolp)
  285.       (error "Extraneous text at end of command line."))
  286.   (goto-char texinfo-command-start)
  287.   (or (bolp)
  288.       (error "Extraneous text at beginning of command line."))
  289.   (delete-region (point) (progn (forward-line 1) (point))))
  290.  
  291. ; @xref {NODE, FNAME, NAME, FILE, DOCUMENT}
  292. ; -> *Note FNAME: (FILE)NODE
  293. ;   If FILE is missing,
  294. ;    *Note FNAME: NODE
  295. ;   If FNAME is empty and NAME is present
  296. ;    *Note NAME: Node
  297. ;   If both NAME and FNAME are missing
  298. ;    *Note NODE::
  299. ;   texinfo ignores the DOCUMENT argument.
  300. ; -> See section <xref to NODE> [NAME, else NODE], page <xref to NODE>
  301. ;   If FILE is specified, (FILE)NODE is used for xrefs.
  302. ;   If fifth argument DOCUMENT is specified, produces
  303. ;    See section <xref to NODE> [NAME, else NODE], page <xref to NODE>
  304. ;    of DOCUMENT
  305. (put 'xref 'texinfo-format 'texinfo-format-xref)
  306. (defun texinfo-format-xref ()
  307.   (let ((args (texinfo-format-parse-args)))
  308.     (texinfo-discard-command)
  309.     (insert "*Note ")
  310.     (let ((fname (or (nth 1 args) (nth 2 args))))
  311.       (if (null (or fname (nth 3 args)))
  312.       (insert (car args) "::")
  313.     (insert (or fname (car args)) ": ")
  314.     (if (nth 3 args)
  315.         (insert "(" (nth 3 args) ")"))
  316.     (insert (car args))))))
  317.  
  318. (put 'pxref 'texinfo-format 'texinfo-format-pxref)
  319. (defun texinfo-format-pxref ()
  320.   (texinfo-format-xref)
  321.   (or (save-excursion
  322.     (forward-char -2)
  323.     (looking-at "::"))
  324.       (insert ".")))
  325.  
  326. ;@inforef{NODE, FNAME, FILE}
  327. ;Like @xref{NODE, FNAME,,FILE} in texinfo.
  328. ;In Tex, generates "See Info file FILE, node NODE"
  329. (put 'inforef 'texinfo-format 'texinfo-format-inforef)
  330. (defun texinfo-format-inforef ()
  331.   (let ((args (texinfo-format-parse-args)))
  332.     (texinfo-discard-command)
  333.     (insert "*Note " (nth 1 args) ": (" (nth 2 args) ")" (car args))))
  334.  
  335. (put 'chapheading 'texinfo-format 'texinfo-format-chapter)
  336. (put 'ichapter 'texinfo-format 'texinfo-format-chapter)
  337. (put 'chapter 'texinfo-format 'texinfo-format-chapter)
  338. (put 'iappendix 'texinfo-format 'texinfo-format-chapter)
  339. (put 'appendix 'texinfo-format 'texinfo-format-chapter)
  340. (put 'iunnumbered 'texinfo-format 'texinfo-format-chapter)
  341. (put 'unnumbered 'texinfo-format 'texinfo-format-chapter)
  342. (defun texinfo-format-chapter ()
  343.   (texinfo-format-chapter-1 ?*))
  344.  
  345. (put 'heading 'texinfo-format 'texinfo-format-section)
  346. (put 'isection 'texinfo-format 'texinfo-format-section)
  347. (put 'section 'texinfo-format 'texinfo-format-section)
  348. (put 'iappendixsection 'texinfo-format 'texinfo-format-section)
  349. (put 'appendixsection 'texinfo-format 'texinfo-format-section)
  350. (put 'iappendixsec 'texinfo-format 'texinfo-format-section)
  351. (put 'appendixsec 'texinfo-format 'texinfo-format-section)
  352. (put 'iunnumberedsec 'texinfo-format 'texinfo-format-section)
  353. (put 'unnumberedsec 'texinfo-format 'texinfo-format-section)
  354. (defun texinfo-format-section ()
  355.   (texinfo-format-chapter-1 ?=))
  356.  
  357. (put 'subheading 'texinfo-format 'texinfo-format-subsection)
  358. (put 'isubsection 'texinfo-format 'texinfo-format-subsection)
  359. (put 'subsection 'texinfo-format 'texinfo-format-subsection)
  360. (put 'iappendixsubsec 'texinfo-format 'texinfo-format-subsection)
  361. (put 'appendixsubsec 'texinfo-format 'texinfo-format-subsection)
  362. (put 'iunnumberedsubsec 'texinfo-format 'texinfo-format-subsection)
  363. (put 'unnumberedsubsec 'texinfo-format 'texinfo-format-subsection)
  364. (defun texinfo-format-subsection ()
  365.   (texinfo-format-chapter-1 ?-))
  366.  
  367. (put 'subsubheading 'texinfo-format 'texinfo-format-subsubsection)
  368. (put 'isubsubsection 'texinfo-format 'texinfo-format-subsubsection)
  369. (put 'subsubsection 'texinfo-format 'texinfo-format-subsubsection)
  370. (put 'iappendixsubsubsec 'texinfo-format 'texinfo-format-subsubsection)
  371. (put 'appendixsubsubsec 'texinfo-format 'texinfo-format-subsubsection)
  372. (put 'iunnumberedsubsubsec 'texinfo-format 'texinfo-format-subsubsection)
  373. (put 'unnumberedsubsubsec 'texinfo-format 'texinfo-format-subsubsection)
  374. (defun texinfo-format-subsubsection ()
  375.   (texinfo-format-chapter-1 ?.))
  376.  
  377. (defun texinfo-format-chapter-1 (belowchar)
  378.   (let ((arg (texinfo-parse-arg-discard)))
  379.     (insert ?\n arg ?\n "@SectionPAD " belowchar ?\n)
  380.     (forward-line -2)))
  381.  
  382. (put 'SectionPAD 'texinfo-format 'texinfo-format-sectionpad)
  383. (defun texinfo-format-sectionpad ()
  384.   (let ((str (texinfo-parse-arg-discard)))
  385.     (forward-char -1)
  386.     (let ((column (current-column)))
  387.       (forward-char 1)
  388.       (while (> column 0)
  389.     (insert str)
  390.     (setq column (1- column))))
  391.     (insert ?\n)))
  392.  
  393. (put '\. 'texinfo-format 'texinfo-format-\.)
  394. (defun texinfo-format-\. ()
  395.   (texinfo-discard-command)
  396.   (insert "."))
  397.  
  398. (put '\: 'texinfo-format 'texinfo-format-\:)
  399. (defun texinfo-format-\: ()
  400.   (texinfo-discard-command))
  401.  
  402. ;; ++jrb
  403. (put '\" 'texinfo-format 'texinfo-format-\dq)
  404. (defun texinfo-format-\dq ()
  405.   (texinfo-discard-command))
  406. ;; -- jrb
  407.  
  408. ;; @itemize pushes (itemize "COMMANDS" STARTPOS) on texinfo-stack.
  409. ;; @enumerate pushes (enumerate 0 STARTPOS).
  410. ;; @item dispatches to the texinfo-item prop of the first elt of the list.
  411. ;; For itemize, this puts in and rescans the COMMANDS.
  412. ;; For enumerate, this increments the number and puts it in.
  413. ;; In either case, it puts a Backspace at the front of the line
  414. ;; which marks it not to be indented later.
  415. ;; All other lines get indented by 5 when the @end is reached.
  416.  
  417. (defun texinfo-push-stack (check arg)
  418.   (setq texinfo-stack
  419.     (cons (list check arg texinfo-command-start)
  420.           texinfo-stack)))
  421.  
  422. (defun texinfo-pop-stack (check)
  423.   (if (null texinfo-stack)
  424.       (error "Unmatched @end %s" check))
  425.   (if (not (eq (car (car texinfo-stack)) check))
  426.       (error "@end %s matches @%s"
  427.          check (car (car texinfo-stack))))
  428.   (prog1 (cdr (car texinfo-stack))
  429.      (setq texinfo-stack (cdr texinfo-stack))))
  430.  
  431. (put 'itemize 'texinfo-format 'texinfo-itemize)
  432. (defun texinfo-itemize ()
  433.   (texinfo-push-stack 'itemize (texinfo-parse-arg-discard))
  434.   (setq fill-column (- fill-column 5)))
  435.  
  436. (put 'itemize 'texinfo-end 'texinfo-end-itemize)
  437. (defun texinfo-end-itemize ()
  438.   (setq fill-column (+ fill-column 5))
  439.   (texinfo-discard-command)
  440.   (let ((stacktop
  441.      (texinfo-pop-stack 'itemize)))
  442.     (texinfo-do-itemize (nth 1 stacktop))))
  443.  
  444. (put 'enumerate 'texinfo-format 'texinfo-enumerate)
  445. (defun texinfo-enumerate ()
  446.   (texinfo-push-stack 'enumerate 0)
  447.   (setq fill-column (- fill-column 5))
  448.   (texinfo-discard-line))
  449.  
  450. (put 'enumerate 'texinfo-end 'texinfo-end-enumerate)
  451. (defun texinfo-end-enumerate ()
  452.   (setq fill-column (+ fill-column 5))
  453.   (texinfo-discard-command)
  454.   (let ((stacktop
  455.      (texinfo-pop-stack 'enumerate)))
  456.     (texinfo-do-itemize (nth 1 stacktop))))
  457.  
  458. (put 'table 'texinfo-format 'texinfo-table)
  459. (defun texinfo-table ()
  460.   (texinfo-push-stack 'table (texinfo-parse-arg-discard))
  461.   (setq fill-column (- fill-column 5)))
  462.  
  463. (put 'ftable 'texinfo-format 'texinfo-ftable)
  464. (defun texinfo-ftable ()
  465.   (texinfo-push-stack 'table "@code")
  466.   (setq fill-column (- fill-column 5))
  467.   (texinfo-discard-line))
  468.  
  469. (put 'description 'texinfo-format 'texinfo-description)
  470. (defun texinfo-description ()
  471.   (texinfo-push-stack 'table "@asis")
  472.   (setq fill-column (- fill-column 5))
  473.   (texinfo-discard-line))
  474.  
  475. (put 'table 'texinfo-end 'texinfo-end-table)
  476. (put 'ftable 'texinfo-end 'texinfo-end-table)
  477. (put 'description 'texinfo-end 'texinfo-end-table)
  478. (defun texinfo-end-table ()
  479.   (setq fill-column (+ fill-column 5))
  480.   (texinfo-discard-command)
  481.   (let ((stacktop
  482.      (texinfo-pop-stack 'table)))
  483.     (texinfo-do-itemize (nth 1 stacktop))))
  484.  
  485. ;; At the @end, indent all the lines within the construct
  486. ;; except those marked with backspace.  FROM says where
  487. ;; construct started.
  488. (defun texinfo-do-itemize (from)
  489.   (save-excursion
  490.    (while (progn (forward-line -1)
  491.          (>= (point) from))
  492.      (if (= (following-char) ?\b)
  493.      (save-excursion
  494.        (delete-char 1)
  495.        (end-of-line)
  496.        (delete-char 6))
  497.        (save-excursion (insert "     "))))))
  498.  
  499. (put 'item 'texinfo-format 'texinfo-item)
  500. (put 'itemx 'texinfo-format 'texinfo-item)
  501. (defun texinfo-item ()
  502.   (funcall (get (car (car texinfo-stack)) 'texinfo-item)))
  503.  
  504. (put 'itemize 'texinfo-item 'texinfo-itemize-item)
  505. (defun texinfo-itemize-item ()
  506.   (texinfo-discard-line)
  507.   (insert "\b   " (nth 1 (car texinfo-stack)) " \n")
  508.   (forward-line -1))
  509.  
  510. (put 'enumerate 'texinfo-item 'texinfo-enumerate-item)
  511. (defun texinfo-enumerate-item ()
  512.   (texinfo-discard-line)
  513.   (let ((next (1+ (car (cdr (car texinfo-stack))))))
  514.     (setcar (cdr (car texinfo-stack)) next)
  515.     (insert ?\b (format "%3d. " next) ?\n))
  516.   (forward-line -1))
  517.  
  518. (put 'table 'texinfo-item 'texinfo-table-item)
  519. (defun texinfo-table-item ()
  520.   (let ((arg (texinfo-parse-arg-discard))
  521.     (itemfont (car (cdr (car texinfo-stack)))))
  522.     (insert ?\b itemfont ?\{ arg "}\n     \n"))
  523.   (forward-line -2))
  524.  
  525. (put 'ifinfo 'texinfo-format 'texinfo-discard-command)
  526. (put 'ifinfo 'texinfo-end 'texinfo-discard-command)
  527.  
  528. (put 'iftex 'texinfo-format 'texinfo-format-iftex)
  529. (defun texinfo-format-iftex ()
  530.   (delete-region texinfo-command-start
  531.          (progn (re-search-forward "@end iftex\n")
  532.             (point))))
  533.  
  534. (put 'tex 'texinfo-format 'texinfo-format-tex)
  535. (defun texinfo-format-tex ()
  536.   (delete-region texinfo-command-start
  537.          (progn (re-search-forward "@end tex\n")
  538.             (point))))
  539.  
  540. (put 'titlepage 'texinfo-format 'texinfo-format-titlepage)
  541. (defun texinfo-format-titlepage ()
  542.   (delete-region texinfo-command-start
  543.          (progn (search-forward "@end titlepage\n")
  544.             (point))))
  545.  
  546. (put 'endtitlepage 'texinfo-format 'texinfo-discard-line)
  547.  
  548. (put 'ignore 'texinfo-format 'texinfo-format-ignore)
  549. (defun texinfo-format-ignore ()
  550.   (delete-region texinfo-command-start
  551.          (progn (search-forward "@end ignore\n")
  552.             (point))))
  553.  
  554. (put 'endignore 'texinfo-format 'texinfo-discard-line)
  555.  
  556. (put 'var 'texinfo-format 'texinfo-format-var)
  557. (defun texinfo-format-var ()
  558.   (insert (upcase (texinfo-parse-arg-discard)))
  559.   (goto-char texinfo-command-start))
  560.  
  561. (put 'asis 'texinfo-format 'texinfo-format-noop)
  562. (put 'b 'texinfo-format 'texinfo-format-noop)
  563. (put 't 'texinfo-format 'texinfo-format-noop)
  564. (put 'i 'texinfo-format 'texinfo-format-noop)
  565. (put 'r 'texinfo-format 'texinfo-format-noop)
  566. (put 'key 'texinfo-format 'texinfo-format-noop)
  567. (put 'w 'texinfo-format 'texinfo-format-noop)
  568. (defun texinfo-format-noop ()
  569.   (insert (texinfo-parse-arg-discard))
  570.   (goto-char texinfo-command-start))
  571.  
  572. (put 'code 'texinfo-format 'texinfo-format-code)
  573. (put 'samp 'texinfo-format 'texinfo-format-code)
  574. (put 'file 'texinfo-format 'texinfo-format-code)
  575. (put 'kbd 'texinfo-format 'texinfo-format-code)
  576. (defun texinfo-format-code ()
  577.   (insert "`" (texinfo-parse-arg-discard) "'")
  578.   (goto-char texinfo-command-start))
  579.  
  580. ;; Added ++jrb ++++++++++++++++++++++++++++++++++++++++
  581. (put 'emph 'texinfo-format 'texinfo-format-emph)
  582. (put 'strong 'texinfo-format 'texinfo-format-emph)
  583. (defun texinfo-format-emph ()
  584.   (insert "*" (texinfo-parse-arg-discard) "*")
  585.   (goto-char texinfo-command-start))
  586. ;;
  587. ;; Think of a better way to do this!! ++jrb
  588. (defun texinfo-format-center ()
  589.   (insert (texinfo-parse-arg-discard))
  590.   (goto-char texinfo-command-start))
  591. ;;+++++++++++++++++++++++++++++++++++++++++++++++++++++
  592.  
  593. (put 'defn 'texinfo-format 'texinfo-format-defn)
  594. (put 'dfn 'texinfo-format 'texinfo-format-defn)
  595. (defun texinfo-format-defn ()
  596.   (insert "\"" (texinfo-parse-arg-discard) "\"")
  597.   (goto-char texinfo-command-start))
  598.  
  599. (put 'bullet 'texinfo-format 'texinfo-format-bullet)
  600. (defun texinfo-format-bullet ()
  601.   (texinfo-discard-command)
  602.   (insert "*"))
  603.  
  604. (put 'smallexample 'texinfo-format 'texinfo-format-example)
  605. (put 'example 'texinfo-format 'texinfo-format-example)
  606. (put 'quotation 'texinfo-format 'texinfo-format-example)
  607. (put 'lisp 'texinfo-format 'texinfo-format-example)
  608. (put 'display 'texinfo-format 'texinfo-format-example)
  609. (put 'format 'texinfo-format 'texinfo-format-example)
  610. (put 'flushleft 'texinfo-format 'texinfo-format-example)
  611. (defun texinfo-format-example ()
  612.   (texinfo-push-stack 'example nil)
  613.   (setq fill-column (- fill-column 5))
  614.   (texinfo-discard-line))
  615.  
  616. (put 'smallexample 'texinfo-end 'texinfo-end-example)
  617. (put 'example 'texinfo-end 'texinfo-end-example)
  618. (put 'quotation 'texinfo-end 'texinfo-end-example)
  619. (put 'lisp 'texinfo-end 'texinfo-end-example)
  620. (put 'display 'texinfo-end 'texinfo-end-example)
  621. (put 'format 'texinfo-end 'texinfo-end-example)
  622. (put 'flushleft 'texinfo-end 'texinfo-end-example)
  623. (defun texinfo-end-example ()
  624.   (setq fill-column (+ fill-column 5))
  625.   (texinfo-discard-command)
  626.   (let ((stacktop
  627.      (texinfo-pop-stack 'example)))
  628.     (texinfo-do-itemize (nth 1 stacktop))))
  629.  
  630.  
  631. (put 'exdent 'texinfo-format 'texinfo-format-exdent)
  632. (defun texinfo-format-exdent ()
  633.   (texinfo-discard-command)
  634.   (delete-region (point)
  635.          (progn
  636.           (skip-chars-forward " ")
  637.           (point)))
  638.   (insert ?\b)
  639.   ;; Cancel out the deletion that texinfo-do-itemize
  640.   ;; is going to do at the end of this line.
  641.   (save-excursion
  642.     (end-of-line)
  643.     (insert "\n     ")))
  644.  
  645. (put 'ctrl 'texinfo-format 'texinfo-format-ctrl)
  646. (defun texinfo-format-ctrl ()
  647.   (let ((str (texinfo-parse-arg-discard)))
  648.     (insert (logand 31 (aref str 0)))))
  649.  
  650. (put 'TeX 'texinfo-format 'texinfo-format-TeX)
  651. (defun texinfo-format-TeX ()
  652.   (texinfo-parse-arg-discard)
  653.   (insert "TeX"))
  654.  
  655. (put 'copyright 'texinfo-format 'texinfo-format-copyright)
  656. (defun texinfo-format-copyright ()
  657.   (texinfo-parse-arg-discard)
  658.   (insert "(C)"))
  659.  
  660. (put 'minus 'texinfo-format 'texinfo-format-minus)
  661. (defun texinfo-format-minus ()
  662.   (texinfo-parse-arg-discard)
  663.   (insert "-"))
  664.  
  665. (put 'dots 'texinfo-format 'texinfo-format-dots)
  666. (defun texinfo-format-dots ()
  667.   (texinfo-parse-arg-discard)
  668.   (insert "..."))
  669.  
  670. (put 'refill 'texinfo-format 'texinfo-format-refill)
  671. (defun texinfo-format-refill ()
  672.   (texinfo-discard-command)
  673.   (fill-paragraph nil))
  674.  
  675. ;; Index generation
  676.  
  677. (put 'vindex 'texinfo-format 'texinfo-format-vindex)
  678. (defun texinfo-format-vindex ()
  679.   (texinfo-index 'texinfo-vindex))
  680.  
  681. (put 'cindex 'texinfo-format 'texinfo-format-cindex)
  682. (defun texinfo-format-cindex ()
  683.   (texinfo-index 'texinfo-cindex))
  684.  
  685. (put 'findex 'texinfo-format 'texinfo-format-findex)
  686. (defun texinfo-format-findex ()
  687.   (texinfo-index 'texinfo-findex))
  688.  
  689. (put 'pindex 'texinfo-format 'texinfo-format-pindex)
  690. (defun texinfo-format-pindex ()
  691.   (texinfo-index 'texinfo-pindex))
  692.  
  693. (put 'tindex 'texinfo-format 'texinfo-format-tindex)
  694. (defun texinfo-format-tindex ()
  695.   (texinfo-index 'texinfo-tindex))
  696.  
  697. (put 'kindex 'texinfo-format 'texinfo-format-kindex)
  698. (defun texinfo-format-kindex ()
  699.   (texinfo-index 'texinfo-kindex))
  700.  
  701. (defun texinfo-index (indexvar)
  702.   (set indexvar
  703.        (cons (list (texinfo-parse-arg-discard) texinfo-last-node)
  704.          (symbol-value indexvar))))
  705.  
  706. (defconst texinfo-indexvar-alist
  707.   '(("cp" . texinfo-cindex)
  708.     ("fn" . texinfo-findex)
  709.     ("vr" . texinfo-vindex)
  710.     ("tp" . texinfo-tindex)
  711.     ("pg" . texinfo-pindex)
  712.     ("ky" . texinfo-kindex)))
  713.  
  714. (put 'printindex 'texinfo-format 'texinfo-format-printindex)
  715. (defun texinfo-format-printindex ()
  716.   (let ((indexelts (symbol-value
  717.             (cdr (assoc (texinfo-parse-arg-discard)
  718.                 texinfo-indexvar-alist))))
  719.     opoint)
  720.     (insert "\n* Menu:\n\n")
  721.     (setq opoint (point))
  722.     (texinfo-print-index nil indexelts)
  723.     (shell-command-on-region opoint (point) "sort -f" 1)))
  724.  
  725. (defun texinfo-print-index (file indexelts)
  726.   (while indexelts
  727.     (if (stringp (car (car indexelts)))
  728.     (insert "* " (car (car indexelts))
  729.         ": " (if file (concat "(" file ")") "")
  730.         (nth 1 (car indexelts)) ".\n")
  731.       ;; index entries from @include'd file
  732.       (texinfo-print-index (nth 1 (car indexelts))
  733.                (nth 2 (car indexelts))))
  734.     (setq indexelts (cdr indexelts))))
  735.  
  736.  
  737. ;; process included files
  738. (put 'include 'texinfo-format 'texinfo-format-include)
  739. (defun texinfo-format-include ()
  740.   (let ((filename (texinfo-parse-arg-discard))
  741.     (default-directory input-directory)
  742.     subindex)
  743.     (setq subindex
  744.       (save-excursion
  745.         (progn (find-file
  746.             (cond ((file-readable-p (concat filename ".texinfo"))
  747.                (concat filename ".texinfo"))
  748.               ((file-readable-p (concat filename ".tex"))
  749.                (concat filename ".tex"))
  750.               ((file-readable-p filename)
  751.                filename)
  752.               (t (error "@include'd file %s not found"
  753.                     filename))))
  754.            (texinfo-format-buffer-1))))
  755.     (texinfo-subindex 'texinfo-vindex (car subindex) (nth 1 subindex))
  756.     (texinfo-subindex 'texinfo-findex (car subindex) (nth 2 subindex))
  757.     (texinfo-subindex 'texinfo-cindex (car subindex) (nth 3 subindex))
  758.     (texinfo-subindex 'texinfo-pindex (car subindex) (nth 4 subindex))
  759.     (texinfo-subindex 'texinfo-tindex (car subindex) (nth 5 subindex))
  760.     (texinfo-subindex 'texinfo-kindex (car subindex) (nth 6 subindex))))
  761.  
  762. (defun texinfo-subindex (indexvar file content)
  763.   (set indexvar (cons (list 'recurse file content)
  764.               (symbol-value indexvar))))
  765.  
  766.  
  767. ;; Lots of bolio constructs do nothing in texinfo.
  768.  
  769. (put 'page 'texinfo-format 'texinfo-discard-line-with-args)
  770. (put 'c 'texinfo-format 'texinfo-discard-line-with-args)
  771. (put 'comment 'texinfo-format 'texinfo-discard-line-with-args)
  772. (put 'setchapternewpage 'texinfo-format 'texinfo-discard-line-with-args)
  773. (put 'contents 'texinfo-format 'texinfo-discard-line-with-args)
  774. (put 'summarycontents 'texinfo-format 'texinfo-discard-line-with-args)
  775. (put 'nopara 'texinfo-format 'texinfo-discard-line-with-args)
  776. (put 'noindent 'texinfo-format 'texinfo-discard-line-with-args)
  777. (put 'setx 'texinfo-format 'texinfo-discard-line-with-args)
  778. (put 'setq 'texinfo-format 'texinfo-discard-line-with-args)
  779. (put 'settitle 'texinfo-format 'texinfo-discard-line-with-args)
  780. (put 'defindex 'texinfo-format 'texinfo-discard-line-with-args)
  781. (put 'synindex 'texinfo-format 'texinfo-discard-line-with-args)
  782. (put 'hsize 'texinfo-format 'texinfo-discard-line-with-args)
  783. (put 'parindent 'texinfo-format 'texinfo-discard-line-with-args)
  784. (put 'lispnarrowing 'texinfo-format 'texinfo-discard-line-with-args)
  785. (put 'itemindent 'texinfo-format 'texinfo-discard-line-with-args)
  786. (put 'headings 'texinfo-format 'texinfo-discard-line-with-args)
  787. (put 'group 'texinfo-format 'texinfo-discard-line-with-args)
  788. (put 'group 'texinfo-end 'texinfo-discard-line-with-args)
  789. (put 'bye 'texinfo-format 'texinfo-discard-line)
  790. (put 'smallbook 'texinfo-format 'texinfo-discard-line)
  791.  
  792. (defun texinfo-discard-line-with-args ()
  793.   (goto-char texinfo-command-start)
  794.   (delete-region (point) (progn (forward-line 1) (point))))
  795.  
  796. ;; Some cannot be handled
  797.  
  798. (defun texinfo-unsupported ()
  799.   (error "%s is not handled by texinfo"
  800.      (buffer-substring texinfo-command-start texinfo-command-end)))
  801.  
  802. (defun batch-texinfo-format ()
  803.   "Runs  texinfo-format-buffer  on the files remaining on the command line.
  804. Must be used only with -batch, and kills emacs on completion.
  805. Each file will be processed even if an error occurred previously.
  806. For example, invoke
  807.   \"emacs -batch -funcall batch-texinfo-format $docs/ ~/*.texinfo\"."
  808.   (if (not noninteractive)
  809.       (error "batch-texinfo-format may only be used -batch."))
  810.   (let ((version-control t)
  811.     (auto-save-default nil)
  812.     (find-file-run-dired nil)
  813.     (kept-old-versions 259259)
  814.     (kept-new-versions 259259))
  815.     (let ((error 0)
  816.       file
  817.       (files ()))
  818.       (while command-line-args-left
  819.     (setq file (expand-file-name (car command-line-args-left)))
  820.     (cond ((not (file-exists-p file))
  821.            (message ">> %s does not exist!" file)
  822.            (setq error 1
  823.              command-line-args-left (cdr command-line-args-left)))
  824.           ((file-directory-p file)
  825.            (setq command-line-args-left
  826.              (nconc (directory-files file)
  827.                 (cdr command-line-args-left))))
  828.           (t
  829.            (setq files (cons file files)
  830.              command-line-args-left (cdr command-line-args-left)))))
  831.       (while files
  832.     (setq file (car files)
  833.           files (cdr files))
  834.     (condition-case err
  835.         (progn
  836.           (if buffer-file-name (kill-buffer (current-buffer)))
  837.           (find-file file)
  838.           (buffer-flush-undo (current-buffer))
  839.           (set-buffer-modified-p nil)
  840.           (texinfo-mode)
  841.           (message "texinfo formatting %s..." file)
  842.           (texinfo-format-buffer nil)
  843.           (if (buffer-modified-p)
  844.           (progn (message "Saving modified %s" file)
  845.              (save-buffer))))
  846.       (error
  847.        (message ">> Error: %s" (prin1-to-string err))
  848.        (message ">>  point at")
  849.        (let ((s (buffer-substring (point)
  850.                       (min (+ (point) 100)
  851.                        (point-max))))
  852.          (tem 0))
  853.          (while (setq tem (string-match "\n+" s tem))
  854.            (setq s (concat (substring s 0 (match-beginning 0))
  855.                    "\n>>  "
  856.                    (substring s (match-end 0)))
  857.              tem (1+ tem)))
  858.          (message ">>  %s" s)))
  859.       (setq error 1)))
  860.       (kill-emacs error))))
  861.