]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/autoload.el
1f7837ba43ab7cb241a54bf6aab95574e42ab775
[gnu-emacs] / lisp / emacs-lisp / autoload.el
1 ;; autoload.el --- maintain autoloads in loaddefs.el
2
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 ;; Free Software Foundation, Inc.
6
7 ;; Author: Roland McGrath <roland@gnu.org>
8 ;; Keywords: maint
9
10 ;; This file is part of GNU Emacs.
11
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.
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
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
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.
31
32 ;;; Code:
33
34 (require 'lisp-mode) ;for `doc-string-elt' properties.
35 (require 'help-fns) ;for help-add-fundoc-usage.
36 (eval-when-compile (require 'cl))
37
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)
45
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)
51
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).
58
59 This string is used:
60
61 \;;;###autoload
62 \(defun function-to-be-autoloaded () ...)
63
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'.")
67
68 (defvar autoload-excludes nil
69 "If non-nil, list of absolute file names not to scan for autoloads.")
70
71 (defconst generate-autoload-section-header "\f\n;;;### "
72 "String that marks the form at the start of a new file's autoload section.")
73
74 (defconst generate-autoload-section-trailer "\n;;;***\n"
75 "String which indicates the end of the section of autoloads for a file.")
76
77 (defconst generate-autoload-section-continuation ";;;;;; "
78 "String to add on each continuation of the section header form.")
79
80 (defvar autoload-modified-buffers) ;Dynamically scoped var.
81
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)))))
101
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))))
135
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
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)))))
150 )
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))))))
157
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
167 ;; nil here indicates that this is not a special autoload form.
168 (t nil))))
169
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.
174
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))))
182
183
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))))))
204
205 (defvar autoload-print-form-outbuf nil
206 "Buffer which gets the output of `autoload-print-form'.")
207
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)
217 (t
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)))))))
256
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."
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"
269 ;; This is used outside of autoload.el.
270 "(provide '"
271 (if (stringp feature)
272 feature
273 (file-name-sans-extension basename))
274 ")\n"
275 ";; Local Variables:\n"
276 ";; version-control: never\n"
277 ";; no-byte-compile: t\n"
278 ";; no-update-autoloads: t\n"
279 ";; coding: utf-8\n"
280 ";; End:\n"
281 ";;; " basename
282 " ends here\n")))
283
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)
287 (write-region (autoload-rubric file) nil file))
288 file)
289
290 (defun autoload-insert-section-header (outbuf autoloads load-name file time)
291 "Insert the section-header line,
292 which lists the file name and which functions are in it, etc."
293 (insert generate-autoload-section-header)
294 (prin1 (list 'autoloads autoloads load-name file time)
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
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.
311 (setq file (expand-file-name file))
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)
318 (setq default-directory (file-name-directory file))
319 (insert-file-contents file nil)
320 (let ((enable-local-variables :safe))
321 (hack-local-variables))
322 (current-buffer)))
323
324 (defvar no-update-autoloads nil
325 "File local variable to prevent scanning this file for autoload cookies.")
326
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
333 (defun generate-file-autoloads (file)
334 "Insert at point a loaddefs autoload section for FILE.
335 Autoloads are generated for defuns and defmacros in FILE
336 marked by `generate-autoload-cookie' (which see).
337 If FILE is being visited in a buffer, the contents of the buffer
338 are used.
339 Return non-nil in the case where no autoloads were added at point."
340 (interactive "fGenerate autoloads for file: ")
341 (autoload-generate-file-autoloads file (current-buffer)))
342
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)
350 "Insert an autoload section for FILE in the appropriate buffer.
351 Autoloads are generated for defuns and defmacros in FILE
352 marked by `generate-autoload-cookie' (which see).
353 If FILE is being visited in a buffer, the contents of the buffer are used.
354 OUTBUF is the buffer in which the autoload statements should be inserted.
355 If OUTBUF is nil, it will be determined by `autoload-generated-file'.
356
357 If provided, OUTFILE is expected to be the file name of OUTBUF.
358 If OUTFILE is non-nil and FILE specifies a `generated-autoload-file'
359 different from OUTFILE, then OUTBUF is ignored.
360
361 Return non-nil if and only if FILE adds no autoloads to OUTFILE
362 \(or OUTBUF if OUTFILE is nil)."
363 (catch 'done
364 (let ((autoloads-done '())
365 load-name
366 (print-length nil)
367 (print-level nil)
368 (print-readably t) ; This does something in Lucid Emacs.
369 (float-output-format nil)
370 (visited (get-file-buffer file))
371 (otherbuf nil)
372 (absfile (expand-file-name file))
373 relfile
374 ;; nil until we found a cookie.
375 output-start)
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)
382 (setq load-name
383 (if (stringp generated-autoload-load-name)
384 generated-autoload-load-name
385 (autoload-file-load-name file)))
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
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))
402 (unless outbuf
403 (setq outbuf (autoload-find-destination absfile))
404 (unless outbuf
405 ;; The file has autoload cookies, but they're
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)))
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
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.
474 (md5 secondary-autoloads-file-buf
475 ;; We'd really want to just use
476 ;; `emacs-internal' instead.
477 nil nil 'emacs-mule-unix)
478 (nth 5 (file-attributes relfile))))
479 (insert ";;; Generated autoloads from " relfile "\n"))
480 (insert generate-autoload-section-trailer))))
481 (message "Generating autoloads for %s...done" file))
482 (or visited
483 ;; We created this buffer, so we should kill it.
484 (kill-buffer (current-buffer))))
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))))
488 \f
489 (defun autoload-save-buffers ()
490 (while autoload-modified-buffers
491 (with-current-buffer (pop autoload-modified-buffers)
492 (save-buffer))))
493
494 ;;;###autoload
495 (defun update-file-autoloads (file &optional save-after)
496 "Update the autoloads for FILE in `generated-autoload-file'
497 \(which FILE might bind in its local variables).
498 If SAVE-AFTER is non-nil (which is always, when called interactively),
499 save the buffer too.
500
501 Return FILE if there was no autoload cookie in it, else nil."
502 (interactive "fUpdate autoloads for file: \np")
503 (let* ((autoload-modified-buffers nil)
504 (no-autoloads (autoload-generate-file-autoloads file)))
505 (if autoload-modified-buffers
506 (if save-after (autoload-save-buffers))
507 (if (called-interactively-p 'interactive)
508 (message "Autoload section for %s is up to date." file)))
509 (if no-autoloads file)))
510
511 (defun autoload-find-destination (file)
512 "Find the destination point of the current buffer's autoloads.
513 FILE is the file name of the current buffer.
514 Returns a buffer whose point is placed at the requested location.
515 Returns nil if the file's autoloads are uptodate, otherwise
516 removes any prior now out-of-date autoload entries."
517 (catch 'up-to-date
518 (let* ((load-name (autoload-file-load-name file))
519 (buf (current-buffer))
520 (existing-buffer (if buffer-file-name buf))
521 (found nil))
522 (with-current-buffer
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)))
527 ;; This is to make generated-autoload-file have Unix EOLs, so
528 ;; that it is portable to all platforms.
529 (or (eq 0 (coding-system-eol-type buffer-file-coding-system))
530 (set-buffer-file-coding-system 'unix))
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)))
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)))))
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)))))
577
578 (defun autoload-remove-section (begin)
579 (goto-char begin)
580 (search-forward generate-autoload-section-trailer)
581 (delete-region begin (point)))
582
583 ;;;###autoload
584 (defun update-directory-autoloads (&rest dirs)
585 "\
586 Update loaddefs.el with all the current autoloads from DIRS, and no old ones.
587 This uses `update-file-autoloads' (which see) to do its work.
588 In an interactive call, you must give one argument, the name
589 of a single directory. In a call from Lisp, you can supply multiple
590 directories as separate arguments, but this usage is discouraged.
591
592 The function does NOT recursively descend into subdirectories of the
593 directory or directories specified."
594 (interactive "DUpdate autoloads from directory: ")
595 (let* ((files-re (let ((tmp nil))
596 (dolist (suf (get-load-suffixes)
597 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
598 (unless (string-match "\\.elc" suf) (push suf tmp)))))
599 (files (apply 'nconc
600 (mapcar (lambda (dir)
601 (directory-files (expand-file-name dir)
602 t files-re))
603 dirs)))
604 (done ())
605 (this-time (current-time))
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))
610
611 (with-current-buffer
612 (find-file-noselect
613 (autoload-ensure-default-file (autoload-generated-file)))
614 (save-excursion
615
616 ;; Canonicalize file names and remove the autoload file itself.
617 (setq files (delete (file-relative-name buffer-file-name)
618 (mapcar 'file-relative-name files)))
619
620 (goto-char (point-min))
621 (while (search-forward generate-autoload-section-header nil t)
622 (let* ((form (autoload-read-section-header))
623 (file (nth 3 form)))
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
633 (not (time-less-p last-time file-time)))
634 ;; file unchanged
635 (push file no-autoloads)
636 (setq files (delete file files)))))))
637 ((not (stringp file)))
638 ((or (not (file-exists-p file))
639 ;; Remove duplicates as well, just in case.
640 (member file done)
641 ;; If the file is actually excluded.
642 (member (expand-file-name file) autoload-excludes))
643 ;; Remove the obsolete section.
644 (autoload-remove-section (match-beginning 0)))
645 ((not (time-less-p (nth 4 form)
646 (nth 5 (file-attributes file))))
647 ;; File hasn't changed.
648 nil)
649 (t
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))))
654 (push file done)
655 (setq files (delete file files)))))
656 ;; Elements remaining in FILES have no existing autoload sections yet.
657 (dolist (file files)
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))))
662
663 (when no-autoloads
664 ;; Sort them for better readability.
665 (setq no-autoloads (sort no-autoloads 'string<))
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
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))))
677
678 (define-obsolete-function-alias 'update-autoloads-from-directories
679 'update-directory-autoloads "22.1")
680
681 (defvar autoload-make-program (or (getenv "MAKE") "make")
682 "Name of the make program in use during the Emacs build process.")
683
684 ;;;###autoload
685 (defun batch-update-autoloads ()
686 "Update loaddefs.el autoloads in batch mode.
687 Calls `update-directory-autoloads' on the command line arguments."
688 ;; For use during the Emacs build process only.
689 (unless autoload-excludes
690 (let* ((ldir (file-name-directory generated-autoload-file))
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")
698 lim)
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.
708 (when (file-readable-p mfile)
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)
727 (when (re-search-forward "^shortlisp= " nil t)
728 (setq lim (line-end-position))
729 (while (re-search-forward "\\.\\./lisp/\\([^ ]+\\.el\\)c?\\>"
730 lim t)
731 (push (expand-file-name (match-string 1) ldir)
732 autoload-excludes))))))))
733 (let ((args command-line-args-left))
734 (setq command-line-args-left nil)
735 (apply 'update-directory-autoloads args)))
736
737 (provide 'autoload)
738
739 ;;; autoload.el ends here