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