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