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