home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / tinfo210.lzh / TINFO210 / ELISP / MAKEINFO.EL < prev    next >
Lisp/Scheme  |  1991-04-05  |  11KB  |  311 lines

  1. ;;;; makeinfo.el
  2.  
  3. ;;;  Texinfo mode interface to the `makeinfo' command
  4. ;;;  for converting part or all of a Texinfo file to an Info file.
  5.  
  6. ;;; Version 2.03   2 April 1991
  7. ;;; Robert J. Chassell      
  8.  
  9. ;;; Copyright (C) 1991 Free Software Foundation, Inc.
  10.  
  11.  
  12. ;;; This file is part of GNU Emacs.
  13.  
  14. ;; GNU Emacs is free software; you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation; either version 1, or (at your option)
  17. ;; any later version.
  18.  
  19. ;; GNU Emacs is distributed in the hope that it will be useful,
  20. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. ;; GNU General Public License for more details.
  23.  
  24. ;; You should have received a copy of the GNU General Public License
  25. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  26. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  27.  
  28.  
  29. ;;; The Texinfo mode `makeinfo' related commands are:
  30.  
  31. ;; makeinfo-region      to run makeinfo on the current region.
  32. ;; makeinfo-buffer      to run makeinfo on the current buffer, or
  33. ;;                        with optional prefix arg, on current region
  34. ;; kill-compilation     to kill currently running makeinfo job
  35. ;; makeinfo-recenter-compilation-buffer  to redisplay *compilation* buffer
  36.  
  37.  
  38. ;;; Keybindings (defined in `texinfo.el')
  39.  
  40. ;; makeinfo bindings
  41. ; (define-key texinfo-mode-map "\C-c\C-m\C-r" 'makeinfo-region)
  42. ; (define-key texinfo-mode-map "\C-c\C-m\C-b" 'makeinfo-buffer)
  43. ; (define-key texinfo-mode-map "\C-c\C-m\C-k" 'kill-compilation)
  44. ; (define-key texinfo-mode-map "\C-c\C-m\C-l"
  45. ;   'makeinfo-recenter-compilation-buffer)
  46.  
  47.  
  48. ;;; Variables used by `makeinfo'
  49.  
  50. (require 'compile)
  51.  
  52. (defvar makeinfo-run-command "makeinfo"
  53.   "*Command used to run makeinfo subjob.
  54. The name of the file will be appended to this string, separated by a space.")
  55.  
  56. (defvar makeinfo-options "+fill-column=70"
  57.   "*String containing flags for  makeinfo.  
  58. Do not include footnote-style or paragraph-indent, which are specified
  59. by @footnotestyle and @paragraphindent.")
  60.  
  61. (require 'texinfo)
  62. (require 'texinfmt)
  63.  
  64. (defvar makeinfo-compilation-process nil
  65.   "Process that runs makeinfo. Should start out nil.")
  66.  
  67. (defvar makeinfo-temp-file nil
  68.   "Temporary file name used for text being sent as input to makeinfo.")
  69.  
  70. (defvar makeinfo-output-file-name nil
  71.   "Info file name used for text output by makeinfo.")
  72.  
  73.  
  74. ;;; The `makeinfo' function definitions
  75.  
  76. (autoload 'compile1 "compile")
  77.  
  78. (defun makeinfo-region (region-beginning region-end)
  79.   "Make Info file from region of current Texinfo file, and switch to it.
  80.  
  81. This command does not offer the `next-error' feature since it would
  82. apply to a temporary file, not the original; use the `makeinfo-buffer'
  83. command to gain use of `next-error'."
  84.   
  85.   (interactive "r")
  86.   (let (filename-or-header
  87.         filename-or-header-beginning
  88.         filename-or-header-end)
  89.     ;; Cannot use `let' for makeinfo-temp-file or
  90.     ;; makeinfo-output-file-name since `makeinfo-compilation-sentinel'
  91.     ;; needs them.
  92.  
  93.     (setq makeinfo-temp-file
  94.           (concat
  95.            (make-temp-name
  96.             (substring (buffer-file-name)
  97.                        0 
  98.                        (or (string-match "\\.tex" (buffer-file-name)) 
  99.                            (length (buffer-file-name)))))
  100.            ".texinfo"))
  101.     
  102.     (save-excursion
  103.       (save-restriction
  104.         (widen)
  105.         (goto-char (point-min))
  106.         (let ((search-end (save-excursion (forward-line 100) (point))))
  107.           ;; Find and record the Info filename,
  108.           ;; or else explain that a filename is needed.
  109.           (if (re-search-forward 
  110.                "^@setfilename[ \t]+\\([^ \t\n]+\\)[ \t]*"
  111.                search-end t)
  112.               (setq makeinfo-output-file-name 
  113.                     (buffer-substring (match-beginning 1) (match-end 1)))
  114.             (error
  115.              "The texinfo file needs a line saying: @setfilename <name>"))
  116.  
  117.           ;; Find header and specify its beginning and end.
  118.           (goto-char (point-min))
  119.           (if (and 
  120.                (prog1 
  121.                    (search-forward texinfo-start-of-header search-end t)
  122.                  (beginning-of-line)
  123.                  ;; Mark beginning of header.
  124.                  (setq filename-or-header-beginning (point)))
  125.                (prog1 
  126.                    (search-forward texinfo-end-of-header nil t)
  127.                  (beginning-of-line)
  128.                  ;; Mark end of header
  129.                  (setq filename-or-header-end (point))))
  130.               
  131.               ;; Insert the header into the temporary file.
  132.               (write-region
  133.                (min filename-or-header-beginning region-beginning)
  134.                filename-or-header-end
  135.                makeinfo-temp-file nil nil)
  136.             
  137.             ;; Else no header; insert @filename line into temporary file.
  138.             (goto-char (point-min))
  139.             (search-forward "@setfilename" search-end t)
  140.             (beginning-of-line)
  141.             (setq filename-or-header-beginning (point))
  142.             (forward-line 1)
  143.             (setq filename-or-header-end (point))
  144.             (write-region
  145.              (min filename-or-header-beginning region-beginning)
  146.              filename-or-header-end
  147.              makeinfo-temp-file nil nil))
  148.           
  149.           ;; Insert the region into the file.
  150.           (write-region
  151.            (max region-beginning filename-or-header-end)
  152.            region-end
  153.            makeinfo-temp-file t nil)
  154.  
  155.           ;; Run the `makeinfo-compile' command in the *compilation* buffer
  156.           (save-excursion
  157.             (makeinfo-compile
  158.              (concat makeinfo-run-command
  159.                      " "
  160.                      makeinfo-options
  161.                      " " 
  162.                      makeinfo-temp-file)
  163.              "Use `makeinfo-buffer' to gain use of the `next-error' command.")))))))
  164.  
  165. ;; Based on `compile1' in compile.el; changed so to make it possible
  166. ;; to delete temporary file.
  167. (defun makeinfo-compile (command error-message &optional name-of-mode)
  168.   (save-some-buffers)
  169.   (if makeinfo-compilation-process
  170.       (if (or (not (eq (process-status makeinfo-compilation-process) 'run))
  171.           (yes-or-no-p "A `makeinfo' compilation process is running; kill it? "))
  172.       (condition-case ()
  173.           (let ((comp-proc makeinfo-compilation-process))
  174.         (interrupt-process comp-proc)
  175.         (sit-for 1)
  176.         (delete-process comp-proc))
  177.         (error nil))
  178.     (error "Cannot have two compilation processes")))
  179.   (setq makeinfo-compilation-process nil)
  180.   (compilation-forget-errors)
  181.   (setq compilation-error-list t)
  182.   (setq compilation-error-message error-message)
  183.   (setq makeinfo-compilation-process
  184.     (start-process "compilation" "*compilation*"
  185.                shell-file-name
  186.                "-c" (concat "exec " command)))
  187.   (with-output-to-temp-buffer "*compilation*"
  188.     (princ "cd ")
  189.     (princ default-directory)
  190.     (terpri)
  191.     (princ command)
  192.     (terpri))
  193.   (let ((regexp compilation-error-regexp))
  194.     (save-excursion
  195.       (set-buffer "*compilation*")
  196.       (make-local-variable 'compilation-error-regexp)
  197.       (setq compilation-error-regexp regexp)))
  198.   (set-process-sentinel
  199.    makeinfo-compilation-process 'makeinfo-compilation-sentinel)
  200.   (let* ((thisdir default-directory)
  201.      (outbuf (process-buffer makeinfo-compilation-process))
  202.      (outwin (get-buffer-window outbuf)))
  203.     (if (eq outbuf (current-buffer))
  204.     (goto-char (point-max)))
  205.     (save-excursion
  206.       (set-buffer outbuf)
  207.       (buffer-flush-undo outbuf)
  208.       (let ((start (save-excursion (set-buffer outbuf) (point-min))))
  209.     (set-window-start outwin start)
  210.     (or (eq outwin (selected-window))
  211.         (set-window-point outwin start)))
  212.       (setq default-directory thisdir)
  213.       (fundamental-mode)
  214.       (setq mode-name (or name-of-mode "compilation"))
  215.       ;; Make log buffer's mode line show process state
  216.       (setq mode-line-process '(": %s")))))
  217.  
  218. ;; Delete makeinfo-temp-file after proccessing is finished,
  219. ;; and visit Info file.
  220. ;; This function is called when the compilation process changes state.
  221. ;; Based on `compilation-sentinel' in compile.el
  222. (defun makeinfo-compilation-sentinel (proc msg)
  223.   (cond ((null (buffer-name (process-buffer proc)))
  224.      ;; buffer killed
  225.      (set-process-buffer proc nil))
  226.     ((memq (process-status proc) '(signal exit))
  227.      (let* ((obuf (current-buffer))
  228.         omax opoint)
  229.        ;; save-excursion isn't the right thing if
  230.        ;;  process-buffer is current-buffer
  231.        (unwind-protect
  232.            (progn
  233.          ;; Write something in *compilation* and hack
  234.          ;; its mode line,
  235.          (set-buffer (process-buffer proc))
  236.          (setq omax (point-max) opoint (point))
  237.          (goto-char (point-max))
  238.          (insert ?\n mode-name " " msg)
  239.          (forward-char -1)
  240.          (insert " at "
  241.              (substring (current-time-string) 0 -5))
  242.          (forward-char 1)
  243.          (setq mode-line-process
  244.                (concat ": "
  245.                    (symbol-name (process-status proc))))
  246.          ;; If buffer and mode line will show that the process
  247.          ;; is dead, we can delete it now.  Otherwise it
  248.          ;; will stay around until M-x list-processes.
  249.          (delete-process proc))
  250.          (setq makeinfo-compilation-process nil)
  251.          ;; Force mode line redisplay soon
  252.          (set-buffer-modified-p (buffer-modified-p)))
  253.        (if (and opoint (< opoint omax))
  254.            (goto-char opoint))
  255.        (set-buffer obuf))
  256.          (if (and makeinfo-temp-file (file-exists-p makeinfo-temp-file))
  257.              (delete-file makeinfo-temp-file))
  258.          (if (and makeinfo-temp-file
  259.                   (get-file-buffer makeinfo-output-file-name))
  260.              (progn (set-buffer makeinfo-output-file-name)
  261.                     (revert-buffer t t)))
  262.          (if makeinfo-temp-file
  263.              (find-file makeinfo-output-file-name)))))
  264.  
  265. (defun makeinfo-buffer (buffer)
  266.   "Make Info file from current buffer.
  267.  
  268. The \\[next-error] command can be used to move to the next error 
  269. \(if any are found\)."
  270.   
  271.   (interactive "bRun `makeinfo' on: ")
  272.   (cond ((null buffer-file-name)
  273.          (error "Buffer not visiting any file!"))
  274.         ((buffer-modified-p)
  275.          (error "Buffer has been modified since last saved!"))
  276.         (t (save-excursion
  277.              (makeinfo-compile
  278.               (concat makeinfo-run-command
  279.                       " " 
  280.                       makeinfo-options
  281.                       " " 
  282.                       "+footnote-style="
  283.                       texinfo-footnote-style
  284.                       " "
  285.                       (buffer-file-name
  286.                        (get-buffer buffer)))
  287.               "No more errors.")))))
  288.  
  289. (defun makeinfo-recenter-compilation-buffer (linenum)
  290.   "Redisplay *compilation* buffer so that most recent output can be seen.
  291. The last line of the buffer is displayed on
  292. line LINE of the window, or centered if LINE is nil."
  293.   (interactive "P")
  294.   (let ((compilation-buffer (get-buffer "*compilation*"))
  295.     (old-buffer (current-buffer)))
  296.     (if (null compilation-buffer)
  297.     (message "No *compilation* buffer")
  298.       (pop-to-buffer compilation-buffer)
  299.       (bury-buffer compilation-buffer)
  300.       (goto-char (point-max))
  301.       (recenter (if linenum
  302.             (prefix-numeric-value linenum)
  303.           (/ (window-height) 2)))
  304.       (pop-to-buffer old-buffer)
  305.       )))
  306.  
  307.  
  308. ;;; Place `provide' at end of file.
  309. (provide 'makeinfo)
  310. ;;;;;;;;;;;;;;;; end makeinfo.el ;;;;;;;;;;;;;;;;
  311.