Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / emacs-lisp / autoload.el
CommitLineData
56eebc29 1;; autoload.el --- maintain autoloads in loaddefs.el
c0274f38 2
b7a45ee1 3;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2001, 2002, 2003,
8b72699e 4;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
b578f267 5
5762abec 6;; Author: Roland McGrath <roland@gnu.org>
e9571d2a 7;; Keywords: maint
e5167999 8
b578f267
EN
9;; This file is part of GNU Emacs.
10
d6cba7ae 11;; GNU Emacs is free software: you can redistribute it and/or modify
b578f267 12;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
b578f267
EN
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
d6cba7ae 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
0231f2dc 23
07b3798c 24;;; Commentary:
e41b2db1 25
00ee57f3 26;; This code helps GNU Emacs maintainers keep the loaddefs.el file up to
e41b2db1
ER
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
e5167999
ER
31;;; Code:
32
b0daab9a 33(require 'lisp-mode) ;for `doc-string-elt' properties.
890df022 34(require 'help-fns) ;for help-add-fundoc-usage.
6826a134 35(eval-when-compile (require 'cl))
b0daab9a 36
0ceb5fe0
RS
37(defvar generated-autoload-file "loaddefs.el"
38 "*File \\[update-file-autoloads] puts autoloads into.
39A `.el' file can set this in its local variables section to make its
c8aca7d1
KH
40autoloads go somewhere else. The autoload file is assumed to contain a
41trailer starting with a FormFeed character.")
a25beddb 42;;;###autoload
3b979520 43(put 'generated-autoload-file 'safe-local-variable 'stringp)
0ceb5fe0 44
3b979520
SM
45;; This feels like it should be a defconst, but MH-E sets it to
46;; ";;;###mh-autoload" for the autoloads that are to go into mh-loaddefs.el.
47(defvar generate-autoload-cookie ";;;###autoload"
0ceb5fe0
RS
48 "Magic comment indicating the following form should be autoloaded.
49Used by \\[update-file-autoloads]. This string should be
50meaningless to Lisp (e.g., a comment).
51
52This string is used:
53
3b979520 54\;;;###autoload
0ceb5fe0
RS
55\(defun function-to-be-autoloaded () ...)
56
57If this string appears alone on a line, the following form will be
58read and an autoload made for it. If there is further text on the line,
59that text will be copied verbatim to `generated-autoload-file'.")
60
61(defconst generate-autoload-section-header "\f\n;;;### "
2336b6a9 62 "String that marks the form at the start of a new file's autoload section.")
0ceb5fe0
RS
63
64(defconst generate-autoload-section-trailer "\n;;;***\n"
65 "String which indicates the end of the section of autoloads for a file.")
66
2336b6a9
KH
67(defconst generate-autoload-section-continuation ";;;;;; "
68 "String to add on each continuation of the section header form.")
69
1fad2b12
SM
70(defvar autoload-modified-buffers) ;Dynamically scoped var.
71
0231f2dc 72(defun make-autoload (form file)
ceaa3695 73 "Turn FORM into an autoload or defvar for source file FILE.
e8139c11
SM
74Returns nil if FORM is not a special autoload form (i.e. a function definition
75or macro definition or a defcustom)."
76 (let ((car (car-safe form)) expand)
77 (cond
78 ;; For complex cases, try again on the macro-expansion.
1b39b493 79 ((and (memq car '(easy-mmode-define-global-mode define-global-minor-mode
32324290 80 define-globalized-minor-mode
e8139c11
SM
81 easy-mmode-define-minor-mode define-minor-mode))
82 (setq expand (let ((load-file-name file)) (macroexpand form)))
83 (eq (car expand) 'progn)
84 (memq :autoload-end expand))
85 (let ((end (memq :autoload-end expand)))
86 ;; Cut-off anything after the :autoload-end marker.
87 (setcdr end nil)
88 (cons 'progn
89 (mapcar (lambda (form) (make-autoload form file))
90 (cdr expand)))))
91
92 ;; For special function-like operators, use the `autoload' function.
93 ((memq car '(defun define-skeleton defmacro define-derived-mode
c6a3142d
JL
94 define-compilation-mode define-generic-mode
95 easy-mmode-define-global-mode define-global-minor-mode
32324290 96 define-globalized-minor-mode
c6a3142d
JL
97 easy-mmode-define-minor-mode define-minor-mode
98 defun* defmacro*))
74312ddc 99 (let* ((macrop (memq car '(defmacro defmacro*)))
e8139c11 100 (name (nth 1 form))
6826a134
SM
101 (args (case car
102 ((defun defmacro defun* defmacro*) (nth 2 form))
103 ((define-skeleton) '(&optional str arg))
bec34fb0
TTN
104 ((define-generic-mode define-derived-mode
105 define-compilation-mode) nil)
6826a134 106 (t)))
e8139c11
SM
107 (body (nthcdr (get car 'doc-string-elt) form))
108 (doc (if (stringp (car body)) (pop body))))
890df022
SM
109 (when (listp args)
110 ;; Add the usage form at the end where describe-function-1
111 ;; can recover it.
112 (setq doc (help-add-fundoc-usage doc args)))
e8139c11
SM
113 ;; `define-generic-mode' quotes the name, so take care of that
114 (list 'autoload (if (listp name) name (list 'quote name)) file doc
115 (or (and (memq car '(define-skeleton define-derived-mode
116 define-generic-mode
117 easy-mmode-define-global-mode
c6a3142d 118 define-global-minor-mode
32324290 119 define-globalized-minor-mode
e8139c11
SM
120 easy-mmode-define-minor-mode
121 define-minor-mode)) t)
122 (eq (car-safe (car body)) 'interactive))
123 (if macrop (list 'quote 'macro) nil))))
124
d49298d9 125 ;; Convert defcustom to less space-consuming data.
e8139c11
SM
126 ((eq car 'defcustom)
127 (let ((varname (car-safe (cdr-safe form)))
128 (init (car-safe (cdr-safe (cdr-safe form))))
129 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form)))))
d49298d9
MR
130 ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
131 )
132 `(progn
133 (defvar ,varname ,init ,doc)
fb2dd970
SM
134 (custom-autoload ',varname ,file
135 ,(condition-case nil
136 (null (cadr (memq :set form)))
137 (error nil))))))
e8139c11 138
1bddeeed
SM
139 ((eq car 'defgroup)
140 ;; In Emacs this is normally handled separately by cus-dep.el, but for
141 ;; third party packages, it can be convenient to explicitly autoload
142 ;; a group.
143 (let ((groupname (nth 1 form)))
144 `(let ((loads (get ',groupname 'custom-loads)))
145 (if (member ',file loads) nil
146 (put ',groupname 'custom-loads (cons ',file loads))))))
147
e8139c11
SM
148 ;; nil here indicates that this is not a special autoload form.
149 (t nil))))
0231f2dc 150
890df022
SM
151;; Forms which have doc-strings which should be printed specially.
152;; A doc-string-elt property of ELT says that (nth ELT FORM) is
153;; the doc-string in FORM.
154;; Those properties are now set in lisp-mode.el.
fc89daee 155
3b979520
SM
156(defun autoload-generated-file ()
157 (expand-file-name generated-autoload-file
438d6bb6
SM
158 ;; File-local settings of generated-autoload-file should
159 ;; be interpreted relative to the file's location,
160 ;; of course.
161 (if (not (local-variable-p 'generated-autoload-file))
162 (expand-file-name "lisp" source-directory))))
0231f2dc 163
72c19d97 164
2336b6a9
KH
165(defun autoload-read-section-header ()
166 "Read a section header form.
167Since continuation lines have been marked as comments,
168we must copy the text of the form and remove those comment
169markers before we call `read'."
170 (save-match-data
171 (let ((beginning (point))
172 string)
173 (forward-line 1)
174 (while (looking-at generate-autoload-section-continuation)
175 (forward-line 1))
176 (setq string (buffer-substring beginning (point)))
177 (with-current-buffer (get-buffer-create " *autoload*")
178 (erase-buffer)
179 (insert string)
180 (goto-char (point-min))
181 (while (search-forward generate-autoload-section-continuation nil t)
182 (replace-match " "))
183 (goto-char (point-min))
184 (read (current-buffer))))))
185
ac644d50
JB
186(defvar autoload-print-form-outbuf nil
187 "Buffer which gets the output of `autoload-print-form'.")
4a3c5b3a 188
a8add29d 189(defun autoload-print-form (form)
4a3c5b3a
RS
190 "Print FORM such that `make-docfile' will find the docstrings.
191The variable `autoload-print-form-outbuf' specifies the buffer to
192put the output in."
a8add29d
SM
193 (cond
194 ;; If the form is a sequence, recurse.
195 ((eq (car form) 'progn) (mapcar 'autoload-print-form (cdr form)))
196 ;; Symbols at the toplevel are meaningless.
197 ((symbolp form) nil)
198 (t
4a3c5b3a
RS
199 (let ((doc-string-elt (get (car-safe form) 'doc-string-elt))
200 (outbuf autoload-print-form-outbuf))
a8add29d
SM
201 (if (and doc-string-elt (stringp (nth doc-string-elt form)))
202 ;; We need to hack the printing because the
203 ;; doc-string must be printed specially for
204 ;; make-docfile (sigh).
205 (let* ((p (nthcdr (1- doc-string-elt) form))
206 (elt (cdr p)))
207 (setcdr p nil)
208 (princ "\n(" outbuf)
209 (let ((print-escape-newlines t)
9d8563ca 210 (print-quoted t)
a8add29d 211 (print-escape-nonascii t))
7dab57b6
RS
212 (dolist (elt form)
213 (prin1 elt outbuf)
214 (princ " " outbuf)))
a8add29d
SM
215 (princ "\"\\\n" outbuf)
216 (let ((begin (with-current-buffer outbuf (point))))
217 (princ (substring (prin1-to-string (car elt)) 1)
218 outbuf)
219 ;; Insert a backslash before each ( that
220 ;; appears at the beginning of a line in
221 ;; the doc string.
222 (with-current-buffer outbuf
223 (save-excursion
890df022 224 (while (re-search-backward "\n[[(]" begin t)
a8add29d
SM
225 (forward-char 1)
226 (insert "\\"))))
227 (if (null (cdr elt))
228 (princ ")" outbuf)
229 (princ " " outbuf)
230 (princ (substring (prin1-to-string (cdr elt)) 1)
231 outbuf))
232 (terpri outbuf)))
233 (let ((print-escape-newlines t)
9d8563ca 234 (print-quoted t)
a8add29d
SM
235 (print-escape-nonascii t))
236 (print form outbuf)))))))
237
a273d3e0
GM
238(defun autoload-ensure-default-file (file)
239 "Make sure that the autoload file FILE exists and if not create it."
240 (unless (file-exists-p file)
5ec99342
GM
241 (let ((basename (file-name-nondirectory file)))
242 (write-region
243 (concat ";;; " basename
244 " --- automatically extracted autoloads\n"
245 ";;\n"
246 ";;; Code:\n\n"
247 "\f\n"
248 "(provide '" (file-name-sans-extension basename) ")\n"
249 ";; Local Variables:\n"
250 ";; version-control: never\n"
251 ";; no-byte-compile: t\n"
252 ";; no-update-autoloads: t\n"
253 ";; End:\n"
254 ";;; " basename
255 " ends here\n")
256 nil file)))
a273d3e0
GM
257 file)
258
259(defun autoload-insert-section-header (outbuf autoloads load-name file time)
260 "Insert the section-header line,
261which lists the file name and which functions are in it, etc."
262 (insert generate-autoload-section-header)
1fad2b12 263 (prin1 (list 'autoloads autoloads load-name file time)
a273d3e0
GM
264 outbuf)
265 (terpri outbuf)
266 ;; Break that line at spaces, to avoid very long lines.
267 ;; Make each sub-line into a comment.
268 (with-current-buffer outbuf
269 (save-excursion
270 (forward-line -1)
271 (while (not (eolp))
272 (move-to-column 64)
273 (skip-chars-forward "^ \n")
274 (or (eolp)
275 (insert "\n" generate-autoload-section-continuation))))))
276
69135525
SM
277(defun autoload-find-file (file)
278 "Fetch file and put it in a temp buffer. Return the buffer."
279 ;; It is faster to avoid visiting the file.
3b979520 280 (setq file (expand-file-name file))
69135525
SM
281 (with-current-buffer (get-buffer-create " *autoload-file*")
282 (kill-all-local-variables)
283 (erase-buffer)
284 (setq buffer-undo-list t
285 buffer-read-only nil)
286 (emacs-lisp-mode)
3b979520 287 (setq default-directory (file-name-directory file))
69135525
SM
288 (insert-file-contents file nil)
289 (let ((enable-local-variables :safe))
290 (hack-local-variables))
291 (current-buffer)))
292
b17b8839
SM
293(defvar no-update-autoloads nil
294 "File local variable to prevent scanning this file for autoload cookies.")
295
3b979520
SM
296(defun autoload-file-load-name (file)
297 (let ((name (file-name-nondirectory file)))
298 (if (string-match "\\.elc?\\(\\.\\|\\'\\)" name)
299 (substring name 0 (match-beginning 0))
300 name)))
301
0231f2dc
JB
302(defun generate-file-autoloads (file)
303 "Insert at point a loaddefs autoload section for FILE.
b17b8839 304Autoloads are generated for defuns and defmacros in FILE
da8826b4 305marked by `generate-autoload-cookie' (which see).
0231f2dc 306If FILE is being visited in a buffer, the contents of the buffer
b17b8839
SM
307are used.
308Return non-nil in the case where no autoloads were added at point."
0231f2dc 309 (interactive "fGenerate autoloads for file: ")
ceea9b18
SM
310 (autoload-generate-file-autoloads file (current-buffer)))
311
438d6bb6
SM
312;; When called from `generate-file-autoloads' we should ignore
313;; `generated-autoload-file' altogether. When called from
314;; `update-file-autoloads' we don't know `outbuf'. And when called from
315;; `update-directory-autoloads' it's in between: we know the default
316;; `outbuf' but we should obey any file-local setting of
317;; `generated-autoload-file'.
318(defun autoload-generate-file-autoloads (file &optional outbuf outfile)
ceea9b18
SM
319 "Insert an autoload section for FILE in the appropriate buffer.
320Autoloads are generated for defuns and defmacros in FILE
321marked by `generate-autoload-cookie' (which see).
322If FILE is being visited in a buffer, the contents of the buffer are used.
438d6bb6 323OUTBUF is the buffer in which the autoload statements should be inserted.
986c5ad5 324If OUTBUF is nil, it will be determined by `autoload-generated-file'.
e66466a6 325
438d6bb6
SM
326If provided, OUTFILE is expected to be the file name of OUTBUF.
327If OUTFILE is non-nil and FILE specifies a `generated-autoload-file'
328different from OUTFILE, then OUTBUF is ignored.
329
330Return non-nil iff FILE adds no autoloads to OUTFILE
331\(or OUTBUF if OUTFILE is nil)."
1fad2b12
SM
332 (catch 'done
333 (let ((autoloads-done '())
334 (load-name (autoload-file-load-name file))
335 (print-length nil)
a113b3ca 336 (print-level nil)
1fad2b12
SM
337 (print-readably t) ; This does something in Lucid Emacs.
338 (float-output-format nil)
339 (visited (get-file-buffer file))
438d6bb6 340 (otherbuf nil)
1fad2b12
SM
341 (absfile (expand-file-name file))
342 relfile
343 ;; nil until we found a cookie.
344 output-start)
345
346 (with-current-buffer (or visited
347 ;; It is faster to avoid visiting the file.
348 (autoload-find-file file))
349 ;; Obey the no-update-autoloads file local variable.
350 (unless no-update-autoloads
351 (message "Generating autoloads for %s..." file)
352 (save-excursion
353 (save-restriction
354 (widen)
355 (goto-char (point-min))
356 (while (not (eobp))
357 (skip-chars-forward " \t\n\f")
358 (cond
359 ((looking-at (regexp-quote generate-autoload-cookie))
360 ;; If not done yet, figure out where to insert this text.
361 (unless output-start
438d6bb6
SM
362 (when (and outfile
363 (not (equal outfile (autoload-generated-file))))
364 ;; A file-local setting of autoload-generated-file says
365 ;; we should ignore OUTBUF.
366 (setq outbuf nil)
367 (setq otherbuf t))
1fad2b12
SM
368 (unless outbuf
369 (setq outbuf (autoload-find-destination absfile))
370 (unless outbuf
371 ;; The file has autoload cookies, but they're
438d6bb6
SM
372 ;; already up-to-date. If OUTFILE is nil, the
373 ;; entries are in the expected OUTBUF, otherwise
374 ;; they're elsewhere.
375 (throw 'done outfile)))
1fad2b12
SM
376 (with-current-buffer outbuf
377 (setq relfile (file-relative-name absfile))
378 (setq output-start (point)))
379 ;; (message "file=%S, relfile=%S, dest=%S"
380 ;; file relfile (autoload-generated-file))
381 )
382 (search-forward generate-autoload-cookie)
383 (skip-chars-forward " \t")
384 (if (eolp)
385 (condition-case err
386 ;; Read the next form and make an autoload.
387 (let* ((form (prog1 (read (current-buffer))
388 (or (bolp) (forward-line 1))))
389 (autoload (make-autoload form load-name)))
390 (if autoload
391 (push (nth 1 form) autoloads-done)
392 (setq autoload form))
393 (let ((autoload-print-form-outbuf outbuf))
394 (autoload-print-form autoload)))
395 (error
396 (message "Error in %s: %S" file err)))
397
398 ;; Copy the rest of the line to the output.
399 (princ (buffer-substring
400 (progn
401 ;; Back up over whitespace, to preserve it.
402 (skip-chars-backward " \f\t")
403 (if (= (char-after (1+ (point))) ? )
404 ;; Eat one space.
405 (forward-char 1))
406 (point))
407 (progn (forward-line 1) (point)))
408 outbuf)))
409 ((looking-at ";")
410 ;; Don't read the comment.
411 (forward-line 1))
412 (t
413 (forward-sexp 1)
414 (forward-line 1))))))
415
416 (when output-start
0b7750a9
SM
417 (let ((secondary-autoloads-file-buf
418 (if (local-variable-p 'generated-autoload-file)
419 (current-buffer))))
420 (with-current-buffer outbuf
421 (save-excursion
422 ;; Insert the section-header line which lists the file name
423 ;; and which functions are in it, etc.
424 (goto-char output-start)
425 (autoload-insert-section-header
426 outbuf autoloads-done load-name relfile
427 (if secondary-autoloads-file-buf
428 ;; MD5 checksums are much better because they do not
429 ;; change unless the file changes (so they'll be
430 ;; equal on two different systems and will change
431 ;; less often than time-stamps, thus leading to fewer
432 ;; unneeded changes causing spurious conflicts), but
433 ;; using time-stamps is a very useful optimization,
434 ;; so we use time-stamps for the main autoloads file
435 ;; (loaddefs.el) where we have special ways to
436 ;; circumvent the "random change problem", and MD5
437 ;; checksum in secondary autoload files where we do
438 ;; not need the time-stamp optimization because it is
439 ;; already provided by the primary autoloads file.
563cfbf7
SM
440 (md5 secondary-autoloads-file-buf
441 ;; We'd really want to just use
442 ;; `emacs-internal' instead.
443 nil nil 'emacs-mule-unix)
0b7750a9
SM
444 (nth 5 (file-attributes relfile))))
445 (insert ";;; Generated autoloads from " relfile "\n"))
446 (insert generate-autoload-section-trailer))))
1fad2b12
SM
447 (message "Generating autoloads for %s...done" file))
448 (or visited
449 ;; We created this buffer, so we should kill it.
450 (kill-buffer (current-buffer))))
438d6bb6
SM
451 ;; If the entries were added to some other buffer, then the file
452 ;; doesn't add entries to OUTFILE.
453 (or (not output-start) otherbuf))))
e2b6138f 454\f
e66466a6
SM
455(defun autoload-save-buffers ()
456 (while autoload-modified-buffers
457 (with-current-buffer (pop autoload-modified-buffers)
458 (save-buffer))))
459
0231f2dc 460;;;###autoload
f7ed02ac 461(defun update-file-autoloads (file &optional save-after)
0231f2dc 462 "Update the autoloads for FILE in `generated-autoload-file'
a273d3e0 463\(which FILE might bind in its local variables).
f7ed02ac
RS
464If SAVE-AFTER is non-nil (which is always, when called interactively),
465save the buffer too.
466
467Return FILE if there was no autoload cookie in it, else nil."
468 (interactive "fUpdate autoloads for file: \np")
1fad2b12
SM
469 (let* ((autoload-modified-buffers nil)
470 (no-autoloads (autoload-generate-file-autoloads file)))
471 (if autoload-modified-buffers
986c5ad5
SM
472 (if save-after (autoload-save-buffers))
473 (if (interactive-p)
474 (message "Autoload section for %s is up to date." file)))
e66466a6 475 (if no-autoloads file)))
57536a83
SM
476
477(defun autoload-find-destination (file)
478 "Find the destination point of the current buffer's autoloads.
479FILE is the file name of the current buffer.
480Returns a buffer whose point is placed at the requested location.
1fad2b12 481Returns nil if the file's autoloads are uptodate, otherwise
0b7750a9 482removes any prior now out-of-date autoload entries."
1fad2b12 483 (catch 'up-to-date
0b7750a9
SM
484 (let* ((load-name (autoload-file-load-name file))
485 (buf (current-buffer))
486 (existing-buffer (if buffer-file-name buf))
487 (found nil))
1fad2b12 488 (with-current-buffer
812e2bd8
SM
489 ;; We used to use `raw-text' to read this file, but this causes
490 ;; problems when the file contains non-ASCII characters.
491 (find-file-noselect
492 (autoload-ensure-default-file (autoload-generated-file)))
1fad2b12
SM
493 ;; This is to make generated-autoload-file have Unix EOLs, so
494 ;; that it is portable to all platforms.
812e2bd8
SM
495 (unless (zerop (coding-system-eol-type buffer-file-coding-system))
496 (set-buffer-file-coding-system 'unix))
1fad2b12
SM
497 (or (> (buffer-size) 0)
498 (error "Autoloads file %s does not exist" buffer-file-name))
499 (or (file-writable-p buffer-file-name)
500 (error "Autoloads file %s is not writable" buffer-file-name))
501 (widen)
502 (goto-char (point-min))
503 ;; Look for the section for LOAD-NAME.
504 (while (and (not found)
505 (search-forward generate-autoload-section-header nil t))
506 (let ((form (autoload-read-section-header)))
507 (cond ((string= (nth 2 form) load-name)
508 ;; We found the section for this file.
509 ;; Check if it is up to date.
510 (let ((begin (match-beginning 0))
511 (last-time (nth 4 form))
512 (file-time (nth 5 (file-attributes file))))
513 (if (and (or (null existing-buffer)
514 (not (buffer-modified-p existing-buffer)))
0b7750a9
SM
515 (or
516 ;; last-time is the time-stamp (specifying
517 ;; the last time we looked at the file) and
518 ;; the file hasn't been changed since.
519 (and (listp last-time) (= (length last-time) 2)
520 (not (time-less-p last-time file-time)))
521 ;; last-time is an MD5 checksum instead.
522 (and (stringp last-time)
523 (equal last-time
524 (md5 buf nil nil 'emacs-mule)))))
1fad2b12
SM
525 (throw 'up-to-date nil)
526 (autoload-remove-section begin)
527 (setq found t))))
528 ((string< load-name (nth 2 form))
529 ;; We've come to a section alphabetically later than
530 ;; LOAD-NAME. We assume the file is in order and so
531 ;; there must be no section for LOAD-NAME. We will
532 ;; insert one before the section here.
533 (goto-char (match-beginning 0))
534 (setq found t)))))
535 (or found
536 (progn
537 ;; No later sections in the file. Put before the last page.
538 (goto-char (point-max))
539 (search-backward "\f" nil t)))
540 (unless (memq (current-buffer) autoload-modified-buffers)
541 (push (current-buffer) autoload-modified-buffers))
542 (current-buffer)))))
a273d3e0 543
a273d3e0
GM
544(defun autoload-remove-section (begin)
545 (goto-char begin)
546 (search-forward generate-autoload-section-trailer)
547 (delete-region begin (point)))
0231f2dc
JB
548
549;;;###autoload
56eebc29 550(defun update-directory-autoloads (&rest dirs)
d08589bf 551 "\
3fbca58a 552Update loaddefs.el with all the current autoloads from DIRS, and no old ones.
ac644d50 553This uses `update-file-autoloads' (which see) to do its work.
56eebc29
RS
554In an interactive call, you must give one argument, the name
555of a single directory. In a call from Lisp, you can supply multiple
556directories as separate arguments, but this usage is discouraged.
557
558The function does NOT recursively descend into subdirectories of the
559directory or directories specified."
b59c7256 560 (interactive "DUpdate autoloads from directory: ")
a85e4d58 561 (let* ((files-re (let ((tmp nil))
de10856c 562 (dolist (suf (get-load-suffixes)
a85e4d58
SM
563 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
564 (unless (string-match "\\.elc" suf) (push suf tmp)))))
565 (files (apply 'nconc
a273d3e0
GM
566 (mapcar (lambda (dir)
567 (directory-files (expand-file-name dir)
a85e4d58 568 t files-re))
a273d3e0 569 dirs)))
48df920c 570 (done ())
a273d3e0 571 (this-time (current-time))
438d6bb6
SM
572 ;; Files with no autoload cookies or whose autoloads go to other
573 ;; files because of file-local autoload-generated-file settings.
574 (no-autoloads nil)
575 (autoload-modified-buffers nil))
a273d3e0
GM
576
577 (with-current-buffer
438d6bb6
SM
578 (find-file-noselect
579 (autoload-ensure-default-file (autoload-generated-file)))
e2b6138f 580 (save-excursion
a273d3e0
GM
581
582 ;; Canonicalize file names and remove the autoload file itself.
1fad2b12
SM
583 (setq files (delete (file-relative-name buffer-file-name)
584 (mapcar 'file-relative-name files)))
a273d3e0 585
b59c7256
RM
586 (goto-char (point-min))
587 (while (search-forward generate-autoload-section-header nil t)
2336b6a9 588 (let* ((form (autoload-read-section-header))
b59c7256 589 (file (nth 3 form)))
a273d3e0
GM
590 (cond ((and (consp file) (stringp (car file)))
591 ;; This is a list of files that have no autoload cookies.
592 ;; There shouldn't be more than one such entry.
593 ;; Remove the obsolete section.
594 (autoload-remove-section (match-beginning 0))
595 (let ((last-time (nth 4 form)))
596 (dolist (file file)
597 (let ((file-time (nth 5 (file-attributes file))))
598 (when (and file-time
66dc9a0f 599 (not (time-less-p last-time file-time)))
a273d3e0
GM
600 ;; file unchanged
601 (push file no-autoloads)
602 (setq files (delete file files)))))))
603 ((not (stringp file)))
48df920c
SM
604 ((or (not (file-exists-p file))
605 ;; Remove duplicates as well, just in case.
606 (member file done))
607 ;; Remove the obsolete section.
a273d3e0 608 (autoload-remove-section (match-beginning 0)))
0b7750a9
SM
609 ((not (time-less-p (nth 4 form)
610 (nth 5 (file-attributes file))))
a273d3e0
GM
611 ;; File hasn't changed.
612 nil)
b59c7256 613 (t
438d6bb6
SM
614 (autoload-remove-section (match-beginning 0))
615 (if (autoload-generate-file-autoloads
616 file (current-buffer) buffer-file-name)
617 (push file no-autoloads))))
48df920c 618 (push file done)
b59c7256 619 (setq files (delete file files)))))
a273d3e0 620 ;; Elements remaining in FILES have no existing autoload sections yet.
438d6bb6
SM
621 (dolist (file files)
622 (if (autoload-generate-file-autoloads file nil buffer-file-name)
623 (push file no-autoloads)))
624
a273d3e0 625 (when no-autoloads
000d9923
MR
626 ;; Sort them for better readability.
627 (setq no-autoloads (sort no-autoloads 'string<))
a273d3e0
GM
628 ;; Add the `no-autoloads' section.
629 (goto-char (point-max))
630 (search-backward "\f" nil t)
631 (autoload-insert-section-header
632 (current-buffer) nil nil no-autoloads this-time)
633 (insert generate-autoload-section-trailer))
634
438d6bb6
SM
635 (save-buffer)
636 ;; In case autoload entries were added to other files because of
637 ;; file-local autoload-generated-file settings.
638 (autoload-save-buffers))))
0231f2dc 639
1e0888f5
LH
640(define-obsolete-function-alias 'update-autoloads-from-directories
641 'update-directory-autoloads "22.1")
642
0231f2dc
JB
643;;;###autoload
644(defun batch-update-autoloads ()
b59c7256 645 "Update loaddefs.el autoloads in batch mode.
375d5635 646Calls `update-directory-autoloads' on the command line arguments."
3a4336e6
MB
647 (let ((args command-line-args-left))
648 (setq command-line-args-left nil)
649 (apply 'update-directory-autoloads args)))
0231f2dc
JB
650
651(provide 'autoload)
ffd56f97 652
b7a45ee1 653;; arch-tag: 00244766-98f4-4767-bf42-8a22103441c6
c0274f38 654;;; autoload.el ends here