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