*** empty log message ***
[emacs.git] / lisp / emacs-lisp / autoload.el
blobb399a29bfa04df9742a47e1032a1eb93dbce5230
1 ;;; autoload.el --- maintain autoloads in loaddefs.el.
3 ;;; Copyright (C) 1991, 1992 Free Software Foundation, Inc.
4 ;;; Written by Roland McGrath.
5 ;;;
6 ;;; This program is free software; you can redistribute it and/or modify
7 ;;; it under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 1, or (at your option)
9 ;;; any later version.
10 ;;;
11 ;;; This program is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; A copy of the GNU General Public License can be obtained from this
17 ;;; program's author (send electronic mail to roland@ai.mit.edu) or from
18 ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
19 ;;; 02139, USA.
20 ;;;
22 (defun make-autoload (form file)
23 "Turn FORM, a defun or defmacro, into an autoload for source file FILE.
24 Returns nil if FORM is not a defun or defmacro."
25 (let ((car (car-safe form)))
26 (if (or (eq car 'defun) (eq car 'defmacro))
27 (let (name doc macrop)
28 (setq macrop (eq car 'defmacro))
29 (setq form (cdr form))
30 (setq name (car form))
31 ;; Ignore the arguments.
32 (setq form (cdr (cdr form)))
33 (setq doc (car form))
34 (if (stringp doc)
35 (setq form (cdr form))
36 (setq doc nil))
37 (list 'autoload (list 'quote name) file doc
38 (eq (car-safe (car form)) 'interactive) macrop))
39 nil)))
41 (defconst generate-autoload-cookie ";;;###autoload"
42 "Magic comment that tells \\[update-file-autoloads]
43 to make the following form into an autoload. This string should be
44 meaningless to Lisp (e.g., a comment).
46 This string is used:
48 ;;;###autoload
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 inserted before the form identifying
57 the section of autoloads for a file.")
59 (defconst generate-autoload-section-trailer "\n;;;***\n"
60 "String which indicates the end of the section of autoloads for a file.")
62 ;; Forms which have doc-strings which should be printed specially.
63 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
64 ;; the doc-string in FORM.
65 ;; Note: defconst and defvar should NOT be marked in this way.
66 ;; We don't want to produce defconsts and defvars that make-docfile can
67 ;; grok, because then it would grok them twice, once in foo.el (where they
68 ;; are given with ;;;###autoload) and once in loaddefs.el.
69 (put 'autoload 'doc-string-elt 3)
71 (defun generate-file-autoloads (file)
72 "Insert at point a loaddefs autoload section for FILE.
73 autoloads are generated for defuns and defmacros in FILE
74 marked by `generate-autoload-regexp' (which see).
75 If FILE is being visited in a buffer, the contents of the buffer
76 are used."
77 (interactive "fGenerate autoloads for file: ")
78 (let ((outbuf (current-buffer))
79 (inbuf (find-file-noselect file))
80 (autoloads-done '())
81 (load-name (let ((name (file-name-nondirectory file)))
82 (if (string-match "\\.elc?$" name)
83 (substring name 0 (match-beginning 0))
84 name)))
85 (print-length nil)
86 (floating-output-format "%20e")
87 (done-any nil)
88 output-end)
89 (message "Generating autoloads for %s..." file)
90 (save-excursion
91 (set-buffer inbuf)
92 (save-excursion
93 (save-restriction
94 (widen)
95 (goto-char (point-min))
96 (while (not (eobp))
97 (skip-chars-forward " \t\n\f")
98 (cond ((looking-at (regexp-quote generate-autoload-cookie))
99 (search-forward generate-autoload-cookie)
100 (skip-chars-forward " \t")
101 (setq done-any t)
102 (if (eolp)
103 ;; Read the next form and make an autoload.
104 (let* ((form (prog1 (read (current-buffer))
105 (forward-line 1)))
106 (autoload (make-autoload form load-name))
107 (doc-string-elt (get (car-safe form)
108 'doc-string-elt)))
109 (if autoload
110 (setq autoloads-done (cons (nth 1 form)
111 autoloads-done))
112 (setq autoload form))
113 (if (and doc-string-elt
114 (stringp (nth doc-string-elt autoload)))
115 ;; We need to hack the printing because the
116 ;; doc-string must be printed specially for
117 ;; make-docfile (sigh).
118 (let* ((p (nthcdr (1- doc-string-elt) autoload))
119 (elt (cdr p)))
120 (setcdr p nil)
121 (princ "\n(" outbuf)
122 (mapcar (function (lambda (elt)
123 (prin1 elt outbuf)
124 (princ " " outbuf)))
125 autoload)
126 (princ "\"\\\n" outbuf)
127 (princ (substring (prin1-to-string (car elt)) 1)
128 outbuf)
129 (if (null (cdr elt))
130 (princ ")" outbuf)
131 (princ " " outbuf)
132 (princ (substring (prin1-to-string (cdr elt))
134 outbuf))
135 (terpri outbuf))
136 (print autoload outbuf)))
137 ;; Copy the rest of the line to the output.
138 (let ((begin (point)))
139 (forward-line 1)
140 (princ (buffer-substring begin (point)) outbuf))))
141 ((looking-at ";")
142 ;; Don't read the comment.
143 (forward-line 1))
145 (forward-sexp 1)
146 (forward-line 1))))))
147 (set-buffer outbuf)
148 (setq output-end (point-marker)))
149 (if done-any
150 (progn
151 (insert generate-autoload-section-header)
152 (prin1 (list 'autoloads autoloads-done load-name file
153 (nth 5 (file-attributes file)))
154 outbuf)
155 (terpri outbuf)
156 (insert ";;; Generated autoloads from " file "\n")
157 (goto-char output-end)
158 (insert generate-autoload-section-trailer)))
159 (message "Generating autoloads for %s...done" file)))
161 (defconst generated-autoload-file "loaddefs.el"
162 "*File \\[update-file-autoloads] puts autoloads into.
163 A .el file can set this in its local variables section to make its
164 autoloads go somewhere else.")
166 ;;;###autoload
167 (defun update-file-autoloads (file)
168 "Update the autoloads for FILE in `generated-autoload-file'
169 \(which FILE might bind in its local variables)."
170 (interactive "fUpdate autoloads for file: ")
171 (let ((load-name (let ((name (file-name-nondirectory file)))
172 (if (string-match "\\.elc?$" name)
173 (substring name 0 (match-beginning 0))
174 name)))
175 (done nil)
176 (existing-buffer (get-file-buffer file)))
177 (save-excursion
178 ;; We want to get a value for generated-autoload-file from
179 ;; the local variables section if it's there.
180 (set-buffer (find-file-noselect file))
181 (set-buffer (find-file-noselect generated-autoload-file))
182 (save-excursion
183 (save-restriction
184 (widen)
185 (goto-char (point-min))
186 (while (search-forward generate-autoload-section-header nil t)
187 (let ((form (condition-case ()
188 (read (current-buffer))
189 (end-of-file nil))))
190 (if (string= (nth 2 form) load-name)
191 (let ((begin (match-beginning 0))
192 (last-time (nth 4 form))
193 (file-time (nth 5 (file-attributes file))))
194 (if (and (or (null existing-buffer)
195 (not (buffer-modified-p existing-buffer)))
196 (listp last-time) (= (length last-time) 2)
197 (or (> (car last-time) (car file-time))
198 (and (= (car last-time) (car file-time))
199 (>= (nth 1 last-time)
200 (nth 1 file-time)))))
201 (message "Autoload section for %s is up to date."
202 file)
203 (search-forward generate-autoload-section-trailer)
204 (delete-region begin (point))
205 (generate-file-autoloads file))
206 (setq done t))))))
207 (if done
209 ;; Have the user tell us where to put the section.
210 (save-window-excursion
211 (switch-to-buffer (current-buffer))
212 (with-output-to-temp-buffer "*Help*"
213 (princ (substitute-command-keys
214 (format "\
215 Move point to where the autoload section
216 for %s should be inserted.
217 Then do \\[exit-recursive-edit]."
218 file))))
219 (recursive-edit))
220 (generate-file-autoloads file)))
221 (if (and (null existing-buffer)
222 (setq existing-buffer (get-file-buffer file)))
223 (kill-buffer existing-buffer)))))
225 ;;;###autoload
226 (defun update-autoloads-here ()
227 "Update the sections of the current buffer generated by
228 \\[update-file-autoloads]."
229 (interactive)
230 (let ((generated-autoload-file (buffer-file-name)))
231 (save-excursion
232 (goto-char (point-min))
233 (while (search-forward generate-autoload-section-header nil t)
234 (let* ((form (condition-case ()
235 (read (current-buffer))
236 (end-of-file nil)))
237 (file (nth 3 form)))
238 (if (and (stringp file)
239 (or (get-file-buffer file)
240 (file-exists-p file)))
242 (setq file (if (y-or-n-p (format "Library \"%s\" (load \
243 file \"%s\") doesn't exist. Remove its autoload section? "
244 (nth 2 form) file))
246 (condition-case ()
247 (read-file-name (format "Find \"%s\" load file: "
248 (nth 2 form))
249 nil nil t)
250 (quit nil)))))
251 (if file
252 (let ((begin (match-beginning 0)))
253 (search-forward generate-autoload-section-trailer)
254 (delete-region begin (point))))
255 (if (stringp file)
256 (generate-file-autoloads file)))))))
258 ;;;###autoload
259 (defun update-directory-autoloads (dir)
260 "Run \\[update-file-autoloads] on each .el file in DIR."
261 (interactive "DUpdate autoloads for directory: ")
262 (mapcar 'update-file-autoloads
263 (directory-files dir nil "\\.el$")))
265 ;;;###autoload
266 (defun batch-update-autoloads ()
267 "Update the autoloads for the files or directories on the command line.
268 Runs \\[update-file-autoloads] on files and \\[update-directory-autoloads]
269 on directories. Must be used only with -batch, and kills Emacs on completion.
270 Each file will be processed even if an error occurred previously.
271 For example, invoke \"emacs -batch -f batch-update-autoloads *.el\""
272 (if (not noninteractive)
273 (error "batch-update-file-autoloads is to be used only with -batch"))
274 (let ((lost nil)
275 (args command-line-args-left))
276 (while args
277 (catch 'file
278 (condition-case lossage
279 (if (file-directory-p (expand-file-name (car args)))
280 (update-directory-autoloads (car args))
281 (update-file-autoloads (car args)))
282 (error (progn (message ">>Error processing %s: %s"
283 (car args) lossage)
284 (setq lost t)
285 (throw 'file nil)))))
286 (setq args (cdr args)))
287 (save-some-buffers t)
288 (message "Done")
289 (kill-emacs (if lost 1 0))))
291 (provide 'autoload)
293 ;;; autoload.el ends here