* lisp/progmodes/hideif.el (hif-compress-define-list): Fix typo. (tiny change)
[emacs.git] / lisp / emacs-lisp / autoload.el
blobe8dd9e8d1b4050cc44378894e30a5b3425d2b60a
1 ;; autoload.el --- maintain autoloads in loaddefs.el
3 ;; Copyright (C) 1991-1997, 2001-2012 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 (enable-local-eval nil))
206 ;; We used to use `raw-text' to read this file, but this causes
207 ;; problems when the file contains non-ASCII characters.
208 (find-file-noselect
209 (autoload-ensure-default-file (autoload-generated-file)))))
211 (defun autoload-generated-file ()
212 (expand-file-name generated-autoload-file
213 ;; File-local settings of generated-autoload-file should
214 ;; be interpreted relative to the file's location,
215 ;; of course.
216 (if (not (local-variable-p 'generated-autoload-file))
217 (expand-file-name "lisp" source-directory))))
220 (defun autoload-read-section-header ()
221 "Read a section header form.
222 Since continuation lines have been marked as comments,
223 we must copy the text of the form and remove those comment
224 markers before we call `read'."
225 (save-match-data
226 (let ((beginning (point))
227 string)
228 (forward-line 1)
229 (while (looking-at generate-autoload-section-continuation)
230 (forward-line 1))
231 (setq string (buffer-substring beginning (point)))
232 (with-current-buffer (get-buffer-create " *autoload*")
233 (erase-buffer)
234 (insert string)
235 (goto-char (point-min))
236 (while (search-forward generate-autoload-section-continuation nil t)
237 (replace-match " "))
238 (goto-char (point-min))
239 (read (current-buffer))))))
241 (defvar autoload-print-form-outbuf nil
242 "Buffer which gets the output of `autoload-print-form'.")
244 (defun autoload-print-form (form)
245 "Print FORM such that `make-docfile' will find the docstrings.
246 The variable `autoload-print-form-outbuf' specifies the buffer to
247 put the output in."
248 (cond
249 ;; If the form is a sequence, recurse.
250 ((eq (car form) 'progn) (mapcar 'autoload-print-form (cdr form)))
251 ;; Symbols at the toplevel are meaningless.
252 ((symbolp form) nil)
254 (let ((doc-string-elt (get (car-safe form) 'doc-string-elt))
255 (outbuf autoload-print-form-outbuf))
256 (if (and doc-string-elt (stringp (nth doc-string-elt form)))
257 ;; We need to hack the printing because the
258 ;; doc-string must be printed specially for
259 ;; make-docfile (sigh).
260 (let* ((p (nthcdr (1- doc-string-elt) form))
261 (elt (cdr p)))
262 (setcdr p nil)
263 (princ "\n(" outbuf)
264 (let ((print-escape-newlines t)
265 (print-quoted t)
266 (print-escape-nonascii t))
267 (dolist (elt form)
268 (prin1 elt outbuf)
269 (princ " " outbuf)))
270 (princ "\"\\\n" outbuf)
271 (let ((begin (with-current-buffer outbuf (point))))
272 (princ (substring (prin1-to-string (car elt)) 1)
273 outbuf)
274 ;; Insert a backslash before each ( that
275 ;; appears at the beginning of a line in
276 ;; the doc string.
277 (with-current-buffer outbuf
278 (save-excursion
279 (while (re-search-backward "\n[[(]" begin t)
280 (forward-char 1)
281 (insert "\\"))))
282 (if (null (cdr elt))
283 (princ ")" outbuf)
284 (princ " " outbuf)
285 (princ (substring (prin1-to-string (cdr elt)) 1)
286 outbuf))
287 (terpri outbuf)))
288 (let ((print-escape-newlines t)
289 (print-quoted t)
290 (print-escape-nonascii t))
291 (print form outbuf)))))))
293 (defun autoload-rubric (file &optional type feature)
294 "Return a string giving the appropriate autoload rubric for FILE.
295 TYPE (default \"autoloads\") is a string stating the type of
296 information contained in FILE. If FEATURE is non-nil, FILE
297 will provide a feature. FEATURE may be a string naming the
298 feature, otherwise it will be based on FILE's name.
300 At present, a feature is in fact always provided, but this should
301 not be relied upon."
302 (let ((basename (file-name-nondirectory file)))
303 (concat ";;; " basename
304 " --- automatically extracted " (or type "autoloads") "\n"
305 ";;\n"
306 ";;; Code:\n\n"
307 "\f\n"
308 ;; This is used outside of autoload.el, eg cus-dep, finder.
309 "(provide '"
310 (if (stringp feature)
311 feature
312 (file-name-sans-extension basename))
313 ")\n"
314 ";; Local Variables:\n"
315 ";; version-control: never\n"
316 ";; no-byte-compile: t\n"
317 ";; no-update-autoloads: t\n"
318 ";; coding: utf-8\n"
319 ";; End:\n"
320 ";;; " basename
321 " ends here\n")))
323 (defun autoload-ensure-default-file (file)
324 "Make sure that the autoload file FILE exists and if not create it."
325 (unless (file-exists-p file)
326 (write-region (autoload-rubric file) nil file))
327 file)
329 (defun autoload-insert-section-header (outbuf autoloads load-name file time)
330 "Insert the section-header line,
331 which lists the file name and which functions are in it, etc."
332 (insert generate-autoload-section-header)
333 (prin1 (list 'autoloads autoloads load-name file time)
334 outbuf)
335 (terpri outbuf)
336 ;; Break that line at spaces, to avoid very long lines.
337 ;; Make each sub-line into a comment.
338 (with-current-buffer outbuf
339 (save-excursion
340 (forward-line -1)
341 (while (not (eolp))
342 (move-to-column 64)
343 (skip-chars-forward "^ \n")
344 (or (eolp)
345 (insert "\n" generate-autoload-section-continuation))))))
347 (defun autoload-find-file (file)
348 "Fetch file and put it in a temp buffer. Return the buffer."
349 ;; It is faster to avoid visiting the file.
350 (setq file (expand-file-name file))
351 (with-current-buffer (get-buffer-create " *autoload-file*")
352 (kill-all-local-variables)
353 (erase-buffer)
354 (setq buffer-undo-list t
355 buffer-read-only nil)
356 (emacs-lisp-mode)
357 (setq default-directory (file-name-directory file))
358 (insert-file-contents file nil)
359 (let ((enable-local-variables :safe)
360 (enable-local-eval nil))
361 (hack-local-variables))
362 (current-buffer)))
364 (defvar no-update-autoloads nil
365 "File local variable to prevent scanning this file for autoload cookies.")
367 (defun autoload-file-load-name (file)
368 "Compute the name that will be used to load FILE."
369 ;; OUTFILE should be the name of the global loaddefs.el file, which
370 ;; is expected to be at the root directory of the files we're
371 ;; scanning for autoloads and will be in the `load-path'.
372 (let* ((outfile (default-value 'generated-autoload-file))
373 (name (file-relative-name file (file-name-directory outfile)))
374 (names '())
375 (dir (file-name-directory outfile)))
376 ;; If `name' has directory components, only keep the
377 ;; last few that are really needed.
378 (while name
379 (setq name (directory-file-name name))
380 (push (file-name-nondirectory name) names)
381 (setq name (file-name-directory name)))
382 (while (not name)
383 (cond
384 ((null (cdr names)) (setq name (car names)))
385 ((file-exists-p (expand-file-name "subdirs.el" dir))
386 ;; FIXME: here we only check the existence of subdirs.el,
387 ;; without checking its content. This makes it generate wrong load
388 ;; names for cases like lisp/term which is not added to load-path.
389 (setq dir (expand-file-name (pop names) dir)))
390 (t (setq name (mapconcat 'identity names "/")))))
391 (if (string-match "\\.elc?\\(\\.\\|\\'\\)" name)
392 (substring name 0 (match-beginning 0))
393 name)))
395 (defun generate-file-autoloads (file)
396 "Insert at point a loaddefs autoload section for FILE.
397 Autoloads are generated for defuns and defmacros in FILE
398 marked by `generate-autoload-cookie' (which see).
399 If FILE is being visited in a buffer, the contents of the buffer
400 are used.
401 Return non-nil in the case where no autoloads were added at point."
402 (interactive "fGenerate autoloads for file: ")
403 (let ((generated-autoload-file buffer-file-name))
404 (autoload-generate-file-autoloads file (current-buffer))))
406 (defvar print-readably)
408 ;; When called from `generate-file-autoloads' we should ignore
409 ;; `generated-autoload-file' altogether. When called from
410 ;; `update-file-autoloads' we don't know `outbuf'. And when called from
411 ;; `update-directory-autoloads' it's in between: we know the default
412 ;; `outbuf' but we should obey any file-local setting of
413 ;; `generated-autoload-file'.
414 (defun autoload-generate-file-autoloads (file &optional outbuf outfile)
415 "Insert an autoload section for FILE in the appropriate buffer.
416 Autoloads are generated for defuns and defmacros in FILE
417 marked by `generate-autoload-cookie' (which see).
418 If FILE is being visited in a buffer, the contents of the buffer are used.
419 OUTBUF is the buffer in which the autoload statements should be inserted.
420 If OUTBUF is nil, it will be determined by `autoload-generated-file'.
422 If provided, OUTFILE is expected to be the file name of OUTBUF.
423 If OUTFILE is non-nil and FILE specifies a `generated-autoload-file'
424 different from OUTFILE, then OUTBUF is ignored.
426 Return non-nil if and only if FILE adds no autoloads to OUTFILE
427 \(or OUTBUF if OUTFILE is nil)."
428 (catch 'done
429 (let ((autoloads-done '())
430 load-name
431 (print-length nil)
432 (print-level nil)
433 (print-readably t) ; This does something in Lucid Emacs.
434 (float-output-format nil)
435 (visited (get-file-buffer file))
436 (otherbuf nil)
437 (absfile (expand-file-name file))
438 ;; nil until we found a cookie.
439 output-start ostart)
440 (with-current-buffer (or visited
441 ;; It is faster to avoid visiting the file.
442 (autoload-find-file file))
443 ;; Obey the no-update-autoloads file local variable.
444 (unless no-update-autoloads
445 (message "Generating autoloads for %s..." file)
446 (setq load-name
447 (if (stringp generated-autoload-load-name)
448 generated-autoload-load-name
449 (autoload-file-load-name absfile)))
450 (when (and outfile
451 (not
452 (if (memq system-type '(ms-dos windows-nt))
453 (equal (downcase outfile)
454 (downcase (autoload-generated-file)))
455 (equal outfile (autoload-generated-file)))))
456 (setq otherbuf t))
457 (save-excursion
458 (save-restriction
459 (widen)
460 (goto-char (point-min))
461 (while (not (eobp))
462 (skip-chars-forward " \t\n\f")
463 (cond
464 ((looking-at (regexp-quote generate-autoload-cookie))
465 ;; If not done yet, figure out where to insert this text.
466 (unless output-start
467 (let ((outbuf
468 (or (if otherbuf
469 ;; A file-local setting of
470 ;; autoload-generated-file says we
471 ;; should ignore OUTBUF.
473 outbuf)
474 (autoload-find-destination absfile load-name)
475 ;; The file has autoload cookies, but they're
476 ;; already up-to-date. If OUTFILE is nil, the
477 ;; entries are in the expected OUTBUF,
478 ;; otherwise they're elsewhere.
479 (throw 'done otherbuf))))
480 (with-current-buffer outbuf
481 (setq output-start (point-marker)
482 ostart (point)))))
483 (search-forward generate-autoload-cookie)
484 (skip-chars-forward " \t")
485 (if (eolp)
486 (condition-case err
487 ;; Read the next form and make an autoload.
488 (let* ((form (prog1 (read (current-buffer))
489 (or (bolp) (forward-line 1))))
490 (autoload (make-autoload form load-name)))
491 (if autoload
492 (push (nth 1 form) autoloads-done)
493 (setq autoload form))
494 (let ((autoload-print-form-outbuf
495 (marker-buffer output-start)))
496 (autoload-print-form autoload)))
497 (error
498 (message "Autoload cookie error in %s:%s %S"
499 file (count-lines (point-min) (point)) err)))
501 ;; Copy the rest of the line to the output.
502 (princ (buffer-substring
503 (progn
504 ;; Back up over whitespace, to preserve it.
505 (skip-chars-backward " \f\t")
506 (if (= (char-after (1+ (point))) ? )
507 ;; Eat one space.
508 (forward-char 1))
509 (point))
510 (progn (forward-line 1) (point)))
511 (marker-buffer output-start))))
512 ((looking-at ";")
513 ;; Don't read the comment.
514 (forward-line 1))
516 (forward-sexp 1)
517 (forward-line 1))))))
519 (when output-start
520 (let ((secondary-autoloads-file-buf
521 (if otherbuf (current-buffer))))
522 (with-current-buffer (marker-buffer output-start)
523 (save-excursion
524 ;; Insert the section-header line which lists the file name
525 ;; and which functions are in it, etc.
526 (assert (= ostart output-start))
527 (goto-char output-start)
528 (let ((relfile (file-relative-name absfile)))
529 (autoload-insert-section-header
530 (marker-buffer output-start)
531 autoloads-done load-name relfile
532 (if secondary-autoloads-file-buf
533 ;; MD5 checksums are much better because they do not
534 ;; change unless the file changes (so they'll be
535 ;; equal on two different systems and will change
536 ;; less often than time-stamps, thus leading to fewer
537 ;; unneeded changes causing spurious conflicts), but
538 ;; using time-stamps is a very useful optimization,
539 ;; so we use time-stamps for the main autoloads file
540 ;; (loaddefs.el) where we have special ways to
541 ;; circumvent the "random change problem", and MD5
542 ;; checksum in secondary autoload files where we do
543 ;; not need the time-stamp optimization because it is
544 ;; already provided by the primary autoloads file.
545 (md5 secondary-autoloads-file-buf
546 ;; We'd really want to just use
547 ;; `emacs-internal' instead.
548 nil nil 'emacs-mule-unix)
549 (nth 5 (file-attributes relfile))))
550 (insert ";;; Generated autoloads from " relfile "\n")))
551 (insert generate-autoload-section-trailer))))
552 (message "Generating autoloads for %s...done" file))
553 (or visited
554 ;; We created this buffer, so we should kill it.
555 (kill-buffer (current-buffer))))
556 (or (not output-start)
557 ;; If the entries were added to some other buffer, then the file
558 ;; doesn't add entries to OUTFILE.
559 otherbuf))))
561 (defun autoload-save-buffers ()
562 (while autoload-modified-buffers
563 (with-current-buffer (pop autoload-modified-buffers)
564 (let ((version-control 'never))
565 (save-buffer)))))
567 ;;;###autoload
568 (defun update-file-autoloads (file &optional save-after outfile)
569 "Update the autoloads for FILE.
570 If prefix arg SAVE-AFTER is non-nil, save the buffer too.
572 If FILE binds `generated-autoload-file' as a file-local variable,
573 autoloads are written into that file. Otherwise, the autoloads
574 file is determined by OUTFILE. If called interactively, prompt
575 for OUTFILE; if called from Lisp with OUTFILE nil, use the
576 existing value of `generated-autoload-file'.
578 Return FILE if there was no autoload cookie in it, else nil."
579 (interactive (list (read-file-name "Update autoloads for file: ")
580 current-prefix-arg
581 (read-file-name "Write autoload definitions to file: ")))
582 (let* ((generated-autoload-file (or outfile generated-autoload-file))
583 (autoload-modified-buffers nil)
584 (no-autoloads (autoload-generate-file-autoloads file)))
585 (if autoload-modified-buffers
586 (if save-after (autoload-save-buffers))
587 (if (called-interactively-p 'interactive)
588 (message "Autoload section for %s is up to date." file)))
589 (if no-autoloads file)))
591 (defun autoload-find-destination (file load-name)
592 "Find the destination point of the current buffer's autoloads.
593 FILE is the file name of the current buffer.
594 Returns a buffer whose point is placed at the requested location.
595 Returns nil if the file's autoloads are uptodate, otherwise
596 removes any prior now out-of-date autoload entries."
597 (catch 'up-to-date
598 (let* ((buf (current-buffer))
599 (existing-buffer (if buffer-file-name buf))
600 (found nil))
601 (with-current-buffer (autoload-find-generated-file)
602 ;; This is to make generated-autoload-file have Unix EOLs, so
603 ;; that it is portable to all platforms.
604 (or (eq 0 (coding-system-eol-type buffer-file-coding-system))
605 (set-buffer-file-coding-system 'unix))
606 (or (> (buffer-size) 0)
607 (error "Autoloads file %s lacks boilerplate" buffer-file-name))
608 (or (file-writable-p buffer-file-name)
609 (error "Autoloads file %s is not writable" buffer-file-name))
610 (widen)
611 (goto-char (point-min))
612 ;; Look for the section for LOAD-NAME.
613 (while (and (not found)
614 (search-forward generate-autoload-section-header nil t))
615 (let ((form (autoload-read-section-header)))
616 (cond ((string= (nth 2 form) load-name)
617 ;; We found the section for this file.
618 ;; Check if it is up to date.
619 (let ((begin (match-beginning 0))
620 (last-time (nth 4 form))
621 (file-time (nth 5 (file-attributes file))))
622 (if (and (or (null existing-buffer)
623 (not (buffer-modified-p existing-buffer)))
625 ;; last-time is the time-stamp (specifying
626 ;; the last time we looked at the file) and
627 ;; the file hasn't been changed since.
628 (and (listp last-time) (= (length last-time) 2)
629 (not (time-less-p last-time file-time)))
630 ;; last-time is an MD5 checksum instead.
631 (and (stringp last-time)
632 (equal last-time
633 (md5 buf nil nil 'emacs-mule)))))
634 (throw 'up-to-date nil)
635 (autoload-remove-section begin)
636 (setq found t))))
637 ((string< load-name (nth 2 form))
638 ;; We've come to a section alphabetically later than
639 ;; LOAD-NAME. We assume the file is in order and so
640 ;; there must be no section for LOAD-NAME. We will
641 ;; insert one before the section here.
642 (goto-char (match-beginning 0))
643 (setq found t)))))
644 (or found
645 (progn
646 ;; No later sections in the file. Put before the last page.
647 (goto-char (point-max))
648 (search-backward "\f" nil t)))
649 (unless (memq (current-buffer) autoload-modified-buffers)
650 (push (current-buffer) autoload-modified-buffers))
651 (current-buffer)))))
653 (defun autoload-remove-section (begin)
654 (goto-char begin)
655 (search-forward generate-autoload-section-trailer)
656 (delete-region begin (point)))
658 ;;;###autoload
659 (defun update-directory-autoloads (&rest dirs)
660 "Update autoload definitions for Lisp files in the directories DIRS.
661 In an interactive call, you must give one argument, the name of a
662 single directory. In a call from Lisp, you can supply multiple
663 directories as separate arguments, but this usage is discouraged.
665 The function does NOT recursively descend into subdirectories of the
666 directory or directories specified.
668 In an interactive call, prompt for a default output file for the
669 autoload definitions, and temporarily bind the variable
670 `generated-autoload-file' to this value. When called from Lisp,
671 use the existing value of `generated-autoload-file'. If any Lisp
672 file binds `generated-autoload-file' as a file-local variable,
673 write its autoloads into the specified file instead."
674 (interactive "DUpdate autoloads from directory: ")
675 (let* ((files-re (let ((tmp nil))
676 (dolist (suf (get-load-suffixes)
677 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
678 (unless (string-match "\\.elc" suf) (push suf tmp)))))
679 (files (apply 'nconc
680 (mapcar (lambda (dir)
681 (directory-files (expand-file-name dir)
682 t files-re))
683 dirs)))
684 (done ())
685 (this-time (current-time))
686 ;; Files with no autoload cookies or whose autoloads go to other
687 ;; files because of file-local autoload-generated-file settings.
688 (no-autoloads nil)
689 (autoload-modified-buffers nil)
690 (generated-autoload-file
691 (if (called-interactively-p 'interactive)
692 (read-file-name "Write autoload definitions to file: ")
693 generated-autoload-file)))
695 (with-current-buffer (autoload-find-generated-file)
696 (save-excursion
697 ;; Canonicalize file names and remove the autoload file itself.
698 (setq files (delete (file-relative-name buffer-file-name)
699 (mapcar 'file-relative-name files)))
701 (goto-char (point-min))
702 (while (search-forward generate-autoload-section-header nil t)
703 (let* ((form (autoload-read-section-header))
704 (file (nth 3 form)))
705 (cond ((and (consp file) (stringp (car file)))
706 ;; This is a list of files that have no autoload cookies.
707 ;; There shouldn't be more than one such entry.
708 ;; Remove the obsolete section.
709 (autoload-remove-section (match-beginning 0))
710 (let ((last-time (nth 4 form)))
711 (dolist (file file)
712 (let ((file-time (nth 5 (file-attributes file))))
713 (when (and file-time
714 (not (time-less-p last-time file-time)))
715 ;; file unchanged
716 (push file no-autoloads)
717 (setq files (delete file files)))))))
718 ((not (stringp file)))
719 ((or (not (file-exists-p file))
720 ;; Remove duplicates as well, just in case.
721 (member file done)
722 ;; If the file is actually excluded.
723 (member (expand-file-name file) autoload-excludes))
724 ;; Remove the obsolete section.
725 (autoload-remove-section (match-beginning 0)))
726 ((not (time-less-p (nth 4 form)
727 (nth 5 (file-attributes file))))
728 ;; File hasn't changed.
729 nil)
731 (autoload-remove-section (match-beginning 0))
732 (if (autoload-generate-file-autoloads
733 ;; Passing `current-buffer' makes it insert at point.
734 file (current-buffer) buffer-file-name)
735 (push file no-autoloads))))
736 (push file done)
737 (setq files (delete file files)))))
738 ;; Elements remaining in FILES have no existing autoload sections yet.
739 (dolist (file files)
740 (cond
741 ((member (expand-file-name file) autoload-excludes) nil)
742 ;; Passing nil as second argument forces
743 ;; autoload-generate-file-autoloads to look for the right
744 ;; spot where to insert each autoloads section.
745 ((autoload-generate-file-autoloads file nil buffer-file-name)
746 (push file no-autoloads))))
748 (when no-autoloads
749 ;; Sort them for better readability.
750 (setq no-autoloads (sort no-autoloads 'string<))
751 ;; Add the `no-autoloads' section.
752 (goto-char (point-max))
753 (search-backward "\f" nil t)
754 (autoload-insert-section-header
755 (current-buffer) nil nil no-autoloads this-time)
756 (insert generate-autoload-section-trailer))
758 (let ((version-control 'never))
759 (save-buffer))
760 ;; In case autoload entries were added to other files because of
761 ;; file-local autoload-generated-file settings.
762 (autoload-save-buffers))))
764 (define-obsolete-function-alias 'update-autoloads-from-directories
765 'update-directory-autoloads "22.1")
767 (defvar autoload-make-program (or (getenv "MAKE") "make")
768 "Name of the make program in use during the Emacs build process.")
770 ;;;###autoload
771 (defun batch-update-autoloads ()
772 "Update loaddefs.el autoloads in batch mode.
773 Calls `update-directory-autoloads' on the command line arguments.
774 Definitions are written to `generated-autoload-file' (which
775 should be non-nil)."
776 ;; For use during the Emacs build process only.
777 ;; Exclude those files that are preloaded on ALL platforms.
778 ;; These are the ones in loadup.el where "(load" is at the start
779 ;; of the line (crude, but it works).
780 (unless autoload-excludes
781 (let ((default-directory (file-name-directory generated-autoload-file))
782 file)
783 (when (file-readable-p "loadup.el")
784 (with-temp-buffer
785 (insert-file-contents "loadup.el")
786 (while (re-search-forward "^(load \"\\([^\"]+\\)\"" nil t)
787 (setq file (match-string 1))
788 (or (string-match "\\.el\\'" file)
789 (setq file (format "%s.el" file)))
790 (or (string-match "\\`site-" file)
791 (push (expand-file-name file) autoload-excludes)))))))
792 (let ((args command-line-args-left))
793 (setq command-line-args-left nil)
794 (apply 'update-directory-autoloads args)))
796 (provide 'autoload)
798 ;;; autoload.el ends here