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 (defvar autoload-modified-buffers) ;Dynamically scoped var.
92 (defun make-autoload (form file &optional expansion)
93 "Turn FORM into an autoload or defvar for source file FILE.
94 Returns nil if FORM is not a special autoload form (i.e. a function definition
95 or macro definition or a defcustom).
96 If EXPANSION is non-nil, we're processing the macro expansion of an
97 expression, in which case we want to handle forms differently."
98 (let ((car (car-safe form)) expand)
100 ((and expansion (eq car 'defalias))
102 ((`(,_ ,_ ,arg . ,rest) form)
103 ;; `type' is non-nil if it defines a macro.
104 ;; `fun' is the function part of `arg' (defaults to `arg').
105 ((or (and (or `(cons 'macro ,fun) `'(macro . ,fun)) (let type t))
106 (and (let fun arg) (let type nil)))
108 ;; `lam' is the lambda expression in `fun' (or nil if not
110 (lam (if (memq (car-safe fun) '(quote function)) (cadr fun)))
111 ;; `args' is the list of arguments (or t if not recognized).
112 ;; `body' is the body of `lam' (or t if not recognized).
113 ((or `(lambda ,args . ,body)
114 (and (let args t) (let body t)))
116 ;; Get the `doc' from `body' or `rest'.
117 (doc (cond ((stringp (car-safe body)) (car body))
118 ((stringp (car-safe rest)) (car rest))))
119 ;; Look for an interactive spec.
120 (interactive (pcase body
121 ((or `((interactive . ,_) . ,_)
122 `(,_ (interactive . ,_) . ,_))
124 ;; Add the usage form at the end where describe-function-1
126 (when (listp args) (setq doc (help-add-fundoc-usage doc args)))
127 ;; (message "autoload of %S" (nth 1 form))
128 `(autoload ,(nth 1 form) ,file ,doc ,interactive ,type)))
130 ((and expansion (memq car '(progn prog1)))
131 (let ((end (memq :autoload-end form)))
132 (when end ;Cut-off anything after the :autoload-end marker.
133 (setq form (copy-sequence form))
134 (setcdr (memq :autoload-end form) nil))
135 (let ((exps (delq nil (mapcar (lambda (form)
136 (make-autoload form file expansion))
138 (when exps (cons 'progn exps)))))
140 ;; For complex cases, try again on the macro-expansion.
141 ((and (memq car '(easy-mmode-define-global-mode define-global-minor-mode
142 define-globalized-minor-mode defun defmacro
143 easy-mmode-define-minor-mode define-minor-mode
144 define-inline cl-defun cl-defmacro))
146 (setq expand (let ((load-file-name file)) (macroexpand form)))
147 (memq (car expand) '(progn prog1 defalias)))
148 (make-autoload expand file 'expansion)) ;Recurse on the expansion.
150 ;; For special function-like operators, use the `autoload' function.
151 ((memq car '(define-skeleton define-derived-mode
152 define-compilation-mode define-generic-mode
153 easy-mmode-define-global-mode define-global-minor-mode
154 define-globalized-minor-mode
155 easy-mmode-define-minor-mode define-minor-mode
156 cl-defun defun* cl-defmacro defmacro*
157 define-overloadable-function))
158 (let* ((macrop (memq car '(defmacro cl-defmacro defmacro*)))
161 ((or `defun `defmacro
162 `defun* `defmacro* `cl-defun `cl-defmacro
163 `define-overloadable-function) (nth 2 form))
164 (`define-skeleton '(&optional str arg))
165 ((or `define-generic-mode `define-derived-mode
166 `define-compilation-mode) nil)
168 (body (nthcdr (or (function-get car 'doc-string-elt) 3) form))
169 (doc (if (stringp (car body)) (pop body))))
170 ;; Add the usage form at the end where describe-function-1
172 (when (listp args) (setq doc (help-add-fundoc-usage doc args)))
173 ;; `define-generic-mode' quotes the name, so take care of that
174 `(autoload ,(if (listp name) name (list 'quote name))
176 ,(or (and (memq car '(define-skeleton define-derived-mode
178 easy-mmode-define-global-mode
179 define-global-minor-mode
180 define-globalized-minor-mode
181 easy-mmode-define-minor-mode
182 define-minor-mode)) t)
183 (eq (car-safe (car body)) 'interactive))
184 ,(if macrop ''macro nil))))
186 ;; For defclass forms, use `eieio-defclass-autoload'.
188 (let ((name (nth 1 form))
189 (superclasses (nth 2 form))
191 (list 'eieio-defclass-autoload (list 'quote name)
192 (list 'quote superclasses) file doc)))
194 ;; Convert defcustom to less space-consuming data.
196 (let ((varname (car-safe (cdr-safe form)))
197 (init (car-safe (cdr-safe (cdr-safe form))))
198 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form)))))
199 ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
202 (defvar ,varname ,init ,doc)
203 (custom-autoload ',varname ,file
205 (null (cadr (memq :set form)))
209 ;; In Emacs this is normally handled separately by cus-dep.el, but for
210 ;; third party packages, it can be convenient to explicitly autoload
212 (let ((groupname (nth 1 form)))
213 `(let ((loads (get ',groupname 'custom-loads)))
214 (if (member ',file loads) nil
215 (put ',groupname 'custom-loads (cons ',file loads))))))
217 ;; When processing a macro expansion, any expression
218 ;; before a :autoload-end should be included. These are typically (put
219 ;; 'fun 'prop val) and things like that.
220 ((and expansion (consp form)) form)
222 ;; nil here indicates that this is not a special autoload form.
225 ;; Forms which have doc-strings which should be printed specially.
226 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
227 ;; the doc-string in FORM.
228 ;; Those properties are now set in lisp-mode.el.
230 (defun autoload-find-generated-file ()
231 "Visit the autoload file for the current buffer, and return its buffer.
232 If a buffer is visiting the desired autoload file, return it."
233 (let ((enable-local-variables :safe)
234 (enable-local-eval nil))
235 ;; We used to use `raw-text' to read this file, but this causes
236 ;; problems when the file contains non-ASCII characters.
237 (let* ((delay-mode-hooks t)
238 (file (autoload-generated-file))
239 (file-missing (not (file-exists-p file))))
241 (autoload-ensure-default-file file))
244 (autoload-ensure-file-writeable
246 ;; block backups when the file has just been created, since
247 ;; the backups will just be the auto-generated headers.
250 (setq buffer-backed-up t)
254 (defun autoload-generated-file ()
255 (expand-file-name generated-autoload-file
256 ;; File-local settings of generated-autoload-file should
257 ;; be interpreted relative to the file's location,
259 (if (not (local-variable-p 'generated-autoload-file))
260 (expand-file-name "lisp" source-directory))))
263 (defun autoload-read-section-header ()
264 "Read a section header form.
265 Since continuation lines have been marked as comments,
266 we must copy the text of the form and remove those comment
267 markers before we call `read'."
269 (let ((beginning (point))
272 (while (looking-at generate-autoload-section-continuation)
274 (setq string (buffer-substring beginning (point)))
275 (with-current-buffer (get-buffer-create " *autoload*")
278 (goto-char (point-min))
279 (while (search-forward generate-autoload-section-continuation nil t)
281 (goto-char (point-min))
282 (read (current-buffer))))))
284 (defvar autoload-print-form-outbuf nil
285 "Buffer which gets the output of `autoload-print-form'.")
287 (defun autoload-print-form (form)
288 "Print FORM such that `make-docfile' will find the docstrings.
289 The variable `autoload-print-form-outbuf' specifies the buffer to
292 ;; If the form is a sequence, recurse.
293 ((eq (car form) 'progn) (mapcar 'autoload-print-form (cdr form)))
294 ;; Symbols at the toplevel are meaningless.
297 (let ((doc-string-elt (function-get (car-safe form) 'doc-string-elt))
298 (outbuf autoload-print-form-outbuf))
299 (if (and doc-string-elt (stringp (nth doc-string-elt form)))
300 ;; We need to hack the printing because the
301 ;; doc-string must be printed specially for
302 ;; make-docfile (sigh).
303 (let* ((p (nthcdr (1- doc-string-elt) form))
307 (let ((print-escape-newlines t)
309 (print-escape-nonascii t))
313 (princ "\"\\\n" outbuf)
314 (let ((begin (with-current-buffer outbuf (point))))
315 (princ (substring (prin1-to-string (car elt)) 1)
317 ;; Insert a backslash before each ( that
318 ;; appears at the beginning of a line in
320 (with-current-buffer outbuf
322 (while (re-search-backward "\n[[(]" begin t)
328 (princ (substring (prin1-to-string (cdr elt)) 1)
331 (let ((print-escape-newlines t)
333 (print-escape-nonascii t))
334 (print form outbuf)))))))
336 (defun autoload-rubric (file &optional type feature)
337 "Return a string giving the appropriate autoload rubric for FILE.
338 TYPE (default \"autoloads\") is a string stating the type of
339 information contained in FILE. If FEATURE is non-nil, FILE
340 will provide a feature. FEATURE may be a string naming the
341 feature, otherwise it will be based on FILE's name.
343 At present, a feature is in fact always provided, but this should
345 (let ((basename (file-name-nondirectory file)))
346 (concat ";;; " basename
347 " --- automatically extracted " (or type "autoloads") "\n"
351 ;; This is used outside of autoload.el, eg cus-dep, finder.
353 (if (stringp feature)
355 (file-name-sans-extension basename))
357 ";; Local Variables:\n"
358 ";; version-control: never\n"
359 ";; no-byte-compile: t\n"
360 ";; no-update-autoloads: t\n"
366 (defvar autoload-ensure-writable nil
367 "Non-nil means `autoload-ensure-default-file' makes existing file writable.")
368 ;; Just in case someone tries to get you to overwrite a file that you
371 (put 'autoload-ensure-writable 'risky-local-variable t)
373 (defun autoload-ensure-file-writeable (file)
374 ;; Probably pointless, but replaces the old AUTOGEN_VCS in lisp/Makefile,
375 ;; which was designed to handle CVSREAD=1 and equivalent.
376 (and autoload-ensure-writable
377 (let ((modes (file-modes file)))
378 (if (zerop (logand modes #o0200))
379 ;; Ignore any errors here, and let subsequent attempts
380 ;; to write the file raise any real error.
381 (ignore-errors (set-file-modes file (logior modes #o0200))))))
384 (defun autoload-ensure-default-file (file)
385 "Make sure that the autoload file FILE exists, creating it if needed.
386 If the file already exists and `autoload-ensure-writable' is non-nil,
388 (write-region (autoload-rubric file) nil file))
390 (defun autoload-insert-section-header (outbuf autoloads load-name file time)
391 "Insert the section-header line,
392 which lists the file name and which functions are in it, etc."
393 (insert generate-autoload-section-header)
394 (prin1 `(autoloads ,autoloads ,load-name ,file ,time)
397 ;; Break that line at spaces, to avoid very long lines.
398 ;; Make each sub-line into a comment.
399 (with-current-buffer outbuf
404 (skip-chars-forward "^ \n")
406 (insert "\n" generate-autoload-section-continuation))))))
408 (defun autoload-find-file (file)
409 "Fetch file and put it in a temp buffer. Return the buffer."
410 ;; It is faster to avoid visiting the file.
411 (setq file (expand-file-name file))
412 (with-current-buffer (get-buffer-create " *autoload-file*")
413 (kill-all-local-variables)
415 (setq buffer-undo-list t
416 buffer-read-only nil)
417 (delay-mode-hooks (emacs-lisp-mode))
418 (setq default-directory (file-name-directory file))
419 (insert-file-contents file nil)
420 (let ((enable-local-variables :safe)
421 (enable-local-eval nil))
422 (hack-local-variables))
425 (defvar no-update-autoloads nil
426 "File local variable to prevent scanning this file for autoload cookies.")
428 (defun autoload-file-load-name (file)
429 "Compute the name that will be used to load FILE."
430 ;; OUTFILE should be the name of the global loaddefs.el file, which
431 ;; is expected to be at the root directory of the files we're
432 ;; scanning for autoloads and will be in the `load-path'.
433 (let* ((outfile (default-value 'generated-autoload-file))
434 (name (file-relative-name file (file-name-directory outfile)))
436 (dir (file-name-directory outfile)))
437 ;; If `name' has directory components, only keep the
438 ;; last few that are really needed.
440 (setq name (directory-file-name name))
441 (push (file-name-nondirectory name) names)
442 (setq name (file-name-directory name)))
445 ((null (cdr names)) (setq name (car names)))
446 ((file-exists-p (expand-file-name "subdirs.el" dir))
447 ;; FIXME: here we only check the existence of subdirs.el,
448 ;; without checking its content. This makes it generate wrong load
449 ;; names for cases like lisp/term which is not added to load-path.
450 (setq dir (expand-file-name (pop names) dir)))
451 (t (setq name (mapconcat 'identity names "/")))))
452 (if (string-match "\\.elc?\\(\\.\\|\\'\\)" name)
453 (substring name 0 (match-beginning 0))
456 (defun generate-file-autoloads (file)
457 "Insert at point a loaddefs autoload section for FILE.
458 Autoloads are generated for defuns and defmacros in FILE
459 marked by `generate-autoload-cookie' (which see).
460 If FILE is being visited in a buffer, the contents of the buffer
462 Return non-nil in the case where no autoloads were added at point."
463 (interactive "fGenerate autoloads for file: ")
464 (let ((generated-autoload-file buffer-file-name))
465 (autoload-generate-file-autoloads file (current-buffer))))
467 (defvar print-readably)
470 (defun autoload--setup-output (otherbuf outbuf absfile load-name)
473 ;; A file-local setting of
474 ;; autoload-generated-file says we
475 ;; should ignore OUTBUF.
478 (autoload-find-destination absfile load-name)
479 ;; The file has autoload cookies, but they're
480 ;; already up-to-date. If OUTFILE is nil, the
481 ;; entries are in the expected OUTBUF,
482 ;; otherwise they're elsewhere.
483 (throw 'done otherbuf))))
484 (with-current-buffer outbuf
487 (defun autoload--print-cookie-text (output-start load-name file)
488 (let ((standard-output (marker-buffer output-start)))
489 (search-forward generate-autoload-cookie)
490 (skip-chars-forward " \t")
492 (condition-case-unless-debug err
493 ;; Read the next form and make an autoload.
494 (let* ((form (prog1 (read (current-buffer))
495 (or (bolp) (forward-line 1))))
496 (autoload (make-autoload form load-name)))
499 (setq autoload form))
500 (let ((autoload-print-form-outbuf
502 (autoload-print-form autoload)))
504 (message "Autoload cookie error in %s:%s %S"
505 file (count-lines (point-min) (point)) err)))
507 ;; Copy the rest of the line to the output.
508 (princ (buffer-substring
510 ;; Back up over whitespace, to preserve it.
511 (skip-chars-backward " \f\t")
512 (if (= (char-after (1+ (point))) ? )
516 (progn (forward-line 1) (point)))))))
518 (defvar autoload-builtin-package-versions nil)
520 ;; When called from `generate-file-autoloads' we should ignore
521 ;; `generated-autoload-file' altogether. When called from
522 ;; `update-file-autoloads' we don't know `outbuf'. And when called from
523 ;; `update-directory-autoloads' it's in between: we know the default
524 ;; `outbuf' but we should obey any file-local setting of
525 ;; `generated-autoload-file'.
526 (defun autoload-generate-file-autoloads (file &optional outbuf outfile)
527 "Insert an autoload section for FILE in the appropriate buffer.
528 Autoloads are generated for defuns and defmacros in FILE
529 marked by `generate-autoload-cookie' (which see).
530 If FILE is being visited in a buffer, the contents of the buffer are used.
531 OUTBUF is the buffer in which the autoload statements should be inserted.
532 If OUTBUF is nil, it will be determined by `autoload-generated-file'.
534 If provided, OUTFILE is expected to be the file name of OUTBUF.
535 If OUTFILE is non-nil and FILE specifies a `generated-autoload-file'
536 different from OUTFILE, then OUTBUF is ignored.
538 Return non-nil if and only if FILE adds no autoloads to OUTFILE
539 \(or OUTBUF if OUTFILE is nil). The actual return value is
540 FILE's modification time."
541 ;; Include the file name in any error messages
546 (print-readably t) ; This does something in Lucid Emacs.
547 (float-output-format nil)
548 (visited (get-file-buffer file))
550 (absfile (expand-file-name file))
551 ;; nil until we found a cookie.
555 (with-current-buffer (or visited
556 ;; It is faster to avoid visiting the file.
557 (autoload-find-file file))
558 ;; Obey the no-update-autoloads file local variable.
559 (unless no-update-autoloads
560 (or noninteractive (message "Generating autoloads for %s..." file))
562 (if (stringp generated-autoload-load-name)
563 generated-autoload-load-name
564 (autoload-file-load-name absfile)))
565 ;; FIXME? Comparing file-names for equality with just equal
566 ;; is fragile, eg if one has an automounter prefix and one
567 ;; does not, but both refer to the same physical file.
570 (if (memq system-type '(ms-dos windows-nt))
571 (equal (downcase outfile)
572 (downcase (autoload-generated-file)))
573 (equal outfile (autoload-generated-file)))))
578 (when autoload-builtin-package-versions
579 (let ((version (lm-header "version"))
582 (setq version (ignore-errors (version-to-list version)))
583 (setq package (or (lm-header "package")
584 (file-name-sans-extension
585 (file-name-nondirectory file))))
586 (setq output-start (autoload--setup-output
587 otherbuf outbuf absfile load-name))
588 (let ((standard-output (marker-buffer output-start))
590 (princ `(push (purecopy
591 ',(cons (intern package) version))
592 package--builtin-versions))
595 (goto-char (point-min))
597 (skip-chars-forward " \t\n\f")
599 ((looking-at (regexp-quote generate-autoload-cookie))
600 ;; If not done yet, figure out where to insert this text.
602 (setq output-start (autoload--setup-output
603 otherbuf outbuf absfile load-name)))
604 (autoload--print-cookie-text output-start load-name file))
606 ;; Don't read the comment.
610 (forward-line 1))))))
613 (let ((secondary-autoloads-file-buf
614 (if otherbuf (current-buffer))))
615 (with-current-buffer (marker-buffer output-start)
617 ;; Insert the section-header line which lists the file name
618 ;; and which functions are in it, etc.
619 (goto-char output-start)
620 (let ((relfile (file-relative-name absfile)))
621 (autoload-insert-section-header
622 (marker-buffer output-start)
624 (if secondary-autoloads-file-buf
625 ;; MD5 checksums are much better because they do not
626 ;; change unless the file changes (so they'll be
627 ;; equal on two different systems and will change
628 ;; less often than time-stamps, thus leading to fewer
629 ;; unneeded changes causing spurious conflicts), but
630 ;; using time-stamps is a very useful optimization,
631 ;; so we use time-stamps for the main autoloads file
632 ;; (loaddefs.el) where we have special ways to
633 ;; circumvent the "random change problem", and MD5
634 ;; checksum in secondary autoload files where we do
635 ;; not need the time-stamp optimization because it is
636 ;; already provided by the primary autoloads file.
637 (md5 secondary-autoloads-file-buf
638 ;; We'd really want to just use
639 ;; `emacs-internal' instead.
640 nil nil 'emacs-mule-unix)
641 (nth 5 (file-attributes relfile))))
642 (insert ";;; Generated autoloads from " relfile "\n")))
643 (insert generate-autoload-section-trailer))))
645 (message "Generating autoloads for %s...done" file)))
647 ;; We created this buffer, so we should kill it.
648 (kill-buffer (current-buffer))))
649 (or (not output-start)
650 ;; If the entries were added to some other buffer, then the file
651 ;; doesn't add entries to OUTFILE.
653 (nth 5 (file-attributes absfile))))
655 ;; Probably unbalanced parens in forward-sexp. In that case, the
656 ;; condition is scan-error, and the signal data includes point
657 ;; where the error was found; we'd like to convert that to
658 ;; line:col, but line-number-at-pos gets the wrong line in batch
659 ;; mode for some reason.
661 ;; At least this gets the file name in the error message; the
662 ;; developer can use goto-char to get to the error position.
663 (error "%s:0:0: error: %s: %s" file (car err) (cdr err)))
666 (defun autoload-save-buffers ()
667 (while autoload-modified-buffers
668 (with-current-buffer (pop autoload-modified-buffers)
669 (let ((version-control 'never))
673 (defun update-file-autoloads (file &optional save-after outfile)
674 "Update the autoloads for FILE.
675 If prefix arg SAVE-AFTER is non-nil, save the buffer too.
677 If FILE binds `generated-autoload-file' as a file-local variable,
678 autoloads are written into that file. Otherwise, the autoloads
679 file is determined by OUTFILE. If called interactively, prompt
680 for OUTFILE; if called from Lisp with OUTFILE nil, use the
681 existing value of `generated-autoload-file'.
683 Return FILE if there was no autoload cookie in it, else nil."
684 (interactive (list (read-file-name "Update autoloads for file: ")
686 (read-file-name "Write autoload definitions to file: ")))
687 (let* ((generated-autoload-file (or outfile generated-autoload-file))
688 (autoload-modified-buffers nil)
689 (no-autoloads (autoload-generate-file-autoloads file)))
690 (if autoload-modified-buffers
691 (if save-after (autoload-save-buffers))
692 (if (called-interactively-p 'interactive)
693 (message "Autoload section for %s is up to date." file)))
694 (if no-autoloads file)))
696 (defun autoload-find-destination (file load-name)
697 "Find the destination point of the current buffer's autoloads.
698 FILE is the file name of the current buffer.
699 LOAD-NAME is the name as it appears in the output.
700 Returns a buffer whose point is placed at the requested location.
701 Returns nil if the file's autoloads are up-to-date, otherwise
702 removes any prior now out-of-date autoload entries."
704 (let* ((buf (current-buffer))
705 (existing-buffer (if buffer-file-name buf))
707 (with-current-buffer (autoload-find-generated-file)
708 ;; This is to make generated-autoload-file have Unix EOLs, so
709 ;; that it is portable to all platforms.
710 (or (eq 0 (coding-system-eol-type buffer-file-coding-system))
711 (set-buffer-file-coding-system 'unix))
712 (or (> (buffer-size) 0)
713 (error "Autoloads file %s lacks boilerplate" buffer-file-name))
714 (or (file-writable-p buffer-file-name)
715 (error "Autoloads file %s is not writable" buffer-file-name))
717 (goto-char (point-min))
718 ;; Look for the section for LOAD-NAME.
719 (while (and (not found)
720 (search-forward generate-autoload-section-header nil t))
721 (let ((form (autoload-read-section-header)))
722 (cond ((string= (nth 2 form) load-name)
723 ;; We found the section for this file.
724 ;; Check if it is up to date.
725 (let ((begin (match-beginning 0))
726 (last-time (nth 4 form))
727 (file-time (nth 5 (file-attributes file))))
728 (if (and (or (null existing-buffer)
729 (not (buffer-modified-p existing-buffer)))
731 ;; last-time is the time-stamp (specifying
732 ;; the last time we looked at the file) and
733 ;; the file hasn't been changed since.
734 (and (listp last-time)
735 (not (time-less-p last-time file-time)))
736 ;; last-time is an MD5 checksum instead.
737 (and (stringp last-time)
739 (md5 buf nil nil 'emacs-mule)))))
740 (throw 'up-to-date nil)
741 (autoload-remove-section begin)
743 ((string< load-name (nth 2 form))
744 ;; We've come to a section alphabetically later than
745 ;; LOAD-NAME. We assume the file is in order and so
746 ;; there must be no section for LOAD-NAME. We will
747 ;; insert one before the section here.
748 (goto-char (match-beginning 0))
752 ;; No later sections in the file. Put before the last page.
753 (goto-char (point-max))
754 (search-backward "\f" nil t)))
755 (unless (memq (current-buffer) autoload-modified-buffers)
756 (push (current-buffer) autoload-modified-buffers))
759 (defun autoload-remove-section (begin)
761 (search-forward generate-autoload-section-trailer)
762 (delete-region begin (point)))
765 (defun update-directory-autoloads (&rest dirs)
766 "Update autoload definitions for Lisp files in the directories DIRS.
767 In an interactive call, you must give one argument, the name of a
768 single directory. In a call from Lisp, you can supply multiple
769 directories as separate arguments, but this usage is discouraged.
771 The function does NOT recursively descend into subdirectories of the
772 directory or directories specified.
774 In an interactive call, prompt for a default output file for the
775 autoload definitions, and temporarily bind the variable
776 `generated-autoload-file' to this value. When called from Lisp,
777 use the existing value of `generated-autoload-file'. If any Lisp
778 file binds `generated-autoload-file' as a file-local variable,
779 write its autoloads into the specified file instead."
780 (interactive "DUpdate autoloads from directory: ")
781 (let* ((files-re (let ((tmp nil))
782 (dolist (suf (get-load-suffixes))
783 (unless (string-match "\\.elc" suf) (push suf tmp)))
784 (concat "^[^=.].*" (regexp-opt tmp t) "\\'")))
786 (mapcar (lambda (dir)
787 (directory-files (expand-file-name dir)
792 ;; Files with no autoload cookies or whose autoloads go to other
793 ;; files because of file-local autoload-generated-file settings.
795 (autoload-modified-buffers nil)
796 (generated-autoload-file
797 (if (called-interactively-p 'interactive)
798 (read-file-name "Write autoload definitions to file: ")
799 generated-autoload-file)))
801 (with-current-buffer (autoload-find-generated-file)
803 ;; Canonicalize file names and remove the autoload file itself.
804 (setq files (delete (file-relative-name buffer-file-name)
805 (mapcar 'file-relative-name files)))
807 (goto-char (point-min))
808 (while (search-forward generate-autoload-section-header nil t)
809 (let* ((form (autoload-read-section-header))
811 (cond ((and (consp file) (stringp (car file)))
812 ;; This is a list of files that have no autoload cookies.
813 ;; There shouldn't be more than one such entry.
814 ;; Remove the obsolete section.
815 (autoload-remove-section (match-beginning 0))
816 (setq last-time (nth 4 form))
818 (let ((file-time (nth 5 (file-attributes file))))
820 (not (time-less-p last-time file-time)))
822 (push file no-autoloads)
823 (setq files (delete file files))))))
824 ((not (stringp file)))
825 ((or (not (file-exists-p file))
826 ;; Remove duplicates as well, just in case.
828 ;; If the file is actually excluded.
829 (member (expand-file-name file) autoload-excludes))
830 ;; Remove the obsolete section.
831 (autoload-remove-section (match-beginning 0)))
832 ((not (time-less-p (nth 4 form)
833 (nth 5 (file-attributes file))))
834 ;; File hasn't changed.
837 (autoload-remove-section (match-beginning 0))
838 (if (autoload-generate-file-autoloads
839 ;; Passing `current-buffer' makes it insert at point.
840 file (current-buffer) buffer-file-name)
841 (push file no-autoloads))))
843 (setq files (delete file files)))))
844 ;; Elements remaining in FILES have no existing autoload sections yet.
845 (let ((no-autoloads-time (or last-time '(0 0 0 0))) file-time)
848 ((member (expand-file-name file) autoload-excludes) nil)
849 ;; Passing nil as second argument forces
850 ;; autoload-generate-file-autoloads to look for the right
851 ;; spot where to insert each autoloads section.
853 (autoload-generate-file-autoloads file nil buffer-file-name))
854 (push file no-autoloads)
855 (if (time-less-p no-autoloads-time file-time)
856 (setq no-autoloads-time file-time)))))
859 ;; Sort them for better readability.
860 (setq no-autoloads (sort no-autoloads 'string<))
861 ;; Add the `no-autoloads' section.
862 (goto-char (point-max))
863 (search-backward "\f" nil t)
864 (autoload-insert-section-header
865 (current-buffer) nil nil no-autoloads no-autoloads-time)
866 (insert generate-autoload-section-trailer)))
868 (let ((version-control 'never))
870 ;; In case autoload entries were added to other files because of
871 ;; file-local autoload-generated-file settings.
872 (autoload-save-buffers))))
874 (define-obsolete-function-alias 'update-autoloads-from-directories
875 'update-directory-autoloads "22.1")
878 (defun batch-update-autoloads ()
879 "Update loaddefs.el autoloads in batch mode.
880 Calls `update-directory-autoloads' on the command line arguments.
881 Definitions are written to `generated-autoload-file' (which
883 ;; For use during the Emacs build process only.
884 ;; Exclude those files that are preloaded on ALL platforms.
885 ;; These are the ones in loadup.el where "(load" is at the start
886 ;; of the line (crude, but it works).
887 (unless autoload-excludes
888 (let ((default-directory (file-name-directory generated-autoload-file))
890 (when (file-readable-p "loadup.el")
892 (insert-file-contents "loadup.el")
893 (while (re-search-forward "^(load \"\\([^\"]+\\)\"" nil t)
894 (setq file (match-string 1))
895 (or (string-match "\\.el\\'" file)
896 (setq file (format "%s.el" file)))
897 (or (string-match "\\`site-" file)
898 (push (expand-file-name file) autoload-excludes)))))))
899 (let ((args command-line-args-left))
900 (setq command-line-args-left nil)
901 (apply 'update-directory-autoloads args)))
905 ;;; autoload.el ends here