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
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/>.
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')
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)
46 ;;; Variables used by `makeinfo'
51 (defvar tex-end-of-header
)
52 (defvar tex-start-of-header
)
55 (defgroup makeinfo nil
56 "Run makeinfo conveniently."
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."
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'."
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'."
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'
106 (setq makeinfo-temp-file
109 (substring (buffer-file-name)
111 (or (string-match "\\.tex" (buffer-file-name))
112 (length (buffer-file-name)))))
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]*"
125 (setq makeinfo-output-file-name
126 (buffer-substring (match-beginning 1) (match-end 1)))
128 "The texinfo file needs a line saying: @setfilename <name>"))
130 ;; Find header and specify its beginning and end.
131 (goto-char (point-min))
134 (search-forward tex-start-of-header search-end t
)
136 ;; Mark beginning of header.
137 (setq filename-or-header-beginning
(point)))
139 (search-forward tex-end-of-header nil t
)
141 ;; Mark end of header
142 (setq filename-or-header-end
(point))))
144 ;; Insert the header into the temporary file.
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
)
154 (setq filename-or-header-beginning
(point))
156 (setq filename-or-header-end
(point))
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.
164 (max region-beginning filename-or-header-end
)
166 makeinfo-temp-file t nil
)
168 ;; Run the `makeinfo-compile' command in the *compilation* buffer
171 (concat makeinfo-run-command
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
190 (defun makeinfo-compile (command disable-errors sentinel
)
191 (let ((buffer (compilation-start command
)))
192 (with-current-buffer buffer
193 (setq next-error-function
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
)))
212 (with-current-buffer buffer
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."
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
)
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\)."
235 (cond ((null buffer-file-name
)
236 (error "Buffer not visiting any file"))
238 (if (y-or-n-p "Buffer modified; do you want to save it? ")
241 ;; Find and record the Info filename,
242 ;; or else explain that a filename is needed.
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]*"
249 (setq makeinfo-output-file-name
251 (buffer-substring (match-beginning 1) (match-end 1))))
253 "The texinfo file needs a line saying: @setfilename <name>"))))
254 (setq makeinfo-output-node-name
(makeinfo-current-node))
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."
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.
292 ;; arch-tag: 5f810713-3de2-4e20-8030-4bc3dd0d9604
293 ;;; makeinfo.el ends here