Comment change.
[bpt/emacs.git] / lisp / textmodes / makeinfo.el
1 ;;; makeinfo.el --- run makeinfo conveniently
2
3 ;; Copyright (C) 1991, 1993 Free Software Foundation, Inc.
4
5 ;; Author: Robert J. Chassell
6 ;; Maintainer: FSF
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;;; The Texinfo mode `makeinfo' related commands are:
27
28 ;; makeinfo-region to run makeinfo on the current region.
29 ;; makeinfo-buffer to run makeinfo on the current buffer, or
30 ;; with optional prefix arg, on current region
31 ;; kill-compilation to kill currently running makeinfo job
32 ;; makeinfo-recenter-makeinfo-buffer to redisplay *compilation* buffer
33
34 ;;; Keybindings (defined in `texinfo.el')
35
36 ;; makeinfo bindings
37 ; (define-key texinfo-mode-map "\C-c\C-m\C-r" 'makeinfo-region)
38 ; (define-key texinfo-mode-map "\C-c\C-m\C-b" 'makeinfo-buffer)
39 ; (define-key texinfo-mode-map "\C-c\C-m\C-k" 'kill-compilation)
40 ; (define-key texinfo-mode-map "\C-c\C-m\C-l"
41 ; 'makeinfo-recenter-compilation-buffer)
42 \f
43 ;;; Code:
44
45 ;;; Variables used by `makeinfo'
46
47 (require 'compile)
48
49 (defvar makeinfo-run-command "makeinfo"
50 "*Command used to run `makeinfo' subjob.
51 The name of the file is appended to this string, separated by a space.")
52
53 (defvar makeinfo-options "--fill-column=70"
54 "*String containing options for running `makeinfo'.
55 Do not include `--footnote-style' or `--paragraph-indent';
56 the proper way to specify those is with the Texinfo commands
57 `@footnotestyle` and `@paragraphindent'.")
58
59 (require 'texinfo)
60
61 (defvar makeinfo-compilation-process nil
62 "Process that runs `makeinfo'. Should start out nil.")
63
64 (defvar makeinfo-temp-file nil
65 "Temporary file name used for text being sent as input to `makeinfo'.")
66
67 (defvar makeinfo-output-file-name nil
68 "Info file name used for text output by `makeinfo'.")
69
70 \f
71 ;;; The `makeinfo' function definitions
72
73 (defun makeinfo-region (region-beginning region-end)
74 "Make Info file from region of current Texinfo file, and switch to it.
75
76 This command does not offer the `next-error' feature since it would
77 apply to a temporary file, not the original; use the `makeinfo-buffer'
78 command to gain use of `next-error'."
79
80 (interactive "r")
81 (let (filename-or-header
82 filename-or-header-beginning
83 filename-or-header-end)
84 ;; Cannot use `let' for makeinfo-temp-file or
85 ;; makeinfo-output-file-name since `makeinfo-compilation-sentinel'
86 ;; needs them.
87
88 (setq makeinfo-temp-file
89 (concat
90 (make-temp-name
91 (substring (buffer-file-name)
92 0
93 (or (string-match "\\.tex" (buffer-file-name))
94 (length (buffer-file-name)))))
95 ".texinfo"))
96
97 (save-excursion
98 (save-restriction
99 (widen)
100 (goto-char (point-min))
101 (let ((search-end (save-excursion (forward-line 100) (point))))
102 ;; Find and record the Info filename,
103 ;; or else explain that a filename is needed.
104 (if (re-search-forward
105 "^@setfilename[ \t]+\\([^ \t\n]+\\)[ \t]*"
106 search-end t)
107 (setq makeinfo-output-file-name
108 (buffer-substring (match-beginning 1) (match-end 1)))
109 (error
110 "The texinfo file needs a line saying: @setfilename <name>"))
111
112 ;; Find header and specify its beginning and end.
113 (goto-char (point-min))
114 (if (and
115 (prog1
116 (search-forward tex-start-of-header search-end t)
117 (beginning-of-line)
118 ;; Mark beginning of header.
119 (setq filename-or-header-beginning (point)))
120 (prog1
121 (search-forward tex-end-of-header nil t)
122 (beginning-of-line)
123 ;; Mark end of header
124 (setq filename-or-header-end (point))))
125
126 ;; Insert the header into the temporary file.
127 (write-region
128 (min filename-or-header-beginning region-beginning)
129 filename-or-header-end
130 makeinfo-temp-file nil nil)
131
132 ;; Else no header; insert @filename line into temporary file.
133 (goto-char (point-min))
134 (search-forward "@setfilename" search-end t)
135 (beginning-of-line)
136 (setq filename-or-header-beginning (point))
137 (forward-line 1)
138 (setq filename-or-header-end (point))
139 (write-region
140 (min filename-or-header-beginning region-beginning)
141 filename-or-header-end
142 makeinfo-temp-file nil nil))
143
144 ;; Insert the region into the file.
145 (write-region
146 (max region-beginning filename-or-header-end)
147 region-end
148 makeinfo-temp-file t nil)
149
150 ;; Run the `makeinfo-compile' command in the *compilation* buffer
151 (save-excursion
152 (makeinfo-compile
153 (concat makeinfo-run-command
154 " "
155 makeinfo-options
156 " "
157 makeinfo-temp-file)
158 "Use `makeinfo-buffer' to gain use of the `next-error' command"
159 nil)))))))
160
161 ;;; Actually run makeinfo. COMMAND is the command to run.
162 ;;; ERROR-MESSAGE is what to say when next-error can't find another error.
163 ;;; If PARSE-ERRORS is non-nil, do try to parse error messages.
164 (defun makeinfo-compile (command error-message parse-errors)
165 (let ((buffer
166 (compile-internal command error-message nil
167 (and (not parse-errors)
168 ;; If we do want to parse errors, pass nil.
169 ;; Otherwise, use this function, which won't
170 ;; ever find any errors.
171 '(lambda (&rest ignore)
172 (setq compilation-error-list nil))))))
173 (set-process-sentinel (get-buffer-process buffer)
174 'makeinfo-compilation-sentinel)))
175
176 ;; Delete makeinfo-temp-file after processing is finished,
177 ;; and visit Info file.
178 ;; This function is called when the compilation process changes state.
179 ;; Based on `compilation-sentinel' in compile.el
180 (defun makeinfo-compilation-sentinel (proc msg)
181 (compilation-sentinel proc msg)
182 (if (and makeinfo-temp-file (file-exists-p makeinfo-temp-file))
183 (delete-file makeinfo-temp-file))
184 ;; Always use the version on disk.
185 (if (get-file-buffer makeinfo-output-file-name)
186 (progn (set-buffer makeinfo-output-file-name)
187 (revert-buffer t t))
188 (find-file makeinfo-output-file-name))
189 (goto-char (point-min)))
190
191 (defun makeinfo-buffer ()
192 "Make Info file from current buffer.
193
194 Use the \\[next-error] command to move to the next error
195 \(if there are errors\)."
196
197 (interactive)
198 (cond ((null buffer-file-name)
199 (error "Buffer not visiting any file"))
200 ((buffer-modified-p)
201 (if (y-or-n-p "Buffer modified; do you want to save it? ")
202 (save-buffer))))
203
204 ;; Find and record the Info filename,
205 ;; or else explain that a filename is needed.
206 (save-excursion
207 (goto-char (point-min))
208 (let ((search-end (save-excursion (forward-line 100) (point))))
209 (if (re-search-forward
210 "^@setfilename[ \t]+\\([^ \t\n]+\\)[ \t]*"
211 search-end t)
212 (setq makeinfo-output-file-name
213 (buffer-substring (match-beginning 1) (match-end 1)))
214 (error
215 "The texinfo file needs a line saying: @setfilename <name>"))))
216
217 (save-excursion
218 (makeinfo-compile
219 (concat makeinfo-run-command " " makeinfo-options
220 " " buffer-file-name)
221 "No more errors."
222 t)))
223
224 (defun makeinfo-recenter-compilation-buffer (linenum)
225 "Redisplay `*compilation*' buffer so most recent output can be seen.
226 The last line of the buffer is displayed on
227 line LINE of the window, or centered if LINE is nil."
228 (interactive "P")
229 (let ((makeinfo-buffer (get-buffer "*compilation*"))
230 (old-buffer (current-buffer)))
231 (if (null makeinfo-buffer)
232 (message "No *compilation* buffer")
233 (pop-to-buffer makeinfo-buffer)
234 (bury-buffer makeinfo-buffer)
235 (goto-char (point-max))
236 (recenter (if linenum
237 (prefix-numeric-value linenum)
238 (/ (window-height) 2)))
239 (pop-to-buffer old-buffer)
240 )))
241
242 ;;; Place `provide' at end of file.
243 (provide 'makeinfo)
244
245 ;;; makeinfo.el ends here
246