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