]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/autoload.el
* bytecomp.el: Declare unread-command-char an obsolete variable.
[gnu-emacs] / lisp / emacs-lisp / autoload.el
1 ;;; autoload.el --- maintain autoloads in loaddefs.el.
2
3 ;;; Copyright (C) 1991, 1992 Free Software Foundation, Inc.
4 ;;;
5 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
6 ;; Keyword: internal
7
8 ;;; This program is free software; you can redistribute it and/or modify
9 ;;; it under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 2, or (at your option)
11 ;;; any later version.
12 ;;;
13 ;;; This program is distributed in the hope that it will be useful,
14 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; A copy of the GNU General Public License can be obtained from this
19 ;;; program's author (send electronic mail to roland@ai.mit.edu) or from
20 ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
21 ;;; 02139, USA.
22 ;;;
23
24 ;;; Code:
25
26 (defun make-autoload (form file)
27 "Turn FORM, a defun or defmacro, into an autoload for source file FILE.
28 Returns nil if FORM is not a defun or defmacro."
29 (let ((car (car-safe form)))
30 (if (or (eq car 'defun) (eq car 'defmacro))
31 (let (name doc macrop)
32 (setq macrop (eq car 'defmacro))
33 (setq form (cdr form))
34 (setq name (car form))
35 ;; Ignore the arguments.
36 (setq form (cdr (cdr form)))
37 (setq doc (car form))
38 (if (stringp doc)
39 (setq form (cdr form))
40 (setq doc nil))
41 (list 'autoload (list 'quote name) file doc
42 (eq (car-safe (car form)) 'interactive)
43 (if macrop (list 'quote 'macro) nil)))
44 nil)))
45
46 (defconst generate-autoload-cookie ";;;###autoload"
47 "Magic comment that tells \\[update-file-autoloads]
48 to make the following form into an autoload. This string should be
49 meaningless to Lisp (e.g., a comment).
50
51 This string is used:
52
53 ;;;###autoload
54 \(defun function-to-be-autoloaded () ...)
55
56 If this string appears alone on a line, the following form will be
57 read and an autoload made for it. If there is further text on the line,
58 that text will be copied verbatim to `generated-autoload-file'.")
59
60 (defconst generate-autoload-section-header "\f\n;;;### "
61 "String inserted before the form identifying
62 the section of autoloads for a file.")
63
64 (defconst generate-autoload-section-trailer "\n;;;***\n"
65 "String which indicates the end of the section of autoloads for a file.")
66
67 ;;; Forms which have doc-strings which should be printed specially.
68 ;;; A doc-string-elt property of ELT says that (nth ELT FORM) is
69 ;;; the doc-string in FORM.
70 ;;;
71 ;;; There used to be the following note here:
72 ;;; ;;; Note: defconst and defvar should NOT be marked in this way.
73 ;;; ;;; We don't want to produce defconsts and defvars that
74 ;;; ;;; make-docfile can grok, because then it would grok them twice,
75 ;;; ;;; once in foo.el (where they are given with ;;;###autoload) and
76 ;;; ;;; once in loaddefs.el.
77 ;;;
78 ;;; Counter-note: Yes, they should be marked in this way.
79 ;;; make-docfile only processes those files that are loaded into the
80 ;;; dumped Emacs, and those files should never have anything
81 ;;; autoloaded here. The above-feared problem only occurs with files
82 ;;; which have autoloaded entries *and* are processed by make-docfile;
83 ;;; there should be no such files.
84
85 (put 'autoload 'doc-string-elt 3)
86 (put 'defun 'doc-string-elt 3)
87 (put 'defvar 'doc-string-elt 3)
88 (put 'defconst 'doc-string-elt 3)
89 (put 'defmacro 'doc-string-elt 3)
90
91 (defun generate-file-autoloads (file)
92 "Insert at point a loaddefs autoload section for FILE.
93 autoloads are generated for defuns and defmacros in FILE
94 marked by `generate-autoload-regexp' (which see).
95 If FILE is being visited in a buffer, the contents of the buffer
96 are used."
97 (interactive "fGenerate autoloads for file: ")
98 (let ((outbuf (current-buffer))
99 (inbuf (find-file-noselect file))
100 (autoloads-done '())
101 (load-name (let ((name (file-name-nondirectory file)))
102 (if (string-match "\\.elc?$" name)
103 (substring name 0 (match-beginning 0))
104 name)))
105 (print-length nil)
106 (floating-output-format "%20e")
107 (done-any nil)
108 output-end)
109
110 ;; If the autoload section we create here uses an absolute
111 ;; pathname for FILE in its header, and then Emacs is installed
112 ;; under a different path on another system,
113 ;; `update-autoloads-here' won't be able to find the files to be
114 ;; autoloaded. So, if FILE is in the same directory or a
115 ;; subdirectory of the current buffer's directory, we'll make it
116 ;; relative to the current buffer's directory.
117 (setq file (expand-file-name file))
118 (if (and (< (length default-directory) (length file))
119 (string= default-directory
120 (substring file 0 (length default-directory))))
121 (progn
122 (setq file (substring file (length default-directory)))))
123
124 (message "Generating autoloads for %s..." file)
125 (save-excursion
126 (set-buffer inbuf)
127 (save-excursion
128 (save-restriction
129 (widen)
130 (goto-char (point-min))
131 (while (not (eobp))
132 (skip-chars-forward " \t\n\f")
133 (cond ((looking-at (regexp-quote generate-autoload-cookie))
134 (search-forward generate-autoload-cookie)
135 (skip-chars-forward " \t")
136 (setq done-any t)
137 (if (eolp)
138 ;; Read the next form and make an autoload.
139 (let* ((form (prog1 (read (current-buffer))
140 (forward-line 1)))
141 (autoload (make-autoload form load-name))
142 (doc-string-elt (get (car-safe form)
143 'doc-string-elt)))
144 (if autoload
145 (setq autoloads-done (cons (nth 1 form)
146 autoloads-done))
147 (setq autoload form))
148 (if (and doc-string-elt
149 (stringp (nth doc-string-elt autoload)))
150 ;; We need to hack the printing because the
151 ;; doc-string must be printed specially for
152 ;; make-docfile (sigh).
153 (let* ((p (nthcdr (1- doc-string-elt) autoload))
154 (elt (cdr p)))
155 (setcdr p nil)
156 (princ "\n(" outbuf)
157 (mapcar (function (lambda (elt)
158 (prin1 elt outbuf)
159 (princ " " outbuf)))
160 autoload)
161 (princ "\"\\\n" outbuf)
162 (princ (substring (prin1-to-string (car elt)) 1)
163 outbuf)
164 (if (null (cdr elt))
165 (princ ")" outbuf)
166 (princ " " outbuf)
167 (princ (substring (prin1-to-string (cdr elt))
168 1)
169 outbuf))
170 (terpri outbuf))
171 (print autoload outbuf)))
172 ;; Copy the rest of the line to the output.
173 (let ((begin (point)))
174 (forward-line 1)
175 (princ (buffer-substring begin (point)) outbuf))))
176 ((looking-at ";")
177 ;; Don't read the comment.
178 (forward-line 1))
179 (t
180 (forward-sexp 1)
181 (forward-line 1))))))
182 (set-buffer outbuf)
183 (setq output-end (point-marker)))
184 (if done-any
185 (progn
186 (insert generate-autoload-section-header)
187 (prin1 (list 'autoloads autoloads-done load-name file
188 (nth 5 (file-attributes file)))
189 outbuf)
190 (terpri outbuf)
191 (insert ";;; Generated autoloads from " file "\n")
192 (goto-char output-end)
193 (insert generate-autoload-section-trailer)))
194 (message "Generating autoloads for %s...done" file)))
195
196 (defconst generated-autoload-file "loaddefs.el"
197 "*File \\[update-file-autoloads] puts autoloads into.
198 A .el file can set this in its local variables section to make its
199 autoloads go somewhere else.")
200
201 ;;;###autoload
202 (defun update-file-autoloads (file)
203 "Update the autoloads for FILE in `generated-autoload-file'
204 \(which FILE might bind in its local variables)."
205 (interactive "fUpdate autoloads for file: ")
206 (let ((load-name (let ((name (file-name-nondirectory file)))
207 (if (string-match "\\.elc?$" name)
208 (substring name 0 (match-beginning 0))
209 name)))
210 (done nil)
211 (existing-buffer (get-file-buffer file)))
212 (save-excursion
213 ;; We want to get a value for generated-autoload-file from
214 ;; the local variables section if it's there.
215 (set-buffer (find-file-noselect file))
216 (set-buffer (find-file-noselect generated-autoload-file))
217 (save-excursion
218 (save-restriction
219 (widen)
220 (goto-char (point-min))
221 (while (search-forward generate-autoload-section-header nil t)
222 (let ((form (condition-case ()
223 (read (current-buffer))
224 (end-of-file nil))))
225 (if (string= (nth 2 form) load-name)
226 (let ((begin (match-beginning 0))
227 (last-time (nth 4 form))
228 (file-time (nth 5 (file-attributes file))))
229 (if (and (or (null existing-buffer)
230 (not (buffer-modified-p existing-buffer)))
231 (listp last-time) (= (length last-time) 2)
232 (or (> (car last-time) (car file-time))
233 (and (= (car last-time) (car file-time))
234 (>= (nth 1 last-time)
235 (nth 1 file-time)))))
236 (message "Autoload section for %s is up to date."
237 file)
238 (search-forward generate-autoload-section-trailer)
239 (delete-region begin (point))
240 (generate-file-autoloads file))
241 (setq done t))))))
242 (if done
243 ()
244 ;; Have the user tell us where to put the section.
245 (save-window-excursion
246 (switch-to-buffer (current-buffer))
247 (with-output-to-temp-buffer "*Help*"
248 (princ (substitute-command-keys
249 (format "\
250 Move point to where the autoload section
251 for %s should be inserted.
252 Then do \\[exit-recursive-edit]."
253 file))))
254 (recursive-edit)
255 (beginning-of-line))
256 (generate-file-autoloads file)))
257 (if (and (null existing-buffer)
258 (setq existing-buffer (get-file-buffer file)))
259 (kill-buffer existing-buffer)))))
260
261 ;;;###autoload
262 (defun update-autoloads-here ()
263 "Update the sections of the current buffer generated by
264 \\[update-file-autoloads]."
265 (interactive)
266 (let ((generated-autoload-file (buffer-file-name)))
267 (save-excursion
268 (goto-char (point-min))
269 (while (search-forward generate-autoload-section-header nil t)
270 (let* ((form (condition-case ()
271 (read (current-buffer))
272 (end-of-file nil)))
273 (file (nth 3 form)))
274 (if (and (stringp file)
275 (or (get-file-buffer file)
276 (file-exists-p file)))
277 ()
278 (setq file (if (y-or-n-p (format "Library \"%s\" (load \
279 file \"%s\") doesn't exist. Remove its autoload section? "
280 (nth 2 form) file))
281 t
282 (condition-case ()
283 (read-file-name (format "Find \"%s\" load file: "
284 (nth 2 form))
285 nil nil t)
286 (quit nil)))))
287 (if file
288 (let ((begin (match-beginning 0)))
289 (search-forward generate-autoload-section-trailer)
290 (delete-region begin (point))))
291 (if (stringp file)
292 (generate-file-autoloads file)))))))
293
294 ;;;###autoload
295 (defun update-directory-autoloads (dir)
296 "Run \\[update-file-autoloads] on each .el file in DIR."
297 (interactive "DUpdate autoloads for directory: ")
298 (mapcar 'update-file-autoloads
299 (directory-files dir nil "\\.el$")))
300
301 ;;;###autoload
302 (defun batch-update-autoloads ()
303 "Update the autoloads for the files or directories on the command line.
304 Runs \\[update-file-autoloads] on files and \\[update-directory-autoloads]
305 on directories. Must be used only with -batch, and kills Emacs on completion.
306 Each file will be processed even if an error occurred previously.
307 For example, invoke \"emacs -batch -f batch-update-autoloads *.el\""
308 (if (not noninteractive)
309 (error "batch-update-file-autoloads is to be used only with -batch"))
310 (let ((lost nil)
311 (args command-line-args-left))
312 (while args
313 (catch 'file
314 (condition-case lossage
315 (if (file-directory-p (expand-file-name (car args)))
316 (update-directory-autoloads (car args))
317 (update-file-autoloads (car args)))
318 (error (progn (message ">>Error processing %s: %s"
319 (car args) lossage)
320 (setq lost t)
321 (throw 'file nil)))))
322 (setq args (cdr args)))
323 (save-some-buffers t)
324 (message "Done")
325 (kill-emacs (if lost 1 0))))
326
327 (provide 'autoload)
328
329 ;;; autoload.el ends here