1 ;; autoload.el --- maintain autoloads in loaddefs.el
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Roland McGrath <roland@gnu.org>
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/>.
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.
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
"loaddefs.el"
38 "*File \\[update-file-autoloads] puts autoloads into.
39 A `.el' file can set this in its local variables section to make its
40 autoloads go somewhere else. The autoload file is assumed to contain a
41 trailer starting with a FormFeed character.")
43 (put 'generated-autoload-file
'safe-local-variable
'stringp
)
45 ;; This feels like it should be a defconst, but MH-E sets it to
46 ;; ";;;###mh-autoload" for the autoloads that are to go into mh-loaddefs.el.
47 (defvar generate-autoload-cookie
";;;###autoload"
48 "Magic comment indicating the following form should be autoloaded.
49 Used by \\[update-file-autoloads]. This string should be
50 meaningless to Lisp (e.g., a comment).
55 \(defun function-to-be-autoloaded () ...)
57 If this string appears alone on a line, the following form will be
58 read and an autoload made for it. If there is further text on the line,
59 that text will be copied verbatim to `generated-autoload-file'.")
61 (defconst generate-autoload-section-header
"\f\n;;;### "
62 "String that marks the form at the start of a new file's autoload section.")
64 (defconst generate-autoload-section-trailer
"\n;;;***\n"
65 "String which indicates the end of the section of autoloads for a file.")
67 (defconst generate-autoload-section-continuation
";;;;;; "
68 "String to add on each continuation of the section header form.")
70 (defvar autoload-modified-buffers
) ;Dynamically scoped var.
72 (defun make-autoload (form file
)
73 "Turn FORM into an autoload or defvar for source file FILE.
74 Returns nil if FORM is not a special autoload form (i.e. a function definition
75 or macro definition or a defcustom)."
76 (let ((car (car-safe form
)) expand
)
78 ;; For complex cases, try again on the macro-expansion.
79 ((and (memq car
'(easy-mmode-define-global-mode define-global-minor-mode
80 define-globalized-minor-mode
81 easy-mmode-define-minor-mode define-minor-mode
))
82 (setq expand
(let ((load-file-name file
)) (macroexpand form
)))
83 (eq (car expand
) 'progn
)
84 (memq :autoload-end expand
))
85 (let ((end (memq :autoload-end expand
)))
86 ;; Cut-off anything after the :autoload-end marker.
89 (mapcar (lambda (form) (make-autoload form file
))
92 ;; For special function-like operators, use the `autoload' function.
93 ((memq car
'(defun define-skeleton defmacro define-derived-mode
94 define-compilation-mode define-generic-mode
95 easy-mmode-define-global-mode define-global-minor-mode
96 define-globalized-minor-mode
97 easy-mmode-define-minor-mode define-minor-mode
99 (let* ((macrop (memq car
'(defmacro defmacro
*)))
102 ((defun defmacro defun
* defmacro
*) (nth 2 form
))
103 ((define-skeleton) '(&optional str arg
))
104 ((define-generic-mode define-derived-mode
105 define-compilation-mode
) nil
)
107 (body (nthcdr (get car
'doc-string-elt
) form
))
108 (doc (if (stringp (car body
)) (pop body
))))
110 ;; Add the usage form at the end where describe-function-1
112 (setq doc
(help-add-fundoc-usage doc args
)))
113 ;; `define-generic-mode' quotes the name, so take care of that
114 (list 'autoload
(if (listp name
) name
(list 'quote name
)) file doc
115 (or (and (memq car
'(define-skeleton define-derived-mode
117 easy-mmode-define-global-mode
118 define-global-minor-mode
119 define-globalized-minor-mode
120 easy-mmode-define-minor-mode
121 define-minor-mode
)) t
)
122 (eq (car-safe (car body
)) 'interactive
))
123 (if macrop
(list 'quote
'macro
) nil
))))
125 ;; Convert defcustom to less space-consuming data.
127 (let ((varname (car-safe (cdr-safe form
)))
128 (init (car-safe (cdr-safe (cdr-safe form
))))
129 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form
)))))
130 ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
133 (defvar ,varname
,init
,doc
)
134 (custom-autoload ',varname
,file
136 (null (cadr (memq :set form
)))
140 ;; In Emacs this is normally handled separately by cus-dep.el, but for
141 ;; third party packages, it can be convenient to explicitly autoload
143 (let ((groupname (nth 1 form
)))
144 `(let ((loads (get ',groupname
'custom-loads
)))
145 (if (member ',file loads
) nil
146 (put ',groupname
'custom-loads
(cons ',file loads
))))))
148 ;; nil here indicates that this is not a special autoload form.
151 ;; Forms which have doc-strings which should be printed specially.
152 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
153 ;; the doc-string in FORM.
154 ;; Those properties are now set in lisp-mode.el.
156 (defun autoload-generated-file ()
157 (expand-file-name generated-autoload-file
158 ;; File-local settings of generated-autoload-file should
159 ;; be interpreted relative to the file's location,
161 (if (not (local-variable-p 'generated-autoload-file
))
162 (expand-file-name "lisp" source-directory
))))
165 (defun autoload-read-section-header ()
166 "Read a section header form.
167 Since continuation lines have been marked as comments,
168 we must copy the text of the form and remove those comment
169 markers before we call `read'."
171 (let ((beginning (point))
174 (while (looking-at generate-autoload-section-continuation
)
176 (setq string
(buffer-substring beginning
(point)))
177 (with-current-buffer (get-buffer-create " *autoload*")
180 (goto-char (point-min))
181 (while (search-forward generate-autoload-section-continuation nil t
)
183 (goto-char (point-min))
184 (read (current-buffer))))))
186 (defvar autoload-print-form-outbuf nil
187 "Buffer which gets the output of `autoload-print-form'.")
189 (defun autoload-print-form (form)
190 "Print FORM such that `make-docfile' will find the docstrings.
191 The variable `autoload-print-form-outbuf' specifies the buffer to
194 ;; If the form is a sequence, recurse.
195 ((eq (car form
) 'progn
) (mapcar 'autoload-print-form
(cdr form
)))
196 ;; Symbols at the toplevel are meaningless.
199 (let ((doc-string-elt (get (car-safe form
) 'doc-string-elt
))
200 (outbuf autoload-print-form-outbuf
))
201 (if (and doc-string-elt
(stringp (nth doc-string-elt form
)))
202 ;; We need to hack the printing because the
203 ;; doc-string must be printed specially for
204 ;; make-docfile (sigh).
205 (let* ((p (nthcdr (1- doc-string-elt
) form
))
209 (let ((print-escape-newlines t
)
211 (print-escape-nonascii t
))
215 (princ "\"\\\n" outbuf
)
216 (let ((begin (with-current-buffer outbuf
(point))))
217 (princ (substring (prin1-to-string (car elt
)) 1)
219 ;; Insert a backslash before each ( that
220 ;; appears at the beginning of a line in
222 (with-current-buffer outbuf
224 (while (re-search-backward "\n[[(]" begin t
)
230 (princ (substring (prin1-to-string (cdr elt
)) 1)
233 (let ((print-escape-newlines t
)
235 (print-escape-nonascii t
))
236 (print form outbuf
)))))))
238 (defun autoload-rubric (file &optional type
)
239 "Return a string giving the appropriate autoload rubric for FILE.
240 TYPE (default \"autoloads\") is a string stating the type of
241 information contained in FILE."
242 (let ((basename (file-name-nondirectory file
)))
243 (concat ";;; " basename
244 " --- automatically extracted " (or type
"autoloads") "\n"
248 "(provide '" (file-name-sans-extension basename
) ")\n"
249 ";; Local Variables:\n"
250 ";; version-control: never\n"
251 ";; no-byte-compile: t\n"
252 ";; no-update-autoloads: t\n"
258 (defun autoload-ensure-default-file (file)
259 "Make sure that the autoload file FILE exists and if not create it."
260 (unless (file-exists-p file
)
261 (write-region (autoload-rubric file
) nil file
))
264 (defun autoload-insert-section-header (outbuf autoloads load-name file time
)
265 "Insert the section-header line,
266 which lists the file name and which functions are in it, etc."
267 (insert generate-autoload-section-header
)
268 (prin1 (list 'autoloads autoloads load-name file time
)
271 ;; Break that line at spaces, to avoid very long lines.
272 ;; Make each sub-line into a comment.
273 (with-current-buffer outbuf
278 (skip-chars-forward "^ \n")
280 (insert "\n" generate-autoload-section-continuation
))))))
282 (defun autoload-find-file (file)
283 "Fetch file and put it in a temp buffer. Return the buffer."
284 ;; It is faster to avoid visiting the file.
285 (setq file
(expand-file-name file
))
286 (with-current-buffer (get-buffer-create " *autoload-file*")
287 (kill-all-local-variables)
289 (setq buffer-undo-list t
290 buffer-read-only nil
)
292 (setq default-directory
(file-name-directory file
))
293 (insert-file-contents file nil
)
294 (let ((enable-local-variables :safe
))
295 (hack-local-variables))
298 (defvar no-update-autoloads nil
299 "File local variable to prevent scanning this file for autoload cookies.")
301 (defun autoload-file-load-name (file)
302 (let ((name (file-name-nondirectory file
)))
303 (if (string-match "\\.elc?\\(\\.\\|\\'\\)" name
)
304 (substring name
0 (match-beginning 0))
307 (defun generate-file-autoloads (file)
308 "Insert at point a loaddefs autoload section for FILE.
309 Autoloads are generated for defuns and defmacros in FILE
310 marked by `generate-autoload-cookie' (which see).
311 If FILE is being visited in a buffer, the contents of the buffer
313 Return non-nil in the case where no autoloads were added at point."
314 (interactive "fGenerate autoloads for file: ")
315 (autoload-generate-file-autoloads file
(current-buffer)))
317 ;; When called from `generate-file-autoloads' we should ignore
318 ;; `generated-autoload-file' altogether. When called from
319 ;; `update-file-autoloads' we don't know `outbuf'. And when called from
320 ;; `update-directory-autoloads' it's in between: we know the default
321 ;; `outbuf' but we should obey any file-local setting of
322 ;; `generated-autoload-file'.
323 (defun autoload-generate-file-autoloads (file &optional outbuf outfile
)
324 "Insert an autoload section for FILE in the appropriate buffer.
325 Autoloads are generated for defuns and defmacros in FILE
326 marked by `generate-autoload-cookie' (which see).
327 If FILE is being visited in a buffer, the contents of the buffer are used.
328 OUTBUF is the buffer in which the autoload statements should be inserted.
329 If OUTBUF is nil, it will be determined by `autoload-generated-file'.
331 If provided, OUTFILE is expected to be the file name of OUTBUF.
332 If OUTFILE is non-nil and FILE specifies a `generated-autoload-file'
333 different from OUTFILE, then OUTBUF is ignored.
335 Return non-nil if and only if FILE adds no autoloads to OUTFILE
336 \(or OUTBUF if OUTFILE is nil)."
338 (let ((autoloads-done '())
339 (load-name (autoload-file-load-name file
))
342 (print-readably t
) ; This does something in Lucid Emacs.
343 (float-output-format nil
)
344 (visited (get-file-buffer file
))
346 (absfile (expand-file-name file
))
348 ;; nil until we found a cookie.
351 (with-current-buffer (or visited
352 ;; It is faster to avoid visiting the file.
353 (autoload-find-file file
))
354 ;; Obey the no-update-autoloads file local variable.
355 (unless no-update-autoloads
356 (message "Generating autoloads for %s..." file
)
360 (goto-char (point-min))
362 (skip-chars-forward " \t\n\f")
364 ((looking-at (regexp-quote generate-autoload-cookie
))
365 ;; If not done yet, figure out where to insert this text.
368 (not (equal outfile
(autoload-generated-file))))
369 ;; A file-local setting of autoload-generated-file says
370 ;; we should ignore OUTBUF.
374 (setq outbuf
(autoload-find-destination absfile
))
376 ;; The file has autoload cookies, but they're
377 ;; already up-to-date. If OUTFILE is nil, the
378 ;; entries are in the expected OUTBUF, otherwise
379 ;; they're elsewhere.
380 (throw 'done outfile
)))
381 (with-current-buffer outbuf
382 (setq relfile
(file-relative-name absfile
))
383 (setq output-start
(point)))
384 ;; (message "file=%S, relfile=%S, dest=%S"
385 ;; file relfile (autoload-generated-file))
387 (search-forward generate-autoload-cookie
)
388 (skip-chars-forward " \t")
391 ;; Read the next form and make an autoload.
392 (let* ((form (prog1 (read (current-buffer))
393 (or (bolp) (forward-line 1))))
394 (autoload (make-autoload form load-name
)))
396 (push (nth 1 form
) autoloads-done
)
397 (setq autoload form
))
398 (let ((autoload-print-form-outbuf outbuf
))
399 (autoload-print-form autoload
)))
401 (message "Error in %s: %S" file err
)))
403 ;; Copy the rest of the line to the output.
404 (princ (buffer-substring
406 ;; Back up over whitespace, to preserve it.
407 (skip-chars-backward " \f\t")
408 (if (= (char-after (1+ (point))) ?
)
412 (progn (forward-line 1) (point)))
415 ;; Don't read the comment.
419 (forward-line 1))))))
422 (let ((secondary-autoloads-file-buf
423 (if (local-variable-p 'generated-autoload-file
)
425 (with-current-buffer outbuf
427 ;; Insert the section-header line which lists the file name
428 ;; and which functions are in it, etc.
429 (goto-char output-start
)
430 (autoload-insert-section-header
431 outbuf autoloads-done load-name relfile
432 (if secondary-autoloads-file-buf
433 ;; MD5 checksums are much better because they do not
434 ;; change unless the file changes (so they'll be
435 ;; equal on two different systems and will change
436 ;; less often than time-stamps, thus leading to fewer
437 ;; unneeded changes causing spurious conflicts), but
438 ;; using time-stamps is a very useful optimization,
439 ;; so we use time-stamps for the main autoloads file
440 ;; (loaddefs.el) where we have special ways to
441 ;; circumvent the "random change problem", and MD5
442 ;; checksum in secondary autoload files where we do
443 ;; not need the time-stamp optimization because it is
444 ;; already provided by the primary autoloads file.
445 (md5 secondary-autoloads-file-buf
446 ;; We'd really want to just use
447 ;; `emacs-internal' instead.
448 nil nil
'emacs-mule-unix
)
449 (nth 5 (file-attributes relfile
))))
450 (insert ";;; Generated autoloads from " relfile
"\n"))
451 (insert generate-autoload-section-trailer
))))
452 (message "Generating autoloads for %s...done" file
))
454 ;; We created this buffer, so we should kill it.
455 (kill-buffer (current-buffer))))
456 ;; If the entries were added to some other buffer, then the file
457 ;; doesn't add entries to OUTFILE.
458 (or (not output-start
) otherbuf
))))
460 (defun autoload-save-buffers ()
461 (while autoload-modified-buffers
462 (with-current-buffer (pop autoload-modified-buffers
)
466 (defun update-file-autoloads (file &optional save-after
)
467 "Update the autoloads for FILE in `generated-autoload-file'
468 \(which FILE might bind in its local variables).
469 If SAVE-AFTER is non-nil (which is always, when called interactively),
472 Return FILE if there was no autoload cookie in it, else nil."
473 (interactive "fUpdate autoloads for file: \np")
474 (let* ((autoload-modified-buffers nil
)
475 (no-autoloads (autoload-generate-file-autoloads file
)))
476 (if autoload-modified-buffers
477 (if save-after
(autoload-save-buffers))
479 (message "Autoload section for %s is up to date." file
)))
480 (if no-autoloads file
)))
482 (defun autoload-find-destination (file)
483 "Find the destination point of the current buffer's autoloads.
484 FILE is the file name of the current buffer.
485 Returns a buffer whose point is placed at the requested location.
486 Returns nil if the file's autoloads are uptodate, otherwise
487 removes any prior now out-of-date autoload entries."
489 (let* ((load-name (autoload-file-load-name file
))
490 (buf (current-buffer))
491 (existing-buffer (if buffer-file-name buf
))
494 ;; We used to use `raw-text' to read this file, but this causes
495 ;; problems when the file contains non-ASCII characters.
497 (autoload-ensure-default-file (autoload-generated-file)))
498 ;; This is to make generated-autoload-file have Unix EOLs, so
499 ;; that it is portable to all platforms.
500 (unless (zerop (coding-system-eol-type buffer-file-coding-system
))
501 (set-buffer-file-coding-system 'unix
))
502 (or (> (buffer-size) 0)
503 (error "Autoloads file %s does not exist" buffer-file-name
))
504 (or (file-writable-p buffer-file-name
)
505 (error "Autoloads file %s is not writable" buffer-file-name
))
507 (goto-char (point-min))
508 ;; Look for the section for LOAD-NAME.
509 (while (and (not found
)
510 (search-forward generate-autoload-section-header nil t
))
511 (let ((form (autoload-read-section-header)))
512 (cond ((string= (nth 2 form
) load-name
)
513 ;; We found the section for this file.
514 ;; Check if it is up to date.
515 (let ((begin (match-beginning 0))
516 (last-time (nth 4 form
))
517 (file-time (nth 5 (file-attributes file
))))
518 (if (and (or (null existing-buffer
)
519 (not (buffer-modified-p existing-buffer
)))
521 ;; last-time is the time-stamp (specifying
522 ;; the last time we looked at the file) and
523 ;; the file hasn't been changed since.
524 (and (listp last-time
) (= (length last-time
) 2)
525 (not (time-less-p last-time file-time
)))
526 ;; last-time is an MD5 checksum instead.
527 (and (stringp last-time
)
529 (md5 buf nil nil
'emacs-mule
)))))
530 (throw 'up-to-date nil
)
531 (autoload-remove-section begin
)
533 ((string< load-name
(nth 2 form
))
534 ;; We've come to a section alphabetically later than
535 ;; LOAD-NAME. We assume the file is in order and so
536 ;; there must be no section for LOAD-NAME. We will
537 ;; insert one before the section here.
538 (goto-char (match-beginning 0))
542 ;; No later sections in the file. Put before the last page.
543 (goto-char (point-max))
544 (search-backward "\f" nil t
)))
545 (unless (memq (current-buffer) autoload-modified-buffers
)
546 (push (current-buffer) autoload-modified-buffers
))
549 (defun autoload-remove-section (begin)
551 (search-forward generate-autoload-section-trailer
)
552 (delete-region begin
(point)))
555 (defun update-directory-autoloads (&rest dirs
)
557 Update loaddefs.el with all the current autoloads from DIRS, and no old ones.
558 This uses `update-file-autoloads' (which see) to do its work.
559 In an interactive call, you must give one argument, the name
560 of a single directory. In a call from Lisp, you can supply multiple
561 directories as separate arguments, but this usage is discouraged.
563 The function does NOT recursively descend into subdirectories of the
564 directory or directories specified."
565 (interactive "DUpdate autoloads from directory: ")
566 (let* ((files-re (let ((tmp nil
))
567 (dolist (suf (get-load-suffixes)
568 (concat "^[^=.].*" (regexp-opt tmp t
) "\\'"))
569 (unless (string-match "\\.elc" suf
) (push suf tmp
)))))
571 (mapcar (lambda (dir)
572 (directory-files (expand-file-name dir
)
576 (this-time (current-time))
577 ;; Files with no autoload cookies or whose autoloads go to other
578 ;; files because of file-local autoload-generated-file settings.
580 (autoload-modified-buffers nil
))
584 (autoload-ensure-default-file (autoload-generated-file)))
587 ;; Canonicalize file names and remove the autoload file itself.
588 (setq files
(delete (file-relative-name buffer-file-name
)
589 (mapcar 'file-relative-name files
)))
591 (goto-char (point-min))
592 (while (search-forward generate-autoload-section-header nil t
)
593 (let* ((form (autoload-read-section-header))
595 (cond ((and (consp file
) (stringp (car file
)))
596 ;; This is a list of files that have no autoload cookies.
597 ;; There shouldn't be more than one such entry.
598 ;; Remove the obsolete section.
599 (autoload-remove-section (match-beginning 0))
600 (let ((last-time (nth 4 form
)))
602 (let ((file-time (nth 5 (file-attributes file
))))
604 (not (time-less-p last-time file-time
)))
606 (push file no-autoloads
)
607 (setq files
(delete file files
)))))))
608 ((not (stringp file
)))
609 ((or (not (file-exists-p file
))
610 ;; Remove duplicates as well, just in case.
612 ;; Remove the obsolete section.
613 (autoload-remove-section (match-beginning 0)))
614 ((not (time-less-p (nth 4 form
)
615 (nth 5 (file-attributes file
))))
616 ;; File hasn't changed.
619 (autoload-remove-section (match-beginning 0))
620 (if (autoload-generate-file-autoloads
621 file
(current-buffer) buffer-file-name
)
622 (push file no-autoloads
))))
624 (setq files
(delete file files
)))))
625 ;; Elements remaining in FILES have no existing autoload sections yet.
627 (if (autoload-generate-file-autoloads file nil buffer-file-name
)
628 (push file no-autoloads
)))
631 ;; Sort them for better readability.
632 (setq no-autoloads
(sort no-autoloads
'string
<))
633 ;; Add the `no-autoloads' section.
634 (goto-char (point-max))
635 (search-backward "\f" nil t
)
636 (autoload-insert-section-header
637 (current-buffer) nil nil no-autoloads this-time
)
638 (insert generate-autoload-section-trailer
))
641 ;; In case autoload entries were added to other files because of
642 ;; file-local autoload-generated-file settings.
643 (autoload-save-buffers))))
645 (define-obsolete-function-alias 'update-autoloads-from-directories
646 'update-directory-autoloads
"22.1")
649 (defun batch-update-autoloads ()
650 "Update loaddefs.el autoloads in batch mode.
651 Calls `update-directory-autoloads' on the command line arguments."
652 (let ((args command-line-args-left
))
653 (setq command-line-args-left nil
)
654 (apply 'update-directory-autoloads args
)))
658 ;; arch-tag: 00244766-98f4-4767-bf42-8a22103441c6
659 ;;; autoload.el ends here