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