scheme interaction mode
[bpt/emacs.git] / lisp / textmodes / makeinfo.el
CommitLineData
be010748 1;;; makeinfo.el --- run makeinfo conveniently
bab8cd70 2
ba318903 3;; Copyright (C) 1991, 1993, 2001-2014 Free Software Foundation, Inc.
bab8cd70 4
db95369b 5;; Author: Robert J. Chassell
34dc21db 6;; Maintainer: emacs-devel@gnu.org
6228c05b 7;; Keywords: docs convenience
be010748
RS
8
9;; This file is part of GNU Emacs.
bab8cd70 10
1fecc8fe 11;; GNU Emacs is free software: you can redistribute it and/or modify
bab8cd70 12;; it under the terms of the GNU General Public License as published by
1fecc8fe
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
bab8cd70
RS
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
1fecc8fe 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
bab8cd70
RS
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)
67f5dcb8 48(require 'info)
bab8cd70 49
c5309fd6
JB
50(defvar tex-end-of-header)
51(defvar tex-start-of-header)
52
53
bbf5eb28 54(defgroup makeinfo nil
a7253b5a 55 "Run makeinfo conveniently."
bbf5eb28
RS
56 :group 'docs)
57
58
59(defcustom makeinfo-run-command "makeinfo"
1fc7dabf 60 "Command used to run `makeinfo' subjob.
bbf5eb28
RS
61The name of the file is appended to this string, separated by a space."
62 :type 'string
63 :group 'makeinfo)
bab8cd70 64
bbf5eb28 65(defcustom makeinfo-options "--fill-column=70"
1fc7dabf 66 "String containing options for running `makeinfo'.
bab8cd70
RS
67Do not include `--footnote-style' or `--paragraph-indent';
68the proper way to specify those is with the Texinfo commands
bbf5eb28
RS
69`@footnotestyle` and `@paragraphindent'."
70 :type 'string
71 :group 'makeinfo)
bab8cd70
RS
72
73(require 'texinfo)
bab8cd70
RS
74
75(defvar makeinfo-compilation-process nil
76 "Process that runs `makeinfo'. Should start out nil.")
77
78(defvar makeinfo-temp-file nil
79 "Temporary file name used for text being sent as input to `makeinfo'.")
80
81(defvar makeinfo-output-file-name nil
82 "Info file name used for text output by `makeinfo'.")
83
67f5dcb8
RS
84(defvar makeinfo-output-node-name nil
85 "Node name to visit in output file, for `makeinfo-buffer'.")
86
bab8cd70
RS
87\f
88;;; The `makeinfo' function definitions
89
90(defun makeinfo-region (region-beginning region-end)
91 "Make Info file from region of current Texinfo file, and switch to it.
92
93This command does not offer the `next-error' feature since it would
94apply to a temporary file, not the original; use the `makeinfo-buffer'
95command to gain use of `next-error'."
db95369b 96
bab8cd70
RS
97 (interactive "r")
98 (let (filename-or-header
99 filename-or-header-beginning
100 filename-or-header-end)
101 ;; Cannot use `let' for makeinfo-temp-file or
102 ;; makeinfo-output-file-name since `makeinfo-compilation-sentinel'
103 ;; needs them.
104
105 (setq makeinfo-temp-file
106 (concat
767d12f2 107 (make-temp-file
bab8cd70 108 (substring (buffer-file-name)
db95369b
JB
109 0
110 (or (string-match "\\.tex" (buffer-file-name))
bab8cd70
RS
111 (length (buffer-file-name)))))
112 ".texinfo"))
db95369b 113
bab8cd70
RS
114 (save-excursion
115 (save-restriction
116 (widen)
117 (goto-char (point-min))
118 (let ((search-end (save-excursion (forward-line 100) (point))))
119 ;; Find and record the Info filename,
120 ;; or else explain that a filename is needed.
db95369b 121 (if (re-search-forward
bab8cd70
RS
122 "^@setfilename[ \t]+\\([^ \t\n]+\\)[ \t]*"
123 search-end t)
db95369b 124 (setq makeinfo-output-file-name
bab8cd70
RS
125 (buffer-substring (match-beginning 1) (match-end 1)))
126 (error
127 "The texinfo file needs a line saying: @setfilename <name>"))
128
129 ;; Find header and specify its beginning and end.
130 (goto-char (point-min))
db95369b
JB
131 (if (and
132 (prog1
86c57d9f 133 (search-forward tex-start-of-header search-end t)
bab8cd70
RS
134 (beginning-of-line)
135 ;; Mark beginning of header.
136 (setq filename-or-header-beginning (point)))
db95369b 137 (prog1
86c57d9f 138 (search-forward tex-end-of-header nil t)
bab8cd70
RS
139 (beginning-of-line)
140 ;; Mark end of header
141 (setq filename-or-header-end (point))))
db95369b 142
bab8cd70
RS
143 ;; Insert the header into the temporary file.
144 (write-region
145 (min filename-or-header-beginning region-beginning)
146 filename-or-header-end
147 makeinfo-temp-file nil nil)
db95369b 148
bab8cd70
RS
149 ;; Else no header; insert @filename line into temporary file.
150 (goto-char (point-min))
151 (search-forward "@setfilename" search-end t)
152 (beginning-of-line)
153 (setq filename-or-header-beginning (point))
154 (forward-line 1)
155 (setq filename-or-header-end (point))
156 (write-region
157 (min filename-or-header-beginning region-beginning)
158 filename-or-header-end
159 makeinfo-temp-file nil nil))
db95369b 160
bab8cd70
RS
161 ;; Insert the region into the file.
162 (write-region
163 (max region-beginning filename-or-header-end)
164 region-end
165 makeinfo-temp-file t nil)
166
167 ;; Run the `makeinfo-compile' command in the *compilation* buffer
168 (save-excursion
169 (makeinfo-compile
170 (concat makeinfo-run-command
171 " "
172 makeinfo-options
db95369b 173 " "
bab8cd70 174 makeinfo-temp-file)
713fbb79 175 t
67f5dcb8 176 'makeinfo-compilation-sentinel-region)))))))
0f99112d 177
713fbb79
RF
178(defun makeinfo-next-error (arg reset)
179 "This function is used to disable `next-error' if the user has
180used `makeinfo-region'. Since the compilation process is used on
181a temporary file in that case, calling `next-error' would give
182nonsensical results."
183 (error "Use `makeinfo-buffer' to gain use of the `next-error' command"))
184
185;; Actually run makeinfo. COMMAND is the command to run. If
186;; DISABLE-ERRORS is non-nil, disable `next-error' by setting
187;; `next-error-function' to `makeinfo-next-error' in the compilation
188;; buffer.
189(defun makeinfo-compile (command disable-errors sentinel)
190 (let ((buffer (compilation-start command)))
191 (with-current-buffer buffer
192 (setq next-error-function
193 (if disable-errors
194 'makeinfo-next-error
195 'compilation-next-error-function)))
67f5dcb8 196 (set-process-sentinel (get-buffer-process buffer) sentinel)))
0f99112d
RS
197
198;; Delete makeinfo-temp-file after processing is finished,
bab8cd70
RS
199;; and visit Info file.
200;; This function is called when the compilation process changes state.
201;; Based on `compilation-sentinel' in compile.el
67f5dcb8
RS
202(defun makeinfo-compilation-sentinel-region (proc msg)
203 "Sentinel for `makeinfo-compile' run from `makeinfo-region'."
0f99112d 204 (compilation-sentinel proc msg)
67f5dcb8
RS
205 (when (memq (process-status proc) '(signal exit))
206 (if (file-exists-p makeinfo-temp-file)
207 (delete-file makeinfo-temp-file))
208 ;; Always use the version on disk.
209 (let ((buffer (get-file-buffer makeinfo-output-file-name)))
210 (if buffer
211 (with-current-buffer buffer
212 (revert-buffer t t))
213 (setq buffer (find-file-noselect makeinfo-output-file-name)))
290d5b58 214 (if (window-dedicated-p)
67f5dcb8
RS
215 (switch-to-buffer-other-window buffer)
216 (switch-to-buffer buffer)))
217 (goto-char (point-min))))
218
219(defun makeinfo-current-node ()
220 "Return the name of the node containing point, in a texinfo file."
221 (save-excursion
222 (end-of-line) ; in case point is at the start of an @node line
223 (if (re-search-backward "^@node\\s-+\\([^,\n]+\\)" (point-min) t)
224 (match-string 1)
225 "Top")))
0f99112d
RS
226
227(defun makeinfo-buffer ()
bab8cd70
RS
228 "Make Info file from current buffer.
229
db95369b 230Use the \\[next-error] command to move to the next error
0f99112d 231\(if there are errors\)."
db95369b 232
0f99112d 233 (interactive)
bab8cd70 234 (cond ((null buffer-file-name)
0f99112d 235 (error "Buffer not visiting any file"))
bab8cd70
RS
236 ((buffer-modified-p)
237 (if (y-or-n-p "Buffer modified; do you want to save it? ")
238 (save-buffer))))
db95369b 239
bab8cd70
RS
240 ;; Find and record the Info filename,
241 ;; or else explain that a filename is needed.
242 (save-excursion
243 (goto-char (point-min))
244 (let ((search-end (save-excursion (forward-line 100) (point))))
db95369b 245 (if (re-search-forward
bab8cd70
RS
246 "^@setfilename[ \t]+\\([^ \t\n]+\\)[ \t]*"
247 search-end t)
db95369b 248 (setq makeinfo-output-file-name
67f5dcb8
RS
249 (expand-file-name
250 (buffer-substring (match-beginning 1) (match-end 1))))
bab8cd70
RS
251 (error
252 "The texinfo file needs a line saying: @setfilename <name>"))))
67f5dcb8 253 (setq makeinfo-output-node-name (makeinfo-current-node))
db95369b 254
bab8cd70
RS
255 (save-excursion
256 (makeinfo-compile
0f99112d 257 (concat makeinfo-run-command " " makeinfo-options
713fbb79
RF
258 " " buffer-file-name)
259 nil
67f5dcb8
RS
260 'makeinfo-compilation-sentinel-buffer)))
261
262(defun makeinfo-compilation-sentinel-buffer (proc msg)
263 "Sentinel for `makeinfo-compile' run from `makeinfo-buffer'."
264 (compilation-sentinel proc msg)
265 (when (memq (process-status proc) '(signal exit))
266 (when (file-exists-p makeinfo-output-file-name)
267 (Info-revert-find-node
268 makeinfo-output-file-name makeinfo-output-node-name))))
bab8cd70
RS
269
270(defun makeinfo-recenter-compilation-buffer (linenum)
271 "Redisplay `*compilation*' buffer so most recent output can be seen.
272The last line of the buffer is displayed on
273line LINE of the window, or centered if LINE is nil."
274 (interactive "P")
275 (let ((makeinfo-buffer (get-buffer "*compilation*"))
276 (old-buffer (current-buffer)))
277 (if (null makeinfo-buffer)
278 (message "No *compilation* buffer")
279 (pop-to-buffer makeinfo-buffer)
280 (bury-buffer makeinfo-buffer)
281 (goto-char (point-max))
282 (recenter (if linenum
283 (prefix-numeric-value linenum)
284 (/ (window-height) 2)))
285 (pop-to-buffer old-buffer)
286 )))
287
bab8cd70
RS
288;;; Place `provide' at end of file.
289(provide 'makeinfo)
290
291;;; makeinfo.el ends here