1 ;;; autoload.el --- maintain autoloads in loaddefs.el.
3 ;; Copyright (C) 1991, 92, 93, 94, 95, 96, 97 Free Software Foundation, Inc.
5 ;; Author: Roland McGrath <roland@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; This code helps GNU Emacs maintainers keep the loaddefs.el file up to
28 ;; date. It interprets magic cookies of the form ";;;###autoload" in
29 ;; lisp source files in various useful ways. To learn more, read the
30 ;; source; if you're going to use this, you'd better be able to.
34 (defvar generated-autoload-file
"loaddefs.el"
35 "*File \\[update-file-autoloads] puts autoloads into.
36 A `.el' file can set this in its local variables section to make its
37 autoloads go somewhere else. The autoload file is assumed to contain a
38 trailer starting with a FormFeed character.")
40 (defconst generate-autoload-cookie
";;;###autoload"
41 "Magic comment indicating the following form should be autoloaded.
42 Used by \\[update-file-autoloads]. This string should be
43 meaningless to Lisp (e.g., a comment).
48 \(defun function-to-be-autoloaded () ...)
50 If this string appears alone on a line, the following form will be
51 read and an autoload made for it. If there is further text on the line,
52 that text will be copied verbatim to `generated-autoload-file'.")
54 (defconst generate-autoload-section-header
"\f\n;;;### "
55 "String that marks the form at the start of a new file's autoload section.")
57 (defconst generate-autoload-section-trailer
"\n;;;***\n"
58 "String which indicates the end of the section of autoloads for a file.")
60 (defconst generate-autoload-section-continuation
";;;;;; "
61 "String to add on each continuation of the section header form.")
63 (defun make-autoload (form file
)
64 "Turn FORM into an autoload or defvar for source file FILE.
65 Returns nil if FORM is not a special autoload form (i.e. a function definition
66 or macro definition or a defcustom)."
67 (let ((car (car-safe form
)) expand
)
69 ;; For complex cases, try again on the macro-expansion.
70 ((and (memq car
'(easy-mmode-define-global-mode
71 easy-mmode-define-minor-mode define-minor-mode
))
72 (setq expand
(let ((load-file-name file
)) (macroexpand form
)))
73 (eq (car expand
) 'progn
)
74 (memq :autoload-end expand
))
75 (let ((end (memq :autoload-end expand
)))
76 ;; Cut-off anything after the :autoload-end marker.
79 (mapcar (lambda (form) (make-autoload form file
))
82 ;; For special function-like operators, use the `autoload' function.
83 ((memq car
'(defun define-skeleton defmacro define-derived-mode
84 define-generic-mode easy-mmode-define-minor-mode
85 easy-mmode-define-global-mode
86 define-minor-mode defun
*))
87 (let* ((macrop (eq car
'defmacro
))
89 (body (nthcdr (get car
'doc-string-elt
) form
))
90 (doc (if (stringp (car body
)) (pop body
))))
91 ;; `define-generic-mode' quotes the name, so take care of that
92 (list 'autoload
(if (listp name
) name
(list 'quote name
)) file doc
93 (or (and (memq car
'(define-skeleton define-derived-mode
95 easy-mmode-define-global-mode
96 easy-mmode-define-minor-mode
97 define-minor-mode
)) t
)
98 (eq (car-safe (car body
)) 'interactive
))
99 (if macrop
(list 'quote
'macro
) nil
))))
101 ;; Convert defcustom to a simpler (and less space-consuming) defvar,
102 ;; but add some extra stuff if it uses :require.
104 (let ((varname (car-safe (cdr-safe form
)))
105 (init (car-safe (cdr-safe (cdr-safe form
))))
106 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form
)))))
107 (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form
))))))
108 (if (not (plist-get rest
:require
))
109 `(defvar ,varname
,init
,doc
)
111 (defvar ,varname
,init
,doc
)
112 (custom-add-to-group ,(plist-get rest
:group
)
113 ',varname
'custom-variable
)
114 (custom-add-load ',varname
115 ,(plist-get rest
:require
))))))
117 ;; nil here indicates that this is not a special autoload form.
120 ;;; Forms which have doc-strings which should be printed specially.
121 ;;; A doc-string-elt property of ELT says that (nth ELT FORM) is
122 ;;; the doc-string in FORM.
124 ;;; There used to be the following note here:
125 ;;; ;;; Note: defconst and defvar should NOT be marked in this way.
126 ;;; ;;; We don't want to produce defconsts and defvars that
127 ;;; ;;; make-docfile can grok, because then it would grok them twice,
128 ;;; ;;; once in foo.el (where they are given with ;;;###autoload) and
129 ;;; ;;; once in loaddefs.el.
131 ;;; Counter-note: Yes, they should be marked in this way.
132 ;;; make-docfile only processes those files that are loaded into the
133 ;;; dumped Emacs, and those files should never have anything
134 ;;; autoloaded here. The above-feared problem only occurs with files
135 ;;; which have autoloaded entries *and* are processed by make-docfile;
136 ;;; there should be no such files.
138 (put 'autoload
'doc-string-elt
3)
139 (put 'defun
'doc-string-elt
3)
140 (put 'defun
* 'doc-string-elt
3)
141 (put 'defvar
'doc-string-elt
3)
142 (put 'defcustom
'doc-string-elt
3)
143 (put 'defconst
'doc-string-elt
3)
144 (put 'defmacro
'doc-string-elt
3)
145 (put 'defsubst
'doc-string-elt
3)
146 (put 'define-skeleton
'doc-string-elt
2)
147 (put 'define-derived-mode
'doc-string-elt
4)
148 (put 'easy-mmode-define-minor-mode
'doc-string-elt
2)
149 (put 'define-minor-mode
'doc-string-elt
2)
150 (put 'define-generic-mode
'doc-string-elt
7)
151 ;; defin-global-mode has no explicit docstring.
152 (put 'easy-mmode-define-global-mode
'doc-string-elt
1000)
155 (defun autoload-trim-file-name (file)
156 ;; Returns a relative pathname of FILE
157 ;; starting from the directory that loaddefs.el is in.
158 ;; That is normally a directory in load-path,
159 ;; which means Emacs will be able to find FILE when it looks.
160 ;; Any extra directory names here would prevent finding the file.
161 (setq file
(expand-file-name file
))
162 (file-relative-name file
163 (file-name-directory generated-autoload-file
)))
165 (defun autoload-read-section-header ()
166 "Read a section header form.
167 Since continuation lines have been marked as comments,
168 we must copy the text of the form and remove those comment
169 markers before we call `read'."
171 (let ((beginning (point))
174 (while (looking-at generate-autoload-section-continuation
)
176 (setq string
(buffer-substring beginning
(point)))
177 (with-current-buffer (get-buffer-create " *autoload*")
180 (goto-char (point-min))
181 (while (search-forward generate-autoload-section-continuation nil t
)
183 (goto-char (point-min))
184 (read (current-buffer))))))
186 ;; !! Requires OUTBUF to be bound !!
187 (defun autoload-print-form (form)
188 "Print FORM such that make-docfile will find the docstrings."
190 ;; If the form is a sequence, recurse.
191 ((eq (car form
) 'progn
) (mapcar 'autoload-print-form
(cdr form
)))
192 ;; Symbols at the toplevel are meaningless.
195 (let ((doc-string-elt (get (car-safe form
) 'doc-string-elt
)))
196 (if (and doc-string-elt
(stringp (nth doc-string-elt form
)))
197 ;; We need to hack the printing because the
198 ;; doc-string must be printed specially for
199 ;; make-docfile (sigh).
200 (let* ((p (nthcdr (1- doc-string-elt
) form
))
204 (let ((print-escape-newlines t
)
205 (print-escape-nonascii t
))
206 (mapcar (lambda (elt)
210 (princ "\"\\\n" outbuf
)
211 (let ((begin (with-current-buffer outbuf
(point))))
212 (princ (substring (prin1-to-string (car elt
)) 1)
214 ;; Insert a backslash before each ( that
215 ;; appears at the beginning of a line in
217 (with-current-buffer outbuf
219 (while (search-backward "\n(" begin t
)
225 (princ (substring (prin1-to-string (cdr elt
)) 1)
228 (let ((print-escape-newlines t
)
229 (print-escape-nonascii t
))
230 (print form outbuf
)))))))
232 (defun generate-file-autoloads (file)
233 "Insert at point a loaddefs autoload section for FILE.
234 autoloads are generated for defuns and defmacros in FILE
235 marked by `generate-autoload-cookie' (which see).
236 If FILE is being visited in a buffer, the contents of the buffer
238 (interactive "fGenerate autoloads for file: ")
239 (let ((outbuf (current-buffer))
241 (load-name (let ((name (file-name-nondirectory file
)))
242 (if (string-match "\\.elc?$" name
)
243 (substring name
0 (match-beginning 0))
246 (print-readably t
) ; This does something in Lucid Emacs.
247 (float-output-format nil
)
249 (visited (get-file-buffer file
))
252 ;; If the autoload section we create here uses an absolute
253 ;; pathname for FILE in its header, and then Emacs is installed
254 ;; under a different path on another system,
255 ;; `update-autoloads-here' won't be able to find the files to be
256 ;; autoloaded. So, if FILE is in the same directory or a
257 ;; subdirectory of the current buffer's directory, we'll make it
258 ;; relative to the current buffer's directory.
259 (setq file
(expand-file-name file
))
260 (let* ((source-truename (file-truename file
))
261 (dir-truename (file-name-as-directory
262 (file-truename default-directory
)))
263 (len (length dir-truename
)))
264 (if (and (< len
(length source-truename
))
265 (string= dir-truename
(substring source-truename
0 len
)))
266 (setq file
(substring source-truename len
))))
268 (message "Generating autoloads for %s..." file
)
274 ;; It is faster to avoid visiting the file.
275 (set-buffer (get-buffer-create " *generate-autoload-file*"))
276 (kill-all-local-variables)
278 (setq buffer-undo-list t
279 buffer-read-only nil
)
281 (insert-file-contents file nil
))
285 (goto-char (point-min))
287 (skip-chars-forward " \t\n\f")
289 ((looking-at (regexp-quote generate-autoload-cookie
))
290 (search-forward generate-autoload-cookie
)
291 (skip-chars-forward " \t")
294 ;; Read the next form and make an autoload.
295 (let* ((form (prog1 (read (current-buffer))
296 (or (bolp) (forward-line 1))))
297 (autoload (make-autoload form load-name
)))
299 (setq autoloads-done
(cons (nth 1 form
)
301 (setq autoload form
))
302 (autoload-print-form autoload
))
304 ;; Copy the rest of the line to the output.
305 (princ (buffer-substring
307 ;; Back up over whitespace, to preserve it.
308 (skip-chars-backward " \f\t")
309 (if (= (char-after (1+ (point))) ?
)
313 (progn (forward-line 1) (point)))
316 ;; Don't read the comment.
320 (forward-line 1)))))))
322 ;; We created this buffer, so we should kill it.
323 (kill-buffer (current-buffer)))
325 (setq output-end
(point-marker))))
328 ;; Insert the section-header line
329 ;; which lists the file name and which functions are in it, etc.
330 (insert generate-autoload-section-header
)
331 (prin1 (list 'autoloads autoloads-done load-name
332 (autoload-trim-file-name file
)
333 (nth 5 (file-attributes file
)))
336 ;; Break that line at spaces, to avoid very long lines.
337 ;; Make each sub-line into a comment.
338 (with-current-buffer outbuf
343 (skip-chars-forward "^ \n")
345 (insert "\n" generate-autoload-section-continuation
)))))
346 (insert ";;; Generated autoloads from "
347 (autoload-trim-file-name file
) "\n")
348 ;; Warn if we put a line in loaddefs.el
349 ;; that is long enough to cause trouble.
350 (while (< (point) output-end
)
353 (if (> (- (point) beg
) 900)
355 (message "A line is too long--over 900 characters")
357 (goto-char output-end
))))
359 (goto-char output-end
)
360 (insert generate-autoload-section-trailer
)))
361 (message "Generating autoloads for %s...done" file
)))
364 (defun update-file-autoloads (file)
365 "Update the autoloads for FILE in `generated-autoload-file'
366 \(which FILE might bind in its local variables)."
367 (interactive "fUpdate autoloads for file: ")
368 (let ((load-name (let ((name (file-name-nondirectory file
)))
369 (if (string-match "\\.elc?$" name
)
370 (substring name
0 (match-beginning 0))
373 (existing-buffer (get-file-buffer file
)))
375 ;; We want to get a value for generated-autoload-file from
376 ;; the local variables section if it's there.
378 (set-buffer existing-buffer
))
379 ;; We must read/write the file without any code conversion,
380 ;; but still decode EOLs.
381 (let ((coding-system-for-read 'raw-text
))
382 (set-buffer (find-file-noselect
383 (expand-file-name generated-autoload-file
384 (expand-file-name "lisp"
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 (or (> (car last-time
) (car file-time
))
411 (and (= (car last-time
) (car file-time
))
412 (>= (nth 1 last-time
)
413 (nth 1 file-time
)))))
417 Autoload section for %s is up to date."
419 (setq found
'up-to-date
))
420 (search-forward generate-autoload-section-trailer
)
421 (delete-region begin
(point))
423 ((string< load-name
(nth 2 form
))
424 ;; We've come to a section alphabetically later than
425 ;; LOAD-NAME. We assume the file is in order and so
426 ;; there must be no section for LOAD-NAME. We will
427 ;; insert one before the section here.
428 (goto-char (match-beginning 0))
429 (setq found
'new
)))))
433 ;; No later sections in the file. Put before the last page.
434 (goto-char (point-max))
435 (search-backward "\f" nil t
)))
436 (or (eq found
'up-to-date
)
438 ;; Check that FILE has any cookies before generating a
439 ;; new section for it.
442 (set-buffer existing-buffer
)
443 ;; It is faster to avoid visiting the file.
444 (set-buffer (get-buffer-create " *autoload-file*"))
445 (kill-all-local-variables)
447 (setq buffer-undo-list t
448 buffer-read-only nil
)
450 (insert-file-contents file nil
))
454 (goto-char (point-min))
456 (if (re-search-forward
457 (concat "^" (regexp-quote
458 generate-autoload-cookie
))
462 (message "%s has no autoloads" file
))
465 (kill-buffer (current-buffer))))))))
466 (generate-file-autoloads file
))))
472 (defun update-autoloads-from-directories (&rest dirs
)
474 Update loaddefs.el with all the current autoloads from DIRS, and no old ones.
475 This uses `update-file-autoloads' (which see) do its work."
476 (interactive "DUpdate autoloads from directory: ")
477 (let ((files (apply 'nconc
478 (mapcar (function (lambda (dir)
479 (directory-files (expand-file-name dir
)
486 (expand-file-name generated-autoload-file
487 (expand-file-name "lisp"
489 (setq top-dir
(file-name-directory autoloads-file
))
491 (set-buffer (find-file-noselect autoloads-file
))
493 (goto-char (point-min))
494 (while (search-forward generate-autoload-section-header nil t
)
495 (let* ((form (autoload-read-section-header))
497 (cond ((not (stringp file
)))
498 ((not (file-exists-p (expand-file-name file top-dir
)))
499 ;; Remove the obsolete section.
500 (let ((begin (match-beginning 0)))
501 (search-forward generate-autoload-section-trailer
)
502 (delete-region begin
(point))))
504 (update-file-autoloads file
)))
505 (setq files
(delete file files
)))))
506 ;; Elements remaining in FILES have no existing autoload sections.
507 (mapcar 'update-file-autoloads files
)
511 (defun batch-update-autoloads ()
512 "Update loaddefs.el autoloads in batch mode.
513 Calls `update-autoloads-from-directories' on the command line arguments."
514 (apply 'update-autoloads-from-directories command-line-args-left
)
515 (setq command-line-args-left nil
))
519 ;;; autoload.el ends here