1 ;; autoload.el --- maintain autoloads in loaddefs.el -*- lexical-binding: t -*-
3 ;; Copyright (C) 1991-1997, 2001-2016 Free Software Foundation, Inc.
5 ;; Author: Roland McGrath <roland@gnu.org>
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/>.
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.
33 (require 'lisp-mode) ;for `doc-string-elt' properties.
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
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
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 Typically, you need to set this when the directory containing the file
57 is not in `load-path'.
58 This also affects the generated cus-load.el file.")
60 (put 'generated-autoload-load-name 'safe-local-variable 'stringp)
62 ;; This feels like it should be a defconst, but MH-E sets it to
63 ;; ";;;###mh-autoload" for the autoloads that are to go into mh-loaddefs.el.
64 (defvar generate-autoload-cookie ";;;###autoload"
65 "Magic comment indicating the following form should be autoloaded.
66 Used by \\[update-file-autoloads]. This string should be
67 meaningless to Lisp (e.g., a comment).
72 \(defun function-to-be-autoloaded () ...)
74 If this string appears alone on a line, the following form will be
75 read and an autoload made for it. If there is further text on the line,
76 that text will be copied verbatim to `generated-autoload-file'.")
78 (defvar autoload-excludes nil
79 "If non-nil, list of absolute file names not to scan for autoloads.")
81 (defconst generate-autoload-section-header "\f\n;;;### "
82 "String that marks the form at the start of a new file's autoload section.")
84 (defconst generate-autoload-section-trailer "\n;;;***\n"
85 "String which indicates the end of the section of autoloads for a file.")
87 (defconst generate-autoload-section-continuation ";;;;;; "
88 "String to add on each continuation of the section header form.")
90 ;; In some ways it would be nicer to use a value that is recognisably
91 ;; not a time-value, eg t, but that can cause issues if an older Emacs
92 ;; that does not expect non-time-values loads the file.
93 (defconst autoload--non-timestamp '(0 0 0 0)
94 "Value to insert when `autoload-timestamps' is nil.")
96 (defvar autoload-timestamps nil ; experimental, see bug#22213
97 "Non-nil means insert a timestamp for each input file into the output.
98 We use these in incremental updates of the output file to decide
99 if we need to rescan an input file. If you set this to nil,
100 then we use the timestamp of the output file instead. As a result:
101 - for fixed inputs, the output will be the same every time
102 - incremental updates of the output file might not be correct if:
103 i) the timestamp of the output file cannot be trusted (at least
104 relative to that of the input files)
105 ii) any of the input files can be modified during the time it takes
107 iii) only a subset of the input files are scanned
108 These issues are unlikely to happen in practice, and would arguably
109 represent bugs in the build system. Item iii) will happen if you
110 use a command like `update-file-autoloads', though, since it only
111 checks a single input file.")
113 (defvar autoload-modified-buffers) ;Dynamically scoped var.
115 (defun make-autoload (form file &optional expansion)
116 "Turn FORM into an autoload or defvar for source file FILE.
117 Returns nil if FORM is not a special autoload form (i.e. a function definition
118 or macro definition or a defcustom).
119 If EXPANSION is non-nil, we're processing the macro expansion of an
120 expression, in which case we want to handle forms differently."
121 (let ((car (car-safe form)) expand)
123 ((and expansion (eq car 'defalias))
125 ((`(,_ ,_ ,arg . ,rest) form)
126 ;; `type' is non-nil if it defines a macro.
127 ;; `fun' is the function part of `arg' (defaults to `arg').
128 ((or (and (or `(cons 'macro ,fun) `'(macro . ,fun)) (let type t))
129 (and (let fun arg) (let type nil)))
131 ;; `lam' is the lambda expression in `fun' (or nil if not
133 (lam (if (memq (car-safe fun) '(quote function)) (cadr fun)))
134 ;; `args' is the list of arguments (or t if not recognized).
135 ;; `body' is the body of `lam' (or t if not recognized).
136 ((or `(lambda ,args . ,body)
137 (and (let args t) (let body t)))
139 ;; Get the `doc' from `body' or `rest'.
140 (doc (cond ((stringp (car-safe body)) (car body))
141 ((stringp (car-safe rest)) (car rest))))
142 ;; Look for an interactive spec.
143 (interactive (pcase body
144 ((or `((interactive . ,_) . ,_)
145 `(,_ (interactive . ,_) . ,_))
147 ;; Add the usage form at the end where describe-function-1
149 (when (listp args) (setq doc (help-add-fundoc-usage doc args)))
150 ;; (message "autoload of %S" (nth 1 form))
151 `(autoload ,(nth 1 form) ,file ,doc ,interactive ,type)))
153 ((and expansion (memq car '(progn prog1)))
154 (let ((end (memq :autoload-end form)))
155 (when end ;Cut-off anything after the :autoload-end marker.
156 (setq form (copy-sequence form))
157 (setcdr (memq :autoload-end form) nil))
158 (let ((exps (delq nil (mapcar (lambda (form)
159 (make-autoload form file expansion))
161 (when exps (cons 'progn exps)))))
163 ;; For complex cases, try again on the macro-expansion.
164 ((and (memq car '(easy-mmode-define-global-mode define-global-minor-mode
165 define-globalized-minor-mode defun defmacro
166 easy-mmode-define-minor-mode define-minor-mode
167 define-inline cl-defun cl-defmacro))
169 (setq expand (let ((load-file-name file)) (macroexpand form)))
170 (memq (car expand) '(progn prog1 defalias)))
171 (make-autoload expand file 'expansion)) ;Recurse on the expansion.
173 ;; For special function-like operators, use the `autoload' function.
174 ((memq car '(define-skeleton define-derived-mode
175 define-compilation-mode define-generic-mode
176 easy-mmode-define-global-mode define-global-minor-mode
177 define-globalized-minor-mode
178 easy-mmode-define-minor-mode define-minor-mode
179 cl-defun defun* cl-defmacro defmacro*
180 define-overloadable-function))
181 (let* ((macrop (memq car '(defmacro cl-defmacro defmacro*)))
184 ((or `defun `defmacro
185 `defun* `defmacro* `cl-defun `cl-defmacro
186 `define-overloadable-function) (nth 2 form))
187 (`define-skeleton '(&optional str arg))
188 ((or `define-generic-mode `define-derived-mode
189 `define-compilation-mode) nil)
191 (body (nthcdr (or (function-get car 'doc-string-elt) 3) form))
192 (doc (if (stringp (car body)) (pop body))))
193 ;; Add the usage form at the end where describe-function-1
195 (when (listp args) (setq doc (help-add-fundoc-usage doc args)))
196 ;; `define-generic-mode' quotes the name, so take care of that
197 `(autoload ,(if (listp name) name (list 'quote name))
199 ,(or (and (memq car '(define-skeleton define-derived-mode
201 easy-mmode-define-global-mode
202 define-global-minor-mode
203 define-globalized-minor-mode
204 easy-mmode-define-minor-mode
205 define-minor-mode)) t)
206 (eq (car-safe (car body)) 'interactive))
207 ,(if macrop ''macro nil))))
209 ;; For defclass forms, use `eieio-defclass-autoload'.
211 (let ((name (nth 1 form))
212 (superclasses (nth 2 form))
214 (list 'eieio-defclass-autoload (list 'quote name)
215 (list 'quote superclasses) file doc)))
217 ;; Convert defcustom to less space-consuming data.
219 (let ((varname (car-safe (cdr-safe form)))
220 (init (car-safe (cdr-safe (cdr-safe form))))
221 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form)))))
222 ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
225 (defvar ,varname ,init ,doc)
226 (custom-autoload ',varname ,file
228 (null (cadr (memq :set form)))
232 ;; In Emacs this is normally handled separately by cus-dep.el, but for
233 ;; third party packages, it can be convenient to explicitly autoload
235 (let ((groupname (nth 1 form)))
236 `(let ((loads (get ',groupname 'custom-loads)))
237 (if (member ',file loads) nil
238 (put ',groupname 'custom-loads (cons ',file loads))))))
240 ;; When processing a macro expansion, any expression
241 ;; before a :autoload-end should be included. These are typically (put
242 ;; 'fun 'prop val) and things like that.
243 ((and expansion (consp form)) form)
245 ;; nil here indicates that this is not a special autoload form.
248 ;; Forms which have doc-strings which should be printed specially.
249 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
250 ;; the doc-string in FORM.
251 ;; Those properties are now set in lisp-mode.el.
253 (defun autoload-find-generated-file ()
254 "Visit the autoload file for the current buffer, and return its buffer.
255 If a buffer is visiting the desired autoload file, return it."
256 (let ((enable-local-variables :safe)
257 (enable-local-eval nil))
258 ;; We used to use `raw-text' to read this file, but this causes
259 ;; problems when the file contains non-ASCII characters.
260 (let* ((delay-mode-hooks t)
261 (file (autoload-generated-file))
262 (file-missing (not (file-exists-p file))))
264 (autoload-ensure-default-file file))
267 (autoload-ensure-file-writeable
269 ;; block backups when the file has just been created, since
270 ;; the backups will just be the auto-generated headers.
273 (setq buffer-backed-up t)
277 (defun autoload-generated-file ()
278 (expand-file-name generated-autoload-file
279 ;; File-local settings of generated-autoload-file should
280 ;; be interpreted relative to the file's location,
282 (if (not (local-variable-p 'generated-autoload-file))
283 (expand-file-name "lisp" source-directory))))
286 (defun autoload-read-section-header ()
287 "Read a section header form.
288 Since continuation lines have been marked as comments,
289 we must copy the text of the form and remove those comment
290 markers before we call `read'."
292 (let ((beginning (point))
295 (while (looking-at generate-autoload-section-continuation)
297 (setq string (buffer-substring beginning (point)))
298 (with-current-buffer (get-buffer-create " *autoload*")
301 (goto-char (point-min))
302 (while (search-forward generate-autoload-section-continuation nil t)
304 (goto-char (point-min))
305 (read (current-buffer))))))
307 (defvar autoload-print-form-outbuf nil
308 "Buffer which gets the output of `autoload-print-form'.")
310 (defun autoload-print-form (form)
311 "Print FORM such that `make-docfile' will find the docstrings.
312 The variable `autoload-print-form-outbuf' specifies the buffer to
315 ;; If the form is a sequence, recurse.
316 ((eq (car form) 'progn) (mapcar 'autoload-print-form (cdr form)))
317 ;; Symbols at the toplevel are meaningless.
320 (let ((doc-string-elt (function-get (car-safe form) 'doc-string-elt))
321 (outbuf autoload-print-form-outbuf))
322 (if (and doc-string-elt (stringp (nth doc-string-elt form)))
323 ;; We need to hack the printing because the
324 ;; doc-string must be printed specially for
325 ;; make-docfile (sigh).
326 (let* ((p (nthcdr (1- doc-string-elt) form))
330 (let ((print-escape-newlines t)
332 (print-escape-nonascii t))
336 (princ "\"\\\n" outbuf)
337 (let ((begin (with-current-buffer outbuf (point))))
338 (princ (substring (prin1-to-string (car elt)) 1)
340 ;; Insert a backslash before each ( that
341 ;; appears at the beginning of a line in
343 (with-current-buffer outbuf
345 (while (re-search-backward "\n[[(]" begin t)
351 (princ (substring (prin1-to-string (cdr elt)) 1)
354 (let ((print-escape-newlines t)
356 (print-escape-nonascii t))
357 (print form outbuf)))))))
359 (defun autoload-rubric (file &optional type feature)
360 "Return a string giving the appropriate autoload rubric for FILE.
361 TYPE (default \"autoloads\") is a string stating the type of
362 information contained in FILE. If FEATURE is non-nil, FILE
363 will provide a feature. FEATURE may be a string naming the
364 feature, otherwise it will be based on FILE's name.
366 At present, a feature is in fact always provided, but this should
368 (let ((basename (file-name-nondirectory file)))
369 (concat ";;; " basename
370 " --- automatically extracted " (or type "autoloads") "\n"
374 ;; This is used outside of autoload.el, eg cus-dep, finder.
376 (if (stringp feature)
378 (file-name-sans-extension basename))
380 ";; Local Variables:\n"
381 ";; version-control: never\n"
382 ";; no-byte-compile: t\n"
383 ";; no-update-autoloads: t\n"
389 (defvar autoload-ensure-writable nil
390 "Non-nil means `autoload-ensure-default-file' makes existing file writable.")
391 ;; Just in case someone tries to get you to overwrite a file that you
394 (put 'autoload-ensure-writable 'risky-local-variable t)
396 (defun autoload-ensure-file-writeable (file)
397 ;; Probably pointless, but replaces the old AUTOGEN_VCS in lisp/Makefile,
398 ;; which was designed to handle CVSREAD=1 and equivalent.
399 (and autoload-ensure-writable
400 (let ((modes (file-modes file)))
401 (if (zerop (logand modes #o0200))
402 ;; Ignore any errors here, and let subsequent attempts
403 ;; to write the file raise any real error.
404 (ignore-errors (set-file-modes file (logior modes #o0200))))))
407 (defun autoload-ensure-default-file (file)
408 "Make sure that the autoload file FILE exists, creating it if needed.
409 If the file already exists and `autoload-ensure-writable' is non-nil,
411 (write-region (autoload-rubric file) nil file))
413 (defun autoload-insert-section-header (outbuf autoloads load-name file time)
414 "Insert the section-header line,
415 which lists the file name and which functions are in it, etc."
416 (insert generate-autoload-section-header)
417 (prin1 `(autoloads ,autoloads ,load-name ,file ,time)
420 ;; Break that line at spaces, to avoid very long lines.
421 ;; Make each sub-line into a comment.
422 (with-current-buffer outbuf
427 (skip-chars-forward "^ \n")
429 (insert "\n" generate-autoload-section-continuation))))))
431 (defun autoload-find-file (file)
432 "Fetch file and put it in a temp buffer. Return the buffer."
433 ;; It is faster to avoid visiting the file.
434 (setq file (expand-file-name file))
435 (with-current-buffer (get-buffer-create " *autoload-file*")
436 (kill-all-local-variables)
438 (setq buffer-undo-list t
439 buffer-read-only nil)
440 (delay-mode-hooks (emacs-lisp-mode))
441 (setq default-directory (file-name-directory file))
442 (insert-file-contents file nil)
443 (let ((enable-local-variables :safe)
444 (enable-local-eval nil))
445 (hack-local-variables))
448 (defvar no-update-autoloads nil
449 "File local variable to prevent scanning this file for autoload cookies.")
451 (defun autoload-file-load-name (file)
452 "Compute the name that will be used to load FILE."
453 ;; OUTFILE should be the name of the global loaddefs.el file, which
454 ;; is expected to be at the root directory of the files we're
455 ;; scanning for autoloads and will be in the `load-path'.
456 (let* ((outfile (default-value 'generated-autoload-file))
457 (name (file-relative-name file (file-name-directory outfile)))
459 (dir (file-name-directory outfile)))
460 ;; If `name' has directory components, only keep the
461 ;; last few that are really needed.
463 (setq name (directory-file-name name))
464 (push (file-name-nondirectory name) names)
465 (setq name (file-name-directory name)))
468 ((null (cdr names)) (setq name (car names)))
469 ((file-exists-p (expand-file-name "subdirs.el" dir))
470 ;; FIXME: here we only check the existence of subdirs.el,
471 ;; without checking its content. This makes it generate wrong load
472 ;; names for cases like lisp/term which is not added to load-path.
473 (setq dir (expand-file-name (pop names) dir)))
474 (t (setq name (mapconcat 'identity names "/")))))
475 (if (string-match "\\.elc?\\(\\.\\|\\'\\)" name)
476 (substring name 0 (match-beginning 0))
479 (defun generate-file-autoloads (file)
480 "Insert at point a loaddefs autoload section for FILE.
481 Autoloads are generated for defuns and defmacros in FILE
482 marked by `generate-autoload-cookie' (which see).
483 If FILE is being visited in a buffer, the contents of the buffer
485 Return non-nil in the case where no autoloads were added at point."
486 (interactive "fGenerate autoloads for file: ")
487 (let ((generated-autoload-file buffer-file-name))
488 (autoload-generate-file-autoloads file (current-buffer))))
490 (defvar print-readably)
493 (defun autoload--setup-output (otherbuf outbuf absfile load-name)
496 ;; A file-local setting of
497 ;; autoload-generated-file says we
498 ;; should ignore 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
510 (defun autoload--print-cookie-text (output-start load-name file)
511 (let ((standard-output (marker-buffer output-start)))
512 (search-forward generate-autoload-cookie)
513 (skip-chars-forward " \t")
515 (condition-case-unless-debug err
516 ;; Read the next form and make an autoload.
517 (let* ((form (prog1 (read (current-buffer))
518 (or (bolp) (forward-line 1))))
519 (autoload (make-autoload form load-name)))
522 (setq autoload form))
523 (let ((autoload-print-form-outbuf
525 (autoload-print-form autoload)))
527 (message "Autoload cookie error in %s:%s %S"
528 file (count-lines (point-min) (point)) err)))
530 ;; Copy the rest of the line to the output.
531 (princ (buffer-substring
533 ;; Back up over whitespace, to preserve it.
534 (skip-chars-backward " \f\t")
535 (if (= (char-after (1+ (point))) ? )
539 (progn (forward-line 1) (point)))))))
541 (defvar autoload-builtin-package-versions nil)
543 ;; When called from `generate-file-autoloads' we should ignore
544 ;; `generated-autoload-file' altogether. When called from
545 ;; `update-file-autoloads' we don't know `outbuf'. And when called from
546 ;; `update-directory-autoloads' it's in between: we know the default
547 ;; `outbuf' but we should obey any file-local setting of
548 ;; `generated-autoload-file'.
549 (defun autoload-generate-file-autoloads (file &optional outbuf outfile)
550 "Insert an autoload section for FILE in the appropriate buffer.
551 Autoloads are generated for defuns and defmacros in FILE
552 marked by `generate-autoload-cookie' (which see).
553 If FILE is being visited in a buffer, the contents of the buffer are used.
554 OUTBUF is the buffer in which the autoload statements should be inserted.
555 If OUTBUF is nil, it will be determined by `autoload-generated-file'.
557 If provided, OUTFILE is expected to be the file name of OUTBUF.
558 If OUTFILE is non-nil and FILE specifies a `generated-autoload-file'
559 different from OUTFILE, then OUTBUF is ignored.
561 Return non-nil if and only if FILE adds no autoloads to OUTFILE
562 \(or OUTBUF if OUTFILE is nil). The actual return value is
563 FILE's modification time."
564 ;; Include the file name in any error messages
569 (print-readably t) ; This does something in Lucid Emacs.
570 (float-output-format nil)
571 (visited (get-file-buffer file))
573 (absfile (expand-file-name file))
574 ;; nil until we found a cookie.
578 (with-current-buffer (or visited
579 ;; It is faster to avoid visiting the file.
580 (autoload-find-file file))
581 ;; Obey the no-update-autoloads file local variable.
582 (unless no-update-autoloads
583 (or noninteractive (message "Generating autoloads for %s..." file))
585 (if (stringp generated-autoload-load-name)
586 generated-autoload-load-name
587 (autoload-file-load-name absfile)))
588 ;; FIXME? Comparing file-names for equality with just equal
589 ;; is fragile, eg if one has an automounter prefix and one
590 ;; does not, but both refer to the same physical file.
593 (if (memq system-type '(ms-dos windows-nt))
594 (equal (downcase outfile)
595 (downcase (autoload-generated-file)))
596 (equal outfile (autoload-generated-file)))))
601 (when autoload-builtin-package-versions
602 (let ((version (lm-header "version"))
605 (setq version (ignore-errors (version-to-list version)))
606 (setq package (or (lm-header "package")
607 (file-name-sans-extension
608 (file-name-nondirectory file))))
609 (setq output-start (autoload--setup-output
610 otherbuf outbuf absfile load-name))
611 (let ((standard-output (marker-buffer output-start))
613 (princ `(push (purecopy
614 ',(cons (intern package) version))
615 package--builtin-versions))
618 (goto-char (point-min))
620 (skip-chars-forward " \t\n\f")
622 ((looking-at (regexp-quote generate-autoload-cookie))
623 ;; If not done yet, figure out where to insert this text.
625 (setq output-start (autoload--setup-output
626 otherbuf outbuf absfile load-name)))
627 (autoload--print-cookie-text output-start load-name file))
629 ;; Don't read the comment.
633 (forward-line 1))))))
636 (let ((secondary-autoloads-file-buf
637 (if otherbuf (current-buffer))))
638 (with-current-buffer (marker-buffer output-start)
640 ;; Insert the section-header line which lists the file name
641 ;; and which functions are in it, etc.
642 (goto-char output-start)
643 (let ((relfile (file-relative-name absfile)))
644 (autoload-insert-section-header
645 (marker-buffer output-start)
647 (if secondary-autoloads-file-buf
648 ;; MD5 checksums are much better because they do not
649 ;; change unless the file changes (so they'll be
650 ;; equal on two different systems and will change
651 ;; less often than time-stamps, thus leading to fewer
652 ;; unneeded changes causing spurious conflicts), but
653 ;; using time-stamps is a very useful optimization,
654 ;; so we use time-stamps for the main autoloads file
655 ;; (loaddefs.el) where we have special ways to
656 ;; circumvent the "random change problem", and MD5
657 ;; checksum in secondary autoload files where we do
658 ;; not need the time-stamp optimization because it is
659 ;; already provided by the primary autoloads file.
660 (md5 secondary-autoloads-file-buf
661 ;; We'd really want to just use
662 ;; `emacs-internal' instead.
663 nil nil 'emacs-mule-unix)
664 (if autoload-timestamps
665 (nth 5 (file-attributes relfile))
666 autoload--non-timestamp)))
667 (insert ";;; Generated autoloads from " relfile "\n")))
668 (insert generate-autoload-section-trailer))))
670 (message "Generating autoloads for %s...done" file)))
672 ;; We created this buffer, so we should kill it.
673 (kill-buffer (current-buffer))))
674 (or (not output-start)
675 ;; If the entries were added to some other buffer, then the file
676 ;; doesn't add entries to OUTFILE.
678 (nth 5 (file-attributes absfile))))
680 ;; Probably unbalanced parens in forward-sexp. In that case, the
681 ;; condition is scan-error, and the signal data includes point
682 ;; where the error was found; we'd like to convert that to
683 ;; line:col, but line-number-at-pos gets the wrong line in batch
684 ;; mode for some reason.
686 ;; At least this gets the file name in the error message; the
687 ;; developer can use goto-char to get to the error position.
688 (error "%s:0:0: error: %s: %s" file (car err) (cdr err)))
691 (defun autoload-save-buffers ()
692 (while autoload-modified-buffers
693 (with-current-buffer (pop autoload-modified-buffers)
694 (let ((version-control 'never))
697 ;; FIXME This command should be deprecated.
698 ;; See http://debbugs.gnu.org/22213#41
700 (defun update-file-autoloads (file &optional save-after outfile)
701 "Update the autoloads for FILE.
702 If prefix arg SAVE-AFTER is non-nil, save the buffer too.
704 If FILE binds `generated-autoload-file' as a file-local variable,
705 autoloads are written into that file. Otherwise, the autoloads
706 file is determined by OUTFILE. If called interactively, prompt
707 for OUTFILE; if called from Lisp with OUTFILE nil, use the
708 existing value of `generated-autoload-file'.
710 Return FILE if there was no autoload cookie in it, else nil."
711 (interactive (list (read-file-name "Update autoloads for file: ")
713 (read-file-name "Write autoload definitions to file: ")))
714 (let* ((generated-autoload-file (or outfile generated-autoload-file))
715 (autoload-modified-buffers nil)
716 ;; We need this only if the output file handles more than one input.
717 ;; See http://debbugs.gnu.org/22213#38 and subsequent.
718 (autoload-timestamps t)
719 (no-autoloads (autoload-generate-file-autoloads file)))
720 (if autoload-modified-buffers
721 (if save-after (autoload-save-buffers))
722 (if (called-interactively-p 'interactive)
723 (message "Autoload section for %s is up to date." file)))
724 (if no-autoloads file)))
726 (defun autoload-find-destination (file load-name)
727 "Find the destination point of the current buffer's autoloads.
728 FILE is the file name of the current buffer.
729 LOAD-NAME is the name as it appears in the output.
730 Returns a buffer whose point is placed at the requested location.
731 Returns nil if the file's autoloads are up-to-date, otherwise
732 removes any prior now out-of-date autoload entries."
734 (let* ((buf (current-buffer))
735 (existing-buffer (if buffer-file-name buf))
736 (output-file (autoload-generated-file))
737 (output-time (if (file-exists-p output-file)
738 (nth 5 (file-attributes output-file))))
740 (with-current-buffer (autoload-find-generated-file)
741 ;; This is to make generated-autoload-file have Unix EOLs, so
742 ;; that it is portable to all platforms.
743 (or (eq 0 (coding-system-eol-type buffer-file-coding-system))
744 (set-buffer-file-coding-system 'unix))
745 (or (> (buffer-size) 0)
746 (error "Autoloads file %s lacks boilerplate" buffer-file-name))
747 (or (file-writable-p buffer-file-name)
748 (error "Autoloads file %s is not writable" buffer-file-name))
750 (goto-char (point-min))
751 ;; Look for the section for LOAD-NAME.
752 (while (and (not found)
753 (search-forward generate-autoload-section-header nil t))
754 (let ((form (autoload-read-section-header)))
755 (cond ((string= (nth 2 form) load-name)
756 ;; We found the section for this file.
757 ;; Check if it is up to date.
758 (let ((begin (match-beginning 0))
759 (last-time (nth 4 form))
760 (file-time (nth 5 (file-attributes file))))
761 (if (and (or (null existing-buffer)
762 (not (buffer-modified-p existing-buffer)))
764 ;; FIXME? Arguably we should throw a
765 ;; user error, or some kind of warning,
766 ;; if we were called from update-file-autoloads,
767 ;; which can update only a single input file.
768 ;; It's not appropriate to use the output
769 ;; file modtime in such a case,
770 ;; if there are multiple input files
771 ;; contributing to the output.
774 (list t autoload--non-timestamp)))
775 (not (time-less-p output-time file-time)))
776 ;; last-time is the time-stamp (specifying
777 ;; the last time we looked at the file) and
778 ;; the file hasn't been changed since.
780 (not (time-less-p last-time file-time)))
781 ;; last-time is an MD5 checksum instead.
784 (md5 buf nil nil 'emacs-mule)))))
785 (throw 'up-to-date nil)
786 (autoload-remove-section begin)
788 ((string< load-name (nth 2 form))
789 ;; We've come to a section alphabetically later than
790 ;; LOAD-NAME. We assume the file is in order and so
791 ;; there must be no section for LOAD-NAME. We will
792 ;; insert one before the section here.
793 (goto-char (match-beginning 0))
797 ;; No later sections in the file. Put before the last page.
798 (goto-char (point-max))
799 (search-backward "\f" nil t)))
800 (unless (memq (current-buffer) autoload-modified-buffers)
801 (push (current-buffer) autoload-modified-buffers))
804 (defun autoload-remove-section (begin)
806 (search-forward generate-autoload-section-trailer)
807 (delete-region begin (point)))
810 (defun update-directory-autoloads (&rest dirs)
811 "Update autoload definitions for Lisp files in the directories DIRS.
812 In an interactive call, you must give one argument, the name of a
813 single directory. In a call from Lisp, you can supply multiple
814 directories as separate arguments, but this usage is discouraged.
816 The function does NOT recursively descend into subdirectories of the
817 directory or directories specified.
819 In an interactive call, prompt for a default output file for the
820 autoload definitions, and temporarily bind the variable
821 `generated-autoload-file' to this value. When called from Lisp,
822 use the existing value of `generated-autoload-file'. If any Lisp
823 file binds `generated-autoload-file' as a file-local variable,
824 write its autoloads into the specified file instead."
825 (interactive "DUpdate autoloads from directory: ")
826 (let* ((files-re (let ((tmp nil))
827 (dolist (suf (get-load-suffixes))
828 (unless (string-match "\\.elc" suf) (push suf tmp)))
829 (concat "^[^=.].*" (regexp-opt tmp t) "\\'")))
831 (mapcar (lambda (dir)
832 (directory-files (expand-file-name dir)
837 ;; Files with no autoload cookies or whose autoloads go to other
838 ;; files because of file-local autoload-generated-file settings.
840 (autoload-modified-buffers nil)
841 (generated-autoload-file
842 (if (called-interactively-p 'interactive)
843 (read-file-name "Write autoload definitions to file: ")
844 generated-autoload-file))
846 (if (file-exists-p generated-autoload-file)
847 (nth 5 (file-attributes generated-autoload-file)))))
849 (with-current-buffer (autoload-find-generated-file)
851 ;; Canonicalize file names and remove the autoload file itself.
852 (setq files (delete (file-relative-name buffer-file-name)
853 (mapcar 'file-relative-name files)))
855 (goto-char (point-min))
856 (while (search-forward generate-autoload-section-header nil t)
857 (let* ((form (autoload-read-section-header))
859 (cond ((and (consp file) (stringp (car file)))
860 ;; This is a list of files that have no autoload cookies.
861 ;; There shouldn't be more than one such entry.
862 ;; Remove the obsolete section.
863 (autoload-remove-section (match-beginning 0))
864 (setq last-time (nth 4 form))
865 (if (member last-time (list t autoload--non-timestamp))
866 (setq last-time output-time))
868 (let ((file-time (nth 5 (file-attributes file))))
870 (not (time-less-p last-time file-time)))
872 (push file no-autoloads)
873 (setq files (delete file files))))))
874 ((not (stringp file)))
875 ((or (not (file-exists-p file))
876 ;; Remove duplicates as well, just in case.
878 ;; If the file is actually excluded.
879 (member (expand-file-name file) autoload-excludes))
880 ;; Remove the obsolete section.
881 (autoload-remove-section (match-beginning 0)))
882 ((not (time-less-p (let ((oldtime (nth 4 form)))
885 t autoload--non-timestamp))
888 (nth 5 (file-attributes file))))
889 ;; File hasn't changed.
892 (autoload-remove-section (match-beginning 0))
893 (if (autoload-generate-file-autoloads
894 ;; Passing `current-buffer' makes it insert at point.
895 file (current-buffer) buffer-file-name)
896 (push file no-autoloads))))
898 (setq files (delete file files)))))
899 ;; Elements remaining in FILES have no existing autoload sections yet.
900 (let ((no-autoloads-time (or last-time '(0 0 0 0))) file-time)
903 ((member (expand-file-name file) autoload-excludes) nil)
904 ;; Passing nil as second argument forces
905 ;; autoload-generate-file-autoloads to look for the right
906 ;; spot where to insert each autoloads section.
908 (autoload-generate-file-autoloads file nil buffer-file-name))
909 (push file no-autoloads)
910 (if (time-less-p no-autoloads-time file-time)
911 (setq no-autoloads-time file-time)))))
914 ;; Sort them for better readability.
915 (setq no-autoloads (sort no-autoloads 'string<))
916 ;; Add the `no-autoloads' section.
917 (goto-char (point-max))
918 (search-backward "\f" nil t)
919 (autoload-insert-section-header
920 (current-buffer) nil nil no-autoloads (if autoload-timestamps
922 autoload--non-timestamp))
923 (insert generate-autoload-section-trailer)))
925 (let ((version-control 'never))
927 ;; In case autoload entries were added to other files because of
928 ;; file-local autoload-generated-file settings.
929 (autoload-save-buffers))))
931 (define-obsolete-function-alias 'update-autoloads-from-directories
932 'update-directory-autoloads "22.1")
935 (defun batch-update-autoloads ()
936 "Update loaddefs.el autoloads in batch mode.
937 Calls `update-directory-autoloads' on the command line arguments.
938 Definitions are written to `generated-autoload-file' (which
940 ;; For use during the Emacs build process only.
941 ;; Exclude those files that are preloaded on ALL platforms.
942 ;; These are the ones in loadup.el where "(load" is at the start
943 ;; of the line (crude, but it works).
944 (unless autoload-excludes
945 (let ((default-directory (file-name-directory generated-autoload-file))
947 (when (file-readable-p "loadup.el")
949 (insert-file-contents "loadup.el")
950 (while (re-search-forward "^(load \"\\([^\"]+\\)\"" nil t)
951 (setq file (match-string 1))
952 (or (string-match "\\.el\\'" file)
953 (setq file (format "%s.el" file)))
954 (or (string-match "\\`site-" file)
955 (push (expand-file-name file) autoload-excludes)))))))
956 (let ((args command-line-args-left))
957 (setq command-line-args-left nil)
958 (apply 'update-directory-autoloads args)))
962 ;;; autoload.el ends here