1 ;;; autoload.el --- maintain autoloads in loaddefs.el.
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2001
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 (defvar generated-autoload-file
"loaddefs.el"
36 "*File \\[update-file-autoloads] puts autoloads into.
37 A `.el' file can set this in its local variables section to make its
38 autoloads go somewhere else. The autoload file is assumed to contain a
39 trailer starting with a FormFeed character.")
41 (defconst generate-autoload-cookie
";;;###autoload"
42 "Magic comment indicating the following form should be autoloaded.
43 Used by \\[update-file-autoloads]. This string should be
44 meaningless to Lisp (e.g., a comment).
49 \(defun function-to-be-autoloaded () ...)
51 If this string appears alone on a line, the following form will be
52 read and an autoload made for it. If there is further text on the line,
53 that text will be copied verbatim to `generated-autoload-file'.")
55 (defconst generate-autoload-section-header
"\f\n;;;### "
56 "String that marks the form at the start of a new file's autoload section.")
58 (defconst generate-autoload-section-trailer
"\n;;;***\n"
59 "String which indicates the end of the section of autoloads for a file.")
61 (defconst generate-autoload-section-continuation
";;;;;; "
62 "String to add on each continuation of the section header form.")
64 (defun make-autoload (form file
)
65 "Turn FORM into an autoload or defvar for source file FILE.
66 Returns nil if FORM is not a special autoload form (i.e. a function definition
67 or macro definition or a defcustom)."
68 (let ((car (car-safe form
)) expand
)
70 ;; For complex cases, try again on the macro-expansion.
71 ((and (memq car
'(easy-mmode-define-global-mode
72 easy-mmode-define-minor-mode define-minor-mode
))
73 (setq expand
(let ((load-file-name file
)) (macroexpand form
)))
74 (eq (car expand
) 'progn
)
75 (memq :autoload-end expand
))
76 (let ((end (memq :autoload-end expand
)))
77 ;; Cut-off anything after the :autoload-end marker.
80 (mapcar (lambda (form) (make-autoload form file
))
83 ;; For special function-like operators, use the `autoload' function.
84 ((memq car
'(defun define-skeleton defmacro define-derived-mode
85 define-generic-mode easy-mmode-define-minor-mode
86 easy-mmode-define-global-mode
87 define-minor-mode defun
*))
88 (let* ((macrop (eq car
'defmacro
))
90 (body (nthcdr (get car
'doc-string-elt
) form
))
91 (doc (if (stringp (car body
)) (pop body
))))
92 ;; `define-generic-mode' quotes the name, so take care of that
93 (list 'autoload
(if (listp name
) name
(list 'quote name
)) file doc
94 (or (and (memq car
'(define-skeleton define-derived-mode
96 easy-mmode-define-global-mode
97 easy-mmode-define-minor-mode
98 define-minor-mode
)) t
)
99 (eq (car-safe (car body
)) 'interactive
))
100 (if macrop
(list 'quote
'macro
) nil
))))
102 ;; Convert defcustom to a simpler (and less space-consuming) defvar,
103 ;; but add some extra stuff if it uses :require.
105 (let ((varname (car-safe (cdr-safe form
)))
106 (init (car-safe (cdr-safe (cdr-safe form
))))
107 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form
)))))
108 (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form
))))))
109 (if (not (plist-get rest
:require
))
110 `(defvar ,varname
,init
,doc
)
112 (defvar ,varname
,init
,doc
)
113 (custom-add-to-group ,(plist-get rest
:group
)
114 ',varname
'custom-variable
)
115 (custom-add-load ',varname
116 ,(plist-get rest
:require
))))))
118 ;; nil here indicates that this is not a special autoload form.
121 ;;; Forms which have doc-strings which should be printed specially.
122 ;;; A doc-string-elt property of ELT says that (nth ELT FORM) is
123 ;;; the doc-string in FORM.
125 ;;; There used to be the following note here:
126 ;;; ;;; Note: defconst and defvar should NOT be marked in this way.
127 ;;; ;;; We don't want to produce defconsts and defvars that
128 ;;; ;;; make-docfile can grok, because then it would grok them twice,
129 ;;; ;;; once in foo.el (where they are given with ;;;###autoload) and
130 ;;; ;;; once in loaddefs.el.
132 ;;; Counter-note: Yes, they should be marked in this way.
133 ;;; make-docfile only processes those files that are loaded into the
134 ;;; dumped Emacs, and those files should never have anything
135 ;;; autoloaded here. The above-feared problem only occurs with files
136 ;;; which have autoloaded entries *and* are processed by make-docfile;
137 ;;; there should be no such files.
139 (put 'autoload
'doc-string-elt
3)
140 (put 'defun
'doc-string-elt
3)
141 (put 'defun
* 'doc-string-elt
3)
142 (put 'defvar
'doc-string-elt
3)
143 (put 'defcustom
'doc-string-elt
3)
144 (put 'defconst
'doc-string-elt
3)
145 (put 'defmacro
'doc-string-elt
3)
146 (put 'defsubst
'doc-string-elt
3)
147 (put 'define-skeleton
'doc-string-elt
2)
148 (put 'define-derived-mode
'doc-string-elt
4)
149 (put 'easy-mmode-define-minor-mode
'doc-string-elt
2)
150 (put 'define-minor-mode
'doc-string-elt
2)
151 (put 'define-generic-mode
'doc-string-elt
7)
152 ;; defin-global-mode has no explicit docstring.
153 (put 'easy-mmode-define-global-mode
'doc-string-elt
1000)
156 (defun autoload-trim-file-name (file)
157 ;; Returns a relative pathname of FILE
158 ;; starting from the directory that loaddefs.el is in.
159 ;; That is normally a directory in load-path,
160 ;; which means Emacs will be able to find FILE when it looks.
161 ;; Any extra directory names here would prevent finding the file.
162 (setq file
(expand-file-name file
))
163 (file-relative-name file
164 (file-name-directory generated-autoload-file
)))
166 (defun autoload-read-section-header ()
167 "Read a section header form.
168 Since continuation lines have been marked as comments,
169 we must copy the text of the form and remove those comment
170 markers before we call `read'."
172 (let ((beginning (point))
175 (while (looking-at generate-autoload-section-continuation
)
177 (setq string
(buffer-substring beginning
(point)))
178 (with-current-buffer (get-buffer-create " *autoload*")
181 (goto-char (point-min))
182 (while (search-forward generate-autoload-section-continuation nil t
)
184 (goto-char (point-min))
185 (read (current-buffer))))))
187 ;; !! Requires OUTBUF to be bound !!
188 (defun autoload-print-form (form)
189 "Print FORM such that make-docfile will find the docstrings."
191 ;; If the form is a sequence, recurse.
192 ((eq (car form
) 'progn
) (mapcar 'autoload-print-form
(cdr form
)))
193 ;; Symbols at the toplevel are meaningless.
196 (let ((doc-string-elt (get (car-safe form
) 'doc-string-elt
)))
197 (if (and doc-string-elt
(stringp (nth doc-string-elt form
)))
198 ;; We need to hack the printing because the
199 ;; doc-string must be printed specially for
200 ;; make-docfile (sigh).
201 (let* ((p (nthcdr (1- doc-string-elt
) form
))
205 (let ((print-escape-newlines t
)
206 (print-escape-nonascii t
))
207 (mapcar (lambda (elt)
211 (princ "\"\\\n" outbuf
)
212 (let ((begin (with-current-buffer outbuf
(point))))
213 (princ (substring (prin1-to-string (car elt
)) 1)
215 ;; Insert a backslash before each ( that
216 ;; appears at the beginning of a line in
218 (with-current-buffer outbuf
220 (while (search-backward "\n(" begin t
)
226 (princ (substring (prin1-to-string (cdr elt
)) 1)
229 (let ((print-escape-newlines t
)
230 (print-escape-nonascii t
))
231 (print form outbuf
)))))))
233 (defun generate-file-autoloads (file)
234 "Insert at point a loaddefs autoload section for FILE.
235 autoloads are generated for defuns and defmacros in FILE
236 marked by `generate-autoload-cookie' (which see).
237 If FILE is being visited in a buffer, the contents of the buffer
239 (interactive "fGenerate autoloads for file: ")
240 (let ((outbuf (current-buffer))
242 (load-name (let ((name (file-name-nondirectory file
)))
243 (if (string-match "\\.elc?$" name
)
244 (substring name
0 (match-beginning 0))
247 (print-readably t
) ; This does something in Lucid Emacs.
248 (float-output-format nil
)
250 (visited (get-file-buffer file
))
253 ;; If the autoload section we create here uses an absolute
254 ;; pathname for FILE in its header, and then Emacs is installed
255 ;; under a different path on another system,
256 ;; `update-autoloads-here' won't be able to find the files to be
257 ;; autoloaded. So, if FILE is in the same directory or a
258 ;; subdirectory of the current buffer's directory, we'll make it
259 ;; relative to the current buffer's directory.
260 (setq file
(expand-file-name file
))
261 (let* ((source-truename (file-truename file
))
262 (dir-truename (file-name-as-directory
263 (file-truename default-directory
)))
264 (len (length dir-truename
)))
265 (if (and (< len
(length source-truename
))
266 (string= dir-truename
(substring source-truename
0 len
)))
267 (setq file
(substring source-truename len
))))
269 (message "Generating autoloads for %s..." file
)
275 ;; It is faster to avoid visiting the file.
276 (set-buffer (get-buffer-create " *generate-autoload-file*"))
277 (kill-all-local-variables)
279 (setq buffer-undo-list t
280 buffer-read-only nil
)
282 (insert-file-contents file nil
))
286 (goto-char (point-min))
288 (skip-chars-forward " \t\n\f")
290 ((looking-at (regexp-quote generate-autoload-cookie
))
291 (search-forward generate-autoload-cookie
)
292 (skip-chars-forward " \t")
295 ;; Read the next form and make an autoload.
296 (let* ((form (prog1 (read (current-buffer))
297 (or (bolp) (forward-line 1))))
298 (autoload (make-autoload form load-name
)))
300 (setq autoloads-done
(cons (nth 1 form
)
302 (setq autoload form
))
303 (autoload-print-form autoload
))
305 ;; Copy the rest of the line to the output.
306 (princ (buffer-substring
308 ;; Back up over whitespace, to preserve it.
309 (skip-chars-backward " \f\t")
310 (if (= (char-after (1+ (point))) ?
)
314 (progn (forward-line 1) (point)))
317 ;; Don't read the comment.
321 (forward-line 1)))))))
323 ;; We created this buffer, so we should kill it.
324 (kill-buffer (current-buffer)))
326 (setq output-end
(point-marker))))
329 ;; Insert the section-header line
330 ;; which lists the file name and which functions are in it, etc.
331 (insert generate-autoload-section-header
)
332 (prin1 (list 'autoloads autoloads-done load-name
333 (autoload-trim-file-name file
)
334 (nth 5 (file-attributes file
)))
337 ;; Break that line at spaces, to avoid very long lines.
338 ;; Make each sub-line into a comment.
339 (with-current-buffer outbuf
344 (skip-chars-forward "^ \n")
346 (insert "\n" generate-autoload-section-continuation
)))))
347 (insert ";;; Generated autoloads from "
348 (autoload-trim-file-name file
) "\n")
349 (goto-char output-end
)
350 (insert generate-autoload-section-trailer
)))
351 (message "Generating autoloads for %s...done" file
)))
354 (defun update-file-autoloads (file)
355 "Update the autoloads for FILE in `generated-autoload-file'
356 \(which FILE might bind in its local variables)."
357 (interactive "fUpdate autoloads for file: ")
358 (let ((load-name (let ((name (file-name-nondirectory file
)))
359 (if (string-match "\\.elc?$" name
)
360 (substring name
0 (match-beginning 0))
363 (existing-buffer (get-file-buffer file
)))
365 ;; We want to get a value for generated-autoload-file from
366 ;; the local variables section if it's there.
368 (set-buffer existing-buffer
))
369 ;; We must read/write the file without any code conversion,
370 ;; but still decode EOLs.
371 (let ((coding-system-for-read 'raw-text
))
372 (set-buffer (find-file-noselect
373 (expand-file-name generated-autoload-file
374 (expand-file-name "lisp"
376 ;; This is to make generated-autoload-file have Unix EOLs, so
377 ;; that it is portable to all platforms.
378 (setq buffer-file-coding-system
'raw-text-unix
))
379 (or (> (buffer-size) 0)
380 (error "Autoloads file %s does not exist" buffer-file-name
))
381 (or (file-writable-p buffer-file-name
)
382 (error "Autoloads file %s is not writable" buffer-file-name
))
386 (goto-char (point-min))
387 ;; Look for the section for LOAD-NAME.
388 (while (and (not found
)
389 (search-forward generate-autoload-section-header nil t
))
390 (let ((form (autoload-read-section-header)))
391 (cond ((string= (nth 2 form
) load-name
)
392 ;; We found the section for this file.
393 ;; Check if it is up to date.
394 (let ((begin (match-beginning 0))
395 (last-time (nth 4 form
))
396 (file-time (nth 5 (file-attributes file
))))
397 (if (and (or (null existing-buffer
)
398 (not (buffer-modified-p existing-buffer
)))
399 (listp last-time
) (= (length last-time
) 2)
400 (or (> (car last-time
) (car file-time
))
401 (and (= (car last-time
) (car file-time
))
402 (>= (nth 1 last-time
)
403 (nth 1 file-time
)))))
407 Autoload section for %s is up to date."
409 (setq found
'up-to-date
))
410 (search-forward generate-autoload-section-trailer
)
411 (delete-region begin
(point))
413 ((string< load-name
(nth 2 form
))
414 ;; We've come to a section alphabetically later than
415 ;; LOAD-NAME. We assume the file is in order and so
416 ;; there must be no section for LOAD-NAME. We will
417 ;; insert one before the section here.
418 (goto-char (match-beginning 0))
419 (setq found
'new
)))))
423 ;; No later sections in the file. Put before the last page.
424 (goto-char (point-max))
425 (search-backward "\f" nil t
)))
426 (or (eq found
'up-to-date
)
428 ;; Check that FILE has any cookies before generating a
429 ;; new section for it.
432 (set-buffer existing-buffer
)
433 ;; It is faster to avoid visiting the file.
434 (set-buffer (get-buffer-create " *autoload-file*"))
435 (kill-all-local-variables)
437 (setq buffer-undo-list t
438 buffer-read-only nil
)
440 (insert-file-contents file nil
))
444 (goto-char (point-min))
446 (if (re-search-forward
447 (concat "^" (regexp-quote
448 generate-autoload-cookie
))
452 (message "%s has no autoloads" file
))
455 (kill-buffer (current-buffer))))))))
456 (generate-file-autoloads file
))))
462 (defun update-autoloads-from-directories (&rest dirs
)
464 Update loaddefs.el with all the current autoloads from DIRS, and no old ones.
465 This uses `update-file-autoloads' (which see) do its work."
466 (interactive "DUpdate autoloads from directory: ")
467 (let ((files (apply 'nconc
468 (mapcar (function (lambda (dir)
469 (directory-files (expand-file-name dir
)
476 (expand-file-name generated-autoload-file
477 (expand-file-name "lisp"
479 (setq top-dir
(file-name-directory autoloads-file
))
481 (set-buffer (find-file-noselect autoloads-file
))
483 (goto-char (point-min))
484 (while (search-forward generate-autoload-section-header nil t
)
485 (let* ((form (autoload-read-section-header))
487 (cond ((not (stringp file
)))
488 ((not (file-exists-p (expand-file-name file top-dir
)))
489 ;; Remove the obsolete section.
490 (let ((begin (match-beginning 0)))
491 (search-forward generate-autoload-section-trailer
)
492 (delete-region begin
(point))))
494 (update-file-autoloads file
)))
495 (setq files
(delete file files
)))))
496 ;; Elements remaining in FILES have no existing autoload sections.
497 (mapcar 'update-file-autoloads files
)
501 (defun batch-update-autoloads ()
502 "Update loaddefs.el autoloads in batch mode.
503 Calls `update-autoloads-from-directories' on the command line arguments."
504 (apply 'update-autoloads-from-directories command-line-args-left
)
505 (setq command-line-args-left nil
))
509 ;;; autoload.el ends here