Fix sectrioning errors in files.texi.
[emacs.git] / lisp / textmodes / makeinfo.el
blob3c7bb86ae8a81c924a6a7aea33a6f0a9fd3cd819
1 ;;; makeinfo.el --- run makeinfo conveniently
3 ;; Copyright (C) 1991, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 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, or (at your option)
15 ;; 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;;; The Texinfo mode `makeinfo' related commands are:
31 ;; makeinfo-region to run makeinfo on the current region.
32 ;; makeinfo-buffer to run makeinfo on the current buffer, or
33 ;; with optional prefix arg, on current region
34 ;; kill-compilation to kill currently running makeinfo job
35 ;; makeinfo-recenter-makeinfo-buffer to redisplay *compilation* buffer
37 ;;; Keybindings (defined in `texinfo.el')
39 ;; makeinfo bindings
40 ; (define-key texinfo-mode-map "\C-c\C-m\C-r" 'makeinfo-region)
41 ; (define-key texinfo-mode-map "\C-c\C-m\C-b" 'makeinfo-buffer)
42 ; (define-key texinfo-mode-map "\C-c\C-m\C-k" 'kill-compilation)
43 ; (define-key texinfo-mode-map "\C-c\C-m\C-l"
44 ; 'makeinfo-recenter-compilation-buffer)
46 ;;; Code:
48 ;;; Variables used by `makeinfo'
50 (require 'compile)
51 (require 'info)
53 (defvar tex-end-of-header)
54 (defvar tex-start-of-header)
57 (defgroup makeinfo nil
58 "Run makeinfo conveniently."
59 :group 'docs)
62 (defcustom makeinfo-run-command "makeinfo"
63 "*Command used to run `makeinfo' subjob.
64 The name of the file is appended to this string, separated by a space."
65 :type 'string
66 :group 'makeinfo)
68 (defcustom makeinfo-options "--fill-column=70"
69 "*String containing options for running `makeinfo'.
70 Do not include `--footnote-style' or `--paragraph-indent';
71 the proper way to specify those is with the Texinfo commands
72 `@footnotestyle` and `@paragraphindent'."
73 :type 'string
74 :group 'makeinfo)
76 (require 'texinfo)
78 (defvar makeinfo-compilation-process nil
79 "Process that runs `makeinfo'. Should start out nil.")
81 (defvar makeinfo-temp-file nil
82 "Temporary file name used for text being sent as input to `makeinfo'.")
84 (defvar makeinfo-output-file-name nil
85 "Info file name used for text output by `makeinfo'.")
87 (defvar makeinfo-output-node-name nil
88 "Node name to visit in output file, for `makeinfo-buffer'.")
91 ;;; The `makeinfo' function definitions
93 (defun makeinfo-region (region-beginning region-end)
94 "Make Info file from region of current Texinfo file, and switch to it.
96 This command does not offer the `next-error' feature since it would
97 apply to a temporary file, not the original; use the `makeinfo-buffer'
98 command to gain use of `next-error'."
100 (interactive "r")
101 (let (filename-or-header
102 filename-or-header-beginning
103 filename-or-header-end)
104 ;; Cannot use `let' for makeinfo-temp-file or
105 ;; makeinfo-output-file-name since `makeinfo-compilation-sentinel'
106 ;; needs them.
108 (setq makeinfo-temp-file
109 (concat
110 (make-temp-file
111 (substring (buffer-file-name)
113 (or (string-match "\\.tex" (buffer-file-name))
114 (length (buffer-file-name)))))
115 ".texinfo"))
117 (save-excursion
118 (save-restriction
119 (widen)
120 (goto-char (point-min))
121 (let ((search-end (save-excursion (forward-line 100) (point))))
122 ;; Find and record the Info filename,
123 ;; or else explain that a filename is needed.
124 (if (re-search-forward
125 "^@setfilename[ \t]+\\([^ \t\n]+\\)[ \t]*"
126 search-end t)
127 (setq makeinfo-output-file-name
128 (buffer-substring (match-beginning 1) (match-end 1)))
129 (error
130 "The texinfo file needs a line saying: @setfilename <name>"))
132 ;; Find header and specify its beginning and end.
133 (goto-char (point-min))
134 (if (and
135 (prog1
136 (search-forward tex-start-of-header search-end t)
137 (beginning-of-line)
138 ;; Mark beginning of header.
139 (setq filename-or-header-beginning (point)))
140 (prog1
141 (search-forward tex-end-of-header nil t)
142 (beginning-of-line)
143 ;; Mark end of header
144 (setq filename-or-header-end (point))))
146 ;; Insert the header into the temporary file.
147 (write-region
148 (min filename-or-header-beginning region-beginning)
149 filename-or-header-end
150 makeinfo-temp-file nil nil)
152 ;; Else no header; insert @filename line into temporary file.
153 (goto-char (point-min))
154 (search-forward "@setfilename" search-end t)
155 (beginning-of-line)
156 (setq filename-or-header-beginning (point))
157 (forward-line 1)
158 (setq filename-or-header-end (point))
159 (write-region
160 (min filename-or-header-beginning region-beginning)
161 filename-or-header-end
162 makeinfo-temp-file nil nil))
164 ;; Insert the region into the file.
165 (write-region
166 (max region-beginning filename-or-header-end)
167 region-end
168 makeinfo-temp-file t nil)
170 ;; Run the `makeinfo-compile' command in the *compilation* buffer
171 (save-excursion
172 (makeinfo-compile
173 (concat makeinfo-run-command
175 makeinfo-options
177 makeinfo-temp-file)
179 'makeinfo-compilation-sentinel-region)))))))
181 (defun makeinfo-next-error (arg reset)
182 "This function is used to disable `next-error' if the user has
183 used `makeinfo-region'. Since the compilation process is used on
184 a temporary file in that case, calling `next-error' would give
185 nonsensical results."
186 (error "Use `makeinfo-buffer' to gain use of the `next-error' command"))
188 ;; Actually run makeinfo. COMMAND is the command to run. If
189 ;; DISABLE-ERRORS is non-nil, disable `next-error' by setting
190 ;; `next-error-function' to `makeinfo-next-error' in the compilation
191 ;; buffer.
192 (defun makeinfo-compile (command disable-errors sentinel)
193 (let ((buffer (compilation-start command)))
194 (with-current-buffer buffer
195 (setq next-error-function
196 (if disable-errors
197 'makeinfo-next-error
198 'compilation-next-error-function)))
199 (set-process-sentinel (get-buffer-process buffer) sentinel)))
201 ;; Delete makeinfo-temp-file after processing is finished,
202 ;; and visit Info file.
203 ;; This function is called when the compilation process changes state.
204 ;; Based on `compilation-sentinel' in compile.el
205 (defun makeinfo-compilation-sentinel-region (proc msg)
206 "Sentinel for `makeinfo-compile' run from `makeinfo-region'."
207 (compilation-sentinel proc msg)
208 (when (memq (process-status proc) '(signal exit))
209 (if (file-exists-p makeinfo-temp-file)
210 (delete-file makeinfo-temp-file))
211 ;; Always use the version on disk.
212 (let ((buffer (get-file-buffer makeinfo-output-file-name)))
213 (if buffer
214 (with-current-buffer buffer
215 (revert-buffer t t))
216 (setq buffer (find-file-noselect makeinfo-output-file-name)))
217 (if (window-dedicated-p (selected-window))
218 (switch-to-buffer-other-window buffer)
219 (switch-to-buffer buffer)))
220 (goto-char (point-min))))
222 (defun makeinfo-current-node ()
223 "Return the name of the node containing point, in a texinfo file."
224 (save-excursion
225 (end-of-line) ; in case point is at the start of an @node line
226 (if (re-search-backward "^@node\\s-+\\([^,\n]+\\)" (point-min) t)
227 (match-string 1)
228 "Top")))
230 (defun makeinfo-buffer ()
231 "Make Info file from current buffer.
233 Use the \\[next-error] command to move to the next error
234 \(if there are errors\)."
236 (interactive)
237 (cond ((null buffer-file-name)
238 (error "Buffer not visiting any file"))
239 ((buffer-modified-p)
240 (if (y-or-n-p "Buffer modified; do you want to save it? ")
241 (save-buffer))))
243 ;; Find and record the Info filename,
244 ;; or else explain that a filename is needed.
245 (save-excursion
246 (goto-char (point-min))
247 (let ((search-end (save-excursion (forward-line 100) (point))))
248 (if (re-search-forward
249 "^@setfilename[ \t]+\\([^ \t\n]+\\)[ \t]*"
250 search-end t)
251 (setq makeinfo-output-file-name
252 (expand-file-name
253 (buffer-substring (match-beginning 1) (match-end 1))))
254 (error
255 "The texinfo file needs a line saying: @setfilename <name>"))))
256 (setq makeinfo-output-node-name (makeinfo-current-node))
258 (save-excursion
259 (makeinfo-compile
260 (concat makeinfo-run-command " " makeinfo-options
261 " " buffer-file-name)
263 'makeinfo-compilation-sentinel-buffer)))
265 (defun makeinfo-compilation-sentinel-buffer (proc msg)
266 "Sentinel for `makeinfo-compile' run from `makeinfo-buffer'."
267 (compilation-sentinel proc msg)
268 (when (memq (process-status proc) '(signal exit))
269 (when (file-exists-p makeinfo-output-file-name)
270 (Info-revert-find-node
271 makeinfo-output-file-name makeinfo-output-node-name))))
273 (defun makeinfo-recenter-compilation-buffer (linenum)
274 "Redisplay `*compilation*' buffer so most recent output can be seen.
275 The last line of the buffer is displayed on
276 line LINE of the window, or centered if LINE is nil."
277 (interactive "P")
278 (let ((makeinfo-buffer (get-buffer "*compilation*"))
279 (old-buffer (current-buffer)))
280 (if (null makeinfo-buffer)
281 (message "No *compilation* buffer")
282 (pop-to-buffer makeinfo-buffer)
283 (bury-buffer makeinfo-buffer)
284 (goto-char (point-max))
285 (recenter (if linenum
286 (prefix-numeric-value linenum)
287 (/ (window-height) 2)))
288 (pop-to-buffer old-buffer)
291 ;;; Place `provide' at end of file.
292 (provide 'makeinfo)
294 ;;; arch-tag: 5f810713-3de2-4e20-8030-4bc3dd0d9604
295 ;;; makeinfo.el ends here