Revision: emacs@sv.gnu.org/emacs--unicode--0--patch-60
[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,
ceb4c4d3 4;; 2004, 2005, 2006 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
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 2, or (at your option)
14;; 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; see the file COPYING. If not, write to the
3a35cf56
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
0231f2dc 25
07b3798c 26;;; Commentary:
e41b2db1 27
00ee57f3 28;; This code helps GNU Emacs maintainers keep the loaddefs.el file up to
e41b2db1
ER
29;; date. It interprets magic cookies of the form ";;;###autoload" in
30;; lisp source files in various useful ways. To learn more, read the
31;; source; if you're going to use this, you'd better be able to.
32
e5167999
ER
33;;; Code:
34
b0daab9a 35(require 'lisp-mode) ;for `doc-string-elt' properties.
890df022 36(require 'help-fns) ;for help-add-fundoc-usage.
6826a134 37(eval-when-compile (require 'cl))
b0daab9a 38
0ceb5fe0
RS
39(defvar generated-autoload-file "loaddefs.el"
40 "*File \\[update-file-autoloads] puts autoloads into.
41A `.el' file can set this in its local variables section to make its
c8aca7d1
KH
42autoloads go somewhere else. The autoload file is assumed to contain a
43trailer starting with a FormFeed character.")
0ceb5fe0
RS
44
45(defconst generate-autoload-cookie ";;;###autoload"
46 "Magic comment indicating the following form should be autoloaded.
47Used by \\[update-file-autoloads]. This string should be
48meaningless to Lisp (e.g., a comment).
49
50This string is used:
51
52;;;###autoload
53\(defun function-to-be-autoloaded () ...)
54
55If this string appears alone on a line, the following form will be
56read and an autoload made for it. If there is further text on the line,
57that text will be copied verbatim to `generated-autoload-file'.")
58
59(defconst generate-autoload-section-header "\f\n;;;### "
2336b6a9 60 "String that marks the form at the start of a new file's autoload section.")
0ceb5fe0
RS
61
62(defconst generate-autoload-section-trailer "\n;;;***\n"
63 "String which indicates the end of the section of autoloads for a file.")
64
2336b6a9
KH
65(defconst generate-autoload-section-continuation ";;;;;; "
66 "String to add on each continuation of the section header form.")
67
0231f2dc 68(defun make-autoload (form file)
ceaa3695 69 "Turn FORM into an autoload or defvar for source file FILE.
e8139c11
SM
70Returns nil if FORM is not a special autoload form (i.e. a function definition
71or macro definition or a defcustom)."
72 (let ((car (car-safe form)) expand)
73 (cond
74 ;; For complex cases, try again on the macro-expansion.
1b39b493 75 ((and (memq car '(easy-mmode-define-global-mode define-global-minor-mode
e8139c11
SM
76 easy-mmode-define-minor-mode define-minor-mode))
77 (setq expand (let ((load-file-name file)) (macroexpand form)))
78 (eq (car expand) 'progn)
79 (memq :autoload-end expand))
80 (let ((end (memq :autoload-end expand)))
81 ;; Cut-off anything after the :autoload-end marker.
82 (setcdr end nil)
83 (cons 'progn
84 (mapcar (lambda (form) (make-autoload form file))
85 (cdr expand)))))
86
87 ;; For special function-like operators, use the `autoload' function.
88 ((memq car '(defun define-skeleton defmacro define-derived-mode
c6a3142d
JL
89 define-compilation-mode define-generic-mode
90 easy-mmode-define-global-mode define-global-minor-mode
91 easy-mmode-define-minor-mode define-minor-mode
92 defun* defmacro*))
74312ddc 93 (let* ((macrop (memq car '(defmacro defmacro*)))
e8139c11 94 (name (nth 1 form))
6826a134
SM
95 (args (case car
96 ((defun defmacro defun* defmacro*) (nth 2 form))
97 ((define-skeleton) '(&optional str arg))
bec34fb0
TTN
98 ((define-generic-mode define-derived-mode
99 define-compilation-mode) nil)
6826a134 100 (t)))
e8139c11
SM
101 (body (nthcdr (get car 'doc-string-elt) form))
102 (doc (if (stringp (car body)) (pop body))))
890df022
SM
103 (when (listp args)
104 ;; Add the usage form at the end where describe-function-1
105 ;; can recover it.
106 (setq doc (help-add-fundoc-usage doc args)))
e8139c11
SM
107 ;; `define-generic-mode' quotes the name, so take care of that
108 (list 'autoload (if (listp name) name (list 'quote name)) file doc
109 (or (and (memq car '(define-skeleton define-derived-mode
110 define-generic-mode
111 easy-mmode-define-global-mode
c6a3142d 112 define-global-minor-mode
e8139c11
SM
113 easy-mmode-define-minor-mode
114 define-minor-mode)) t)
115 (eq (car-safe (car body)) 'interactive))
116 (if macrop (list 'quote 'macro) nil))))
117
d49298d9 118 ;; Convert defcustom to less space-consuming data.
e8139c11
SM
119 ((eq car 'defcustom)
120 (let ((varname (car-safe (cdr-safe form)))
121 (init (car-safe (cdr-safe (cdr-safe form))))
122 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form)))))
d49298d9
MR
123 ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
124 )
125 `(progn
126 (defvar ,varname ,init ,doc)
b7a45ee1
SM
127 (custom-autoload ',varname ,file)
128 ;; The use of :require in a defcustom can be annoying, especially
129 ;; when defcustoms are moved from one file to another between
130 ;; releases because the :require arg gets placed in the user's
131 ;; .emacs. In order for autoloaded minor modes not to need the
132 ;; use of :require, we arrange to store their :setter.
133 ,(let ((setter (condition-case nil
134 (cadr (memq :set form))
135 (error nil))))
136 (if (equal setter ''custom-set-minor-mode)
137 `(put ',varname 'custom-set 'custom-set-minor-mode))))))
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
0231f2dc 156
72c19d97 157(defun autoload-trim-file-name (file)
8d3e5475 158 ;; Returns a relative file path for FILE
1eb0a345
RS
159 ;; starting from the directory that loaddefs.el is in.
160 ;; That is normally a directory in load-path,
161 ;; which means Emacs will be able to find FILE when it looks.
162 ;; Any extra directory names here would prevent finding the file.
72c19d97
RM
163 (setq file (expand-file-name file))
164 (file-relative-name file
1eb0a345 165 (file-name-directory generated-autoload-file)))
72c19d97 166
2336b6a9
KH
167(defun autoload-read-section-header ()
168 "Read a section header form.
169Since continuation lines have been marked as comments,
170we must copy the text of the form and remove those comment
171markers before we call `read'."
172 (save-match-data
173 (let ((beginning (point))
174 string)
175 (forward-line 1)
176 (while (looking-at generate-autoload-section-continuation)
177 (forward-line 1))
178 (setq string (buffer-substring beginning (point)))
179 (with-current-buffer (get-buffer-create " *autoload*")
180 (erase-buffer)
181 (insert string)
182 (goto-char (point-min))
183 (while (search-forward generate-autoload-section-continuation nil t)
184 (replace-match " "))
185 (goto-char (point-min))
186 (read (current-buffer))))))
187
ac644d50
JB
188(defvar autoload-print-form-outbuf nil
189 "Buffer which gets the output of `autoload-print-form'.")
4a3c5b3a 190
a8add29d 191(defun autoload-print-form (form)
4a3c5b3a
RS
192 "Print FORM such that `make-docfile' will find the docstrings.
193The variable `autoload-print-form-outbuf' specifies the buffer to
194put the output in."
a8add29d
SM
195 (cond
196 ;; If the form is a sequence, recurse.
197 ((eq (car form) 'progn) (mapcar 'autoload-print-form (cdr form)))
198 ;; Symbols at the toplevel are meaningless.
199 ((symbolp form) nil)
200 (t
4a3c5b3a
RS
201 (let ((doc-string-elt (get (car-safe form) 'doc-string-elt))
202 (outbuf autoload-print-form-outbuf))
a8add29d
SM
203 (if (and doc-string-elt (stringp (nth doc-string-elt form)))
204 ;; We need to hack the printing because the
205 ;; doc-string must be printed specially for
206 ;; make-docfile (sigh).
207 (let* ((p (nthcdr (1- doc-string-elt) form))
208 (elt (cdr p)))
209 (setcdr p nil)
210 (princ "\n(" outbuf)
211 (let ((print-escape-newlines t)
212 (print-escape-nonascii t))
7dab57b6
RS
213 (dolist (elt form)
214 (prin1 elt outbuf)
215 (princ " " outbuf)))
a8add29d
SM
216 (princ "\"\\\n" outbuf)
217 (let ((begin (with-current-buffer outbuf (point))))
218 (princ (substring (prin1-to-string (car elt)) 1)
219 outbuf)
220 ;; Insert a backslash before each ( that
221 ;; appears at the beginning of a line in
222 ;; the doc string.
223 (with-current-buffer outbuf
224 (save-excursion
890df022 225 (while (re-search-backward "\n[[(]" begin t)
a8add29d
SM
226 (forward-char 1)
227 (insert "\\"))))
228 (if (null (cdr elt))
229 (princ ")" outbuf)
230 (princ " " outbuf)
231 (princ (substring (prin1-to-string (cdr elt)) 1)
232 outbuf))
233 (terpri outbuf)))
234 (let ((print-escape-newlines t)
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)
241 (write-region
242 (concat ";;; " (file-name-nondirectory file)
243 " --- automatically extracted autoloads\n"
244 ";;\n"
245 ";;; Code:\n\n"
246 "\f\n;; Local Variables:\n"
247 ";; version-control: never\n"
248 ";; no-byte-compile: t\n"
249 ";; no-update-autoloads: t\n"
250 ";; End:\n"
251 ";;; " (file-name-nondirectory file)
4994a50d 252 " ends here\n")
a273d3e0
GM
253 nil file))
254 file)
255
256(defun autoload-insert-section-header (outbuf autoloads load-name file time)
257 "Insert the section-header line,
258which lists the file name and which functions are in it, etc."
259 (insert generate-autoload-section-header)
260 (prin1 (list 'autoloads autoloads load-name
261 (if (stringp file) (autoload-trim-file-name file) file)
262 time)
263 outbuf)
264 (terpri outbuf)
265 ;; Break that line at spaces, to avoid very long lines.
266 ;; Make each sub-line into a comment.
267 (with-current-buffer outbuf
268 (save-excursion
269 (forward-line -1)
270 (while (not (eolp))
271 (move-to-column 64)
272 (skip-chars-forward "^ \n")
273 (or (eolp)
274 (insert "\n" generate-autoload-section-continuation))))))
275
0231f2dc
JB
276(defun generate-file-autoloads (file)
277 "Insert at point a loaddefs autoload section for FILE.
278autoloads are generated for defuns and defmacros in FILE
da8826b4 279marked by `generate-autoload-cookie' (which see).
0231f2dc
JB
280If FILE is being visited in a buffer, the contents of the buffer
281are used."
282 (interactive "fGenerate autoloads for file: ")
283 (let ((outbuf (current-buffer))
0231f2dc
JB
284 (autoloads-done '())
285 (load-name (let ((name (file-name-nondirectory file)))
a85e4d58 286 (if (string-match "\\.elc?\\(\\.\\|$\\)" name)
0231f2dc
JB
287 (substring name 0 (match-beginning 0))
288 name)))
289 (print-length nil)
72c19d97 290 (print-readably t) ; This does something in Lucid Emacs.
aa7ea8bd 291 (float-output-format nil)
0231f2dc 292 (done-any nil)
7e4263eb 293 (visited (get-file-buffer file))
0231f2dc 294 output-end)
daa37602
JB
295
296 ;; If the autoload section we create here uses an absolute
8d3e5475 297 ;; file name for FILE in its header, and then Emacs is installed
daa37602
JB
298 ;; under a different path on another system,
299 ;; `update-autoloads-here' won't be able to find the files to be
300 ;; autoloaded. So, if FILE is in the same directory or a
e5d77022 301 ;; subdirectory of the current buffer's directory, we'll make it
daa37602
JB
302 ;; relative to the current buffer's directory.
303 (setq file (expand-file-name file))
1265394f
RM
304 (let* ((source-truename (file-truename file))
305 (dir-truename (file-name-as-directory
306 (file-truename default-directory)))
307 (len (length dir-truename)))
308 (if (and (< len (length source-truename))
309 (string= dir-truename (substring source-truename 0 len)))
310 (setq file (substring source-truename len))))
daa37602 311
0231f2dc 312 (message "Generating autoloads for %s..." file)
6798a385
JB
313 (save-excursion
314 (unwind-protect
315 (progn
66fc2bf5
RM
316 (if visited
317 (set-buffer visited)
318 ;; It is faster to avoid visiting the file.
319 (set-buffer (get-buffer-create " *generate-autoload-file*"))
320 (kill-all-local-variables)
321 (erase-buffer)
b59c7256
RM
322 (setq buffer-undo-list t
323 buffer-read-only nil)
324 (emacs-lisp-mode)
66fc2bf5 325 (insert-file-contents file nil))
6798a385
JB
326 (save-excursion
327 (save-restriction
328 (widen)
329 (goto-char (point-min))
330 (while (not (eobp))
331 (skip-chars-forward " \t\n\f")
332 (cond
333 ((looking-at (regexp-quote generate-autoload-cookie))
334 (search-forward generate-autoload-cookie)
335 (skip-chars-forward " \t")
336 (setq done-any t)
9ed4d64f
RM
337 (if (eolp)
338 ;; Read the next form and make an autoload.
339 (let* ((form (prog1 (read (current-buffer))
a8add29d
SM
340 (or (bolp) (forward-line 1))))
341 (autoload (make-autoload form load-name)))
9ed4d64f
RM
342 (if autoload
343 (setq autoloads-done (cons (nth 1 form)
344 autoloads-done))
345 (setq autoload form))
4a3c5b3a
RS
346 (let ((autoload-print-form-outbuf outbuf))
347 (autoload-print-form autoload)))
a1506d29 348
a8add29d 349 ;; Copy the rest of the line to the output.
6e21af56
RM
350 (princ (buffer-substring
351 (progn
352 ;; Back up over whitespace, to preserve it.
353 (skip-chars-backward " \f\t")
354 (if (= (char-after (1+ (point))) ? )
355 ;; Eat one space.
356 (forward-char 1))
357 (point))
358 (progn (forward-line 1) (point)))
359 outbuf)))
72c19d97
RM
360 ((looking-at ";")
361 ;; Don't read the comment.
362 (forward-line 1))
363 (t
364 (forward-sexp 1)
365 (forward-line 1)))))))
6798a385
JB
366 (or visited
367 ;; We created this buffer, so we should kill it.
368 (kill-buffer (current-buffer)))
369 (set-buffer outbuf)
370 (setq output-end (point-marker))))
0231f2dc
JB
371 (if done-any
372 (progn
2336b6a9
KH
373 ;; Insert the section-header line
374 ;; which lists the file name and which functions are in it, etc.
a273d3e0
GM
375 (autoload-insert-section-header outbuf autoloads-done load-name file
376 (nth 5 (file-attributes file)))
72c19d97
RM
377 (insert ";;; Generated autoloads from "
378 (autoload-trim-file-name file) "\n")
0231f2dc
JB
379 (goto-char output-end)
380 (insert generate-autoload-section-trailer)))
381 (message "Generating autoloads for %s...done" file)))
e2b6138f 382\f
0231f2dc 383;;;###autoload
f7ed02ac 384(defun update-file-autoloads (file &optional save-after)
0231f2dc 385 "Update the autoloads for FILE in `generated-autoload-file'
a273d3e0 386\(which FILE might bind in its local variables).
f7ed02ac
RS
387If SAVE-AFTER is non-nil (which is always, when called interactively),
388save the buffer too.
389
390Return FILE if there was no autoload cookie in it, else nil."
391 (interactive "fUpdate autoloads for file: \np")
0231f2dc 392 (let ((load-name (let ((name (file-name-nondirectory file)))
a85e4d58 393 (if (string-match "\\.elc?\\(\\.\\|$\\)" name)
0231f2dc
JB
394 (substring name 0 (match-beginning 0))
395 name)))
d55e3ff0 396 (found nil)
a273d3e0
GM
397 (existing-buffer (get-file-buffer file))
398 (no-autoloads nil))
0231f2dc
JB
399 (save-excursion
400 ;; We want to get a value for generated-autoload-file from
401 ;; the local variables section if it's there.
b59c7256
RM
402 (if existing-buffer
403 (set-buffer existing-buffer))
9e3366e4
EZ
404 ;; We must read/write the file without any code conversion,
405 ;; but still decode EOLs.
406 (let ((coding-system-for-read 'raw-text))
68521d61 407 (set-buffer (find-file-noselect
a273d3e0
GM
408 (autoload-ensure-default-file
409 (expand-file-name generated-autoload-file
410 (expand-file-name "lisp"
411 source-directory)))))
9e3366e4
EZ
412 ;; This is to make generated-autoload-file have Unix EOLs, so
413 ;; that it is portable to all platforms.
414 (setq buffer-file-coding-system 'raw-text-unix))
0b33ec46
RS
415 (or (> (buffer-size) 0)
416 (error "Autoloads file %s does not exist" buffer-file-name))
417 (or (file-writable-p buffer-file-name)
418 (error "Autoloads file %s is not writable" buffer-file-name))
0231f2dc
JB
419 (save-excursion
420 (save-restriction
421 (widen)
422 (goto-char (point-min))
d55e3ff0
RM
423 ;; Look for the section for LOAD-NAME.
424 (while (and (not found)
425 (search-forward generate-autoload-section-header nil t))
2336b6a9 426 (let ((form (autoload-read-section-header)))
d55e3ff0
RM
427 (cond ((string= (nth 2 form) load-name)
428 ;; We found the section for this file.
429 ;; Check if it is up to date.
430 (let ((begin (match-beginning 0))
431 (last-time (nth 4 form))
432 (file-time (nth 5 (file-attributes file))))
433 (if (and (or (null existing-buffer)
434 (not (buffer-modified-p existing-buffer)))
435 (listp last-time) (= (length last-time) 2)
66dc9a0f 436 (not (time-less-p last-time file-time)))
d55e3ff0 437 (progn
d5aa62ec
RM
438 (if (interactive-p)
439 (message "\
440Autoload section for %s is up to date."
441 file))
d55e3ff0
RM
442 (setq found 'up-to-date))
443 (search-forward generate-autoload-section-trailer)
444 (delete-region begin (point))
445 (setq found t))))
446 ((string< load-name (nth 2 form))
447 ;; We've come to a section alphabetically later than
448 ;; LOAD-NAME. We assume the file is in order and so
449 ;; there must be no section for LOAD-NAME. We will
450 ;; insert one before the section here.
451 (goto-char (match-beginning 0))
72c19d97 452 (setq found 'new)))))
0f09bac6
RM
453 (or found
454 (progn
455 (setq found 'new)
456 ;; No later sections in the file. Put before the last page.
457 (goto-char (point-max))
5a161ab5 458 (search-backward "\f" nil t)))
72c19d97 459 (or (eq found 'up-to-date)
0f09bac6 460 (and (eq found 'new)
72c19d97
RM
461 ;; Check that FILE has any cookies before generating a
462 ;; new section for it.
463 (save-excursion
b59c7256
RM
464 (if existing-buffer
465 (set-buffer existing-buffer)
466 ;; It is faster to avoid visiting the file.
467 (set-buffer (get-buffer-create " *autoload-file*"))
468 (kill-all-local-variables)
469 (erase-buffer)
470 (setq buffer-undo-list t
471 buffer-read-only nil)
472 (emacs-lisp-mode)
473 (insert-file-contents file nil))
72c19d97 474 (save-excursion
b59c7256
RM
475 (save-restriction
476 (widen)
477 (goto-char (point-min))
478 (prog1
f0646ca0
RM
479 (if (re-search-forward
480 (concat "^" (regexp-quote
481 generate-autoload-cookie))
b59c7256
RM
482 nil t)
483 nil
484 (if (interactive-p)
75632953 485 (message "%s has no autoloads" file))
a273d3e0 486 (setq no-autoloads t)
b59c7256
RM
487 t)
488 (or existing-buffer
489 (kill-buffer (current-buffer))))))))
57cf354d 490 (generate-file-autoloads file))))
f7ed02ac 491 (and save-after
2336b6a9 492 (buffer-modified-p)
a273d3e0
GM
493 (save-buffer))
494
495 (if no-autoloads file))))
496
a273d3e0
GM
497(defun autoload-remove-section (begin)
498 (goto-char begin)
499 (search-forward generate-autoload-section-trailer)
500 (delete-region begin (point)))
0231f2dc
JB
501
502;;;###autoload
56eebc29 503(defun update-directory-autoloads (&rest dirs)
d08589bf 504 "\
3fbca58a 505Update loaddefs.el with all the current autoloads from DIRS, and no old ones.
ac644d50 506This uses `update-file-autoloads' (which see) to do its work.
56eebc29
RS
507In an interactive call, you must give one argument, the name
508of a single directory. In a call from Lisp, you can supply multiple
509directories as separate arguments, but this usage is discouraged.
510
511The function does NOT recursively descend into subdirectories of the
512directory or directories specified."
b59c7256 513 (interactive "DUpdate autoloads from directory: ")
a85e4d58 514 (let* ((files-re (let ((tmp nil))
de10856c 515 (dolist (suf (get-load-suffixes)
a85e4d58
SM
516 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
517 (unless (string-match "\\.elc" suf) (push suf tmp)))))
518 (files (apply 'nconc
a273d3e0
GM
519 (mapcar (lambda (dir)
520 (directory-files (expand-file-name dir)
a85e4d58 521 t files-re))
a273d3e0
GM
522 dirs)))
523 (this-time (current-time))
524 (no-autoloads nil) ;files with no autoload cookies.
525 (autoloads-file
0e193890 526 (expand-file-name generated-autoload-file
a273d3e0
GM
527 (expand-file-name "lisp" source-directory)))
528 (top-dir (file-name-directory autoloads-file)))
529
530 (with-current-buffer
531 (find-file-noselect (autoload-ensure-default-file autoloads-file))
e2b6138f 532 (save-excursion
a273d3e0
GM
533
534 ;; Canonicalize file names and remove the autoload file itself.
535 (setq files (delete (autoload-trim-file-name buffer-file-name)
536 (mapcar 'autoload-trim-file-name files)))
537
b59c7256
RM
538 (goto-char (point-min))
539 (while (search-forward generate-autoload-section-header nil t)
2336b6a9 540 (let* ((form (autoload-read-section-header))
b59c7256 541 (file (nth 3 form)))
a273d3e0
GM
542 (cond ((and (consp file) (stringp (car file)))
543 ;; This is a list of files that have no autoload cookies.
544 ;; There shouldn't be more than one such entry.
545 ;; Remove the obsolete section.
546 (autoload-remove-section (match-beginning 0))
547 (let ((last-time (nth 4 form)))
548 (dolist (file file)
549 (let ((file-time (nth 5 (file-attributes file))))
550 (when (and file-time
66dc9a0f 551 (not (time-less-p last-time file-time)))
a273d3e0
GM
552 ;; file unchanged
553 (push file no-autoloads)
554 (setq files (delete file files)))))))
555 ((not (stringp file)))
3fbca58a 556 ((not (file-exists-p (expand-file-name file top-dir)))
b59c7256 557 ;; Remove the obsolete section.
a273d3e0
GM
558 (autoload-remove-section (match-beginning 0)))
559 ((equal (nth 4 form) (nth 5 (file-attributes file)))
560 ;; File hasn't changed.
561 nil)
b59c7256
RM
562 (t
563 (update-file-autoloads file)))
564 (setq files (delete file files)))))
a273d3e0
GM
565 ;; Elements remaining in FILES have no existing autoload sections yet.
566 (setq no-autoloads
567 (append no-autoloads
568 (delq nil (mapcar 'update-file-autoloads files))))
569 (when no-autoloads
000d9923
MR
570 ;; Sort them for better readability.
571 (setq no-autoloads (sort no-autoloads 'string<))
a273d3e0
GM
572 ;; Add the `no-autoloads' section.
573 (goto-char (point-max))
574 (search-backward "\f" nil t)
575 (autoload-insert-section-header
576 (current-buffer) nil nil no-autoloads this-time)
577 (insert generate-autoload-section-trailer))
578
b59c7256 579 (save-buffer))))
0231f2dc 580
1e0888f5
LH
581(define-obsolete-function-alias 'update-autoloads-from-directories
582 'update-directory-autoloads "22.1")
583
0231f2dc
JB
584;;;###autoload
585(defun batch-update-autoloads ()
b59c7256 586 "Update loaddefs.el autoloads in batch mode.
375d5635
JPW
587Calls `update-directory-autoloads' on the command line arguments."
588 (apply 'update-directory-autoloads command-line-args-left)
b59c7256 589 (setq command-line-args-left nil))
0231f2dc
JB
590
591(provide 'autoload)
ffd56f97 592
b7a45ee1 593;; arch-tag: 00244766-98f4-4767-bf42-8a22103441c6
c0274f38 594;;; autoload.el ends here