(generated-autoload-file): Change defconst to defvar.
[emacs.git] / lisp / emacs-lisp / autoload.el
blobbf5b5fffc42f8e2b6a1974123f5c82c5078dbdcd
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.ai.mit.edu>
6 ;; Keywords: maint
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)
13 ;; any later version.
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.
25 ;;; Commentary:
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.
32 ;;; Code:
34 (defun make-autoload (form file)
35 "Turn FORM, a defun or defmacro, into an autoload for source file FILE.
36 Returns nil if FORM is not a defun, define-skeleton or defmacro."
37 (let ((car (car-safe form)))
38 (if (memq car '(defun define-skeleton defmacro))
39 (let ((macrop (eq car 'defmacro))
40 name doc)
41 (setq form (cdr form)
42 name (car form)
43 ;; Ignore the arguments.
44 form (cdr (if (eq car 'define-skeleton)
45 form
46 (cdr form)))
47 doc (car form))
48 (if (stringp doc)
49 (setq form (cdr form))
50 (setq doc nil))
51 (list 'autoload (list 'quote name) file doc
52 (or (eq car 'define-skeleton)
53 (eq (car-safe (car form)) 'interactive))
54 (if macrop (list 'quote 'macro) nil)))
55 nil)))
57 (put 'define-skeleton 'doc-string-elt 3)
59 (defconst generate-autoload-cookie ";;;###autoload"
60 "Magic comment indicating the following form should be autoloaded.
61 Used by \\[update-file-autoloads]. This string should be
62 meaningless to Lisp (e.g., a comment).
64 This string is used:
66 ;;;###autoload
67 \(defun function-to-be-autoloaded () ...)
69 If this string appears alone on a line, the following form will be
70 read and an autoload made for it. If there is further text on the line,
71 that text will be copied verbatim to `generated-autoload-file'.")
73 (defconst generate-autoload-section-header "\f\n;;;### "
74 "String inserted before the form identifying
75 the section of autoloads for a file.")
77 (defconst generate-autoload-section-trailer "\n;;;***\n"
78 "String which indicates the end of the section of autoloads for a file.")
80 ;;; Forms which have doc-strings which should be printed specially.
81 ;;; A doc-string-elt property of ELT says that (nth ELT FORM) is
82 ;;; the doc-string in FORM.
83 ;;;
84 ;;; There used to be the following note here:
85 ;;; ;;; Note: defconst and defvar should NOT be marked in this way.
86 ;;; ;;; We don't want to produce defconsts and defvars that
87 ;;; ;;; make-docfile can grok, because then it would grok them twice,
88 ;;; ;;; once in foo.el (where they are given with ;;;###autoload) and
89 ;;; ;;; once in loaddefs.el.
90 ;;;
91 ;;; Counter-note: Yes, they should be marked in this way.
92 ;;; make-docfile only processes those files that are loaded into the
93 ;;; dumped Emacs, and those files should never have anything
94 ;;; autoloaded here. The above-feared problem only occurs with files
95 ;;; which have autoloaded entries *and* are processed by make-docfile;
96 ;;; there should be no such files.
98 (put 'autoload 'doc-string-elt 3)
99 (put 'defun 'doc-string-elt 3)
100 (put 'defvar 'doc-string-elt 3)
101 (put 'defconst 'doc-string-elt 3)
102 (put 'defmacro 'doc-string-elt 3)
104 (defun autoload-trim-file-name (file)
105 ;; Returns a relative pathname of FILE
106 ;; starting from the directory that loaddefs.el is in.
107 ;; That is normally a directory in load-path,
108 ;; which means Emacs will be able to find FILE when it looks.
109 ;; Any extra directory names here would prevent finding the file.
110 (setq file (expand-file-name file))
111 (file-relative-name file
112 (file-name-directory generated-autoload-file)))
114 (defun generate-file-autoloads (file)
115 "Insert at point a loaddefs autoload section for FILE.
116 autoloads are generated for defuns and defmacros in FILE
117 marked by `generate-autoload-cookie' (which see).
118 If FILE is being visited in a buffer, the contents of the buffer
119 are used."
120 (interactive "fGenerate autoloads for file: ")
121 (let ((outbuf (current-buffer))
122 (autoloads-done '())
123 (load-name (let ((name (file-name-nondirectory file)))
124 (if (string-match "\\.elc?$" name)
125 (substring name 0 (match-beginning 0))
126 name)))
127 (print-length nil)
128 (print-readably t) ; This does something in Lucid Emacs.
129 (float-output-format nil)
130 (done-any nil)
131 (visited (get-file-buffer file))
132 output-end)
134 ;; If the autoload section we create here uses an absolute
135 ;; pathname for FILE in its header, and then Emacs is installed
136 ;; under a different path on another system,
137 ;; `update-autoloads-here' won't be able to find the files to be
138 ;; autoloaded. So, if FILE is in the same directory or a
139 ;; subdirectory of the current buffer's directory, we'll make it
140 ;; relative to the current buffer's directory.
141 (setq file (expand-file-name file))
142 (let* ((source-truename (file-truename file))
143 (dir-truename (file-name-as-directory
144 (file-truename default-directory)))
145 (len (length dir-truename)))
146 (if (and (< len (length source-truename))
147 (string= dir-truename (substring source-truename 0 len)))
148 (setq file (substring source-truename len))))
150 (message "Generating autoloads for %s..." file)
151 (save-excursion
152 (unwind-protect
153 (progn
154 (if visited
155 (set-buffer visited)
156 ;; It is faster to avoid visiting the file.
157 (set-buffer (get-buffer-create " *generate-autoload-file*"))
158 (kill-all-local-variables)
159 (erase-buffer)
160 (setq buffer-undo-list t
161 buffer-read-only nil)
162 (emacs-lisp-mode)
163 (insert-file-contents file nil))
164 (save-excursion
165 (save-restriction
166 (widen)
167 (goto-char (point-min))
168 (while (not (eobp))
169 (skip-chars-forward " \t\n\f")
170 (cond
171 ((looking-at (regexp-quote generate-autoload-cookie))
172 (search-forward generate-autoload-cookie)
173 (skip-chars-forward " \t")
174 (setq done-any t)
175 (if (eolp)
176 ;; Read the next form and make an autoload.
177 (let* ((form (prog1 (read (current-buffer))
178 (or (bolp) (forward-line 1))))
179 (autoload (make-autoload form load-name))
180 (doc-string-elt (get (car-safe form)
181 'doc-string-elt)))
182 (if autoload
183 (setq autoloads-done (cons (nth 1 form)
184 autoloads-done))
185 (setq autoload form))
186 (if (and doc-string-elt
187 (stringp (nth doc-string-elt autoload)))
188 ;; We need to hack the printing because the
189 ;; doc-string must be printed specially for
190 ;; make-docfile (sigh).
191 (let* ((p (nthcdr (1- doc-string-elt)
192 autoload))
193 (elt (cdr p)))
194 (setcdr p nil)
195 (princ "\n(" outbuf)
196 (let ((print-escape-newlines t))
197 (mapcar (function (lambda (elt)
198 (prin1 elt outbuf)
199 (princ " " outbuf)))
200 autoload))
201 (princ "\"\\\n" outbuf)
202 (let ((begin (save-excursion
203 (set-buffer outbuf)
204 (point))))
205 (princ (substring
206 (prin1-to-string (car elt)) 1)
207 outbuf)
208 ;; Insert a backslash before each ( that
209 ;; appears at the beginning of a line in
210 ;; the doc string.
211 (save-excursion
212 (set-buffer outbuf)
213 (save-excursion
214 (while (search-backward "\n(" begin t)
215 (forward-char 1)
216 (insert "\\"))))
217 (if (null (cdr elt))
218 (princ ")" outbuf)
219 (princ " " outbuf)
220 (princ (substring
221 (prin1-to-string (cdr elt))
223 outbuf))
224 (terpri outbuf)))
225 (let ((print-escape-newlines t))
226 (print autoload outbuf))))
227 ;; Copy the rest of the line to the output.
228 (princ (buffer-substring
229 (progn
230 ;; Back up over whitespace, to preserve it.
231 (skip-chars-backward " \f\t")
232 (if (= (char-after (1+ (point))) ? )
233 ;; Eat one space.
234 (forward-char 1))
235 (point))
236 (progn (forward-line 1) (point)))
237 outbuf)))
238 ((looking-at ";")
239 ;; Don't read the comment.
240 (forward-line 1))
242 (forward-sexp 1)
243 (forward-line 1)))))))
244 (or visited
245 ;; We created this buffer, so we should kill it.
246 (kill-buffer (current-buffer)))
247 (set-buffer outbuf)
248 (setq output-end (point-marker))))
249 (if done-any
250 (progn
251 (insert generate-autoload-section-header)
252 (prin1 (list 'autoloads autoloads-done load-name
253 (autoload-trim-file-name file)
254 (nth 5 (file-attributes file)))
255 outbuf)
256 (terpri outbuf)
257 (insert ";;; Generated autoloads from "
258 (autoload-trim-file-name file) "\n")
259 ;; Warn if we put a line in loaddefs.el
260 ;; that is long enough to cause trouble.
261 (while (< (point) output-end)
262 (let ((beg (point)))
263 (end-of-line)
264 (if (> (- (point) beg) 900)
265 (progn
266 (message "A line is too long--over 900 characters")
267 (sleep-for 2)
268 (goto-char output-end))))
269 (forward-line 1))
270 (goto-char output-end)
271 (insert generate-autoload-section-trailer)))
272 (message "Generating autoloads for %s...done" file)))
274 (defvar generated-autoload-file "loaddefs.el"
275 "*File \\[update-file-autoloads] puts autoloads into.
276 A `.el' file can set this in its local variables section to make its
277 autoloads go somewhere else.")
279 ;;;###autoload
280 (defun update-file-autoloads (file)
281 "Update the autoloads for FILE in `generated-autoload-file'
282 \(which FILE might bind in its local variables)."
283 (interactive "fUpdate autoloads for file: ")
284 (let ((load-name (let ((name (file-name-nondirectory file)))
285 (if (string-match "\\.elc?$" name)
286 (substring name 0 (match-beginning 0))
287 name)))
288 (found nil)
289 (existing-buffer (get-file-buffer file)))
290 (save-excursion
291 ;; We want to get a value for generated-autoload-file from
292 ;; the local variables section if it's there.
293 (if existing-buffer
294 (set-buffer existing-buffer))
295 ;; We must read/write the file without any code conversion.
296 (let ((coding-system-for-read 'no-conversion))
297 (set-buffer (find-file-noselect generated-autoload-file)))
298 (save-excursion
299 (save-restriction
300 (widen)
301 (goto-char (point-min))
302 ;; Look for the section for LOAD-NAME.
303 (while (and (not found)
304 (search-forward generate-autoload-section-header nil t))
305 (let ((form (condition-case ()
306 (read (current-buffer))
307 (end-of-file nil))))
308 (cond ((string= (nth 2 form) load-name)
309 ;; We found the section for this file.
310 ;; Check if it is up to date.
311 (let ((begin (match-beginning 0))
312 (last-time (nth 4 form))
313 (file-time (nth 5 (file-attributes file))))
314 (if (and (or (null existing-buffer)
315 (not (buffer-modified-p existing-buffer)))
316 (listp last-time) (= (length last-time) 2)
317 (or (> (car last-time) (car file-time))
318 (and (= (car last-time) (car file-time))
319 (>= (nth 1 last-time)
320 (nth 1 file-time)))))
321 (progn
322 (if (interactive-p)
323 (message "\
324 Autoload section for %s is up to date."
325 file))
326 (setq found 'up-to-date))
327 (search-forward generate-autoload-section-trailer)
328 (delete-region begin (point))
329 (setq found t))))
330 ((string< load-name (nth 2 form))
331 ;; We've come to a section alphabetically later than
332 ;; LOAD-NAME. We assume the file is in order and so
333 ;; there must be no section for LOAD-NAME. We will
334 ;; insert one before the section here.
335 (goto-char (match-beginning 0))
336 (setq found 'new)))))
337 (or found
338 (progn
339 (setq found 'new)
340 ;; No later sections in the file. Put before the last page.
341 (goto-char (point-max))
342 (search-backward "\f" nil t)))
343 (or (eq found 'up-to-date)
344 (and (eq found 'new)
345 ;; Check that FILE has any cookies before generating a
346 ;; new section for it.
347 (save-excursion
348 (if existing-buffer
349 (set-buffer existing-buffer)
350 ;; It is faster to avoid visiting the file.
351 (set-buffer (get-buffer-create " *autoload-file*"))
352 (kill-all-local-variables)
353 (erase-buffer)
354 (setq buffer-undo-list t
355 buffer-read-only nil)
356 (emacs-lisp-mode)
357 (insert-file-contents file nil))
358 (save-excursion
359 (save-restriction
360 (widen)
361 (goto-char (point-min))
362 (prog1
363 (if (search-forward
364 (concat "\n" generate-autoload-cookie)
365 nil t)
367 (if (interactive-p)
368 (message "%s has no autoloads" file))
370 (or existing-buffer
371 (kill-buffer (current-buffer))))))))
372 (generate-file-autoloads file))))
373 (if (interactive-p) (save-buffer)))))
375 ;;;###autoload
376 (defun update-autoloads-from-directory (dir)
378 Update loaddefs.el with all the current autoloads from DIR, and no old ones.
379 This uses `update-file-autoloads' (which see) do its work."
380 (interactive "DUpdate autoloads from directory: ")
381 (setq dir (expand-file-name dir))
382 (let ((files (directory-files dir t "^[^=].*\\.el$")))
383 (save-excursion
384 (set-buffer (find-file-noselect
385 (if (file-exists-p generated-autoload-file)
386 (expand-file-name generated-autoload-file)
387 (expand-file-name generated-autoload-file
388 dir))))
389 (save-excursion
390 (goto-char (point-min))
391 (while (search-forward generate-autoload-section-header nil t)
392 (let* ((form (condition-case ()
393 (read (current-buffer))
394 (end-of-file nil)))
395 (file (nth 3 form)))
396 (cond ((not (stringp file)))
397 ((not (file-exists-p (expand-file-name file dir)))
398 ;; Remove the obsolete section.
399 (let ((begin (match-beginning 0)))
400 (search-forward generate-autoload-section-trailer)
401 (delete-region begin (point))))
403 (update-file-autoloads file)))
404 (setq files (delete file files)))))
405 ;; Elements remaining in FILES have no existing autoload sections.
406 (mapcar 'update-file-autoloads files)
407 (save-buffer))))
409 ;;;###autoload
410 (defun batch-update-autoloads ()
411 "Update loaddefs.el autoloads in batch mode.
412 Calls `update-autoloads-from-directory' on each command line argument."
413 (mapcar 'update-autoloads-from-directory command-line-args-left)
414 (setq command-line-args-left nil))
416 (provide 'autoload)
418 ;;; autoload.el ends here