home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / docs.lha / latexinfo / elisp / latexnfo-tex.el < prev    next >
Encoding:
Text File  |  1991-12-02  |  14.4 KB  |  349 lines

  1. ;;;; latexnfo-tex.el
  2.  
  3. ;;; Latexinfo mode LaTeX and hardcopy printing commands.
  4.  
  5. ;; These commands are for running LaTeX on a region of a Latexinfo file in
  6. ;; GNU Emacs, or on the whole buffer, and for printing the resulting
  7. ;; DVI file.
  8.  
  9. ;;; Version 2.07    22 October 1991
  10. ;;; Robert J. Chassell      
  11. ;;; Converted to LaTeXinfo by Mike Clarkson
  12. ;;; Please send bug reports to:  mike@apl.ists.ca
  13.  
  14. ;;; Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
  15. ;;; Copyleft  (C) 1988, 1989, 1990, 1991 Michael E. Clarkson
  16.  
  17.  
  18. ;;; This file is part of GNU Emacs.
  19.  
  20. ;; GNU Emacs is free software; you can redistribute it and/or modify
  21. ;; it under the terms of the GNU General Public License as published by
  22. ;; the Free Software Foundation; either version 1, or (at your option)
  23. ;; any later version.
  24.  
  25. ;; GNU Emacs is distributed in the hope that it will be useful,
  26. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  28. ;; GNU General Public License for more details.
  29.  
  30. ;; You should have received a copy of the GNU General Public License
  31. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  32. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  33.  
  34.  
  35. ;;; The Latexinfo mode LaTeX related commands are:
  36.  
  37. ; latexinfo-latex-region        to run LaTeX on the current region.
  38. ; latexinfo-latex-buffer        to run LaTeX on the current buffer.
  39. ; latexinfo-latexindex          to sort unsorted index files.
  40. ; latexinfo-latex-print         to print the .dvi file made by LaTeX.
  41. ; latexinfo-kill-latex-job      to kill the currently running LaTeX job.
  42. ; latexinfo-recenter-latex-output-buffer    to redisplay LaTeX output buffer.
  43. ; latexinfo-show-latex-print-queue          to show the print queue.
  44.  
  45.  
  46. ;;; Keys common both to Latexinfo mode and to LaTeX shell.
  47.  
  48. ;; Defined in `latexinfo-mde.el'
  49. ; (defun latexinfo-define-common-keys (keymap)
  50. ;   "Define the keys both in Latexinfo mode and in the latexinfo-latex-shell."
  51. ;   (define-key keymap "\C-c\C-t\C-k"    'latexinfo-kill-latex-job)
  52. ;   (define-key keymap "\C-c\C-t\C-x"    'latexinfo-quit-latex-job)
  53. ;   (define-key keymap "\C-c\C-t\C-l"    'latexinfo-recenter-latex-output-buffer)
  54. ;   (define-key keymap "\C-c\C-t\C-d"    'latexinfo-delete-from-latex-print-queue)
  55. ;   (define-key keymap "\C-c\C-t\C-q"    'latexinfo-show-latex-print-queue)
  56. ;   (define-key keymap "\C-c\C-t\C-p"    'latexinfo-latex-print)
  57. ;   (define-key keymap "\C-c\C-t\C-i"    'latexinfo-latexindex)
  58. ;   (define-key keymap "\C-c\C-t\C-r"    'latexinfo-latex-region)
  59. ;   (define-key keymap "\C-c\C-t\C-b"    'latexinfo-latex-buffer))
  60.  
  61. ;; See also latexinfo-latex-start-shell. 
  62. ;; The following is executed in the `latexinfo.el' file
  63. ;(latexinfo-define-common-keys latexinfo-mode-map)
  64.  
  65.  
  66. ;;; Variable definitions:
  67.  
  68. (require 'shell)
  69.  
  70. (defvar latexinfo-latex-shell-cd-command "cd"
  71.   "Command to give to shell running LaTeX to change directory.")
  72.  
  73. (defvar latexinfo-latex-command "latex"
  74.   "*Command used by  latexinfo-latex-region  to run LaTeX on a region.")
  75.  
  76. (defvar latexinfo-latexindex-command "latexindex"
  77.   "*Command used by  latexinfo-latexindex  to sort unsorted index files.")
  78.  
  79. (defvar latexinfo-latex-dvi-print-command "lpr -d"
  80.   "*Command string used by \\[tex-print] to print a .dvi file.")
  81.  
  82. (defvar latexinfo-show-latex-queue-command "lpq"
  83.   "*Command string used to show the Latexinfo LaTeX print queue.
  84. Command is used by \\[latexinfo-show-latex-print-queue] and it
  85. should show the queue that \\[latexinfo-latex-print] puts jobs on.")
  86.  
  87. (defvar latexinfo-delete-from-print-queue-command "lprm"
  88.   "*Command string used to delete a job from the line printer queue.
  89. Command is used by \\[latexinfo-delete-from-latex-print-queue] based on
  90. number provided by a previous \\[latexinfo-show-latex-print-queue]
  91. command.")
  92.  
  93. (defvar latexinfo-latex-trailer "\\end{document}"
  94.   "String appended after a region sent to LaTeX by latexinfo-latex-region.")
  95.  
  96. (defvar latexinfo-latex-original-file ""
  97.   "Original name of file on which to run LaTeX.")
  98.  
  99. (defvar latexinfo-latex-temp-file nil
  100.   "Temporary file name used for text being sent as input to LaTeX.")
  101.  
  102. (defvar latexinfo-latex-root-temp-file nil
  103.   "Temporary file name used for text being sent as input to LaTeX.")
  104.  
  105.  
  106. ;;; Latexinfo LaTeX main functions
  107.  
  108. (defun latexinfo-latex-region (beginning end)
  109.   "Run LaTeX on the current region. 
  110.  
  111. A temporary file is written in the default directory, and LaTeX is run
  112. in that directory.  The first line of the file is copied to the
  113. temporary file; and if the buffer has a header, it is written to the
  114. temporary file before the region itself.  The buffer's header is all
  115. lines between the strings defined by latexinfo-start-of-header and
  116. latexinfo-end-of-header inclusive.  The header must start in the first 100
  117. lines.  The value of latexinfo-latex-trailer is appended to the temporary file
  118. after the region."
  119.   
  120.   (interactive "r")
  121.   (if (get-buffer "*latexinfo-latex-shell*")
  122.       (quit-process (get-process "latexinfo-latex-shell") t)
  123.     (latexinfo-latex-start-shell))
  124.   
  125.   (setq latexinfo-latex-root-temp-file
  126.         (expand-file-name 
  127.          (make-temp-name
  128.           (prin1-to-string (read (buffer-name))))))
  129.   
  130.   (let ((latexinfo-latex-temp-file (concat latexinfo-latex-root-temp-file ".tex")))
  131.     (save-excursion
  132.       (save-restriction
  133.         (widen)
  134.         (goto-char (point-min))
  135.         (forward-line 100)
  136.         (let ((search-end (point))
  137.               (header-beginning (point-min)) (header-end (point-min)))
  138.           (goto-char (point-min))
  139.           ;; Copy first line, the `\input latexinfo' line, to temp file
  140.           (write-region (point) 
  141.                         (save-excursion (forward-line 1) (point))
  142.                         latexinfo-latex-temp-file nil nil)
  143.           ;; Don't copy first line twice if region includes it.
  144.           (forward-line 1)
  145.           (if (< beginning  (point)) (setq beginning  (point)))
  146.           ;; Initialize the temp file with either the header or nothing
  147.           (if (search-forward latexinfo-start-of-header search-end t)
  148.               (progn
  149.                 (beginning-of-line)
  150.                 (setq header-beginning (point)) ; Mark beginning of header.
  151.                 (if (search-forward latexinfo-end-of-header nil t)
  152.                     (progn (forward-line 1)
  153.                            (setq header-end (point)))   ; Mark end of header.
  154.                   (setq header-beginning (point-min))))) ; Else no header.
  155.           ;; Copy  header  to temp file.
  156.           (write-region
  157.            (min header-beginning beginning )
  158.            header-end
  159.            latexinfo-latex-temp-file t nil)
  160.           ;; Copy  region  to temp file.
  161.           (write-region
  162.            (max beginning  header-end)
  163.            end
  164.            latexinfo-latex-temp-file t nil)
  165.           ;; This is a kludge to insert the latexinfo-latex-trailer into the
  166.           ;; latexinfo-latex-temp-file.  We have to create a special buffer
  167.           ;; in which to insert the latexinfo-latex-trailer first because there is
  168.           ;; no function with which to append a literal string directly
  169.           ;; to a file.
  170.           (let ((local-latex-trailer latexinfo-latex-trailer)
  171.                 (temp-buffer (get-buffer-create " latexinfo-trailer-buffer")))
  172.             (set-buffer temp-buffer)
  173.             (erase-buffer)
  174.             ;; make sure trailer isn't hidden by a comment
  175.             (insert-string "\n")
  176.             (if local-latex-trailer (insert local-latex-trailer))
  177.             (write-region (point-min) (point-max) 
  178.                           latexinfo-latex-temp-file t nil)))
  179.         (set-process-sentinel (get-process "latexinfo-latex-shell") 
  180.                               'latexinfo-latex-shell-sentinel)
  181.         (send-string "latexinfo-latex-shell"
  182.                      (concat latexinfo-latex-shell-cd-command " "
  183.                              default-directory "\n"))
  184.         (send-string "latexinfo-latex-shell"
  185.                      (concat latexinfo-latex-command " "
  186.                              latexinfo-latex-temp-file "\n"))
  187.         (latexinfo-recenter-latex-output-buffer 0)))))
  188.  
  189. (defun latexinfo-latex-buffer (buffer)
  190.   "Run LaTeX on current buffer.
  191. After running LaTeX the first time, you may have to run \\[latexinfo-latexindex]
  192. and then \\[latexinfo-latex-buffer] again."
  193.   (interactive 
  194.    (list
  195.     ;; Sometimes you put point into *latexinfo-latex-shell*; this prompts
  196.     ;; you for the correct file regardless.
  197.     (if (and 
  198.          (string= (buffer-name (current-buffer)) "*latexinfo-latex-shell*")
  199.          latexinfo-latex-root-temp-file)
  200.         (read-string (format "Run LaTeX on: ")
  201.                      latexinfo-latex-original-file)
  202.       (read-string (format "Run LaTeX on: ") (buffer-name (current-buffer))))))
  203.   
  204.   ;; Set to original buffer if in *latexinfo-texj-shell*; otherwise,
  205.   ;; record name of current buffer.
  206.   (if (string= (buffer-name (current-buffer)) "*latexinfo-latex-shell*")
  207.       (set-buffer buffer)
  208.     (setq latexinfo-latex-original-file
  209.            (buffer-name (current-buffer))))
  210.  
  211.   (if (get-buffer "*latexinfo-latex-shell*")
  212.       (quit-process (get-process "latexinfo-latex-shell") t)
  213.     (latexinfo-latex-start-shell))
  214.   (cond ((null buffer-file-name)
  215.          (error "Buffer not visiting any file!"))
  216.         ((buffer-modified-p)
  217.          (error "Buffer has been modified since last saved!"))
  218.         (t (set-process-sentinel (get-process "latexinfo-latex-shell") 
  219.                                  'latexinfo-latex-shell-sentinel)
  220.            (send-string "latexinfo-latex-shell"
  221.                         (concat latexinfo-latex-shell-cd-command 
  222.                                 " "
  223.                                 (file-name-directory
  224.                                  (buffer-file-name
  225.                                   (get-buffer buffer)))
  226.                                 "\n"))
  227.            (send-string "latexinfo-latex-shell"
  228.                         (concat latexinfo-latex-command " " buffer "\n"))
  229.            
  230.            ;; so the latexinfo-latex-print command works
  231.            (setq latexinfo-latex-root-temp-file
  232.                  (substring buffer 0
  233.                             (or (string-match "\\.tex" buffer)
  234.                                 (length buffer))))
  235.            
  236.            (latexinfo-recenter-latex-output-buffer 0))))
  237.  
  238. (defun latexinfo-latexindex ()
  239.   "Run latexindex on unsorted index files.
  240. The index files are made by \\[latexinfo-latex-region] or \\[latexinfo-latex-buffer].
  241. Runs the shell command defined by latexinfo-latexindex-command."
  242.   (interactive)
  243.   (send-string "latexinfo-latex-shell"
  244.                (concat latexinfo-latexindex-command
  245.                        " " latexinfo-latex-root-temp-file ".??" "\n"))
  246.   (latexinfo-recenter-latex-output-buffer nil))
  247.  
  248. (defun latexinfo-latex-print ()
  249.   "Print .dvi file made by \\[latexinfo-latex-region] or \\[latexinfo-latex-buffer].
  250. Runs the shell command defined by latexinfo-latex-dvi-print-command."
  251.   (interactive)
  252.   (send-string "latexinfo-latex-shell"
  253.                (concat latexinfo-latex-dvi-print-command
  254.                        " " latexinfo-latex-root-temp-file ".dvi" "\n"))
  255.   (latexinfo-recenter-latex-output-buffer nil))
  256.  
  257.  
  258. ;;; Latexinfo LaTeX utility functions
  259.  
  260. (defun latexinfo-latex-start-shell ()
  261.   (save-excursion
  262.     (if (not (fboundp 'latexinfo-mode)) (load "latexnfo-mde"))
  263.     (set-buffer (make-shell "latexinfo-latex-shell" "/bin/sh" nil "-v"))
  264.     (setq latexinfo-latex-shell-map (copy-keymap shell-mode-map))
  265.     (latexinfo-define-common-keys latexinfo-latex-shell-map)
  266.     (use-local-map latexinfo-latex-shell-map)
  267.     (run-hooks 'latexinfo-latex-shell-hook)
  268.     (if (zerop (buffer-size))
  269.         (sleep-for 1))))
  270.  
  271. (defun latexinfo-quit-latex-job ()
  272.   "Quit currently running LaTeX job, by sending an `x' to it."
  273.   (interactive)
  274.   (if (not (get-process "latexinfo-latex-shell"))
  275.       (error "No LaTeX shell running."))
  276.   (save-excursion
  277.     (set-buffer (get-buffer "*latexinfo-latex-shell*"))
  278.     (goto-char (point-max))
  279.     (insert "x")
  280.     (shell-send-input)))
  281.  
  282. (defun latexinfo-kill-latex-job ()
  283.   "Kill the currently running LaTeX job."
  284.   (interactive)
  285.   (if (get-process "latexinfo-latex-shell")
  286.         ;; Use `latexinfo-latex-shell-sentinel' to restart
  287.         ;; latexinfo-latex-shell after it is killed.
  288.         (kill-process (get-process "latexinfo-latex-shell"))))
  289.  
  290. (defun latexinfo-latex-shell-sentinel (process event)
  291.   "Restart latexinfo-latex-shell after it is killed."
  292.   (if (equal event "killed\n")
  293.       (save-excursion
  294.         (set-buffer  "*latexinfo-latex-shell*")
  295.         (insert "\n")
  296.         (latexinfo-latex-start-shell))))
  297.  
  298. (defun latexinfo-recenter-latex-output-buffer (linenum)
  299.   "Redisplay buffer of LaTeX job output so that most recent output can be seen.
  300. The last line of the buffer is displayed on
  301. line LINE of the window, or centered if LINE is nil."
  302.   (interactive "P")
  303.   (let ((latexinfo-latex-shell (get-buffer "*latexinfo-latex-shell*"))
  304.         (old-buffer (current-buffer)))
  305.     (if (null latexinfo-latex-shell)
  306.         (message "No LaTeX output buffer")
  307.       (pop-to-buffer latexinfo-latex-shell)
  308.       (bury-buffer latexinfo-latex-shell)
  309.       (goto-char (point-max))
  310.       (recenter (if linenum
  311.                     (prefix-numeric-value linenum)
  312.                   (/ (window-height) 2)))
  313.       (pop-to-buffer old-buffer)
  314.       )))
  315.  
  316. (defun latexinfo-show-latex-print-queue ()
  317.   "Show the print queue that \\[latexinfo-latex-print] put your job on.
  318. Runs the shell command defined by latexinfo-show-latex-queue-command."
  319.   (interactive)
  320.   (if (not (latexinfo-latex-shell-running-p))
  321.       (latexinfo-latex-start-shell))
  322.   (send-string "latexinfo-latex-shell"
  323.                (concat latexinfo-show-latex-queue-command "\n"))
  324.   (latexinfo-recenter-latex-output-buffer nil))
  325.  
  326. (defun latexinfo-delete-from-latex-print-queue (job-number)
  327.   "Delete job from the line printer spooling queue.
  328. You are prompted for the job number (shown by a previous
  329. \\[latexinfo-show-latex-print-queue] command."
  330.   (interactive "nPrinter job number for deletion: ")
  331.   (if (latexinfo-latex-shell-running-p)
  332.       (latexinfo-kill-latex-job)
  333.     (latexinfo-latex-start-shell))
  334.   (send-string "latexinfo-latex-shell"
  335.                (concat 
  336.                 latexinfo-delete-from-print-queue-command
  337.                 " "
  338.                 job-number"\n"))
  339.   (latexinfo-recenter-latex-output-buffer nil))
  340.  
  341. (defun latexinfo-latex-shell-running-p ()
  342.   (and (get-process "latexinfo-latex-shell")
  343.        (eq (process-status (get-process "latexinfo-latex-shell")) 'run)))
  344.  
  345.  
  346. ;;; Place `provide' at end of file.
  347. (provide 'latexnfo-tex)
  348. ;;;;;;;;;;;;;;;; end latexnfo-tex.el ;;;;;;;;;;;;;;;;
  349.