662
|
1 ;;; autoload.el --- maintain autoloads in loaddefs.el.
|
|
2
|
1884
4a8bc12e7017
(generate-file-autoloads): If no buffer was visiting FILE when we started,
Roland McGrath <roland@gnu.org>
diff
changeset
|
3 ;;; Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
|
473
|
4 ;;;
|
846
|
5 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
|
2247
|
6 ;; Keywords: maint
|
846
|
7
|
473
|
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
|
807
|
10 ;;; the Free Software Foundation; either version 2, or (at your option)
|
473
|
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
|
2307
|
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
|
807
|
31 ;;; Code:
|
|
32
|
473
|
33 (defun make-autoload (form file)
|
|
34 "Turn FORM, a defun or defmacro, into an autoload for source file FILE.
|
|
35 Returns nil if FORM is not a defun or defmacro."
|
|
36 (let ((car (car-safe form)))
|
|
37 (if (or (eq car 'defun) (eq car 'defmacro))
|
|
38 (let (name doc macrop)
|
|
39 (setq macrop (eq car 'defmacro))
|
|
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
|
1552
|
49 (eq (car-safe (car form)) 'interactive)
|
|
50 (if macrop (list 'quote 'macro) nil)))
|
473
|
51 nil)))
|
|
52
|
|
53 (defconst generate-autoload-cookie ";;;###autoload"
|
|
54 "Magic comment that tells \\[update-file-autoloads]
|
|
55 to make the following form into an autoload. This string should be
|
|
56 meaningless to Lisp (e.g., a comment).
|
|
57
|
|
58 This string is used:
|
|
59
|
|
60 ;;;###autoload
|
|
61 \(defun function-to-be-autoloaded () ...)
|
|
62
|
|
63 If this string appears alone on a line, the following form will be
|
|
64 read and an autoload made for it. If there is further text on the line,
|
|
65 that 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
|
|
69 the 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
|
727
|
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
|
473
|
92 (put 'autoload 'doc-string-elt 3)
|
727
|
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)
|
473
|
97
|
|
98 (defun generate-file-autoloads (file)
|
|
99 "Insert at point a loaddefs autoload section for FILE.
|
|
100 autoloads are generated for defuns and defmacros in FILE
|
2494
|
101 marked by `generate-autoload-cookie' (which see).
|
473
|
102 If FILE is being visited in a buffer, the contents of the buffer
|
|
103 are used."
|
|
104 (interactive "fGenerate autoloads for file: ")
|
|
105 (let ((outbuf (current-buffer))
|
|
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)
|
|
112 (floating-output-format "%20e")
|
|
113 (done-any nil)
|
1884
4a8bc12e7017
(generate-file-autoloads): If no buffer was visiting FILE when we started,
Roland McGrath <roland@gnu.org>
diff
changeset
|
114 (visited (get-file-buffer file))
|
473
|
115 output-end)
|
727
|
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
|
732
|
122 ;; subdirectory of the current buffer's directory, we'll make it
|
727
|
123 ;; relative to the current buffer's directory.
|
|
124 (setq file (expand-file-name file))
|
|
125 (if (and (< (length default-directory) (length file))
|
|
126 (string= default-directory
|
|
127 (substring file 0 (length default-directory))))
|
|
128 (progn
|
|
129 (setq file (substring file (length default-directory)))))
|
|
130
|
473
|
131 (message "Generating autoloads for %s..." file)
|
1975
|
132 (save-excursion
|
|
133 (unwind-protect
|
|
134 (progn
|
|
135 (set-buffer (find-file-noselect file))
|
|
136 (save-excursion
|
|
137 (save-restriction
|
|
138 (widen)
|
|
139 (goto-char (point-min))
|
|
140 (while (not (eobp))
|
|
141 (skip-chars-forward " \t\n\f")
|
|
142 (cond
|
|
143 ((looking-at (regexp-quote generate-autoload-cookie))
|
|
144 (search-forward generate-autoload-cookie)
|
|
145 (skip-chars-forward " \t")
|
|
146 (setq done-any t)
|
|
147 (if (eolp)
|
|
148 ;; Read the next form and make an autoload.
|
|
149 (let* ((form (prog1 (read (current-buffer))
|
|
150 (forward-line 1)))
|
|
151 (autoload (make-autoload form load-name))
|
|
152 (doc-string-elt (get (car-safe form)
|
|
153 'doc-string-elt)))
|
|
154 (if autoload
|
|
155 (setq autoloads-done (cons (nth 1 form)
|
|
156 autoloads-done))
|
|
157 (setq autoload form))
|
|
158 (if (and doc-string-elt
|
|
159 (stringp (nth doc-string-elt autoload)))
|
|
160 ;; We need to hack the printing because the
|
|
161 ;; doc-string must be printed specially for
|
|
162 ;; make-docfile (sigh).
|
|
163 (let* ((p (nthcdr (1- doc-string-elt)
|
|
164 autoload))
|
|
165 (elt (cdr p)))
|
|
166 (setcdr p nil)
|
|
167 (princ "\n(" outbuf)
|
|
168 (mapcar (function (lambda (elt)
|
|
169 (prin1 elt outbuf)
|
|
170 (princ " " outbuf)))
|
|
171 autoload)
|
|
172 (princ "\"\\\n" outbuf)
|
|
173 (princ (substring
|
|
174 (prin1-to-string (car elt)) 1)
|
|
175 outbuf)
|
|
176 (if (null (cdr elt))
|
|
177 (princ ")" outbuf)
|
|
178 (princ " " outbuf)
|
|
179 (princ (substring
|
|
180 (prin1-to-string (cdr elt))
|
|
181 1)
|
|
182 outbuf))
|
|
183 (terpri outbuf))
|
|
184 (print autoload outbuf)))
|
|
185 ;; Copy the rest of the line to the output.
|
|
186 (let ((begin (point)))
|
|
187 (forward-line 1)
|
|
188 (princ (buffer-substring begin (point)) outbuf))))
|
|
189 ((looking-at ";")
|
|
190 ;; Don't read the comment.
|
|
191 (forward-line 1))
|
|
192 (t
|
|
193 (forward-sexp 1)
|
|
194 (forward-line 1)))))))
|
|
195 (or visited
|
|
196 ;; We created this buffer, so we should kill it.
|
|
197 (kill-buffer (current-buffer)))
|
|
198 (set-buffer outbuf)
|
|
199 (setq output-end (point-marker))))
|
473
|
200 (if done-any
|
|
201 (progn
|
|
202 (insert generate-autoload-section-header)
|
|
203 (prin1 (list 'autoloads autoloads-done load-name file
|
|
204 (nth 5 (file-attributes file)))
|
|
205 outbuf)
|
|
206 (terpri outbuf)
|
|
207 (insert ";;; Generated autoloads from " file "\n")
|
|
208 (goto-char output-end)
|
|
209 (insert generate-autoload-section-trailer)))
|
|
210 (message "Generating autoloads for %s...done" file)))
|
2535
|
211
|
473
|
212 (defconst generated-autoload-file "loaddefs.el"
|
|
213 "*File \\[update-file-autoloads] puts autoloads into.
|
|
214 A .el file can set this in its local variables section to make its
|
|
215 autoloads go somewhere else.")
|
|
216
|
|
217 ;;;###autoload
|
|
218 (defun update-file-autoloads (file)
|
|
219 "Update the autoloads for FILE in `generated-autoload-file'
|
|
220 \(which FILE might bind in its local variables)."
|
|
221 (interactive "fUpdate autoloads for file: ")
|
|
222 (let ((load-name (let ((name (file-name-nondirectory file)))
|
|
223 (if (string-match "\\.elc?$" name)
|
|
224 (substring name 0 (match-beginning 0))
|
|
225 name)))
|
|
226 (done nil)
|
|
227 (existing-buffer (get-file-buffer file)))
|
|
228 (save-excursion
|
|
229 ;; We want to get a value for generated-autoload-file from
|
|
230 ;; the local variables section if it's there.
|
|
231 (set-buffer (find-file-noselect file))
|
|
232 (set-buffer (find-file-noselect generated-autoload-file))
|
|
233 (save-excursion
|
|
234 (save-restriction
|
|
235 (widen)
|
|
236 (goto-char (point-min))
|
|
237 (while (search-forward generate-autoload-section-header nil t)
|
|
238 (let ((form (condition-case ()
|
|
239 (read (current-buffer))
|
|
240 (end-of-file nil))))
|
|
241 (if (string= (nth 2 form) load-name)
|
|
242 (let ((begin (match-beginning 0))
|
|
243 (last-time (nth 4 form))
|
|
244 (file-time (nth 5 (file-attributes file))))
|
|
245 (if (and (or (null existing-buffer)
|
|
246 (not (buffer-modified-p existing-buffer)))
|
|
247 (listp last-time) (= (length last-time) 2)
|
|
248 (or (> (car last-time) (car file-time))
|
|
249 (and (= (car last-time) (car file-time))
|
|
250 (>= (nth 1 last-time)
|
|
251 (nth 1 file-time)))))
|
|
252 (message "Autoload section for %s is up to date."
|
|
253 file)
|
|
254 (search-forward generate-autoload-section-trailer)
|
|
255 (delete-region begin (point))
|
|
256 (generate-file-autoloads file))
|
|
257 (setq done t))))))
|
|
258 (if done
|
|
259 ()
|
|
260 ;; Have the user tell us where to put the section.
|
|
261 (save-window-excursion
|
|
262 (switch-to-buffer (current-buffer))
|
|
263 (with-output-to-temp-buffer "*Help*"
|
|
264 (princ (substitute-command-keys
|
|
265 (format "\
|
|
266 Move point to where the autoload section
|
|
267 for %s should be inserted.
|
|
268 Then do \\[exit-recursive-edit]."
|
|
269 file))))
|
1108
|
270 (recursive-edit)
|
|
271 (beginning-of-line))
|
473
|
272 (generate-file-autoloads file)))
|
2535
|
273 (if (interactive-p) (save-buffer))
|
473
|
274 (if (and (null existing-buffer)
|
|
275 (setq existing-buffer (get-file-buffer file)))
|
|
276 (kill-buffer existing-buffer)))))
|
|
277
|
|
278 ;;;###autoload
|
|
279 (defun update-autoloads-here ()
|
|
280 "Update the sections of the current buffer generated by
|
|
281 \\[update-file-autoloads]."
|
|
282 (interactive)
|
|
283 (let ((generated-autoload-file (buffer-file-name)))
|
|
284 (save-excursion
|
|
285 (goto-char (point-min))
|
|
286 (while (search-forward generate-autoload-section-header nil t)
|
|
287 (let* ((form (condition-case ()
|
|
288 (read (current-buffer))
|
|
289 (end-of-file nil)))
|
|
290 (file (nth 3 form)))
|
|
291 (if (and (stringp file)
|
|
292 (or (get-file-buffer file)
|
|
293 (file-exists-p file)))
|
|
294 ()
|
|
295 (setq file (if (y-or-n-p (format "Library \"%s\" (load \
|
|
296 file \"%s\") doesn't exist. Remove its autoload section? "
|
|
297 (nth 2 form) file))
|
|
298 t
|
|
299 (condition-case ()
|
|
300 (read-file-name (format "Find \"%s\" load file: "
|
|
301 (nth 2 form))
|
|
302 nil nil t)
|
|
303 (quit nil)))))
|
|
304 (if file
|
|
305 (let ((begin (match-beginning 0)))
|
|
306 (search-forward generate-autoload-section-trailer)
|
|
307 (delete-region begin (point))))
|
|
308 (if (stringp file)
|
|
309 (generate-file-autoloads file)))))))
|
|
310
|
|
311 ;;;###autoload
|
|
312 (defun update-directory-autoloads (dir)
|
|
313 "Run \\[update-file-autoloads] on each .el file in DIR."
|
|
314 (interactive "DUpdate autoloads for directory: ")
|
|
315 (mapcar 'update-file-autoloads
|
2535
|
316 (directory-files dir nil "\\.el$"))
|
|
317 (if (interactive-p)
|
|
318 (save-excursion
|
|
319 (set-buffer (find-file-noselect generated-autoload-file))
|
|
320 (save-buffer))))
|
473
|
321
|
|
322 ;;;###autoload
|
|
323 (defun batch-update-autoloads ()
|
|
324 "Update the autoloads for the files or directories on the command line.
|
|
325 Runs \\[update-file-autoloads] on files and \\[update-directory-autoloads]
|
|
326 on directories. Must be used only with -batch, and kills Emacs on completion.
|
|
327 Each file will be processed even if an error occurred previously.
|
648
|
328 For example, invoke \"emacs -batch -f batch-update-autoloads *.el\""
|
473
|
329 (if (not noninteractive)
|
|
330 (error "batch-update-file-autoloads is to be used only with -batch"))
|
|
331 (let ((lost nil)
|
|
332 (args command-line-args-left))
|
|
333 (while args
|
|
334 (catch 'file
|
|
335 (condition-case lossage
|
|
336 (if (file-directory-p (expand-file-name (car args)))
|
|
337 (update-directory-autoloads (car args))
|
|
338 (update-file-autoloads (car args)))
|
|
339 (error (progn (message ">>Error processing %s: %s"
|
|
340 (car args) lossage)
|
|
341 (setq lost t)
|
|
342 (throw 'file nil)))))
|
|
343 (setq args (cdr args)))
|
|
344 (save-some-buffers t)
|
|
345 (message "Done")
|
|
346 (kill-emacs (if lost 1 0))))
|
|
347
|
|
348 (provide 'autoload)
|
648
|
349
|
662
|
350 ;;; autoload.el ends here
|