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
)
244 (concat ";;; " (file-name-nondirectory file
)
245 " --- automatically extracted autoloads\n"
248 "\f\n;; Local Variables:\n"
249 ";; version-control: never\n"
250 ";; no-byte-compile: t\n"
251 ";; no-update-autoloads: t\n"
253 ";;; " (file-name-nondirectory file
)
258 (defun autoload-insert-section-header (outbuf autoloads load-name file time
)
259 "Insert the section-header line,
260 which lists the file name and which functions are in it, etc."
261 (insert generate-autoload-section-header
)
262 (prin1 (list 'autoloads autoloads load-name file time
)
265 ;; Break that line at spaces, to avoid very long lines.
266 ;; Make each sub-line into a comment.
267 (with-current-buffer outbuf
272 (skip-chars-forward "^ \n")
274 (insert "\n" generate-autoload-section-continuation
))))))
276 (defun autoload-find-file (file)
277 "Fetch file and put it in a temp buffer. Return the buffer."
278 ;; It is faster to avoid visiting the file.
279 (setq file
(expand-file-name file
))
280 (with-current-buffer (get-buffer-create " *autoload-file*")
281 (kill-all-local-variables)
283 (setq buffer-undo-list t
284 buffer-read-only nil
)
286 (setq default-directory
(file-name-directory file
))
287 (insert-file-contents file nil
)
288 (let ((enable-local-variables :safe
))
289 (hack-local-variables))
292 (defvar no-update-autoloads nil
293 "File local variable to prevent scanning this file for autoload cookies.")
295 (defun autoload-file-load-name (file)
296 (let ((name (file-name-nondirectory file
)))
297 (if (string-match "\\.elc?\\(\\.\\|\\'\\)" name
)
298 (substring name
0 (match-beginning 0))
301 (defun generate-file-autoloads (file)
302 "Insert at point a loaddefs autoload section for FILE.
303 Autoloads are generated for defuns and defmacros in FILE
304 marked by `generate-autoload-cookie' (which see).
305 If FILE is being visited in a buffer, the contents of the buffer
307 Return non-nil in the case where no autoloads were added at point."
308 (interactive "fGenerate autoloads for file: ")
309 (autoload-generate-file-autoloads file
(current-buffer)))
311 ;; When called from `generate-file-autoloads' we should ignore
312 ;; `generated-autoload-file' altogether. When called from
313 ;; `update-file-autoloads' we don't know `outbuf'. And when called from
314 ;; `update-directory-autoloads' it's in between: we know the default
315 ;; `outbuf' but we should obey any file-local setting of
316 ;; `generated-autoload-file'.
317 (defun autoload-generate-file-autoloads (file &optional outbuf outfile
)
318 "Insert an autoload section for FILE in the appropriate buffer.
319 Autoloads are generated for defuns and defmacros in FILE
320 marked by `generate-autoload-cookie' (which see).
321 If FILE is being visited in a buffer, the contents of the buffer are used.
322 OUTBUF is the buffer in which the autoload statements should be inserted.
323 If OUTBUF is nil, it will be determined by `autoload-generated-file'.
325 If provided, OUTFILE is expected to be the file name of OUTBUF.
326 If OUTFILE is non-nil and FILE specifies a `generated-autoload-file'
327 different from OUTFILE, then OUTBUF is ignored.
329 Return non-nil iff FILE adds no autoloads to OUTFILE
330 \(or OUTBUF if OUTFILE is nil)."
332 (let ((autoloads-done '())
333 (load-name (autoload-file-load-name file
))
335 (print-readably t
) ; This does something in Lucid Emacs.
336 (float-output-format nil
)
337 (visited (get-file-buffer file
))
339 (absfile (expand-file-name file
))
341 ;; nil until we found a cookie.
344 (with-current-buffer (or visited
345 ;; It is faster to avoid visiting the file.
346 (autoload-find-file file
))
347 ;; Obey the no-update-autoloads file local variable.
348 (unless no-update-autoloads
349 (message "Generating autoloads for %s..." file
)
353 (goto-char (point-min))
355 (skip-chars-forward " \t\n\f")
357 ((looking-at (regexp-quote generate-autoload-cookie
))
358 ;; If not done yet, figure out where to insert this text.
361 (not (equal outfile
(autoload-generated-file))))
362 ;; A file-local setting of autoload-generated-file says
363 ;; we should ignore OUTBUF.
367 (setq outbuf
(autoload-find-destination absfile
))
369 ;; The file has autoload cookies, but they're
370 ;; already up-to-date. If OUTFILE is nil, the
371 ;; entries are in the expected OUTBUF, otherwise
372 ;; they're elsewhere.
373 (throw 'done outfile
)))
374 (with-current-buffer outbuf
375 (setq relfile
(file-relative-name absfile
))
376 (setq output-start
(point)))
377 ;; (message "file=%S, relfile=%S, dest=%S"
378 ;; file relfile (autoload-generated-file))
380 (search-forward generate-autoload-cookie
)
381 (skip-chars-forward " \t")
384 ;; Read the next form and make an autoload.
385 (let* ((form (prog1 (read (current-buffer))
386 (or (bolp) (forward-line 1))))
387 (autoload (make-autoload form load-name
)))
389 (push (nth 1 form
) autoloads-done
)
390 (setq autoload form
))
391 (let ((autoload-print-form-outbuf outbuf
))
392 (autoload-print-form autoload
)))
394 (message "Error in %s: %S" file err
)))
396 ;; Copy the rest of the line to the output.
397 (princ (buffer-substring
399 ;; Back up over whitespace, to preserve it.
400 (skip-chars-backward " \f\t")
401 (if (= (char-after (1+ (point))) ?
)
405 (progn (forward-line 1) (point)))
408 ;; Don't read the comment.
412 (forward-line 1))))))
415 (let ((secondary-autoloads-file-buf
416 (if (local-variable-p 'generated-autoload-file
)
418 (with-current-buffer outbuf
420 ;; Insert the section-header line which lists the file name
421 ;; and which functions are in it, etc.
422 (goto-char output-start
)
423 (autoload-insert-section-header
424 outbuf autoloads-done load-name relfile
425 (if secondary-autoloads-file-buf
426 ;; MD5 checksums are much better because they do not
427 ;; change unless the file changes (so they'll be
428 ;; equal on two different systems and will change
429 ;; less often than time-stamps, thus leading to fewer
430 ;; unneeded changes causing spurious conflicts), but
431 ;; using time-stamps is a very useful optimization,
432 ;; so we use time-stamps for the main autoloads file
433 ;; (loaddefs.el) where we have special ways to
434 ;; circumvent the "random change problem", and MD5
435 ;; checksum in secondary autoload files where we do
436 ;; not need the time-stamp optimization because it is
437 ;; already provided by the primary autoloads file.
438 (md5 secondary-autoloads-file-buf
439 ;; We'd really want to just use
440 ;; `emacs-internal' instead.
441 nil nil
'emacs-mule-unix
)
442 (nth 5 (file-attributes relfile
))))
443 (insert ";;; Generated autoloads from " relfile
"\n"))
444 (insert generate-autoload-section-trailer
))))
445 (message "Generating autoloads for %s...done" file
))
447 ;; We created this buffer, so we should kill it.
448 (kill-buffer (current-buffer))))
449 ;; If the entries were added to some other buffer, then the file
450 ;; doesn't add entries to OUTFILE.
451 (or (not output-start
) otherbuf
))))
453 (defun autoload-save-buffers ()
454 (while autoload-modified-buffers
455 (with-current-buffer (pop autoload-modified-buffers
)
459 (defun update-file-autoloads (file &optional save-after
)
460 "Update the autoloads for FILE in `generated-autoload-file'
461 \(which FILE might bind in its local variables).
462 If SAVE-AFTER is non-nil (which is always, when called interactively),
465 Return FILE if there was no autoload cookie in it, else nil."
466 (interactive "fUpdate autoloads for file: \np")
467 (let* ((autoload-modified-buffers nil
)
468 (no-autoloads (autoload-generate-file-autoloads file
)))
469 (if autoload-modified-buffers
470 (if save-after
(autoload-save-buffers))
472 (message "Autoload section for %s is up to date." file
)))
473 (if no-autoloads file
)))
475 (defun autoload-find-destination (file)
476 "Find the destination point of the current buffer's autoloads.
477 FILE is the file name of the current buffer.
478 Returns a buffer whose point is placed at the requested location.
479 Returns nil if the file's autoloads are uptodate, otherwise
480 removes any prior now out-of-date autoload entries."
482 (let* ((load-name (autoload-file-load-name file
))
483 (buf (current-buffer))
484 (existing-buffer (if buffer-file-name buf
))
487 ;; We used to use `raw-text' to read this file, but this causes
488 ;; problems when the file contains non-ASCII characters.
490 (autoload-ensure-default-file (autoload-generated-file)))
491 ;; This is to make generated-autoload-file have Unix EOLs, so
492 ;; that it is portable to all platforms.
493 (unless (zerop (coding-system-eol-type buffer-file-coding-system
))
494 (set-buffer-file-coding-system 'unix
))
495 (or (> (buffer-size) 0)
496 (error "Autoloads file %s does not exist" buffer-file-name
))
497 (or (file-writable-p buffer-file-name
)
498 (error "Autoloads file %s is not writable" buffer-file-name
))
500 (goto-char (point-min))
501 ;; Look for the section for LOAD-NAME.
502 (while (and (not found
)
503 (search-forward generate-autoload-section-header nil t
))
504 (let ((form (autoload-read-section-header)))
505 (cond ((string= (nth 2 form
) load-name
)
506 ;; We found the section for this file.
507 ;; Check if it is up to date.
508 (let ((begin (match-beginning 0))
509 (last-time (nth 4 form
))
510 (file-time (nth 5 (file-attributes file
))))
511 (if (and (or (null existing-buffer
)
512 (not (buffer-modified-p existing-buffer
)))
514 ;; last-time is the time-stamp (specifying
515 ;; the last time we looked at the file) and
516 ;; the file hasn't been changed since.
517 (and (listp last-time
) (= (length last-time
) 2)
518 (not (time-less-p last-time file-time
)))
519 ;; last-time is an MD5 checksum instead.
520 (and (stringp last-time
)
522 (md5 buf nil nil
'emacs-mule
)))))
523 (throw 'up-to-date nil
)
524 (autoload-remove-section begin
)
526 ((string< load-name
(nth 2 form
))
527 ;; We've come to a section alphabetically later than
528 ;; LOAD-NAME. We assume the file is in order and so
529 ;; there must be no section for LOAD-NAME. We will
530 ;; insert one before the section here.
531 (goto-char (match-beginning 0))
535 ;; No later sections in the file. Put before the last page.
536 (goto-char (point-max))
537 (search-backward "\f" nil t
)))
538 (unless (memq (current-buffer) autoload-modified-buffers
)
539 (push (current-buffer) autoload-modified-buffers
))
542 (defun autoload-remove-section (begin)
544 (search-forward generate-autoload-section-trailer
)
545 (delete-region begin
(point)))
548 (defun update-directory-autoloads (&rest dirs
)
550 Update loaddefs.el with all the current autoloads from DIRS, and no old ones.
551 This uses `update-file-autoloads' (which see) to do its work.
552 In an interactive call, you must give one argument, the name
553 of a single directory. In a call from Lisp, you can supply multiple
554 directories as separate arguments, but this usage is discouraged.
556 The function does NOT recursively descend into subdirectories of the
557 directory or directories specified."
558 (interactive "DUpdate autoloads from directory: ")
559 (let* ((files-re (let ((tmp nil
))
560 (dolist (suf (get-load-suffixes)
561 (concat "^[^=.].*" (regexp-opt tmp t
) "\\'"))
562 (unless (string-match "\\.elc" suf
) (push suf tmp
)))))
564 (mapcar (lambda (dir)
565 (directory-files (expand-file-name dir
)
569 (this-time (current-time))
570 ;; Files with no autoload cookies or whose autoloads go to other
571 ;; files because of file-local autoload-generated-file settings.
573 (autoload-modified-buffers nil
))
577 (autoload-ensure-default-file (autoload-generated-file)))
580 ;; Canonicalize file names and remove the autoload file itself.
581 (setq files
(delete (file-relative-name buffer-file-name
)
582 (mapcar 'file-relative-name files
)))
584 (goto-char (point-min))
585 (while (search-forward generate-autoload-section-header nil t
)
586 (let* ((form (autoload-read-section-header))
588 (cond ((and (consp file
) (stringp (car file
)))
589 ;; This is a list of files that have no autoload cookies.
590 ;; There shouldn't be more than one such entry.
591 ;; Remove the obsolete section.
592 (autoload-remove-section (match-beginning 0))
593 (let ((last-time (nth 4 form
)))
595 (let ((file-time (nth 5 (file-attributes file
))))
597 (not (time-less-p last-time file-time
)))
599 (push file no-autoloads
)
600 (setq files
(delete file files
)))))))
601 ((not (stringp file
)))
602 ((or (not (file-exists-p file
))
603 ;; Remove duplicates as well, just in case.
605 ;; Remove the obsolete section.
606 (autoload-remove-section (match-beginning 0)))
607 ((not (time-less-p (nth 4 form
)
608 (nth 5 (file-attributes file
))))
609 ;; File hasn't changed.
612 (autoload-remove-section (match-beginning 0))
613 (if (autoload-generate-file-autoloads
614 file
(current-buffer) buffer-file-name
)
615 (push file no-autoloads
))))
617 (setq files
(delete file files
)))))
618 ;; Elements remaining in FILES have no existing autoload sections yet.
620 (if (autoload-generate-file-autoloads file nil buffer-file-name
)
621 (push file no-autoloads
)))
624 ;; Sort them for better readability.
625 (setq no-autoloads
(sort no-autoloads
'string
<))
626 ;; Add the `no-autoloads' section.
627 (goto-char (point-max))
628 (search-backward "\f" nil t
)
629 (autoload-insert-section-header
630 (current-buffer) nil nil no-autoloads this-time
)
631 (insert generate-autoload-section-trailer
))
634 ;; In case autoload entries were added to other files because of
635 ;; file-local autoload-generated-file settings.
636 (autoload-save-buffers))))
638 (define-obsolete-function-alias 'update-autoloads-from-directories
639 'update-directory-autoloads
"22.1")
642 (defun batch-update-autoloads ()
643 "Update loaddefs.el autoloads in batch mode.
644 Calls `update-directory-autoloads' on the command line arguments."
645 (let ((args command-line-args-left
))
646 (setq command-line-args-left nil
)
647 (apply 'update-directory-autoloads args
)))
651 ;; arch-tag: 00244766-98f4-4767-bf42-8a22103441c6
652 ;;; autoload.el ends here