Merge from emacs-24; up to 2012-05-02T11:38:01Z!lekktu@gmail.com
[emacs.git] / lisp / emacs-lisp / autoload.el
blobe6e2d1e60e0a7ddf19b49a49be26f20f99fb576c
1 ;; autoload.el --- maintain autoloads in loaddefs.el -*- lexical-binding: t -*-
3 ;; Copyright (C) 1991-1997, 2001-2012 Free Software Foundation, Inc.
5 ;; Author: Roland McGrath <roland@gnu.org>
6 ;; Keywords: maint
7 ;; Package: emacs
9 ;; This file is part of GNU Emacs.
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 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
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.
31 ;;; Code:
33 (require 'lisp-mode) ;for `doc-string-elt' properties.
34 (require 'help-fns) ;for help-add-fundoc-usage.
35 (eval-when-compile (require 'cl-lib))
37 (defvar generated-autoload-file nil
38 "File into which to write autoload definitions.
39 A Lisp file can set this in its local variables section to make
40 its autoloads go somewhere else.
42 If this is a relative file name, the directory is determined as
43 follows:
44 - If a Lisp file defined `generated-autoload-file' as a
45 file-local variable, use its containing directory.
46 - Otherwise use the \"lisp\" subdirectory of `source-directory'.
48 The autoload file is assumed to contain a trailer starting with a
49 FormFeed character.")
50 ;;;###autoload
51 (put 'generated-autoload-file 'safe-local-variable 'stringp)
53 (defvar generated-autoload-load-name nil
54 "Load name for `autoload' statements generated from autoload cookies.
55 If nil, this defaults to the file name, sans extension.")
56 ;;;###autoload
57 (put 'generated-autoload-load-name 'safe-local-variable 'stringp)
59 ;; This feels like it should be a defconst, but MH-E sets it to
60 ;; ";;;###mh-autoload" for the autoloads that are to go into mh-loaddefs.el.
61 (defvar generate-autoload-cookie ";;;###autoload"
62 "Magic comment indicating the following form should be autoloaded.
63 Used by \\[update-file-autoloads]. This string should be
64 meaningless to Lisp (e.g., a comment).
66 This string is used:
68 \;;;###autoload
69 \(defun function-to-be-autoloaded () ...)
71 If this string appears alone on a line, the following form will be
72 read and an autoload made for it. If there is further text on the line,
73 that text will be copied verbatim to `generated-autoload-file'.")
75 (defvar autoload-excludes nil
76 "If non-nil, list of absolute file names not to scan for autoloads.")
78 (defconst generate-autoload-section-header "\f\n;;;### "
79 "String that marks the form at the start of a new file's autoload section.")
81 (defconst generate-autoload-section-trailer "\n;;;***\n"
82 "String which indicates the end of the section of autoloads for a file.")
84 (defconst generate-autoload-section-continuation ";;;;;; "
85 "String to add on each continuation of the section header form.")
87 (defvar autoload-modified-buffers) ;Dynamically scoped var.
89 (defun make-autoload (form file &optional expansion)
90 "Turn FORM into an autoload or defvar for source file FILE.
91 Returns nil if FORM is not a special autoload form (i.e. a function definition
92 or macro definition or a defcustom).
93 If EXPANSION is non-nil, we're processing the macro expansion of an
94 expression, in which case we want to handle forms differently."
95 (let ((car (car-safe form)) expand)
96 (cond
97 ((and expansion (eq car 'defalias))
98 (pcase-let*
99 ((`(,_ ,_ ,arg . ,rest) form)
100 ;; `type' is non-nil if it defines a macro.
101 ;; `fun' is the function part of `arg' (defaults to `arg').
102 ((or (and (or `(cons 'macro ,fun) `'(macro . ,fun)) (let type t))
103 (and (let fun arg) (let type nil)))
104 arg)
105 ;; `lam' is the lambda expression in `fun' (or nil if not
106 ;; recognized).
107 (lam (if (memq (car-safe fun) '(quote function)) (cadr fun)))
108 ;; `args' is the list of arguments (or t if not recognized).
109 ;; `body' is the body of `lam' (or t if not recognized).
110 ((or `(lambda ,args . ,body)
111 (and (let args t) (let body t)))
112 lam)
113 ;; Get the `doc' from `body' or `rest'.
114 (doc (cond ((stringp (car-safe body)) (car body))
115 ((stringp (car-safe rest)) (car rest))))
116 ;; Look for an interactive spec.
117 (interactive (pcase body
118 ((or `((interactive . ,_) . ,_)
119 `(,_ (interactive . ,_) . ,_)) t))))
120 ;; Add the usage form at the end where describe-function-1
121 ;; can recover it.
122 (when (listp args) (setq doc (help-add-fundoc-usage doc args)))
123 ;; (message "autoload of %S" (nth 1 form))
124 `(autoload ,(nth 1 form) ,file ,doc ,interactive ,type)))
126 ((and expansion (memq car '(progn prog1)))
127 (let ((end (memq :autoload-end form)))
128 (when end ;Cut-off anything after the :autoload-end marker.
129 (setq form (copy-sequence form))
130 (setcdr (memq :autoload-end form) nil))
131 (let ((exps (delq nil (mapcar (lambda (form)
132 (make-autoload form file expansion))
133 (cdr form)))))
134 (when exps (cons 'progn exps)))))
136 ;; For complex cases, try again on the macro-expansion.
137 ((and (memq car '(easy-mmode-define-global-mode define-global-minor-mode
138 define-globalized-minor-mode defun defmacro
139 ;; FIXME: we'd want `defmacro*' here as well, so as
140 ;; to handle its `declare', but when autoload is run
141 ;; CL is not loaded so macroexpand doesn't know how
142 ;; to expand it!
143 easy-mmode-define-minor-mode define-minor-mode))
144 (setq expand (let ((load-file-name file)) (macroexpand form)))
145 (memq (car expand) '(progn prog1 defalias)))
146 (make-autoload expand file 'expansion)) ;Recurse on the expansion.
148 ;; For special function-like operators, use the `autoload' function.
149 ((memq car '(define-skeleton define-derived-mode
150 define-compilation-mode define-generic-mode
151 easy-mmode-define-global-mode define-global-minor-mode
152 define-globalized-minor-mode
153 easy-mmode-define-minor-mode define-minor-mode
154 cl-defun defun* cl-defmacro defmacro*
155 define-overloadable-function))
156 (let* ((macrop (memq car '(defmacro defmacro*)))
157 (name (nth 1 form))
158 (args (pcase car
159 ((or `defun `defmacro
160 `defun* `defmacro* `cl-defun `cl-defmacro
161 `define-overloadable-function) (nth 2 form))
162 (`define-skeleton '(&optional str arg))
163 ((or `define-generic-mode `define-derived-mode
164 `define-compilation-mode) nil)
165 (_ t)))
166 (body (nthcdr (or (function-get car 'doc-string-elt) 3) form))
167 (doc (if (stringp (car body)) (pop body))))
168 ;; Add the usage form at the end where describe-function-1
169 ;; can recover it.
170 (when (listp args) (setq doc (help-add-fundoc-usage doc args)))
171 ;; `define-generic-mode' quotes the name, so take care of that
172 `(autoload ,(if (listp name) name (list 'quote name))
173 ,file ,doc
174 ,(or (and (memq car '(define-skeleton define-derived-mode
175 define-generic-mode
176 easy-mmode-define-global-mode
177 define-global-minor-mode
178 define-globalized-minor-mode
179 easy-mmode-define-minor-mode
180 define-minor-mode)) t)
181 (eq (car-safe (car body)) 'interactive))
182 ,(if macrop ''macro nil))))
184 ;; For defclass forms, use `eieio-defclass-autoload'.
185 ((eq car 'defclass)
186 (let ((name (nth 1 form))
187 (superclasses (nth 2 form))
188 (doc (nth 4 form)))
189 (list 'eieio-defclass-autoload (list 'quote name)
190 (list 'quote superclasses) file doc)))
192 ;; Convert defcustom to less space-consuming data.
193 ((eq car 'defcustom)
194 (let ((varname (car-safe (cdr-safe form)))
195 (init (car-safe (cdr-safe (cdr-safe form))))
196 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form)))))
197 ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
199 `(progn
200 (defvar ,varname ,init ,doc)
201 (custom-autoload ',varname ,file
202 ,(condition-case nil
203 (null (cadr (memq :set form)))
204 (error nil))))))
206 ((eq car 'defgroup)
207 ;; In Emacs this is normally handled separately by cus-dep.el, but for
208 ;; third party packages, it can be convenient to explicitly autoload
209 ;; a group.
210 (let ((groupname (nth 1 form)))
211 `(let ((loads (get ',groupname 'custom-loads)))
212 (if (member ',file loads) nil
213 (put ',groupname 'custom-loads (cons ',file loads))))))
215 ;; When processing a macro expansion, any expression
216 ;; before a :autoload-end should be included. These are typically (put
217 ;; 'fun 'prop val) and things like that.
218 ((and expansion (consp form)) form)
220 ;; nil here indicates that this is not a special autoload form.
221 (t nil))))
223 ;; Forms which have doc-strings which should be printed specially.
224 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
225 ;; the doc-string in FORM.
226 ;; Those properties are now set in lisp-mode.el.
228 (defun autoload-find-generated-file ()
229 "Visit the autoload file for the current buffer, and return its buffer.
230 If a buffer is visiting the desired autoload file, return it."
231 (let ((enable-local-variables :safe)
232 (enable-local-eval nil))
233 ;; We used to use `raw-text' to read this file, but this causes
234 ;; problems when the file contains non-ASCII characters.
235 (find-file-noselect
236 (autoload-ensure-default-file (autoload-generated-file)))))
238 (defun autoload-generated-file ()
239 (expand-file-name generated-autoload-file
240 ;; File-local settings of generated-autoload-file should
241 ;; be interpreted relative to the file's location,
242 ;; of course.
243 (if (not (local-variable-p 'generated-autoload-file))
244 (expand-file-name "lisp" source-directory))))
247 (defun autoload-read-section-header ()
248 "Read a section header form.
249 Since continuation lines have been marked as comments,
250 we must copy the text of the form and remove those comment
251 markers before we call `read'."
252 (save-match-data
253 (let ((beginning (point))
254 string)
255 (forward-line 1)
256 (while (looking-at generate-autoload-section-continuation)
257 (forward-line 1))
258 (setq string (buffer-substring beginning (point)))
259 (with-current-buffer (get-buffer-create " *autoload*")
260 (erase-buffer)
261 (insert string)
262 (goto-char (point-min))
263 (while (search-forward generate-autoload-section-continuation nil t)
264 (replace-match " "))
265 (goto-char (point-min))
266 (read (current-buffer))))))
268 (defvar autoload-print-form-outbuf nil
269 "Buffer which gets the output of `autoload-print-form'.")
271 (defun autoload-print-form (form)
272 "Print FORM such that `make-docfile' will find the docstrings.
273 The variable `autoload-print-form-outbuf' specifies the buffer to
274 put the output in."
275 (cond
276 ;; If the form is a sequence, recurse.
277 ((eq (car form) 'progn) (mapcar 'autoload-print-form (cdr form)))
278 ;; Symbols at the toplevel are meaningless.
279 ((symbolp form) nil)
281 (let ((doc-string-elt (function-get (car-safe form) 'doc-string-elt))
282 (outbuf autoload-print-form-outbuf))
283 (if (and doc-string-elt (stringp (nth doc-string-elt form)))
284 ;; We need to hack the printing because the
285 ;; doc-string must be printed specially for
286 ;; make-docfile (sigh).
287 (let* ((p (nthcdr (1- doc-string-elt) form))
288 (elt (cdr p)))
289 (setcdr p nil)
290 (princ "\n(" outbuf)
291 (let ((print-escape-newlines t)
292 (print-quoted t)
293 (print-escape-nonascii t))
294 (dolist (elt form)
295 (prin1 elt outbuf)
296 (princ " " outbuf)))
297 (princ "\"\\\n" outbuf)
298 (let ((begin (with-current-buffer outbuf (point))))
299 (princ (substring (prin1-to-string (car elt)) 1)
300 outbuf)
301 ;; Insert a backslash before each ( that
302 ;; appears at the beginning of a line in
303 ;; the doc string.
304 (with-current-buffer outbuf
305 (save-excursion
306 (while (re-search-backward "\n[[(]" begin t)
307 (forward-char 1)
308 (insert "\\"))))
309 (if (null (cdr elt))
310 (princ ")" outbuf)
311 (princ " " outbuf)
312 (princ (substring (prin1-to-string (cdr elt)) 1)
313 outbuf))
314 (terpri outbuf)))
315 (let ((print-escape-newlines t)
316 (print-quoted t)
317 (print-escape-nonascii t))
318 (print form outbuf)))))))
320 (defun autoload-rubric (file &optional type feature)
321 "Return a string giving the appropriate autoload rubric for FILE.
322 TYPE (default \"autoloads\") is a string stating the type of
323 information contained in FILE. If FEATURE is non-nil, FILE
324 will provide a feature. FEATURE may be a string naming the
325 feature, otherwise it will be based on FILE's name.
327 At present, a feature is in fact always provided, but this should
328 not be relied upon."
329 (let ((basename (file-name-nondirectory file)))
330 (concat ";;; " basename
331 " --- automatically extracted " (or type "autoloads") "\n"
332 ";;\n"
333 ";;; Code:\n\n"
334 "\f\n"
335 ;; This is used outside of autoload.el, eg cus-dep, finder.
336 "(provide '"
337 (if (stringp feature)
338 feature
339 (file-name-sans-extension basename))
340 ")\n"
341 ";; Local Variables:\n"
342 ";; version-control: never\n"
343 ";; no-byte-compile: t\n"
344 ";; no-update-autoloads: t\n"
345 ";; coding: utf-8\n"
346 ";; End:\n"
347 ";;; " basename
348 " ends here\n")))
350 (defun autoload-ensure-default-file (file)
351 "Make sure that the autoload file FILE exists and if not create it."
352 (unless (file-exists-p file)
353 (write-region (autoload-rubric file) nil file))
354 file)
356 (defun autoload-insert-section-header (outbuf autoloads load-name file time)
357 "Insert the section-header line,
358 which lists the file name and which functions are in it, etc."
359 (insert generate-autoload-section-header)
360 (prin1 `(autoloads ,autoloads ,load-name ,file ,time)
361 outbuf)
362 (terpri outbuf)
363 ;; Break that line at spaces, to avoid very long lines.
364 ;; Make each sub-line into a comment.
365 (with-current-buffer outbuf
366 (save-excursion
367 (forward-line -1)
368 (while (not (eolp))
369 (move-to-column 64)
370 (skip-chars-forward "^ \n")
371 (or (eolp)
372 (insert "\n" generate-autoload-section-continuation))))))
374 (defun autoload-find-file (file)
375 "Fetch file and put it in a temp buffer. Return the buffer."
376 ;; It is faster to avoid visiting the file.
377 (setq file (expand-file-name file))
378 (with-current-buffer (get-buffer-create " *autoload-file*")
379 (kill-all-local-variables)
380 (erase-buffer)
381 (setq buffer-undo-list t
382 buffer-read-only nil)
383 (emacs-lisp-mode)
384 (setq default-directory (file-name-directory file))
385 (insert-file-contents file nil)
386 (let ((enable-local-variables :safe)
387 (enable-local-eval nil))
388 (hack-local-variables))
389 (current-buffer)))
391 (defvar no-update-autoloads nil
392 "File local variable to prevent scanning this file for autoload cookies.")
394 (defun autoload-file-load-name (file)
395 "Compute the name that will be used to load FILE."
396 ;; OUTFILE should be the name of the global loaddefs.el file, which
397 ;; is expected to be at the root directory of the files we're
398 ;; scanning for autoloads and will be in the `load-path'.
399 (let* ((outfile (default-value 'generated-autoload-file))
400 (name (file-relative-name file (file-name-directory outfile)))
401 (names '())
402 (dir (file-name-directory outfile)))
403 ;; If `name' has directory components, only keep the
404 ;; last few that are really needed.
405 (while name
406 (setq name (directory-file-name name))
407 (push (file-name-nondirectory name) names)
408 (setq name (file-name-directory name)))
409 (while (not name)
410 (cond
411 ((null (cdr names)) (setq name (car names)))
412 ((file-exists-p (expand-file-name "subdirs.el" dir))
413 ;; FIXME: here we only check the existence of subdirs.el,
414 ;; without checking its content. This makes it generate wrong load
415 ;; names for cases like lisp/term which is not added to load-path.
416 (setq dir (expand-file-name (pop names) dir)))
417 (t (setq name (mapconcat 'identity names "/")))))
418 (if (string-match "\\.elc?\\(\\.\\|\\'\\)" name)
419 (substring name 0 (match-beginning 0))
420 name)))
422 (defun generate-file-autoloads (file)
423 "Insert at point a loaddefs autoload section for FILE.
424 Autoloads are generated for defuns and defmacros in FILE
425 marked by `generate-autoload-cookie' (which see).
426 If FILE is being visited in a buffer, the contents of the buffer
427 are used.
428 Return non-nil in the case where no autoloads were added at point."
429 (interactive "fGenerate autoloads for file: ")
430 (let ((generated-autoload-file buffer-file-name))
431 (autoload-generate-file-autoloads file (current-buffer))))
433 (defvar print-readably)
435 ;; When called from `generate-file-autoloads' we should ignore
436 ;; `generated-autoload-file' altogether. When called from
437 ;; `update-file-autoloads' we don't know `outbuf'. And when called from
438 ;; `update-directory-autoloads' it's in between: we know the default
439 ;; `outbuf' but we should obey any file-local setting of
440 ;; `generated-autoload-file'.
441 (defun autoload-generate-file-autoloads (file &optional outbuf outfile)
442 "Insert an autoload section for FILE in the appropriate buffer.
443 Autoloads are generated for defuns and defmacros in FILE
444 marked by `generate-autoload-cookie' (which see).
445 If FILE is being visited in a buffer, the contents of the buffer are used.
446 OUTBUF is the buffer in which the autoload statements should be inserted.
447 If OUTBUF is nil, it will be determined by `autoload-generated-file'.
449 If provided, OUTFILE is expected to be the file name of OUTBUF.
450 If OUTFILE is non-nil and FILE specifies a `generated-autoload-file'
451 different from OUTFILE, then OUTBUF is ignored.
453 Return non-nil if and only if FILE adds no autoloads to OUTFILE
454 \(or OUTBUF if OUTFILE is nil)."
455 (catch 'done
456 (let ((autoloads-done '())
457 load-name
458 (print-length nil)
459 (print-level nil)
460 (print-readably t) ; This does something in Lucid Emacs.
461 (float-output-format nil)
462 (visited (get-file-buffer file))
463 (otherbuf nil)
464 (absfile (expand-file-name file))
465 ;; nil until we found a cookie.
466 output-start ostart)
467 (with-current-buffer (or visited
468 ;; It is faster to avoid visiting the file.
469 (autoload-find-file file))
470 ;; Obey the no-update-autoloads file local variable.
471 (unless no-update-autoloads
472 (message "Generating autoloads for %s..." file)
473 (setq load-name
474 (if (stringp generated-autoload-load-name)
475 generated-autoload-load-name
476 (autoload-file-load-name absfile)))
477 (when (and outfile
478 (not
479 (if (memq system-type '(ms-dos windows-nt))
480 (equal (downcase outfile)
481 (downcase (autoload-generated-file)))
482 (equal outfile (autoload-generated-file)))))
483 (setq otherbuf t))
484 (save-excursion
485 (save-restriction
486 (widen)
487 (goto-char (point-min))
488 (while (not (eobp))
489 (skip-chars-forward " \t\n\f")
490 (cond
491 ((looking-at (regexp-quote generate-autoload-cookie))
492 ;; If not done yet, figure out where to insert this text.
493 (unless output-start
494 (let ((outbuf
495 (or (if otherbuf
496 ;; A file-local setting of
497 ;; autoload-generated-file says we
498 ;; should ignore OUTBUF.
500 outbuf)
501 (autoload-find-destination absfile load-name)
502 ;; The file has autoload cookies, but they're
503 ;; already up-to-date. If OUTFILE is nil, the
504 ;; entries are in the expected OUTBUF,
505 ;; otherwise they're elsewhere.
506 (throw 'done otherbuf))))
507 (with-current-buffer outbuf
508 (setq output-start (point-marker)
509 ostart (point)))))
510 (search-forward generate-autoload-cookie)
511 (skip-chars-forward " \t")
512 (if (eolp)
513 (condition-case-unless-debug err
514 ;; Read the next form and make an autoload.
515 (let* ((form (prog1 (read (current-buffer))
516 (or (bolp) (forward-line 1))))
517 (autoload (make-autoload form load-name)))
518 (if autoload
519 (push (nth 1 form) autoloads-done)
520 (setq autoload form))
521 (let ((autoload-print-form-outbuf
522 (marker-buffer output-start)))
523 (autoload-print-form autoload)))
524 (error
525 (message "Autoload cookie error in %s:%s %S"
526 file (count-lines (point-min) (point)) err)))
528 ;; Copy the rest of the line to the output.
529 (princ (buffer-substring
530 (progn
531 ;; Back up over whitespace, to preserve it.
532 (skip-chars-backward " \f\t")
533 (if (= (char-after (1+ (point))) ? )
534 ;; Eat one space.
535 (forward-char 1))
536 (point))
537 (progn (forward-line 1) (point)))
538 (marker-buffer output-start))))
539 ((looking-at ";")
540 ;; Don't read the comment.
541 (forward-line 1))
543 (forward-sexp 1)
544 (forward-line 1))))))
546 (when output-start
547 (let ((secondary-autoloads-file-buf
548 (if otherbuf (current-buffer))))
549 (with-current-buffer (marker-buffer output-start)
550 (save-excursion
551 ;; Insert the section-header line which lists the file name
552 ;; and which functions are in it, etc.
553 (cl-assert (= ostart output-start))
554 (goto-char output-start)
555 (let ((relfile (file-relative-name absfile)))
556 (autoload-insert-section-header
557 (marker-buffer output-start)
558 autoloads-done load-name relfile
559 (if secondary-autoloads-file-buf
560 ;; MD5 checksums are much better because they do not
561 ;; change unless the file changes (so they'll be
562 ;; equal on two different systems and will change
563 ;; less often than time-stamps, thus leading to fewer
564 ;; unneeded changes causing spurious conflicts), but
565 ;; using time-stamps is a very useful optimization,
566 ;; so we use time-stamps for the main autoloads file
567 ;; (loaddefs.el) where we have special ways to
568 ;; circumvent the "random change problem", and MD5
569 ;; checksum in secondary autoload files where we do
570 ;; not need the time-stamp optimization because it is
571 ;; already provided by the primary autoloads file.
572 (md5 secondary-autoloads-file-buf
573 ;; We'd really want to just use
574 ;; `emacs-internal' instead.
575 nil nil 'emacs-mule-unix)
576 (nth 5 (file-attributes relfile))))
577 (insert ";;; Generated autoloads from " relfile "\n")))
578 (insert generate-autoload-section-trailer))))
579 (message "Generating autoloads for %s...done" file))
580 (or visited
581 ;; We created this buffer, so we should kill it.
582 (kill-buffer (current-buffer))))
583 (or (not output-start)
584 ;; If the entries were added to some other buffer, then the file
585 ;; doesn't add entries to OUTFILE.
586 otherbuf))))
588 (defun autoload-save-buffers ()
589 (while autoload-modified-buffers
590 (with-current-buffer (pop autoload-modified-buffers)
591 (let ((version-control 'never))
592 (save-buffer)))))
594 ;;;###autoload
595 (defun update-file-autoloads (file &optional save-after outfile)
596 "Update the autoloads for FILE.
597 If prefix arg SAVE-AFTER is non-nil, save the buffer too.
599 If FILE binds `generated-autoload-file' as a file-local variable,
600 autoloads are written into that file. Otherwise, the autoloads
601 file is determined by OUTFILE. If called interactively, prompt
602 for OUTFILE; if called from Lisp with OUTFILE nil, use the
603 existing value of `generated-autoload-file'.
605 Return FILE if there was no autoload cookie in it, else nil."
606 (interactive (list (read-file-name "Update autoloads for file: ")
607 current-prefix-arg
608 (read-file-name "Write autoload definitions to file: ")))
609 (let* ((generated-autoload-file (or outfile generated-autoload-file))
610 (autoload-modified-buffers nil)
611 (no-autoloads (autoload-generate-file-autoloads file)))
612 (if autoload-modified-buffers
613 (if save-after (autoload-save-buffers))
614 (if (called-interactively-p 'interactive)
615 (message "Autoload section for %s is up to date." file)))
616 (if no-autoloads file)))
618 (defun autoload-find-destination (file load-name)
619 "Find the destination point of the current buffer's autoloads.
620 FILE is the file name of the current buffer.
621 Returns a buffer whose point is placed at the requested location.
622 Returns nil if the file's autoloads are uptodate, otherwise
623 removes any prior now out-of-date autoload entries."
624 (catch 'up-to-date
625 (let* ((buf (current-buffer))
626 (existing-buffer (if buffer-file-name buf))
627 (found nil))
628 (with-current-buffer (autoload-find-generated-file)
629 ;; This is to make generated-autoload-file have Unix EOLs, so
630 ;; that it is portable to all platforms.
631 (or (eq 0 (coding-system-eol-type buffer-file-coding-system))
632 (set-buffer-file-coding-system 'unix))
633 (or (> (buffer-size) 0)
634 (error "Autoloads file %s lacks boilerplate" buffer-file-name))
635 (or (file-writable-p buffer-file-name)
636 (error "Autoloads file %s is not writable" buffer-file-name))
637 (widen)
638 (goto-char (point-min))
639 ;; Look for the section for LOAD-NAME.
640 (while (and (not found)
641 (search-forward generate-autoload-section-header nil t))
642 (let ((form (autoload-read-section-header)))
643 (cond ((string= (nth 2 form) load-name)
644 ;; We found the section for this file.
645 ;; Check if it is up to date.
646 (let ((begin (match-beginning 0))
647 (last-time (nth 4 form))
648 (file-time (nth 5 (file-attributes file))))
649 (if (and (or (null existing-buffer)
650 (not (buffer-modified-p existing-buffer)))
652 ;; last-time is the time-stamp (specifying
653 ;; the last time we looked at the file) and
654 ;; the file hasn't been changed since.
655 (and (listp last-time) (= (length last-time) 2)
656 (not (time-less-p last-time file-time)))
657 ;; last-time is an MD5 checksum instead.
658 (and (stringp last-time)
659 (equal last-time
660 (md5 buf nil nil 'emacs-mule)))))
661 (throw 'up-to-date nil)
662 (autoload-remove-section begin)
663 (setq found t))))
664 ((string< load-name (nth 2 form))
665 ;; We've come to a section alphabetically later than
666 ;; LOAD-NAME. We assume the file is in order and so
667 ;; there must be no section for LOAD-NAME. We will
668 ;; insert one before the section here.
669 (goto-char (match-beginning 0))
670 (setq found t)))))
671 (or found
672 (progn
673 ;; No later sections in the file. Put before the last page.
674 (goto-char (point-max))
675 (search-backward "\f" nil t)))
676 (unless (memq (current-buffer) autoload-modified-buffers)
677 (push (current-buffer) autoload-modified-buffers))
678 (current-buffer)))))
680 (defun autoload-remove-section (begin)
681 (goto-char begin)
682 (search-forward generate-autoload-section-trailer)
683 (delete-region begin (point)))
685 ;;;###autoload
686 (defun update-directory-autoloads (&rest dirs)
687 "Update autoload definitions for Lisp files in the directories DIRS.
688 In an interactive call, you must give one argument, the name of a
689 single directory. In a call from Lisp, you can supply multiple
690 directories as separate arguments, but this usage is discouraged.
692 The function does NOT recursively descend into subdirectories of the
693 directory or directories specified.
695 In an interactive call, prompt for a default output file for the
696 autoload definitions, and temporarily bind the variable
697 `generated-autoload-file' to this value. When called from Lisp,
698 use the existing value of `generated-autoload-file'. If any Lisp
699 file binds `generated-autoload-file' as a file-local variable,
700 write its autoloads into the specified file instead."
701 (interactive "DUpdate autoloads from directory: ")
702 (let* ((files-re (let ((tmp nil))
703 (dolist (suf (get-load-suffixes))
704 (unless (string-match "\\.elc" suf) (push suf tmp)))
705 (concat "^[^=.].*" (regexp-opt tmp t) "\\'")))
706 (files (apply 'nconc
707 (mapcar (lambda (dir)
708 (directory-files (expand-file-name dir)
709 t files-re))
710 dirs)))
711 (done ())
712 (this-time (current-time))
713 ;; Files with no autoload cookies or whose autoloads go to other
714 ;; files because of file-local autoload-generated-file settings.
715 (no-autoloads nil)
716 (autoload-modified-buffers nil)
717 (generated-autoload-file
718 (if (called-interactively-p 'interactive)
719 (read-file-name "Write autoload definitions to file: ")
720 generated-autoload-file)))
722 (with-current-buffer (autoload-find-generated-file)
723 (save-excursion
724 ;; Canonicalize file names and remove the autoload file itself.
725 (setq files (delete (file-relative-name buffer-file-name)
726 (mapcar 'file-relative-name files)))
728 (goto-char (point-min))
729 (while (search-forward generate-autoload-section-header nil t)
730 (let* ((form (autoload-read-section-header))
731 (file (nth 3 form)))
732 (cond ((and (consp file) (stringp (car file)))
733 ;; This is a list of files that have no autoload cookies.
734 ;; There shouldn't be more than one such entry.
735 ;; Remove the obsolete section.
736 (autoload-remove-section (match-beginning 0))
737 (let ((last-time (nth 4 form)))
738 (dolist (file file)
739 (let ((file-time (nth 5 (file-attributes file))))
740 (when (and file-time
741 (not (time-less-p last-time file-time)))
742 ;; file unchanged
743 (push file no-autoloads)
744 (setq files (delete file files)))))))
745 ((not (stringp file)))
746 ((or (not (file-exists-p file))
747 ;; Remove duplicates as well, just in case.
748 (member file done)
749 ;; If the file is actually excluded.
750 (member (expand-file-name file) autoload-excludes))
751 ;; Remove the obsolete section.
752 (autoload-remove-section (match-beginning 0)))
753 ((not (time-less-p (nth 4 form)
754 (nth 5 (file-attributes file))))
755 ;; File hasn't changed.
756 nil)
758 (autoload-remove-section (match-beginning 0))
759 (if (autoload-generate-file-autoloads
760 ;; Passing `current-buffer' makes it insert at point.
761 file (current-buffer) buffer-file-name)
762 (push file no-autoloads))))
763 (push file done)
764 (setq files (delete file files)))))
765 ;; Elements remaining in FILES have no existing autoload sections yet.
766 (dolist (file files)
767 (cond
768 ((member (expand-file-name file) autoload-excludes) nil)
769 ;; Passing nil as second argument forces
770 ;; autoload-generate-file-autoloads to look for the right
771 ;; spot where to insert each autoloads section.
772 ((autoload-generate-file-autoloads file nil buffer-file-name)
773 (push file no-autoloads))))
775 (when no-autoloads
776 ;; Sort them for better readability.
777 (setq no-autoloads (sort no-autoloads 'string<))
778 ;; Add the `no-autoloads' section.
779 (goto-char (point-max))
780 (search-backward "\f" nil t)
781 (autoload-insert-section-header
782 (current-buffer) nil nil no-autoloads this-time)
783 (insert generate-autoload-section-trailer))
785 (let ((version-control 'never))
786 (save-buffer))
787 ;; In case autoload entries were added to other files because of
788 ;; file-local autoload-generated-file settings.
789 (autoload-save-buffers))))
791 (define-obsolete-function-alias 'update-autoloads-from-directories
792 'update-directory-autoloads "22.1")
794 ;;;###autoload
795 (defun batch-update-autoloads ()
796 "Update loaddefs.el autoloads in batch mode.
797 Calls `update-directory-autoloads' on the command line arguments.
798 Definitions are written to `generated-autoload-file' (which
799 should be non-nil)."
800 ;; For use during the Emacs build process only.
801 ;; Exclude those files that are preloaded on ALL platforms.
802 ;; These are the ones in loadup.el where "(load" is at the start
803 ;; of the line (crude, but it works).
804 (unless autoload-excludes
805 (let ((default-directory (file-name-directory generated-autoload-file))
806 file)
807 (when (file-readable-p "loadup.el")
808 (with-temp-buffer
809 (insert-file-contents "loadup.el")
810 (while (re-search-forward "^(load \"\\([^\"]+\\)\"" nil t)
811 (setq file (match-string 1))
812 (or (string-match "\\.el\\'" file)
813 (setq file (format "%s.el" file)))
814 (or (string-match "\\`site-" file)
815 (push (expand-file-name file) autoload-excludes)))))))
816 (let ((args command-line-args-left))
817 (setq command-line-args-left nil)
818 (apply 'update-directory-autoloads args)))
820 (provide 'autoload)
822 ;;; autoload.el ends here