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>
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.
34 (require 'help-fns
) ;for help-add-fundoc-usage.
35 (eval-when-compile (require 'cl-lib
))
37 (defvar generated-autoload-file nil
38 "File into which to write autoload definitions.
39 A Lisp file can set this in its local variables section to make
40 its autoloads go somewhere else.
42 If this is a relative file name, the directory is determined as
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.")
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).
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
)
97 ((and expansion
(eq car
'defalias
))
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
)))
105 ;; `lam' is the lambda expression in `fun' (or nil if not
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
)))
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
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
))
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
143 easy-mmode-define-minor-mode define-minor-mode
))
144 (setq expand
(let ((load-file-name file
)) (macroexpand form
)))
145 (memq (car expand
) '(progn prog1 defalias
)))
146 (make-autoload expand file
'expansion
)) ;Recurse on the expansion.
148 ;; For special function-like operators, use the `autoload' function.
149 ((memq car
'(define-skeleton define-derived-mode
150 define-compilation-mode define-generic-mode
151 easy-mmode-define-global-mode define-global-minor-mode
152 define-globalized-minor-mode
153 easy-mmode-define-minor-mode define-minor-mode
154 cl-defun
defun* cl-defmacro defmacro
*
155 define-overloadable-function
))
156 (let* ((macrop (memq car
'(defmacro defmacro
*)))
159 ((defun defmacro defun
* defmacro
*
160 define-overloadable-function
) (nth 2 form
))
161 ((define-skeleton) '(&optional str arg
))
162 ((define-generic-mode define-derived-mode
163 define-compilation-mode
) nil
)
165 (body (nthcdr (or (get car
'doc-string-elt
) 3) form
))
166 (doc (if (stringp (car body
)) (pop body
))))
167 ;; Add the usage form at the end where describe-function-1
169 (when (listp args
) (setq doc
(help-add-fundoc-usage doc args
)))
170 ;; `define-generic-mode' quotes the name, so take care of that
171 (list 'autoload
(if (listp name
) name
(list 'quote name
))
173 (or (and (memq car
'(define-skeleton define-derived-mode
175 easy-mmode-define-global-mode
176 define-global-minor-mode
177 define-globalized-minor-mode
178 easy-mmode-define-minor-mode
179 define-minor-mode
)) t
)
180 (eq (car-safe (car body
)) 'interactive
))
181 (if macrop
(list 'quote
'macro
) nil
))))
183 ;; For defclass forms, use `eieio-defclass-autoload'.
185 (let ((name (nth 1 form
))
186 (superclasses (nth 2 form
))
188 (list 'eieio-defclass-autoload
(list 'quote name
)
189 (list 'quote superclasses
) file doc
)))
191 ;; Convert defcustom to less space-consuming data.
193 (let ((varname (car-safe (cdr-safe form
)))
194 (init (car-safe (cdr-safe (cdr-safe form
))))
195 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form
)))))
196 ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
199 (defvar ,varname
,init
,doc
)
200 (custom-autoload ',varname
,file
202 (null (cadr (memq :set form
)))
206 ;; In Emacs this is normally handled separately by cus-dep.el, but for
207 ;; third party packages, it can be convenient to explicitly autoload
209 (let ((groupname (nth 1 form
)))
210 `(let ((loads (get ',groupname
'custom-loads
)))
211 (if (member ',file loads
) nil
212 (put ',groupname
'custom-loads
(cons ',file loads
))))))
214 ;; When processing a macro expansion, any expression
215 ;; before a :autoload-end should be included. These are typically (put
216 ;; 'fun 'prop val) and things like that.
217 ((and expansion
(consp form
)) form
)
219 ;; nil here indicates that this is not a special autoload form.
222 ;; Forms which have doc-strings which should be printed specially.
223 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
224 ;; the doc-string in FORM.
225 ;; Those properties are now set in lisp-mode.el.
227 (defun autoload-find-generated-file ()
228 "Visit the autoload file for the current buffer, and return its buffer.
229 If a buffer is visiting the desired autoload file, return it."
230 (let ((enable-local-variables :safe
))
231 ;; We used to use `raw-text' to read this file, but this causes
232 ;; problems when the file contains non-ASCII characters.
234 (autoload-ensure-default-file (autoload-generated-file)))))
236 (defun autoload-generated-file ()
237 (expand-file-name generated-autoload-file
238 ;; File-local settings of generated-autoload-file should
239 ;; be interpreted relative to the file's location,
241 (if (not (local-variable-p 'generated-autoload-file
))
242 (expand-file-name "lisp" source-directory
))))
245 (defun autoload-read-section-header ()
246 "Read a section header form.
247 Since continuation lines have been marked as comments,
248 we must copy the text of the form and remove those comment
249 markers before we call `read'."
251 (let ((beginning (point))
254 (while (looking-at generate-autoload-section-continuation
)
256 (setq string
(buffer-substring beginning
(point)))
257 (with-current-buffer (get-buffer-create " *autoload*")
260 (goto-char (point-min))
261 (while (search-forward generate-autoload-section-continuation nil t
)
263 (goto-char (point-min))
264 (read (current-buffer))))))
266 (defvar autoload-print-form-outbuf nil
267 "Buffer which gets the output of `autoload-print-form'.")
269 (defun autoload-print-form (form)
270 "Print FORM such that `make-docfile' will find the docstrings.
271 The variable `autoload-print-form-outbuf' specifies the buffer to
274 ;; If the form is a sequence, recurse.
275 ((eq (car form
) 'progn
) (mapcar 'autoload-print-form
(cdr form
)))
276 ;; Symbols at the toplevel are meaningless.
279 (let ((doc-string-elt (get (car-safe form
) 'doc-string-elt
))
280 (outbuf autoload-print-form-outbuf
))
281 (if (and doc-string-elt
(stringp (nth doc-string-elt form
)))
282 ;; We need to hack the printing because the
283 ;; doc-string must be printed specially for
284 ;; make-docfile (sigh).
285 (let* ((p (nthcdr (1- doc-string-elt
) form
))
289 (let ((print-escape-newlines t
)
291 (print-escape-nonascii t
))
295 (princ "\"\\\n" outbuf
)
296 (let ((begin (with-current-buffer outbuf
(point))))
297 (princ (substring (prin1-to-string (car elt
)) 1)
299 ;; Insert a backslash before each ( that
300 ;; appears at the beginning of a line in
302 (with-current-buffer outbuf
304 (while (re-search-backward "\n[[(]" begin t
)
310 (princ (substring (prin1-to-string (cdr elt
)) 1)
313 (let ((print-escape-newlines t
)
315 (print-escape-nonascii t
))
316 (print form outbuf
)))))))
318 (defun autoload-rubric (file &optional type feature
)
319 "Return a string giving the appropriate autoload rubric for FILE.
320 TYPE (default \"autoloads\") is a string stating the type of
321 information contained in FILE. If FEATURE is non-nil, FILE
322 will provide a feature. FEATURE may be a string naming the
323 feature, otherwise it will be based on FILE's name.
325 At present, a feature is in fact always provided, but this should
327 (let ((basename (file-name-nondirectory file
)))
328 (concat ";;; " basename
329 " --- automatically extracted " (or type
"autoloads") "\n"
333 ;; This is used outside of autoload.el, eg cus-dep, finder.
335 (if (stringp feature
)
337 (file-name-sans-extension basename
))
339 ";; Local Variables:\n"
340 ";; version-control: never\n"
341 ";; no-byte-compile: t\n"
342 ";; no-update-autoloads: t\n"
348 (defun autoload-ensure-default-file (file)
349 "Make sure that the autoload file FILE exists and if not create it."
350 (unless (file-exists-p file
)
351 (write-region (autoload-rubric file
) nil file
))
354 (defun autoload-insert-section-header (outbuf autoloads load-name file time
)
355 "Insert the section-header line,
356 which lists the file name and which functions are in it, etc."
357 (insert generate-autoload-section-header
)
358 (prin1 (list 'autoloads autoloads load-name file time
)
361 ;; Break that line at spaces, to avoid very long lines.
362 ;; Make each sub-line into a comment.
363 (with-current-buffer outbuf
368 (skip-chars-forward "^ \n")
370 (insert "\n" generate-autoload-section-continuation
))))))
372 (defun autoload-find-file (file)
373 "Fetch file and put it in a temp buffer. Return the buffer."
374 ;; It is faster to avoid visiting the file.
375 (setq file
(expand-file-name file
))
376 (with-current-buffer (get-buffer-create " *autoload-file*")
377 (kill-all-local-variables)
379 (setq buffer-undo-list t
380 buffer-read-only nil
)
382 (setq default-directory
(file-name-directory file
))
383 (insert-file-contents file nil
)
384 (let ((enable-local-variables :safe
))
385 (hack-local-variables))
388 (defvar no-update-autoloads nil
389 "File local variable to prevent scanning this file for autoload cookies.")
391 (defun autoload-file-load-name (file)
392 "Compute the name that will be used to load FILE."
393 ;; OUTFILE should be the name of the global loaddefs.el file, which
394 ;; is expected to be at the root directory of the files we're
395 ;; scanning for autoloads and will be in the `load-path'.
396 (let* ((outfile (default-value 'generated-autoload-file
))
397 (name (file-relative-name file
(file-name-directory outfile
)))
399 (dir (file-name-directory outfile
)))
400 ;; If `name' has directory components, only keep the
401 ;; last few that are really needed.
403 (setq name
(directory-file-name name
))
404 (push (file-name-nondirectory name
) names
)
405 (setq name
(file-name-directory name
)))
408 ((null (cdr names
)) (setq name
(car names
)))
409 ((file-exists-p (expand-file-name "subdirs.el" dir
))
410 ;; FIXME: here we only check the existence of subdirs.el,
411 ;; without checking its content. This makes it generate wrong load
412 ;; names for cases like lisp/term which is not added to load-path.
413 (setq dir
(expand-file-name (pop names
) dir
)))
414 (t (setq name
(mapconcat 'identity names
"/")))))
415 (if (string-match "\\.elc?\\(\\.\\|\\'\\)" name
)
416 (substring name
0 (match-beginning 0))
419 (defun generate-file-autoloads (file)
420 "Insert at point a loaddefs autoload section for FILE.
421 Autoloads are generated for defuns and defmacros in FILE
422 marked by `generate-autoload-cookie' (which see).
423 If FILE is being visited in a buffer, the contents of the buffer
425 Return non-nil in the case where no autoloads were added at point."
426 (interactive "fGenerate autoloads for file: ")
427 (let ((generated-autoload-file buffer-file-name
))
428 (autoload-generate-file-autoloads file
(current-buffer))))
430 (defvar print-readably
)
432 ;; When called from `generate-file-autoloads' we should ignore
433 ;; `generated-autoload-file' altogether. When called from
434 ;; `update-file-autoloads' we don't know `outbuf'. And when called from
435 ;; `update-directory-autoloads' it's in between: we know the default
436 ;; `outbuf' but we should obey any file-local setting of
437 ;; `generated-autoload-file'.
438 (defun autoload-generate-file-autoloads (file &optional outbuf outfile
)
439 "Insert an autoload section for FILE in the appropriate buffer.
440 Autoloads are generated for defuns and defmacros in FILE
441 marked by `generate-autoload-cookie' (which see).
442 If FILE is being visited in a buffer, the contents of the buffer are used.
443 OUTBUF is the buffer in which the autoload statements should be inserted.
444 If OUTBUF is nil, it will be determined by `autoload-generated-file'.
446 If provided, OUTFILE is expected to be the file name of OUTBUF.
447 If OUTFILE is non-nil and FILE specifies a `generated-autoload-file'
448 different from OUTFILE, then OUTBUF is ignored.
450 Return non-nil if and only if FILE adds no autoloads to OUTFILE
451 \(or OUTBUF if OUTFILE is nil)."
453 (let ((autoloads-done '())
457 (print-readably t
) ; This does something in Lucid Emacs.
458 (float-output-format nil
)
459 (visited (get-file-buffer file
))
461 (absfile (expand-file-name file
))
462 ;; nil until we found a cookie.
464 (with-current-buffer (or visited
465 ;; It is faster to avoid visiting the file.
466 (autoload-find-file file
))
467 ;; Obey the no-update-autoloads file local variable.
468 (unless no-update-autoloads
469 (message "Generating autoloads for %s..." file
)
471 (if (stringp generated-autoload-load-name
)
472 generated-autoload-load-name
473 (autoload-file-load-name absfile
)))
476 (if (memq system-type
'(ms-dos windows-nt
))
477 (equal (downcase outfile
)
478 (downcase (autoload-generated-file)))
479 (equal outfile
(autoload-generated-file)))))
484 (goto-char (point-min))
486 (skip-chars-forward " \t\n\f")
488 ((looking-at (regexp-quote generate-autoload-cookie
))
489 ;; If not done yet, figure out where to insert this text.
493 ;; A file-local setting of
494 ;; autoload-generated-file says we
495 ;; should ignore OUTBUF.
498 (autoload-find-destination absfile load-name
)
499 ;; The file has autoload cookies, but they're
500 ;; already up-to-date. If OUTFILE is nil, the
501 ;; entries are in the expected OUTBUF,
502 ;; otherwise they're elsewhere.
503 (throw 'done otherbuf
))))
504 (with-current-buffer outbuf
505 (setq output-start
(point-marker)
507 (search-forward generate-autoload-cookie
)
508 (skip-chars-forward " \t")
510 (condition-case-unless-debug err
511 ;; Read the next form and make an autoload.
512 (let* ((form (prog1 (read (current-buffer))
513 (or (bolp) (forward-line 1))))
514 (autoload (make-autoload form load-name
)))
516 (push (nth 1 form
) autoloads-done
)
517 (setq autoload form
))
518 (let ((autoload-print-form-outbuf
519 (marker-buffer output-start
)))
520 (autoload-print-form autoload
)))
522 (message "Autoload cookie error in %s:%s %S"
523 file
(count-lines (point-min) (point)) err
)))
525 ;; Copy the rest of the line to the output.
526 (princ (buffer-substring
528 ;; Back up over whitespace, to preserve it.
529 (skip-chars-backward " \f\t")
530 (if (= (char-after (1+ (point))) ?
)
534 (progn (forward-line 1) (point)))
535 (marker-buffer output-start
))))
537 ;; Don't read the comment.
541 (forward-line 1))))))
544 (let ((secondary-autoloads-file-buf
545 (if otherbuf
(current-buffer))))
546 (with-current-buffer (marker-buffer output-start
)
548 ;; Insert the section-header line which lists the file name
549 ;; and which functions are in it, etc.
550 (cl-assert (= ostart output-start
))
551 (goto-char output-start
)
552 (let ((relfile (file-relative-name absfile
)))
553 (autoload-insert-section-header
554 (marker-buffer output-start
)
555 autoloads-done load-name relfile
556 (if secondary-autoloads-file-buf
557 ;; MD5 checksums are much better because they do not
558 ;; change unless the file changes (so they'll be
559 ;; equal on two different systems and will change
560 ;; less often than time-stamps, thus leading to fewer
561 ;; unneeded changes causing spurious conflicts), but
562 ;; using time-stamps is a very useful optimization,
563 ;; so we use time-stamps for the main autoloads file
564 ;; (loaddefs.el) where we have special ways to
565 ;; circumvent the "random change problem", and MD5
566 ;; checksum in secondary autoload files where we do
567 ;; not need the time-stamp optimization because it is
568 ;; already provided by the primary autoloads file.
569 (md5 secondary-autoloads-file-buf
570 ;; We'd really want to just use
571 ;; `emacs-internal' instead.
572 nil nil
'emacs-mule-unix
)
573 (nth 5 (file-attributes relfile
))))
574 (insert ";;; Generated autoloads from " relfile
"\n")))
575 (insert generate-autoload-section-trailer
))))
576 (message "Generating autoloads for %s...done" file
))
578 ;; We created this buffer, so we should kill it.
579 (kill-buffer (current-buffer))))
580 (or (not output-start
)
581 ;; If the entries were added to some other buffer, then the file
582 ;; doesn't add entries to OUTFILE.
585 (defun autoload-save-buffers ()
586 (while autoload-modified-buffers
587 (with-current-buffer (pop autoload-modified-buffers
)
588 (let ((version-control 'never
))
592 (defun update-file-autoloads (file &optional save-after outfile
)
593 "Update the autoloads for FILE.
594 If prefix arg SAVE-AFTER is non-nil, save the buffer too.
596 If FILE binds `generated-autoload-file' as a file-local variable,
597 autoloads are written into that file. Otherwise, the autoloads
598 file is determined by OUTFILE. If called interactively, prompt
599 for OUTFILE; if called from Lisp with OUTFILE nil, use the
600 existing value of `generated-autoload-file'.
602 Return FILE if there was no autoload cookie in it, else nil."
603 (interactive (list (read-file-name "Update autoloads for file: ")
605 (read-file-name "Write autoload definitions to file: ")))
606 (let* ((generated-autoload-file (or outfile generated-autoload-file
))
607 (autoload-modified-buffers nil
)
608 (no-autoloads (autoload-generate-file-autoloads file
)))
609 (if autoload-modified-buffers
610 (if save-after
(autoload-save-buffers))
611 (if (called-interactively-p 'interactive
)
612 (message "Autoload section for %s is up to date." file
)))
613 (if no-autoloads file
)))
615 (defun autoload-find-destination (file load-name
)
616 "Find the destination point of the current buffer's autoloads.
617 FILE is the file name of the current buffer.
618 Returns a buffer whose point is placed at the requested location.
619 Returns nil if the file's autoloads are uptodate, otherwise
620 removes any prior now out-of-date autoload entries."
622 (let* ((buf (current-buffer))
623 (existing-buffer (if buffer-file-name buf
))
625 (with-current-buffer (autoload-find-generated-file)
626 ;; This is to make generated-autoload-file have Unix EOLs, so
627 ;; that it is portable to all platforms.
628 (or (eq 0 (coding-system-eol-type buffer-file-coding-system
))
629 (set-buffer-file-coding-system 'unix
))
630 (or (> (buffer-size) 0)
631 (error "Autoloads file %s lacks boilerplate" buffer-file-name
))
632 (or (file-writable-p buffer-file-name
)
633 (error "Autoloads file %s is not writable" buffer-file-name
))
635 (goto-char (point-min))
636 ;; Look for the section for LOAD-NAME.
637 (while (and (not found
)
638 (search-forward generate-autoload-section-header nil t
))
639 (let ((form (autoload-read-section-header)))
640 (cond ((string= (nth 2 form
) load-name
)
641 ;; We found the section for this file.
642 ;; Check if it is up to date.
643 (let ((begin (match-beginning 0))
644 (last-time (nth 4 form
))
645 (file-time (nth 5 (file-attributes file
))))
646 (if (and (or (null existing-buffer
)
647 (not (buffer-modified-p existing-buffer
)))
649 ;; last-time is the time-stamp (specifying
650 ;; the last time we looked at the file) and
651 ;; the file hasn't been changed since.
652 (and (listp last-time
) (= (length last-time
) 2)
653 (not (time-less-p last-time file-time
)))
654 ;; last-time is an MD5 checksum instead.
655 (and (stringp last-time
)
657 (md5 buf nil nil
'emacs-mule
)))))
658 (throw 'up-to-date nil
)
659 (autoload-remove-section begin
)
661 ((string< load-name
(nth 2 form
))
662 ;; We've come to a section alphabetically later than
663 ;; LOAD-NAME. We assume the file is in order and so
664 ;; there must be no section for LOAD-NAME. We will
665 ;; insert one before the section here.
666 (goto-char (match-beginning 0))
670 ;; No later sections in the file. Put before the last page.
671 (goto-char (point-max))
672 (search-backward "\f" nil t
)))
673 (unless (memq (current-buffer) autoload-modified-buffers
)
674 (push (current-buffer) autoload-modified-buffers
))
677 (defun autoload-remove-section (begin)
679 (search-forward generate-autoload-section-trailer
)
680 (delete-region begin
(point)))
683 (defun update-directory-autoloads (&rest dirs
)
684 "Update autoload definitions for Lisp files in the directories DIRS.
685 In an interactive call, you must give one argument, the name of a
686 single directory. In a call from Lisp, you can supply multiple
687 directories as separate arguments, but this usage is discouraged.
689 The function does NOT recursively descend into subdirectories of the
690 directory or directories specified.
692 In an interactive call, prompt for a default output file for the
693 autoload definitions, and temporarily bind the variable
694 `generated-autoload-file' to this value. When called from Lisp,
695 use the existing value of `generated-autoload-file'. If any Lisp
696 file binds `generated-autoload-file' as a file-local variable,
697 write its autoloads into the specified file instead."
698 (interactive "DUpdate autoloads from directory: ")
699 (let* ((files-re (let ((tmp nil
))
700 (dolist (suf (get-load-suffixes))
701 (unless (string-match "\\.elc" suf
) (push suf tmp
)))
702 (concat "^[^=.].*" (regexp-opt tmp t
) "\\'")))
704 (mapcar (lambda (dir)
705 (directory-files (expand-file-name dir
)
709 (this-time (current-time))
710 ;; Files with no autoload cookies or whose autoloads go to other
711 ;; files because of file-local autoload-generated-file settings.
713 (autoload-modified-buffers nil
)
714 (generated-autoload-file
715 (if (called-interactively-p 'interactive
)
716 (read-file-name "Write autoload definitions to file: ")
717 generated-autoload-file
)))
719 (with-current-buffer (autoload-find-generated-file)
721 ;; Canonicalize file names and remove the autoload file itself.
722 (setq files
(delete (file-relative-name buffer-file-name
)
723 (mapcar 'file-relative-name files
)))
725 (goto-char (point-min))
726 (while (search-forward generate-autoload-section-header nil t
)
727 (let* ((form (autoload-read-section-header))
729 (cond ((and (consp file
) (stringp (car file
)))
730 ;; This is a list of files that have no autoload cookies.
731 ;; There shouldn't be more than one such entry.
732 ;; Remove the obsolete section.
733 (autoload-remove-section (match-beginning 0))
734 (let ((last-time (nth 4 form
)))
736 (let ((file-time (nth 5 (file-attributes file
))))
738 (not (time-less-p last-time file-time
)))
740 (push file no-autoloads
)
741 (setq files
(delete file files
)))))))
742 ((not (stringp file
)))
743 ((or (not (file-exists-p file
))
744 ;; Remove duplicates as well, just in case.
746 ;; If the file is actually excluded.
747 (member (expand-file-name file
) autoload-excludes
))
748 ;; Remove the obsolete section.
749 (autoload-remove-section (match-beginning 0)))
750 ((not (time-less-p (nth 4 form
)
751 (nth 5 (file-attributes file
))))
752 ;; File hasn't changed.
755 (autoload-remove-section (match-beginning 0))
756 (if (autoload-generate-file-autoloads
757 ;; Passing `current-buffer' makes it insert at point.
758 file
(current-buffer) buffer-file-name
)
759 (push file no-autoloads
))))
761 (setq files
(delete file files
)))))
762 ;; Elements remaining in FILES have no existing autoload sections yet.
765 ((member (expand-file-name file
) autoload-excludes
) nil
)
766 ;; Passing nil as second argument forces
767 ;; autoload-generate-file-autoloads to look for the right
768 ;; spot where to insert each autoloads section.
769 ((autoload-generate-file-autoloads file nil buffer-file-name
)
770 (push file no-autoloads
))))
773 ;; Sort them for better readability.
774 (setq no-autoloads
(sort no-autoloads
'string
<))
775 ;; Add the `no-autoloads' section.
776 (goto-char (point-max))
777 (search-backward "\f" nil t
)
778 (autoload-insert-section-header
779 (current-buffer) nil nil no-autoloads this-time
)
780 (insert generate-autoload-section-trailer
))
782 (let ((version-control 'never
))
784 ;; In case autoload entries were added to other files because of
785 ;; file-local autoload-generated-file settings.
786 (autoload-save-buffers))))
788 (define-obsolete-function-alias 'update-autoloads-from-directories
789 'update-directory-autoloads
"22.1")
792 (defun batch-update-autoloads ()
793 "Update loaddefs.el autoloads in batch mode.
794 Calls `update-directory-autoloads' on the command line arguments.
795 Definitions are written to `generated-autoload-file' (which
797 ;; For use during the Emacs build process only.
798 ;; Exclude those files that are preloaded on ALL platforms.
799 ;; These are the ones in loadup.el where "(load" is at the start
800 ;; of the line (crude, but it works).
801 (unless autoload-excludes
802 (let ((default-directory (file-name-directory generated-autoload-file
))
804 (when (file-readable-p "loadup.el")
806 (insert-file-contents "loadup.el")
807 (while (re-search-forward "^(load \"\\([^\"]+\\)\"" nil t
)
808 (setq file
(match-string 1))
809 (or (string-match "\\.el\\'" file
)
810 (setq file
(format "%s.el" file
)))
811 (or (string-match "\\`site-" file
)
812 (push (expand-file-name file
) autoload-excludes
)))))))
813 (let ((args command-line-args-left
))
814 (setq command-line-args-left nil
)
815 (apply 'update-directory-autoloads args
)))
819 ;;; autoload.el ends here