Merge from emacs-23; up to 2010-06-22T07:41:10Z!rgm@gnu.org
[emacs.git] / lisp / emacs-lisp / autoload.el
blob6d5067151d32e1425d6178e55700fcad1c945106
1 ;; autoload.el --- maintain autoloads in loaddefs.el
3 ;; Copyright (C) 1991-1997, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Roland McGrath <roland@gnu.org>
6 ;; Keywords: maint
7 ;; Package: emacs
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This code helps GNU Emacs maintainers keep the loaddefs.el file up to
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.
31 ;;; Code:
33 (require 'lisp-mode) ;for `doc-string-elt' properties.
34 (require 'help-fns) ;for help-add-fundoc-usage.
35 (eval-when-compile (require 'cl))
37 (defvar generated-autoload-file nil
38 "File into which to write autoload definitions.
39 A Lisp file can set this in its local variables section to make
40 its autoloads go somewhere else.
42 If this is a relative file name, the directory is determined as
43 follows:
44 - If a Lisp file defined `generated-autoload-file' as a
45 file-local variable, use its containing directory.
46 - Otherwise use the \"lisp\" subdirectory of `source-directory'.
48 The autoload file is assumed to contain a trailer starting with a
49 FormFeed character.")
50 ;;;###autoload
51 (put 'generated-autoload-file 'safe-local-variable 'stringp)
53 (defvar generated-autoload-load-name nil
54 "Load name for `autoload' statements generated from autoload cookies.
55 If nil, this defaults to the file name, sans extension.")
56 ;;;###autoload
57 (put 'generated-autoload-load-name 'safe-local-variable 'stringp)
59 ;; This feels like it should be a defconst, but MH-E sets it to
60 ;; ";;;###mh-autoload" for the autoloads that are to go into mh-loaddefs.el.
61 (defvar generate-autoload-cookie ";;;###autoload"
62 "Magic comment indicating the following form should be autoloaded.
63 Used by \\[update-file-autoloads]. This string should be
64 meaningless to Lisp (e.g., a comment).
66 This string is used:
68 \;;;###autoload
69 \(defun function-to-be-autoloaded () ...)
71 If this string appears alone on a line, the following form will be
72 read and an autoload made for it. If there is further text on the line,
73 that text will be copied verbatim to `generated-autoload-file'.")
75 (defvar autoload-excludes nil
76 "If non-nil, list of absolute file names not to scan for autoloads.")
78 (defconst generate-autoload-section-header "\f\n;;;### "
79 "String that marks the form at the start of a new file's autoload section.")
81 (defconst generate-autoload-section-trailer "\n;;;***\n"
82 "String which indicates the end of the section of autoloads for a file.")
84 (defconst generate-autoload-section-continuation ";;;;;; "
85 "String to add on each continuation of the section header form.")
87 (defvar autoload-modified-buffers) ;Dynamically scoped var.
89 (defun make-autoload (form file)
90 "Turn FORM into an autoload or defvar for source file FILE.
91 Returns nil if FORM is not a special autoload form (i.e. a function definition
92 or macro definition or a defcustom)."
93 (let ((car (car-safe form)) expand)
94 (cond
95 ;; For complex cases, try again on the macro-expansion.
96 ((and (memq car '(easy-mmode-define-global-mode define-global-minor-mode
97 define-globalized-minor-mode
98 easy-mmode-define-minor-mode define-minor-mode))
99 (setq expand (let ((load-file-name file)) (macroexpand form)))
100 (eq (car expand) 'progn)
101 (memq :autoload-end expand))
102 (let ((end (memq :autoload-end expand)))
103 ;; Cut-off anything after the :autoload-end marker.
104 (setcdr end nil)
105 (cons 'progn
106 (mapcar (lambda (form) (make-autoload form file))
107 (cdr expand)))))
109 ;; For special function-like operators, use the `autoload' function.
110 ((memq car '(defun define-skeleton defmacro define-derived-mode
111 define-compilation-mode define-generic-mode
112 easy-mmode-define-global-mode define-global-minor-mode
113 define-globalized-minor-mode
114 easy-mmode-define-minor-mode define-minor-mode
115 defun* defmacro* define-overloadable-function))
116 (let* ((macrop (memq car '(defmacro defmacro*)))
117 (name (nth 1 form))
118 (args (case car
119 ((defun defmacro defun* defmacro*
120 define-overloadable-function) (nth 2 form))
121 ((define-skeleton) '(&optional str arg))
122 ((define-generic-mode define-derived-mode
123 define-compilation-mode) nil)
124 (t)))
125 (body (nthcdr (get car 'doc-string-elt) form))
126 (doc (if (stringp (car body)) (pop body))))
127 (when (listp args)
128 ;; Add the usage form at the end where describe-function-1
129 ;; can recover it.
130 (setq doc (help-add-fundoc-usage doc args)))
131 (let ((exp
132 ;; `define-generic-mode' quotes the name, so take care of that
133 (list 'autoload (if (listp name) name (list 'quote name))
134 file doc
135 (or (and (memq car '(define-skeleton define-derived-mode
136 define-generic-mode
137 easy-mmode-define-global-mode
138 define-global-minor-mode
139 define-globalized-minor-mode
140 easy-mmode-define-minor-mode
141 define-minor-mode)) t)
142 (eq (car-safe (car body)) 'interactive))
143 (if macrop (list 'quote 'macro) nil))))
144 (when macrop
145 ;; Special case to autoload some of the macro's declarations.
146 (let ((decls (nth (if (stringp (nth 3 form)) 4 3) form))
147 (exps '()))
148 (when (eq (car-safe decls) 'declare)
149 ;; FIXME: We'd like to reuse macro-declaration-function,
150 ;; but we can't since it doesn't return anything.
151 (dolist (decl decls)
152 (case (car-safe decl)
153 (indent
154 (push `(put ',name 'lisp-indent-function ',(cadr decl))
155 exps))
156 (doc-string
157 (push `(put ',name 'doc-string-elt ',(cadr decl)) exps))))
158 (when exps
159 (setq exp `(progn ,exp ,@exps))))))
160 exp)))
162 ;; For defclass forms, use `eieio-defclass-autoload'.
163 ((eq car 'defclass)
164 (let ((name (nth 1 form))
165 (superclasses (nth 2 form))
166 (doc (nth 4 form)))
167 (list 'eieio-defclass-autoload (list 'quote name)
168 (list 'quote superclasses) file doc)))
170 ;; Convert defcustom to less space-consuming data.
171 ((eq car 'defcustom)
172 (let ((varname (car-safe (cdr-safe form)))
173 (init (car-safe (cdr-safe (cdr-safe form))))
174 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form)))))
175 ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
177 `(progn
178 (defvar ,varname ,init ,doc)
179 (custom-autoload ',varname ,file
180 ,(condition-case nil
181 (null (cadr (memq :set form)))
182 (error nil))))))
184 ((eq car 'defgroup)
185 ;; In Emacs this is normally handled separately by cus-dep.el, but for
186 ;; third party packages, it can be convenient to explicitly autoload
187 ;; a group.
188 (let ((groupname (nth 1 form)))
189 `(let ((loads (get ',groupname 'custom-loads)))
190 (if (member ',file loads) nil
191 (put ',groupname 'custom-loads (cons ',file loads))))))
193 ;; nil here indicates that this is not a special autoload form.
194 (t nil))))
196 ;; Forms which have doc-strings which should be printed specially.
197 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
198 ;; the doc-string in FORM.
199 ;; Those properties are now set in lisp-mode.el.
201 (defun autoload-find-generated-file ()
202 "Visit the autoload file for the current buffer, and return its buffer.
203 If a buffer is visiting the desired autoload file, return it."
204 (let ((enable-local-variables :safe))
205 ;; We used to use `raw-text' to read this file, but this causes
206 ;; problems when the file contains non-ASCII characters.
207 (find-file-noselect
208 (autoload-ensure-default-file (autoload-generated-file)))))
210 (defun autoload-generated-file ()
211 (expand-file-name generated-autoload-file
212 ;; File-local settings of generated-autoload-file should
213 ;; be interpreted relative to the file's location,
214 ;; of course.
215 (if (not (local-variable-p 'generated-autoload-file))
216 (expand-file-name "lisp" source-directory))))
219 (defun autoload-read-section-header ()
220 "Read a section header form.
221 Since continuation lines have been marked as comments,
222 we must copy the text of the form and remove those comment
223 markers before we call `read'."
224 (save-match-data
225 (let ((beginning (point))
226 string)
227 (forward-line 1)
228 (while (looking-at generate-autoload-section-continuation)
229 (forward-line 1))
230 (setq string (buffer-substring beginning (point)))
231 (with-current-buffer (get-buffer-create " *autoload*")
232 (erase-buffer)
233 (insert string)
234 (goto-char (point-min))
235 (while (search-forward generate-autoload-section-continuation nil t)
236 (replace-match " "))
237 (goto-char (point-min))
238 (read (current-buffer))))))
240 (defvar autoload-print-form-outbuf nil
241 "Buffer which gets the output of `autoload-print-form'.")
243 (defun autoload-print-form (form)
244 "Print FORM such that `make-docfile' will find the docstrings.
245 The variable `autoload-print-form-outbuf' specifies the buffer to
246 put the output in."
247 (cond
248 ;; If the form is a sequence, recurse.
249 ((eq (car form) 'progn) (mapcar 'autoload-print-form (cdr form)))
250 ;; Symbols at the toplevel are meaningless.
251 ((symbolp form) nil)
253 (let ((doc-string-elt (get (car-safe form) 'doc-string-elt))
254 (outbuf autoload-print-form-outbuf))
255 (if (and doc-string-elt (stringp (nth doc-string-elt form)))
256 ;; We need to hack the printing because the
257 ;; doc-string must be printed specially for
258 ;; make-docfile (sigh).
259 (let* ((p (nthcdr (1- doc-string-elt) form))
260 (elt (cdr p)))
261 (setcdr p nil)
262 (princ "\n(" outbuf)
263 (let ((print-escape-newlines t)
264 (print-quoted t)
265 (print-escape-nonascii t))
266 (dolist (elt form)
267 (prin1 elt outbuf)
268 (princ " " outbuf)))
269 (princ "\"\\\n" outbuf)
270 (let ((begin (with-current-buffer outbuf (point))))
271 (princ (substring (prin1-to-string (car elt)) 1)
272 outbuf)
273 ;; Insert a backslash before each ( that
274 ;; appears at the beginning of a line in
275 ;; the doc string.
276 (with-current-buffer outbuf
277 (save-excursion
278 (while (re-search-backward "\n[[(]" begin t)
279 (forward-char 1)
280 (insert "\\"))))
281 (if (null (cdr elt))
282 (princ ")" outbuf)
283 (princ " " outbuf)
284 (princ (substring (prin1-to-string (cdr elt)) 1)
285 outbuf))
286 (terpri outbuf)))
287 (let ((print-escape-newlines t)
288 (print-quoted t)
289 (print-escape-nonascii t))
290 (print form outbuf)))))))
292 (defun autoload-rubric (file &optional type feature)
293 "Return a string giving the appropriate autoload rubric for FILE.
294 TYPE (default \"autoloads\") is a string stating the type of
295 information contained in FILE. If FEATURE is non-nil, FILE
296 will provide a feature. FEATURE may be a string naming the
297 feature, otherwise it will be based on FILE's name.
299 At present, a feature is in fact always provided, but this should
300 not be relied upon."
301 (let ((basename (file-name-nondirectory file)))
302 (concat ";;; " basename
303 " --- automatically extracted " (or type "autoloads") "\n"
304 ";;\n"
305 ";;; Code:\n\n"
306 "\f\n"
307 ;; This is used outside of autoload.el, eg cus-dep, finder.
308 "(provide '"
309 (if (stringp feature)
310 feature
311 (file-name-sans-extension basename))
312 ")\n"
313 ";; Local Variables:\n"
314 ";; version-control: never\n"
315 ";; no-byte-compile: t\n"
316 ";; no-update-autoloads: t\n"
317 ";; coding: utf-8\n"
318 ";; End:\n"
319 ";;; " basename
320 " ends here\n")))
322 (defun autoload-ensure-default-file (file)
323 "Make sure that the autoload file FILE exists and if not create it."
324 (unless (file-exists-p file)
325 (write-region (autoload-rubric file) nil file))
326 file)
328 (defun autoload-insert-section-header (outbuf autoloads load-name file time)
329 "Insert the section-header line,
330 which lists the file name and which functions are in it, etc."
331 (insert generate-autoload-section-header)
332 (prin1 (list 'autoloads autoloads load-name file time)
333 outbuf)
334 (terpri outbuf)
335 ;; Break that line at spaces, to avoid very long lines.
336 ;; Make each sub-line into a comment.
337 (with-current-buffer outbuf
338 (save-excursion
339 (forward-line -1)
340 (while (not (eolp))
341 (move-to-column 64)
342 (skip-chars-forward "^ \n")
343 (or (eolp)
344 (insert "\n" generate-autoload-section-continuation))))))
346 (defun autoload-find-file (file)
347 "Fetch file and put it in a temp buffer. Return the buffer."
348 ;; It is faster to avoid visiting the file.
349 (setq file (expand-file-name file))
350 (with-current-buffer (get-buffer-create " *autoload-file*")
351 (kill-all-local-variables)
352 (erase-buffer)
353 (setq buffer-undo-list t
354 buffer-read-only nil)
355 (emacs-lisp-mode)
356 (setq default-directory (file-name-directory file))
357 (insert-file-contents file nil)
358 (let ((enable-local-variables :safe))
359 (hack-local-variables))
360 (current-buffer)))
362 (defvar no-update-autoloads nil
363 "File local variable to prevent scanning this file for autoload cookies.")
365 (defun autoload-file-load-name (file)
366 "Compute the name that will be used to load FILE."
367 ;; OUTFILE should be the name of the global loaddefs.el file, which
368 ;; is expected to be at the root directory of the files we're
369 ;; scanning for autoloads and will be in the `load-path'.
370 (let* ((outfile (default-value 'generated-autoload-file))
371 (name (file-relative-name file (file-name-directory outfile)))
372 (names '())
373 (dir (file-name-directory outfile)))
374 ;; If `name' has directory components, only keep the
375 ;; last few that are really needed.
376 (while name
377 (setq name (directory-file-name name))
378 (push (file-name-nondirectory name) names)
379 (setq name (file-name-directory name)))
380 (while (not name)
381 (cond
382 ((null (cdr names)) (setq name (car names)))
383 ((file-exists-p (expand-file-name "subdirs.el" dir))
384 ;; FIXME: here we only check the existence of subdirs.el,
385 ;; without checking its content. This makes it generate wrong load
386 ;; names for cases like lisp/term which is not added to load-path.
387 (setq dir (expand-file-name (pop names) dir)))
388 (t (setq name (mapconcat 'identity names "/")))))
389 (if (string-match "\\.elc?\\(\\.\\|\\'\\)" name)
390 (substring name 0 (match-beginning 0))
391 name)))
393 (defun generate-file-autoloads (file)
394 "Insert at point a loaddefs autoload section for FILE.
395 Autoloads are generated for defuns and defmacros in FILE
396 marked by `generate-autoload-cookie' (which see).
397 If FILE is being visited in a buffer, the contents of the buffer
398 are used.
399 Return non-nil in the case where no autoloads were added at point."
400 (interactive "fGenerate autoloads for file: ")
401 (let ((generated-autoload-file buffer-file-name))
402 (autoload-generate-file-autoloads file (current-buffer))))
404 (defvar print-readably)
406 ;; When called from `generate-file-autoloads' we should ignore
407 ;; `generated-autoload-file' altogether. When called from
408 ;; `update-file-autoloads' we don't know `outbuf'. And when called from
409 ;; `update-directory-autoloads' it's in between: we know the default
410 ;; `outbuf' but we should obey any file-local setting of
411 ;; `generated-autoload-file'.
412 (defun autoload-generate-file-autoloads (file &optional outbuf outfile)
413 "Insert an autoload section for FILE in the appropriate buffer.
414 Autoloads are generated for defuns and defmacros in FILE
415 marked by `generate-autoload-cookie' (which see).
416 If FILE is being visited in a buffer, the contents of the buffer are used.
417 OUTBUF is the buffer in which the autoload statements should be inserted.
418 If OUTBUF is nil, it will be determined by `autoload-generated-file'.
420 If provided, OUTFILE is expected to be the file name of OUTBUF.
421 If OUTFILE is non-nil and FILE specifies a `generated-autoload-file'
422 different from OUTFILE, then OUTBUF is ignored.
424 Return non-nil if and only if FILE adds no autoloads to OUTFILE
425 \(or OUTBUF if OUTFILE is nil)."
426 (catch 'done
427 (let ((autoloads-done '())
428 load-name
429 (print-length nil)
430 (print-level nil)
431 (print-readably t) ; This does something in Lucid Emacs.
432 (float-output-format nil)
433 (visited (get-file-buffer file))
434 (otherbuf nil)
435 (absfile (expand-file-name file))
436 ;; nil until we found a cookie.
437 output-start ostart)
438 (with-current-buffer (or visited
439 ;; It is faster to avoid visiting the file.
440 (autoload-find-file file))
441 ;; Obey the no-update-autoloads file local variable.
442 (unless no-update-autoloads
443 (message "Generating autoloads for %s..." file)
444 (setq load-name
445 (if (stringp generated-autoload-load-name)
446 generated-autoload-load-name
447 (autoload-file-load-name absfile)))
448 (when (and outfile
449 (not (equal outfile (autoload-generated-file))))
450 (setq otherbuf t))
451 (save-excursion
452 (save-restriction
453 (widen)
454 (goto-char (point-min))
455 (while (not (eobp))
456 (skip-chars-forward " \t\n\f")
457 (cond
458 ((looking-at (regexp-quote generate-autoload-cookie))
459 ;; If not done yet, figure out where to insert this text.
460 (unless output-start
461 (let ((outbuf
462 (or (if otherbuf
463 ;; A file-local setting of
464 ;; autoload-generated-file says we
465 ;; should ignore OUTBUF.
467 outbuf)
468 (autoload-find-destination absfile load-name)
469 ;; The file has autoload cookies, but they're
470 ;; already up-to-date. If OUTFILE is nil, the
471 ;; entries are in the expected OUTBUF,
472 ;; otherwise they're elsewhere.
473 (throw 'done otherbuf))))
474 (with-current-buffer outbuf
475 (setq output-start (point-marker)
476 ostart (point)))))
477 (search-forward generate-autoload-cookie)
478 (skip-chars-forward " \t")
479 (if (eolp)
480 (condition-case err
481 ;; Read the next form and make an autoload.
482 (let* ((form (prog1 (read (current-buffer))
483 (or (bolp) (forward-line 1))))
484 (autoload (make-autoload form load-name)))
485 (if autoload
486 (push (nth 1 form) autoloads-done)
487 (setq autoload form))
488 (let ((autoload-print-form-outbuf
489 (marker-buffer output-start)))
490 (autoload-print-form autoload)))
491 (error
492 (message "Autoload cookie error in %s:%s %S"
493 file (count-lines (point-min) (point)) err)))
495 ;; Copy the rest of the line to the output.
496 (princ (buffer-substring
497 (progn
498 ;; Back up over whitespace, to preserve it.
499 (skip-chars-backward " \f\t")
500 (if (= (char-after (1+ (point))) ? )
501 ;; Eat one space.
502 (forward-char 1))
503 (point))
504 (progn (forward-line 1) (point)))
505 (marker-buffer output-start))))
506 ((looking-at ";")
507 ;; Don't read the comment.
508 (forward-line 1))
510 (forward-sexp 1)
511 (forward-line 1))))))
513 (when output-start
514 (let ((secondary-autoloads-file-buf
515 (if (local-variable-p 'generated-autoload-file)
516 (current-buffer))))
517 (with-current-buffer (marker-buffer output-start)
518 (save-excursion
519 ;; Insert the section-header line which lists the file name
520 ;; and which functions are in it, etc.
521 (assert (= ostart output-start))
522 (goto-char output-start)
523 (let ((relfile (file-relative-name absfile)))
524 (autoload-insert-section-header
525 (marker-buffer output-start)
526 autoloads-done load-name relfile
527 (if secondary-autoloads-file-buf
528 ;; MD5 checksums are much better because they do not
529 ;; change unless the file changes (so they'll be
530 ;; equal on two different systems and will change
531 ;; less often than time-stamps, thus leading to fewer
532 ;; unneeded changes causing spurious conflicts), but
533 ;; using time-stamps is a very useful optimization,
534 ;; so we use time-stamps for the main autoloads file
535 ;; (loaddefs.el) where we have special ways to
536 ;; circumvent the "random change problem", and MD5
537 ;; checksum in secondary autoload files where we do
538 ;; not need the time-stamp optimization because it is
539 ;; already provided by the primary autoloads file.
540 (md5 secondary-autoloads-file-buf
541 ;; We'd really want to just use
542 ;; `emacs-internal' instead.
543 nil nil 'emacs-mule-unix)
544 (nth 5 (file-attributes relfile))))
545 (insert ";;; Generated autoloads from " relfile "\n")))
546 (insert generate-autoload-section-trailer))))
547 (message "Generating autoloads for %s...done" file))
548 (or visited
549 ;; We created this buffer, so we should kill it.
550 (kill-buffer (current-buffer))))
551 (or (not output-start)
552 ;; If the entries were added to some other buffer, then the file
553 ;; doesn't add entries to OUTFILE.
554 otherbuf))))
556 (defun autoload-save-buffers ()
557 (while autoload-modified-buffers
558 (with-current-buffer (pop autoload-modified-buffers)
559 (let ((version-control 'never))
560 (save-buffer)))))
562 ;;;###autoload
563 (defun update-file-autoloads (file &optional save-after outfile)
564 "Update the autoloads for FILE.
565 If prefix arg SAVE-AFTER is non-nil, save the buffer too.
567 If FILE binds `generated-autoload-file' as a file-local variable,
568 autoloads are written into that file. Otherwise, the autoloads
569 file is determined by OUTFILE. If called interactively, prompt
570 for OUTFILE; if called from Lisp with OUTFILE nil, use the
571 existing value of `generated-autoload-file'.
573 Return FILE if there was no autoload cookie in it, else nil."
574 (interactive (list (read-file-name "Update autoloads for file: ")
575 current-prefix-arg
576 (read-file-name "Write autoload definitions to file: ")))
577 (let* ((generated-autoload-file (or outfile generated-autoload-file))
578 (autoload-modified-buffers nil)
579 (no-autoloads (autoload-generate-file-autoloads file)))
580 (if autoload-modified-buffers
581 (if save-after (autoload-save-buffers))
582 (if (called-interactively-p 'interactive)
583 (message "Autoload section for %s is up to date." file)))
584 (if no-autoloads file)))
586 (defun autoload-find-destination (file load-name)
587 "Find the destination point of the current buffer's autoloads.
588 FILE is the file name of the current buffer.
589 Returns a buffer whose point is placed at the requested location.
590 Returns nil if the file's autoloads are uptodate, otherwise
591 removes any prior now out-of-date autoload entries."
592 (catch 'up-to-date
593 (let* ((buf (current-buffer))
594 (existing-buffer (if buffer-file-name buf))
595 (found nil))
596 (with-current-buffer (autoload-find-generated-file)
597 ;; This is to make generated-autoload-file have Unix EOLs, so
598 ;; that it is portable to all platforms.
599 (or (eq 0 (coding-system-eol-type buffer-file-coding-system))
600 (set-buffer-file-coding-system 'unix))
601 (or (> (buffer-size) 0)
602 (error "Autoloads file %s lacks boilerplate" buffer-file-name))
603 (or (file-writable-p buffer-file-name)
604 (error "Autoloads file %s is not writable" buffer-file-name))
605 (widen)
606 (goto-char (point-min))
607 ;; Look for the section for LOAD-NAME.
608 (while (and (not found)
609 (search-forward generate-autoload-section-header nil t))
610 (let ((form (autoload-read-section-header)))
611 (cond ((string= (nth 2 form) load-name)
612 ;; We found the section for this file.
613 ;; Check if it is up to date.
614 (let ((begin (match-beginning 0))
615 (last-time (nth 4 form))
616 (file-time (nth 5 (file-attributes file))))
617 (if (and (or (null existing-buffer)
618 (not (buffer-modified-p existing-buffer)))
620 ;; last-time is the time-stamp (specifying
621 ;; the last time we looked at the file) and
622 ;; the file hasn't been changed since.
623 (and (listp last-time) (= (length last-time) 2)
624 (not (time-less-p last-time file-time)))
625 ;; last-time is an MD5 checksum instead.
626 (and (stringp last-time)
627 (equal last-time
628 (md5 buf nil nil 'emacs-mule)))))
629 (throw 'up-to-date nil)
630 (autoload-remove-section begin)
631 (setq found t))))
632 ((string< load-name (nth 2 form))
633 ;; We've come to a section alphabetically later than
634 ;; LOAD-NAME. We assume the file is in order and so
635 ;; there must be no section for LOAD-NAME. We will
636 ;; insert one before the section here.
637 (goto-char (match-beginning 0))
638 (setq found t)))))
639 (or found
640 (progn
641 ;; No later sections in the file. Put before the last page.
642 (goto-char (point-max))
643 (search-backward "\f" nil t)))
644 (unless (memq (current-buffer) autoload-modified-buffers)
645 (push (current-buffer) autoload-modified-buffers))
646 (current-buffer)))))
648 (defun autoload-remove-section (begin)
649 (goto-char begin)
650 (search-forward generate-autoload-section-trailer)
651 (delete-region begin (point)))
653 ;;;###autoload
654 (defun update-directory-autoloads (&rest dirs)
655 "Update autoload definitions for Lisp files in the directories DIRS.
656 In an interactive call, you must give one argument, the name of a
657 single directory. In a call from Lisp, you can supply multiple
658 directories as separate arguments, but this usage is discouraged.
660 The function does NOT recursively descend into subdirectories of the
661 directory or directories specified.
663 In an interactive call, prompt for a default output file for the
664 autoload definitions, and temporarily bind the variable
665 `generated-autoload-file' to this value. When called from Lisp,
666 use the existing value of `generated-autoload-file'. If any Lisp
667 file binds `generated-autoload-file' as a file-local variable,
668 write its autoloads into the specified file instead."
669 (interactive "DUpdate autoloads from directory: ")
670 (let* ((files-re (let ((tmp nil))
671 (dolist (suf (get-load-suffixes)
672 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
673 (unless (string-match "\\.elc" suf) (push suf tmp)))))
674 (files (apply 'nconc
675 (mapcar (lambda (dir)
676 (directory-files (expand-file-name dir)
677 t files-re))
678 dirs)))
679 (done ())
680 (this-time (current-time))
681 ;; Files with no autoload cookies or whose autoloads go to other
682 ;; files because of file-local autoload-generated-file settings.
683 (no-autoloads nil)
684 (autoload-modified-buffers nil)
685 (generated-autoload-file
686 (if (called-interactively-p 'interactive)
687 (read-file-name "Write autoload definitions to file: ")
688 generated-autoload-file)))
690 (with-current-buffer (autoload-find-generated-file)
691 (save-excursion
692 ;; Canonicalize file names and remove the autoload file itself.
693 (setq files (delete (file-relative-name buffer-file-name)
694 (mapcar 'file-relative-name files)))
696 (goto-char (point-min))
697 (while (search-forward generate-autoload-section-header nil t)
698 (let* ((form (autoload-read-section-header))
699 (file (nth 3 form)))
700 (cond ((and (consp file) (stringp (car file)))
701 ;; This is a list of files that have no autoload cookies.
702 ;; There shouldn't be more than one such entry.
703 ;; Remove the obsolete section.
704 (autoload-remove-section (match-beginning 0))
705 (let ((last-time (nth 4 form)))
706 (dolist (file file)
707 (let ((file-time (nth 5 (file-attributes file))))
708 (when (and file-time
709 (not (time-less-p last-time file-time)))
710 ;; file unchanged
711 (push file no-autoloads)
712 (setq files (delete file files)))))))
713 ((not (stringp file)))
714 ((or (not (file-exists-p file))
715 ;; Remove duplicates as well, just in case.
716 (member file done)
717 ;; If the file is actually excluded.
718 (member (expand-file-name file) autoload-excludes))
719 ;; Remove the obsolete section.
720 (autoload-remove-section (match-beginning 0)))
721 ((not (time-less-p (nth 4 form)
722 (nth 5 (file-attributes file))))
723 ;; File hasn't changed.
724 nil)
726 (autoload-remove-section (match-beginning 0))
727 (if (autoload-generate-file-autoloads
728 ;; Passing `current-buffer' makes it insert at point.
729 file (current-buffer) buffer-file-name)
730 (push file no-autoloads))))
731 (push file done)
732 (setq files (delete file files)))))
733 ;; Elements remaining in FILES have no existing autoload sections yet.
734 (dolist (file files)
735 (cond
736 ((member (expand-file-name file) autoload-excludes) nil)
737 ;; Passing nil as second argument forces
738 ;; autoload-generate-file-autoloads to look for the right
739 ;; spot where to insert each autoloads section.
740 ((autoload-generate-file-autoloads file nil buffer-file-name)
741 (push file no-autoloads))))
743 (when no-autoloads
744 ;; Sort them for better readability.
745 (setq no-autoloads (sort no-autoloads 'string<))
746 ;; Add the `no-autoloads' section.
747 (goto-char (point-max))
748 (search-backward "\f" nil t)
749 (autoload-insert-section-header
750 (current-buffer) nil nil no-autoloads this-time)
751 (insert generate-autoload-section-trailer))
753 (let ((version-control 'never))
754 (save-buffer))
755 ;; In case autoload entries were added to other files because of
756 ;; file-local autoload-generated-file settings.
757 (autoload-save-buffers))))
759 (define-obsolete-function-alias 'update-autoloads-from-directories
760 'update-directory-autoloads "22.1")
762 (defvar autoload-make-program (or (getenv "MAKE") "make")
763 "Name of the make program in use during the Emacs build process.")
765 ;;;###autoload
766 (defun batch-update-autoloads ()
767 "Update loaddefs.el autoloads in batch mode.
768 Calls `update-directory-autoloads' on the command line arguments.
769 Definitions are written to `generated-autoload-file' (which
770 should be non-nil)."
771 ;; For use during the Emacs build process only.
772 ;; Exclude those files that are preloaded on ALL platforms.
773 ;; These are the ones in loadup.el where "(load" is at the start
774 ;; of the line (crude, but it works).
775 (unless autoload-excludes
776 (let ((default-directory (file-name-directory generated-autoload-file))
777 file)
778 (when (file-readable-p "loadup.el")
779 (with-temp-buffer
780 (insert-file-contents "loadup.el")
781 (while (re-search-forward "^(load \"\\([^\"]+\\)\"" nil t)
782 (setq file (match-string 1))
783 (or (string-match "\\.el\\'" file)
784 (setq file (format "%s.el" file)))
785 (or (string-match "\\`site-" file)
786 (push (expand-file-name file) autoload-excludes)))))))
787 (let ((args command-line-args-left))
788 (setq command-line-args-left nil)
789 (apply 'update-directory-autoloads args)))
791 (provide 'autoload)
793 ;;; autoload.el ends here