1 ;; autoload.el --- maintain autoloads in loaddefs.el
3 ;; Copyright (C) 1991,92,93,94,95,96,97, 2001,02,03,04
4 ;; 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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, 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 (defconst generate-autoload-cookie
";;;###autoload"
46 "Magic comment indicating the following form should be autoloaded.
47 Used by \\[update-file-autoloads]. This string should be
48 meaningless to Lisp (e.g., a comment).
53 \(defun function-to-be-autoloaded () ...)
55 If this string appears alone on a line, the following form will be
56 read and an autoload made for it. If there is further text on the line,
57 that text will be copied verbatim to `generated-autoload-file'.")
59 (defconst generate-autoload-section-header
"\f\n;;;### "
60 "String that marks the form at the start of a new file's autoload section.")
62 (defconst generate-autoload-section-trailer
"\n;;;***\n"
63 "String which indicates the end of the section of autoloads for a file.")
65 (defconst generate-autoload-section-continuation
";;;;;; "
66 "String to add on each continuation of the section header form.")
68 (defun make-autoload (form file
)
69 "Turn FORM into an autoload or defvar for source file FILE.
70 Returns nil if FORM is not a special autoload form (i.e. a function definition
71 or macro definition or a defcustom)."
72 (let ((car (car-safe form
)) expand
)
74 ;; For complex cases, try again on the macro-expansion.
75 ((and (memq car
'(easy-mmode-define-global-mode
76 easy-mmode-define-minor-mode define-minor-mode
))
77 (setq expand
(let ((load-file-name file
)) (macroexpand form
)))
78 (eq (car expand
) 'progn
)
79 (memq :autoload-end expand
))
80 (let ((end (memq :autoload-end expand
)))
81 ;; Cut-off anything after the :autoload-end marker.
84 (mapcar (lambda (form) (make-autoload form file
))
87 ;; For special function-like operators, use the `autoload' function.
88 ((memq car
'(defun define-skeleton defmacro define-derived-mode
89 define-generic-mode easy-mmode-define-minor-mode
90 easy-mmode-define-global-mode
91 define-minor-mode defun
* defmacro
*))
92 (let* ((macrop (memq car
'(defmacro defmacro
*)))
95 ((defun defmacro defun
* defmacro
*) (nth 2 form
))
96 ((define-skeleton) '(&optional str arg
))
97 ((define-generic-mode define-derived-mode
) nil
)
99 (body (nthcdr (get car
'doc-string-elt
) form
))
100 (doc (if (stringp (car body
)) (pop body
))))
102 ;; Add the usage form at the end where describe-function-1
104 (setq doc
(help-add-fundoc-usage doc args
)))
105 ;; `define-generic-mode' quotes the name, so take care of that
106 (list 'autoload
(if (listp name
) name
(list 'quote name
)) file doc
107 (or (and (memq car
'(define-skeleton define-derived-mode
109 easy-mmode-define-global-mode
110 easy-mmode-define-minor-mode
111 define-minor-mode
)) t
)
112 (eq (car-safe (car body
)) 'interactive
))
113 (if macrop
(list 'quote
'macro
) nil
))))
115 ;; Convert defcustom to less space-consuming data.
117 (let ((varname (car-safe (cdr-safe form
)))
118 (init (car-safe (cdr-safe (cdr-safe form
))))
119 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form
)))))
120 ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
123 (defvar ,varname
,init
,doc
)
124 (custom-autoload ',varname
,file
))))
126 ;; nil here indicates that this is not a special autoload form.
129 ;; Forms which have doc-strings which should be printed specially.
130 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
131 ;; the doc-string in FORM.
132 ;; Those properties are now set in lisp-mode.el.
135 (defun autoload-trim-file-name (file)
136 ;; Returns a relative pathname of FILE
137 ;; starting from the directory that loaddefs.el is in.
138 ;; That is normally a directory in load-path,
139 ;; which means Emacs will be able to find FILE when it looks.
140 ;; Any extra directory names here would prevent finding the file.
141 (setq file
(expand-file-name file
))
142 (file-relative-name file
143 (file-name-directory generated-autoload-file
)))
145 (defun autoload-read-section-header ()
146 "Read a section header form.
147 Since continuation lines have been marked as comments,
148 we must copy the text of the form and remove those comment
149 markers before we call `read'."
151 (let ((beginning (point))
154 (while (looking-at generate-autoload-section-continuation
)
156 (setq string
(buffer-substring beginning
(point)))
157 (with-current-buffer (get-buffer-create " *autoload*")
160 (goto-char (point-min))
161 (while (search-forward generate-autoload-section-continuation nil t
)
163 (goto-char (point-min))
164 (read (current-buffer))))))
166 (defvar autoload-print-form-outbuf
)
168 (defun autoload-print-form (form)
169 "Print FORM such that `make-docfile' will find the docstrings.
170 The variable `autoload-print-form-outbuf' specifies the buffer to
173 ;; If the form is a sequence, recurse.
174 ((eq (car form
) 'progn
) (mapcar 'autoload-print-form
(cdr form
)))
175 ;; Symbols at the toplevel are meaningless.
178 (let ((doc-string-elt (get (car-safe form
) 'doc-string-elt
))
179 (outbuf autoload-print-form-outbuf
))
180 (if (and doc-string-elt
(stringp (nth doc-string-elt form
)))
181 ;; We need to hack the printing because the
182 ;; doc-string must be printed specially for
183 ;; make-docfile (sigh).
184 (let* ((p (nthcdr (1- doc-string-elt
) form
))
188 (let ((print-escape-newlines t
)
189 (print-escape-nonascii t
))
193 (princ "\"\\\n" outbuf
)
194 (let ((begin (with-current-buffer outbuf
(point))))
195 (princ (substring (prin1-to-string (car elt
)) 1)
197 ;; Insert a backslash before each ( that
198 ;; appears at the beginning of a line in
200 (with-current-buffer outbuf
202 (while (re-search-backward "\n[[(]" begin t
)
208 (princ (substring (prin1-to-string (cdr elt
)) 1)
211 (let ((print-escape-newlines t
)
212 (print-escape-nonascii t
))
213 (print form outbuf
)))))))
215 (defun autoload-ensure-default-file (file)
216 "Make sure that the autoload file FILE exists and if not create it."
217 (unless (file-exists-p file
)
219 (concat ";;; " (file-name-nondirectory file
)
220 " --- automatically extracted autoloads\n"
223 "\f\n;; Local Variables:\n"
224 ";; version-control: never\n"
225 ";; no-byte-compile: t\n"
226 ";; no-update-autoloads: t\n"
228 ";;; " (file-name-nondirectory file
)
233 (defun autoload-insert-section-header (outbuf autoloads load-name file time
)
234 "Insert the section-header line,
235 which lists the file name and which functions are in it, etc."
236 (insert generate-autoload-section-header
)
237 (prin1 (list 'autoloads autoloads load-name
238 (if (stringp file
) (autoload-trim-file-name file
) file
)
242 ;; Break that line at spaces, to avoid very long lines.
243 ;; Make each sub-line into a comment.
244 (with-current-buffer outbuf
249 (skip-chars-forward "^ \n")
251 (insert "\n" generate-autoload-section-continuation
))))))
253 (defun generate-file-autoloads (file)
254 "Insert at point a loaddefs autoload section for FILE.
255 autoloads are generated for defuns and defmacros in FILE
256 marked by `generate-autoload-cookie' (which see).
257 If FILE is being visited in a buffer, the contents of the buffer
259 (interactive "fGenerate autoloads for file: ")
260 (let ((outbuf (current-buffer))
262 (load-name (let ((name (file-name-nondirectory file
)))
263 (if (string-match "\\.elc?\\(\\.\\|$\\)" name
)
264 (substring name
0 (match-beginning 0))
267 (print-readably t
) ; This does something in Lucid Emacs.
268 (float-output-format nil
)
270 (visited (get-file-buffer file
))
273 ;; If the autoload section we create here uses an absolute
274 ;; pathname for FILE in its header, and then Emacs is installed
275 ;; under a different path on another system,
276 ;; `update-autoloads-here' won't be able to find the files to be
277 ;; autoloaded. So, if FILE is in the same directory or a
278 ;; subdirectory of the current buffer's directory, we'll make it
279 ;; relative to the current buffer's directory.
280 (setq file
(expand-file-name file
))
281 (let* ((source-truename (file-truename file
))
282 (dir-truename (file-name-as-directory
283 (file-truename default-directory
)))
284 (len (length dir-truename
)))
285 (if (and (< len
(length source-truename
))
286 (string= dir-truename
(substring source-truename
0 len
)))
287 (setq file
(substring source-truename len
))))
289 (message "Generating autoloads for %s..." file
)
295 ;; It is faster to avoid visiting the file.
296 (set-buffer (get-buffer-create " *generate-autoload-file*"))
297 (kill-all-local-variables)
299 (setq buffer-undo-list t
300 buffer-read-only nil
)
302 (insert-file-contents file nil
))
306 (goto-char (point-min))
308 (skip-chars-forward " \t\n\f")
310 ((looking-at (regexp-quote generate-autoload-cookie
))
311 (search-forward generate-autoload-cookie
)
312 (skip-chars-forward " \t")
315 ;; Read the next form and make an autoload.
316 (let* ((form (prog1 (read (current-buffer))
317 (or (bolp) (forward-line 1))))
318 (autoload (make-autoload form load-name
)))
320 (setq autoloads-done
(cons (nth 1 form
)
322 (setq autoload form
))
323 (let ((autoload-print-form-outbuf outbuf
))
324 (autoload-print-form autoload
)))
326 ;; Copy the rest of the line to the output.
327 (princ (buffer-substring
329 ;; Back up over whitespace, to preserve it.
330 (skip-chars-backward " \f\t")
331 (if (= (char-after (1+ (point))) ?
)
335 (progn (forward-line 1) (point)))
338 ;; Don't read the comment.
342 (forward-line 1)))))))
344 ;; We created this buffer, so we should kill it.
345 (kill-buffer (current-buffer)))
347 (setq output-end
(point-marker))))
350 ;; Insert the section-header line
351 ;; which lists the file name and which functions are in it, etc.
352 (autoload-insert-section-header outbuf autoloads-done load-name file
353 (nth 5 (file-attributes file
)))
354 (insert ";;; Generated autoloads from "
355 (autoload-trim-file-name file
) "\n")
356 (goto-char output-end
)
357 (insert generate-autoload-section-trailer
)))
358 (message "Generating autoloads for %s...done" file
)))
361 (defun update-file-autoloads (file)
362 "Update the autoloads for FILE in `generated-autoload-file'
363 \(which FILE might bind in its local variables).
364 Return FILE if there was no autoload cookie in it."
365 (interactive "fUpdate autoloads for file: ")
366 (let ((load-name (let ((name (file-name-nondirectory file
)))
367 (if (string-match "\\.elc?\\(\\.\\|$\\)" name
)
368 (substring name
0 (match-beginning 0))
371 (existing-buffer (get-file-buffer file
))
374 ;; We want to get a value for generated-autoload-file from
375 ;; the local variables section if it's there.
377 (set-buffer existing-buffer
))
378 ;; We must read/write the file without any code conversion,
379 ;; but still decode EOLs.
380 (let ((coding-system-for-read 'raw-text
))
381 (set-buffer (find-file-noselect
382 (autoload-ensure-default-file
383 (expand-file-name generated-autoload-file
384 (expand-file-name "lisp"
385 source-directory
)))))
386 ;; This is to make generated-autoload-file have Unix EOLs, so
387 ;; that it is portable to all platforms.
388 (setq buffer-file-coding-system
'raw-text-unix
))
389 (or (> (buffer-size) 0)
390 (error "Autoloads file %s does not exist" buffer-file-name
))
391 (or (file-writable-p buffer-file-name
)
392 (error "Autoloads file %s is not writable" buffer-file-name
))
396 (goto-char (point-min))
397 ;; Look for the section for LOAD-NAME.
398 (while (and (not found
)
399 (search-forward generate-autoload-section-header nil t
))
400 (let ((form (autoload-read-section-header)))
401 (cond ((string= (nth 2 form
) load-name
)
402 ;; We found the section for this file.
403 ;; Check if it is up to date.
404 (let ((begin (match-beginning 0))
405 (last-time (nth 4 form
))
406 (file-time (nth 5 (file-attributes file
))))
407 (if (and (or (null existing-buffer
)
408 (not (buffer-modified-p existing-buffer
)))
409 (listp last-time
) (= (length last-time
) 2)
410 (not (time-less-p last-time file-time
)))
414 Autoload section for %s is up to date."
416 (setq found
'up-to-date
))
417 (search-forward generate-autoload-section-trailer
)
418 (delete-region begin
(point))
420 ((string< load-name
(nth 2 form
))
421 ;; We've come to a section alphabetically later than
422 ;; LOAD-NAME. We assume the file is in order and so
423 ;; there must be no section for LOAD-NAME. We will
424 ;; insert one before the section here.
425 (goto-char (match-beginning 0))
426 (setq found
'new
)))))
430 ;; No later sections in the file. Put before the last page.
431 (goto-char (point-max))
432 (search-backward "\f" nil t
)))
433 (or (eq found
'up-to-date
)
435 ;; Check that FILE has any cookies before generating a
436 ;; new section for it.
439 (set-buffer existing-buffer
)
440 ;; It is faster to avoid visiting the file.
441 (set-buffer (get-buffer-create " *autoload-file*"))
442 (kill-all-local-variables)
444 (setq buffer-undo-list t
445 buffer-read-only nil
)
447 (insert-file-contents file nil
))
451 (goto-char (point-min))
453 (if (re-search-forward
454 (concat "^" (regexp-quote
455 generate-autoload-cookie
))
459 (message "%s has no autoloads" file
))
460 (setq no-autoloads t
)
463 (kill-buffer (current-buffer))))))))
464 (generate-file-autoloads file
))))
469 (if no-autoloads file
))))
471 (defun autoload-remove-section (begin)
473 (search-forward generate-autoload-section-trailer
)
474 (delete-region begin
(point)))
477 (defun update-directory-autoloads (&rest dirs
)
479 Update loaddefs.el with all the current autoloads from DIRS, and no old ones.
480 This uses `update-file-autoloads' (which see) do its work.
481 In an interactive call, you must give one argument, the name
482 of a single directory. In a call from Lisp, you can supply multiple
483 directories as separate arguments, but this usage is discouraged.
485 The function does NOT recursively descend into subdirectories of the
486 directory or directories specified."
487 (interactive "DUpdate autoloads from directory: ")
488 (let* ((files-re (let ((tmp nil
))
489 (dolist (suf load-suffixes
490 (concat "^[^=.].*" (regexp-opt tmp t
) "\\'"))
491 (unless (string-match "\\.elc" suf
) (push suf tmp
)))))
493 (mapcar (lambda (dir)
494 (directory-files (expand-file-name dir
)
497 (this-time (current-time))
498 (no-autoloads nil
) ;files with no autoload cookies.
500 (expand-file-name generated-autoload-file
501 (expand-file-name "lisp" source-directory
)))
502 (top-dir (file-name-directory autoloads-file
)))
505 (find-file-noselect (autoload-ensure-default-file autoloads-file
))
508 ;; Canonicalize file names and remove the autoload file itself.
509 (setq files
(delete (autoload-trim-file-name buffer-file-name
)
510 (mapcar 'autoload-trim-file-name files
)))
512 (goto-char (point-min))
513 (while (search-forward generate-autoload-section-header nil t
)
514 (let* ((form (autoload-read-section-header))
516 (cond ((and (consp file
) (stringp (car file
)))
517 ;; This is a list of files that have no autoload cookies.
518 ;; There shouldn't be more than one such entry.
519 ;; Remove the obsolete section.
520 (autoload-remove-section (match-beginning 0))
521 (let ((last-time (nth 4 form
)))
523 (let ((file-time (nth 5 (file-attributes file
))))
525 (not (time-less-p last-time file-time
)))
527 (push file no-autoloads
)
528 (setq files
(delete file files
)))))))
529 ((not (stringp file
)))
530 ((not (file-exists-p (expand-file-name file top-dir
)))
531 ;; Remove the obsolete section.
532 (autoload-remove-section (match-beginning 0)))
533 ((equal (nth 4 form
) (nth 5 (file-attributes file
)))
534 ;; File hasn't changed.
537 (update-file-autoloads file
)))
538 (setq files
(delete file files
)))))
539 ;; Elements remaining in FILES have no existing autoload sections yet.
542 (delq nil
(mapcar 'update-file-autoloads files
))))
544 ;; Sort them for better readability.
545 (setq no-autoloads
(sort no-autoloads
'string
<))
546 ;; Add the `no-autoloads' section.
547 (goto-char (point-max))
548 (search-backward "\f" nil t
)
549 (autoload-insert-section-header
550 (current-buffer) nil nil no-autoloads this-time
)
551 (insert generate-autoload-section-trailer
))
556 (defun batch-update-autoloads ()
557 "Update loaddefs.el autoloads in batch mode.
558 Calls `update-directory-autoloads' on the command line arguments."
559 (apply 'update-directory-autoloads command-line-args-left
)
560 (setq command-line-args-left nil
))
564 ;;; arch-tag: 00244766-98f4-4767-bf42-8a22103441c6
565 ;;; autoload.el ends here