]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/autoload.el
e688d6be7253fa09c275ead531e8b2d96b5926d8
[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 (defvar autoload-modified-buffers) ;Dynamically scoped var.
91
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)))
129
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)))))
139
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.
149
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))))
185
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)))
193
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)))))
200 )
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))))))
207
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))))))
216
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)
221
222 ;; nil here indicates that this is not a special autoload form.
223 (t nil))))
224
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.
229
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))))))
240
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))))
248
249
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))))))
270
271 (defvar autoload-print-form-outbuf nil
272 "Buffer which gets the output of `autoload-print-form'.")
273
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)
283 (t
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)))))))
322
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.
329
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")))
352
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)
359
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)
375
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))))))
393
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)))
410
411 (defvar no-update-autoloads nil
412 "File local variable to prevent scanning this file for autoload cookies.")
413
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)))
441
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))))
452
453 (defvar print-readably)
454
455
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.
462 nil
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))))
472
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
484 nil
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)))
492
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)))))))
503
504 (defvar autoload-builtin-package-versions nil)
505
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'.
519
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.
523
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")))))
580
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))
594 (t
595 (forward-sexp 1)
596 (forward-line 1))))))
597
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.
646 ;;
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)))
650 ))
651 \f
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)))))
657
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.
662
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'.
668
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)))
681
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 LOAD-NAME is the name as it appears in the output.
686 Returns a buffer whose point is placed at the requested location.
687 Returns nil if the file's autoloads are up-to-date, otherwise
688 removes any prior now out-of-date autoload entries."
689 (catch 'up-to-date
690 (let* ((buf (current-buffer))
691 (existing-buffer (if buffer-file-name buf))
692 (found nil))
693 (with-current-buffer (autoload-find-generated-file)
694 ;; This is to make generated-autoload-file have Unix EOLs, so
695 ;; that it is portable to all platforms.
696 (or (eq 0 (coding-system-eol-type buffer-file-coding-system))
697 (set-buffer-file-coding-system 'unix))
698 (or (> (buffer-size) 0)
699 (error "Autoloads file %s lacks boilerplate" buffer-file-name))
700 (or (file-writable-p buffer-file-name)
701 (error "Autoloads file %s is not writable" buffer-file-name))
702 (widen)
703 (goto-char (point-min))
704 ;; Look for the section for LOAD-NAME.
705 (while (and (not found)
706 (search-forward generate-autoload-section-header nil t))
707 (let ((form (autoload-read-section-header)))
708 (cond ((string= (nth 2 form) load-name)
709 ;; We found the section for this file.
710 ;; Check if it is up to date.
711 (let ((begin (match-beginning 0))
712 (last-time (nth 4 form))
713 (file-time (nth 5 (file-attributes file))))
714 (if (and (or (null existing-buffer)
715 (not (buffer-modified-p existing-buffer)))
716 (or
717 ;; last-time is the time-stamp (specifying
718 ;; the last time we looked at the file) and
719 ;; the file hasn't been changed since.
720 (and (listp last-time)
721 (not (time-less-p last-time file-time)))
722 ;; last-time is an MD5 checksum instead.
723 (and (stringp last-time)
724 (equal last-time
725 (md5 buf nil nil 'emacs-mule)))))
726 (throw 'up-to-date nil)
727 (autoload-remove-section begin)
728 (setq found t))))
729 ((string< load-name (nth 2 form))
730 ;; We've come to a section alphabetically later than
731 ;; LOAD-NAME. We assume the file is in order and so
732 ;; there must be no section for LOAD-NAME. We will
733 ;; insert one before the section here.
734 (goto-char (match-beginning 0))
735 (setq found t)))))
736 (or found
737 (progn
738 ;; No later sections in the file. Put before the last page.
739 (goto-char (point-max))
740 (search-backward "\f" nil t)))
741 (unless (memq (current-buffer) autoload-modified-buffers)
742 (push (current-buffer) autoload-modified-buffers))
743 (current-buffer)))))
744
745 (defun autoload-remove-section (begin)
746 (goto-char begin)
747 (search-forward generate-autoload-section-trailer)
748 (delete-region begin (point)))
749
750 ;;;###autoload
751 (defun update-directory-autoloads (&rest dirs)
752 "Update autoload definitions for Lisp files in the directories DIRS.
753 In an interactive call, you must give one argument, the name of a
754 single directory. In a call from Lisp, you can supply multiple
755 directories as separate arguments, but this usage is discouraged.
756
757 The function does NOT recursively descend into subdirectories of the
758 directory or directories specified.
759
760 In an interactive call, prompt for a default output file for the
761 autoload definitions, and temporarily bind the variable
762 `generated-autoload-file' to this value. When called from Lisp,
763 use the existing value of `generated-autoload-file'. If any Lisp
764 file binds `generated-autoload-file' as a file-local variable,
765 write its autoloads into the specified file instead."
766 (interactive "DUpdate autoloads from directory: ")
767 (let* ((files-re (let ((tmp nil))
768 (dolist (suf (get-load-suffixes))
769 (unless (string-match "\\.elc" suf) (push suf tmp)))
770 (concat "^[^=.].*" (regexp-opt tmp t) "\\'")))
771 (files (apply 'nconc
772 (mapcar (lambda (dir)
773 (directory-files (expand-file-name dir)
774 t files-re))
775 dirs)))
776 (done ())
777 (last-time)
778 ;; Files with no autoload cookies or whose autoloads go to other
779 ;; files because of file-local autoload-generated-file settings.
780 (no-autoloads nil)
781 (autoload-modified-buffers nil)
782 (generated-autoload-file
783 (if (called-interactively-p 'interactive)
784 (read-file-name "Write autoload definitions to file: ")
785 generated-autoload-file)))
786
787 (with-current-buffer (autoload-find-generated-file)
788 (save-excursion
789 ;; Canonicalize file names and remove the autoload file itself.
790 (setq files (delete (file-relative-name buffer-file-name)
791 (mapcar 'file-relative-name files)))
792
793 (goto-char (point-min))
794 (while (search-forward generate-autoload-section-header nil t)
795 (let* ((form (autoload-read-section-header))
796 (file (nth 3 form)))
797 (cond ((and (consp file) (stringp (car file)))
798 ;; This is a list of files that have no autoload cookies.
799 ;; There shouldn't be more than one such entry.
800 ;; Remove the obsolete section.
801 (autoload-remove-section (match-beginning 0))
802 (setq last-time (nth 4 form))
803 (dolist (file file)
804 (let ((file-time (nth 5 (file-attributes file))))
805 (when (and file-time
806 (not (time-less-p last-time file-time)))
807 ;; file unchanged
808 (push file no-autoloads)
809 (setq files (delete file files))))))
810 ((not (stringp file)))
811 ((or (not (file-exists-p file))
812 ;; Remove duplicates as well, just in case.
813 (member file done)
814 ;; If the file is actually excluded.
815 (member (expand-file-name file) autoload-excludes))
816 ;; Remove the obsolete section.
817 (autoload-remove-section (match-beginning 0)))
818 ((not (time-less-p (nth 4 form)
819 (nth 5 (file-attributes file))))
820 ;; File hasn't changed.
821 nil)
822 (t
823 (autoload-remove-section (match-beginning 0))
824 (if (autoload-generate-file-autoloads
825 ;; Passing `current-buffer' makes it insert at point.
826 file (current-buffer) buffer-file-name)
827 (push file no-autoloads))))
828 (push file done)
829 (setq files (delete file files)))))
830 ;; Elements remaining in FILES have no existing autoload sections yet.
831 (let ((no-autoloads-time (or last-time '(0 0 0 0))) file-time)
832 (dolist (file files)
833 (cond
834 ((member (expand-file-name file) autoload-excludes) nil)
835 ;; Passing nil as second argument forces
836 ;; autoload-generate-file-autoloads to look for the right
837 ;; spot where to insert each autoloads section.
838 ((setq file-time
839 (autoload-generate-file-autoloads file nil buffer-file-name))
840 (push file no-autoloads)
841 (if (time-less-p no-autoloads-time file-time)
842 (setq no-autoloads-time file-time)))))
843
844 (when no-autoloads
845 ;; Sort them for better readability.
846 (setq no-autoloads (sort no-autoloads 'string<))
847 ;; Add the `no-autoloads' section.
848 (goto-char (point-max))
849 (search-backward "\f" nil t)
850 (autoload-insert-section-header
851 (current-buffer) nil nil no-autoloads no-autoloads-time)
852 (insert generate-autoload-section-trailer)))
853
854 (let ((version-control 'never))
855 (save-buffer))
856 ;; In case autoload entries were added to other files because of
857 ;; file-local autoload-generated-file settings.
858 (autoload-save-buffers))))
859
860 (define-obsolete-function-alias 'update-autoloads-from-directories
861 'update-directory-autoloads "22.1")
862
863 ;;;###autoload
864 (defun batch-update-autoloads ()
865 "Update loaddefs.el autoloads in batch mode.
866 Calls `update-directory-autoloads' on the command line arguments.
867 Definitions are written to `generated-autoload-file' (which
868 should be non-nil)."
869 ;; For use during the Emacs build process only.
870 ;; Exclude those files that are preloaded on ALL platforms.
871 ;; These are the ones in loadup.el where "(load" is at the start
872 ;; of the line (crude, but it works).
873 (unless autoload-excludes
874 (let ((default-directory (file-name-directory generated-autoload-file))
875 file)
876 (when (file-readable-p "loadup.el")
877 (with-temp-buffer
878 (insert-file-contents "loadup.el")
879 (while (re-search-forward "^(load \"\\([^\"]+\\)\"" nil t)
880 (setq file (match-string 1))
881 (or (string-match "\\.el\\'" file)
882 (setq file (format "%s.el" file)))
883 (or (string-match "\\`site-" file)
884 (push (expand-file-name file) autoload-excludes)))))))
885 (let ((args command-line-args-left))
886 (setq command-line-args-left nil)
887 (apply 'update-directory-autoloads args)))
888
889 (provide 'autoload)
890
891 ;;; autoload.el ends here