]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/autoload.el
Prevent bootstrap autoload backup files
[gnu-emacs] / lisp / emacs-lisp / autoload.el
1 ;; autoload.el --- maintain autoloads in loaddefs.el -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1991-1997, 2001-2016 Free Software Foundation, Inc.
4
5 ;; Author: Roland McGrath <roland@gnu.org>
6 ;; Keywords: maint
7 ;; Package: emacs
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
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
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This code helps GNU Emacs maintainers keep the loaddefs.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
31 ;;; Code:
32
33 (require 'lisp-mode) ;for `doc-string-elt' properties.
34 (require 'lisp-mnt)
35 (eval-when-compile (require 'cl-lib))
36
37 (defvar generated-autoload-file nil
38 "File into which to write autoload definitions.
39 A Lisp file can set this in its local variables section to make
40 its autoloads go somewhere else.
41
42 If this is a relative file name, the directory is determined as
43 follows:
44 - If a Lisp file defined `generated-autoload-file' as a
45 file-local variable, use its containing directory.
46 - Otherwise use the \"lisp\" subdirectory of `source-directory'.
47
48 The autoload file is assumed to contain a trailer starting with a
49 FormFeed character.")
50 ;;;###autoload
51 (put 'generated-autoload-file 'safe-local-variable 'stringp)
52
53 (defvar generated-autoload-load-name nil
54 "Load name for `autoload' statements generated from autoload cookies.
55 If nil, this defaults to the file name, sans extension.
56 Typically, you need to set this when the directory containing the file
57 is not in `load-path'.
58 This also affects the generated cus-load.el file.")
59 ;;;###autoload
60 (put 'generated-autoload-load-name 'safe-local-variable 'stringp)
61
62 ;; This feels like it should be a defconst, but MH-E sets it to
63 ;; ";;;###mh-autoload" for the autoloads that are to go into mh-loaddefs.el.
64 (defvar generate-autoload-cookie ";;;###autoload"
65 "Magic comment indicating the following form should be autoloaded.
66 Used by \\[update-file-autoloads]. This string should be
67 meaningless to Lisp (e.g., a comment).
68
69 This string is used:
70
71 \;;;###autoload
72 \(defun function-to-be-autoloaded () ...)
73
74 If this string appears alone on a line, the following form will be
75 read and an autoload made for it. If there is further text on the line,
76 that text will be copied verbatim to `generated-autoload-file'.")
77
78 (defvar autoload-excludes nil
79 "If non-nil, list of absolute file names not to scan for autoloads.")
80
81 (defconst generate-autoload-section-header "\f\n;;;### "
82 "String that marks the form at the start of a new file's autoload section.")
83
84 (defconst generate-autoload-section-trailer "\n;;;***\n"
85 "String which indicates the end of the section of autoloads for a file.")
86
87 (defconst generate-autoload-section-continuation ";;;;;; "
88 "String to add on each continuation of the section header form.")
89
90 (defvar autoload-modified-buffers) ;Dynamically scoped var.
91
92 (defun make-autoload (form file &optional expansion)
93 "Turn FORM into an autoload or defvar for source file FILE.
94 Returns nil if FORM is not a special autoload form (i.e. a function definition
95 or macro definition or a defcustom).
96 If EXPANSION is non-nil, we're processing the macro expansion of an
97 expression, in which case we want to handle forms differently."
98 (let ((car (car-safe form)) expand)
99 (cond
100 ((and expansion (eq car 'defalias))
101 (pcase-let*
102 ((`(,_ ,_ ,arg . ,rest) form)
103 ;; `type' is non-nil if it defines a macro.
104 ;; `fun' is the function part of `arg' (defaults to `arg').
105 ((or (and (or `(cons 'macro ,fun) `'(macro . ,fun)) (let type t))
106 (and (let fun arg) (let type nil)))
107 arg)
108 ;; `lam' is the lambda expression in `fun' (or nil if not
109 ;; recognized).
110 (lam (if (memq (car-safe fun) '(quote function)) (cadr fun)))
111 ;; `args' is the list of arguments (or t if not recognized).
112 ;; `body' is the body of `lam' (or t if not recognized).
113 ((or `(lambda ,args . ,body)
114 (and (let args t) (let body t)))
115 lam)
116 ;; Get the `doc' from `body' or `rest'.
117 (doc (cond ((stringp (car-safe body)) (car body))
118 ((stringp (car-safe rest)) (car rest))))
119 ;; Look for an interactive spec.
120 (interactive (pcase body
121 ((or `((interactive . ,_) . ,_)
122 `(,_ (interactive . ,_) . ,_))
123 t))))
124 ;; Add the usage form at the end where describe-function-1
125 ;; can recover it.
126 (when (listp args) (setq doc (help-add-fundoc-usage doc args)))
127 ;; (message "autoload of %S" (nth 1 form))
128 `(autoload ,(nth 1 form) ,file ,doc ,interactive ,type)))
129
130 ((and expansion (memq car '(progn prog1)))
131 (let ((end (memq :autoload-end form)))
132 (when end ;Cut-off anything after the :autoload-end marker.
133 (setq form (copy-sequence form))
134 (setcdr (memq :autoload-end form) nil))
135 (let ((exps (delq nil (mapcar (lambda (form)
136 (make-autoload form file expansion))
137 (cdr form)))))
138 (when exps (cons 'progn exps)))))
139
140 ;; For complex cases, try again on the macro-expansion.
141 ((and (memq car '(easy-mmode-define-global-mode define-global-minor-mode
142 define-globalized-minor-mode defun defmacro
143 easy-mmode-define-minor-mode define-minor-mode
144 define-inline cl-defun cl-defmacro))
145 (macrop car)
146 (setq expand (let ((load-file-name file)) (macroexpand form)))
147 (memq (car expand) '(progn prog1 defalias)))
148 (make-autoload expand file 'expansion)) ;Recurse on the expansion.
149
150 ;; For special function-like operators, use the `autoload' function.
151 ((memq car '(define-skeleton define-derived-mode
152 define-compilation-mode define-generic-mode
153 easy-mmode-define-global-mode define-global-minor-mode
154 define-globalized-minor-mode
155 easy-mmode-define-minor-mode define-minor-mode
156 cl-defun defun* cl-defmacro defmacro*
157 define-overloadable-function))
158 (let* ((macrop (memq car '(defmacro cl-defmacro defmacro*)))
159 (name (nth 1 form))
160 (args (pcase car
161 ((or `defun `defmacro
162 `defun* `defmacro* `cl-defun `cl-defmacro
163 `define-overloadable-function) (nth 2 form))
164 (`define-skeleton '(&optional str arg))
165 ((or `define-generic-mode `define-derived-mode
166 `define-compilation-mode) nil)
167 (_ t)))
168 (body (nthcdr (or (function-get car 'doc-string-elt) 3) form))
169 (doc (if (stringp (car body)) (pop body))))
170 ;; Add the usage form at the end where describe-function-1
171 ;; can recover it.
172 (when (listp args) (setq doc (help-add-fundoc-usage doc args)))
173 ;; `define-generic-mode' quotes the name, so take care of that
174 `(autoload ,(if (listp name) name (list 'quote name))
175 ,file ,doc
176 ,(or (and (memq car '(define-skeleton define-derived-mode
177 define-generic-mode
178 easy-mmode-define-global-mode
179 define-global-minor-mode
180 define-globalized-minor-mode
181 easy-mmode-define-minor-mode
182 define-minor-mode)) t)
183 (eq (car-safe (car body)) 'interactive))
184 ,(if macrop ''macro nil))))
185
186 ;; For defclass forms, use `eieio-defclass-autoload'.
187 ((eq car 'defclass)
188 (let ((name (nth 1 form))
189 (superclasses (nth 2 form))
190 (doc (nth 4 form)))
191 (list 'eieio-defclass-autoload (list 'quote name)
192 (list 'quote superclasses) file doc)))
193
194 ;; Convert defcustom to less space-consuming data.
195 ((eq car 'defcustom)
196 (let ((varname (car-safe (cdr-safe form)))
197 (init (car-safe (cdr-safe (cdr-safe form))))
198 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form)))))
199 ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
200 )
201 `(progn
202 (defvar ,varname ,init ,doc)
203 (custom-autoload ',varname ,file
204 ,(condition-case nil
205 (null (cadr (memq :set form)))
206 (error nil))))))
207
208 ((eq car 'defgroup)
209 ;; In Emacs this is normally handled separately by cus-dep.el, but for
210 ;; third party packages, it can be convenient to explicitly autoload
211 ;; a group.
212 (let ((groupname (nth 1 form)))
213 `(let ((loads (get ',groupname 'custom-loads)))
214 (if (member ',file loads) nil
215 (put ',groupname 'custom-loads (cons ',file loads))))))
216
217 ;; When processing a macro expansion, any expression
218 ;; before a :autoload-end should be included. These are typically (put
219 ;; 'fun 'prop val) and things like that.
220 ((and expansion (consp form)) form)
221
222 ;; nil here indicates that this is not a special autoload form.
223 (t nil))))
224
225 ;; Forms which have doc-strings which should be printed specially.
226 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
227 ;; the doc-string in FORM.
228 ;; Those properties are now set in lisp-mode.el.
229
230 (defun autoload-find-generated-file ()
231 "Visit the autoload file for the current buffer, and return its buffer.
232 If a buffer is visiting the desired autoload file, return it."
233 (let ((enable-local-variables :safe)
234 (enable-local-eval nil))
235 ;; We used to use `raw-text' to read this file, but this causes
236 ;; problems when the file contains non-ASCII characters.
237 (let* ((delay-mode-hooks t)
238 (file (autoload-generated-file))
239 (file-missing (not (file-exists-p file))))
240 (when file-missing
241 (autoload-ensure-default-file file))
242 (with-current-buffer
243 (find-file-noselect
244 (autoload-ensure-file-writeable
245 file))
246 ;; block backups when the file has just been created, since
247 ;; the backups will just be the auto-generated headers.
248 ;; bug#23203
249 (when file-missing
250 (setq buffer-backed-up t)
251 (save-buffer))
252 (current-buffer)))))
253
254 (defun autoload-generated-file ()
255 (expand-file-name generated-autoload-file
256 ;; File-local settings of generated-autoload-file should
257 ;; be interpreted relative to the file's location,
258 ;; of course.
259 (if (not (local-variable-p 'generated-autoload-file))
260 (expand-file-name "lisp" source-directory))))
261
262
263 (defun autoload-read-section-header ()
264 "Read a section header form.
265 Since continuation lines have been marked as comments,
266 we must copy the text of the form and remove those comment
267 markers before we call `read'."
268 (save-match-data
269 (let ((beginning (point))
270 string)
271 (forward-line 1)
272 (while (looking-at generate-autoload-section-continuation)
273 (forward-line 1))
274 (setq string (buffer-substring beginning (point)))
275 (with-current-buffer (get-buffer-create " *autoload*")
276 (erase-buffer)
277 (insert string)
278 (goto-char (point-min))
279 (while (search-forward generate-autoload-section-continuation nil t)
280 (replace-match " "))
281 (goto-char (point-min))
282 (read (current-buffer))))))
283
284 (defvar autoload-print-form-outbuf nil
285 "Buffer which gets the output of `autoload-print-form'.")
286
287 (defun autoload-print-form (form)
288 "Print FORM such that `make-docfile' will find the docstrings.
289 The variable `autoload-print-form-outbuf' specifies the buffer to
290 put the output in."
291 (cond
292 ;; If the form is a sequence, recurse.
293 ((eq (car form) 'progn) (mapcar 'autoload-print-form (cdr form)))
294 ;; Symbols at the toplevel are meaningless.
295 ((symbolp form) nil)
296 (t
297 (let ((doc-string-elt (function-get (car-safe form) 'doc-string-elt))
298 (outbuf autoload-print-form-outbuf))
299 (if (and doc-string-elt (stringp (nth doc-string-elt form)))
300 ;; We need to hack the printing because the
301 ;; doc-string must be printed specially for
302 ;; make-docfile (sigh).
303 (let* ((p (nthcdr (1- doc-string-elt) form))
304 (elt (cdr p)))
305 (setcdr p nil)
306 (princ "\n(" outbuf)
307 (let ((print-escape-newlines t)
308 (print-quoted t)
309 (print-escape-nonascii t))
310 (dolist (elt form)
311 (prin1 elt outbuf)
312 (princ " " outbuf)))
313 (princ "\"\\\n" outbuf)
314 (let ((begin (with-current-buffer outbuf (point))))
315 (princ (substring (prin1-to-string (car elt)) 1)
316 outbuf)
317 ;; Insert a backslash before each ( that
318 ;; appears at the beginning of a line in
319 ;; the doc string.
320 (with-current-buffer outbuf
321 (save-excursion
322 (while (re-search-backward "\n[[(]" begin t)
323 (forward-char 1)
324 (insert "\\"))))
325 (if (null (cdr elt))
326 (princ ")" outbuf)
327 (princ " " outbuf)
328 (princ (substring (prin1-to-string (cdr elt)) 1)
329 outbuf))
330 (terpri outbuf)))
331 (let ((print-escape-newlines t)
332 (print-quoted t)
333 (print-escape-nonascii t))
334 (print form outbuf)))))))
335
336 (defun autoload-rubric (file &optional type feature)
337 "Return a string giving the appropriate autoload rubric for FILE.
338 TYPE (default \"autoloads\") is a string stating the type of
339 information contained in FILE. If FEATURE is non-nil, FILE
340 will provide a feature. FEATURE may be a string naming the
341 feature, otherwise it will be based on FILE's name.
342
343 At present, a feature is in fact always provided, but this should
344 not be relied upon."
345 (let ((basename (file-name-nondirectory file)))
346 (concat ";;; " basename
347 " --- automatically extracted " (or type "autoloads") "\n"
348 ";;\n"
349 ";;; Code:\n\n"
350 "\f\n"
351 ;; This is used outside of autoload.el, eg cus-dep, finder.
352 "(provide '"
353 (if (stringp feature)
354 feature
355 (file-name-sans-extension basename))
356 ")\n"
357 ";; Local Variables:\n"
358 ";; version-control: never\n"
359 ";; no-byte-compile: t\n"
360 ";; no-update-autoloads: t\n"
361 ";; coding: utf-8\n"
362 ";; End:\n"
363 ";;; " basename
364 " ends here\n")))
365
366 (defvar autoload-ensure-writable nil
367 "Non-nil means `autoload-ensure-default-file' makes existing file writable.")
368 ;; Just in case someone tries to get you to overwrite a file that you
369 ;; don't want to.
370 ;;;###autoload
371 (put 'autoload-ensure-writable 'risky-local-variable t)
372
373 (defun autoload-ensure-file-writeable (file)
374 ;; Probably pointless, but replaces the old AUTOGEN_VCS in lisp/Makefile,
375 ;; which was designed to handle CVSREAD=1 and equivalent.
376 (and autoload-ensure-writable
377 (let ((modes (file-modes file)))
378 (if (zerop (logand modes #o0200))
379 ;; Ignore any errors here, and let subsequent attempts
380 ;; to write the file raise any real error.
381 (ignore-errors (set-file-modes file (logior modes #o0200))))))
382 file)
383
384 (defun autoload-ensure-default-file (file)
385 "Make sure that the autoload file FILE exists, creating it if needed.
386 If the file already exists and `autoload-ensure-writable' is non-nil,
387 make it writable."
388 (write-region (autoload-rubric file) nil file))
389
390 (defun autoload-insert-section-header (outbuf autoloads load-name file time)
391 "Insert the section-header line,
392 which lists the file name and which functions are in it, etc."
393 (insert generate-autoload-section-header)
394 (prin1 `(autoloads ,autoloads ,load-name ,file ,time)
395 outbuf)
396 (terpri outbuf)
397 ;; Break that line at spaces, to avoid very long lines.
398 ;; Make each sub-line into a comment.
399 (with-current-buffer outbuf
400 (save-excursion
401 (forward-line -1)
402 (while (not (eolp))
403 (move-to-column 64)
404 (skip-chars-forward "^ \n")
405 (or (eolp)
406 (insert "\n" generate-autoload-section-continuation))))))
407
408 (defun autoload-find-file (file)
409 "Fetch file and put it in a temp buffer. Return the buffer."
410 ;; It is faster to avoid visiting the file.
411 (setq file (expand-file-name file))
412 (with-current-buffer (get-buffer-create " *autoload-file*")
413 (kill-all-local-variables)
414 (erase-buffer)
415 (setq buffer-undo-list t
416 buffer-read-only nil)
417 (delay-mode-hooks (emacs-lisp-mode))
418 (setq default-directory (file-name-directory file))
419 (insert-file-contents file nil)
420 (let ((enable-local-variables :safe)
421 (enable-local-eval nil))
422 (hack-local-variables))
423 (current-buffer)))
424
425 (defvar no-update-autoloads nil
426 "File local variable to prevent scanning this file for autoload cookies.")
427
428 (defun autoload-file-load-name (file)
429 "Compute the name that will be used to load FILE."
430 ;; OUTFILE should be the name of the global loaddefs.el file, which
431 ;; is expected to be at the root directory of the files we're
432 ;; scanning for autoloads and will be in the `load-path'.
433 (let* ((outfile (default-value 'generated-autoload-file))
434 (name (file-relative-name file (file-name-directory outfile)))
435 (names '())
436 (dir (file-name-directory outfile)))
437 ;; If `name' has directory components, only keep the
438 ;; last few that are really needed.
439 (while name
440 (setq name (directory-file-name name))
441 (push (file-name-nondirectory name) names)
442 (setq name (file-name-directory name)))
443 (while (not name)
444 (cond
445 ((null (cdr names)) (setq name (car names)))
446 ((file-exists-p (expand-file-name "subdirs.el" dir))
447 ;; FIXME: here we only check the existence of subdirs.el,
448 ;; without checking its content. This makes it generate wrong load
449 ;; names for cases like lisp/term which is not added to load-path.
450 (setq dir (expand-file-name (pop names) dir)))
451 (t (setq name (mapconcat 'identity names "/")))))
452 (if (string-match "\\.elc?\\(\\.\\|\\'\\)" name)
453 (substring name 0 (match-beginning 0))
454 name)))
455
456 (defun generate-file-autoloads (file)
457 "Insert at point a loaddefs autoload section for FILE.
458 Autoloads are generated for defuns and defmacros in FILE
459 marked by `generate-autoload-cookie' (which see).
460 If FILE is being visited in a buffer, the contents of the buffer
461 are used.
462 Return non-nil in the case where no autoloads were added at point."
463 (interactive "fGenerate autoloads for file: ")
464 (let ((generated-autoload-file buffer-file-name))
465 (autoload-generate-file-autoloads file (current-buffer))))
466
467 (defvar print-readably)
468
469
470 (defun autoload--setup-output (otherbuf outbuf absfile load-name)
471 (let ((outbuf
472 (or (if otherbuf
473 ;; A file-local setting of
474 ;; autoload-generated-file says we
475 ;; should ignore OUTBUF.
476 nil
477 outbuf)
478 (autoload-find-destination absfile load-name)
479 ;; The file has autoload cookies, but they're
480 ;; already up-to-date. If OUTFILE is nil, the
481 ;; entries are in the expected OUTBUF,
482 ;; otherwise they're elsewhere.
483 (throw 'done otherbuf))))
484 (with-current-buffer outbuf
485 (point-marker))))
486
487 (defun autoload--print-cookie-text (output-start load-name file)
488 (let ((standard-output (marker-buffer output-start)))
489 (search-forward generate-autoload-cookie)
490 (skip-chars-forward " \t")
491 (if (eolp)
492 (condition-case-unless-debug err
493 ;; Read the next form and make an autoload.
494 (let* ((form (prog1 (read (current-buffer))
495 (or (bolp) (forward-line 1))))
496 (autoload (make-autoload form load-name)))
497 (if autoload
498 nil
499 (setq autoload form))
500 (let ((autoload-print-form-outbuf
501 standard-output))
502 (autoload-print-form autoload)))
503 (error
504 (message "Autoload cookie error in %s:%s %S"
505 file (count-lines (point-min) (point)) err)))
506
507 ;; Copy the rest of the line to the output.
508 (princ (buffer-substring
509 (progn
510 ;; Back up over whitespace, to preserve it.
511 (skip-chars-backward " \f\t")
512 (if (= (char-after (1+ (point))) ? )
513 ;; Eat one space.
514 (forward-char 1))
515 (point))
516 (progn (forward-line 1) (point)))))))
517
518 (defvar autoload-builtin-package-versions nil)
519
520 ;; When called from `generate-file-autoloads' we should ignore
521 ;; `generated-autoload-file' altogether. When called from
522 ;; `update-file-autoloads' we don't know `outbuf'. And when called from
523 ;; `update-directory-autoloads' it's in between: we know the default
524 ;; `outbuf' but we should obey any file-local setting of
525 ;; `generated-autoload-file'.
526 (defun autoload-generate-file-autoloads (file &optional outbuf outfile)
527 "Insert an autoload section for FILE in the appropriate buffer.
528 Autoloads are generated for defuns and defmacros in FILE
529 marked by `generate-autoload-cookie' (which see).
530 If FILE is being visited in a buffer, the contents of the buffer are used.
531 OUTBUF is the buffer in which the autoload statements should be inserted.
532 If OUTBUF is nil, it will be determined by `autoload-generated-file'.
533
534 If provided, OUTFILE is expected to be the file name of OUTBUF.
535 If OUTFILE is non-nil and FILE specifies a `generated-autoload-file'
536 different from OUTFILE, then OUTBUF is ignored.
537
538 Return non-nil if and only if FILE adds no autoloads to OUTFILE
539 \(or OUTBUF if OUTFILE is nil). The actual return value is
540 FILE's modification time."
541 ;; Include the file name in any error messages
542 (condition-case err
543 (let (load-name
544 (print-length nil)
545 (print-level nil)
546 (print-readably t) ; This does something in Lucid Emacs.
547 (float-output-format nil)
548 (visited (get-file-buffer file))
549 (otherbuf nil)
550 (absfile (expand-file-name file))
551 ;; nil until we found a cookie.
552 output-start)
553 (when
554 (catch 'done
555 (with-current-buffer (or visited
556 ;; It is faster to avoid visiting the file.
557 (autoload-find-file file))
558 ;; Obey the no-update-autoloads file local variable.
559 (unless no-update-autoloads
560 (or noninteractive (message "Generating autoloads for %s..." file))
561 (setq load-name
562 (if (stringp generated-autoload-load-name)
563 generated-autoload-load-name
564 (autoload-file-load-name absfile)))
565 ;; FIXME? Comparing file-names for equality with just equal
566 ;; is fragile, eg if one has an automounter prefix and one
567 ;; does not, but both refer to the same physical file.
568 (when (and outfile
569 (not
570 (if (memq system-type '(ms-dos windows-nt))
571 (equal (downcase outfile)
572 (downcase (autoload-generated-file)))
573 (equal outfile (autoload-generated-file)))))
574 (setq otherbuf t))
575 (save-excursion
576 (save-restriction
577 (widen)
578 (when autoload-builtin-package-versions
579 (let ((version (lm-header "version"))
580 package)
581 (and version
582 (setq version (ignore-errors (version-to-list version)))
583 (setq package (or (lm-header "package")
584 (file-name-sans-extension
585 (file-name-nondirectory file))))
586 (setq output-start (autoload--setup-output
587 otherbuf outbuf absfile load-name))
588 (let ((standard-output (marker-buffer output-start))
589 (print-quoted t))
590 (princ `(push (purecopy
591 ',(cons (intern package) version))
592 package--builtin-versions))
593 (princ "\n")))))
594
595 (goto-char (point-min))
596 (while (not (eobp))
597 (skip-chars-forward " \t\n\f")
598 (cond
599 ((looking-at (regexp-quote generate-autoload-cookie))
600 ;; If not done yet, figure out where to insert this text.
601 (unless output-start
602 (setq output-start (autoload--setup-output
603 otherbuf outbuf absfile load-name)))
604 (autoload--print-cookie-text output-start load-name file))
605 ((looking-at ";")
606 ;; Don't read the comment.
607 (forward-line 1))
608 (t
609 (forward-sexp 1)
610 (forward-line 1))))))
611
612 (when output-start
613 (let ((secondary-autoloads-file-buf
614 (if otherbuf (current-buffer))))
615 (with-current-buffer (marker-buffer output-start)
616 (save-excursion
617 ;; Insert the section-header line which lists the file name
618 ;; and which functions are in it, etc.
619 (goto-char output-start)
620 (let ((relfile (file-relative-name absfile)))
621 (autoload-insert-section-header
622 (marker-buffer output-start)
623 () load-name relfile
624 (if secondary-autoloads-file-buf
625 ;; MD5 checksums are much better because they do not
626 ;; change unless the file changes (so they'll be
627 ;; equal on two different systems and will change
628 ;; less often than time-stamps, thus leading to fewer
629 ;; unneeded changes causing spurious conflicts), but
630 ;; using time-stamps is a very useful optimization,
631 ;; so we use time-stamps for the main autoloads file
632 ;; (loaddefs.el) where we have special ways to
633 ;; circumvent the "random change problem", and MD5
634 ;; checksum in secondary autoload files where we do
635 ;; not need the time-stamp optimization because it is
636 ;; already provided by the primary autoloads file.
637 (md5 secondary-autoloads-file-buf
638 ;; We'd really want to just use
639 ;; `emacs-internal' instead.
640 nil nil 'emacs-mule-unix)
641 (nth 5 (file-attributes relfile))))
642 (insert ";;; Generated autoloads from " relfile "\n")))
643 (insert generate-autoload-section-trailer))))
644 (or noninteractive
645 (message "Generating autoloads for %s...done" file)))
646 (or visited
647 ;; We created this buffer, so we should kill it.
648 (kill-buffer (current-buffer))))
649 (or (not output-start)
650 ;; If the entries were added to some other buffer, then the file
651 ;; doesn't add entries to OUTFILE.
652 otherbuf))
653 (nth 5 (file-attributes absfile))))
654 (error
655 ;; Probably unbalanced parens in forward-sexp. In that case, the
656 ;; condition is scan-error, and the signal data includes point
657 ;; where the error was found; we'd like to convert that to
658 ;; line:col, but line-number-at-pos gets the wrong line in batch
659 ;; mode for some reason.
660 ;;
661 ;; At least this gets the file name in the error message; the
662 ;; developer can use goto-char to get to the error position.
663 (error "%s:0:0: error: %s: %s" file (car err) (cdr err)))
664 ))
665 \f
666 (defun autoload-save-buffers ()
667 (while autoload-modified-buffers
668 (with-current-buffer (pop autoload-modified-buffers)
669 (let ((version-control 'never))
670 (save-buffer)))))
671
672 ;;;###autoload
673 (defun update-file-autoloads (file &optional save-after outfile)
674 "Update the autoloads for FILE.
675 If prefix arg SAVE-AFTER is non-nil, save the buffer too.
676
677 If FILE binds `generated-autoload-file' as a file-local variable,
678 autoloads are written into that file. Otherwise, the autoloads
679 file is determined by OUTFILE. If called interactively, prompt
680 for OUTFILE; if called from Lisp with OUTFILE nil, use the
681 existing value of `generated-autoload-file'.
682
683 Return FILE if there was no autoload cookie in it, else nil."
684 (interactive (list (read-file-name "Update autoloads for file: ")
685 current-prefix-arg
686 (read-file-name "Write autoload definitions to file: ")))
687 (let* ((generated-autoload-file (or outfile generated-autoload-file))
688 (autoload-modified-buffers nil)
689 (no-autoloads (autoload-generate-file-autoloads file)))
690 (if autoload-modified-buffers
691 (if save-after (autoload-save-buffers))
692 (if (called-interactively-p 'interactive)
693 (message "Autoload section for %s is up to date." file)))
694 (if no-autoloads file)))
695
696 (defun autoload-find-destination (file load-name)
697 "Find the destination point of the current buffer's autoloads.
698 FILE is the file name of the current buffer.
699 LOAD-NAME is the name as it appears in the output.
700 Returns a buffer whose point is placed at the requested location.
701 Returns nil if the file's autoloads are up-to-date, otherwise
702 removes any prior now out-of-date autoload entries."
703 (catch 'up-to-date
704 (let* ((buf (current-buffer))
705 (existing-buffer (if buffer-file-name buf))
706 (found nil))
707 (with-current-buffer (autoload-find-generated-file)
708 ;; This is to make generated-autoload-file have Unix EOLs, so
709 ;; that it is portable to all platforms.
710 (or (eq 0 (coding-system-eol-type buffer-file-coding-system))
711 (set-buffer-file-coding-system 'unix))
712 (or (> (buffer-size) 0)
713 (error "Autoloads file %s lacks boilerplate" buffer-file-name))
714 (or (file-writable-p buffer-file-name)
715 (error "Autoloads file %s is not writable" buffer-file-name))
716 (widen)
717 (goto-char (point-min))
718 ;; Look for the section for LOAD-NAME.
719 (while (and (not found)
720 (search-forward generate-autoload-section-header nil t))
721 (let ((form (autoload-read-section-header)))
722 (cond ((string= (nth 2 form) load-name)
723 ;; We found the section for this file.
724 ;; Check if it is up to date.
725 (let ((begin (match-beginning 0))
726 (last-time (nth 4 form))
727 (file-time (nth 5 (file-attributes file))))
728 (if (and (or (null existing-buffer)
729 (not (buffer-modified-p existing-buffer)))
730 (or
731 ;; last-time is the time-stamp (specifying
732 ;; the last time we looked at the file) and
733 ;; the file hasn't been changed since.
734 (and (listp last-time)
735 (not (time-less-p last-time file-time)))
736 ;; last-time is an MD5 checksum instead.
737 (and (stringp last-time)
738 (equal last-time
739 (md5 buf nil nil 'emacs-mule)))))
740 (throw 'up-to-date nil)
741 (autoload-remove-section begin)
742 (setq found t))))
743 ((string< load-name (nth 2 form))
744 ;; We've come to a section alphabetically later than
745 ;; LOAD-NAME. We assume the file is in order and so
746 ;; there must be no section for LOAD-NAME. We will
747 ;; insert one before the section here.
748 (goto-char (match-beginning 0))
749 (setq found t)))))
750 (or found
751 (progn
752 ;; No later sections in the file. Put before the last page.
753 (goto-char (point-max))
754 (search-backward "\f" nil t)))
755 (unless (memq (current-buffer) autoload-modified-buffers)
756 (push (current-buffer) autoload-modified-buffers))
757 (current-buffer)))))
758
759 (defun autoload-remove-section (begin)
760 (goto-char begin)
761 (search-forward generate-autoload-section-trailer)
762 (delete-region begin (point)))
763
764 ;;;###autoload
765 (defun update-directory-autoloads (&rest dirs)
766 "Update autoload definitions for Lisp files in the directories DIRS.
767 In an interactive call, you must give one argument, the name of a
768 single directory. In a call from Lisp, you can supply multiple
769 directories as separate arguments, but this usage is discouraged.
770
771 The function does NOT recursively descend into subdirectories of the
772 directory or directories specified.
773
774 In an interactive call, prompt for a default output file for the
775 autoload definitions, and temporarily bind the variable
776 `generated-autoload-file' to this value. When called from Lisp,
777 use the existing value of `generated-autoload-file'. If any Lisp
778 file binds `generated-autoload-file' as a file-local variable,
779 write its autoloads into the specified file instead."
780 (interactive "DUpdate autoloads from directory: ")
781 (let* ((files-re (let ((tmp nil))
782 (dolist (suf (get-load-suffixes))
783 (unless (string-match "\\.elc" suf) (push suf tmp)))
784 (concat "^[^=.].*" (regexp-opt tmp t) "\\'")))
785 (files (apply 'nconc
786 (mapcar (lambda (dir)
787 (directory-files (expand-file-name dir)
788 t files-re))
789 dirs)))
790 (done ())
791 (last-time)
792 ;; Files with no autoload cookies or whose autoloads go to other
793 ;; files because of file-local autoload-generated-file settings.
794 (no-autoloads nil)
795 (autoload-modified-buffers nil)
796 (generated-autoload-file
797 (if (called-interactively-p 'interactive)
798 (read-file-name "Write autoload definitions to file: ")
799 generated-autoload-file)))
800
801 (with-current-buffer (autoload-find-generated-file)
802 (save-excursion
803 ;; Canonicalize file names and remove the autoload file itself.
804 (setq files (delete (file-relative-name buffer-file-name)
805 (mapcar 'file-relative-name files)))
806
807 (goto-char (point-min))
808 (while (search-forward generate-autoload-section-header nil t)
809 (let* ((form (autoload-read-section-header))
810 (file (nth 3 form)))
811 (cond ((and (consp file) (stringp (car file)))
812 ;; This is a list of files that have no autoload cookies.
813 ;; There shouldn't be more than one such entry.
814 ;; Remove the obsolete section.
815 (autoload-remove-section (match-beginning 0))
816 (setq last-time (nth 4 form))
817 (dolist (file file)
818 (let ((file-time (nth 5 (file-attributes file))))
819 (when (and file-time
820 (not (time-less-p last-time file-time)))
821 ;; file unchanged
822 (push file no-autoloads)
823 (setq files (delete file files))))))
824 ((not (stringp file)))
825 ((or (not (file-exists-p file))
826 ;; Remove duplicates as well, just in case.
827 (member file done)
828 ;; If the file is actually excluded.
829 (member (expand-file-name file) autoload-excludes))
830 ;; Remove the obsolete section.
831 (autoload-remove-section (match-beginning 0)))
832 ((not (time-less-p (nth 4 form)
833 (nth 5 (file-attributes file))))
834 ;; File hasn't changed.
835 nil)
836 (t
837 (autoload-remove-section (match-beginning 0))
838 (if (autoload-generate-file-autoloads
839 ;; Passing `current-buffer' makes it insert at point.
840 file (current-buffer) buffer-file-name)
841 (push file no-autoloads))))
842 (push file done)
843 (setq files (delete file files)))))
844 ;; Elements remaining in FILES have no existing autoload sections yet.
845 (let ((no-autoloads-time (or last-time '(0 0 0 0))) file-time)
846 (dolist (file files)
847 (cond
848 ((member (expand-file-name file) autoload-excludes) nil)
849 ;; Passing nil as second argument forces
850 ;; autoload-generate-file-autoloads to look for the right
851 ;; spot where to insert each autoloads section.
852 ((setq file-time
853 (autoload-generate-file-autoloads file nil buffer-file-name))
854 (push file no-autoloads)
855 (if (time-less-p no-autoloads-time file-time)
856 (setq no-autoloads-time file-time)))))
857
858 (when no-autoloads
859 ;; Sort them for better readability.
860 (setq no-autoloads (sort no-autoloads 'string<))
861 ;; Add the `no-autoloads' section.
862 (goto-char (point-max))
863 (search-backward "\f" nil t)
864 (autoload-insert-section-header
865 (current-buffer) nil nil no-autoloads no-autoloads-time)
866 (insert generate-autoload-section-trailer)))
867
868 (let ((version-control 'never))
869 (save-buffer))
870 ;; In case autoload entries were added to other files because of
871 ;; file-local autoload-generated-file settings.
872 (autoload-save-buffers))))
873
874 (define-obsolete-function-alias 'update-autoloads-from-directories
875 'update-directory-autoloads "22.1")
876
877 ;;;###autoload
878 (defun batch-update-autoloads ()
879 "Update loaddefs.el autoloads in batch mode.
880 Calls `update-directory-autoloads' on the command line arguments.
881 Definitions are written to `generated-autoload-file' (which
882 should be non-nil)."
883 ;; For use during the Emacs build process only.
884 ;; Exclude those files that are preloaded on ALL platforms.
885 ;; These are the ones in loadup.el where "(load" is at the start
886 ;; of the line (crude, but it works).
887 (unless autoload-excludes
888 (let ((default-directory (file-name-directory generated-autoload-file))
889 file)
890 (when (file-readable-p "loadup.el")
891 (with-temp-buffer
892 (insert-file-contents "loadup.el")
893 (while (re-search-forward "^(load \"\\([^\"]+\\)\"" nil t)
894 (setq file (match-string 1))
895 (or (string-match "\\.el\\'" file)
896 (setq file (format "%s.el" file)))
897 (or (string-match "\\`site-" file)
898 (push (expand-file-name file) autoload-excludes)))))))
899 (let ((args command-line-args-left))
900 (setq command-line-args-left nil)
901 (apply 'update-directory-autoloads args)))
902
903 (provide 'autoload)
904
905 ;;; autoload.el ends here