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