lisp/help.el: Turn ChangeLog comment into source comment.
[emacs.git] / lisp / emacs-lisp / autoload.el
blob044969799b07392fd0366fcc52359ae67b2c9af0
1 ;; autoload.el --- maintain autoloads in loaddefs.el
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 ;; Free Software Foundation, Inc.
7 ;; Author: Roland McGrath <roland@gnu.org>
8 ;; Keywords: maint
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This code helps GNU Emacs maintainers keep the loaddefs.el file up to
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.
32 ;;; Code:
34 (require 'lisp-mode) ;for `doc-string-elt' properties.
35 (require 'help-fns) ;for help-add-fundoc-usage.
36 (eval-when-compile (require 'cl))
38 (defvar generated-autoload-file "loaddefs.el"
39 "*File \\[update-file-autoloads] puts autoloads into.
40 A `.el' file can set this in its local variables section to make its
41 autoloads go somewhere else. The autoload file is assumed to contain a
42 trailer starting with a FormFeed character.")
43 ;;;###autoload
44 (put 'generated-autoload-file 'safe-local-variable 'stringp)
46 (defvar generated-autoload-load-name nil
47 "Load name for `autoload' statements generated from autoload cookies.
48 If nil, this defaults to the file name, sans extension.")
49 ;;;###autoload
50 (put 'generated-autoload-load-name 'safe-local-variable 'stringp)
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"
55 "Magic comment indicating the following form should be autoloaded.
56 Used by \\[update-file-autoloads]. This string should be
57 meaningless to Lisp (e.g., a comment).
59 This string is used:
61 \;;;###autoload
62 \(defun function-to-be-autoloaded () ...)
64 If this string appears alone on a line, the following form will be
65 read and an autoload made for it. If there is further text on the line,
66 that text will be copied verbatim to `generated-autoload-file'.")
68 (defvar autoload-excludes nil
69 "If non-nil, list of absolute file names not to scan for autoloads.")
71 (defconst generate-autoload-section-header "\f\n;;;### "
72 "String that marks the form at the start of a new file's autoload section.")
74 (defconst generate-autoload-section-trailer "\n;;;***\n"
75 "String which indicates the end of the section of autoloads for a file.")
77 (defconst generate-autoload-section-continuation ";;;;;; "
78 "String to add on each continuation of the section header form.")
80 (defvar autoload-modified-buffers) ;Dynamically scoped var.
82 (defun make-autoload (form file)
83 "Turn FORM into an autoload or defvar for source file FILE.
84 Returns nil if FORM is not a special autoload form (i.e. a function definition
85 or macro definition or a defcustom)."
86 (let ((car (car-safe form)) expand)
87 (cond
88 ;; For complex cases, try again on the macro-expansion.
89 ((and (memq car '(easy-mmode-define-global-mode define-global-minor-mode
90 define-globalized-minor-mode
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)))))
102 ;; For special function-like operators, use the `autoload' function.
103 ((memq car '(defun define-skeleton defmacro define-derived-mode
104 define-compilation-mode define-generic-mode
105 easy-mmode-define-global-mode define-global-minor-mode
106 define-globalized-minor-mode
107 easy-mmode-define-minor-mode define-minor-mode
108 defun* defmacro* define-overloadable-function))
109 (let* ((macrop (memq car '(defmacro defmacro*)))
110 (name (nth 1 form))
111 (args (case car
112 ((defun defmacro defun* defmacro*
113 define-overloadable-function) (nth 2 form))
114 ((define-skeleton) '(&optional str arg))
115 ((define-generic-mode define-derived-mode
116 define-compilation-mode) nil)
117 (t)))
118 (body (nthcdr (get car 'doc-string-elt) form))
119 (doc (if (stringp (car body)) (pop body))))
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)))
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
129 define-global-minor-mode
130 define-globalized-minor-mode
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))))
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)))
144 ;; Convert defcustom to less space-consuming data.
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)))))
149 ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
151 `(progn
152 (defvar ,varname ,init ,doc)
153 (custom-autoload ',varname ,file
154 ,(condition-case nil
155 (null (cadr (memq :set form)))
156 (error nil))))))
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))))))
167 ;; nil here indicates that this is not a special autoload form.
168 (t nil))))
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.
175 (defun autoload-generated-file ()
176 (expand-file-name generated-autoload-file
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))))
184 (defun autoload-read-section-header ()
185 "Read a section header form.
186 Since continuation lines have been marked as comments,
187 we must copy the text of the form and remove those comment
188 markers 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))))))
205 (defvar autoload-print-form-outbuf nil
206 "Buffer which gets the output of `autoload-print-form'.")
208 (defun autoload-print-form (form)
209 "Print FORM such that `make-docfile' will find the docstrings.
210 The variable `autoload-print-form-outbuf' specifies the buffer to
211 put the output in."
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)
218 (let ((doc-string-elt (get (car-safe form) 'doc-string-elt))
219 (outbuf autoload-print-form-outbuf))
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)
229 (print-quoted t)
230 (print-escape-nonascii t))
231 (dolist (elt form)
232 (prin1 elt outbuf)
233 (princ " " outbuf)))
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
243 (while (re-search-backward "\n[[(]" begin t)
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)
253 (print-quoted t)
254 (print-escape-nonascii t))
255 (print form outbuf)))))))
257 (defun autoload-rubric (file &optional type feature)
258 "Return a string giving the appropriate autoload rubric for FILE.
259 TYPE (default \"autoloads\") is a string stating the type of
260 information contained in FILE. If FEATURE is non-nil, FILE
261 will provide a feature. FEATURE may be a string naming the
262 feature, otherwise it will be based on FILE's name.
264 At present, a feature is in fact always provided, but this should
265 not be relied upon."
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"
272 ;; This is used outside of autoload.el, eg cus-dep, finder.
273 "(provide '"
274 (if (stringp feature)
275 feature
276 (file-name-sans-extension basename))
277 ")\n"
278 ";; Local Variables:\n"
279 ";; version-control: never\n"
280 ";; no-byte-compile: t\n"
281 ";; no-update-autoloads: t\n"
282 ";; coding: utf-8\n"
283 ";; End:\n"
284 ";;; " basename
285 " ends here\n")))
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)
290 (write-region (autoload-rubric file) nil file))
291 file)
293 (defun autoload-insert-section-header (outbuf autoloads load-name file time)
294 "Insert the section-header line,
295 which lists the file name and which functions are in it, etc."
296 (insert generate-autoload-section-header)
297 (prin1 (list 'autoloads autoloads load-name file time)
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))))))
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.
314 (setq file (expand-file-name file))
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)
321 (setq default-directory (file-name-directory file))
322 (insert-file-contents file nil)
323 (let ((enable-local-variables :safe))
324 (hack-local-variables))
325 (current-buffer)))
327 (defvar no-update-autoloads nil
328 "File local variable to prevent scanning this file for autoload cookies.")
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)))
336 (defun generate-file-autoloads (file)
337 "Insert at point a loaddefs autoload section for FILE.
338 Autoloads are generated for defuns and defmacros in FILE
339 marked by `generate-autoload-cookie' (which see).
340 If FILE is being visited in a buffer, the contents of the buffer
341 are used.
342 Return non-nil in the case where no autoloads were added at point."
343 (interactive "fGenerate autoloads for file: ")
344 (autoload-generate-file-autoloads file (current-buffer)))
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)
353 "Insert an autoload section for FILE in the appropriate buffer.
354 Autoloads are generated for defuns and defmacros in FILE
355 marked by `generate-autoload-cookie' (which see).
356 If FILE is being visited in a buffer, the contents of the buffer are used.
357 OUTBUF is the buffer in which the autoload statements should be inserted.
358 If OUTBUF is nil, it will be determined by `autoload-generated-file'.
360 If provided, OUTFILE is expected to be the file name of OUTBUF.
361 If OUTFILE is non-nil and FILE specifies a `generated-autoload-file'
362 different from OUTFILE, then OUTBUF is ignored.
364 Return non-nil if and only if FILE adds no autoloads to OUTFILE
365 \(or OUTBUF if OUTFILE is nil)."
366 (catch 'done
367 (let ((autoloads-done '())
368 load-name
369 (print-length nil)
370 (print-level nil)
371 (print-readably t) ; This does something in Lucid Emacs.
372 (float-output-format nil)
373 (visited (get-file-buffer file))
374 (otherbuf nil)
375 (absfile (expand-file-name file))
376 relfile
377 ;; nil until we found a cookie.
378 output-start)
379 (with-current-buffer (or visited
380 ;; It is faster to avoid visiting the file.
381 (autoload-find-file file))
382 ;; Obey the no-update-autoloads file local variable.
383 (unless no-update-autoloads
384 (message "Generating autoloads for %s..." file)
385 (setq load-name
386 (if (stringp generated-autoload-load-name)
387 generated-autoload-load-name
388 (autoload-file-load-name file)))
389 (save-excursion
390 (save-restriction
391 (widen)
392 (goto-char (point-min))
393 (while (not (eobp))
394 (skip-chars-forward " \t\n\f")
395 (cond
396 ((looking-at (regexp-quote generate-autoload-cookie))
397 ;; If not done yet, figure out where to insert this text.
398 (unless output-start
399 (when (and outfile
400 (not (equal outfile (autoload-generated-file))))
401 ;; A file-local setting of autoload-generated-file says
402 ;; we should ignore OUTBUF.
403 (setq outbuf nil)
404 (setq otherbuf t))
405 (unless outbuf
406 (setq outbuf (autoload-find-destination absfile))
407 (unless outbuf
408 ;; The file has autoload cookies, but they're
409 ;; already up-to-date. If OUTFILE is nil, the
410 ;; entries are in the expected OUTBUF, otherwise
411 ;; they're elsewhere.
412 (throw 'done outfile)))
413 (with-current-buffer outbuf
414 (setq relfile (file-relative-name absfile))
415 (setq output-start (point)))
416 ;; (message "file=%S, relfile=%S, dest=%S"
417 ;; file relfile (autoload-generated-file))
419 (search-forward generate-autoload-cookie)
420 (skip-chars-forward " \t")
421 (if (eolp)
422 (condition-case err
423 ;; Read the next form and make an autoload.
424 (let* ((form (prog1 (read (current-buffer))
425 (or (bolp) (forward-line 1))))
426 (autoload (make-autoload form load-name)))
427 (if autoload
428 (push (nth 1 form) autoloads-done)
429 (setq autoload form))
430 (let ((autoload-print-form-outbuf outbuf))
431 (autoload-print-form autoload)))
432 (error
433 (message "Error in %s: %S" file err)))
435 ;; Copy the rest of the line to the output.
436 (princ (buffer-substring
437 (progn
438 ;; Back up over whitespace, to preserve it.
439 (skip-chars-backward " \f\t")
440 (if (= (char-after (1+ (point))) ? )
441 ;; Eat one space.
442 (forward-char 1))
443 (point))
444 (progn (forward-line 1) (point)))
445 outbuf)))
446 ((looking-at ";")
447 ;; Don't read the comment.
448 (forward-line 1))
450 (forward-sexp 1)
451 (forward-line 1))))))
453 (when output-start
454 (let ((secondary-autoloads-file-buf
455 (if (local-variable-p 'generated-autoload-file)
456 (current-buffer))))
457 (with-current-buffer outbuf
458 (save-excursion
459 ;; Insert the section-header line which lists the file name
460 ;; and which functions are in it, etc.
461 (goto-char output-start)
462 (autoload-insert-section-header
463 outbuf autoloads-done load-name relfile
464 (if secondary-autoloads-file-buf
465 ;; MD5 checksums are much better because they do not
466 ;; change unless the file changes (so they'll be
467 ;; equal on two different systems and will change
468 ;; less often than time-stamps, thus leading to fewer
469 ;; unneeded changes causing spurious conflicts), but
470 ;; using time-stamps is a very useful optimization,
471 ;; so we use time-stamps for the main autoloads file
472 ;; (loaddefs.el) where we have special ways to
473 ;; circumvent the "random change problem", and MD5
474 ;; checksum in secondary autoload files where we do
475 ;; not need the time-stamp optimization because it is
476 ;; already provided by the primary autoloads file.
477 (md5 secondary-autoloads-file-buf
478 ;; We'd really want to just use
479 ;; `emacs-internal' instead.
480 nil nil 'emacs-mule-unix)
481 (nth 5 (file-attributes relfile))))
482 (insert ";;; Generated autoloads from " relfile "\n"))
483 (insert generate-autoload-section-trailer))))
484 (message "Generating autoloads for %s...done" file))
485 (or visited
486 ;; We created this buffer, so we should kill it.
487 (kill-buffer (current-buffer))))
488 ;; If the entries were added to some other buffer, then the file
489 ;; doesn't add entries to OUTFILE.
490 (or (not output-start) otherbuf))))
492 (defun autoload-save-buffers ()
493 (while autoload-modified-buffers
494 (with-current-buffer (pop autoload-modified-buffers)
495 (save-buffer))))
497 ;;;###autoload
498 (defun update-file-autoloads (file &optional save-after)
499 "Update the autoloads for FILE in `generated-autoload-file'
500 \(which FILE might bind in its local variables).
501 If SAVE-AFTER is non-nil (which is always, when called interactively),
502 save the buffer too.
504 Return FILE if there was no autoload cookie in it, else nil."
505 (interactive "fUpdate autoloads for file: \np")
506 (let* ((autoload-modified-buffers nil)
507 (no-autoloads (autoload-generate-file-autoloads file)))
508 (if autoload-modified-buffers
509 (if save-after (autoload-save-buffers))
510 (if (called-interactively-p 'interactive)
511 (message "Autoload section for %s is up to date." file)))
512 (if no-autoloads file)))
514 (defun autoload-find-destination (file)
515 "Find the destination point of the current buffer's autoloads.
516 FILE is the file name of the current buffer.
517 Returns a buffer whose point is placed at the requested location.
518 Returns nil if the file's autoloads are uptodate, otherwise
519 removes any prior now out-of-date autoload entries."
520 (catch 'up-to-date
521 (let* ((load-name (autoload-file-load-name file))
522 (buf (current-buffer))
523 (existing-buffer (if buffer-file-name buf))
524 (found nil))
525 (with-current-buffer
526 ;; We used to use `raw-text' to read this file, but this causes
527 ;; problems when the file contains non-ASCII characters.
528 (find-file-noselect
529 (autoload-ensure-default-file (autoload-generated-file)))
530 ;; This is to make generated-autoload-file have Unix EOLs, so
531 ;; that it is portable to all platforms.
532 (unless (zerop (coding-system-eol-type buffer-file-coding-system))
533 (set-buffer-file-coding-system 'unix))
534 (or (> (buffer-size) 0)
535 (error "Autoloads file %s does not exist" buffer-file-name))
536 (or (file-writable-p buffer-file-name)
537 (error "Autoloads file %s is not writable" buffer-file-name))
538 (widen)
539 (goto-char (point-min))
540 ;; Look for the section for LOAD-NAME.
541 (while (and (not found)
542 (search-forward generate-autoload-section-header nil t))
543 (let ((form (autoload-read-section-header)))
544 (cond ((string= (nth 2 form) load-name)
545 ;; We found the section for this file.
546 ;; Check if it is up to date.
547 (let ((begin (match-beginning 0))
548 (last-time (nth 4 form))
549 (file-time (nth 5 (file-attributes file))))
550 (if (and (or (null existing-buffer)
551 (not (buffer-modified-p existing-buffer)))
553 ;; last-time is the time-stamp (specifying
554 ;; the last time we looked at the file) and
555 ;; the file hasn't been changed since.
556 (and (listp last-time) (= (length last-time) 2)
557 (not (time-less-p last-time file-time)))
558 ;; last-time is an MD5 checksum instead.
559 (and (stringp last-time)
560 (equal last-time
561 (md5 buf nil nil 'emacs-mule)))))
562 (throw 'up-to-date nil)
563 (autoload-remove-section begin)
564 (setq found t))))
565 ((string< load-name (nth 2 form))
566 ;; We've come to a section alphabetically later than
567 ;; LOAD-NAME. We assume the file is in order and so
568 ;; there must be no section for LOAD-NAME. We will
569 ;; insert one before the section here.
570 (goto-char (match-beginning 0))
571 (setq found t)))))
572 (or found
573 (progn
574 ;; No later sections in the file. Put before the last page.
575 (goto-char (point-max))
576 (search-backward "\f" nil t)))
577 (unless (memq (current-buffer) autoload-modified-buffers)
578 (push (current-buffer) autoload-modified-buffers))
579 (current-buffer)))))
581 (defun autoload-remove-section (begin)
582 (goto-char begin)
583 (search-forward generate-autoload-section-trailer)
584 (delete-region begin (point)))
586 ;;;###autoload
587 (defun update-directory-autoloads (&rest dirs)
589 Update loaddefs.el with all the current autoloads from DIRS, and no old ones.
590 This uses `update-file-autoloads' (which see) to do its work.
591 In an interactive call, you must give one argument, the name
592 of a single directory. In a call from Lisp, you can supply multiple
593 directories as separate arguments, but this usage is discouraged.
595 The function does NOT recursively descend into subdirectories of the
596 directory or directories specified."
597 (interactive "DUpdate autoloads from directory: ")
598 (let* ((files-re (let ((tmp nil))
599 (dolist (suf (get-load-suffixes)
600 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
601 (unless (string-match "\\.elc" suf) (push suf tmp)))))
602 (files (apply 'nconc
603 (mapcar (lambda (dir)
604 (directory-files (expand-file-name dir)
605 t files-re))
606 dirs)))
607 (done ())
608 (this-time (current-time))
609 ;; Files with no autoload cookies or whose autoloads go to other
610 ;; files because of file-local autoload-generated-file settings.
611 (no-autoloads nil)
612 (autoload-modified-buffers nil))
614 (with-current-buffer
615 (find-file-noselect
616 (autoload-ensure-default-file (autoload-generated-file)))
617 (save-excursion
619 ;; Canonicalize file names and remove the autoload file itself.
620 (setq files (delete (file-relative-name buffer-file-name)
621 (mapcar 'file-relative-name files)))
623 (goto-char (point-min))
624 (while (search-forward generate-autoload-section-header nil t)
625 (let* ((form (autoload-read-section-header))
626 (file (nth 3 form)))
627 (cond ((and (consp file) (stringp (car file)))
628 ;; This is a list of files that have no autoload cookies.
629 ;; There shouldn't be more than one such entry.
630 ;; Remove the obsolete section.
631 (autoload-remove-section (match-beginning 0))
632 (let ((last-time (nth 4 form)))
633 (dolist (file file)
634 (let ((file-time (nth 5 (file-attributes file))))
635 (when (and file-time
636 (not (time-less-p last-time file-time)))
637 ;; file unchanged
638 (push file no-autoloads)
639 (setq files (delete file files)))))))
640 ((not (stringp file)))
641 ((or (not (file-exists-p file))
642 ;; Remove duplicates as well, just in case.
643 (member file done)
644 ;; If the file is actually excluded.
645 (member (expand-file-name file) autoload-excludes))
646 ;; Remove the obsolete section.
647 (autoload-remove-section (match-beginning 0)))
648 ((not (time-less-p (nth 4 form)
649 (nth 5 (file-attributes file))))
650 ;; File hasn't changed.
651 nil)
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))))
657 (push file done)
658 (setq files (delete file files)))))
659 ;; Elements remaining in FILES have no existing autoload sections yet.
660 (dolist (file files)
661 (cond
662 ((member (expand-file-name file) autoload-excludes) nil)
663 ((autoload-generate-file-autoloads file nil buffer-file-name)
664 (push file no-autoloads))))
666 (when no-autoloads
667 ;; Sort them for better readability.
668 (setq no-autoloads (sort no-autoloads 'string<))
669 ;; Add the `no-autoloads' section.
670 (goto-char (point-max))
671 (search-backward "\f" nil t)
672 (autoload-insert-section-header
673 (current-buffer) nil nil no-autoloads this-time)
674 (insert generate-autoload-section-trailer))
676 (save-buffer)
677 ;; In case autoload entries were added to other files because of
678 ;; file-local autoload-generated-file settings.
679 (autoload-save-buffers))))
681 (define-obsolete-function-alias 'update-autoloads-from-directories
682 'update-directory-autoloads "22.1")
684 (defvar autoload-make-program (or (getenv "MAKE") "make")
685 "Name of the make program in use during the Emacs build process.")
687 ;;;###autoload
688 (defun batch-update-autoloads ()
689 "Update loaddefs.el autoloads in batch mode.
690 Calls `update-directory-autoloads' on the command line arguments."
691 ;; For use during the Emacs build process only.
692 (unless autoload-excludes
693 (let* ((ldir (file-name-directory generated-autoload-file))
694 (default-directory
695 (file-name-as-directory
696 (expand-file-name (if (eq system-type 'windows-nt)
697 "../lib-src"
698 "../src") ldir)))
699 (mfile "Makefile")
700 (tmpfile "echolisp.tmp")
701 lim)
702 ;; Windows uses the 'echolisp' approach because:
703 ;; i) It does not have $lisp as a single simple definition, so
704 ;; it would be harder to parse the Makefile.
705 ;; ii) It can, since it already has $lisp broken up into pieces
706 ;; that the command-line can handle.
707 ;; Non-Windows builds do not use the 'echolisp' approach because
708 ;; no-one knows (?) the maximum safe command-line length on all
709 ;; supported systems. $lisp is much longer there since it uses
710 ;; absolute paths, and it would seem a shame to split it just for this.
711 (when (file-readable-p mfile)
712 (if (eq system-type 'windows-nt)
713 (when (ignore-errors
714 (if (file-exists-p tmpfile) (delete-file tmpfile))
715 ;; FIXME call-process is better, if it works.
716 (shell-command (format "%s echolisp > %s"
717 autoload-make-program tmpfile))
718 (file-readable-p tmpfile))
719 (with-temp-buffer
720 (insert-file-contents tmpfile)
721 ;; FIXME could be a single while loop.
722 (while (not (eobp))
723 (setq lim (line-end-position))
724 (while (re-search-forward "\\([^ ]+\\.el\\)c?\\>" lim t)
725 (push (expand-file-name (match-string 1))
726 autoload-excludes))
727 (forward-line 1))))
728 (with-temp-buffer
729 (insert-file-contents mfile)
730 (when (re-search-forward "^shortlisp= " nil t)
731 (setq lim (line-end-position))
732 (while (re-search-forward "\\.\\./lisp/\\([^ ]+\\.el\\)c?\\>"
733 lim t)
734 (push (expand-file-name (match-string 1) ldir)
735 autoload-excludes))))))))
736 (let ((args command-line-args-left))
737 (setq command-line-args-left nil)
738 (apply 'update-directory-autoloads args)))
740 (provide 'autoload)
742 ;; arch-tag: 00244766-98f4-4767-bf42-8a22103441c6
743 ;;; autoload.el ends here