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