Commit | Line | Data |
---|---|---|
56eebc29 | 1 | ;; autoload.el --- maintain autoloads in loaddefs.el |
c0274f38 | 2 | |
95df8112 | 3 | ;; Copyright (C) 1991-1997, 2001-2011 Free Software Foundation, Inc. |
b578f267 | 4 | |
5762abec | 5 | ;; Author: Roland McGrath <roland@gnu.org> |
e9571d2a | 6 | ;; Keywords: maint |
bd78fa1d | 7 | ;; Package: emacs |
e5167999 | 8 | |
b578f267 EN |
9 | ;; This file is part of GNU Emacs. |
10 | ||
d6cba7ae | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
b578f267 | 12 | ;; it under the terms of the GNU General Public License as published by |
d6cba7ae GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
b578f267 EN |
15 | |
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
d6cba7ae | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
0231f2dc | 23 | |
07b3798c | 24 | ;;; Commentary: |
e41b2db1 | 25 | |
00ee57f3 | 26 | ;; This code helps GNU Emacs maintainers keep the loaddefs.el file up to |
e41b2db1 ER |
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 | ||
b0daab9a | 33 | (require 'lisp-mode) ;for `doc-string-elt' properties. |
890df022 | 34 | (require 'help-fns) ;for help-add-fundoc-usage. |
6826a134 | 35 | (eval-when-compile (require 'cl)) |
b0daab9a | 36 | |
0ceb5fe0 RS |
37 | (defvar generated-autoload-file "loaddefs.el" |
38 | "*File \\[update-file-autoloads] puts autoloads into. | |
39 | A `.el' file can set this in its local variables section to make its | |
c8aca7d1 KH |
40 | autoloads go somewhere else. The autoload file is assumed to contain a |
41 | trailer starting with a FormFeed character.") | |
a25beddb | 42 | ;;;###autoload |
3b979520 | 43 | (put 'generated-autoload-file 'safe-local-variable 'stringp) |
0ceb5fe0 | 44 | |
15120dec CY |
45 | (defvar generated-autoload-load-name nil |
46 | "Load name for `autoload' statements generated from autoload cookies. | |
47 | If nil, this defaults to the file name, sans extension.") | |
48 | ;;;###autoload | |
49 | (put 'generated-autoload-load-name 'safe-local-variable 'stringp) | |
50 | ||
3b979520 SM |
51 | ;; This feels like it should be a defconst, but MH-E sets it to |
52 | ;; ";;;###mh-autoload" for the autoloads that are to go into mh-loaddefs.el. | |
53 | (defvar generate-autoload-cookie ";;;###autoload" | |
0ceb5fe0 RS |
54 | "Magic comment indicating the following form should be autoloaded. |
55 | Used by \\[update-file-autoloads]. This string should be | |
56 | meaningless to Lisp (e.g., a comment). | |
57 | ||
58 | This string is used: | |
59 | ||
3b979520 | 60 | \;;;###autoload |
0ceb5fe0 RS |
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 | ||
a0436952 GM |
67 | (defvar autoload-excludes nil |
68 | "If non-nil, list of absolute file names not to scan for autoloads.") | |
69 | ||
0ceb5fe0 | 70 | (defconst generate-autoload-section-header "\f\n;;;### " |
2336b6a9 | 71 | "String that marks the form at the start of a new file's autoload section.") |
0ceb5fe0 RS |
72 | |
73 | (defconst generate-autoload-section-trailer "\n;;;***\n" | |
74 | "String which indicates the end of the section of autoloads for a file.") | |
75 | ||
2336b6a9 KH |
76 | (defconst generate-autoload-section-continuation ";;;;;; " |
77 | "String to add on each continuation of the section header form.") | |
78 | ||
1fad2b12 SM |
79 | (defvar autoload-modified-buffers) ;Dynamically scoped var. |
80 | ||
0231f2dc | 81 | (defun make-autoload (form file) |
ceaa3695 | 82 | "Turn FORM into an autoload or defvar for source file FILE. |
e8139c11 SM |
83 | Returns nil if FORM is not a special autoload form (i.e. a function definition |
84 | or macro definition or a defcustom)." | |
85 | (let ((car (car-safe form)) expand) | |
86 | (cond | |
87 | ;; For complex cases, try again on the macro-expansion. | |
1b39b493 | 88 | ((and (memq car '(easy-mmode-define-global-mode define-global-minor-mode |
32324290 | 89 | define-globalized-minor-mode |
e8139c11 SM |
90 | easy-mmode-define-minor-mode define-minor-mode)) |
91 | (setq expand (let ((load-file-name file)) (macroexpand form))) | |
92 | (eq (car expand) 'progn) | |
93 | (memq :autoload-end expand)) | |
94 | (let ((end (memq :autoload-end expand))) | |
95 | ;; Cut-off anything after the :autoload-end marker. | |
96 | (setcdr end nil) | |
97 | (cons 'progn | |
98 | (mapcar (lambda (form) (make-autoload form file)) | |
99 | (cdr expand))))) | |
100 | ||
101 | ;; For special function-like operators, use the `autoload' function. | |
102 | ((memq car '(defun define-skeleton defmacro define-derived-mode | |
c6a3142d JL |
103 | define-compilation-mode define-generic-mode |
104 | easy-mmode-define-global-mode define-global-minor-mode | |
32324290 | 105 | define-globalized-minor-mode |
c6a3142d | 106 | easy-mmode-define-minor-mode define-minor-mode |
15120dec | 107 | defun* defmacro* define-overloadable-function)) |
74312ddc | 108 | (let* ((macrop (memq car '(defmacro defmacro*))) |
e8139c11 | 109 | (name (nth 1 form)) |
6826a134 | 110 | (args (case car |
0193499f SM |
111 | ((defun defmacro defun* defmacro* |
112 | define-overloadable-function) (nth 2 form)) | |
113 | ((define-skeleton) '(&optional str arg)) | |
114 | ((define-generic-mode define-derived-mode | |
115 | define-compilation-mode) nil) | |
116 | (t))) | |
e8139c11 SM |
117 | (body (nthcdr (get car 'doc-string-elt) form)) |
118 | (doc (if (stringp (car body)) (pop body)))) | |
890df022 SM |
119 | (when (listp args) |
120 | ;; Add the usage form at the end where describe-function-1 | |
121 | ;; can recover it. | |
122 | (setq doc (help-add-fundoc-usage doc args))) | |
0193499f SM |
123 | (let ((exp |
124 | ;; `define-generic-mode' quotes the name, so take care of that | |
125 | (list 'autoload (if (listp name) name (list 'quote name)) | |
126 | file doc | |
127 | (or (and (memq car '(define-skeleton define-derived-mode | |
128 | define-generic-mode | |
129 | easy-mmode-define-global-mode | |
130 | define-global-minor-mode | |
131 | define-globalized-minor-mode | |
132 | easy-mmode-define-minor-mode | |
133 | define-minor-mode)) t) | |
134 | (eq (car-safe (car body)) 'interactive)) | |
135 | (if macrop (list 'quote 'macro) nil)))) | |
136 | (when macrop | |
137 | ;; Special case to autoload some of the macro's declarations. | |
138 | (let ((decls (nth (if (stringp (nth 3 form)) 4 3) form)) | |
139 | (exps '())) | |
ba83908c | 140 | (when (eq (car-safe decls) 'declare) |
0193499f SM |
141 | ;; FIXME: We'd like to reuse macro-declaration-function, |
142 | ;; but we can't since it doesn't return anything. | |
143 | (dolist (decl decls) | |
144 | (case (car-safe decl) | |
145 | (indent | |
146 | (push `(put ',name 'lisp-indent-function ',(cadr decl)) | |
147 | exps)) | |
148 | (doc-string | |
149 | (push `(put ',name 'doc-string-elt ',(cadr decl)) exps)))) | |
150 | (when exps | |
151 | (setq exp `(progn ,exp ,@exps)))))) | |
152 | exp))) | |
e8139c11 | 153 | |
15120dec CY |
154 | ;; For defclass forms, use `eieio-defclass-autoload'. |
155 | ((eq car 'defclass) | |
156 | (let ((name (nth 1 form)) | |
157 | (superclasses (nth 2 form)) | |
158 | (doc (nth 4 form))) | |
159 | (list 'eieio-defclass-autoload (list 'quote name) | |
160 | (list 'quote superclasses) file doc))) | |
161 | ||
d49298d9 | 162 | ;; Convert defcustom to less space-consuming data. |
e8139c11 SM |
163 | ((eq car 'defcustom) |
164 | (let ((varname (car-safe (cdr-safe form))) | |
165 | (init (car-safe (cdr-safe (cdr-safe form)))) | |
166 | (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form))))) | |
d49298d9 MR |
167 | ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form))))) |
168 | ) | |
169 | `(progn | |
170 | (defvar ,varname ,init ,doc) | |
fb2dd970 SM |
171 | (custom-autoload ',varname ,file |
172 | ,(condition-case nil | |
173 | (null (cadr (memq :set form))) | |
174 | (error nil)))))) | |
e8139c11 | 175 | |
1bddeeed SM |
176 | ((eq car 'defgroup) |
177 | ;; In Emacs this is normally handled separately by cus-dep.el, but for | |
178 | ;; third party packages, it can be convenient to explicitly autoload | |
179 | ;; a group. | |
180 | (let ((groupname (nth 1 form))) | |
181 | `(let ((loads (get ',groupname 'custom-loads))) | |
182 | (if (member ',file loads) nil | |
183 | (put ',groupname 'custom-loads (cons ',file loads)))))) | |
184 | ||
e8139c11 SM |
185 | ;; nil here indicates that this is not a special autoload form. |
186 | (t nil)))) | |
0231f2dc | 187 | |
890df022 SM |
188 | ;; Forms which have doc-strings which should be printed specially. |
189 | ;; A doc-string-elt property of ELT says that (nth ELT FORM) is | |
190 | ;; the doc-string in FORM. | |
191 | ;; Those properties are now set in lisp-mode.el. | |
fc89daee | 192 | |
3b979520 SM |
193 | (defun autoload-generated-file () |
194 | (expand-file-name generated-autoload-file | |
438d6bb6 SM |
195 | ;; File-local settings of generated-autoload-file should |
196 | ;; be interpreted relative to the file's location, | |
197 | ;; of course. | |
198 | (if (not (local-variable-p 'generated-autoload-file)) | |
199 | (expand-file-name "lisp" source-directory)))) | |
0231f2dc | 200 | |
72c19d97 | 201 | |
2336b6a9 KH |
202 | (defun autoload-read-section-header () |
203 | "Read a section header form. | |
204 | Since continuation lines have been marked as comments, | |
205 | we must copy the text of the form and remove those comment | |
206 | markers before we call `read'." | |
207 | (save-match-data | |
208 | (let ((beginning (point)) | |
209 | string) | |
210 | (forward-line 1) | |
211 | (while (looking-at generate-autoload-section-continuation) | |
212 | (forward-line 1)) | |
213 | (setq string (buffer-substring beginning (point))) | |
214 | (with-current-buffer (get-buffer-create " *autoload*") | |
215 | (erase-buffer) | |
216 | (insert string) | |
217 | (goto-char (point-min)) | |
218 | (while (search-forward generate-autoload-section-continuation nil t) | |
219 | (replace-match " ")) | |
220 | (goto-char (point-min)) | |
221 | (read (current-buffer)))))) | |
222 | ||
ac644d50 JB |
223 | (defvar autoload-print-form-outbuf nil |
224 | "Buffer which gets the output of `autoload-print-form'.") | |
4a3c5b3a | 225 | |
a8add29d | 226 | (defun autoload-print-form (form) |
4a3c5b3a RS |
227 | "Print FORM such that `make-docfile' will find the docstrings. |
228 | The variable `autoload-print-form-outbuf' specifies the buffer to | |
229 | put the output in." | |
a8add29d SM |
230 | (cond |
231 | ;; If the form is a sequence, recurse. | |
232 | ((eq (car form) 'progn) (mapcar 'autoload-print-form (cdr form))) | |
233 | ;; Symbols at the toplevel are meaningless. | |
234 | ((symbolp form) nil) | |
235 | (t | |
4a3c5b3a RS |
236 | (let ((doc-string-elt (get (car-safe form) 'doc-string-elt)) |
237 | (outbuf autoload-print-form-outbuf)) | |
a8add29d SM |
238 | (if (and doc-string-elt (stringp (nth doc-string-elt form))) |
239 | ;; We need to hack the printing because the | |
240 | ;; doc-string must be printed specially for | |
241 | ;; make-docfile (sigh). | |
242 | (let* ((p (nthcdr (1- doc-string-elt) form)) | |
243 | (elt (cdr p))) | |
244 | (setcdr p nil) | |
245 | (princ "\n(" outbuf) | |
246 | (let ((print-escape-newlines t) | |
9d8563ca | 247 | (print-quoted t) |
a8add29d | 248 | (print-escape-nonascii t)) |
7dab57b6 RS |
249 | (dolist (elt form) |
250 | (prin1 elt outbuf) | |
251 | (princ " " outbuf))) | |
a8add29d SM |
252 | (princ "\"\\\n" outbuf) |
253 | (let ((begin (with-current-buffer outbuf (point)))) | |
254 | (princ (substring (prin1-to-string (car elt)) 1) | |
255 | outbuf) | |
256 | ;; Insert a backslash before each ( that | |
257 | ;; appears at the beginning of a line in | |
258 | ;; the doc string. | |
259 | (with-current-buffer outbuf | |
260 | (save-excursion | |
890df022 | 261 | (while (re-search-backward "\n[[(]" begin t) |
a8add29d SM |
262 | (forward-char 1) |
263 | (insert "\\")))) | |
264 | (if (null (cdr elt)) | |
265 | (princ ")" outbuf) | |
266 | (princ " " outbuf) | |
267 | (princ (substring (prin1-to-string (cdr elt)) 1) | |
268 | outbuf)) | |
269 | (terpri outbuf))) | |
270 | (let ((print-escape-newlines t) | |
9d8563ca | 271 | (print-quoted t) |
a8add29d SM |
272 | (print-escape-nonascii t)) |
273 | (print form outbuf))))))) | |
274 | ||
4ad6a5e7 | 275 | (defun autoload-rubric (file &optional type feature) |
a98f63d4 GM |
276 | "Return a string giving the appropriate autoload rubric for FILE. |
277 | TYPE (default \"autoloads\") is a string stating the type of | |
4ad6a5e7 GM |
278 | information contained in FILE. If FEATURE is non-nil, FILE |
279 | will provide a feature. FEATURE may be a string naming the | |
c70815f1 GM |
280 | feature, otherwise it will be based on FILE's name. |
281 | ||
282 | At present, a feature is in fact always provided, but this should | |
283 | not be relied upon." | |
a98f63d4 GM |
284 | (let ((basename (file-name-nondirectory file))) |
285 | (concat ";;; " basename | |
286 | " --- automatically extracted " (or type "autoloads") "\n" | |
287 | ";;\n" | |
288 | ";;; Code:\n\n" | |
289 | "\f\n" | |
c70815f1 | 290 | ;; This is used outside of autoload.el, eg cus-dep, finder. |
0ad57dfd CY |
291 | "(provide '" |
292 | (if (stringp feature) | |
293 | feature | |
294 | (file-name-sans-extension basename)) | |
295 | ")\n" | |
a98f63d4 GM |
296 | ";; Local Variables:\n" |
297 | ";; version-control: never\n" | |
298 | ";; no-byte-compile: t\n" | |
299 | ";; no-update-autoloads: t\n" | |
e542c600 | 300 | ";; coding: utf-8\n" |
a98f63d4 GM |
301 | ";; End:\n" |
302 | ";;; " basename | |
303 | " ends here\n"))) | |
304 | ||
a273d3e0 GM |
305 | (defun autoload-ensure-default-file (file) |
306 | "Make sure that the autoload file FILE exists and if not create it." | |
307 | (unless (file-exists-p file) | |
a98f63d4 | 308 | (write-region (autoload-rubric file) nil file)) |
a273d3e0 GM |
309 | file) |
310 | ||
311 | (defun autoload-insert-section-header (outbuf autoloads load-name file time) | |
312 | "Insert the section-header line, | |
313 | which lists the file name and which functions are in it, etc." | |
314 | (insert generate-autoload-section-header) | |
1fad2b12 | 315 | (prin1 (list 'autoloads autoloads load-name file time) |
a273d3e0 GM |
316 | outbuf) |
317 | (terpri outbuf) | |
318 | ;; Break that line at spaces, to avoid very long lines. | |
319 | ;; Make each sub-line into a comment. | |
320 | (with-current-buffer outbuf | |
321 | (save-excursion | |
322 | (forward-line -1) | |
323 | (while (not (eolp)) | |
324 | (move-to-column 64) | |
325 | (skip-chars-forward "^ \n") | |
326 | (or (eolp) | |
327 | (insert "\n" generate-autoload-section-continuation)))))) | |
328 | ||
69135525 SM |
329 | (defun autoload-find-file (file) |
330 | "Fetch file and put it in a temp buffer. Return the buffer." | |
331 | ;; It is faster to avoid visiting the file. | |
3b979520 | 332 | (setq file (expand-file-name file)) |
69135525 SM |
333 | (with-current-buffer (get-buffer-create " *autoload-file*") |
334 | (kill-all-local-variables) | |
335 | (erase-buffer) | |
336 | (setq buffer-undo-list t | |
337 | buffer-read-only nil) | |
338 | (emacs-lisp-mode) | |
3b979520 | 339 | (setq default-directory (file-name-directory file)) |
69135525 SM |
340 | (insert-file-contents file nil) |
341 | (let ((enable-local-variables :safe)) | |
342 | (hack-local-variables)) | |
343 | (current-buffer))) | |
344 | ||
b17b8839 SM |
345 | (defvar no-update-autoloads nil |
346 | "File local variable to prevent scanning this file for autoload cookies.") | |
347 | ||
3b979520 | 348 | (defun autoload-file-load-name (file) |
f8ea0098 SM |
349 | "Compute the name that will be used to load FILE." |
350 | ;; OUTFILE should be the name of the global loaddefs.el file, which | |
351 | ;; is expected to be at the root directory of the files we're | |
352 | ;; scanning for autoloads and will be in the `load-path'. | |
353 | (let* ((outfile (default-value 'generated-autoload-file)) | |
354 | (name (file-relative-name file (file-name-directory outfile))) | |
355 | (names '()) | |
356 | (dir (file-name-directory outfile))) | |
357 | ;; If `name' has directory components, only keep the | |
358 | ;; last few that are really needed. | |
359 | (while name | |
360 | (setq name (directory-file-name name)) | |
361 | (push (file-name-nondirectory name) names) | |
362 | (setq name (file-name-directory name))) | |
363 | (while (not name) | |
364 | (cond | |
365 | ((null (cdr names)) (setq name (car names))) | |
366 | ((file-exists-p (expand-file-name "subdirs.el" dir)) | |
367 | ;; FIXME: here we only check the existence of subdirs.el, | |
368 | ;; without checking its content. This makes it generate wrong load | |
369 | ;; names for cases like lisp/term which is not added to load-path. | |
370 | (setq dir (expand-file-name (pop names) dir))) | |
371 | (t (setq name (mapconcat 'identity names "/"))))) | |
3b979520 SM |
372 | (if (string-match "\\.elc?\\(\\.\\|\\'\\)" name) |
373 | (substring name 0 (match-beginning 0)) | |
374 | name))) | |
375 | ||
0231f2dc JB |
376 | (defun generate-file-autoloads (file) |
377 | "Insert at point a loaddefs autoload section for FILE. | |
b17b8839 | 378 | Autoloads are generated for defuns and defmacros in FILE |
da8826b4 | 379 | marked by `generate-autoload-cookie' (which see). |
0231f2dc | 380 | If FILE is being visited in a buffer, the contents of the buffer |
b17b8839 SM |
381 | are used. |
382 | Return non-nil in the case where no autoloads were added at point." | |
0231f2dc | 383 | (interactive "fGenerate autoloads for file: ") |
ceea9b18 SM |
384 | (autoload-generate-file-autoloads file (current-buffer))) |
385 | ||
f8ea0098 SM |
386 | (defvar print-readably) |
387 | ||
438d6bb6 SM |
388 | ;; When called from `generate-file-autoloads' we should ignore |
389 | ;; `generated-autoload-file' altogether. When called from | |
390 | ;; `update-file-autoloads' we don't know `outbuf'. And when called from | |
391 | ;; `update-directory-autoloads' it's in between: we know the default | |
392 | ;; `outbuf' but we should obey any file-local setting of | |
393 | ;; `generated-autoload-file'. | |
394 | (defun autoload-generate-file-autoloads (file &optional outbuf outfile) | |
ceea9b18 SM |
395 | "Insert an autoload section for FILE in the appropriate buffer. |
396 | Autoloads are generated for defuns and defmacros in FILE | |
397 | marked by `generate-autoload-cookie' (which see). | |
398 | If FILE is being visited in a buffer, the contents of the buffer are used. | |
438d6bb6 | 399 | OUTBUF is the buffer in which the autoload statements should be inserted. |
986c5ad5 | 400 | If OUTBUF is nil, it will be determined by `autoload-generated-file'. |
e66466a6 | 401 | |
438d6bb6 SM |
402 | If provided, OUTFILE is expected to be the file name of OUTBUF. |
403 | If OUTFILE is non-nil and FILE specifies a `generated-autoload-file' | |
404 | different from OUTFILE, then OUTBUF is ignored. | |
405 | ||
1603d855 | 406 | Return non-nil if and only if FILE adds no autoloads to OUTFILE |
438d6bb6 | 407 | \(or OUTBUF if OUTFILE is nil)." |
1fad2b12 SM |
408 | (catch 'done |
409 | (let ((autoloads-done '()) | |
15120dec | 410 | load-name |
1fad2b12 | 411 | (print-length nil) |
a113b3ca | 412 | (print-level nil) |
1fad2b12 SM |
413 | (print-readably t) ; This does something in Lucid Emacs. |
414 | (float-output-format nil) | |
415 | (visited (get-file-buffer file)) | |
438d6bb6 | 416 | (otherbuf nil) |
1fad2b12 | 417 | (absfile (expand-file-name file)) |
1fad2b12 | 418 | ;; nil until we found a cookie. |
f8ea0098 | 419 | output-start ostart) |
1fad2b12 SM |
420 | (with-current-buffer (or visited |
421 | ;; It is faster to avoid visiting the file. | |
422 | (autoload-find-file file)) | |
423 | ;; Obey the no-update-autoloads file local variable. | |
424 | (unless no-update-autoloads | |
425 | (message "Generating autoloads for %s..." file) | |
15120dec CY |
426 | (setq load-name |
427 | (if (stringp generated-autoload-load-name) | |
428 | generated-autoload-load-name | |
f8ea0098 SM |
429 | (autoload-file-load-name absfile))) |
430 | (when (and outfile | |
431 | (not (equal outfile (autoload-generated-file)))) | |
432 | (setq otherbuf t)) | |
1fad2b12 SM |
433 | (save-excursion |
434 | (save-restriction | |
435 | (widen) | |
436 | (goto-char (point-min)) | |
437 | (while (not (eobp)) | |
438 | (skip-chars-forward " \t\n\f") | |
439 | (cond | |
440 | ((looking-at (regexp-quote generate-autoload-cookie)) | |
441 | ;; If not done yet, figure out where to insert this text. | |
442 | (unless output-start | |
f8ea0098 SM |
443 | (let ((outbuf |
444 | (or (if otherbuf | |
445 | ;; A file-local setting of | |
446 | ;; autoload-generated-file says we | |
447 | ;; should ignore OUTBUF. | |
448 | nil | |
449 | outbuf) | |
450 | (autoload-find-destination absfile load-name) | |
451 | ;; The file has autoload cookies, but they're | |
452 | ;; already up-to-date. If OUTFILE is nil, the | |
453 | ;; entries are in the expected OUTBUF, | |
454 | ;; otherwise they're elsewhere. | |
455 | (throw 'done otherbuf)))) | |
456 | (with-current-buffer outbuf | |
457 | (setq output-start (point-marker) | |
458 | ostart (point))))) | |
1fad2b12 SM |
459 | (search-forward generate-autoload-cookie) |
460 | (skip-chars-forward " \t") | |
461 | (if (eolp) | |
462 | (condition-case err | |
463 | ;; Read the next form and make an autoload. | |
464 | (let* ((form (prog1 (read (current-buffer)) | |
465 | (or (bolp) (forward-line 1)))) | |
466 | (autoload (make-autoload form load-name))) | |
467 | (if autoload | |
468 | (push (nth 1 form) autoloads-done) | |
469 | (setq autoload form)) | |
f8ea0098 SM |
470 | (let ((autoload-print-form-outbuf |
471 | (marker-buffer output-start))) | |
1fad2b12 SM |
472 | (autoload-print-form autoload))) |
473 | (error | |
ba83908c SM |
474 | (message "Autoload cookie error in %s:%s %S" |
475 | file (count-lines (point-min) (point)) err))) | |
1fad2b12 SM |
476 | |
477 | ;; Copy the rest of the line to the output. | |
478 | (princ (buffer-substring | |
479 | (progn | |
480 | ;; Back up over whitespace, to preserve it. | |
481 | (skip-chars-backward " \f\t") | |
482 | (if (= (char-after (1+ (point))) ? ) | |
483 | ;; Eat one space. | |
484 | (forward-char 1)) | |
485 | (point)) | |
486 | (progn (forward-line 1) (point))) | |
f8ea0098 | 487 | (marker-buffer output-start)))) |
1fad2b12 SM |
488 | ((looking-at ";") |
489 | ;; Don't read the comment. | |
490 | (forward-line 1)) | |
491 | (t | |
492 | (forward-sexp 1) | |
493 | (forward-line 1)))))) | |
494 | ||
495 | (when output-start | |
0b7750a9 SM |
496 | (let ((secondary-autoloads-file-buf |
497 | (if (local-variable-p 'generated-autoload-file) | |
498 | (current-buffer)))) | |
f8ea0098 | 499 | (with-current-buffer (marker-buffer output-start) |
0b7750a9 SM |
500 | (save-excursion |
501 | ;; Insert the section-header line which lists the file name | |
502 | ;; and which functions are in it, etc. | |
f8ea0098 | 503 | (assert (= ostart output-start)) |
0b7750a9 | 504 | (goto-char output-start) |
f8ea0098 SM |
505 | (let ((relfile (file-relative-name absfile))) |
506 | (autoload-insert-section-header | |
507 | (marker-buffer output-start) | |
508 | autoloads-done load-name relfile | |
509 | (if secondary-autoloads-file-buf | |
510 | ;; MD5 checksums are much better because they do not | |
511 | ;; change unless the file changes (so they'll be | |
512 | ;; equal on two different systems and will change | |
513 | ;; less often than time-stamps, thus leading to fewer | |
514 | ;; unneeded changes causing spurious conflicts), but | |
515 | ;; using time-stamps is a very useful optimization, | |
516 | ;; so we use time-stamps for the main autoloads file | |
517 | ;; (loaddefs.el) where we have special ways to | |
518 | ;; circumvent the "random change problem", and MD5 | |
519 | ;; checksum in secondary autoload files where we do | |
520 | ;; not need the time-stamp optimization because it is | |
521 | ;; already provided by the primary autoloads file. | |
522 | (md5 secondary-autoloads-file-buf | |
523 | ;; We'd really want to just use | |
524 | ;; `emacs-internal' instead. | |
525 | nil nil 'emacs-mule-unix) | |
526 | (nth 5 (file-attributes relfile)))) | |
527 | (insert ";;; Generated autoloads from " relfile "\n"))) | |
0b7750a9 | 528 | (insert generate-autoload-section-trailer)))) |
1fad2b12 SM |
529 | (message "Generating autoloads for %s...done" file)) |
530 | (or visited | |
531 | ;; We created this buffer, so we should kill it. | |
1c67aeaa | 532 | (kill-buffer (current-buffer)))) |
f8ea0098 SM |
533 | (or (not output-start) |
534 | ;; If the entries were added to some other buffer, then the file | |
535 | ;; doesn't add entries to OUTFILE. | |
536 | otherbuf)))) | |
e2b6138f | 537 | \f |
e66466a6 SM |
538 | (defun autoload-save-buffers () |
539 | (while autoload-modified-buffers | |
540 | (with-current-buffer (pop autoload-modified-buffers) | |
2d8a57ef GM |
541 | (let ((version-control 'never)) |
542 | (save-buffer))))) | |
e66466a6 | 543 | |
0231f2dc | 544 | ;;;###autoload |
f7ed02ac | 545 | (defun update-file-autoloads (file &optional save-after) |
0231f2dc | 546 | "Update the autoloads for FILE in `generated-autoload-file' |
a273d3e0 | 547 | \(which FILE might bind in its local variables). |
f7ed02ac RS |
548 | If SAVE-AFTER is non-nil (which is always, when called interactively), |
549 | save the buffer too. | |
550 | ||
551 | Return FILE if there was no autoload cookie in it, else nil." | |
552 | (interactive "fUpdate autoloads for file: \np") | |
1fad2b12 SM |
553 | (let* ((autoload-modified-buffers nil) |
554 | (no-autoloads (autoload-generate-file-autoloads file))) | |
555 | (if autoload-modified-buffers | |
986c5ad5 | 556 | (if save-after (autoload-save-buffers)) |
32226619 | 557 | (if (called-interactively-p 'interactive) |
986c5ad5 | 558 | (message "Autoload section for %s is up to date." file))) |
e66466a6 | 559 | (if no-autoloads file))) |
57536a83 | 560 | |
f8ea0098 | 561 | (defun autoload-find-destination (file load-name) |
57536a83 SM |
562 | "Find the destination point of the current buffer's autoloads. |
563 | FILE is the file name of the current buffer. | |
564 | Returns a buffer whose point is placed at the requested location. | |
1fad2b12 | 565 | Returns nil if the file's autoloads are uptodate, otherwise |
0b7750a9 | 566 | removes any prior now out-of-date autoload entries." |
1fad2b12 | 567 | (catch 'up-to-date |
f8ea0098 | 568 | (let* ((buf (current-buffer)) |
0b7750a9 SM |
569 | (existing-buffer (if buffer-file-name buf)) |
570 | (found nil)) | |
1fad2b12 | 571 | (with-current-buffer |
812e2bd8 SM |
572 | ;; We used to use `raw-text' to read this file, but this causes |
573 | ;; problems when the file contains non-ASCII characters. | |
2d8a57ef GM |
574 | (let ((enable-local-variables :safe)) |
575 | (find-file-noselect | |
576 | (autoload-ensure-default-file (autoload-generated-file)))) | |
1fad2b12 SM |
577 | ;; This is to make generated-autoload-file have Unix EOLs, so |
578 | ;; that it is portable to all platforms. | |
4c0eb0d3 GM |
579 | (or (eq 0 (coding-system-eol-type buffer-file-coding-system)) |
580 | (set-buffer-file-coding-system 'unix)) | |
1fad2b12 | 581 | (or (> (buffer-size) 0) |
f8ea0098 | 582 | (error "Autoloads file %s lacks boilerplate" buffer-file-name)) |
1fad2b12 SM |
583 | (or (file-writable-p buffer-file-name) |
584 | (error "Autoloads file %s is not writable" buffer-file-name)) | |
585 | (widen) | |
586 | (goto-char (point-min)) | |
587 | ;; Look for the section for LOAD-NAME. | |
588 | (while (and (not found) | |
589 | (search-forward generate-autoload-section-header nil t)) | |
590 | (let ((form (autoload-read-section-header))) | |
591 | (cond ((string= (nth 2 form) load-name) | |
592 | ;; We found the section for this file. | |
593 | ;; Check if it is up to date. | |
594 | (let ((begin (match-beginning 0)) | |
595 | (last-time (nth 4 form)) | |
596 | (file-time (nth 5 (file-attributes file)))) | |
597 | (if (and (or (null existing-buffer) | |
598 | (not (buffer-modified-p existing-buffer))) | |
0b7750a9 SM |
599 | (or |
600 | ;; last-time is the time-stamp (specifying | |
601 | ;; the last time we looked at the file) and | |
602 | ;; the file hasn't been changed since. | |
603 | (and (listp last-time) (= (length last-time) 2) | |
604 | (not (time-less-p last-time file-time))) | |
605 | ;; last-time is an MD5 checksum instead. | |
606 | (and (stringp last-time) | |
607 | (equal last-time | |
608 | (md5 buf nil nil 'emacs-mule))))) | |
1fad2b12 SM |
609 | (throw 'up-to-date nil) |
610 | (autoload-remove-section begin) | |
611 | (setq found t)))) | |
612 | ((string< load-name (nth 2 form)) | |
613 | ;; We've come to a section alphabetically later than | |
614 | ;; LOAD-NAME. We assume the file is in order and so | |
615 | ;; there must be no section for LOAD-NAME. We will | |
616 | ;; insert one before the section here. | |
617 | (goto-char (match-beginning 0)) | |
618 | (setq found t))))) | |
619 | (or found | |
620 | (progn | |
621 | ;; No later sections in the file. Put before the last page. | |
622 | (goto-char (point-max)) | |
623 | (search-backward "\f" nil t))) | |
624 | (unless (memq (current-buffer) autoload-modified-buffers) | |
625 | (push (current-buffer) autoload-modified-buffers)) | |
626 | (current-buffer))))) | |
a273d3e0 | 627 | |
a273d3e0 GM |
628 | (defun autoload-remove-section (begin) |
629 | (goto-char begin) | |
630 | (search-forward generate-autoload-section-trailer) | |
631 | (delete-region begin (point))) | |
0231f2dc JB |
632 | |
633 | ;;;###autoload | |
56eebc29 | 634 | (defun update-directory-autoloads (&rest dirs) |
d08589bf | 635 | "\ |
3fbca58a | 636 | Update loaddefs.el with all the current autoloads from DIRS, and no old ones. |
ac644d50 | 637 | This uses `update-file-autoloads' (which see) to do its work. |
56eebc29 RS |
638 | In an interactive call, you must give one argument, the name |
639 | of a single directory. In a call from Lisp, you can supply multiple | |
640 | directories as separate arguments, but this usage is discouraged. | |
641 | ||
642 | The function does NOT recursively descend into subdirectories of the | |
643 | directory or directories specified." | |
b59c7256 | 644 | (interactive "DUpdate autoloads from directory: ") |
a85e4d58 | 645 | (let* ((files-re (let ((tmp nil)) |
de10856c | 646 | (dolist (suf (get-load-suffixes) |
a85e4d58 SM |
647 | (concat "^[^=.].*" (regexp-opt tmp t) "\\'")) |
648 | (unless (string-match "\\.elc" suf) (push suf tmp))))) | |
649 | (files (apply 'nconc | |
a273d3e0 GM |
650 | (mapcar (lambda (dir) |
651 | (directory-files (expand-file-name dir) | |
a85e4d58 | 652 | t files-re)) |
a273d3e0 | 653 | dirs))) |
48df920c | 654 | (done ()) |
a273d3e0 | 655 | (this-time (current-time)) |
438d6bb6 SM |
656 | ;; Files with no autoload cookies or whose autoloads go to other |
657 | ;; files because of file-local autoload-generated-file settings. | |
658 | (no-autoloads nil) | |
659 | (autoload-modified-buffers nil)) | |
a273d3e0 GM |
660 | |
661 | (with-current-buffer | |
2d8a57ef GM |
662 | (let ((enable-local-variables :safe)) |
663 | (find-file-noselect | |
664 | (autoload-ensure-default-file (autoload-generated-file)))) | |
e2b6138f | 665 | (save-excursion |
a273d3e0 GM |
666 | |
667 | ;; Canonicalize file names and remove the autoload file itself. | |
1fad2b12 SM |
668 | (setq files (delete (file-relative-name buffer-file-name) |
669 | (mapcar 'file-relative-name files))) | |
a273d3e0 | 670 | |
b59c7256 RM |
671 | (goto-char (point-min)) |
672 | (while (search-forward generate-autoload-section-header nil t) | |
2336b6a9 | 673 | (let* ((form (autoload-read-section-header)) |
b59c7256 | 674 | (file (nth 3 form))) |
a273d3e0 GM |
675 | (cond ((and (consp file) (stringp (car file))) |
676 | ;; This is a list of files that have no autoload cookies. | |
677 | ;; There shouldn't be more than one such entry. | |
678 | ;; Remove the obsolete section. | |
679 | (autoload-remove-section (match-beginning 0)) | |
680 | (let ((last-time (nth 4 form))) | |
681 | (dolist (file file) | |
682 | (let ((file-time (nth 5 (file-attributes file)))) | |
683 | (when (and file-time | |
66dc9a0f | 684 | (not (time-less-p last-time file-time))) |
a273d3e0 GM |
685 | ;; file unchanged |
686 | (push file no-autoloads) | |
687 | (setq files (delete file files))))))) | |
688 | ((not (stringp file))) | |
48df920c SM |
689 | ((or (not (file-exists-p file)) |
690 | ;; Remove duplicates as well, just in case. | |
1c67aeaa SM |
691 | (member file done) |
692 | ;; If the file is actually excluded. | |
693 | (member (expand-file-name file) autoload-excludes)) | |
48df920c | 694 | ;; Remove the obsolete section. |
a273d3e0 | 695 | (autoload-remove-section (match-beginning 0))) |
0b7750a9 SM |
696 | ((not (time-less-p (nth 4 form) |
697 | (nth 5 (file-attributes file)))) | |
a273d3e0 GM |
698 | ;; File hasn't changed. |
699 | nil) | |
b59c7256 | 700 | (t |
438d6bb6 SM |
701 | (autoload-remove-section (match-beginning 0)) |
702 | (if (autoload-generate-file-autoloads | |
f8ea0098 | 703 | ;; Passing `current-buffer' makes it insert at point. |
438d6bb6 SM |
704 | file (current-buffer) buffer-file-name) |
705 | (push file no-autoloads)))) | |
48df920c | 706 | (push file done) |
b59c7256 | 707 | (setq files (delete file files))))) |
a273d3e0 | 708 | ;; Elements remaining in FILES have no existing autoload sections yet. |
438d6bb6 | 709 | (dolist (file files) |
1c67aeaa SM |
710 | (cond |
711 | ((member (expand-file-name file) autoload-excludes) nil) | |
f8ea0098 SM |
712 | ;; Passing nil as second argument forces |
713 | ;; autoload-generate-file-autoloads to look for the right | |
714 | ;; spot where to insert each autoloads section. | |
1c67aeaa SM |
715 | ((autoload-generate-file-autoloads file nil buffer-file-name) |
716 | (push file no-autoloads)))) | |
438d6bb6 | 717 | |
a273d3e0 | 718 | (when no-autoloads |
000d9923 MR |
719 | ;; Sort them for better readability. |
720 | (setq no-autoloads (sort no-autoloads 'string<)) | |
a273d3e0 GM |
721 | ;; Add the `no-autoloads' section. |
722 | (goto-char (point-max)) | |
723 | (search-backward "\f" nil t) | |
724 | (autoload-insert-section-header | |
725 | (current-buffer) nil nil no-autoloads this-time) | |
726 | (insert generate-autoload-section-trailer)) | |
727 | ||
2d8a57ef GM |
728 | (let ((version-control 'never)) |
729 | (save-buffer)) | |
438d6bb6 SM |
730 | ;; In case autoload entries were added to other files because of |
731 | ;; file-local autoload-generated-file settings. | |
732 | (autoload-save-buffers)))) | |
0231f2dc | 733 | |
1e0888f5 LH |
734 | (define-obsolete-function-alias 'update-autoloads-from-directories |
735 | 'update-directory-autoloads "22.1") | |
736 | ||
f8641890 GM |
737 | (defvar autoload-make-program (or (getenv "MAKE") "make") |
738 | "Name of the make program in use during the Emacs build process.") | |
739 | ||
0231f2dc JB |
740 | ;;;###autoload |
741 | (defun batch-update-autoloads () | |
b59c7256 | 742 | "Update loaddefs.el autoloads in batch mode. |
375d5635 | 743 | Calls `update-directory-autoloads' on the command line arguments." |
5152da64 GM |
744 | ;; For use during the Emacs build process only. |
745 | (unless autoload-excludes | |
746 | (let* ((ldir (file-name-directory generated-autoload-file)) | |
f8641890 GM |
747 | (default-directory |
748 | (file-name-as-directory | |
749 | (expand-file-name (if (eq system-type 'windows-nt) | |
750 | "../lib-src" | |
751 | "../src") ldir))) | |
752 | (mfile "Makefile") | |
753 | (tmpfile "echolisp.tmp") | |
5152da64 | 754 | lim) |
f8641890 GM |
755 | ;; Windows uses the 'echolisp' approach because: |
756 | ;; i) It does not have $lisp as a single simple definition, so | |
757 | ;; it would be harder to parse the Makefile. | |
758 | ;; ii) It can, since it already has $lisp broken up into pieces | |
759 | ;; that the command-line can handle. | |
760 | ;; Non-Windows builds do not use the 'echolisp' approach because | |
761 | ;; no-one knows (?) the maximum safe command-line length on all | |
762 | ;; supported systems. $lisp is much longer there since it uses | |
763 | ;; absolute paths, and it would seem a shame to split it just for this. | |
5152da64 | 764 | (when (file-readable-p mfile) |
f8641890 GM |
765 | (if (eq system-type 'windows-nt) |
766 | (when (ignore-errors | |
767 | (if (file-exists-p tmpfile) (delete-file tmpfile)) | |
768 | ;; FIXME call-process is better, if it works. | |
769 | (shell-command (format "%s echolisp > %s" | |
770 | autoload-make-program tmpfile)) | |
771 | (file-readable-p tmpfile)) | |
772 | (with-temp-buffer | |
773 | (insert-file-contents tmpfile) | |
774 | ;; FIXME could be a single while loop. | |
775 | (while (not (eobp)) | |
776 | (setq lim (line-end-position)) | |
777 | (while (re-search-forward "\\([^ ]+\\.el\\)c?\\>" lim t) | |
778 | (push (expand-file-name (match-string 1)) | |
779 | autoload-excludes)) | |
780 | (forward-line 1)))) | |
781 | (with-temp-buffer | |
782 | (insert-file-contents mfile) | |
3616e0b9 | 783 | (when (re-search-forward "^shortlisp= " nil t) |
69b55131 GM |
784 | (while (and (not lim) |
785 | (re-search-forward "\\.\\./lisp/\\([^ ]+\\.el\\)c?\\>" | |
786 | nil t)) | |
f8641890 | 787 | (push (expand-file-name (match-string 1) ldir) |
69b55131 GM |
788 | autoload-excludes) |
789 | (skip-chars-forward " \t") | |
790 | (if (eolp) (setq lim t))))))))) | |
3a4336e6 MB |
791 | (let ((args command-line-args-left)) |
792 | (setq command-line-args-left nil) | |
793 | (apply 'update-directory-autoloads args))) | |
0231f2dc JB |
794 | |
795 | (provide 'autoload) | |
ffd56f97 | 796 | |
c0274f38 | 797 | ;;; autoload.el ends here |