Merge from trunk.
[bpt/emacs.git] / lisp / emacs-lisp / autoload.el
1 ;; autoload.el --- maintain autoloads in loaddefs.el
2
3 ;; Copyright (C) 1991-1997, 2001-2011 Free Software Foundation, Inc.
4
5 ;; Author: Roland McGrath <roland@gnu.org>
6 ;; Keywords: maint
7 ;; Package: emacs
8
9 ;; This file is part of GNU Emacs.
10
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.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
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.
30
31 ;;; Code:
32
33 (require 'lisp-mode) ;for `doc-string-elt' properties.
34 (require 'help-fns) ;for help-add-fundoc-usage.
35 (eval-when-compile (require 'cl))
36
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.
41
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'.
47
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)
52
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)
58
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).
65
66 This string is used:
67
68 \;;;###autoload
69 \(defun function-to-be-autoloaded () ...)
70
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'.")
74
75 (defvar autoload-excludes nil
76 "If non-nil, list of absolute file names not to scan for autoloads.")
77
78 (defconst generate-autoload-section-header "\f\n;;;### "
79 "String that marks the form at the start of a new file's autoload section.")
80
81 (defconst generate-autoload-section-trailer "\n;;;***\n"
82 "String which indicates the end of the section of autoloads for a file.")
83
84 (defconst generate-autoload-section-continuation ";;;;;; "
85 "String to add on each continuation of the section header form.")
86
87 (defvar autoload-modified-buffers) ;Dynamically scoped var.
88
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)))))
108
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)))
161
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)))
169
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)))))
176 )
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))))))
183
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))))))
192
193 ;; nil here indicates that this is not a special autoload form.
194 (t nil))))
195
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.
200
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)))))
209
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))))
217
218
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))))))
239
240 (defvar autoload-print-form-outbuf nil
241 "Buffer which gets the output of `autoload-print-form'.")
242
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)
252 (t
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)))))))
291
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.
298
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")))
321
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)
327
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))))))
345
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)))
361
362 (defvar no-update-autoloads nil
363 "File local variable to prevent scanning this file for autoload cookies.")
364
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)))
392
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))))
403
404 (defvar print-readably)
405
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'.
419
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.
423
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.
466 nil
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)))
494
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))
509 (t
510 (forward-sexp 1)
511 (forward-line 1))))))
512
513 (when output-start
514 (let ((secondary-autoloads-file-buf
515 (if (local-variable-p 'generated-autoload-file)
516 (current-buffer))))
517 ;; Ignore a buffer-local setting if it points to the
518 ;; global value. Otherwise we end up writing a mix of md5s
519 ;; and time-stamps to the global file. (Bug#10049)
520 (and secondary-autoloads-file-buf
521 outfile
522 (not otherbuf)
523 (setq secondary-autoloads-file-buf nil))
524 (with-current-buffer (marker-buffer output-start)
525 (save-excursion
526 ;; Insert the section-header line which lists the file name
527 ;; and which functions are in it, etc.
528 (assert (= ostart output-start))
529 (goto-char output-start)
530 (let ((relfile (file-relative-name absfile)))
531 (autoload-insert-section-header
532 (marker-buffer output-start)
533 autoloads-done load-name relfile
534 (if secondary-autoloads-file-buf
535 ;; MD5 checksums are much better because they do not
536 ;; change unless the file changes (so they'll be
537 ;; equal on two different systems and will change
538 ;; less often than time-stamps, thus leading to fewer
539 ;; unneeded changes causing spurious conflicts), but
540 ;; using time-stamps is a very useful optimization,
541 ;; so we use time-stamps for the main autoloads file
542 ;; (loaddefs.el) where we have special ways to
543 ;; circumvent the "random change problem", and MD5
544 ;; checksum in secondary autoload files where we do
545 ;; not need the time-stamp optimization because it is
546 ;; already provided by the primary autoloads file.
547 (md5 secondary-autoloads-file-buf
548 ;; We'd really want to just use
549 ;; `emacs-internal' instead.
550 nil nil 'emacs-mule-unix)
551 (nth 5 (file-attributes relfile))))
552 (insert ";;; Generated autoloads from " relfile "\n")))
553 (insert generate-autoload-section-trailer))))
554 (message "Generating autoloads for %s...done" file))
555 (or visited
556 ;; We created this buffer, so we should kill it.
557 (kill-buffer (current-buffer))))
558 (or (not output-start)
559 ;; If the entries were added to some other buffer, then the file
560 ;; doesn't add entries to OUTFILE.
561 otherbuf))))
562 \f
563 (defun autoload-save-buffers ()
564 (while autoload-modified-buffers
565 (with-current-buffer (pop autoload-modified-buffers)
566 (let ((version-control 'never))
567 (save-buffer)))))
568
569 ;;;###autoload
570 (defun update-file-autoloads (file &optional save-after outfile)
571 "Update the autoloads for FILE.
572 If prefix arg SAVE-AFTER is non-nil, save the buffer too.
573
574 If FILE binds `generated-autoload-file' as a file-local variable,
575 autoloads are written into that file. Otherwise, the autoloads
576 file is determined by OUTFILE. If called interactively, prompt
577 for OUTFILE; if called from Lisp with OUTFILE nil, use the
578 existing value of `generated-autoload-file'.
579
580 Return FILE if there was no autoload cookie in it, else nil."
581 (interactive (list (read-file-name "Update autoloads for file: ")
582 current-prefix-arg
583 (read-file-name "Write autoload definitions to file: ")))
584 (let* ((generated-autoload-file (or outfile generated-autoload-file))
585 (autoload-modified-buffers nil)
586 (no-autoloads (autoload-generate-file-autoloads file)))
587 (if autoload-modified-buffers
588 (if save-after (autoload-save-buffers))
589 (if (called-interactively-p 'interactive)
590 (message "Autoload section for %s is up to date." file)))
591 (if no-autoloads file)))
592
593 (defun autoload-find-destination (file load-name)
594 "Find the destination point of the current buffer's autoloads.
595 FILE is the file name of the current buffer.
596 Returns a buffer whose point is placed at the requested location.
597 Returns nil if the file's autoloads are uptodate, otherwise
598 removes any prior now out-of-date autoload entries."
599 (catch 'up-to-date
600 (let* ((buf (current-buffer))
601 (existing-buffer (if buffer-file-name buf))
602 (found nil))
603 (with-current-buffer (autoload-find-generated-file)
604 ;; This is to make generated-autoload-file have Unix EOLs, so
605 ;; that it is portable to all platforms.
606 (or (eq 0 (coding-system-eol-type buffer-file-coding-system))
607 (set-buffer-file-coding-system 'unix))
608 (or (> (buffer-size) 0)
609 (error "Autoloads file %s lacks boilerplate" buffer-file-name))
610 (or (file-writable-p buffer-file-name)
611 (error "Autoloads file %s is not writable" buffer-file-name))
612 (widen)
613 (goto-char (point-min))
614 ;; Look for the section for LOAD-NAME.
615 (while (and (not found)
616 (search-forward generate-autoload-section-header nil t))
617 (let ((form (autoload-read-section-header)))
618 (cond ((string= (nth 2 form) load-name)
619 ;; We found the section for this file.
620 ;; Check if it is up to date.
621 (let ((begin (match-beginning 0))
622 (last-time (nth 4 form))
623 (file-time (nth 5 (file-attributes file))))
624 (if (and (or (null existing-buffer)
625 (not (buffer-modified-p existing-buffer)))
626 (or
627 ;; last-time is the time-stamp (specifying
628 ;; the last time we looked at the file) and
629 ;; the file hasn't been changed since.
630 (and (listp last-time) (= (length last-time) 2)
631 (not (time-less-p last-time file-time)))
632 ;; last-time is an MD5 checksum instead.
633 (and (stringp last-time)
634 (equal last-time
635 (md5 buf nil nil 'emacs-mule)))))
636 (throw 'up-to-date nil)
637 (autoload-remove-section begin)
638 (setq found t))))
639 ((string< load-name (nth 2 form))
640 ;; We've come to a section alphabetically later than
641 ;; LOAD-NAME. We assume the file is in order and so
642 ;; there must be no section for LOAD-NAME. We will
643 ;; insert one before the section here.
644 (goto-char (match-beginning 0))
645 (setq found t)))))
646 (or found
647 (progn
648 ;; No later sections in the file. Put before the last page.
649 (goto-char (point-max))
650 (search-backward "\f" nil t)))
651 (unless (memq (current-buffer) autoload-modified-buffers)
652 (push (current-buffer) autoload-modified-buffers))
653 (current-buffer)))))
654
655 (defun autoload-remove-section (begin)
656 (goto-char begin)
657 (search-forward generate-autoload-section-trailer)
658 (delete-region begin (point)))
659
660 ;;;###autoload
661 (defun update-directory-autoloads (&rest dirs)
662 "Update autoload definitions for Lisp files in the directories DIRS.
663 In an interactive call, you must give one argument, the name of a
664 single directory. In a call from Lisp, you can supply multiple
665 directories as separate arguments, but this usage is discouraged.
666
667 The function does NOT recursively descend into subdirectories of the
668 directory or directories specified.
669
670 In an interactive call, prompt for a default output file for the
671 autoload definitions, and temporarily bind the variable
672 `generated-autoload-file' to this value. When called from Lisp,
673 use the existing value of `generated-autoload-file'. If any Lisp
674 file binds `generated-autoload-file' as a file-local variable,
675 write its autoloads into the specified file instead."
676 (interactive "DUpdate autoloads from directory: ")
677 (let* ((files-re (let ((tmp nil))
678 (dolist (suf (get-load-suffixes)
679 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
680 (unless (string-match "\\.elc" suf) (push suf tmp)))))
681 (files (apply 'nconc
682 (mapcar (lambda (dir)
683 (directory-files (expand-file-name dir)
684 t files-re))
685 dirs)))
686 (done ())
687 (this-time (current-time))
688 ;; Files with no autoload cookies or whose autoloads go to other
689 ;; files because of file-local autoload-generated-file settings.
690 (no-autoloads nil)
691 (autoload-modified-buffers nil)
692 (generated-autoload-file
693 (if (called-interactively-p 'interactive)
694 (read-file-name "Write autoload definitions to file: ")
695 generated-autoload-file)))
696
697 (with-current-buffer (autoload-find-generated-file)
698 (save-excursion
699 ;; Canonicalize file names and remove the autoload file itself.
700 (setq files (delete (file-relative-name buffer-file-name)
701 (mapcar 'file-relative-name files)))
702
703 (goto-char (point-min))
704 (while (search-forward generate-autoload-section-header nil t)
705 (let* ((form (autoload-read-section-header))
706 (file (nth 3 form)))
707 (cond ((and (consp file) (stringp (car file)))
708 ;; This is a list of files that have no autoload cookies.
709 ;; There shouldn't be more than one such entry.
710 ;; Remove the obsolete section.
711 (autoload-remove-section (match-beginning 0))
712 (let ((last-time (nth 4 form)))
713 (dolist (file file)
714 (let ((file-time (nth 5 (file-attributes file))))
715 (when (and file-time
716 (not (time-less-p last-time file-time)))
717 ;; file unchanged
718 (push file no-autoloads)
719 (setq files (delete file files)))))))
720 ((not (stringp file)))
721 ((or (not (file-exists-p file))
722 ;; Remove duplicates as well, just in case.
723 (member file done)
724 ;; If the file is actually excluded.
725 (member (expand-file-name file) autoload-excludes))
726 ;; Remove the obsolete section.
727 (autoload-remove-section (match-beginning 0)))
728 ((not (time-less-p (nth 4 form)
729 (nth 5 (file-attributes file))))
730 ;; File hasn't changed.
731 nil)
732 (t
733 (autoload-remove-section (match-beginning 0))
734 (if (autoload-generate-file-autoloads
735 ;; Passing `current-buffer' makes it insert at point.
736 file (current-buffer) buffer-file-name)
737 (push file no-autoloads))))
738 (push file done)
739 (setq files (delete file files)))))
740 ;; Elements remaining in FILES have no existing autoload sections yet.
741 (dolist (file files)
742 (cond
743 ((member (expand-file-name file) autoload-excludes) nil)
744 ;; Passing nil as second argument forces
745 ;; autoload-generate-file-autoloads to look for the right
746 ;; spot where to insert each autoloads section.
747 ((autoload-generate-file-autoloads file nil buffer-file-name)
748 (push file no-autoloads))))
749
750 (when no-autoloads
751 ;; Sort them for better readability.
752 (setq no-autoloads (sort no-autoloads 'string<))
753 ;; Add the `no-autoloads' section.
754 (goto-char (point-max))
755 (search-backward "\f" nil t)
756 (autoload-insert-section-header
757 (current-buffer) nil nil no-autoloads this-time)
758 (insert generate-autoload-section-trailer))
759
760 (let ((version-control 'never))
761 (save-buffer))
762 ;; In case autoload entries were added to other files because of
763 ;; file-local autoload-generated-file settings.
764 (autoload-save-buffers))))
765
766 (define-obsolete-function-alias 'update-autoloads-from-directories
767 'update-directory-autoloads "22.1")
768
769 (defvar autoload-make-program (or (getenv "MAKE") "make")
770 "Name of the make program in use during the Emacs build process.")
771
772 ;;;###autoload
773 (defun batch-update-autoloads ()
774 "Update loaddefs.el autoloads in batch mode.
775 Calls `update-directory-autoloads' on the command line arguments.
776 Definitions are written to `generated-autoload-file' (which
777 should be non-nil)."
778 ;; For use during the Emacs build process only.
779 ;; Exclude those files that are preloaded on ALL platforms.
780 ;; These are the ones in loadup.el where "(load" is at the start
781 ;; of the line (crude, but it works).
782 (unless autoload-excludes
783 (let ((default-directory (file-name-directory generated-autoload-file))
784 file)
785 (when (file-readable-p "loadup.el")
786 (with-temp-buffer
787 (insert-file-contents "loadup.el")
788 (while (re-search-forward "^(load \"\\([^\"]+\\)\"" nil t)
789 (setq file (match-string 1))
790 (or (string-match "\\.el\\'" file)
791 (setq file (format "%s.el" file)))
792 (or (string-match "\\`site-" file)
793 (push (expand-file-name file) autoload-excludes)))))))
794 (let ((args command-line-args-left))
795 (setq command-line-args-left nil)
796 (apply 'update-directory-autoloads args)))
797
798 (provide 'autoload)
799
800 ;;; autoload.el ends here