(exec-suffixes): Initialize to a system-dependent value.
[bpt/emacs.git] / lisp / textmodes / makeinfo.el
CommitLineData
be010748 1;;; makeinfo.el --- run makeinfo conveniently
bab8cd70 2
be010748 3;; Copyright (C) 1991, 1993 Free Software Foundation, Inc.
bab8cd70 4
be010748
RS
5;; Author: Robert J. Chassell
6;; Maintainer: FSF
6228c05b 7;; Keywords: docs convenience
be010748
RS
8
9;; This file is part of GNU Emacs.
bab8cd70
RS
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
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
b578f267
EN
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
bab8cd70
RS
25
26;;; Commentary:
27
28;;; The Texinfo mode `makeinfo' related commands are:
29
30;; makeinfo-region to run makeinfo on the current region.
31;; makeinfo-buffer to run makeinfo on the current buffer, or
32;; with optional prefix arg, on current region
33;; kill-compilation to kill currently running makeinfo job
34;; makeinfo-recenter-makeinfo-buffer to redisplay *compilation* buffer
35
36;;; Keybindings (defined in `texinfo.el')
37
38;; makeinfo bindings
39; (define-key texinfo-mode-map "\C-c\C-m\C-r" 'makeinfo-region)
40; (define-key texinfo-mode-map "\C-c\C-m\C-b" 'makeinfo-buffer)
41; (define-key texinfo-mode-map "\C-c\C-m\C-k" 'kill-compilation)
42; (define-key texinfo-mode-map "\C-c\C-m\C-l"
43; 'makeinfo-recenter-compilation-buffer)
44\f
45;;; Code:
46
47;;; Variables used by `makeinfo'
48
49(require 'compile)
50
bbf5eb28
RS
51(defgroup makeinfo nil
52 "Run makeinfo conveniently"
53 :group 'docs)
54
55
56(defcustom makeinfo-run-command "makeinfo"
bab8cd70 57 "*Command used to run `makeinfo' subjob.
bbf5eb28
RS
58The name of the file is appended to this string, separated by a space."
59 :type 'string
60 :group 'makeinfo)
bab8cd70 61
bbf5eb28 62(defcustom makeinfo-options "--fill-column=70"
bab8cd70
RS
63 "*String containing options for running `makeinfo'.
64Do not include `--footnote-style' or `--paragraph-indent';
65the proper way to specify those is with the Texinfo commands
bbf5eb28
RS
66`@footnotestyle` and `@paragraphindent'."
67 :type 'string
68 :group 'makeinfo)
bab8cd70
RS
69
70(require 'texinfo)
bab8cd70
RS
71
72(defvar makeinfo-compilation-process nil
73 "Process that runs `makeinfo'. Should start out nil.")
74
75(defvar makeinfo-temp-file nil
76 "Temporary file name used for text being sent as input to `makeinfo'.")
77
78(defvar makeinfo-output-file-name nil
79 "Info file name used for text output by `makeinfo'.")
80
81\f
82;;; The `makeinfo' function definitions
83
84(defun makeinfo-region (region-beginning region-end)
85 "Make Info file from region of current Texinfo file, and switch to it.
86
87This command does not offer the `next-error' feature since it would
88apply to a temporary file, not the original; use the `makeinfo-buffer'
89command to gain use of `next-error'."
90
91 (interactive "r")
92 (let (filename-or-header
93 filename-or-header-beginning
94 filename-or-header-end)
95 ;; Cannot use `let' for makeinfo-temp-file or
96 ;; makeinfo-output-file-name since `makeinfo-compilation-sentinel'
97 ;; needs them.
98
99 (setq makeinfo-temp-file
100 (concat
767d12f2 101 (make-temp-file
bab8cd70
RS
102 (substring (buffer-file-name)
103 0
104 (or (string-match "\\.tex" (buffer-file-name))
105 (length (buffer-file-name)))))
106 ".texinfo"))
107
108 (save-excursion
109 (save-restriction
110 (widen)
111 (goto-char (point-min))
112 (let ((search-end (save-excursion (forward-line 100) (point))))
113 ;; Find and record the Info filename,
114 ;; or else explain that a filename is needed.
115 (if (re-search-forward
116 "^@setfilename[ \t]+\\([^ \t\n]+\\)[ \t]*"
117 search-end t)
118 (setq makeinfo-output-file-name
119 (buffer-substring (match-beginning 1) (match-end 1)))
120 (error
121 "The texinfo file needs a line saying: @setfilename <name>"))
122
123 ;; Find header and specify its beginning and end.
124 (goto-char (point-min))
125 (if (and
126 (prog1
86c57d9f 127 (search-forward tex-start-of-header search-end t)
bab8cd70
RS
128 (beginning-of-line)
129 ;; Mark beginning of header.
130 (setq filename-or-header-beginning (point)))
131 (prog1
86c57d9f 132 (search-forward tex-end-of-header nil t)
bab8cd70
RS
133 (beginning-of-line)
134 ;; Mark end of header
135 (setq filename-or-header-end (point))))
136
137 ;; Insert the header into the temporary file.
138 (write-region
139 (min filename-or-header-beginning region-beginning)
140 filename-or-header-end
141 makeinfo-temp-file nil nil)
142
143 ;; Else no header; insert @filename line into temporary file.
144 (goto-char (point-min))
145 (search-forward "@setfilename" search-end t)
146 (beginning-of-line)
147 (setq filename-or-header-beginning (point))
148 (forward-line 1)
149 (setq filename-or-header-end (point))
150 (write-region
151 (min filename-or-header-beginning region-beginning)
152 filename-or-header-end
153 makeinfo-temp-file nil nil))
154
155 ;; Insert the region into the file.
156 (write-region
157 (max region-beginning filename-or-header-end)
158 region-end
159 makeinfo-temp-file t nil)
160
161 ;; Run the `makeinfo-compile' command in the *compilation* buffer
162 (save-excursion
163 (makeinfo-compile
164 (concat makeinfo-run-command
165 " "
166 makeinfo-options
167 " "
168 makeinfo-temp-file)
0f99112d
RS
169 "Use `makeinfo-buffer' to gain use of the `next-error' command"
170 nil)))))))
171
172;;; Actually run makeinfo. COMMAND is the command to run.
173;;; ERROR-MESSAGE is what to say when next-error can't find another error.
174;;; If PARSE-ERRORS is non-nil, do try to parse error messages.
175(defun makeinfo-compile (command error-message parse-errors)
176 (let ((buffer
177 (compile-internal command error-message nil
178 (and (not parse-errors)
179 ;; If we do want to parse errors, pass nil.
180 ;; Otherwise, use this function, which won't
181 ;; ever find any errors.
b94deab8
SM
182 (lambda (&rest ignore)
183 (setq compilation-error-list nil))))))
0f99112d
RS
184 (set-process-sentinel (get-buffer-process buffer)
185 'makeinfo-compilation-sentinel)))
186
187;; Delete makeinfo-temp-file after processing is finished,
bab8cd70
RS
188;; and visit Info file.
189;; This function is called when the compilation process changes state.
190;; Based on `compilation-sentinel' in compile.el
191(defun makeinfo-compilation-sentinel (proc msg)
0f99112d
RS
192 (compilation-sentinel proc msg)
193 (if (and makeinfo-temp-file (file-exists-p makeinfo-temp-file))
194 (delete-file makeinfo-temp-file))
195 ;; Always use the version on disk.
196 (if (get-file-buffer makeinfo-output-file-name)
197 (progn (set-buffer makeinfo-output-file-name)
198 (revert-buffer t t))
199 (find-file makeinfo-output-file-name))
200 (goto-char (point-min)))
201
202(defun makeinfo-buffer ()
bab8cd70
RS
203 "Make Info file from current buffer.
204
0f99112d
RS
205Use the \\[next-error] command to move to the next error
206\(if there are errors\)."
207
208 (interactive)
bab8cd70 209 (cond ((null buffer-file-name)
0f99112d 210 (error "Buffer not visiting any file"))
bab8cd70
RS
211 ((buffer-modified-p)
212 (if (y-or-n-p "Buffer modified; do you want to save it? ")
213 (save-buffer))))
214
215 ;; Find and record the Info filename,
216 ;; or else explain that a filename is needed.
217 (save-excursion
218 (goto-char (point-min))
219 (let ((search-end (save-excursion (forward-line 100) (point))))
220 (if (re-search-forward
221 "^@setfilename[ \t]+\\([^ \t\n]+\\)[ \t]*"
222 search-end t)
223 (setq makeinfo-output-file-name
224 (buffer-substring (match-beginning 1) (match-end 1)))
225 (error
226 "The texinfo file needs a line saying: @setfilename <name>"))))
227
228 (save-excursion
229 (makeinfo-compile
0f99112d
RS
230 (concat makeinfo-run-command " " makeinfo-options
231 " " buffer-file-name)
232 "No more errors."
233 t)))
bab8cd70
RS
234
235(defun makeinfo-recenter-compilation-buffer (linenum)
236 "Redisplay `*compilation*' buffer so most recent output can be seen.
237The last line of the buffer is displayed on
238line LINE of the window, or centered if LINE is nil."
239 (interactive "P")
240 (let ((makeinfo-buffer (get-buffer "*compilation*"))
241 (old-buffer (current-buffer)))
242 (if (null makeinfo-buffer)
243 (message "No *compilation* buffer")
244 (pop-to-buffer makeinfo-buffer)
245 (bury-buffer makeinfo-buffer)
246 (goto-char (point-max))
247 (recenter (if linenum
248 (prefix-numeric-value linenum)
249 (/ (window-height) 2)))
250 (pop-to-buffer old-buffer)
251 )))
252
bab8cd70
RS
253;;; Place `provide' at end of file.
254(provide 'makeinfo)
255
256;;; makeinfo.el ends here