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 (defvar generated-autoload-feature nil
46 "Feature for `generated-autoload-file' to provide.
47 If nil, this defaults to `generated-autoload-file', sans extension.")
49 (put 'generated-autoload-feature
'safe-local-variable
'symbolp
)
51 (defvar generated-autoload-load-name nil
52 "Load name for `autoload' statements generated from autoload cookies.
53 If nil, this defaults to the file name, sans extension.")
55 (put 'generated-autoload-load-name
'safe-local-variable
'stringp
)
57 ;; This feels like it should be a defconst, but MH-E sets it to
58 ;; ";;;###mh-autoload" for the autoloads that are to go into mh-loaddefs.el.
59 (defvar generate-autoload-cookie
";;;###autoload"
60 "Magic comment indicating the following form should be autoloaded.
61 Used by \\[update-file-autoloads]. This string should be
62 meaningless to Lisp (e.g., a comment).
67 \(defun function-to-be-autoloaded () ...)
69 If this string appears alone on a line, the following form will be
70 read and an autoload made for it. If there is further text on the line,
71 that text will be copied verbatim to `generated-autoload-file'.")
73 (defconst generate-autoload-section-header
"\f\n;;;### "
74 "String that marks the form at the start of a new file's autoload section.")
76 (defconst generate-autoload-section-trailer
"\n;;;***\n"
77 "String which indicates the end of the section of autoloads for a file.")
79 (defconst generate-autoload-section-continuation
";;;;;; "
80 "String to add on each continuation of the section header form.")
82 (defvar autoload-modified-buffers
) ;Dynamically scoped var.
84 (defun make-autoload (form file
)
85 "Turn FORM into an autoload or defvar for source file FILE.
86 Returns nil if FORM is not a special autoload form (i.e. a function definition
87 or macro definition or a defcustom)."
88 (let ((car (car-safe form
)) expand
)
90 ;; For complex cases, try again on the macro-expansion.
91 ((and (memq car
'(easy-mmode-define-global-mode define-global-minor-mode
92 define-globalized-minor-mode
93 easy-mmode-define-minor-mode define-minor-mode
))
94 (setq expand
(let ((load-file-name file
)) (macroexpand form
)))
95 (eq (car expand
) 'progn
)
96 (memq :autoload-end expand
))
97 (let ((end (memq :autoload-end expand
)))
98 ;; Cut-off anything after the :autoload-end marker.
101 (mapcar (lambda (form) (make-autoload form file
))
104 ;; For special function-like operators, use the `autoload' function.
105 ((memq car
'(defun define-skeleton defmacro define-derived-mode
106 define-compilation-mode define-generic-mode
107 easy-mmode-define-global-mode define-global-minor-mode
108 define-globalized-minor-mode
109 easy-mmode-define-minor-mode define-minor-mode
110 defun
* defmacro
* define-overloadable-function
))
111 (let* ((macrop (memq car
'(defmacro defmacro
*)))
114 ((defun defmacro defun
* defmacro
*
115 define-overloadable-function
) (nth 2 form
))
116 ((define-skeleton) '(&optional str arg
))
117 ((define-generic-mode define-derived-mode
118 define-compilation-mode
) nil
)
120 (body (nthcdr (get car
'doc-string-elt
) form
))
121 (doc (if (stringp (car body
)) (pop body
))))
123 ;; Add the usage form at the end where describe-function-1
125 (setq doc
(help-add-fundoc-usage doc args
)))
126 ;; `define-generic-mode' quotes the name, so take care of that
127 (list 'autoload
(if (listp name
) name
(list 'quote name
)) file doc
128 (or (and (memq car
'(define-skeleton define-derived-mode
130 easy-mmode-define-global-mode
131 define-global-minor-mode
132 define-globalized-minor-mode
133 easy-mmode-define-minor-mode
134 define-minor-mode
)) t
)
135 (eq (car-safe (car body
)) 'interactive
))
136 (if macrop
(list 'quote
'macro
) nil
))))
138 ;; For defclass forms, use `eieio-defclass-autoload'.
140 (let ((name (nth 1 form
))
141 (superclasses (nth 2 form
))
143 (list 'eieio-defclass-autoload
(list 'quote name
)
144 (list 'quote superclasses
) file doc
)))
146 ;; Convert defcustom to less space-consuming data.
148 (let ((varname (car-safe (cdr-safe form
)))
149 (init (car-safe (cdr-safe (cdr-safe form
))))
150 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form
)))))
151 ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
154 (defvar ,varname
,init
,doc
)
155 (custom-autoload ',varname
,file
157 (null (cadr (memq :set form
)))
161 ;; In Emacs this is normally handled separately by cus-dep.el, but for
162 ;; third party packages, it can be convenient to explicitly autoload
164 (let ((groupname (nth 1 form
)))
165 `(let ((loads (get ',groupname
'custom-loads
)))
166 (if (member ',file loads
) nil
167 (put ',groupname
'custom-loads
(cons ',file loads
))))))
169 ;; nil here indicates that this is not a special autoload form.
172 ;; Forms which have doc-strings which should be printed specially.
173 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
174 ;; the doc-string in FORM.
175 ;; Those properties are now set in lisp-mode.el.
177 (defun autoload-generated-file ()
178 (expand-file-name generated-autoload-file
179 ;; File-local settings of generated-autoload-file should
180 ;; be interpreted relative to the file's location,
182 (if (not (local-variable-p 'generated-autoload-file
))
183 (expand-file-name "lisp" source-directory
))))
186 (defun autoload-read-section-header ()
187 "Read a section header form.
188 Since continuation lines have been marked as comments,
189 we must copy the text of the form and remove those comment
190 markers before we call `read'."
192 (let ((beginning (point))
195 (while (looking-at generate-autoload-section-continuation
)
197 (setq string
(buffer-substring beginning
(point)))
198 (with-current-buffer (get-buffer-create " *autoload*")
201 (goto-char (point-min))
202 (while (search-forward generate-autoload-section-continuation nil t
)
204 (goto-char (point-min))
205 (read (current-buffer))))))
207 (defvar autoload-print-form-outbuf nil
208 "Buffer which gets the output of `autoload-print-form'.")
210 (defun autoload-print-form (form)
211 "Print FORM such that `make-docfile' will find the docstrings.
212 The variable `autoload-print-form-outbuf' specifies the buffer to
215 ;; If the form is a sequence, recurse.
216 ((eq (car form
) 'progn
) (mapcar 'autoload-print-form
(cdr form
)))
217 ;; Symbols at the toplevel are meaningless.
220 (let ((doc-string-elt (get (car-safe form
) 'doc-string-elt
))
221 (outbuf autoload-print-form-outbuf
))
222 (if (and doc-string-elt
(stringp (nth doc-string-elt form
)))
223 ;; We need to hack the printing because the
224 ;; doc-string must be printed specially for
225 ;; make-docfile (sigh).
226 (let* ((p (nthcdr (1- doc-string-elt
) form
))
230 (let ((print-escape-newlines t
)
232 (print-escape-nonascii t
))
236 (princ "\"\\\n" outbuf
)
237 (let ((begin (with-current-buffer outbuf
(point))))
238 (princ (substring (prin1-to-string (car elt
)) 1)
240 ;; Insert a backslash before each ( that
241 ;; appears at the beginning of a line in
243 (with-current-buffer outbuf
245 (while (re-search-backward "\n[[(]" begin t
)
251 (princ (substring (prin1-to-string (cdr elt
)) 1)
254 (let ((print-escape-newlines t
)
256 (print-escape-nonascii t
))
257 (print form outbuf
)))))))
259 (defun autoload-rubric (file &optional type
)
260 "Return a string giving the appropriate autoload rubric for FILE.
261 TYPE (default \"autoloads\") is a string stating the type of
262 information contained in FILE."
263 (let ((basename (file-name-nondirectory file
)))
264 (concat ";;; " basename
265 " --- automatically extracted " (or type
"autoloads") "\n"
270 (if (and generated-autoload-feature
271 (symbolp generated-autoload-feature
))
272 (format "%s" generated-autoload-feature
)
273 (file-name-sans-extension basename
))
275 ";; Local Variables:\n"
276 ";; version-control: never\n"
277 ";; no-byte-compile: t\n"
278 ";; no-update-autoloads: t\n"
284 (defun autoload-ensure-default-file (file)
285 "Make sure that the autoload file FILE exists and if not create it."
286 (unless (file-exists-p file
)
287 (write-region (autoload-rubric file
) nil file
))
290 (defun autoload-insert-section-header (outbuf autoloads load-name file time
)
291 "Insert the section-header line,
292 which lists the file name and which functions are in it, etc."
293 (insert generate-autoload-section-header
)
294 (prin1 (list 'autoloads autoloads load-name file time
)
297 ;; Break that line at spaces, to avoid very long lines.
298 ;; Make each sub-line into a comment.
299 (with-current-buffer outbuf
304 (skip-chars-forward "^ \n")
306 (insert "\n" generate-autoload-section-continuation
))))))
308 (defun autoload-find-file (file)
309 "Fetch file and put it in a temp buffer. Return the buffer."
310 ;; It is faster to avoid visiting the file.
311 (setq file
(expand-file-name file
))
312 (with-current-buffer (get-buffer-create " *autoload-file*")
313 (kill-all-local-variables)
315 (setq buffer-undo-list t
316 buffer-read-only nil
)
318 (setq default-directory
(file-name-directory file
))
319 (insert-file-contents file nil
)
320 (let ((enable-local-variables :safe
))
321 (hack-local-variables))
324 (defvar no-update-autoloads nil
325 "File local variable to prevent scanning this file for autoload cookies.")
327 (defun autoload-file-load-name (file)
328 (let ((name (file-name-nondirectory file
)))
329 (if (string-match "\\.elc?\\(\\.\\|\\'\\)" name
)
330 (substring name
0 (match-beginning 0))
333 (defun generate-file-autoloads (file)
334 "Insert at point a loaddefs autoload section for FILE.
335 Autoloads are generated for defuns and defmacros in FILE
336 marked by `generate-autoload-cookie' (which see).
337 If FILE is being visited in a buffer, the contents of the buffer
339 Return non-nil in the case where no autoloads were added at point."
340 (interactive "fGenerate autoloads for file: ")
341 (autoload-generate-file-autoloads file
(current-buffer)))
343 ;; When called from `generate-file-autoloads' we should ignore
344 ;; `generated-autoload-file' altogether. When called from
345 ;; `update-file-autoloads' we don't know `outbuf'. And when called from
346 ;; `update-directory-autoloads' it's in between: we know the default
347 ;; `outbuf' but we should obey any file-local setting of
348 ;; `generated-autoload-file'.
349 (defun autoload-generate-file-autoloads (file &optional outbuf outfile
)
350 "Insert an autoload section for FILE in the appropriate buffer.
351 Autoloads are generated for defuns and defmacros in FILE
352 marked by `generate-autoload-cookie' (which see).
353 If FILE is being visited in a buffer, the contents of the buffer are used.
354 OUTBUF is the buffer in which the autoload statements should be inserted.
355 If OUTBUF is nil, it will be determined by `autoload-generated-file'.
357 If provided, OUTFILE is expected to be the file name of OUTBUF.
358 If OUTFILE is non-nil and FILE specifies a `generated-autoload-file'
359 different from OUTFILE, then OUTBUF is ignored.
361 Return non-nil if and only if FILE adds no autoloads to OUTFILE
362 \(or OUTBUF if OUTFILE is nil)."
364 (let ((autoloads-done '())
368 (print-readably t
) ; This does something in Lucid Emacs.
369 (float-output-format nil
)
370 (visited (get-file-buffer file
))
372 (absfile (expand-file-name file
))
374 ;; nil until we found a cookie.
377 (with-current-buffer (or visited
378 ;; It is faster to avoid visiting the file.
379 (autoload-find-file file
))
380 ;; Obey the no-update-autoloads file local variable.
381 (unless no-update-autoloads
382 (message "Generating autoloads for %s..." file
)
384 (if (stringp generated-autoload-load-name
)
385 generated-autoload-load-name
386 (autoload-file-load-name file
)))
390 (goto-char (point-min))
392 (skip-chars-forward " \t\n\f")
394 ((looking-at (regexp-quote generate-autoload-cookie
))
395 ;; If not done yet, figure out where to insert this text.
398 (not (equal outfile
(autoload-generated-file))))
399 ;; A file-local setting of autoload-generated-file says
400 ;; we should ignore OUTBUF.
404 (setq outbuf
(autoload-find-destination absfile
))
406 ;; The file has autoload cookies, but they're
407 ;; already up-to-date. If OUTFILE is nil, the
408 ;; entries are in the expected OUTBUF, otherwise
409 ;; they're elsewhere.
410 (throw 'done outfile
)))
411 (with-current-buffer outbuf
412 (setq relfile
(file-relative-name absfile
))
413 (setq output-start
(point)))
414 ;; (message "file=%S, relfile=%S, dest=%S"
415 ;; file relfile (autoload-generated-file))
417 (search-forward generate-autoload-cookie
)
418 (skip-chars-forward " \t")
421 ;; Read the next form and make an autoload.
422 (let* ((form (prog1 (read (current-buffer))
423 (or (bolp) (forward-line 1))))
424 (autoload (make-autoload form load-name
)))
426 (push (nth 1 form
) autoloads-done
)
427 (setq autoload form
))
428 (let ((autoload-print-form-outbuf outbuf
))
429 (autoload-print-form autoload
)))
431 (message "Error in %s: %S" file err
)))
433 ;; Copy the rest of the line to the output.
434 (princ (buffer-substring
436 ;; Back up over whitespace, to preserve it.
437 (skip-chars-backward " \f\t")
438 (if (= (char-after (1+ (point))) ?
)
442 (progn (forward-line 1) (point)))
445 ;; Don't read the comment.
449 (forward-line 1))))))
452 (let ((secondary-autoloads-file-buf
453 (if (local-variable-p 'generated-autoload-file
)
455 (with-current-buffer outbuf
457 ;; Insert the section-header line which lists the file name
458 ;; and which functions are in it, etc.
459 (goto-char output-start
)
460 (autoload-insert-section-header
461 outbuf autoloads-done load-name relfile
462 (if secondary-autoloads-file-buf
463 ;; MD5 checksums are much better because they do not
464 ;; change unless the file changes (so they'll be
465 ;; equal on two different systems and will change
466 ;; less often than time-stamps, thus leading to fewer
467 ;; unneeded changes causing spurious conflicts), but
468 ;; using time-stamps is a very useful optimization,
469 ;; so we use time-stamps for the main autoloads file
470 ;; (loaddefs.el) where we have special ways to
471 ;; circumvent the "random change problem", and MD5
472 ;; checksum in secondary autoload files where we do
473 ;; not need the time-stamp optimization because it is
474 ;; already provided by the primary autoloads file.
475 (md5 secondary-autoloads-file-buf
476 ;; We'd really want to just use
477 ;; `emacs-internal' instead.
478 nil nil
'emacs-mule-unix
)
479 (nth 5 (file-attributes relfile
))))
480 (insert ";;; Generated autoloads from " relfile
"\n"))
481 (insert generate-autoload-section-trailer
))))
482 (message "Generating autoloads for %s...done" file
))
484 ;; We created this buffer, so we should kill it.
485 (kill-buffer (current-buffer))))
486 ;; If the entries were added to some other buffer, then the file
487 ;; doesn't add entries to OUTFILE.
488 (or (not output-start
) otherbuf
))))
490 (defun autoload-save-buffers ()
491 (while autoload-modified-buffers
492 (with-current-buffer (pop autoload-modified-buffers
)
496 (defun update-file-autoloads (file &optional save-after
)
497 "Update the autoloads for FILE in `generated-autoload-file'
498 \(which FILE might bind in its local variables).
499 If SAVE-AFTER is non-nil (which is always, when called interactively),
502 Return FILE if there was no autoload cookie in it, else nil."
503 (interactive "fUpdate autoloads for file: \np")
504 (let* ((autoload-modified-buffers nil
)
505 (no-autoloads (autoload-generate-file-autoloads file
)))
506 (if autoload-modified-buffers
507 (if save-after
(autoload-save-buffers))
508 (if (called-interactively-p 'interactive
)
509 (message "Autoload section for %s is up to date." file
)))
510 (if no-autoloads file
)))
512 (defun autoload-find-destination (file)
513 "Find the destination point of the current buffer's autoloads.
514 FILE is the file name of the current buffer.
515 Returns a buffer whose point is placed at the requested location.
516 Returns nil if the file's autoloads are uptodate, otherwise
517 removes any prior now out-of-date autoload entries."
519 (let* ((load-name (autoload-file-load-name file
))
520 (buf (current-buffer))
521 (existing-buffer (if buffer-file-name buf
))
524 ;; We used to use `raw-text' to read this file, but this causes
525 ;; problems when the file contains non-ASCII characters.
527 (autoload-ensure-default-file (autoload-generated-file)))
528 ;; This is to make generated-autoload-file have Unix EOLs, so
529 ;; that it is portable to all platforms.
530 (unless (zerop (coding-system-eol-type buffer-file-coding-system
))
531 (set-buffer-file-coding-system 'unix
))
532 (or (> (buffer-size) 0)
533 (error "Autoloads file %s does not exist" buffer-file-name
))
534 (or (file-writable-p buffer-file-name
)
535 (error "Autoloads file %s is not writable" buffer-file-name
))
537 (goto-char (point-min))
538 ;; Look for the section for LOAD-NAME.
539 (while (and (not found
)
540 (search-forward generate-autoload-section-header nil t
))
541 (let ((form (autoload-read-section-header)))
542 (cond ((string= (nth 2 form
) load-name
)
543 ;; We found the section for this file.
544 ;; Check if it is up to date.
545 (let ((begin (match-beginning 0))
546 (last-time (nth 4 form
))
547 (file-time (nth 5 (file-attributes file
))))
548 (if (and (or (null existing-buffer
)
549 (not (buffer-modified-p existing-buffer
)))
551 ;; last-time is the time-stamp (specifying
552 ;; the last time we looked at the file) and
553 ;; the file hasn't been changed since.
554 (and (listp last-time
) (= (length last-time
) 2)
555 (not (time-less-p last-time file-time
)))
556 ;; last-time is an MD5 checksum instead.
557 (and (stringp last-time
)
559 (md5 buf nil nil
'emacs-mule
)))))
560 (throw 'up-to-date nil
)
561 (autoload-remove-section begin
)
563 ((string< load-name
(nth 2 form
))
564 ;; We've come to a section alphabetically later than
565 ;; LOAD-NAME. We assume the file is in order and so
566 ;; there must be no section for LOAD-NAME. We will
567 ;; insert one before the section here.
568 (goto-char (match-beginning 0))
572 ;; No later sections in the file. Put before the last page.
573 (goto-char (point-max))
574 (search-backward "\f" nil t
)))
575 (unless (memq (current-buffer) autoload-modified-buffers
)
576 (push (current-buffer) autoload-modified-buffers
))
579 (defun autoload-remove-section (begin)
581 (search-forward generate-autoload-section-trailer
)
582 (delete-region begin
(point)))
585 (defun update-directory-autoloads (&rest dirs
)
587 Update loaddefs.el with all the current autoloads from DIRS, and no old ones.
588 This uses `update-file-autoloads' (which see) to do its work.
589 In an interactive call, you must give one argument, the name
590 of a single directory. In a call from Lisp, you can supply multiple
591 directories as separate arguments, but this usage is discouraged.
593 The function does NOT recursively descend into subdirectories of the
594 directory or directories specified."
595 (interactive "DUpdate autoloads from directory: ")
596 (let* ((files-re (let ((tmp nil
))
597 (dolist (suf (get-load-suffixes)
598 (concat "^[^=.].*" (regexp-opt tmp t
) "\\'"))
599 (unless (string-match "\\.elc" suf
) (push suf tmp
)))))
601 (mapcar (lambda (dir)
602 (directory-files (expand-file-name dir
)
606 (this-time (current-time))
607 ;; Files with no autoload cookies or whose autoloads go to other
608 ;; files because of file-local autoload-generated-file settings.
610 (autoload-modified-buffers nil
))
614 (autoload-ensure-default-file (autoload-generated-file)))
617 ;; Canonicalize file names and remove the autoload file itself.
618 (setq files
(delete (file-relative-name buffer-file-name
)
619 (mapcar 'file-relative-name files
)))
621 (goto-char (point-min))
622 (while (search-forward generate-autoload-section-header nil t
)
623 (let* ((form (autoload-read-section-header))
625 (cond ((and (consp file
) (stringp (car file
)))
626 ;; This is a list of files that have no autoload cookies.
627 ;; There shouldn't be more than one such entry.
628 ;; Remove the obsolete section.
629 (autoload-remove-section (match-beginning 0))
630 (let ((last-time (nth 4 form
)))
632 (let ((file-time (nth 5 (file-attributes file
))))
634 (not (time-less-p last-time file-time
)))
636 (push file no-autoloads
)
637 (setq files
(delete file files
)))))))
638 ((not (stringp file
)))
639 ((or (not (file-exists-p file
))
640 ;; Remove duplicates as well, just in case.
642 ;; Remove the obsolete section.
643 (autoload-remove-section (match-beginning 0)))
644 ((not (time-less-p (nth 4 form
)
645 (nth 5 (file-attributes file
))))
646 ;; File hasn't changed.
649 (autoload-remove-section (match-beginning 0))
650 (if (autoload-generate-file-autoloads
651 file
(current-buffer) buffer-file-name
)
652 (push file no-autoloads
))))
654 (setq files
(delete file files
)))))
655 ;; Elements remaining in FILES have no existing autoload sections yet.
657 (if (autoload-generate-file-autoloads file nil buffer-file-name
)
658 (push file no-autoloads
)))
661 ;; Sort them for better readability.
662 (setq no-autoloads
(sort no-autoloads
'string
<))
663 ;; Add the `no-autoloads' section.
664 (goto-char (point-max))
665 (search-backward "\f" nil t
)
666 (autoload-insert-section-header
667 (current-buffer) nil nil no-autoloads this-time
)
668 (insert generate-autoload-section-trailer
))
671 ;; In case autoload entries were added to other files because of
672 ;; file-local autoload-generated-file settings.
673 (autoload-save-buffers))))
675 (define-obsolete-function-alias 'update-autoloads-from-directories
676 'update-directory-autoloads
"22.1")
679 (defun batch-update-autoloads ()
680 "Update loaddefs.el autoloads in batch mode.
681 Calls `update-directory-autoloads' on the command line arguments."
682 (let ((args command-line-args-left
))
683 (setq command-line-args-left nil
)
684 (apply 'update-directory-autoloads args
)))
688 ;; arch-tag: 00244766-98f4-4767-bf42-8a22103441c6
689 ;;; autoload.el ends here