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 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, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This code helps GNU Emacs maintainers keep the loaddefs.el file up to
29 ;; date. It interprets magic cookies of the form ";;;###autoload" in
30 ;; lisp source files in various useful ways. To learn more, read the
31 ;; source; if you're going to use this, you'd better be able to.
35 (require 'lisp-mode
) ;for `doc-string-elt' properties.
36 (require 'help-fns
) ;for help-add-fundoc-usage.
37 (eval-when-compile (require 'cl
))
39 (defvar generated-autoload-file
"loaddefs.el"
40 "*File \\[update-file-autoloads] puts autoloads into.
41 A `.el' file can set this in its local variables section to make its
42 autoloads go somewhere else. The autoload file is assumed to contain a
43 trailer starting with a FormFeed character.")
45 (put 'generated-autoload-file
'safe-local-variable
'stringp
)
47 ;; This feels like it should be a defconst, but MH-E sets it to
48 ;; ";;;###mh-autoload" for the autoloads that are to go into mh-loaddefs.el.
49 (defvar generate-autoload-cookie
";;;###autoload"
50 "Magic comment indicating the following form should be autoloaded.
51 Used by \\[update-file-autoloads]. This string should be
52 meaningless to Lisp (e.g., a comment).
57 \(defun function-to-be-autoloaded () ...)
59 If this string appears alone on a line, the following form will be
60 read and an autoload made for it. If there is further text on the line,
61 that text will be copied verbatim to `generated-autoload-file'.")
63 (defconst generate-autoload-section-header
"\f\n;;;### "
64 "String that marks the form at the start of a new file's autoload section.")
66 (defconst generate-autoload-section-trailer
"\n;;;***\n"
67 "String which indicates the end of the section of autoloads for a file.")
69 (defconst generate-autoload-section-continuation
";;;;;; "
70 "String to add on each continuation of the section header form.")
72 (defvar autoload-modified-buffers
) ;Dynamically scoped var.
74 (defun make-autoload (form file
)
75 "Turn FORM into an autoload or defvar for source file FILE.
76 Returns nil if FORM is not a special autoload form (i.e. a function definition
77 or macro definition or a defcustom)."
78 (let ((car (car-safe form
)) expand
)
80 ;; For complex cases, try again on the macro-expansion.
81 ((and (memq car
'(easy-mmode-define-global-mode define-global-minor-mode
82 define-globalized-minor-mode
83 easy-mmode-define-minor-mode define-minor-mode
))
84 (setq expand
(let ((load-file-name file
)) (macroexpand form
)))
85 (eq (car expand
) 'progn
)
86 (memq :autoload-end expand
))
87 (let ((end (memq :autoload-end expand
)))
88 ;; Cut-off anything after the :autoload-end marker.
91 (mapcar (lambda (form) (make-autoload form file
))
94 ;; For special function-like operators, use the `autoload' function.
95 ((memq car
'(defun define-skeleton defmacro define-derived-mode
96 define-compilation-mode define-generic-mode
97 easy-mmode-define-global-mode define-global-minor-mode
98 define-globalized-minor-mode
99 easy-mmode-define-minor-mode define-minor-mode
101 (let* ((macrop (memq car
'(defmacro defmacro
*)))
104 ((defun defmacro defun
* defmacro
*) (nth 2 form
))
105 ((define-skeleton) '(&optional str arg
))
106 ((define-generic-mode define-derived-mode
107 define-compilation-mode
) nil
)
109 (body (nthcdr (get car
'doc-string-elt
) form
))
110 (doc (if (stringp (car body
)) (pop body
))))
112 ;; Add the usage form at the end where describe-function-1
114 (setq doc
(help-add-fundoc-usage doc args
)))
115 ;; `define-generic-mode' quotes the name, so take care of that
116 (list 'autoload
(if (listp name
) name
(list 'quote name
)) file doc
117 (or (and (memq car
'(define-skeleton define-derived-mode
119 easy-mmode-define-global-mode
120 define-global-minor-mode
121 define-globalized-minor-mode
122 easy-mmode-define-minor-mode
123 define-minor-mode
)) t
)
124 (eq (car-safe (car body
)) 'interactive
))
125 (if macrop
(list 'quote
'macro
) nil
))))
127 ;; Convert defcustom to less space-consuming data.
129 (let ((varname (car-safe (cdr-safe form
)))
130 (init (car-safe (cdr-safe (cdr-safe form
))))
131 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form
)))))
132 ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
135 (defvar ,varname
,init
,doc
)
136 (custom-autoload ',varname
,file
138 (null (cadr (memq :set form
)))
142 ;; In Emacs this is normally handled separately by cus-dep.el, but for
143 ;; third party packages, it can be convenient to explicitly autoload
145 (let ((groupname (nth 1 form
)))
146 `(let ((loads (get ',groupname
'custom-loads
)))
147 (if (member ',file loads
) nil
148 (put ',groupname
'custom-loads
(cons ',file loads
))))))
150 ;; nil here indicates that this is not a special autoload form.
153 ;; Forms which have doc-strings which should be printed specially.
154 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
155 ;; the doc-string in FORM.
156 ;; Those properties are now set in lisp-mode.el.
158 (defun autoload-generated-file ()
159 (expand-file-name generated-autoload-file
160 ;; File-local settings of generated-autoload-file should
161 ;; be interpreted relative to the file's location,
163 (if (not (local-variable-p 'generated-autoload-file
))
164 (expand-file-name "lisp" source-directory
))))
167 (defun autoload-read-section-header ()
168 "Read a section header form.
169 Since continuation lines have been marked as comments,
170 we must copy the text of the form and remove those comment
171 markers before we call `read'."
173 (let ((beginning (point))
176 (while (looking-at generate-autoload-section-continuation
)
178 (setq string
(buffer-substring beginning
(point)))
179 (with-current-buffer (get-buffer-create " *autoload*")
182 (goto-char (point-min))
183 (while (search-forward generate-autoload-section-continuation nil t
)
185 (goto-char (point-min))
186 (read (current-buffer))))))
188 (defvar autoload-print-form-outbuf nil
189 "Buffer which gets the output of `autoload-print-form'.")
191 (defun autoload-print-form (form)
192 "Print FORM such that `make-docfile' will find the docstrings.
193 The variable `autoload-print-form-outbuf' specifies the buffer to
196 ;; If the form is a sequence, recurse.
197 ((eq (car form
) 'progn
) (mapcar 'autoload-print-form
(cdr form
)))
198 ;; Symbols at the toplevel are meaningless.
201 (let ((doc-string-elt (get (car-safe form
) 'doc-string-elt
))
202 (outbuf autoload-print-form-outbuf
))
203 (if (and doc-string-elt
(stringp (nth doc-string-elt form
)))
204 ;; We need to hack the printing because the
205 ;; doc-string must be printed specially for
206 ;; make-docfile (sigh).
207 (let* ((p (nthcdr (1- doc-string-elt
) form
))
211 (let ((print-escape-newlines t
)
213 (print-escape-nonascii t
))
217 (princ "\"\\\n" outbuf
)
218 (let ((begin (with-current-buffer outbuf
(point))))
219 (princ (substring (prin1-to-string (car elt
)) 1)
221 ;; Insert a backslash before each ( that
222 ;; appears at the beginning of a line in
224 (with-current-buffer outbuf
226 (while (re-search-backward "\n[[(]" begin t
)
232 (princ (substring (prin1-to-string (cdr elt
)) 1)
235 (let ((print-escape-newlines t
)
237 (print-escape-nonascii t
))
238 (print form outbuf
)))))))
240 (defun autoload-ensure-default-file (file)
241 "Make sure that the autoload file FILE exists and if not create it."
242 (unless (file-exists-p file
)
243 (let ((basename (file-name-nondirectory file
)))
245 (concat ";;; " basename
246 " --- automatically extracted autoloads\n"
250 "(provide '" (file-name-sans-extension basename
) ")\n"
251 ";; Local Variables:\n"
252 ";; version-control: never\n"
253 ";; no-byte-compile: t\n"
254 ";; no-update-autoloads: t\n"
261 (defun autoload-insert-section-header (outbuf autoloads load-name file time
)
262 "Insert the section-header line,
263 which lists the file name and which functions are in it, etc."
264 (insert generate-autoload-section-header
)
265 (prin1 (list 'autoloads autoloads load-name file time
)
268 ;; Break that line at spaces, to avoid very long lines.
269 ;; Make each sub-line into a comment.
270 (with-current-buffer outbuf
275 (skip-chars-forward "^ \n")
277 (insert "\n" generate-autoload-section-continuation
))))))
279 (defun autoload-find-file (file)
280 "Fetch file and put it in a temp buffer. Return the buffer."
281 ;; It is faster to avoid visiting the file.
282 (setq file
(expand-file-name file
))
283 (with-current-buffer (get-buffer-create " *autoload-file*")
284 (kill-all-local-variables)
286 (setq buffer-undo-list t
287 buffer-read-only nil
)
289 (setq default-directory
(file-name-directory file
))
290 (insert-file-contents file nil
)
291 (let ((enable-local-variables :safe
))
292 (hack-local-variables))
295 (defvar no-update-autoloads nil
296 "File local variable to prevent scanning this file for autoload cookies.")
298 (defun autoload-file-load-name (file)
299 (let ((name (file-name-nondirectory file
)))
300 (if (string-match "\\.elc?\\(\\.\\|\\'\\)" name
)
301 (substring name
0 (match-beginning 0))
304 (defun generate-file-autoloads (file)
305 "Insert at point a loaddefs autoload section for FILE.
306 Autoloads are generated for defuns and defmacros in FILE
307 marked by `generate-autoload-cookie' (which see).
308 If FILE is being visited in a buffer, the contents of the buffer
310 Return non-nil in the case where no autoloads were added at point."
311 (interactive "fGenerate autoloads for file: ")
312 (autoload-generate-file-autoloads file
(current-buffer)))
314 ;; When called from `generate-file-autoloads' we should ignore
315 ;; `generated-autoload-file' altogether. When called from
316 ;; `update-file-autoloads' we don't know `outbuf'. And when called from
317 ;; `update-directory-autoloads' it's in between: we know the default
318 ;; `outbuf' but we should obey any file-local setting of
319 ;; `generated-autoload-file'.
320 (defun autoload-generate-file-autoloads (file &optional outbuf outfile
)
321 "Insert an autoload section for FILE in the appropriate buffer.
322 Autoloads are generated for defuns and defmacros in FILE
323 marked by `generate-autoload-cookie' (which see).
324 If FILE is being visited in a buffer, the contents of the buffer are used.
325 OUTBUF is the buffer in which the autoload statements should be inserted.
326 If OUTBUF is nil, it will be determined by `autoload-generated-file'.
328 If provided, OUTFILE is expected to be the file name of OUTBUF.
329 If OUTFILE is non-nil and FILE specifies a `generated-autoload-file'
330 different from OUTFILE, then OUTBUF is ignored.
332 Return non-nil iff FILE adds no autoloads to OUTFILE
333 \(or OUTBUF if OUTFILE is nil)."
335 (let ((autoloads-done '())
336 (load-name (autoload-file-load-name file
))
339 (print-readably t
) ; This does something in Lucid Emacs.
340 (float-output-format nil
)
341 (visited (get-file-buffer file
))
343 (absfile (expand-file-name file
))
345 ;; nil until we found a cookie.
348 (with-current-buffer (or visited
349 ;; It is faster to avoid visiting the file.
350 (autoload-find-file file
))
351 ;; Obey the no-update-autoloads file local variable.
352 (unless no-update-autoloads
353 (message "Generating autoloads for %s..." file
)
357 (goto-char (point-min))
359 (skip-chars-forward " \t\n\f")
361 ((looking-at (regexp-quote generate-autoload-cookie
))
362 ;; If not done yet, figure out where to insert this text.
365 (not (equal outfile
(autoload-generated-file))))
366 ;; A file-local setting of autoload-generated-file says
367 ;; we should ignore OUTBUF.
371 (setq outbuf
(autoload-find-destination absfile
))
373 ;; The file has autoload cookies, but they're
374 ;; already up-to-date. If OUTFILE is nil, the
375 ;; entries are in the expected OUTBUF, otherwise
376 ;; they're elsewhere.
377 (throw 'done outfile
)))
378 (with-current-buffer outbuf
379 (setq relfile
(file-relative-name absfile
))
380 (setq output-start
(point)))
381 ;; (message "file=%S, relfile=%S, dest=%S"
382 ;; file relfile (autoload-generated-file))
384 (search-forward generate-autoload-cookie
)
385 (skip-chars-forward " \t")
388 ;; Read the next form and make an autoload.
389 (let* ((form (prog1 (read (current-buffer))
390 (or (bolp) (forward-line 1))))
391 (autoload (make-autoload form load-name
)))
393 (push (nth 1 form
) autoloads-done
)
394 (setq autoload form
))
395 (let ((autoload-print-form-outbuf outbuf
))
396 (autoload-print-form autoload
)))
398 (message "Error in %s: %S" file err
)))
400 ;; Copy the rest of the line to the output.
401 (princ (buffer-substring
403 ;; Back up over whitespace, to preserve it.
404 (skip-chars-backward " \f\t")
405 (if (= (char-after (1+ (point))) ?
)
409 (progn (forward-line 1) (point)))
412 ;; Don't read the comment.
416 (forward-line 1))))))
419 (let ((secondary-autoloads-file-buf
420 (if (local-variable-p 'generated-autoload-file
)
422 (with-current-buffer outbuf
424 ;; Insert the section-header line which lists the file name
425 ;; and which functions are in it, etc.
426 (goto-char output-start
)
427 (autoload-insert-section-header
428 outbuf autoloads-done load-name relfile
429 (if secondary-autoloads-file-buf
430 ;; MD5 checksums are much better because they do not
431 ;; change unless the file changes (so they'll be
432 ;; equal on two different systems and will change
433 ;; less often than time-stamps, thus leading to fewer
434 ;; unneeded changes causing spurious conflicts), but
435 ;; using time-stamps is a very useful optimization,
436 ;; so we use time-stamps for the main autoloads file
437 ;; (loaddefs.el) where we have special ways to
438 ;; circumvent the "random change problem", and MD5
439 ;; checksum in secondary autoload files where we do
440 ;; not need the time-stamp optimization because it is
441 ;; already provided by the primary autoloads file.
442 (md5 secondary-autoloads-file-buf
443 ;; We'd really want to just use
444 ;; `emacs-internal' instead.
445 nil nil
'emacs-mule-unix
)
446 (nth 5 (file-attributes relfile
))))
447 (insert ";;; Generated autoloads from " relfile
"\n"))
448 (insert generate-autoload-section-trailer
))))
449 (message "Generating autoloads for %s...done" file
))
451 ;; We created this buffer, so we should kill it.
452 (kill-buffer (current-buffer))))
453 ;; If the entries were added to some other buffer, then the file
454 ;; doesn't add entries to OUTFILE.
455 (or (not output-start
) otherbuf
))))
457 (defun autoload-save-buffers ()
458 (while autoload-modified-buffers
459 (with-current-buffer (pop autoload-modified-buffers
)
463 (defun update-file-autoloads (file &optional save-after
)
464 "Update the autoloads for FILE in `generated-autoload-file'
465 \(which FILE might bind in its local variables).
466 If SAVE-AFTER is non-nil (which is always, when called interactively),
469 Return FILE if there was no autoload cookie in it, else nil."
470 (interactive "fUpdate autoloads for file: \np")
471 (let* ((autoload-modified-buffers nil
)
472 (no-autoloads (autoload-generate-file-autoloads file
)))
473 (if autoload-modified-buffers
474 (if save-after
(autoload-save-buffers))
476 (message "Autoload section for %s is up to date." file
)))
477 (if no-autoloads file
)))
479 (defun autoload-find-destination (file)
480 "Find the destination point of the current buffer's autoloads.
481 FILE is the file name of the current buffer.
482 Returns a buffer whose point is placed at the requested location.
483 Returns nil if the file's autoloads are uptodate, otherwise
484 removes any prior now out-of-date autoload entries."
486 (let* ((load-name (autoload-file-load-name file
))
487 (buf (current-buffer))
488 (existing-buffer (if buffer-file-name buf
))
491 ;; We used to use `raw-text' to read this file, but this causes
492 ;; problems when the file contains non-ASCII characters.
494 (autoload-ensure-default-file (autoload-generated-file)))
495 ;; This is to make generated-autoload-file have Unix EOLs, so
496 ;; that it is portable to all platforms.
497 (unless (zerop (coding-system-eol-type buffer-file-coding-system
))
498 (set-buffer-file-coding-system 'unix
))
499 (or (> (buffer-size) 0)
500 (error "Autoloads file %s does not exist" buffer-file-name
))
501 (or (file-writable-p buffer-file-name
)
502 (error "Autoloads file %s is not writable" buffer-file-name
))
504 (goto-char (point-min))
505 ;; Look for the section for LOAD-NAME.
506 (while (and (not found
)
507 (search-forward generate-autoload-section-header nil t
))
508 (let ((form (autoload-read-section-header)))
509 (cond ((string= (nth 2 form
) load-name
)
510 ;; We found the section for this file.
511 ;; Check if it is up to date.
512 (let ((begin (match-beginning 0))
513 (last-time (nth 4 form
))
514 (file-time (nth 5 (file-attributes file
))))
515 (if (and (or (null existing-buffer
)
516 (not (buffer-modified-p existing-buffer
)))
518 ;; last-time is the time-stamp (specifying
519 ;; the last time we looked at the file) and
520 ;; the file hasn't been changed since.
521 (and (listp last-time
) (= (length last-time
) 2)
522 (not (time-less-p last-time file-time
)))
523 ;; last-time is an MD5 checksum instead.
524 (and (stringp last-time
)
526 (md5 buf nil nil
'emacs-mule
)))))
527 (throw 'up-to-date nil
)
528 (autoload-remove-section begin
)
530 ((string< load-name
(nth 2 form
))
531 ;; We've come to a section alphabetically later than
532 ;; LOAD-NAME. We assume the file is in order and so
533 ;; there must be no section for LOAD-NAME. We will
534 ;; insert one before the section here.
535 (goto-char (match-beginning 0))
539 ;; No later sections in the file. Put before the last page.
540 (goto-char (point-max))
541 (search-backward "\f" nil t
)))
542 (unless (memq (current-buffer) autoload-modified-buffers
)
543 (push (current-buffer) autoload-modified-buffers
))
546 (defun autoload-remove-section (begin)
548 (search-forward generate-autoload-section-trailer
)
549 (delete-region begin
(point)))
552 (defun update-directory-autoloads (&rest dirs
)
554 Update loaddefs.el with all the current autoloads from DIRS, and no old ones.
555 This uses `update-file-autoloads' (which see) to do its work.
556 In an interactive call, you must give one argument, the name
557 of a single directory. In a call from Lisp, you can supply multiple
558 directories as separate arguments, but this usage is discouraged.
560 The function does NOT recursively descend into subdirectories of the
561 directory or directories specified."
562 (interactive "DUpdate autoloads from directory: ")
563 (let* ((files-re (let ((tmp nil
))
564 (dolist (suf (get-load-suffixes)
565 (concat "^[^=.].*" (regexp-opt tmp t
) "\\'"))
566 (unless (string-match "\\.elc" suf
) (push suf tmp
)))))
568 (mapcar (lambda (dir)
569 (directory-files (expand-file-name dir
)
573 (this-time (current-time))
574 ;; Files with no autoload cookies or whose autoloads go to other
575 ;; files because of file-local autoload-generated-file settings.
577 (autoload-modified-buffers nil
))
581 (autoload-ensure-default-file (autoload-generated-file)))
584 ;; Canonicalize file names and remove the autoload file itself.
585 (setq files
(delete (file-relative-name buffer-file-name
)
586 (mapcar 'file-relative-name files
)))
588 (goto-char (point-min))
589 (while (search-forward generate-autoload-section-header nil t
)
590 (let* ((form (autoload-read-section-header))
592 (cond ((and (consp file
) (stringp (car file
)))
593 ;; This is a list of files that have no autoload cookies.
594 ;; There shouldn't be more than one such entry.
595 ;; Remove the obsolete section.
596 (autoload-remove-section (match-beginning 0))
597 (let ((last-time (nth 4 form
)))
599 (let ((file-time (nth 5 (file-attributes file
))))
601 (not (time-less-p last-time file-time
)))
603 (push file no-autoloads
)
604 (setq files
(delete file files
)))))))
605 ((not (stringp file
)))
606 ((or (not (file-exists-p file
))
607 ;; Remove duplicates as well, just in case.
609 ;; Remove the obsolete section.
610 (autoload-remove-section (match-beginning 0)))
611 ((not (time-less-p (nth 4 form
)
612 (nth 5 (file-attributes file
))))
613 ;; File hasn't changed.
616 (autoload-remove-section (match-beginning 0))
617 (if (autoload-generate-file-autoloads
618 file
(current-buffer) buffer-file-name
)
619 (push file no-autoloads
))))
621 (setq files
(delete file files
)))))
622 ;; Elements remaining in FILES have no existing autoload sections yet.
624 (if (autoload-generate-file-autoloads file nil buffer-file-name
)
625 (push file no-autoloads
)))
628 ;; Sort them for better readability.
629 (setq no-autoloads
(sort no-autoloads
'string
<))
630 ;; Add the `no-autoloads' section.
631 (goto-char (point-max))
632 (search-backward "\f" nil t
)
633 (autoload-insert-section-header
634 (current-buffer) nil nil no-autoloads this-time
)
635 (insert generate-autoload-section-trailer
))
638 ;; In case autoload entries were added to other files because of
639 ;; file-local autoload-generated-file settings.
640 (autoload-save-buffers))))
642 (define-obsolete-function-alias 'update-autoloads-from-directories
643 'update-directory-autoloads
"22.1")
646 (defun batch-update-autoloads ()
647 "Update loaddefs.el autoloads in batch mode.
648 Calls `update-directory-autoloads' on the command line arguments."
649 (let ((args command-line-args-left
))
650 (setq command-line-args-left nil
)
651 (apply 'update-directory-autoloads args
)))
655 ;; arch-tag: 00244766-98f4-4767-bf42-8a22103441c6
656 ;;; autoload.el ends here