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