* lisp/emacs-lisp/macroexp.el (macroexpand-all-1): Don't spam the output
[emacs.git] / lisp / emacs-lisp / autoload.el
blobd9fc0fccf0ecbc221d3bac4a93eed7f01ba31126
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))
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 defun* defmacro* define-overloadable-function))
155 (let* ((macrop (memq car '(defmacro defmacro*)))
156 (name (nth 1 form))
157 (args (case car
158 ((defun defmacro defun* defmacro*
159 define-overloadable-function) (nth 2 form))
160 ((define-skeleton) '(&optional str arg))
161 ((define-generic-mode define-derived-mode
162 define-compilation-mode) nil)
163 (t)))
164 (body (nthcdr (get car 'doc-string-elt) form))
165 (doc (if (stringp (car body)) (pop body))))
166 ;; Add the usage form at the end where describe-function-1
167 ;; can recover it.
168 (when (listp args) (setq doc (help-add-fundoc-usage doc args)))
169 ;; `define-generic-mode' quotes the name, so take care of that
170 (list 'autoload (if (listp name) name (list 'quote name))
171 file doc
172 (or (and (memq car '(define-skeleton define-derived-mode
173 define-generic-mode
174 easy-mmode-define-global-mode
175 define-global-minor-mode
176 define-globalized-minor-mode
177 easy-mmode-define-minor-mode
178 define-minor-mode)) t)
179 (eq (car-safe (car body)) 'interactive))
180 (if macrop (list 'quote 'macro) nil))))
182 ;; For defclass forms, use `eieio-defclass-autoload'.
183 ((eq car 'defclass)
184 (let ((name (nth 1 form))
185 (superclasses (nth 2 form))
186 (doc (nth 4 form)))
187 (list 'eieio-defclass-autoload (list 'quote name)
188 (list 'quote superclasses) file doc)))
190 ;; Convert defcustom to less space-consuming data.
191 ((eq car 'defcustom)
192 (let ((varname (car-safe (cdr-safe form)))
193 (init (car-safe (cdr-safe (cdr-safe form))))
194 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form)))))
195 ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
197 `(progn
198 (defvar ,varname ,init ,doc)
199 (custom-autoload ',varname ,file
200 ,(condition-case nil
201 (null (cadr (memq :set form)))
202 (error nil))))))
204 ((eq car 'defgroup)
205 ;; In Emacs this is normally handled separately by cus-dep.el, but for
206 ;; third party packages, it can be convenient to explicitly autoload
207 ;; a group.
208 (let ((groupname (nth 1 form)))
209 `(let ((loads (get ',groupname 'custom-loads)))
210 (if (member ',file loads) nil
211 (put ',groupname 'custom-loads (cons ',file loads))))))
213 ;; When processing a macro expansion, any expression
214 ;; before a :autoload-end should be included. These are typically (put
215 ;; 'fun 'prop val) and things like that.
216 ((and expansion (consp form)) form)
218 ;; nil here indicates that this is not a special autoload form.
219 (t nil))))
221 ;; Forms which have doc-strings which should be printed specially.
222 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
223 ;; the doc-string in FORM.
224 ;; Those properties are now set in lisp-mode.el.
226 (defun autoload-find-generated-file ()
227 "Visit the autoload file for the current buffer, and return its buffer.
228 If a buffer is visiting the desired autoload file, return it."
229 (let ((enable-local-variables :safe))
230 ;; We used to use `raw-text' to read this file, but this causes
231 ;; problems when the file contains non-ASCII characters.
232 (find-file-noselect
233 (autoload-ensure-default-file (autoload-generated-file)))))
235 (defun autoload-generated-file ()
236 (expand-file-name generated-autoload-file
237 ;; File-local settings of generated-autoload-file should
238 ;; be interpreted relative to the file's location,
239 ;; of course.
240 (if (not (local-variable-p 'generated-autoload-file))
241 (expand-file-name "lisp" source-directory))))
244 (defun autoload-read-section-header ()
245 "Read a section header form.
246 Since continuation lines have been marked as comments,
247 we must copy the text of the form and remove those comment
248 markers before we call `read'."
249 (save-match-data
250 (let ((beginning (point))
251 string)
252 (forward-line 1)
253 (while (looking-at generate-autoload-section-continuation)
254 (forward-line 1))
255 (setq string (buffer-substring beginning (point)))
256 (with-current-buffer (get-buffer-create " *autoload*")
257 (erase-buffer)
258 (insert string)
259 (goto-char (point-min))
260 (while (search-forward generate-autoload-section-continuation nil t)
261 (replace-match " "))
262 (goto-char (point-min))
263 (read (current-buffer))))))
265 (defvar autoload-print-form-outbuf nil
266 "Buffer which gets the output of `autoload-print-form'.")
268 (defun autoload-print-form (form)
269 "Print FORM such that `make-docfile' will find the docstrings.
270 The variable `autoload-print-form-outbuf' specifies the buffer to
271 put the output in."
272 (cond
273 ;; If the form is a sequence, recurse.
274 ((eq (car form) 'progn) (mapcar 'autoload-print-form (cdr form)))
275 ;; Symbols at the toplevel are meaningless.
276 ((symbolp form) nil)
278 (let ((doc-string-elt (get (car-safe form) 'doc-string-elt))
279 (outbuf autoload-print-form-outbuf))
280 (if (and doc-string-elt (stringp (nth doc-string-elt form)))
281 ;; We need to hack the printing because the
282 ;; doc-string must be printed specially for
283 ;; make-docfile (sigh).
284 (let* ((p (nthcdr (1- doc-string-elt) form))
285 (elt (cdr p)))
286 (setcdr p nil)
287 (princ "\n(" outbuf)
288 (let ((print-escape-newlines t)
289 (print-quoted t)
290 (print-escape-nonascii t))
291 (dolist (elt form)
292 (prin1 elt outbuf)
293 (princ " " outbuf)))
294 (princ "\"\\\n" outbuf)
295 (let ((begin (with-current-buffer outbuf (point))))
296 (princ (substring (prin1-to-string (car elt)) 1)
297 outbuf)
298 ;; Insert a backslash before each ( that
299 ;; appears at the beginning of a line in
300 ;; the doc string.
301 (with-current-buffer outbuf
302 (save-excursion
303 (while (re-search-backward "\n[[(]" begin t)
304 (forward-char 1)
305 (insert "\\"))))
306 (if (null (cdr elt))
307 (princ ")" outbuf)
308 (princ " " outbuf)
309 (princ (substring (prin1-to-string (cdr elt)) 1)
310 outbuf))
311 (terpri outbuf)))
312 (let ((print-escape-newlines t)
313 (print-quoted t)
314 (print-escape-nonascii t))
315 (print form outbuf)))))))
317 (defun autoload-rubric (file &optional type feature)
318 "Return a string giving the appropriate autoload rubric for FILE.
319 TYPE (default \"autoloads\") is a string stating the type of
320 information contained in FILE. If FEATURE is non-nil, FILE
321 will provide a feature. FEATURE may be a string naming the
322 feature, otherwise it will be based on FILE's name.
324 At present, a feature is in fact always provided, but this should
325 not be relied upon."
326 (let ((basename (file-name-nondirectory file)))
327 (concat ";;; " basename
328 " --- automatically extracted " (or type "autoloads") "\n"
329 ";;\n"
330 ";;; Code:\n\n"
331 "\f\n"
332 ;; This is used outside of autoload.el, eg cus-dep, finder.
333 "(provide '"
334 (if (stringp feature)
335 feature
336 (file-name-sans-extension basename))
337 ")\n"
338 ";; Local Variables:\n"
339 ";; version-control: never\n"
340 ";; no-byte-compile: t\n"
341 ";; no-update-autoloads: t\n"
342 ";; coding: utf-8\n"
343 ";; End:\n"
344 ";;; " basename
345 " ends here\n")))
347 (defun autoload-ensure-default-file (file)
348 "Make sure that the autoload file FILE exists and if not create it."
349 (unless (file-exists-p file)
350 (write-region (autoload-rubric file) nil file))
351 file)
353 (defun autoload-insert-section-header (outbuf autoloads load-name file time)
354 "Insert the section-header line,
355 which lists the file name and which functions are in it, etc."
356 (insert generate-autoload-section-header)
357 (prin1 (list 'autoloads autoloads load-name file time)
358 outbuf)
359 (terpri outbuf)
360 ;; Break that line at spaces, to avoid very long lines.
361 ;; Make each sub-line into a comment.
362 (with-current-buffer outbuf
363 (save-excursion
364 (forward-line -1)
365 (while (not (eolp))
366 (move-to-column 64)
367 (skip-chars-forward "^ \n")
368 (or (eolp)
369 (insert "\n" generate-autoload-section-continuation))))))
371 (defun autoload-find-file (file)
372 "Fetch file and put it in a temp buffer. Return the buffer."
373 ;; It is faster to avoid visiting the file.
374 (setq file (expand-file-name file))
375 (with-current-buffer (get-buffer-create " *autoload-file*")
376 (kill-all-local-variables)
377 (erase-buffer)
378 (setq buffer-undo-list t
379 buffer-read-only nil)
380 (emacs-lisp-mode)
381 (setq default-directory (file-name-directory file))
382 (insert-file-contents file nil)
383 (let ((enable-local-variables :safe))
384 (hack-local-variables))
385 (current-buffer)))
387 (defvar no-update-autoloads nil
388 "File local variable to prevent scanning this file for autoload cookies.")
390 (defun autoload-file-load-name (file)
391 "Compute the name that will be used to load FILE."
392 ;; OUTFILE should be the name of the global loaddefs.el file, which
393 ;; is expected to be at the root directory of the files we're
394 ;; scanning for autoloads and will be in the `load-path'.
395 (let* ((outfile (default-value 'generated-autoload-file))
396 (name (file-relative-name file (file-name-directory outfile)))
397 (names '())
398 (dir (file-name-directory outfile)))
399 ;; If `name' has directory components, only keep the
400 ;; last few that are really needed.
401 (while name
402 (setq name (directory-file-name name))
403 (push (file-name-nondirectory name) names)
404 (setq name (file-name-directory name)))
405 (while (not name)
406 (cond
407 ((null (cdr names)) (setq name (car names)))
408 ((file-exists-p (expand-file-name "subdirs.el" dir))
409 ;; FIXME: here we only check the existence of subdirs.el,
410 ;; without checking its content. This makes it generate wrong load
411 ;; names for cases like lisp/term which is not added to load-path.
412 (setq dir (expand-file-name (pop names) dir)))
413 (t (setq name (mapconcat 'identity names "/")))))
414 (if (string-match "\\.elc?\\(\\.\\|\\'\\)" name)
415 (substring name 0 (match-beginning 0))
416 name)))
418 (defun generate-file-autoloads (file)
419 "Insert at point a loaddefs autoload section for FILE.
420 Autoloads are generated for defuns and defmacros in FILE
421 marked by `generate-autoload-cookie' (which see).
422 If FILE is being visited in a buffer, the contents of the buffer
423 are used.
424 Return non-nil in the case where no autoloads were added at point."
425 (interactive "fGenerate autoloads for file: ")
426 (let ((generated-autoload-file buffer-file-name))
427 (autoload-generate-file-autoloads file (current-buffer))))
429 (defvar print-readably)
431 ;; When called from `generate-file-autoloads' we should ignore
432 ;; `generated-autoload-file' altogether. When called from
433 ;; `update-file-autoloads' we don't know `outbuf'. And when called from
434 ;; `update-directory-autoloads' it's in between: we know the default
435 ;; `outbuf' but we should obey any file-local setting of
436 ;; `generated-autoload-file'.
437 (defun autoload-generate-file-autoloads (file &optional outbuf outfile)
438 "Insert an autoload section for FILE in the appropriate buffer.
439 Autoloads are generated for defuns and defmacros in FILE
440 marked by `generate-autoload-cookie' (which see).
441 If FILE is being visited in a buffer, the contents of the buffer are used.
442 OUTBUF is the buffer in which the autoload statements should be inserted.
443 If OUTBUF is nil, it will be determined by `autoload-generated-file'.
445 If provided, OUTFILE is expected to be the file name of OUTBUF.
446 If OUTFILE is non-nil and FILE specifies a `generated-autoload-file'
447 different from OUTFILE, then OUTBUF is ignored.
449 Return non-nil if and only if FILE adds no autoloads to OUTFILE
450 \(or OUTBUF if OUTFILE is nil)."
451 (catch 'done
452 (let ((autoloads-done '())
453 load-name
454 (print-length nil)
455 (print-level nil)
456 (print-readably t) ; This does something in Lucid Emacs.
457 (float-output-format nil)
458 (visited (get-file-buffer file))
459 (otherbuf nil)
460 (absfile (expand-file-name file))
461 ;; nil until we found a cookie.
462 output-start ostart)
463 (with-current-buffer (or visited
464 ;; It is faster to avoid visiting the file.
465 (autoload-find-file file))
466 ;; Obey the no-update-autoloads file local variable.
467 (unless no-update-autoloads
468 (message "Generating autoloads for %s..." file)
469 (setq load-name
470 (if (stringp generated-autoload-load-name)
471 generated-autoload-load-name
472 (autoload-file-load-name absfile)))
473 (when (and outfile
474 (not
475 (if (memq system-type '(ms-dos windows-nt))
476 (equal (downcase outfile)
477 (downcase (autoload-generated-file)))
478 (equal outfile (autoload-generated-file)))))
479 (setq otherbuf t))
480 (save-excursion
481 (save-restriction
482 (widen)
483 (goto-char (point-min))
484 (while (not (eobp))
485 (skip-chars-forward " \t\n\f")
486 (cond
487 ((looking-at (regexp-quote generate-autoload-cookie))
488 ;; If not done yet, figure out where to insert this text.
489 (unless output-start
490 (let ((outbuf
491 (or (if otherbuf
492 ;; A file-local setting of
493 ;; autoload-generated-file says we
494 ;; should ignore OUTBUF.
496 outbuf)
497 (autoload-find-destination absfile load-name)
498 ;; The file has autoload cookies, but they're
499 ;; already up-to-date. If OUTFILE is nil, the
500 ;; entries are in the expected OUTBUF,
501 ;; otherwise they're elsewhere.
502 (throw 'done otherbuf))))
503 (with-current-buffer outbuf
504 (setq output-start (point-marker)
505 ostart (point)))))
506 (search-forward generate-autoload-cookie)
507 (skip-chars-forward " \t")
508 (if (eolp)
509 (condition-case-unless-debug err
510 ;; Read the next form and make an autoload.
511 (let* ((form (prog1 (read (current-buffer))
512 (or (bolp) (forward-line 1))))
513 (autoload (make-autoload form load-name)))
514 (if autoload
515 (push (nth 1 form) autoloads-done)
516 (setq autoload form))
517 (let ((autoload-print-form-outbuf
518 (marker-buffer output-start)))
519 (autoload-print-form autoload)))
520 (error
521 (message "Autoload cookie error in %s:%s %S"
522 file (count-lines (point-min) (point)) err)))
524 ;; Copy the rest of the line to the output.
525 (princ (buffer-substring
526 (progn
527 ;; Back up over whitespace, to preserve it.
528 (skip-chars-backward " \f\t")
529 (if (= (char-after (1+ (point))) ? )
530 ;; Eat one space.
531 (forward-char 1))
532 (point))
533 (progn (forward-line 1) (point)))
534 (marker-buffer output-start))))
535 ((looking-at ";")
536 ;; Don't read the comment.
537 (forward-line 1))
539 (forward-sexp 1)
540 (forward-line 1))))))
542 (when output-start
543 (let ((secondary-autoloads-file-buf
544 (if otherbuf (current-buffer))))
545 (with-current-buffer (marker-buffer output-start)
546 (save-excursion
547 ;; Insert the section-header line which lists the file name
548 ;; and which functions are in it, etc.
549 (assert (= ostart output-start))
550 (goto-char output-start)
551 (let ((relfile (file-relative-name absfile)))
552 (autoload-insert-section-header
553 (marker-buffer output-start)
554 autoloads-done load-name relfile
555 (if secondary-autoloads-file-buf
556 ;; MD5 checksums are much better because they do not
557 ;; change unless the file changes (so they'll be
558 ;; equal on two different systems and will change
559 ;; less often than time-stamps, thus leading to fewer
560 ;; unneeded changes causing spurious conflicts), but
561 ;; using time-stamps is a very useful optimization,
562 ;; so we use time-stamps for the main autoloads file
563 ;; (loaddefs.el) where we have special ways to
564 ;; circumvent the "random change problem", and MD5
565 ;; checksum in secondary autoload files where we do
566 ;; not need the time-stamp optimization because it is
567 ;; already provided by the primary autoloads file.
568 (md5 secondary-autoloads-file-buf
569 ;; We'd really want to just use
570 ;; `emacs-internal' instead.
571 nil nil 'emacs-mule-unix)
572 (nth 5 (file-attributes relfile))))
573 (insert ";;; Generated autoloads from " relfile "\n")))
574 (insert generate-autoload-section-trailer))))
575 (message "Generating autoloads for %s...done" file))
576 (or visited
577 ;; We created this buffer, so we should kill it.
578 (kill-buffer (current-buffer))))
579 (or (not output-start)
580 ;; If the entries were added to some other buffer, then the file
581 ;; doesn't add entries to OUTFILE.
582 otherbuf))))
584 (defun autoload-save-buffers ()
585 (while autoload-modified-buffers
586 (with-current-buffer (pop autoload-modified-buffers)
587 (let ((version-control 'never))
588 (save-buffer)))))
590 ;;;###autoload
591 (defun update-file-autoloads (file &optional save-after outfile)
592 "Update the autoloads for FILE.
593 If prefix arg SAVE-AFTER is non-nil, save the buffer too.
595 If FILE binds `generated-autoload-file' as a file-local variable,
596 autoloads are written into that file. Otherwise, the autoloads
597 file is determined by OUTFILE. If called interactively, prompt
598 for OUTFILE; if called from Lisp with OUTFILE nil, use the
599 existing value of `generated-autoload-file'.
601 Return FILE if there was no autoload cookie in it, else nil."
602 (interactive (list (read-file-name "Update autoloads for file: ")
603 current-prefix-arg
604 (read-file-name "Write autoload definitions to file: ")))
605 (let* ((generated-autoload-file (or outfile generated-autoload-file))
606 (autoload-modified-buffers nil)
607 (no-autoloads (autoload-generate-file-autoloads file)))
608 (if autoload-modified-buffers
609 (if save-after (autoload-save-buffers))
610 (if (called-interactively-p 'interactive)
611 (message "Autoload section for %s is up to date." file)))
612 (if no-autoloads file)))
614 (defun autoload-find-destination (file load-name)
615 "Find the destination point of the current buffer's autoloads.
616 FILE is the file name of the current buffer.
617 Returns a buffer whose point is placed at the requested location.
618 Returns nil if the file's autoloads are uptodate, otherwise
619 removes any prior now out-of-date autoload entries."
620 (catch 'up-to-date
621 (let* ((buf (current-buffer))
622 (existing-buffer (if buffer-file-name buf))
623 (found nil))
624 (with-current-buffer (autoload-find-generated-file)
625 ;; This is to make generated-autoload-file have Unix EOLs, so
626 ;; that it is portable to all platforms.
627 (or (eq 0 (coding-system-eol-type buffer-file-coding-system))
628 (set-buffer-file-coding-system 'unix))
629 (or (> (buffer-size) 0)
630 (error "Autoloads file %s lacks boilerplate" buffer-file-name))
631 (or (file-writable-p buffer-file-name)
632 (error "Autoloads file %s is not writable" buffer-file-name))
633 (widen)
634 (goto-char (point-min))
635 ;; Look for the section for LOAD-NAME.
636 (while (and (not found)
637 (search-forward generate-autoload-section-header nil t))
638 (let ((form (autoload-read-section-header)))
639 (cond ((string= (nth 2 form) load-name)
640 ;; We found the section for this file.
641 ;; Check if it is up to date.
642 (let ((begin (match-beginning 0))
643 (last-time (nth 4 form))
644 (file-time (nth 5 (file-attributes file))))
645 (if (and (or (null existing-buffer)
646 (not (buffer-modified-p existing-buffer)))
648 ;; last-time is the time-stamp (specifying
649 ;; the last time we looked at the file) and
650 ;; the file hasn't been changed since.
651 (and (listp last-time) (= (length last-time) 2)
652 (not (time-less-p last-time file-time)))
653 ;; last-time is an MD5 checksum instead.
654 (and (stringp last-time)
655 (equal last-time
656 (md5 buf nil nil 'emacs-mule)))))
657 (throw 'up-to-date nil)
658 (autoload-remove-section begin)
659 (setq found t))))
660 ((string< load-name (nth 2 form))
661 ;; We've come to a section alphabetically later than
662 ;; LOAD-NAME. We assume the file is in order and so
663 ;; there must be no section for LOAD-NAME. We will
664 ;; insert one before the section here.
665 (goto-char (match-beginning 0))
666 (setq found t)))))
667 (or found
668 (progn
669 ;; No later sections in the file. Put before the last page.
670 (goto-char (point-max))
671 (search-backward "\f" nil t)))
672 (unless (memq (current-buffer) autoload-modified-buffers)
673 (push (current-buffer) autoload-modified-buffers))
674 (current-buffer)))))
676 (defun autoload-remove-section (begin)
677 (goto-char begin)
678 (search-forward generate-autoload-section-trailer)
679 (delete-region begin (point)))
681 ;;;###autoload
682 (defun update-directory-autoloads (&rest dirs)
683 "Update autoload definitions for Lisp files in the directories DIRS.
684 In an interactive call, you must give one argument, the name of a
685 single directory. In a call from Lisp, you can supply multiple
686 directories as separate arguments, but this usage is discouraged.
688 The function does NOT recursively descend into subdirectories of the
689 directory or directories specified.
691 In an interactive call, prompt for a default output file for the
692 autoload definitions, and temporarily bind the variable
693 `generated-autoload-file' to this value. When called from Lisp,
694 use the existing value of `generated-autoload-file'. If any Lisp
695 file binds `generated-autoload-file' as a file-local variable,
696 write its autoloads into the specified file instead."
697 (interactive "DUpdate autoloads from directory: ")
698 (let* ((files-re (let ((tmp nil))
699 (dolist (suf (get-load-suffixes))
700 (unless (string-match "\\.elc" suf) (push suf tmp)))
701 (concat "^[^=.].*" (regexp-opt tmp t) "\\'")))
702 (files (apply 'nconc
703 (mapcar (lambda (dir)
704 (directory-files (expand-file-name dir)
705 t files-re))
706 dirs)))
707 (done ())
708 (this-time (current-time))
709 ;; Files with no autoload cookies or whose autoloads go to other
710 ;; files because of file-local autoload-generated-file settings.
711 (no-autoloads nil)
712 (autoload-modified-buffers nil)
713 (generated-autoload-file
714 (if (called-interactively-p 'interactive)
715 (read-file-name "Write autoload definitions to file: ")
716 generated-autoload-file)))
718 (with-current-buffer (autoload-find-generated-file)
719 (save-excursion
720 ;; Canonicalize file names and remove the autoload file itself.
721 (setq files (delete (file-relative-name buffer-file-name)
722 (mapcar 'file-relative-name files)))
724 (goto-char (point-min))
725 (while (search-forward generate-autoload-section-header nil t)
726 (let* ((form (autoload-read-section-header))
727 (file (nth 3 form)))
728 (cond ((and (consp file) (stringp (car file)))
729 ;; This is a list of files that have no autoload cookies.
730 ;; There shouldn't be more than one such entry.
731 ;; Remove the obsolete section.
732 (autoload-remove-section (match-beginning 0))
733 (let ((last-time (nth 4 form)))
734 (dolist (file file)
735 (let ((file-time (nth 5 (file-attributes file))))
736 (when (and file-time
737 (not (time-less-p last-time file-time)))
738 ;; file unchanged
739 (push file no-autoloads)
740 (setq files (delete file files)))))))
741 ((not (stringp file)))
742 ((or (not (file-exists-p file))
743 ;; Remove duplicates as well, just in case.
744 (member file done)
745 ;; If the file is actually excluded.
746 (member (expand-file-name file) autoload-excludes))
747 ;; Remove the obsolete section.
748 (autoload-remove-section (match-beginning 0)))
749 ((not (time-less-p (nth 4 form)
750 (nth 5 (file-attributes file))))
751 ;; File hasn't changed.
752 nil)
754 (autoload-remove-section (match-beginning 0))
755 (if (autoload-generate-file-autoloads
756 ;; Passing `current-buffer' makes it insert at point.
757 file (current-buffer) buffer-file-name)
758 (push file no-autoloads))))
759 (push file done)
760 (setq files (delete file files)))))
761 ;; Elements remaining in FILES have no existing autoload sections yet.
762 (dolist (file files)
763 (cond
764 ((member (expand-file-name file) autoload-excludes) nil)
765 ;; Passing nil as second argument forces
766 ;; autoload-generate-file-autoloads to look for the right
767 ;; spot where to insert each autoloads section.
768 ((autoload-generate-file-autoloads file nil buffer-file-name)
769 (push file no-autoloads))))
771 (when no-autoloads
772 ;; Sort them for better readability.
773 (setq no-autoloads (sort no-autoloads 'string<))
774 ;; Add the `no-autoloads' section.
775 (goto-char (point-max))
776 (search-backward "\f" nil t)
777 (autoload-insert-section-header
778 (current-buffer) nil nil no-autoloads this-time)
779 (insert generate-autoload-section-trailer))
781 (let ((version-control 'never))
782 (save-buffer))
783 ;; In case autoload entries were added to other files because of
784 ;; file-local autoload-generated-file settings.
785 (autoload-save-buffers))))
787 (define-obsolete-function-alias 'update-autoloads-from-directories
788 'update-directory-autoloads "22.1")
790 ;;;###autoload
791 (defun batch-update-autoloads ()
792 "Update loaddefs.el autoloads in batch mode.
793 Calls `update-directory-autoloads' on the command line arguments.
794 Definitions are written to `generated-autoload-file' (which
795 should be non-nil)."
796 ;; For use during the Emacs build process only.
797 ;; Exclude those files that are preloaded on ALL platforms.
798 ;; These are the ones in loadup.el where "(load" is at the start
799 ;; of the line (crude, but it works).
800 (unless autoload-excludes
801 (let ((default-directory (file-name-directory generated-autoload-file))
802 file)
803 (when (file-readable-p "loadup.el")
804 (with-temp-buffer
805 (insert-file-contents "loadup.el")
806 (while (re-search-forward "^(load \"\\([^\"]+\\)\"" nil t)
807 (setq file (match-string 1))
808 (or (string-match "\\.el\\'" file)
809 (setq file (format "%s.el" file)))
810 (or (string-match "\\`site-" file)
811 (push (expand-file-name file) autoload-excludes)))))))
812 (let ((args command-line-args-left))
813 (setq command-line-args-left nil)
814 (apply 'update-directory-autoloads args)))
816 (provide 'autoload)
818 ;;; autoload.el ends here