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