]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/autoload.el
(lisp-mode-variables, lisp-fill-paragraph): Remove
[gnu-emacs] / lisp / emacs-lisp / autoload.el
1 ;;; autoload.el --- maintain autoloads in loaddefs.el.
2
3 ;;; Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 ;;;
5 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
6 ;; Keywords: maint
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 ;;; Commentary:
25
26 ;; This code helps GNU Emacs maintainers keep the loaddefs.el file up to
27 ;; date. It interprets magic cookies of the form ";;;###autoload" in
28 ;; lisp source files in various useful ways. To learn more, read the
29 ;; source; if you're going to use this, you'd better be able to.
30
31 ;;; Code:
32
33 (defun make-autoload (form file)
34 "Turn FORM, a defun or defmacro, into an autoload for source file FILE.
35 Returns nil if FORM is not a defun, define-skeleton or defmacro."
36 (let ((car (car-safe form)))
37 (if (memq car '(defun define-skeleton defmacro))
38 (let ((macrop (eq car 'defmacro))
39 name doc)
40 (setq form (cdr form)
41 name (car form)
42 ;; Ignore the arguments.
43 form (cdr (if (eq car 'define-skeleton)
44 form
45 (cdr form)))
46 doc (car form))
47 (if (stringp doc)
48 (setq form (cdr form))
49 (setq doc nil))
50 (list 'autoload (list 'quote name) file doc
51 (or (eq car 'define-skeleton)
52 (eq (car-safe (car form)) 'interactive))
53 (if macrop (list 'quote 'macro) nil)))
54 nil)))
55
56 (put 'define-skeleton 'doc-string-elt 3)
57
58 (defconst generate-autoload-cookie ";;;###autoload"
59 "Magic comment indicating the following form should be autoloaded.
60 Used by \\[update-file-autoloads]. This string should be
61 meaningless to Lisp (e.g., a comment).
62
63 This string is used:
64
65 ;;;###autoload
66 \(defun function-to-be-autoloaded () ...)
67
68 If this string appears alone on a line, the following form will be
69 read and an autoload made for it. If there is further text on the line,
70 that text will be copied verbatim to `generated-autoload-file'.")
71
72 (defconst generate-autoload-section-header "\f\n;;;### "
73 "String inserted before the form identifying
74 the section of autoloads for a file.")
75
76 (defconst generate-autoload-section-trailer "\n;;;***\n"
77 "String which indicates the end of the section of autoloads for a file.")
78
79 ;;; Forms which have doc-strings which should be printed specially.
80 ;;; A doc-string-elt property of ELT says that (nth ELT FORM) is
81 ;;; the doc-string in FORM.
82 ;;;
83 ;;; There used to be the following note here:
84 ;;; ;;; Note: defconst and defvar should NOT be marked in this way.
85 ;;; ;;; We don't want to produce defconsts and defvars that
86 ;;; ;;; make-docfile can grok, because then it would grok them twice,
87 ;;; ;;; once in foo.el (where they are given with ;;;###autoload) and
88 ;;; ;;; once in loaddefs.el.
89 ;;;
90 ;;; Counter-note: Yes, they should be marked in this way.
91 ;;; make-docfile only processes those files that are loaded into the
92 ;;; dumped Emacs, and those files should never have anything
93 ;;; autoloaded here. The above-feared problem only occurs with files
94 ;;; which have autoloaded entries *and* are processed by make-docfile;
95 ;;; there should be no such files.
96
97 (put 'autoload 'doc-string-elt 3)
98 (put 'defun 'doc-string-elt 3)
99 (put 'defvar 'doc-string-elt 3)
100 (put 'defconst 'doc-string-elt 3)
101 (put 'defmacro 'doc-string-elt 3)
102
103 (defun autoload-trim-file-name (file)
104 ;; Returns a relative pathname of FILE
105 ;; starting from the directory that loaddefs.el is in.
106 ;; That is normally a directory in load-path,
107 ;; which means Emacs will be able to find FILE when it looks.
108 ;; Any extra directory names here would prevent finding the file.
109 (setq file (expand-file-name file))
110 (file-relative-name file
111 (file-name-directory generated-autoload-file)))
112
113 (defun generate-file-autoloads (file)
114 "Insert at point a loaddefs autoload section for FILE.
115 autoloads are generated for defuns and defmacros in FILE
116 marked by `generate-autoload-cookie' (which see).
117 If FILE is being visited in a buffer, the contents of the buffer
118 are used."
119 (interactive "fGenerate autoloads for file: ")
120 (let ((outbuf (current-buffer))
121 (autoloads-done '())
122 (load-name (let ((name (file-name-nondirectory file)))
123 (if (string-match "\\.elc?$" name)
124 (substring name 0 (match-beginning 0))
125 name)))
126 (print-length nil)
127 (print-readably t) ; This does something in Lucid Emacs.
128 (float-output-format nil)
129 (done-any nil)
130 (visited (get-file-buffer file))
131 output-end)
132
133 ;; If the autoload section we create here uses an absolute
134 ;; pathname for FILE in its header, and then Emacs is installed
135 ;; under a different path on another system,
136 ;; `update-autoloads-here' won't be able to find the files to be
137 ;; autoloaded. So, if FILE is in the same directory or a
138 ;; subdirectory of the current buffer's directory, we'll make it
139 ;; relative to the current buffer's directory.
140 (setq file (expand-file-name file))
141 (let* ((source-truename (file-truename file))
142 (dir-truename (file-name-as-directory
143 (file-truename default-directory)))
144 (len (length dir-truename)))
145 (if (and (< len (length source-truename))
146 (string= dir-truename (substring source-truename 0 len)))
147 (setq file (substring source-truename len))))
148
149 (message "Generating autoloads for %s..." file)
150 (save-excursion
151 (unwind-protect
152 (progn
153 (set-buffer (find-file-noselect file))
154 (save-excursion
155 (save-restriction
156 (widen)
157 (goto-char (point-min))
158 (while (not (eobp))
159 (skip-chars-forward " \t\n\f")
160 (cond
161 ((looking-at (regexp-quote generate-autoload-cookie))
162 (search-forward generate-autoload-cookie)
163 (skip-chars-forward " \t")
164 (setq done-any t)
165 (if (eolp)
166 ;; Read the next form and make an autoload.
167 (let* ((form (prog1 (read (current-buffer))
168 (or (bolp) (forward-line 1))))
169 (autoload (make-autoload form load-name))
170 (doc-string-elt (get (car-safe form)
171 'doc-string-elt)))
172 (if autoload
173 (setq autoloads-done (cons (nth 1 form)
174 autoloads-done))
175 (setq autoload form))
176 (if (and doc-string-elt
177 (stringp (nth doc-string-elt autoload)))
178 ;; We need to hack the printing because the
179 ;; doc-string must be printed specially for
180 ;; make-docfile (sigh).
181 (let* ((p (nthcdr (1- doc-string-elt)
182 autoload))
183 (elt (cdr p)))
184 (setcdr p nil)
185 (princ "\n(" outbuf)
186 (let ((print-escape-newlines t))
187 (mapcar (function (lambda (elt)
188 (prin1 elt outbuf)
189 (princ " " outbuf)))
190 autoload))
191 (princ "\"\\\n" outbuf)
192 (let ((begin (save-excursion
193 (set-buffer outbuf)
194 (point))))
195 (princ (substring
196 (prin1-to-string (car elt)) 1)
197 outbuf)
198 ;; Insert a backslash before each ( that
199 ;; appears at the beginning of a line in
200 ;; the doc string.
201 (save-excursion
202 (set-buffer outbuf)
203 (save-excursion
204 (while (search-backward "\n(" begin t)
205 (forward-char 1)
206 (insert "\\"))))
207 (if (null (cdr elt))
208 (princ ")" outbuf)
209 (princ " " outbuf)
210 (princ (substring
211 (prin1-to-string (cdr elt))
212 1)
213 outbuf))
214 (terpri outbuf)))
215 (let ((print-escape-newlines t))
216 (print autoload outbuf))))
217 ;; Copy the rest of the line to the output.
218 (let ((begin (point)))
219 (forward-line 1)
220 (princ (buffer-substring begin (point)) outbuf))))
221 ((looking-at ";")
222 ;; Don't read the comment.
223 (forward-line 1))
224 (t
225 (forward-sexp 1)
226 (forward-line 1)))))))
227 (or visited
228 ;; We created this buffer, so we should kill it.
229 (kill-buffer (current-buffer)))
230 (set-buffer outbuf)
231 (setq output-end (point-marker))))
232 (if done-any
233 (progn
234 (insert generate-autoload-section-header)
235 (prin1 (list 'autoloads autoloads-done load-name
236 (autoload-trim-file-name file)
237 (nth 5 (file-attributes file)))
238 outbuf)
239 (terpri outbuf)
240 (insert ";;; Generated autoloads from "
241 (autoload-trim-file-name file) "\n")
242 ;; Warn if we put a line in loaddefs.el
243 ;; that is long enough to cause trouble.
244 (while (< (point) output-end)
245 (let ((beg (point)))
246 (end-of-line)
247 (if (> (- (point) beg) 900)
248 (progn
249 (message "A line is too long--over 900 characters")
250 (sleep-for 2)
251 (goto-char output-end))))
252 (forward-line 1))
253 (goto-char output-end)
254 (insert generate-autoload-section-trailer)))
255 (message "Generating autoloads for %s...done" file)))
256 \f
257 (defconst generated-autoload-file "loaddefs.el"
258 "*File \\[update-file-autoloads] puts autoloads into.
259 A .el file can set this in its local variables section to make its
260 autoloads go somewhere else.")
261
262 ;;;###autoload
263 (defun update-file-autoloads (file)
264 "Update the autoloads for FILE in `generated-autoload-file'
265 \(which FILE might bind in its local variables)."
266 (interactive "fUpdate autoloads for file: ")
267 (let ((load-name (let ((name (file-name-nondirectory file)))
268 (if (string-match "\\.elc?$" name)
269 (substring name 0 (match-beginning 0))
270 name)))
271 (found nil)
272 (existing-buffer (get-file-buffer file)))
273 (save-excursion
274 ;; We want to get a value for generated-autoload-file from
275 ;; the local variables section if it's there.
276 (set-buffer (find-file-noselect file))
277 (set-buffer (find-file-noselect generated-autoload-file))
278 (save-excursion
279 (save-restriction
280 (widen)
281 (goto-char (point-min))
282 ;; Look for the section for LOAD-NAME.
283 (while (and (not found)
284 (search-forward generate-autoload-section-header nil t))
285 (let ((form (condition-case ()
286 (read (current-buffer))
287 (end-of-file nil))))
288 (cond ((string= (nth 2 form) load-name)
289 ;; We found the section for this file.
290 ;; Check if it is up to date.
291 (let ((begin (match-beginning 0))
292 (last-time (nth 4 form))
293 (file-time (nth 5 (file-attributes file))))
294 (if (and (or (null existing-buffer)
295 (not (buffer-modified-p existing-buffer)))
296 (listp last-time) (= (length last-time) 2)
297 (or (> (car last-time) (car file-time))
298 (and (= (car last-time) (car file-time))
299 (>= (nth 1 last-time)
300 (nth 1 file-time)))))
301 (progn
302 (message "Autoload section for %s is up to date."
303 file)
304 (setq found 'up-to-date))
305 (search-forward generate-autoload-section-trailer)
306 (delete-region begin (point))
307 (setq found t))))
308 ((string< load-name (nth 2 form))
309 ;; We've come to a section alphabetically later than
310 ;; LOAD-NAME. We assume the file is in order and so
311 ;; there must be no section for LOAD-NAME. We will
312 ;; insert one before the section here.
313 (goto-char (match-beginning 0))
314 (setq found 'new)))))
315 (or (eq found 'up-to-date)
316 (and (eq found 'new)
317 ;; Check that FILE has any cookies before generating a
318 ;; new section for it.
319 (save-excursion
320 (set-buffer (find-file-noselect file))
321 (save-excursion
322 (widen)
323 (goto-char (point-min))
324 (if (search-forward (concat "\n"
325 generate-autoload-cookie)
326 nil t)
327 nil
328 (if (interactive-p)
329 (message file " has no autoloads"))
330 t))))
331 (generate-file-autoloads file))))
332 (if (interactive-p) (save-buffer))
333 (if (and (null existing-buffer)
334 (setq existing-buffer (get-file-buffer file)))
335 (kill-buffer existing-buffer)))))
336
337 ;;;###autoload
338 (defun update-autoloads-here ()
339 "\
340 Update sections of the current buffer generated by \\[update-file-autoloads]."
341 (interactive)
342 (let ((generated-autoload-file (buffer-file-name)))
343 (save-excursion
344 (goto-char (point-min))
345 (while (search-forward generate-autoload-section-header nil t)
346 (let* ((form (condition-case ()
347 (read (current-buffer))
348 (end-of-file nil)))
349 (file (nth 3 form)))
350 (if (and (stringp file)
351 (or (get-file-buffer file)
352 (file-exists-p file)))
353 ()
354 (setq file (if (y-or-n-p (format "Can't find library `%s'; remove its autoloads? "
355 (nth 2 form) file))
356 t
357 (condition-case ()
358 (read-file-name (format "Find `%s' load file: "
359 (nth 2 form))
360 nil nil t)
361 (quit nil)))))
362 (if file
363 (let ((begin (match-beginning 0)))
364 (search-forward generate-autoload-section-trailer)
365 (delete-region begin (point))))
366 (if (stringp file)
367 (generate-file-autoloads file)))))))
368
369 ;;;###autoload
370 (defun update-directory-autoloads (dir)
371 "Run \\[update-file-autoloads] on each .el file in DIR."
372 (interactive "DUpdate autoloads for directory: ")
373 (let ((enable-local-eval nil))
374 (mapcar 'update-file-autoloads
375 (directory-files dir t "^[^=].*\\.el$")))
376 (if (interactive-p)
377 (save-excursion
378 (set-buffer (find-file-noselect generated-autoload-file))
379 (save-buffer))))
380
381 ;;;###autoload
382 (defun batch-update-autoloads ()
383 "Update the autoloads for the files or directories on the command line.
384 Runs \\[update-file-autoloads] on files and \\[update-directory-autoloads]
385 on directories. Must be used only with -batch, and kills Emacs on completion.
386 Each file will be processed even if an error occurred previously.
387 For example, invoke `emacs -batch -f batch-update-autoloads *.el'."
388 (if (not noninteractive)
389 (error "batch-update-autoloads is to be used only with -batch"))
390 (let ((lost nil)
391 (args command-line-args-left)
392 (enable-local-eval nil)) ;Don't query in batch mode.
393 (message "Updating autoloads in %s..." generated-autoload-file)
394 (let ((frob (function
395 (lambda (file)
396 (condition-case lossage
397 (update-file-autoloads file)
398 (error
399 (princ ">>Error processing ")
400 (princ file)
401 (princ ": ")
402 (if (fboundp 'display-error)
403 (display-error lossage nil)
404 (prin1 lossage))
405 (princ "\n")
406 (setq lost t)))))))
407 (while args
408 (if (file-directory-p (expand-file-name (car args)))
409 (let ((rest (directory-files (car args) t "\\.el$")))
410 (while rest
411 (funcall frob (car rest))
412 (setq rest (cdr rest))))
413 (funcall frob (car args)))
414 (setq args (cdr args))))
415 (save-some-buffers t)
416 (message "Done")
417 (kill-emacs (if lost 1 0))))
418
419 (provide 'autoload)
420
421 ;;; autoload.el ends here