; Merge from origin/emacs-25
[emacs.git] / lisp / emacs-lisp / autoload.el
blobaa58f7b27fa4490c4f123530d9582f57bd6993c3
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 ;; In some ways it would be nicer to use a value that is recognizably
91 ;; not a time-value, eg t, but that can cause issues if an older Emacs
92 ;; that does not expect non-time-values loads the file.
93 (defconst autoload--non-timestamp '(0 0 0 0)
94 "Value to insert when `autoload-timestamps' is nil.")
96 (defvar autoload-timestamps nil ; experimental, see bug#22213
97 "Non-nil means insert a timestamp for each input file into the output.
98 We use these in incremental updates of the output file to decide
99 if we need to rescan an input file. If you set this to nil,
100 then we use the timestamp of the output file instead. As a result:
101 - for fixed inputs, the output will be the same every time
102 - incremental updates of the output file might not be correct if:
103 i) the timestamp of the output file cannot be trusted (at least
104 relative to that of the input files)
105 ii) any of the input files can be modified during the time it takes
106 to create the output
107 iii) only a subset of the input files are scanned
108 These issues are unlikely to happen in practice, and would arguably
109 represent bugs in the build system. Item iii) will happen if you
110 use a command like `update-file-autoloads', though, since it only
111 checks a single input file.")
113 (defvar autoload-modified-buffers) ;Dynamically scoped var.
115 (defun make-autoload (form file &optional expansion)
116 "Turn FORM into an autoload or defvar for source file FILE.
117 Returns nil if FORM is not a special autoload form (i.e. a function definition
118 or macro definition or a defcustom).
119 If EXPANSION is non-nil, we're processing the macro expansion of an
120 expression, in which case we want to handle forms differently."
121 (let ((car (car-safe form)) expand)
122 (cond
123 ((and expansion (eq car 'defalias))
124 (pcase-let*
125 ((`(,_ ,_ ,arg . ,rest) form)
126 ;; `type' is non-nil if it defines a macro.
127 ;; `fun' is the function part of `arg' (defaults to `arg').
128 ((or (and (or `(cons 'macro ,fun) `'(macro . ,fun)) (let type t))
129 (and (let fun arg) (let type nil)))
130 arg)
131 ;; `lam' is the lambda expression in `fun' (or nil if not
132 ;; recognized).
133 (lam (if (memq (car-safe fun) '(quote function)) (cadr fun)))
134 ;; `args' is the list of arguments (or t if not recognized).
135 ;; `body' is the body of `lam' (or t if not recognized).
136 ((or `(lambda ,args . ,body)
137 (and (let args t) (let body t)))
138 lam)
139 ;; Get the `doc' from `body' or `rest'.
140 (doc (cond ((stringp (car-safe body)) (car body))
141 ((stringp (car-safe rest)) (car rest))))
142 ;; Look for an interactive spec.
143 (interactive (pcase body
144 ((or `((interactive . ,_) . ,_)
145 `(,_ (interactive . ,_) . ,_))
146 t))))
147 ;; Add the usage form at the end where describe-function-1
148 ;; can recover it.
149 (when (listp args) (setq doc (help-add-fundoc-usage doc args)))
150 ;; (message "autoload of %S" (nth 1 form))
151 `(autoload ,(nth 1 form) ,file ,doc ,interactive ,type)))
153 ((and expansion (memq car '(progn prog1)))
154 (let ((end (memq :autoload-end form)))
155 (when end ;Cut-off anything after the :autoload-end marker.
156 (setq form (copy-sequence form))
157 (setcdr (memq :autoload-end form) nil))
158 (let ((exps (delq nil (mapcar (lambda (form)
159 (make-autoload form file expansion))
160 (cdr form)))))
161 (when exps (cons 'progn exps)))))
163 ;; For complex cases, try again on the macro-expansion.
164 ((and (memq car '(easy-mmode-define-global-mode define-global-minor-mode
165 define-globalized-minor-mode defun defmacro
166 easy-mmode-define-minor-mode define-minor-mode
167 define-inline cl-defun cl-defmacro))
168 (macrop car)
169 (setq expand (let ((load-file-name file)) (macroexpand form)))
170 (memq (car expand) '(progn prog1 defalias)))
171 (make-autoload expand file 'expansion)) ;Recurse on the expansion.
173 ;; For special function-like operators, use the `autoload' function.
174 ((memq car '(define-skeleton define-derived-mode
175 define-compilation-mode define-generic-mode
176 easy-mmode-define-global-mode define-global-minor-mode
177 define-globalized-minor-mode
178 easy-mmode-define-minor-mode define-minor-mode
179 cl-defun defun* cl-defmacro defmacro*
180 define-overloadable-function))
181 (let* ((macrop (memq car '(defmacro cl-defmacro defmacro*)))
182 (name (nth 1 form))
183 (args (pcase car
184 ((or `defun `defmacro
185 `defun* `defmacro* `cl-defun `cl-defmacro
186 `define-overloadable-function)
187 (nth 2 form))
188 (`define-skeleton '(&optional str arg))
189 ((or `define-generic-mode `define-derived-mode
190 `define-compilation-mode)
191 nil)
192 (_ t)))
193 (body (nthcdr (or (function-get car 'doc-string-elt) 3) form))
194 (doc (if (stringp (car body)) (pop body))))
195 ;; Add the usage form at the end where describe-function-1
196 ;; can recover it.
197 (when (listp args) (setq doc (help-add-fundoc-usage doc args)))
198 ;; `define-generic-mode' quotes the name, so take care of that
199 `(autoload ,(if (listp name) name (list 'quote name))
200 ,file ,doc
201 ,(or (and (memq car '(define-skeleton define-derived-mode
202 define-generic-mode
203 easy-mmode-define-global-mode
204 define-global-minor-mode
205 define-globalized-minor-mode
206 easy-mmode-define-minor-mode
207 define-minor-mode))
209 (eq (car-safe (car body)) 'interactive))
210 ,(if macrop ''macro nil))))
212 ;; For defclass forms, use `eieio-defclass-autoload'.
213 ((eq car 'defclass)
214 (let ((name (nth 1 form))
215 (superclasses (nth 2 form))
216 (doc (nth 4 form)))
217 (list 'eieio-defclass-autoload (list 'quote name)
218 (list 'quote superclasses) file doc)))
220 ;; Convert defcustom to less space-consuming data.
221 ((eq car 'defcustom)
222 (let ((varname (car-safe (cdr-safe form)))
223 (init (car-safe (cdr-safe (cdr-safe form))))
224 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form)))))
225 ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
227 `(progn
228 (defvar ,varname ,init ,doc)
229 (custom-autoload ',varname ,file
230 ,(condition-case nil
231 (null (cadr (memq :set form)))
232 (error nil))))))
234 ((eq car 'defgroup)
235 ;; In Emacs this is normally handled separately by cus-dep.el, but for
236 ;; third party packages, it can be convenient to explicitly autoload
237 ;; a group.
238 (let ((groupname (nth 1 form)))
239 `(let ((loads (get ',groupname 'custom-loads)))
240 (if (member ',file loads) nil
241 (put ',groupname 'custom-loads (cons ',file loads))))))
243 ;; When processing a macro expansion, any expression
244 ;; before a :autoload-end should be included. These are typically (put
245 ;; 'fun 'prop val) and things like that.
246 ((and expansion (consp form)) form)
248 ;; nil here indicates that this is not a special autoload form.
249 (t nil))))
251 ;; Forms which have doc-strings which should be printed specially.
252 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
253 ;; the doc-string in FORM.
254 ;; Those properties are now set in lisp-mode.el.
256 (defun autoload-find-generated-file ()
257 "Visit the autoload file for the current buffer, and return its buffer.
258 If a buffer is visiting the desired autoload file, return it."
259 (let ((enable-local-variables :safe)
260 (enable-local-eval nil))
261 ;; We used to use `raw-text' to read this file, but this causes
262 ;; problems when the file contains non-ASCII characters.
263 (let* ((delay-mode-hooks t)
264 (file (autoload-generated-file))
265 (file-missing (not (file-exists-p file))))
266 (when file-missing
267 (autoload-ensure-default-file file))
268 (with-current-buffer
269 (find-file-noselect
270 (autoload-ensure-file-writeable
271 file))
272 ;; block backups when the file has just been created, since
273 ;; the backups will just be the auto-generated headers.
274 ;; bug#23203
275 (when file-missing
276 (setq buffer-backed-up t)
277 (save-buffer))
278 (current-buffer)))))
280 (defun autoload-generated-file ()
281 (expand-file-name generated-autoload-file
282 ;; File-local settings of generated-autoload-file should
283 ;; be interpreted relative to the file's location,
284 ;; of course.
285 (if (not (local-variable-p 'generated-autoload-file))
286 (expand-file-name "lisp" source-directory))))
289 (defun autoload-read-section-header ()
290 "Read a section header form.
291 Since continuation lines have been marked as comments,
292 we must copy the text of the form and remove those comment
293 markers before we call `read'."
294 (save-match-data
295 (let ((beginning (point))
296 string)
297 (forward-line 1)
298 (while (looking-at generate-autoload-section-continuation)
299 (forward-line 1))
300 (setq string (buffer-substring beginning (point)))
301 (with-current-buffer (get-buffer-create " *autoload*")
302 (erase-buffer)
303 (insert string)
304 (goto-char (point-min))
305 (while (search-forward generate-autoload-section-continuation nil t)
306 (replace-match " "))
307 (goto-char (point-min))
308 (read (current-buffer))))))
310 (defvar autoload-print-form-outbuf nil
311 "Buffer which gets the output of `autoload-print-form'.")
313 (defun autoload-print-form (form)
314 "Print FORM such that `make-docfile' will find the docstrings.
315 The variable `autoload-print-form-outbuf' specifies the buffer to
316 put the output in."
317 (cond
318 ;; If the form is a sequence, recurse.
319 ((eq (car form) 'progn) (mapcar #'autoload-print-form (cdr form)))
320 ;; Symbols at the toplevel are meaningless.
321 ((symbolp form) nil)
323 (let ((doc-string-elt (function-get (car-safe form) 'doc-string-elt))
324 (outbuf autoload-print-form-outbuf))
325 (if (and doc-string-elt (stringp (nth doc-string-elt form)))
326 ;; We need to hack the printing because the
327 ;; doc-string must be printed specially for
328 ;; make-docfile (sigh).
329 (let* ((p (nthcdr (1- doc-string-elt) form))
330 (elt (cdr p)))
331 (setcdr p nil)
332 (princ "\n(" outbuf)
333 (let ((print-escape-newlines t)
334 (print-quoted t)
335 (print-escape-nonascii t))
336 (dolist (elt form)
337 (prin1 elt outbuf)
338 (princ " " outbuf)))
339 (princ "\"\\\n" outbuf)
340 (let ((begin (with-current-buffer outbuf (point))))
341 (princ (substring (prin1-to-string (car elt)) 1)
342 outbuf)
343 ;; Insert a backslash before each ( that
344 ;; appears at the beginning of a line in
345 ;; the doc string.
346 (with-current-buffer outbuf
347 (save-excursion
348 (while (re-search-backward "\n[[(]" begin t)
349 (forward-char 1)
350 (insert "\\"))))
351 (if (null (cdr elt))
352 (princ ")" outbuf)
353 (princ " " outbuf)
354 (princ (substring (prin1-to-string (cdr elt)) 1)
355 outbuf))
356 (terpri outbuf)))
357 (let ((print-escape-newlines t)
358 (print-quoted t)
359 (print-escape-nonascii t))
360 (print form outbuf)))))))
362 (defun autoload-rubric (file &optional type feature)
363 "Return a string giving the appropriate autoload rubric for FILE.
364 TYPE (default \"autoloads\") is a string stating the type of
365 information contained in FILE. If FEATURE is non-nil, FILE
366 will provide a feature. FEATURE may be a string naming the
367 feature, otherwise it will be based on FILE's name.
369 At present, a feature is in fact always provided, but this should
370 not be relied upon."
371 (let ((basename (file-name-nondirectory file)))
372 (concat ";;; " basename
373 " --- automatically extracted " (or type "autoloads") "\n"
374 ";;\n"
375 ";;; Code:\n\n"
376 "\f\n"
377 ;; This is used outside of autoload.el, eg cus-dep, finder.
378 "(provide '"
379 (if (stringp feature)
380 feature
381 (file-name-sans-extension basename))
382 ")\n"
383 ";; Local Variables:\n"
384 ";; version-control: never\n"
385 ";; no-byte-compile: t\n"
386 ";; no-update-autoloads: t\n"
387 ";; coding: utf-8\n"
388 ";; End:\n"
389 ";;; " basename
390 " ends here\n")))
392 (defvar autoload-ensure-writable nil
393 "Non-nil means `autoload-ensure-default-file' makes existing file writable.")
394 ;; Just in case someone tries to get you to overwrite a file that you
395 ;; don't want to.
396 ;;;###autoload
397 (put 'autoload-ensure-writable 'risky-local-variable t)
399 (defun autoload-ensure-file-writeable (file)
400 ;; Probably pointless, but replaces the old AUTOGEN_VCS in lisp/Makefile,
401 ;; which was designed to handle CVSREAD=1 and equivalent.
402 (and autoload-ensure-writable
403 (let ((modes (file-modes file)))
404 (if (zerop (logand modes #o0200))
405 ;; Ignore any errors here, and let subsequent attempts
406 ;; to write the file raise any real error.
407 (ignore-errors (set-file-modes file (logior modes #o0200))))))
408 file)
410 (defun autoload-ensure-default-file (file)
411 "Make sure that the autoload file FILE exists, creating it if needed.
412 If the file already exists and `autoload-ensure-writable' is non-nil,
413 make it writable."
414 (write-region (autoload-rubric file) nil file))
416 (defun autoload-insert-section-header (outbuf autoloads load-name file time)
417 "Insert the section-header line,
418 which lists the file name and which functions are in it, etc."
419 ;; (cl-assert ;Make sure we don't insert it in the middle of another section.
420 ;; (save-excursion
421 ;; (or (not (re-search-backward
422 ;; (concat "\\("
423 ;; (regexp-quote generate-autoload-section-header)
424 ;; "\\)\\|\\("
425 ;; (regexp-quote generate-autoload-section-trailer)
426 ;; "\\)")
427 ;; nil t))
428 ;; (match-end 2))))
429 (insert generate-autoload-section-header)
430 (prin1 `(autoloads ,autoloads ,load-name ,file ,time)
431 outbuf)
432 (terpri outbuf)
433 ;; Break that line at spaces, to avoid very long lines.
434 ;; Make each sub-line into a comment.
435 (with-current-buffer outbuf
436 (save-excursion
437 (forward-line -1)
438 (while (not (eolp))
439 (move-to-column 64)
440 (skip-chars-forward "^ \n")
441 (or (eolp)
442 (insert "\n" generate-autoload-section-continuation))))))
444 (defun autoload-find-file (file)
445 "Fetch file and put it in a temp buffer. Return the buffer."
446 ;; It is faster to avoid visiting the file.
447 (setq file (expand-file-name file))
448 (with-current-buffer (get-buffer-create " *autoload-file*")
449 (kill-all-local-variables)
450 (erase-buffer)
451 (setq buffer-undo-list t
452 buffer-read-only nil)
453 (delay-mode-hooks (emacs-lisp-mode))
454 (setq default-directory (file-name-directory file))
455 (insert-file-contents file nil)
456 (let ((enable-local-variables :safe)
457 (enable-local-eval nil))
458 (hack-local-variables))
459 (current-buffer)))
461 (defvar no-update-autoloads nil
462 "File local variable to prevent scanning this file for autoload cookies.")
464 (defun autoload-file-load-name (file)
465 "Compute the name that will be used to load FILE."
466 ;; OUTFILE should be the name of the global loaddefs.el file, which
467 ;; is expected to be at the root directory of the files we're
468 ;; scanning for autoloads and will be in the `load-path'.
469 (let* ((outfile (default-value 'generated-autoload-file))
470 (name (file-relative-name file (file-name-directory outfile)))
471 (names '())
472 (dir (file-name-directory outfile)))
473 ;; If `name' has directory components, only keep the
474 ;; last few that are really needed.
475 (while name
476 (setq name (directory-file-name name))
477 (push (file-name-nondirectory name) names)
478 (setq name (file-name-directory name)))
479 (while (not name)
480 (cond
481 ((null (cdr names)) (setq name (car names)))
482 ((file-exists-p (expand-file-name "subdirs.el" dir))
483 ;; FIXME: here we only check the existence of subdirs.el,
484 ;; without checking its content. This makes it generate wrong load
485 ;; names for cases like lisp/term which is not added to load-path.
486 (setq dir (expand-file-name (pop names) dir)))
487 (t (setq name (mapconcat #'identity names "/")))))
488 (if (string-match "\\.elc?\\(\\.\\|\\'\\)" name)
489 (substring name 0 (match-beginning 0))
490 name)))
492 (defun generate-file-autoloads (file)
493 "Insert at point a loaddefs autoload section for FILE.
494 Autoloads are generated for defuns and defmacros in FILE
495 marked by `generate-autoload-cookie' (which see).
496 If FILE is being visited in a buffer, the contents of the buffer
497 are used.
498 Return non-nil in the case where no autoloads were added at point."
499 (interactive "fGenerate autoloads for file: ")
500 (let ((generated-autoload-file buffer-file-name))
501 (autoload-generate-file-autoloads file (current-buffer))))
503 (defvar autoload-compute-prefixes t
504 "If non-nil, autoload will add code to register the prefixes used in a file.
505 Standard prefixes won't be registered anyway. I.e. if a file \"foo.el\" defines
506 variables or functions that use \"foo-\" as prefix, that will not be registered.
507 But all other prefixes will be included.")
509 (defconst autoload-def-prefixes-max-entries 5
510 "Target length of the list of definition prefixes per file.
511 If set too small, the prefixes will be too generic (i.e. they'll use little
512 memory, we'll end up looking in too many files when we need a particular
513 prefix), and if set too large, they will be too specific (i.e. they will
514 cost more memory use).")
516 (defconst autoload-def-prefixes-max-length 12
517 "Target size of definition prefixes.
518 Don't try to split prefixes that are already longer than that.")
520 (require 'radix-tree)
522 (defun autoload--make-defs-autoload (defs file)
524 ;; Remove the defs that obey the rule that file foo.el (or
525 ;; foo-mode.el) uses "foo-" as prefix.
526 ;; FIXME: help--symbol-completion-table still doesn't know how to use
527 ;; the rule that file foo.el (or foo-mode.el) uses "foo-" as prefix.
528 ;;(let ((prefix
529 ;; (concat (substring file 0 (string-match "-mode\\'" file)) "-")))
530 ;; (dolist (def (prog1 defs (setq defs nil)))
531 ;; (unless (string-prefix-p prefix def)
532 ;; (push def defs))))
534 ;; Then compute a small set of prefixes that cover all the
535 ;; remaining definitions.
536 (let* ((tree (let ((tree radix-tree-empty))
537 (dolist (def defs)
538 (setq tree (radix-tree-insert tree def t)))
539 tree))
540 (prefixes nil))
541 ;; Get the root prefixes, that we should include in any case.
542 (radix-tree-iter-subtrees
543 tree (lambda (prefix subtree)
544 (push (cons prefix subtree) prefixes)))
545 ;; In some cases, the root prefixes are too short, e.g. if you define
546 ;; "cc-helper" and "c-mode", you'll get "c" in the root prefixes.
547 (dolist (pair (prog1 prefixes (setq prefixes nil)))
548 (let ((s (car pair)))
549 (if (or (> (length s) 2) ;Long enough!
550 (string-match ".[[:punct:]]\\'" s) ;A real (tho short) prefix?
551 (radix-tree-lookup (cdr pair) "")) ;Nothing to expand!
552 (push pair prefixes) ;Keep it as is.
553 (radix-tree-iter-subtrees
554 (cdr pair) (lambda (prefix subtree)
555 (push (cons (concat s prefix) subtree) prefixes))))))
556 ;; FIXME: The expansions done below are mostly pointless, such as
557 ;; for `yenc', where we replace "yenc-" with an exhaustive list (5
558 ;; elements).
559 ;; (while
560 ;; (let ((newprefixes nil)
561 ;; (changes nil))
562 ;; (dolist (pair prefixes)
563 ;; (let ((prefix (car pair)))
564 ;; (if (or (> (length prefix) autoload-def-prefixes-max-length)
565 ;; (radix-tree-lookup (cdr pair) ""))
566 ;; ;; No point splitting it any further.
567 ;; (push pair newprefixes)
568 ;; (setq changes t)
569 ;; (radix-tree-iter-subtrees
570 ;; (cdr pair) (lambda (sprefix subtree)
571 ;; (push (cons (concat prefix sprefix) subtree)
572 ;; newprefixes))))))
573 ;; (and changes
574 ;; (<= (length newprefixes)
575 ;; autoload-def-prefixes-max-entries)
576 ;; (let ((new nil)
577 ;; (old nil))
578 ;; (dolist (pair prefixes)
579 ;; (unless (memq pair newprefixes) ;Not old
580 ;; (push pair old)))
581 ;; (dolist (pair newprefixes)
582 ;; (unless (memq pair prefixes) ;Not new
583 ;; (push pair new)))
584 ;; (cl-assert new)
585 ;; (message "Expanding %S to %S"
586 ;; (mapcar #'car old) (mapcar #'car new))
587 ;; t)
588 ;; (setq prefixes newprefixes)
589 ;; (< (length prefixes) autoload-def-prefixes-max-entries))))
591 ;; (message "Final prefixes %s : %S" file (mapcar #'car prefixes))
592 (when prefixes
593 (let ((strings
594 (mapcar
595 (lambda (x)
596 (let ((prefix (car x)))
597 (if (or (> (length prefix) 2) ;Long enough!
598 (string-match ".[[:punct:]]\\'" prefix))
599 prefix
600 ;; Some packages really don't follow the rules.
601 ;; Drop the most egregious cases such as the
602 ;; one-letter prefixes.
603 (let ((dropped ()))
604 (radix-tree-iter-mappings
605 (cdr x) (lambda (s _)
606 (push (concat prefix s) dropped)))
607 (message "Not registering prefix \"%s\" from %s. Affects: %S"
608 prefix file dropped)
609 nil))))
610 prefixes)))
611 `(if (fboundp 'register-definition-prefixes)
612 (register-definition-prefixes ,file ',(delq nil strings)))))))
614 (defun autoload--setup-output (otherbuf outbuf absfile load-name)
615 (let ((outbuf
616 (or (if otherbuf
617 ;; A file-local setting of
618 ;; autoload-generated-file says we
619 ;; should ignore OUTBUF.
621 outbuf)
622 (autoload-find-destination absfile load-name)
623 ;; The file has autoload cookies, but they're
624 ;; already up-to-date. If OUTFILE is nil, the
625 ;; entries are in the expected OUTBUF,
626 ;; otherwise they're elsewhere.
627 (throw 'done otherbuf))))
628 (with-current-buffer outbuf
629 (point-marker))))
631 (defun autoload--print-cookie-text (output-start load-name file)
632 (let ((standard-output (marker-buffer output-start)))
633 (search-forward generate-autoload-cookie)
634 (skip-chars-forward " \t")
635 (if (eolp)
636 (condition-case-unless-debug err
637 ;; Read the next form and make an autoload.
638 (let* ((form (prog1 (read (current-buffer))
639 (or (bolp) (forward-line 1))))
640 (autoload (make-autoload form load-name)))
641 (if autoload
643 (setq autoload form))
644 (let ((autoload-print-form-outbuf
645 standard-output))
646 (autoload-print-form autoload)))
647 (error
648 (message "Autoload cookie error in %s:%s %S"
649 file (count-lines (point-min) (point)) err)))
651 ;; Copy the rest of the line to the output.
652 (princ (buffer-substring
653 (progn
654 ;; Back up over whitespace, to preserve it.
655 (skip-chars-backward " \f\t")
656 (if (= (char-after (1+ (point))) ? )
657 ;; Eat one space.
658 (forward-char 1))
659 (point))
660 (progn (forward-line 1) (point)))))))
662 (defvar autoload-builtin-package-versions nil)
664 ;; When called from `generate-file-autoloads' we should ignore
665 ;; `generated-autoload-file' altogether. When called from
666 ;; `update-file-autoloads' we don't know `outbuf'. And when called from
667 ;; `update-directory-autoloads' it's in between: we know the default
668 ;; `outbuf' but we should obey any file-local setting of
669 ;; `generated-autoload-file'.
670 (defun autoload-generate-file-autoloads (file &optional outbuf outfile)
671 "Insert an autoload section for FILE in the appropriate buffer.
672 Autoloads are generated for defuns and defmacros in FILE
673 marked by `generate-autoload-cookie' (which see).
674 If FILE is being visited in a buffer, the contents of the buffer are used.
675 OUTBUF is the buffer in which the autoload statements should be inserted.
676 If OUTBUF is nil, it will be determined by `autoload-generated-file'.
678 If provided, OUTFILE is expected to be the file name of OUTBUF.
679 If OUTFILE is non-nil and FILE specifies a `generated-autoload-file'
680 different from OUTFILE, then OUTBUF is ignored.
682 Return non-nil if and only if FILE adds no autoloads to OUTFILE
683 \(or OUTBUF if OUTFILE is nil). The actual return value is
684 FILE's modification time."
685 ;; Include the file name in any error messages
686 (condition-case err
687 (let (load-name
688 (print-length nil)
689 (print-level nil)
690 (float-output-format nil)
691 (visited (get-file-buffer file))
692 (otherbuf nil)
693 (absfile (expand-file-name file))
694 (defs '())
695 ;; nil until we found a cookie.
696 output-start)
697 (when
698 (catch 'done
699 (with-current-buffer (or visited
700 ;; It is faster to avoid visiting the file.
701 (autoload-find-file file))
702 ;; Obey the no-update-autoloads file local variable.
703 (unless no-update-autoloads
704 (or noninteractive (message "Generating autoloads for %s..." file))
705 (setq load-name
706 (if (stringp generated-autoload-load-name)
707 generated-autoload-load-name
708 (autoload-file-load-name absfile)))
709 ;; FIXME? Comparing file-names for equality with just equal
710 ;; is fragile, eg if one has an automounter prefix and one
711 ;; does not, but both refer to the same physical file.
712 (when (and outfile
713 (not
714 (if (memq system-type '(ms-dos windows-nt))
715 (equal (downcase outfile)
716 (downcase (autoload-generated-file)))
717 (equal outfile (autoload-generated-file)))))
718 (setq otherbuf t))
719 (save-excursion
720 (save-restriction
721 (widen)
722 (when autoload-builtin-package-versions
723 (let ((version (lm-header "version"))
724 package)
725 (and version
726 (setq version (ignore-errors (version-to-list version)))
727 (setq package (or (lm-header "package")
728 (file-name-sans-extension
729 (file-name-nondirectory file))))
730 (setq output-start (autoload--setup-output
731 otherbuf outbuf absfile load-name))
732 (let ((standard-output (marker-buffer output-start))
733 (print-quoted t))
734 (princ `(push (purecopy
735 ',(cons (intern package) version))
736 package--builtin-versions))
737 (princ "\n")))))
739 ;; Do not insert autoload entries for excluded files.
740 (unless (member absfile autoload-excludes)
741 (goto-char (point-min))
742 (while (not (eobp))
743 (skip-chars-forward " \t\n\f")
744 (cond
745 ((looking-at (regexp-quote generate-autoload-cookie))
746 ;; If not done yet, figure out where to insert this text.
747 (unless output-start
748 (setq output-start (autoload--setup-output
749 otherbuf outbuf absfile load-name)))
750 (autoload--print-cookie-text output-start load-name file))
751 ((looking-at ";")
752 ;; Don't read the comment.
753 (forward-line 1))
755 ;; Avoid (defvar <foo>) by requiring a trailing space.
756 ;; Also, ignore this prefix business
757 ;; for ;;;###tramp-autoload and friends.
758 (when (and (equal generate-autoload-cookie ";;;###autoload")
759 (looking-at "(\\(def[^ ]+\\) ['(]*\\([^' ()\"\n]+\\)[\n \t]")
760 (not (member
761 (match-string 1)
762 '("define-obsolete-function-alias"
763 "define-obsolete-variable-alias"
764 "define-category" "define-key"
765 "defgroup" "defface" "defadvice"
766 "def-edebug-spec"
767 ;; Hmm... this is getting ugly:
768 "define-widget"
769 "define-erc-response-handler"
770 "defun-rcirc-command"))))
771 (push (match-string 2) defs))
772 (forward-sexp 1)
773 (forward-line 1)))))))
775 (when (and autoload-compute-prefixes defs)
776 ;; This output needs to always go in the main loaddefs.el,
777 ;; regardless of generated-autoload-file.
778 ;; FIXME: the files that don't have autoload cookies but
779 ;; do have definitions end up listed twice in loaddefs.el:
780 ;; once for their register-definition-prefixes and once in
781 ;; the list of "files without any autoloads".
782 (let ((form (autoload--make-defs-autoload defs load-name)))
783 (cond
784 ((null form)) ;All defs obey the default rule, yay!
785 ((not otherbuf)
786 (unless output-start
787 (setq output-start (autoload--setup-output
788 nil outbuf absfile load-name)))
789 (let ((autoload-print-form-outbuf
790 (marker-buffer output-start)))
791 (autoload-print-form form)))
793 (let* ((other-output-start
794 ;; To force the output to go to the main loaddefs.el
795 ;; rather than to generated-autoload-file,
796 ;; there are two cases: if outbuf is non-nil,
797 ;; then passing otherbuf=nil is enough, but if
798 ;; outbuf is nil, that won't cut it, so we
799 ;; locally bind generated-autoload-file.
800 (let ((generated-autoload-file
801 (default-value 'generated-autoload-file)))
802 (autoload--setup-output nil outbuf absfile load-name)))
803 (autoload-print-form-outbuf
804 (marker-buffer other-output-start)))
805 (autoload-print-form form)
806 (with-current-buffer (marker-buffer other-output-start)
807 (save-excursion
808 ;; Insert the section-header line which lists
809 ;; the file name and which functions are in it, etc.
810 (goto-char other-output-start)
811 (let ((relfile (file-relative-name absfile)))
812 (autoload-insert-section-header
813 (marker-buffer other-output-start)
814 "actual autoloads are elsewhere" load-name relfile
815 (if autoload-timestamps
816 (nth 5 (file-attributes absfile))
817 autoload--non-timestamp))
818 (insert ";;; Generated autoloads from " relfile "\n")))
819 (insert generate-autoload-section-trailer)))))))
821 (when output-start
822 (let ((secondary-autoloads-file-buf
823 (if otherbuf (current-buffer))))
824 (with-current-buffer (marker-buffer output-start)
825 (cl-assert (> (point) output-start))
826 (save-excursion
827 ;; Insert the section-header line which lists the file name
828 ;; and which functions are in it, etc.
829 (goto-char output-start)
830 (let ((relfile (file-relative-name absfile)))
831 (autoload-insert-section-header
832 (marker-buffer output-start)
833 () load-name relfile
834 (if secondary-autoloads-file-buf
835 ;; MD5 checksums are much better because they do not
836 ;; change unless the file changes (so they'll be
837 ;; equal on two different systems and will change
838 ;; less often than time-stamps, thus leading to fewer
839 ;; unneeded changes causing spurious conflicts), but
840 ;; using time-stamps is a very useful optimization,
841 ;; so we use time-stamps for the main autoloads file
842 ;; (loaddefs.el) where we have special ways to
843 ;; circumvent the "random change problem", and MD5
844 ;; checksum in secondary autoload files where we do
845 ;; not need the time-stamp optimization because it is
846 ;; already provided by the primary autoloads file.
847 (md5 secondary-autoloads-file-buf
848 ;; We'd really want to just use
849 ;; `emacs-internal' instead.
850 nil nil 'emacs-mule-unix)
851 (if autoload-timestamps
852 (nth 5 (file-attributes relfile))
853 autoload--non-timestamp)))
854 (insert ";;; Generated autoloads from " relfile "\n")))
855 (insert generate-autoload-section-trailer))))
856 (or noninteractive
857 (message "Generating autoloads for %s...done" file)))
858 (or visited
859 ;; We created this buffer, so we should kill it.
860 (kill-buffer (current-buffer))))
861 (or (not output-start)
862 ;; If the entries were added to some other buffer, then the file
863 ;; doesn't add entries to OUTFILE.
864 otherbuf))
865 (nth 5 (file-attributes absfile))))
866 (error
867 ;; Probably unbalanced parens in forward-sexp. In that case, the
868 ;; condition is scan-error, and the signal data includes point
869 ;; where the error was found; we'd like to convert that to
870 ;; line:col, but line-number-at-pos gets the wrong line in batch
871 ;; mode for some reason.
873 ;; At least this gets the file name in the error message; the
874 ;; developer can use goto-char to get to the error position.
875 (error "%s:0:0: error: %s: %s" file (car err) (cdr err)))
878 (defun autoload-save-buffers ()
879 (while autoload-modified-buffers
880 (with-current-buffer (pop autoload-modified-buffers)
881 (let ((version-control 'never))
882 (save-buffer)))))
884 ;; FIXME This command should be deprecated.
885 ;; See http://debbugs.gnu.org/22213#41
886 ;;;###autoload
887 (defun update-file-autoloads (file &optional save-after outfile)
888 "Update the autoloads for FILE.
889 If prefix arg SAVE-AFTER is non-nil, save the buffer too.
891 If FILE binds `generated-autoload-file' as a file-local variable,
892 autoloads are written into that file. Otherwise, the autoloads
893 file is determined by OUTFILE. If called interactively, prompt
894 for OUTFILE; if called from Lisp with OUTFILE nil, use the
895 existing value of `generated-autoload-file'.
897 Return FILE if there was no autoload cookie in it, else nil."
898 (interactive (list (read-file-name "Update autoloads for file: ")
899 current-prefix-arg
900 (read-file-name "Write autoload definitions to file: ")))
901 (let* ((generated-autoload-file (or outfile generated-autoload-file))
902 (autoload-modified-buffers nil)
903 ;; We need this only if the output file handles more than one input.
904 ;; See http://debbugs.gnu.org/22213#38 and subsequent.
905 (autoload-timestamps t)
906 (no-autoloads (autoload-generate-file-autoloads file)))
907 (if autoload-modified-buffers
908 (if save-after (autoload-save-buffers))
909 (if (called-interactively-p 'interactive)
910 (message "Autoload section for %s is up to date." file)))
911 (if no-autoloads file)))
913 (defun autoload-find-destination (file load-name)
914 "Find the destination point of the current buffer's autoloads.
915 FILE is the file name of the current buffer.
916 LOAD-NAME is the name as it appears in the output.
917 Returns a buffer whose point is placed at the requested location.
918 Returns nil if the file's autoloads are up-to-date, otherwise
919 removes any prior now out-of-date autoload entries."
920 (catch 'up-to-date
921 (let* ((buf (current-buffer))
922 (existing-buffer (if buffer-file-name buf))
923 (output-file (autoload-generated-file))
924 (output-time (if (file-exists-p output-file)
925 (nth 5 (file-attributes output-file))))
926 (found nil))
927 (with-current-buffer (autoload-find-generated-file)
928 ;; This is to make generated-autoload-file have Unix EOLs, so
929 ;; that it is portable to all platforms.
930 (or (eq 0 (coding-system-eol-type buffer-file-coding-system))
931 (set-buffer-file-coding-system 'unix))
932 (or (> (buffer-size) 0)
933 (error "Autoloads file %s lacks boilerplate" buffer-file-name))
934 (or (file-writable-p buffer-file-name)
935 (error "Autoloads file %s is not writable" buffer-file-name))
936 (widen)
937 (goto-char (point-min))
938 ;; Look for the section for LOAD-NAME.
939 (while (and (not found)
940 (search-forward generate-autoload-section-header nil t))
941 (let ((form (autoload-read-section-header)))
942 (cond ((string= (nth 2 form) load-name)
943 ;; We found the section for this file.
944 ;; Check if it is up to date.
945 (let ((begin (match-beginning 0))
946 (last-time (nth 4 form))
947 (file-time (nth 5 (file-attributes file))))
948 (if (and (or (null existing-buffer)
949 (not (buffer-modified-p existing-buffer)))
950 (cond
951 ;; FIXME? Arguably we should throw a
952 ;; user error, or some kind of warning,
953 ;; if we were called from update-file-autoloads,
954 ;; which can update only a single input file.
955 ;; It's not appropriate to use the output
956 ;; file modtime in such a case,
957 ;; if there are multiple input files
958 ;; contributing to the output.
959 ((and output-time
960 (member last-time
961 (list t autoload--non-timestamp)))
962 (not (time-less-p output-time file-time)))
963 ;; last-time is the time-stamp (specifying
964 ;; the last time we looked at the file) and
965 ;; the file hasn't been changed since.
966 ((listp last-time)
967 (not (time-less-p last-time file-time)))
968 ;; last-time is an MD5 checksum instead.
969 ((stringp last-time)
970 (equal last-time
971 (md5 buf nil nil 'emacs-mule)))))
972 (throw 'up-to-date nil)
973 (autoload-remove-section begin)
974 (setq found t))))
975 ((string< load-name (nth 2 form))
976 ;; We've come to a section alphabetically later than
977 ;; LOAD-NAME. We assume the file is in order and so
978 ;; there must be no section for LOAD-NAME. We will
979 ;; insert one before the section here.
980 (goto-char (match-beginning 0))
981 (setq found t)))))
982 (or found
983 (progn
984 ;; No later sections in the file. Put before the last page.
985 (goto-char (point-max))
986 (search-backward "\f" nil t)))
987 (unless (memq (current-buffer) autoload-modified-buffers)
988 (push (current-buffer) autoload-modified-buffers))
989 (current-buffer)))))
991 (defun autoload-remove-section (begin)
992 (goto-char begin)
993 (search-forward generate-autoload-section-trailer)
994 (delete-region begin (point)))
996 ;;;###autoload
997 (defun update-directory-autoloads (&rest dirs)
998 "Update autoload definitions for Lisp files in the directories DIRS.
999 In an interactive call, you must give one argument, the name of a
1000 single directory. In a call from Lisp, you can supply multiple
1001 directories as separate arguments, but this usage is discouraged.
1003 The function does NOT recursively descend into subdirectories of the
1004 directory or directories specified.
1006 In an interactive call, prompt for a default output file for the
1007 autoload definitions, and temporarily bind the variable
1008 `generated-autoload-file' to this value. When called from Lisp,
1009 use the existing value of `generated-autoload-file'. If any Lisp
1010 file binds `generated-autoload-file' as a file-local variable,
1011 write its autoloads into the specified file instead."
1012 (interactive "DUpdate autoloads from directory: ")
1013 (let* ((files-re (let ((tmp nil))
1014 (dolist (suf (get-load-suffixes))
1015 (unless (string-match "\\.elc" suf) (push suf tmp)))
1016 (concat "^[^=.].*" (regexp-opt tmp t) "\\'")))
1017 (files (apply #'nconc
1018 (mapcar (lambda (dir)
1019 (directory-files (expand-file-name dir)
1020 t files-re))
1021 dirs)))
1022 (done ()) ;Files processed; to remove duplicates.
1023 (changed nil) ;Non-nil if some change occurred.
1024 (last-time)
1025 ;; Files with no autoload cookies or whose autoloads go to other
1026 ;; files because of file-local autoload-generated-file settings.
1027 (no-autoloads nil)
1028 (autoload-modified-buffers nil)
1029 (generated-autoload-file
1030 (if (called-interactively-p 'interactive)
1031 (read-file-name "Write autoload definitions to file: ")
1032 generated-autoload-file))
1033 (output-time
1034 (if (file-exists-p generated-autoload-file)
1035 (nth 5 (file-attributes generated-autoload-file)))))
1037 (with-current-buffer (autoload-find-generated-file)
1038 (save-excursion
1039 ;; Canonicalize file names and remove the autoload file itself.
1040 (setq files (delete (file-relative-name buffer-file-name)
1041 (mapcar #'file-relative-name files)))
1043 (goto-char (point-min))
1044 (while (search-forward generate-autoload-section-header nil t)
1045 (let* ((form (autoload-read-section-header))
1046 (file (nth 3 form)))
1047 (cond ((and (consp file) (stringp (car file)))
1048 ;; This is a list of files that have no autoload cookies.
1049 ;; There shouldn't be more than one such entry.
1050 ;; Remove the obsolete section.
1051 (autoload-remove-section (match-beginning 0))
1052 (setq last-time (nth 4 form))
1053 (if (member last-time (list t autoload--non-timestamp))
1054 (setq last-time output-time))
1055 (dolist (file file)
1056 (let ((file-time (nth 5 (file-attributes file))))
1057 (when (and file-time
1058 (not (time-less-p last-time file-time)))
1059 ;; file unchanged
1060 (push file no-autoloads)
1061 (setq files (delete file files))))))
1062 ((not (stringp file)))
1063 ((or (not (file-exists-p file))
1064 ;; Remove duplicates as well, just in case.
1065 (member file done))
1066 ;; Remove the obsolete section.
1067 (setq changed t)
1068 (autoload-remove-section (match-beginning 0)))
1069 ((not (time-less-p (let ((oldtime (nth 4 form)))
1070 (if (member oldtime
1071 (list
1072 t autoload--non-timestamp))
1073 output-time
1074 oldtime))
1075 (nth 5 (file-attributes file))))
1076 ;; File hasn't changed.
1077 nil)
1079 (setq changed t)
1080 (autoload-remove-section (match-beginning 0))
1081 (if (autoload-generate-file-autoloads
1082 ;; Passing `current-buffer' makes it insert at point.
1083 file (current-buffer) buffer-file-name)
1084 (push file no-autoloads))))
1085 (push file done)
1086 (setq files (delete file files)))))
1087 ;; Elements remaining in FILES have no existing autoload sections yet.
1088 (let ((no-autoloads-time (or last-time '(0 0 0 0))) file-time)
1089 (dolist (file files)
1090 (cond
1091 ;; Passing nil as second argument forces
1092 ;; autoload-generate-file-autoloads to look for the right
1093 ;; spot where to insert each autoloads section.
1094 ((setq file-time
1095 (autoload-generate-file-autoloads file nil buffer-file-name))
1096 (push file no-autoloads)
1097 (if (time-less-p no-autoloads-time file-time)
1098 (setq no-autoloads-time file-time)))
1099 (t (setq changed t))))
1101 (when no-autoloads
1102 ;; Sort them for better readability.
1103 (setq no-autoloads (sort no-autoloads 'string<))
1104 ;; Add the `no-autoloads' section.
1105 (goto-char (point-max))
1106 (search-backward "\f" nil t)
1107 (autoload-insert-section-header
1108 (current-buffer) nil nil no-autoloads (if autoload-timestamps
1109 no-autoloads-time
1110 autoload--non-timestamp))
1111 (insert generate-autoload-section-trailer)))
1113 ;; Don't modify the file if its content has not been changed, so `make'
1114 ;; dependencies don't trigger unnecessarily.
1115 (when changed
1116 (let ((version-control 'never))
1117 (save-buffer)))
1119 ;; In case autoload entries were added to other files because of
1120 ;; file-local autoload-generated-file settings.
1121 (autoload-save-buffers))))
1123 (define-obsolete-function-alias 'update-autoloads-from-directories
1124 'update-directory-autoloads "22.1")
1126 ;;;###autoload
1127 (defun batch-update-autoloads ()
1128 "Update loaddefs.el autoloads in batch mode.
1129 Calls `update-directory-autoloads' on the command line arguments.
1130 Definitions are written to `generated-autoload-file' (which
1131 should be non-nil)."
1132 ;; For use during the Emacs build process only.
1133 ;; Exclude those files that are preloaded on ALL platforms.
1134 ;; These are the ones in loadup.el where "(load" is at the start
1135 ;; of the line (crude, but it works).
1136 (unless autoload-excludes
1137 (let ((default-directory (file-name-directory generated-autoload-file))
1138 file)
1139 (when (file-readable-p "loadup.el")
1140 (with-temp-buffer
1141 (insert-file-contents "loadup.el")
1142 (while (re-search-forward "^(load \"\\([^\"]+\\)\"" nil t)
1143 (setq file (match-string 1))
1144 (or (string-match "\\.el\\'" file)
1145 (setq file (format "%s.el" file)))
1146 (or (string-match "\\`site-" file)
1147 (push (expand-file-name file) autoload-excludes)))))))
1148 (let ((args command-line-args-left))
1149 (setq command-line-args-left nil)
1150 (apply #'update-directory-autoloads args)))
1152 (provide 'autoload)
1154 ;;; autoload.el ends here