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