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