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