(maybe_call_debugger): New function.
[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
0b7750a9
SM
412 (let ((secondary-autoloads-file-buf
413 (if (local-variable-p 'generated-autoload-file)
414 (current-buffer))))
415 (with-current-buffer outbuf
416 (save-excursion
417 ;; Insert the section-header line which lists the file name
418 ;; and which functions are in it, etc.
419 (goto-char output-start)
420 (autoload-insert-section-header
421 outbuf autoloads-done load-name relfile
422 (if secondary-autoloads-file-buf
423 ;; MD5 checksums are much better because they do not
424 ;; change unless the file changes (so they'll be
425 ;; equal on two different systems and will change
426 ;; less often than time-stamps, thus leading to fewer
427 ;; unneeded changes causing spurious conflicts), but
428 ;; using time-stamps is a very useful optimization,
429 ;; so we use time-stamps for the main autoloads file
430 ;; (loaddefs.el) where we have special ways to
431 ;; circumvent the "random change problem", and MD5
432 ;; checksum in secondary autoload files where we do
433 ;; not need the time-stamp optimization because it is
434 ;; already provided by the primary autoloads file.
563cfbf7
SM
435 (md5 secondary-autoloads-file-buf
436 ;; We'd really want to just use
437 ;; `emacs-internal' instead.
438 nil nil 'emacs-mule-unix)
0b7750a9
SM
439 (nth 5 (file-attributes relfile))))
440 (insert ";;; Generated autoloads from " relfile "\n"))
441 (insert generate-autoload-section-trailer))))
1fad2b12
SM
442 (message "Generating autoloads for %s...done" file))
443 (or visited
444 ;; We created this buffer, so we should kill it.
445 (kill-buffer (current-buffer))))
438d6bb6
SM
446 ;; If the entries were added to some other buffer, then the file
447 ;; doesn't add entries to OUTFILE.
448 (or (not output-start) otherbuf))))
1fad2b12 449\f
e66466a6
SM
450(defun autoload-save-buffers ()
451 (while autoload-modified-buffers
452 (with-current-buffer (pop autoload-modified-buffers)
453 (save-buffer))))
454
0231f2dc 455;;;###autoload
f7ed02ac 456(defun update-file-autoloads (file &optional save-after)
0231f2dc 457 "Update the autoloads for FILE in `generated-autoload-file'
a273d3e0 458\(which FILE might bind in its local variables).
f7ed02ac
RS
459If SAVE-AFTER is non-nil (which is always, when called interactively),
460save the buffer too.
461
462Return FILE if there was no autoload cookie in it, else nil."
463 (interactive "fUpdate autoloads for file: \np")
1fad2b12
SM
464 (let* ((autoload-modified-buffers nil)
465 (no-autoloads (autoload-generate-file-autoloads file)))
466 (if autoload-modified-buffers
986c5ad5
SM
467 (if save-after (autoload-save-buffers))
468 (if (interactive-p)
469 (message "Autoload section for %s is up to date." file)))
e66466a6 470 (if no-autoloads file)))
57536a83
SM
471
472(defun autoload-find-destination (file)
473 "Find the destination point of the current buffer's autoloads.
474FILE is the file name of the current buffer.
475Returns a buffer whose point is placed at the requested location.
1fad2b12 476Returns nil if the file's autoloads are uptodate, otherwise
0b7750a9 477removes any prior now out-of-date autoload entries."
1fad2b12 478 (catch 'up-to-date
0b7750a9
SM
479 (let* ((load-name (autoload-file-load-name file))
480 (buf (current-buffer))
481 (existing-buffer (if buffer-file-name buf))
482 (found nil))
1fad2b12
SM
483 (with-current-buffer
484 ;; We must read/write the file without any code conversion,
485 ;; but still decode EOLs.
486 (let ((coding-system-for-read 'raw-text))
487 (find-file-noselect
488 (autoload-ensure-default-file (autoload-generated-file))))
489 ;; This is to make generated-autoload-file have Unix EOLs, so
490 ;; that it is portable to all platforms.
491 (setq buffer-file-coding-system 'raw-text-unix)
492 (or (> (buffer-size) 0)
493 (error "Autoloads file %s does not exist" buffer-file-name))
494 (or (file-writable-p buffer-file-name)
495 (error "Autoloads file %s is not writable" buffer-file-name))
496 (widen)
497 (goto-char (point-min))
498 ;; Look for the section for LOAD-NAME.
499 (while (and (not found)
500 (search-forward generate-autoload-section-header nil t))
501 (let ((form (autoload-read-section-header)))
502 (cond ((string= (nth 2 form) load-name)
503 ;; We found the section for this file.
504 ;; Check if it is up to date.
505 (let ((begin (match-beginning 0))
506 (last-time (nth 4 form))
507 (file-time (nth 5 (file-attributes file))))
508 (if (and (or (null existing-buffer)
509 (not (buffer-modified-p existing-buffer)))
0b7750a9
SM
510 (or
511 ;; last-time is the time-stamp (specifying
512 ;; the last time we looked at the file) and
513 ;; the file hasn't been changed since.
514 (and (listp last-time) (= (length last-time) 2)
515 (not (time-less-p last-time file-time)))
516 ;; last-time is an MD5 checksum instead.
517 (and (stringp last-time)
518 (equal last-time
519 (md5 buf nil nil 'emacs-mule)))))
1fad2b12
SM
520 (throw 'up-to-date nil)
521 (autoload-remove-section begin)
522 (setq found t))))
523 ((string< load-name (nth 2 form))
524 ;; We've come to a section alphabetically later than
525 ;; LOAD-NAME. We assume the file is in order and so
526 ;; there must be no section for LOAD-NAME. We will
527 ;; insert one before the section here.
528 (goto-char (match-beginning 0))
529 (setq found t)))))
530 (or found
531 (progn
532 ;; No later sections in the file. Put before the last page.
533 (goto-char (point-max))
534 (search-backward "\f" nil t)))
535 (unless (memq (current-buffer) autoload-modified-buffers)
536 (push (current-buffer) autoload-modified-buffers))
537 (current-buffer)))))
a273d3e0 538
a273d3e0
GM
539(defun autoload-remove-section (begin)
540 (goto-char begin)
541 (search-forward generate-autoload-section-trailer)
542 (delete-region begin (point)))
0231f2dc
JB
543
544;;;###autoload
56eebc29 545(defun update-directory-autoloads (&rest dirs)
d08589bf 546 "\
3fbca58a 547Update loaddefs.el with all the current autoloads from DIRS, and no old ones.
ac644d50 548This uses `update-file-autoloads' (which see) to do its work.
56eebc29
RS
549In an interactive call, you must give one argument, the name
550of a single directory. In a call from Lisp, you can supply multiple
551directories as separate arguments, but this usage is discouraged.
552
553The function does NOT recursively descend into subdirectories of the
554directory or directories specified."
b59c7256 555 (interactive "DUpdate autoloads from directory: ")
a85e4d58 556 (let* ((files-re (let ((tmp nil))
de10856c 557 (dolist (suf (get-load-suffixes)
a85e4d58
SM
558 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
559 (unless (string-match "\\.elc" suf) (push suf tmp)))))
560 (files (apply 'nconc
a273d3e0
GM
561 (mapcar (lambda (dir)
562 (directory-files (expand-file-name dir)
a85e4d58 563 t files-re))
a273d3e0 564 dirs)))
48df920c 565 (done ())
a273d3e0 566 (this-time (current-time))
438d6bb6
SM
567 ;; Files with no autoload cookies or whose autoloads go to other
568 ;; files because of file-local autoload-generated-file settings.
569 (no-autoloads nil)
570 (autoload-modified-buffers nil))
a273d3e0
GM
571
572 (with-current-buffer
438d6bb6
SM
573 (find-file-noselect
574 (autoload-ensure-default-file (autoload-generated-file)))
e2b6138f 575 (save-excursion
a273d3e0
GM
576
577 ;; Canonicalize file names and remove the autoload file itself.
1fad2b12
SM
578 (setq files (delete (file-relative-name buffer-file-name)
579 (mapcar 'file-relative-name files)))
a273d3e0 580
b59c7256
RM
581 (goto-char (point-min))
582 (while (search-forward generate-autoload-section-header nil t)
2336b6a9 583 (let* ((form (autoload-read-section-header))
b59c7256 584 (file (nth 3 form)))
a273d3e0
GM
585 (cond ((and (consp file) (stringp (car file)))
586 ;; This is a list of files that have no autoload cookies.
587 ;; There shouldn't be more than one such entry.
588 ;; Remove the obsolete section.
589 (autoload-remove-section (match-beginning 0))
590 (let ((last-time (nth 4 form)))
591 (dolist (file file)
592 (let ((file-time (nth 5 (file-attributes file))))
593 (when (and file-time
66dc9a0f 594 (not (time-less-p last-time file-time)))
a273d3e0
GM
595 ;; file unchanged
596 (push file no-autoloads)
597 (setq files (delete file files)))))))
598 ((not (stringp file)))
48df920c
SM
599 ((or (not (file-exists-p file))
600 ;; Remove duplicates as well, just in case.
601 (member file done))
602 ;; Remove the obsolete section.
a273d3e0 603 (autoload-remove-section (match-beginning 0)))
0b7750a9
SM
604 ((not (time-less-p (nth 4 form)
605 (nth 5 (file-attributes file))))
a273d3e0
GM
606 ;; File hasn't changed.
607 nil)
b59c7256 608 (t
438d6bb6
SM
609 (autoload-remove-section (match-beginning 0))
610 (if (autoload-generate-file-autoloads
611 file (current-buffer) buffer-file-name)
612 (push file no-autoloads))))
48df920c 613 (push file done)
b59c7256 614 (setq files (delete file files)))))
a273d3e0 615 ;; Elements remaining in FILES have no existing autoload sections yet.
438d6bb6
SM
616 (dolist (file files)
617 (if (autoload-generate-file-autoloads file nil buffer-file-name)
618 (push file no-autoloads)))
619
a273d3e0 620 (when no-autoloads
000d9923
MR
621 ;; Sort them for better readability.
622 (setq no-autoloads (sort no-autoloads 'string<))
a273d3e0
GM
623 ;; Add the `no-autoloads' section.
624 (goto-char (point-max))
625 (search-backward "\f" nil t)
626 (autoload-insert-section-header
627 (current-buffer) nil nil no-autoloads this-time)
628 (insert generate-autoload-section-trailer))
629
438d6bb6
SM
630 (save-buffer)
631 ;; In case autoload entries were added to other files because of
632 ;; file-local autoload-generated-file settings.
633 (autoload-save-buffers))))
0231f2dc 634
1e0888f5
LH
635(define-obsolete-function-alias 'update-autoloads-from-directories
636 'update-directory-autoloads "22.1")
637
0231f2dc
JB
638;;;###autoload
639(defun batch-update-autoloads ()
b59c7256 640 "Update loaddefs.el autoloads in batch mode.
375d5635
JPW
641Calls `update-directory-autoloads' on the command line arguments."
642 (apply 'update-directory-autoloads command-line-args-left)
b59c7256 643 (setq command-line-args-left nil))
0231f2dc
JB
644
645(provide 'autoload)
ffd56f97 646
b7a45ee1 647;; arch-tag: 00244766-98f4-4767-bf42-8a22103441c6
c0274f38 648;;; autoload.el ends here