]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/autoload.el
* lisp/subr.el (definition-prefixes): Expand docstring
[gnu-emacs] / lisp / emacs-lisp / autoload.el
1 ;; autoload.el --- maintain autoloads in loaddefs.el -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1991-1997, 2001-2016 Free Software Foundation, Inc.
4
5 ;; Author: Roland McGrath <roland@gnu.org>
6 ;; Keywords: maint
7 ;; Package: emacs
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25
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.
30
31 ;;; Code:
32
33 (require 'lisp-mode) ;for `doc-string-elt' properties.
34 (require 'lisp-mnt)
35 (eval-when-compile (require 'cl-lib))
36
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.
41
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'.
47
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)
52
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)
61
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).
68
69 This string is used:
70
71 \;;;###autoload
72 \(defun function-to-be-autoloaded () ...)
73
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'.")
77
78 (defvar autoload-excludes nil
79 "If non-nil, list of absolute file names not to scan for autoloads.")
80
81 (defconst generate-autoload-section-header "\f\n;;;### "
82 "String that marks the form at the start of a new file's autoload section.")
83
84 (defconst generate-autoload-section-trailer "\n;;;***\n"
85 "String which indicates the end of the section of autoloads for a file.")
86
87 (defconst generate-autoload-section-continuation ";;;;;; "
88 "String to add on each continuation of the section header form.")
89
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.")
95
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.")
112
113 (defvar autoload-modified-buffers) ;Dynamically scoped var.
114
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)))
152
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)))))
162
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.
172
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))
208 t)
209 (eq (car-safe (car body)) 'interactive))
210 ,(if macrop ''macro nil))))
211
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)))
219
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)))))
226 )
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))))))
233
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))))))
242
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)
247
248 ;; nil here indicates that this is not a special autoload form.
249 (t nil))))
250
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.
255
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)))))
279
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))))
287
288
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))))))
309
310 (defvar autoload-print-form-outbuf nil
311 "Buffer which gets the output of `autoload-print-form'.")
312
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)
322 (t
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)))))))
361
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.
368
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")))
391
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)
398
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)
409
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))
415
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))))))
443
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)))
460
461 (defvar no-update-autoloads nil
462 "File local variable to prevent scanning this file for autoload cookies.")
463
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)))
491
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))))
502
503 (defun autoload--split-prefixes-1 (strs)
504 (let ((prefixes ()))
505 (dolist (str strs)
506 (string-match "\\`[^-:/_]*[-:/_]*" str)
507 (let* ((prefix (match-string 0 str))
508 (tail (substring str (match-end 0)))
509 (cell (assoc prefix prefixes)))
510 (cond
511 ((null cell) (push (list prefix tail) prefixes))
512 ((equal (cadr cell) tail) nil)
513 (t (setcdr cell (cons tail (cdr cell)))))))
514 prefixes))
515
516 (defvar autoload-compute-prefixes t
517 "If non-nil, autoload will add code to register the prefixes used in a file.
518 Standard prefixes won't be registered anyway. I.e. if a file \"foo.el\" defines
519 variables or functions that use \"foo-\" as prefix, that will not be registered.
520 But all other prefixes will be included.")
521
522 (defconst autoload-defs-autoload-max-size 5
523 "Target length of the list of definition prefixes per file.
524 If set too small, the prefixes will be too generic (i.e. they'll use little
525 memory, we'll end up looking in too many files when we need a particular
526 prefix), and if set too large, they will be too specific (i.e. they will
527 cost more memory use).")
528
529 (defvar autoload-popular-prefixes nil)
530
531 (defun autoload--make-defs-autoload (defs file)
532 ;; FIXME: avoid redundant entries. E.g. opascal currently has
533 ;; "opascal-" "opascal--literal-start-re" "opascal--syntax-propertize"
534 ;; where only the first one should be kept.
535 ;; FIXME: Avoid keeping too-long-prefixes. E.g. ob-scheme currently has
536 ;; "org-babel-scheme-" "org-babel-default-header-args:scheme"
537 ;; "org-babel-expand-body:scheme" "org-babel-execute:scheme".
538
539 ;; Remove the defs that obey the rule that file foo.el (or
540 ;; foo-mode.el) uses "foo-" as prefix.
541 ;; FIXME: help--symbol-completion-table still doesn't know how to use
542 ;; the rule that file foo.el (or foo-mode.el) uses "foo-" as prefix.
543 ;;(let ((prefix
544 ;; (concat (substring file 0 (string-match "-mode\\'" file)) "-")))
545 ;; (dolist (def (prog1 defs (setq defs nil)))
546 ;; (unless (string-prefix-p prefix def)
547 ;; (push def defs))))
548
549 ;; Then compute a small set of prefixes that cover all the
550 ;; remaining definitions.
551 (let ((prefixes (autoload--split-prefixes-1 defs))
552 (again t))
553 ;; (message "Initial prefixes %s : %S" file (mapcar #'car prefixes))
554 (while again
555 (setq again nil)
556 (let ((newprefixes
557 (sort
558 (mapcar (lambda (cell)
559 (cons cell
560 (autoload--split-prefixes-1 (cdr cell))))
561 prefixes)
562 (lambda (x y) (< (length (cdr x)) (length (cdr y)))))))
563 (setq prefixes nil)
564 (while newprefixes
565 (let ((x (pop newprefixes)))
566 (if (or (equal '("") (cdar x))
567 (and (cddr x)
568 (not (member (caar x)
569 autoload-popular-prefixes))
570 (> (+ (length prefixes) (length newprefixes)
571 (length (cdr x)))
572 autoload-defs-autoload-max-size)))
573 ;; Nothing to split or would split too deep.
574 (push (car x) prefixes)
575 ;; (message "Expand %S to %S" (caar x) (cdr x))
576 (setq again t)
577 (setq prefixes
578 (nconc (mapcar (lambda (cell)
579 (cons (concat (caar x)
580 (car cell))
581 (cdr cell)))
582 (cdr x))
583 prefixes)))))))
584 ;; (message "Final prefixes %s : %S" file (mapcar #'car prefixes))
585 (when prefixes
586 `(if (fboundp 'register-definition-prefixes)
587 (register-definition-prefixes ,file ',(mapcar #'car prefixes))))))
588
589 (defun autoload--setup-output (otherbuf outbuf absfile load-name)
590 (let ((outbuf
591 (or (if otherbuf
592 ;; A file-local setting of
593 ;; autoload-generated-file says we
594 ;; should ignore OUTBUF.
595 nil
596 outbuf)
597 (autoload-find-destination absfile load-name)
598 ;; The file has autoload cookies, but they're
599 ;; already up-to-date. If OUTFILE is nil, the
600 ;; entries are in the expected OUTBUF,
601 ;; otherwise they're elsewhere.
602 (throw 'done otherbuf))))
603 (with-current-buffer outbuf
604 (point-marker))))
605
606 (defun autoload--print-cookie-text (output-start load-name file)
607 (let ((standard-output (marker-buffer output-start)))
608 (search-forward generate-autoload-cookie)
609 (skip-chars-forward " \t")
610 (if (eolp)
611 (condition-case-unless-debug err
612 ;; Read the next form and make an autoload.
613 (let* ((form (prog1 (read (current-buffer))
614 (or (bolp) (forward-line 1))))
615 (autoload (make-autoload form load-name)))
616 (if autoload
617 nil
618 (setq autoload form))
619 (let ((autoload-print-form-outbuf
620 standard-output))
621 (autoload-print-form autoload)))
622 (error
623 (message "Autoload cookie error in %s:%s %S"
624 file (count-lines (point-min) (point)) err)))
625
626 ;; Copy the rest of the line to the output.
627 (princ (buffer-substring
628 (progn
629 ;; Back up over whitespace, to preserve it.
630 (skip-chars-backward " \f\t")
631 (if (= (char-after (1+ (point))) ? )
632 ;; Eat one space.
633 (forward-char 1))
634 (point))
635 (progn (forward-line 1) (point)))))))
636
637 (defvar autoload-builtin-package-versions nil)
638
639 ;; When called from `generate-file-autoloads' we should ignore
640 ;; `generated-autoload-file' altogether. When called from
641 ;; `update-file-autoloads' we don't know `outbuf'. And when called from
642 ;; `update-directory-autoloads' it's in between: we know the default
643 ;; `outbuf' but we should obey any file-local setting of
644 ;; `generated-autoload-file'.
645 (defun autoload-generate-file-autoloads (file &optional outbuf outfile)
646 "Insert an autoload section for FILE in the appropriate buffer.
647 Autoloads are generated for defuns and defmacros in FILE
648 marked by `generate-autoload-cookie' (which see).
649 If FILE is being visited in a buffer, the contents of the buffer are used.
650 OUTBUF is the buffer in which the autoload statements should be inserted.
651 If OUTBUF is nil, it will be determined by `autoload-generated-file'.
652
653 If provided, OUTFILE is expected to be the file name of OUTBUF.
654 If OUTFILE is non-nil and FILE specifies a `generated-autoload-file'
655 different from OUTFILE, then OUTBUF is ignored.
656
657 Return non-nil if and only if FILE adds no autoloads to OUTFILE
658 \(or OUTBUF if OUTFILE is nil). The actual return value is
659 FILE's modification time."
660 ;; Include the file name in any error messages
661 (condition-case err
662 (let (load-name
663 (print-length nil)
664 (print-level nil)
665 (float-output-format nil)
666 (visited (get-file-buffer file))
667 (otherbuf nil)
668 (absfile (expand-file-name file))
669 (defs '())
670 ;; nil until we found a cookie.
671 output-start)
672 (when
673 (catch 'done
674 (with-current-buffer (or visited
675 ;; It is faster to avoid visiting the file.
676 (autoload-find-file file))
677 ;; Obey the no-update-autoloads file local variable.
678 (unless no-update-autoloads
679 (or noninteractive (message "Generating autoloads for %s..." file))
680 (setq load-name
681 (if (stringp generated-autoload-load-name)
682 generated-autoload-load-name
683 (autoload-file-load-name absfile)))
684 ;; FIXME? Comparing file-names for equality with just equal
685 ;; is fragile, eg if one has an automounter prefix and one
686 ;; does not, but both refer to the same physical file.
687 (when (and outfile
688 (not
689 (if (memq system-type '(ms-dos windows-nt))
690 (equal (downcase outfile)
691 (downcase (autoload-generated-file)))
692 (equal outfile (autoload-generated-file)))))
693 (setq otherbuf t))
694 (save-excursion
695 (save-restriction
696 (widen)
697 (when autoload-builtin-package-versions
698 (let ((version (lm-header "version"))
699 package)
700 (and version
701 (setq version (ignore-errors (version-to-list version)))
702 (setq package (or (lm-header "package")
703 (file-name-sans-extension
704 (file-name-nondirectory file))))
705 (setq output-start (autoload--setup-output
706 otherbuf outbuf absfile load-name))
707 (let ((standard-output (marker-buffer output-start))
708 (print-quoted t))
709 (princ `(push (purecopy
710 ',(cons (intern package) version))
711 package--builtin-versions))
712 (princ "\n")))))
713
714 (goto-char (point-min))
715 (while (not (eobp))
716 (skip-chars-forward " \t\n\f")
717 (cond
718 ((looking-at (regexp-quote generate-autoload-cookie))
719 ;; If not done yet, figure out where to insert this text.
720 (unless output-start
721 (setq output-start (autoload--setup-output
722 otherbuf outbuf absfile load-name)))
723 (autoload--print-cookie-text output-start load-name file))
724 ((looking-at ";")
725 ;; Don't read the comment.
726 (forward-line 1))
727 (t
728 ;; Avoid (defvar <foo>) by requiring a trailing space.
729 ;; Also, ignore this prefix business
730 ;; for ;;;###tramp-autoload and friends.
731 (when (and (equal generate-autoload-cookie ";;;###autoload")
732 (looking-at "(\\(def[^ ]+\\) ['(]*\\([^' ()\"\n]+\\)[\n \t]")
733 (not (member
734 (match-string 1)
735 '("define-obsolete-function-alias"
736 "define-obsolete-variable-alias"
737 "define-category" "define-key"
738 "defgroup" "defface" "defadvice"
739 ;; Hmm... this is getting ugly:
740 "define-widget"
741 "defun-rcirc-command"))))
742 (push (match-string 2) defs))
743 (forward-sexp 1)
744 (forward-line 1))))))
745
746 (when (and autoload-compute-prefixes defs)
747 ;; This output needs to always go in the main loaddefs.el,
748 ;; regardless of generated-autoload-file.
749 ;; FIXME: the files that don't have autoload cookies but
750 ;; do have definitions end up listed twice in loaddefs.el:
751 ;; once for their register-definition-prefixes and once in
752 ;; the list of "files without any autoloads".
753 (let ((form (autoload--make-defs-autoload defs load-name)))
754 (cond
755 ((null form)) ;All defs obey the default rule, yay!
756 ((not otherbuf)
757 (unless output-start
758 (setq output-start (autoload--setup-output
759 nil outbuf absfile load-name)))
760 (let ((autoload-print-form-outbuf
761 (marker-buffer output-start)))
762 (autoload-print-form form)))
763 (t
764 (let* ((other-output-start
765 ;; To force the output to go to the main loaddefs.el
766 ;; rather than to generated-autoload-file,
767 ;; there are two cases: if outbuf is non-nil,
768 ;; then passing otherbuf=nil is enough, but if
769 ;; outbuf is nil, that won't cut it, so we
770 ;; locally bind generated-autoload-file.
771 (let ((generated-autoload-file
772 (default-value 'generated-autoload-file)))
773 (autoload--setup-output nil outbuf absfile load-name)))
774 (autoload-print-form-outbuf
775 (marker-buffer other-output-start)))
776 (autoload-print-form form)
777 (with-current-buffer (marker-buffer other-output-start)
778 (save-excursion
779 ;; Insert the section-header line which lists
780 ;; the file name and which functions are in it, etc.
781 (goto-char other-output-start)
782 (let ((relfile (file-relative-name absfile)))
783 (autoload-insert-section-header
784 (marker-buffer other-output-start)
785 "actual autoloads are elsewhere" load-name relfile
786 (nth 5 (file-attributes absfile)))
787 (insert ";;; Generated autoloads from " relfile "\n")))
788 (insert generate-autoload-section-trailer)))))))
789
790 (when output-start
791 (let ((secondary-autoloads-file-buf
792 (if otherbuf (current-buffer))))
793 (with-current-buffer (marker-buffer output-start)
794 (cl-assert (> (point) output-start))
795 (save-excursion
796 ;; Insert the section-header line which lists the file name
797 ;; and which functions are in it, etc.
798 (goto-char output-start)
799 (let ((relfile (file-relative-name absfile)))
800 (autoload-insert-section-header
801 (marker-buffer output-start)
802 () load-name relfile
803 (if secondary-autoloads-file-buf
804 ;; MD5 checksums are much better because they do not
805 ;; change unless the file changes (so they'll be
806 ;; equal on two different systems and will change
807 ;; less often than time-stamps, thus leading to fewer
808 ;; unneeded changes causing spurious conflicts), but
809 ;; using time-stamps is a very useful optimization,
810 ;; so we use time-stamps for the main autoloads file
811 ;; (loaddefs.el) where we have special ways to
812 ;; circumvent the "random change problem", and MD5
813 ;; checksum in secondary autoload files where we do
814 ;; not need the time-stamp optimization because it is
815 ;; already provided by the primary autoloads file.
816 (md5 secondary-autoloads-file-buf
817 ;; We'd really want to just use
818 ;; `emacs-internal' instead.
819 nil nil 'emacs-mule-unix)
820 (if autoload-timestamps
821 (nth 5 (file-attributes relfile))
822 autoload--non-timestamp)))
823 (insert ";;; Generated autoloads from " relfile "\n")))
824 (insert generate-autoload-section-trailer))))
825 (or noninteractive
826 (message "Generating autoloads for %s...done" file)))
827 (or visited
828 ;; We created this buffer, so we should kill it.
829 (kill-buffer (current-buffer))))
830 (or (not output-start)
831 ;; If the entries were added to some other buffer, then the file
832 ;; doesn't add entries to OUTFILE.
833 otherbuf))
834 (nth 5 (file-attributes absfile))))
835 (error
836 ;; Probably unbalanced parens in forward-sexp. In that case, the
837 ;; condition is scan-error, and the signal data includes point
838 ;; where the error was found; we'd like to convert that to
839 ;; line:col, but line-number-at-pos gets the wrong line in batch
840 ;; mode for some reason.
841 ;;
842 ;; At least this gets the file name in the error message; the
843 ;; developer can use goto-char to get to the error position.
844 (error "%s:0:0: error: %s: %s" file (car err) (cdr err)))
845 ))
846 \f
847 (defun autoload-save-buffers ()
848 (while autoload-modified-buffers
849 (with-current-buffer (pop autoload-modified-buffers)
850 (let ((version-control 'never))
851 (save-buffer)))))
852
853 ;; FIXME This command should be deprecated.
854 ;; See http://debbugs.gnu.org/22213#41
855 ;;;###autoload
856 (defun update-file-autoloads (file &optional save-after outfile)
857 "Update the autoloads for FILE.
858 If prefix arg SAVE-AFTER is non-nil, save the buffer too.
859
860 If FILE binds `generated-autoload-file' as a file-local variable,
861 autoloads are written into that file. Otherwise, the autoloads
862 file is determined by OUTFILE. If called interactively, prompt
863 for OUTFILE; if called from Lisp with OUTFILE nil, use the
864 existing value of `generated-autoload-file'.
865
866 Return FILE if there was no autoload cookie in it, else nil."
867 (interactive (list (read-file-name "Update autoloads for file: ")
868 current-prefix-arg
869 (read-file-name "Write autoload definitions to file: ")))
870 (let* ((generated-autoload-file (or outfile generated-autoload-file))
871 (autoload-modified-buffers nil)
872 ;; We need this only if the output file handles more than one input.
873 ;; See http://debbugs.gnu.org/22213#38 and subsequent.
874 (autoload-timestamps t)
875 (no-autoloads (autoload-generate-file-autoloads file)))
876 (if autoload-modified-buffers
877 (if save-after (autoload-save-buffers))
878 (if (called-interactively-p 'interactive)
879 (message "Autoload section for %s is up to date." file)))
880 (if no-autoloads file)))
881
882 (defun autoload-find-destination (file load-name)
883 "Find the destination point of the current buffer's autoloads.
884 FILE is the file name of the current buffer.
885 LOAD-NAME is the name as it appears in the output.
886 Returns a buffer whose point is placed at the requested location.
887 Returns nil if the file's autoloads are up-to-date, otherwise
888 removes any prior now out-of-date autoload entries."
889 (catch 'up-to-date
890 (let* ((buf (current-buffer))
891 (existing-buffer (if buffer-file-name buf))
892 (output-file (autoload-generated-file))
893 (output-time (if (file-exists-p output-file)
894 (nth 5 (file-attributes output-file))))
895 (found nil))
896 (with-current-buffer (autoload-find-generated-file)
897 ;; This is to make generated-autoload-file have Unix EOLs, so
898 ;; that it is portable to all platforms.
899 (or (eq 0 (coding-system-eol-type buffer-file-coding-system))
900 (set-buffer-file-coding-system 'unix))
901 (or (> (buffer-size) 0)
902 (error "Autoloads file %s lacks boilerplate" buffer-file-name))
903 (or (file-writable-p buffer-file-name)
904 (error "Autoloads file %s is not writable" buffer-file-name))
905 (widen)
906 (goto-char (point-min))
907 ;; Look for the section for LOAD-NAME.
908 (while (and (not found)
909 (search-forward generate-autoload-section-header nil t))
910 (let ((form (autoload-read-section-header)))
911 (cond ((string= (nth 2 form) load-name)
912 ;; We found the section for this file.
913 ;; Check if it is up to date.
914 (let ((begin (match-beginning 0))
915 (last-time (nth 4 form))
916 (file-time (nth 5 (file-attributes file))))
917 (if (and (or (null existing-buffer)
918 (not (buffer-modified-p existing-buffer)))
919 (cond
920 ;; FIXME? Arguably we should throw a
921 ;; user error, or some kind of warning,
922 ;; if we were called from update-file-autoloads,
923 ;; which can update only a single input file.
924 ;; It's not appropriate to use the output
925 ;; file modtime in such a case,
926 ;; if there are multiple input files
927 ;; contributing to the output.
928 ((and output-time
929 (member last-time
930 (list t autoload--non-timestamp)))
931 (not (time-less-p output-time file-time)))
932 ;; last-time is the time-stamp (specifying
933 ;; the last time we looked at the file) and
934 ;; the file hasn't been changed since.
935 ((listp last-time)
936 (not (time-less-p last-time file-time)))
937 ;; last-time is an MD5 checksum instead.
938 ((stringp last-time)
939 (equal last-time
940 (md5 buf nil nil 'emacs-mule)))))
941 (throw 'up-to-date nil)
942 (autoload-remove-section begin)
943 (setq found t))))
944 ((string< load-name (nth 2 form))
945 ;; We've come to a section alphabetically later than
946 ;; LOAD-NAME. We assume the file is in order and so
947 ;; there must be no section for LOAD-NAME. We will
948 ;; insert one before the section here.
949 (goto-char (match-beginning 0))
950 (setq found t)))))
951 (or found
952 (progn
953 ;; No later sections in the file. Put before the last page.
954 (goto-char (point-max))
955 (search-backward "\f" nil t)))
956 (unless (memq (current-buffer) autoload-modified-buffers)
957 (push (current-buffer) autoload-modified-buffers))
958 (current-buffer)))))
959
960 (defun autoload-remove-section (begin)
961 (goto-char begin)
962 (search-forward generate-autoload-section-trailer)
963 (delete-region begin (point)))
964
965 ;;;###autoload
966 (defun update-directory-autoloads (&rest dirs)
967 "Update autoload definitions for Lisp files in the directories DIRS.
968 In an interactive call, you must give one argument, the name of a
969 single directory. In a call from Lisp, you can supply multiple
970 directories as separate arguments, but this usage is discouraged.
971
972 The function does NOT recursively descend into subdirectories of the
973 directory or directories specified.
974
975 In an interactive call, prompt for a default output file for the
976 autoload definitions, and temporarily bind the variable
977 `generated-autoload-file' to this value. When called from Lisp,
978 use the existing value of `generated-autoload-file'. If any Lisp
979 file binds `generated-autoload-file' as a file-local variable,
980 write its autoloads into the specified file instead."
981 (interactive "DUpdate autoloads from directory: ")
982 (let* ((files-re (let ((tmp nil))
983 (dolist (suf (get-load-suffixes))
984 (unless (string-match "\\.elc" suf) (push suf tmp)))
985 (concat "^[^=.].*" (regexp-opt tmp t) "\\'")))
986 (files (apply #'nconc
987 (mapcar (lambda (dir)
988 (directory-files (expand-file-name dir)
989 t files-re))
990 dirs)))
991 (done ()) ;Files processed; to remove duplicates.
992 (changed nil) ;Non-nil if some change occured.
993 (last-time)
994 ;; Files with no autoload cookies or whose autoloads go to other
995 ;; files because of file-local autoload-generated-file settings.
996 (no-autoloads nil)
997 (autoload-modified-buffers nil)
998 (generated-autoload-file
999 (if (called-interactively-p 'interactive)
1000 (read-file-name "Write autoload definitions to file: ")
1001 generated-autoload-file))
1002 (output-time
1003 (if (file-exists-p generated-autoload-file)
1004 (nth 5 (file-attributes generated-autoload-file)))))
1005
1006 (with-current-buffer (autoload-find-generated-file)
1007 (save-excursion
1008 ;; Canonicalize file names and remove the autoload file itself.
1009 (setq files (delete (file-relative-name buffer-file-name)
1010 (mapcar #'file-relative-name files)))
1011
1012 (goto-char (point-min))
1013 (while (search-forward generate-autoload-section-header nil t)
1014 (let* ((form (autoload-read-section-header))
1015 (file (nth 3 form)))
1016 (cond ((and (consp file) (stringp (car file)))
1017 ;; This is a list of files that have no autoload cookies.
1018 ;; There shouldn't be more than one such entry.
1019 ;; Remove the obsolete section.
1020 (autoload-remove-section (match-beginning 0))
1021 (setq last-time (nth 4 form))
1022 (if (member last-time (list t autoload--non-timestamp))
1023 (setq last-time output-time))
1024 (dolist (file file)
1025 (let ((file-time (nth 5 (file-attributes file))))
1026 (when (and file-time
1027 (not (time-less-p last-time file-time)))
1028 ;; file unchanged
1029 (push file no-autoloads)
1030 (setq files (delete file files))))))
1031 ((not (stringp file)))
1032 ((or (not (file-exists-p file))
1033 ;; Remove duplicates as well, just in case.
1034 (member file done)
1035 ;; If the file is actually excluded.
1036 (member (expand-file-name file) autoload-excludes))
1037 ;; Remove the obsolete section.
1038 (setq changed t)
1039 (autoload-remove-section (match-beginning 0)))
1040 ((not (time-less-p (let ((oldtime (nth 4 form)))
1041 (if (member oldtime
1042 (list
1043 t autoload--non-timestamp))
1044 output-time
1045 oldtime))
1046 (nth 5 (file-attributes file))))
1047 ;; File hasn't changed.
1048 nil)
1049 (t
1050 (setq changed t)
1051 (autoload-remove-section (match-beginning 0))
1052 (if (autoload-generate-file-autoloads
1053 ;; Passing `current-buffer' makes it insert at point.
1054 file (current-buffer) buffer-file-name)
1055 (push file no-autoloads))))
1056 (push file done)
1057 (setq files (delete file files)))))
1058 ;; Elements remaining in FILES have no existing autoload sections yet.
1059 (let ((no-autoloads-time (or last-time '(0 0 0 0))) file-time)
1060 (dolist (file files)
1061 (cond
1062 ((member (expand-file-name file) autoload-excludes) nil)
1063 ;; Passing nil as second argument forces
1064 ;; autoload-generate-file-autoloads to look for the right
1065 ;; spot where to insert each autoloads section.
1066 ((setq file-time
1067 (autoload-generate-file-autoloads file nil buffer-file-name))
1068 (push file no-autoloads)
1069 (if (time-less-p no-autoloads-time file-time)
1070 (setq no-autoloads-time file-time)))
1071 (t (setq changed t))))
1072
1073 (when no-autoloads
1074 ;; Sort them for better readability.
1075 (setq no-autoloads (sort no-autoloads 'string<))
1076 ;; Add the `no-autoloads' section.
1077 (goto-char (point-max))
1078 (search-backward "\f" nil t)
1079 (autoload-insert-section-header
1080 (current-buffer) nil nil no-autoloads (if autoload-timestamps
1081 no-autoloads-time
1082 autoload--non-timestamp))
1083 (insert generate-autoload-section-trailer)))
1084
1085 ;; Don't modify the file if its content has not been changed, so `make'
1086 ;; dependencies don't trigger unnecessarily.
1087 (when changed
1088 (let ((version-control 'never))
1089 (save-buffer)))
1090
1091 ;; In case autoload entries were added to other files because of
1092 ;; file-local autoload-generated-file settings.
1093 (autoload-save-buffers))))
1094
1095 (define-obsolete-function-alias 'update-autoloads-from-directories
1096 'update-directory-autoloads "22.1")
1097
1098 ;;;###autoload
1099 (defun batch-update-autoloads ()
1100 "Update loaddefs.el autoloads in batch mode.
1101 Calls `update-directory-autoloads' on the command line arguments.
1102 Definitions are written to `generated-autoload-file' (which
1103 should be non-nil)."
1104 ;; For use during the Emacs build process only.
1105 ;; Exclude those files that are preloaded on ALL platforms.
1106 ;; These are the ones in loadup.el where "(load" is at the start
1107 ;; of the line (crude, but it works).
1108 (unless autoload-excludes
1109 (let ((default-directory (file-name-directory generated-autoload-file))
1110 file)
1111 (when (file-readable-p "loadup.el")
1112 (with-temp-buffer
1113 (insert-file-contents "loadup.el")
1114 (while (re-search-forward "^(load \"\\([^\"]+\\)\"" nil t)
1115 (setq file (match-string 1))
1116 (or (string-match "\\.el\\'" file)
1117 (setq file (format "%s.el" file)))
1118 (or (string-match "\\`site-" file)
1119 (push (expand-file-name file) autoload-excludes)))))))
1120 (let ((args command-line-args-left))
1121 (setq command-line-args-left nil)
1122 (apply #'update-directory-autoloads args)))
1123
1124 (provide 'autoload)
1125
1126 ;;; autoload.el ends here