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