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 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 2, 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.")
44 (put 'generated-autoload-file 'safe-local-variable 'stringp)
46 ;; This feels like it should be a defconst, but MH-E sets it to
47 ;; ";;;###mh-autoload" for the autoloads that are to go into mh-loaddefs.el.
48 (defvar generate-autoload-cookie ";;;###autoload"
49 "Magic comment indicating the following form should be autoloaded.
50 Used by \\[update-file-autoloads]. This string should be
51 meaningless to Lisp (e.g., a comment).
56 \(defun function-to-be-autoloaded () ...)
58 If this string appears alone on a line, the following form will be
59 read and an autoload made for it. If there is further text on the line,
60 that text will be copied verbatim to `generated-autoload-file'.")
62 (defconst generate-autoload-section-header "\f\n;;;### "
63 "String that marks the form at the start of a new file's autoload section.")
65 (defconst generate-autoload-section-trailer "\n;;;***\n"
66 "String which indicates the end of the section of autoloads for a file.")
68 (defconst generate-autoload-section-continuation ";;;;;; "
69 "String to add on each continuation of the section header form.")
71 (defun make-autoload (form file)
72 "Turn FORM into an autoload or defvar for source file FILE.
73 Returns nil if FORM is not a special autoload form (i.e. a function definition
74 or macro definition or a defcustom)."
75 (let ((car (car-safe form)) expand)
77 ;; For complex cases, try again on the macro-expansion.
78 ((and (memq car '(easy-mmode-define-global-mode define-global-minor-mode
79 define-globalized-minor-mode
80 easy-mmode-define-minor-mode define-minor-mode))
81 (setq expand (let ((load-file-name file)) (macroexpand form)))
82 (eq (car expand) 'progn)
83 (memq :autoload-end expand))
84 (let ((end (memq :autoload-end expand)))
85 ;; Cut-off anything after the :autoload-end marker.
88 (mapcar (lambda (form) (make-autoload form file))
91 ;; For special function-like operators, use the `autoload' function.
92 ((memq car '(defun define-skeleton defmacro define-derived-mode
93 define-compilation-mode define-generic-mode
94 easy-mmode-define-global-mode define-global-minor-mode
95 define-globalized-minor-mode
96 easy-mmode-define-minor-mode define-minor-mode
98 (let* ((macrop (memq car '(defmacro defmacro*)))
101 ((defun defmacro defun* defmacro*) (nth 2 form))
102 ((define-skeleton) '(&optional str arg))
103 ((define-generic-mode define-derived-mode
104 define-compilation-mode) nil)
106 (body (nthcdr (get car 'doc-string-elt) form))
107 (doc (if (stringp (car body)) (pop body))))
109 ;; Add the usage form at the end where describe-function-1
111 (setq doc (help-add-fundoc-usage doc args)))
112 ;; `define-generic-mode' quotes the name, so take care of that
113 (list 'autoload (if (listp name) name (list 'quote name)) file doc
114 (or (and (memq car '(define-skeleton define-derived-mode
116 easy-mmode-define-global-mode
117 define-global-minor-mode
118 define-globalized-minor-mode
119 easy-mmode-define-minor-mode
120 define-minor-mode)) t)
121 (eq (car-safe (car body)) 'interactive))
122 (if macrop (list 'quote 'macro) nil))))
124 ;; Convert defcustom to less space-consuming data.
126 (let ((varname (car-safe (cdr-safe form)))
127 (init (car-safe (cdr-safe (cdr-safe form))))
128 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form)))))
129 ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
132 (defvar ,varname ,init ,doc)
133 (custom-autoload ',varname ,file
135 (null (cadr (memq :set form)))
139 ;; In Emacs this is normally handled separately by cus-dep.el, but for
140 ;; third party packages, it can be convenient to explicitly autoload
142 (let ((groupname (nth 1 form)))
143 `(let ((loads (get ',groupname 'custom-loads)))
144 (if (member ',file loads) nil
145 (put ',groupname 'custom-loads (cons ',file loads))))))
147 ;; nil here indicates that this is not a special autoload form.
150 ;; Forms which have doc-strings which should be printed specially.
151 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
152 ;; the doc-string in FORM.
153 ;; Those properties are now set in lisp-mode.el.
155 (defun autoload-generated-file ()
156 (expand-file-name generated-autoload-file
157 (expand-file-name "lisp"
160 (defun autoload-trim-file-name (file)
161 ;; Returns a relative file path for FILE
162 ;; starting from the directory that loaddefs.el is in.
163 ;; That is normally a directory in load-path,
164 ;; which means Emacs will be able to find FILE when it looks.
165 ;; Any extra directory names here would prevent finding the file.
166 (setq file (expand-file-name file))
167 (file-relative-name file
168 (file-name-directory generated-autoload-file)))
170 (defun autoload-read-section-header ()
171 "Read a section header form.
172 Since continuation lines have been marked as comments,
173 we must copy the text of the form and remove those comment
174 markers before we call `read'."
176 (let ((beginning (point))
179 (while (looking-at generate-autoload-section-continuation)
181 (setq string (buffer-substring beginning (point)))
182 (with-current-buffer (get-buffer-create " *autoload*")
185 (goto-char (point-min))
186 (while (search-forward generate-autoload-section-continuation nil t)
188 (goto-char (point-min))
189 (read (current-buffer))))))
191 (defvar autoload-print-form-outbuf nil
192 "Buffer which gets the output of `autoload-print-form'.")
194 (defun autoload-print-form (form)
195 "Print FORM such that `make-docfile' will find the docstrings.
196 The variable `autoload-print-form-outbuf' specifies the buffer to
199 ;; If the form is a sequence, recurse.
200 ((eq (car form) 'progn) (mapcar 'autoload-print-form (cdr form)))
201 ;; Symbols at the toplevel are meaningless.
204 (let ((doc-string-elt (get (car-safe form) 'doc-string-elt))
205 (outbuf autoload-print-form-outbuf))
206 (if (and doc-string-elt (stringp (nth doc-string-elt form)))
207 ;; We need to hack the printing because the
208 ;; doc-string must be printed specially for
209 ;; make-docfile (sigh).
210 (let* ((p (nthcdr (1- doc-string-elt) form))
214 (let ((print-escape-newlines t)
215 (print-escape-nonascii t))
219 (princ "\"\\\n" outbuf)
220 (let ((begin (with-current-buffer outbuf (point))))
221 (princ (substring (prin1-to-string (car elt)) 1)
223 ;; Insert a backslash before each ( that
224 ;; appears at the beginning of a line in
226 (with-current-buffer outbuf
228 (while (re-search-backward "\n[[(]" begin t)
234 (princ (substring (prin1-to-string (cdr elt)) 1)
237 (let ((print-escape-newlines t)
238 (print-escape-nonascii t))
239 (print form outbuf)))))))
241 (defun autoload-ensure-default-file (file)
242 "Make sure that the autoload file FILE exists and if not create it."
243 (unless (file-exists-p file)
245 (concat ";;; " (file-name-nondirectory file)
246 " --- automatically extracted autoloads\n"
249 "
\f\n;; Local Variables:\n"
250 ";; version-control: never\n"
251 ";; no-byte-compile: t\n"
252 ";; no-update-autoloads: t\n"
254 ";;; " (file-name-nondirectory file)
259 (defun autoload-insert-section-header (outbuf autoloads load-name file time)
260 "Insert the section-header line,
261 which lists the file name and which functions are in it, etc."
262 (insert generate-autoload-section-header)
263 (prin1 (list 'autoloads autoloads load-name
264 (if (stringp file) (autoload-trim-file-name file) file)
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 (defun autoload-generate-file-autoloads (file &optional outbuf)
315 "Insert an autoload section for FILE in the appropriate buffer.
316 Autoloads are generated for defuns and defmacros in FILE
317 marked by `generate-autoload-cookie' (which see).
318 If FILE is being visited in a buffer, the contents of the buffer are used.
319 OUTBUF is the buffer in which the autoload statements will be inserted.
320 If OUTBUF is nil, it will be determined by `autoload-generated-file'.
321 Return non-nil in the case where no autoloads were added to OUTBUF.
323 Can throw `up-to-date' to mean that the entries were found already and are
324 up-to-date. Of course, this can only be the case if OUTBUF is not used."
325 (let ((autoloads-done '())
326 (load-name (autoload-file-load-name file))
328 (print-readably t) ; This does something in Lucid Emacs.
329 (float-output-format nil)
330 (visited (get-file-buffer file))
331 (absfile (expand-file-name file))
333 ;; nil until we found a cookie.
336 (with-current-buffer (or visited
337 ;; It is faster to avoid visiting the file.
338 (autoload-find-file file))
339 ;; Obey the no-update-autoloads file local variable.
340 (unless no-update-autoloads
341 (message "Generating autoloads for %s..." file)
345 (goto-char (point-min))
347 (skip-chars-forward " \t\n\f")
349 ((looking-at (regexp-quote generate-autoload-cookie))
350 ;; If not done yet, figure out where to insert this text.
353 (setq outbuf (autoload-find-destination absfile)))
354 (with-current-buffer outbuf
355 (setq relfile (file-relative-name absfile))
356 (setq output-start (point)))
357 ;; (message "file=%S, relfile=%S, dest=%S"
358 ;; file relfile (autoload-generated-file))
360 (search-forward generate-autoload-cookie)
361 (skip-chars-forward " \t")
364 ;; Read the next form and make an autoload.
365 (let* ((form (prog1 (read (current-buffer))
366 (or (bolp) (forward-line 1))))
367 (autoload (make-autoload form load-name)))
369 (push (nth 1 form) autoloads-done)
370 (setq autoload form))
371 (let ((autoload-print-form-outbuf outbuf))
372 (autoload-print-form autoload)))
374 (message "Error in %s: %S" file err)))
376 ;; Copy the rest of the line to the output.
377 (princ (buffer-substring
379 ;; Back up over whitespace, to preserve it.
380 (skip-chars-backward " \f\t")
381 (if (= (char-after (1+ (point))) ? )
385 (progn (forward-line 1) (point)))
388 ;; Don't read the comment.
392 (forward-line 1))))))
395 (with-current-buffer outbuf
397 ;; Insert the section-header line which lists the file name
398 ;; and which functions are in it, etc.
399 (goto-char output-start)
400 (autoload-insert-section-header
401 outbuf autoloads-done load-name relfile
402 (nth 5 (file-attributes relfile)))
403 (insert ";;; Generated autoloads from " relfile "\n"))
404 (insert generate-autoload-section-trailer)))
405 (message "Generating autoloads for %s...done" file))
407 ;; We created this buffer, so we should kill it.
408 (kill-buffer (current-buffer))))
411 (defvar autoload-modified-buffers nil)
413 (defun autoload-save-buffers ()
414 (while autoload-modified-buffers
415 (with-current-buffer (pop autoload-modified-buffers)
419 (defun update-file-autoloads (file &optional save-after)
420 "Update the autoloads for FILE in `generated-autoload-file'
421 \(which FILE might bind in its local variables).
422 If SAVE-AFTER is non-nil (which is always, when called interactively),
425 Return FILE if there was no autoload cookie in it, else nil."
426 (interactive "fUpdate autoloads for file: \np")
427 (let ((no-autoloads nil))
428 (if (catch 'up-to-date
430 (setq no-autoloads (autoload-generate-file-autoloads file))
432 (if save-after (autoload-save-buffers))
434 (message "Autoload section for %s is up to date." file)))
435 ;; If we caught `up-to-date', it means there are autoload entries, since
436 ;; otherwise we wouldn't have detected their up-to-dateness.
437 (if no-autoloads file)))
439 (defun autoload-find-destination (file)
440 "Find the destination point of the current buffer's autoloads.
441 FILE is the file name of the current buffer.
442 Returns a buffer whose point is placed at the requested location.
443 Throws `up-to-date' if the file's autoloads are uptodate, otherwise
444 removes any prior now out-of-date autoload entries.
445 The current buffer only matters if it is visiting a file or if it has a buffer-local
446 value for some variables such as `generated-autoload-file', so it's OK
447 to call it from a dummy buffer if FILE is not currently visited."
448 ;; (message "autoload-find-destination %S" file)
449 (let ((load-name (autoload-file-load-name file))
450 (existing-buffer (if buffer-file-name (current-buffer)))
453 ;; We must read/write the file without any code conversion,
454 ;; but still decode EOLs.
455 (let ((coding-system-for-read 'raw-text))
457 (autoload-ensure-default-file (autoload-generated-file))))
458 ;; This is to make generated-autoload-file have Unix EOLs, so
459 ;; that it is portable to all platforms.
460 (setq buffer-file-coding-system 'raw-text-unix)
461 (or (> (buffer-size) 0)
462 (error "Autoloads file %s does not exist" buffer-file-name))
463 (or (file-writable-p buffer-file-name)
464 (error "Autoloads file %s is not writable" buffer-file-name))
466 (goto-char (point-min))
467 ;; Look for the section for LOAD-NAME.
468 (while (and (not found)
469 (search-forward generate-autoload-section-header nil t))
470 (let ((form (autoload-read-section-header)))
471 (cond ((string= (nth 2 form) load-name)
472 ;; We found the section for this file.
473 ;; Check if it is up to date.
474 (let ((begin (match-beginning 0))
475 (last-time (nth 4 form))
476 (file-time (nth 5 (file-attributes file))))
477 (if (and (or (null existing-buffer)
478 (not (buffer-modified-p existing-buffer)))
479 (listp last-time) (= (length last-time) 2)
480 (not (time-less-p last-time file-time)))
481 (throw 'up-to-date nil)
482 (autoload-remove-section begin)
484 ((string< load-name (nth 2 form))
485 ;; We've come to a section alphabetically later than
486 ;; LOAD-NAME. We assume the file is in order and so
487 ;; there must be no section for LOAD-NAME. We will
488 ;; insert one before the section here.
489 (goto-char (match-beginning 0))
493 ;; No later sections in the file. Put before the last page.
494 (goto-char (point-max))
495 (search-backward "\f" nil t)))
496 (unless (memq (current-buffer) autoload-modified-buffers)
497 (push (current-buffer) autoload-modified-buffers))
500 (defun autoload-remove-section (begin)
502 (search-forward generate-autoload-section-trailer)
503 (delete-region begin (point)))
506 (defun update-directory-autoloads (&rest dirs)
508 Update loaddefs.el with all the current autoloads from DIRS, and no old ones.
509 This uses `update-file-autoloads' (which see) to do its work.
510 In an interactive call, you must give one argument, the name
511 of a single directory. In a call from Lisp, you can supply multiple
512 directories as separate arguments, but this usage is discouraged.
514 The function does NOT recursively descend into subdirectories of the
515 directory or directories specified."
516 (interactive "DUpdate autoloads from directory: ")
517 (let* ((files-re (let ((tmp nil))
518 (dolist (suf (get-load-suffixes)
519 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
520 (unless (string-match "\\.elc" suf) (push suf tmp)))))
522 (mapcar (lambda (dir)
523 (directory-files (expand-file-name dir)
526 (this-time (current-time))
527 (no-autoloads nil) ;files with no autoload cookies.
528 (autoloads-file (autoload-generated-file))
529 (top-dir (file-name-directory autoloads-file)))
532 (find-file-noselect (autoload-ensure-default-file autoloads-file))
535 ;; Canonicalize file names and remove the autoload file itself.
536 (setq files (delete (autoload-trim-file-name buffer-file-name)
537 (mapcar 'autoload-trim-file-name files)))
539 (goto-char (point-min))
540 (while (search-forward generate-autoload-section-header nil t)
541 (let* ((form (autoload-read-section-header))
543 (cond ((and (consp file) (stringp (car file)))
544 ;; This is a list of files that have no autoload cookies.
545 ;; There shouldn't be more than one such entry.
546 ;; Remove the obsolete section.
547 (autoload-remove-section (match-beginning 0))
548 (let ((last-time (nth 4 form)))
550 (let ((file-time (nth 5 (file-attributes file))))
552 (not (time-less-p last-time file-time)))
554 (push file no-autoloads)
555 (setq files (delete file files)))))))
556 ((not (stringp file)))
557 ((not (file-exists-p (expand-file-name file top-dir)))
558 ;; Remove the obsolete section.
559 (autoload-remove-section (match-beginning 0)))
560 ((equal (nth 4 form) (nth 5 (file-attributes file)))
561 ;; File hasn't changed.
564 (update-file-autoloads file)))
565 (setq files (delete file files)))))
566 ;; Elements remaining in FILES have no existing autoload sections yet.
569 (delq nil (mapcar 'update-file-autoloads files))))
571 ;; Sort them for better readability.
572 (setq no-autoloads (sort no-autoloads 'string<))
573 ;; Add the `no-autoloads' section.
574 (goto-char (point-max))
575 (search-backward "\f" nil t)
576 (autoload-insert-section-header
577 (current-buffer) nil nil no-autoloads this-time)
578 (insert generate-autoload-section-trailer))
582 (define-obsolete-function-alias 'update-autoloads-from-directories
583 'update-directory-autoloads "22.1")
586 (defun batch-update-autoloads ()
587 "Update loaddefs.el autoloads in batch mode.
588 Calls `update-directory-autoloads' on the command line arguments."
589 (apply 'update-directory-autoloads command-line-args-left)
590 (setq command-line-args-left nil))
594 ;; arch-tag: 00244766-98f4-4767-bf42-8a22103441c6
595 ;;; autoload.el ends here