(bcopy) Use memmove instead of memcpy.
[bpt/emacs.git] / lisp / emacs-lisp / autoload.el
CommitLineData
c0274f38
ER
1;;; autoload.el --- maintain autoloads in loaddefs.el.
2
7e4263eb 3;;; Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
3a801d0c 4;;;
e5167999 5;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
e9571d2a 6;; Keywords: maint
e5167999 7
0231f2dc
JB
8;;; This program is free software; you can redistribute it and/or modify
9;;; it under the terms of the GNU General Public License as published by
e5167999 10;;; the Free Software Foundation; either version 2, or (at your option)
0231f2dc
JB
11;;; any later version.
12;;;
13;;; This program is distributed in the hope that it will be useful,
14;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;;; GNU General Public License for more details.
17;;;
18;;; A copy of the GNU General Public License can be obtained from this
19;;; program's author (send electronic mail to roland@ai.mit.edu) or from
20;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
21;;; 02139, USA.
22;;;
23
e41b2db1
ER
24;;; Commentary;:
25
26;; This code helps GNU Emacs maintainers keep the autoload.el file up to
27;; date. It interprets magic cookies of the form ";;;###autoload" in
28;; lisp source files in various useful ways. To learn more, read the
29;; source; if you're going to use this, you'd better be able to.
30
e5167999
ER
31;;; Code:
32
0231f2dc
JB
33(defun make-autoload (form file)
34 "Turn FORM, a defun or defmacro, into an autoload for source file FILE.
35Returns nil if FORM is not a defun or defmacro."
36 (let ((car (car-safe form)))
327ab40d
RM
37 (if (memq car '(defun defmacro))
38 (let ((macrop (eq car 'defmacro))
39 name doc)
0231f2dc
JB
40 (setq form (cdr form))
41 (setq name (car form))
42 ;; Ignore the arguments.
43 (setq form (cdr (cdr form)))
44 (setq doc (car form))
45 (if (stringp doc)
46 (setq form (cdr form))
47 (setq doc nil))
48 (list 'autoload (list 'quote name) file doc
44893360
JB
49 (eq (car-safe (car form)) 'interactive)
50 (if macrop (list 'quote 'macro) nil)))
0231f2dc
JB
51 nil)))
52
53(defconst generate-autoload-cookie ";;;###autoload"
d08589bf
RM
54 "Magic comment indicating the following form should be autoloaded.
55Used by \\[update-file-autoloads]. This string should be
0231f2dc
JB
56meaningless to Lisp (e.g., a comment).
57
58This string is used:
59
60;;;###autoload
61\(defun function-to-be-autoloaded () ...)
62
63If this string appears alone on a line, the following form will be
64read and an autoload made for it. If there is further text on the line,
65that text will be copied verbatim to `generated-autoload-file'.")
66
67(defconst generate-autoload-section-header "\f\n;;;### "
68 "String inserted before the form identifying
69the section of autoloads for a file.")
70
71(defconst generate-autoload-section-trailer "\n;;;***\n"
72 "String which indicates the end of the section of autoloads for a file.")
73
daa37602
JB
74;;; Forms which have doc-strings which should be printed specially.
75;;; A doc-string-elt property of ELT says that (nth ELT FORM) is
76;;; the doc-string in FORM.
77;;;
78;;; There used to be the following note here:
79;;; ;;; Note: defconst and defvar should NOT be marked in this way.
80;;; ;;; We don't want to produce defconsts and defvars that
81;;; ;;; make-docfile can grok, because then it would grok them twice,
82;;; ;;; once in foo.el (where they are given with ;;;###autoload) and
83;;; ;;; once in loaddefs.el.
84;;;
85;;; Counter-note: Yes, they should be marked in this way.
86;;; make-docfile only processes those files that are loaded into the
87;;; dumped Emacs, and those files should never have anything
88;;; autoloaded here. The above-feared problem only occurs with files
89;;; which have autoloaded entries *and* are processed by make-docfile;
90;;; there should be no such files.
91
0231f2dc 92(put 'autoload 'doc-string-elt 3)
daa37602
JB
93(put 'defun 'doc-string-elt 3)
94(put 'defvar 'doc-string-elt 3)
95(put 'defconst 'doc-string-elt 3)
96(put 'defmacro 'doc-string-elt 3)
0231f2dc
JB
97
98(defun generate-file-autoloads (file)
99 "Insert at point a loaddefs autoload section for FILE.
100autoloads are generated for defuns and defmacros in FILE
da8826b4 101marked by `generate-autoload-cookie' (which see).
0231f2dc
JB
102If FILE is being visited in a buffer, the contents of the buffer
103are used."
104 (interactive "fGenerate autoloads for file: ")
105 (let ((outbuf (current-buffer))
0231f2dc
JB
106 (autoloads-done '())
107 (load-name (let ((name (file-name-nondirectory file)))
108 (if (string-match "\\.elc?$" name)
109 (substring name 0 (match-beginning 0))
110 name)))
111 (print-length nil)
aa7ea8bd 112 (float-output-format nil)
0231f2dc 113 (done-any nil)
7e4263eb 114 (visited (get-file-buffer file))
0231f2dc 115 output-end)
daa37602
JB
116
117 ;; If the autoload section we create here uses an absolute
118 ;; pathname for FILE in its header, and then Emacs is installed
119 ;; under a different path on another system,
120 ;; `update-autoloads-here' won't be able to find the files to be
121 ;; autoloaded. So, if FILE is in the same directory or a
e5d77022 122 ;; subdirectory of the current buffer's directory, we'll make it
daa37602
JB
123 ;; relative to the current buffer's directory.
124 (setq file (expand-file-name file))
1265394f
RM
125 (let* ((source-truename (file-truename file))
126 (dir-truename (file-name-as-directory
127 (file-truename default-directory)))
128 (len (length dir-truename)))
129 (if (and (< len (length source-truename))
130 (string= dir-truename (substring source-truename 0 len)))
131 (setq file (substring source-truename len))))
daa37602 132
0231f2dc 133 (message "Generating autoloads for %s..." file)
6798a385
JB
134 (save-excursion
135 (unwind-protect
136 (progn
137 (set-buffer (find-file-noselect file))
138 (save-excursion
139 (save-restriction
140 (widen)
141 (goto-char (point-min))
142 (while (not (eobp))
143 (skip-chars-forward " \t\n\f")
144 (cond
145 ((looking-at (regexp-quote generate-autoload-cookie))
146 (search-forward generate-autoload-cookie)
147 (skip-chars-forward " \t")
148 (setq done-any t)
149 (if (eolp)
150 ;; Read the next form and make an autoload.
c7516631 151 (let* ((form (prog1 (read (current-buffer))
6798a385
JB
152 (forward-line 1)))
153 (autoload (make-autoload form load-name))
154 (doc-string-elt (get (car-safe form)
155 'doc-string-elt)))
c7516631
RS
156 (if autoload
157 (setq autoloads-done (cons (nth 1 form)
158 autoloads-done))
159 (setq autoload form))
160 (if (and doc-string-elt
161 (stringp (nth doc-string-elt autoload)))
162 ;; We need to hack the printing because the
163 ;; doc-string must be printed specially for
164 ;; make-docfile (sigh).
165 (let* ((p (nthcdr (1- doc-string-elt)
166 autoload))
167 (elt (cdr p)))
168 (setcdr p nil)
169 (princ "\n(" outbuf)
0a933d4b
RS
170 (let ((print-escape-newlines t))
171 (mapcar (function (lambda (elt)
172 (prin1 elt outbuf)
173 (princ " " outbuf)))
174 autoload))
c7516631
RS
175 (princ "\"\\\n" outbuf)
176 (princ (substring
177 (prin1-to-string (car elt)) 1)
178 outbuf)
179 (if (null (cdr elt))
180 (princ ")" outbuf)
181 (princ " " outbuf)
6798a385 182 (princ (substring
c7516631
RS
183 (prin1-to-string (cdr elt))
184 1)
185 outbuf))
186 (terpri outbuf))
187 (print autoload outbuf)))
6798a385
JB
188 ;; Copy the rest of the line to the output.
189 (let ((begin (point)))
190 (forward-line 1)
191 (princ (buffer-substring begin (point)) outbuf))))
c7516631
RS
192 ((looking-at ";")
193 ;; Don't read the comment.
194 (forward-line 1))
195 (t
196 (forward-sexp 1)
197 (forward-line 1)))))))
6798a385
JB
198 (or visited
199 ;; We created this buffer, so we should kill it.
200 (kill-buffer (current-buffer)))
201 (set-buffer outbuf)
202 (setq output-end (point-marker))))
0231f2dc
JB
203 (if done-any
204 (progn
205 (insert generate-autoload-section-header)
206 (prin1 (list 'autoloads autoloads-done load-name file
207 (nth 5 (file-attributes file)))
208 outbuf)
209 (terpri outbuf)
210 (insert ";;; Generated autoloads from " file "\n")
211 (goto-char output-end)
212 (insert generate-autoload-section-trailer)))
213 (message "Generating autoloads for %s...done" file)))
e2b6138f 214\f
0231f2dc
JB
215(defconst generated-autoload-file "loaddefs.el"
216 "*File \\[update-file-autoloads] puts autoloads into.
217A .el file can set this in its local variables section to make its
218autoloads go somewhere else.")
219
220;;;###autoload
221(defun update-file-autoloads (file)
222 "Update the autoloads for FILE in `generated-autoload-file'
223\(which FILE might bind in its local variables)."
224 (interactive "fUpdate autoloads for file: ")
225 (let ((load-name (let ((name (file-name-nondirectory file)))
226 (if (string-match "\\.elc?$" name)
227 (substring name 0 (match-beginning 0))
228 name)))
229 (done nil)
230 (existing-buffer (get-file-buffer file)))
231 (save-excursion
232 ;; We want to get a value for generated-autoload-file from
233 ;; the local variables section if it's there.
234 (set-buffer (find-file-noselect file))
235 (set-buffer (find-file-noselect generated-autoload-file))
236 (save-excursion
237 (save-restriction
238 (widen)
239 (goto-char (point-min))
240 (while (search-forward generate-autoload-section-header nil t)
241 (let ((form (condition-case ()
242 (read (current-buffer))
243 (end-of-file nil))))
244 (if (string= (nth 2 form) load-name)
245 (let ((begin (match-beginning 0))
246 (last-time (nth 4 form))
247 (file-time (nth 5 (file-attributes file))))
248 (if (and (or (null existing-buffer)
249 (not (buffer-modified-p existing-buffer)))
250 (listp last-time) (= (length last-time) 2)
251 (or (> (car last-time) (car file-time))
252 (and (= (car last-time) (car file-time))
253 (>= (nth 1 last-time)
254 (nth 1 file-time)))))
255 (message "Autoload section for %s is up to date."
256 file)
257 (search-forward generate-autoload-section-trailer)
258 (delete-region begin (point))
259 (generate-file-autoloads file))
260 (setq done t))))))
261 (if done
88e37e7b 262 ;; There was an existing section and we have updated it.
0231f2dc 263 ()
88e37e7b
RM
264 (if (save-excursion
265 (set-buffer (find-file-noselect file))
266 (save-excursion
811eaa60
RM
267 (save-restriction
268 (widen)
269 (goto-char (point-min))
270 (search-forward generate-autoload-cookie nil t))))
88e37e7b
RM
271 ;; There are autoload cookies in FILE.
272 ;; Have the user tell us where to put the new section.
273 (progn
274 (save-window-excursion
275 (switch-to-buffer (current-buffer))
276 (with-output-to-temp-buffer "*Help*"
277 (princ (substitute-command-keys
278 (format "\
0231f2dc
JB
279Move point to where the autoload section
280for %s should be inserted.
281Then do \\[exit-recursive-edit]."
88e37e7b
RM
282 file))))
283 (recursive-edit)
284 (beginning-of-line))
285 (generate-file-autoloads file)))))
e2b6138f 286 (if (interactive-p) (save-buffer))
0231f2dc
JB
287 (if (and (null existing-buffer)
288 (setq existing-buffer (get-file-buffer file)))
289 (kill-buffer existing-buffer)))))
290
291;;;###autoload
292(defun update-autoloads-here ()
d08589bf
RM
293 "\
294Update sections of the current buffer generated by \\[update-file-autoloads]."
0231f2dc
JB
295 (interactive)
296 (let ((generated-autoload-file (buffer-file-name)))
297 (save-excursion
298 (goto-char (point-min))
299 (while (search-forward generate-autoload-section-header nil t)
300 (let* ((form (condition-case ()
301 (read (current-buffer))
302 (end-of-file nil)))
303 (file (nth 3 form)))
304 (if (and (stringp file)
305 (or (get-file-buffer file)
306 (file-exists-p file)))
307 ()
308 (setq file (if (y-or-n-p (format "Library \"%s\" (load \
309file \"%s\") doesn't exist. Remove its autoload section? "
310 (nth 2 form) file))
311 t
312 (condition-case ()
313 (read-file-name (format "Find \"%s\" load file: "
314 (nth 2 form))
315 nil nil t)
316 (quit nil)))))
317 (if file
318 (let ((begin (match-beginning 0)))
319 (search-forward generate-autoload-section-trailer)
320 (delete-region begin (point))))
321 (if (stringp file)
322 (generate-file-autoloads file)))))))
323
324;;;###autoload
325(defun update-directory-autoloads (dir)
326 "Run \\[update-file-autoloads] on each .el file in DIR."
327 (interactive "DUpdate autoloads for directory: ")
328 (mapcar 'update-file-autoloads
e2b6138f
RM
329 (directory-files dir nil "\\.el$"))
330 (if (interactive-p)
331 (save-excursion
332 (set-buffer (find-file-noselect generated-autoload-file))
333 (save-buffer))))
0231f2dc
JB
334
335;;;###autoload
336(defun batch-update-autoloads ()
337 "Update the autoloads for the files or directories on the command line.
338Runs \\[update-file-autoloads] on files and \\[update-directory-autoloads]
339on directories. Must be used only with -batch, and kills Emacs on completion.
340Each file will be processed even if an error occurred previously.
ffd56f97 341For example, invoke \"emacs -batch -f batch-update-autoloads *.el\""
0231f2dc 342 (if (not noninteractive)
23de5766 343 (error "batch-update-autoloads is to be used only with -batch"))
0231f2dc
JB
344 (let ((lost nil)
345 (args command-line-args-left))
346 (while args
347 (catch 'file
348 (condition-case lossage
349 (if (file-directory-p (expand-file-name (car args)))
350 (update-directory-autoloads (car args))
351 (update-file-autoloads (car args)))
352 (error (progn (message ">>Error processing %s: %s"
353 (car args) lossage)
354 (setq lost t)
355 (throw 'file nil)))))
356 (setq args (cdr args)))
357 (save-some-buffers t)
358 (message "Done")
359 (kill-emacs (if lost 1 0))))
360
361(provide 'autoload)
ffd56f97 362
c0274f38 363;;; autoload.el ends here