* progmodes/meta-mode.el (meta-complete-symbol):
[emacs.git] / lisp / textmodes / makeinfo.el
blob23cf937a60aa775ef444e50758422060415aef9f
1 ;;; makeinfo.el --- run makeinfo conveniently
3 ;; Copyright (C) 1991, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Robert J. Chassell
7 ;; Maintainer: FSF
8 ;; Keywords: docs convenience
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; The Texinfo mode `makeinfo' related commands are:
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
35 ;;; Keybindings (defined in `texinfo.el')
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)
44 ;;; Code:
46 ;;; Variables used by `makeinfo'
48 (require 'compile)
49 (require 'info)
51 (defvar tex-end-of-header)
52 (defvar tex-start-of-header)
55 (defgroup makeinfo nil
56 "Run makeinfo conveniently."
57 :group 'docs)
60 (defcustom makeinfo-run-command "makeinfo"
61 "*Command used to run `makeinfo' subjob.
62 The name of the file is appended to this string, separated by a space."
63 :type 'string
64 :group 'makeinfo)
66 (defcustom makeinfo-options "--fill-column=70"
67 "*String containing options for running `makeinfo'.
68 Do not include `--footnote-style' or `--paragraph-indent';
69 the proper way to specify those is with the Texinfo commands
70 `@footnotestyle` and `@paragraphindent'."
71 :type 'string
72 :group 'makeinfo)
74 (require 'texinfo)
76 (defvar makeinfo-compilation-process nil
77 "Process that runs `makeinfo'. Should start out nil.")
79 (defvar makeinfo-temp-file nil
80 "Temporary file name used for text being sent as input to `makeinfo'.")
82 (defvar makeinfo-output-file-name nil
83 "Info file name used for text output by `makeinfo'.")
85 (defvar makeinfo-output-node-name nil
86 "Node name to visit in output file, for `makeinfo-buffer'.")
89 ;;; The `makeinfo' function definitions
91 (defun makeinfo-region (region-beginning region-end)
92 "Make Info file from region of current Texinfo file, and switch to it.
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'."
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.
106 (setq makeinfo-temp-file
107 (concat
108 (make-temp-file
109 (substring (buffer-file-name)
111 (or (string-match "\\.tex" (buffer-file-name))
112 (length (buffer-file-name)))))
113 ".texinfo"))
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.
122 (if (re-search-forward
123 "^@setfilename[ \t]+\\([^ \t\n]+\\)[ \t]*"
124 search-end t)
125 (setq makeinfo-output-file-name
126 (buffer-substring (match-beginning 1) (match-end 1)))
127 (error
128 "The texinfo file needs a line saying: @setfilename <name>"))
130 ;; Find header and specify its beginning and end.
131 (goto-char (point-min))
132 (if (and
133 (prog1
134 (search-forward tex-start-of-header search-end t)
135 (beginning-of-line)
136 ;; Mark beginning of header.
137 (setq filename-or-header-beginning (point)))
138 (prog1
139 (search-forward tex-end-of-header nil t)
140 (beginning-of-line)
141 ;; Mark end of header
142 (setq filename-or-header-end (point))))
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)
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))
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)
168 ;; Run the `makeinfo-compile' command in the *compilation* buffer
169 (save-excursion
170 (makeinfo-compile
171 (concat makeinfo-run-command
173 makeinfo-options
175 makeinfo-temp-file)
177 'makeinfo-compilation-sentinel-region)))))))
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"))
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)))
197 (set-process-sentinel (get-buffer-process buffer) sentinel)))
199 ;; Delete makeinfo-temp-file after processing is finished,
200 ;; and visit Info file.
201 ;; This function is called when the compilation process changes state.
202 ;; Based on `compilation-sentinel' in compile.el
203 (defun makeinfo-compilation-sentinel-region (proc msg)
204 "Sentinel for `makeinfo-compile' run from `makeinfo-region'."
205 (compilation-sentinel proc msg)
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))))
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")))
228 (defun makeinfo-buffer ()
229 "Make Info file from current buffer.
231 Use the \\[next-error] command to move to the next error
232 \(if there are errors\)."
234 (interactive)
235 (cond ((null buffer-file-name)
236 (error "Buffer not visiting any file"))
237 ((buffer-modified-p)
238 (if (y-or-n-p "Buffer modified; do you want to save it? ")
239 (save-buffer))))
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))))
246 (if (re-search-forward
247 "^@setfilename[ \t]+\\([^ \t\n]+\\)[ \t]*"
248 search-end t)
249 (setq makeinfo-output-file-name
250 (expand-file-name
251 (buffer-substring (match-beginning 1) (match-end 1))))
252 (error
253 "The texinfo file needs a line saying: @setfilename <name>"))))
254 (setq makeinfo-output-node-name (makeinfo-current-node))
256 (save-excursion
257 (makeinfo-compile
258 (concat makeinfo-run-command " " makeinfo-options
259 " " buffer-file-name)
261 'makeinfo-compilation-sentinel-buffer)))
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))))
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)
289 ;;; Place `provide' at end of file.
290 (provide 'makeinfo)
292 ;; arch-tag: 5f810713-3de2-4e20-8030-4bc3dd0d9604
293 ;;; makeinfo.el ends here