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