]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/autoload.el
(update-directory-autoloads): Doc fix.
[gnu-emacs] / lisp / emacs-lisp / autoload.el
1 ;; autoload.el --- maintain autoloads in loaddefs.el
2
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2001, 2002, 2003,
4 ;; 2004, 2005 Free Software Foundation, Inc.
5
6 ;; Author: Roland McGrath <roland@gnu.org>
7 ;; Keywords: maint
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 2, or (at your option)
14 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; This code helps GNU Emacs maintainers keep the loaddefs.el file up to
29 ;; date. It interprets magic cookies of the form ";;;###autoload" in
30 ;; lisp source files in various useful ways. To learn more, read the
31 ;; source; if you're going to use this, you'd better be able to.
32
33 ;;; Code:
34
35 (require 'lisp-mode) ;for `doc-string-elt' properties.
36 (require 'help-fns) ;for help-add-fundoc-usage.
37 (eval-when-compile (require 'cl))
38
39 (defvar generated-autoload-file "loaddefs.el"
40 "*File \\[update-file-autoloads] puts autoloads into.
41 A `.el' file can set this in its local variables section to make its
42 autoloads go somewhere else. The autoload file is assumed to contain a
43 trailer starting with a FormFeed character.")
44
45 (defconst generate-autoload-cookie ";;;###autoload"
46 "Magic comment indicating the following form should be autoloaded.
47 Used by \\[update-file-autoloads]. This string should be
48 meaningless to Lisp (e.g., a comment).
49
50 This string is used:
51
52 ;;;###autoload
53 \(defun function-to-be-autoloaded () ...)
54
55 If this string appears alone on a line, the following form will be
56 read and an autoload made for it. If there is further text on the line,
57 that text will be copied verbatim to `generated-autoload-file'.")
58
59 (defconst generate-autoload-section-header "\f\n;;;### "
60 "String that marks the form at the start of a new file's autoload section.")
61
62 (defconst generate-autoload-section-trailer "\n;;;***\n"
63 "String which indicates the end of the section of autoloads for a file.")
64
65 (defconst generate-autoload-section-continuation ";;;;;; "
66 "String to add on each continuation of the section header form.")
67
68 (defun make-autoload (form file)
69 "Turn FORM into an autoload or defvar for source file FILE.
70 Returns nil if FORM is not a special autoload form (i.e. a function definition
71 or macro definition or a defcustom)."
72 (let ((car (car-safe form)) expand)
73 (cond
74 ;; For complex cases, try again on the macro-expansion.
75 ((and (memq car '(easy-mmode-define-global-mode define-global-minor-mode
76 easy-mmode-define-minor-mode define-minor-mode))
77 (setq expand (let ((load-file-name file)) (macroexpand form)))
78 (eq (car expand) 'progn)
79 (memq :autoload-end expand))
80 (let ((end (memq :autoload-end expand)))
81 ;; Cut-off anything after the :autoload-end marker.
82 (setcdr end nil)
83 (cons 'progn
84 (mapcar (lambda (form) (make-autoload form file))
85 (cdr expand)))))
86
87 ;; For special function-like operators, use the `autoload' function.
88 ((memq car '(defun define-skeleton defmacro define-derived-mode
89 define-compilation-mode
90 define-generic-mode easy-mmode-define-minor-mode
91 easy-mmode-define-global-mode
92 define-minor-mode defun* defmacro*))
93 (let* ((macrop (memq car '(defmacro defmacro*)))
94 (name (nth 1 form))
95 (args (case car
96 ((defun defmacro defun* defmacro*) (nth 2 form))
97 ((define-skeleton) '(&optional str arg))
98 ((define-generic-mode define-derived-mode
99 define-compilation-mode) nil)
100 (t)))
101 (body (nthcdr (get car 'doc-string-elt) form))
102 (doc (if (stringp (car body)) (pop body))))
103 (when (listp args)
104 ;; Add the usage form at the end where describe-function-1
105 ;; can recover it.
106 (setq doc (help-add-fundoc-usage doc args)))
107 ;; `define-generic-mode' quotes the name, so take care of that
108 (list 'autoload (if (listp name) name (list 'quote name)) file doc
109 (or (and (memq car '(define-skeleton define-derived-mode
110 define-generic-mode
111 easy-mmode-define-global-mode
112 easy-mmode-define-minor-mode
113 define-minor-mode)) t)
114 (eq (car-safe (car body)) 'interactive))
115 (if macrop (list 'quote 'macro) nil))))
116
117 ;; Convert defcustom to less space-consuming data.
118 ((eq car 'defcustom)
119 (let ((varname (car-safe (cdr-safe form)))
120 (init (car-safe (cdr-safe (cdr-safe form))))
121 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form)))))
122 ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
123 )
124 `(progn
125 (defvar ,varname ,init ,doc)
126 (custom-autoload ',varname ,file)
127 ;; The use of :require in a defcustom can be annoying, especially
128 ;; when defcustoms are moved from one file to another between
129 ;; releases because the :require arg gets placed in the user's
130 ;; .emacs. In order for autoloaded minor modes not to need the
131 ;; use of :require, we arrange to store their :setter.
132 ,(let ((setter (condition-case nil
133 (cadr (memq :set form))
134 (error nil))))
135 (if (equal setter ''custom-set-minor-mode)
136 `(put ',varname 'custom-set 'custom-set-minor-mode))))))
137
138 ;; nil here indicates that this is not a special autoload form.
139 (t nil))))
140
141 ;; Forms which have doc-strings which should be printed specially.
142 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
143 ;; the doc-string in FORM.
144 ;; Those properties are now set in lisp-mode.el.
145
146
147 (defun autoload-trim-file-name (file)
148 ;; Returns a relative file path for FILE
149 ;; starting from the directory that loaddefs.el is in.
150 ;; That is normally a directory in load-path,
151 ;; which means Emacs will be able to find FILE when it looks.
152 ;; Any extra directory names here would prevent finding the file.
153 (setq file (expand-file-name file))
154 (file-relative-name file
155 (file-name-directory generated-autoload-file)))
156
157 (defun autoload-read-section-header ()
158 "Read a section header form.
159 Since continuation lines have been marked as comments,
160 we must copy the text of the form and remove those comment
161 markers before we call `read'."
162 (save-match-data
163 (let ((beginning (point))
164 string)
165 (forward-line 1)
166 (while (looking-at generate-autoload-section-continuation)
167 (forward-line 1))
168 (setq string (buffer-substring beginning (point)))
169 (with-current-buffer (get-buffer-create " *autoload*")
170 (erase-buffer)
171 (insert string)
172 (goto-char (point-min))
173 (while (search-forward generate-autoload-section-continuation nil t)
174 (replace-match " "))
175 (goto-char (point-min))
176 (read (current-buffer))))))
177
178 (defvar autoload-print-form-outbuf nil
179 "Buffer which gets the output of `autoload-print-form'.")
180
181 (defun autoload-print-form (form)
182 "Print FORM such that `make-docfile' will find the docstrings.
183 The variable `autoload-print-form-outbuf' specifies the buffer to
184 put the output in."
185 (cond
186 ;; If the form is a sequence, recurse.
187 ((eq (car form) 'progn) (mapcar 'autoload-print-form (cdr form)))
188 ;; Symbols at the toplevel are meaningless.
189 ((symbolp form) nil)
190 (t
191 (let ((doc-string-elt (get (car-safe form) 'doc-string-elt))
192 (outbuf autoload-print-form-outbuf))
193 (if (and doc-string-elt (stringp (nth doc-string-elt form)))
194 ;; We need to hack the printing because the
195 ;; doc-string must be printed specially for
196 ;; make-docfile (sigh).
197 (let* ((p (nthcdr (1- doc-string-elt) form))
198 (elt (cdr p)))
199 (setcdr p nil)
200 (princ "\n(" outbuf)
201 (let ((print-escape-newlines t)
202 (print-escape-nonascii t))
203 (dolist (elt form)
204 (prin1 elt outbuf)
205 (princ " " outbuf)))
206 (princ "\"\\\n" outbuf)
207 (let ((begin (with-current-buffer outbuf (point))))
208 (princ (substring (prin1-to-string (car elt)) 1)
209 outbuf)
210 ;; Insert a backslash before each ( that
211 ;; appears at the beginning of a line in
212 ;; the doc string.
213 (with-current-buffer outbuf
214 (save-excursion
215 (while (re-search-backward "\n[[(]" begin t)
216 (forward-char 1)
217 (insert "\\"))))
218 (if (null (cdr elt))
219 (princ ")" outbuf)
220 (princ " " outbuf)
221 (princ (substring (prin1-to-string (cdr elt)) 1)
222 outbuf))
223 (terpri outbuf)))
224 (let ((print-escape-newlines t)
225 (print-escape-nonascii t))
226 (print form outbuf)))))))
227
228 (defun autoload-ensure-default-file (file)
229 "Make sure that the autoload file FILE exists and if not create it."
230 (unless (file-exists-p file)
231 (write-region
232 (concat ";;; " (file-name-nondirectory file)
233 " --- automatically extracted autoloads\n"
234 ";;\n"
235 ";;; Code:\n\n"
236 "\f\n;; Local Variables:\n"
237 ";; version-control: never\n"
238 ";; no-byte-compile: t\n"
239 ";; no-update-autoloads: t\n"
240 ";; End:\n"
241 ";;; " (file-name-nondirectory file)
242 " ends here\n")
243 nil file))
244 file)
245
246 (defun autoload-insert-section-header (outbuf autoloads load-name file time)
247 "Insert the section-header line,
248 which lists the file name and which functions are in it, etc."
249 (insert generate-autoload-section-header)
250 (prin1 (list 'autoloads autoloads load-name
251 (if (stringp file) (autoload-trim-file-name file) file)
252 time)
253 outbuf)
254 (terpri outbuf)
255 ;; Break that line at spaces, to avoid very long lines.
256 ;; Make each sub-line into a comment.
257 (with-current-buffer outbuf
258 (save-excursion
259 (forward-line -1)
260 (while (not (eolp))
261 (move-to-column 64)
262 (skip-chars-forward "^ \n")
263 (or (eolp)
264 (insert "\n" generate-autoload-section-continuation))))))
265
266 (defun generate-file-autoloads (file)
267 "Insert at point a loaddefs autoload section for FILE.
268 autoloads are generated for defuns and defmacros in FILE
269 marked by `generate-autoload-cookie' (which see).
270 If FILE is being visited in a buffer, the contents of the buffer
271 are used."
272 (interactive "fGenerate autoloads for file: ")
273 (let ((outbuf (current-buffer))
274 (autoloads-done '())
275 (load-name (let ((name (file-name-nondirectory file)))
276 (if (string-match "\\.elc?\\(\\.\\|$\\)" name)
277 (substring name 0 (match-beginning 0))
278 name)))
279 (print-length nil)
280 (print-readably t) ; This does something in Lucid Emacs.
281 (float-output-format nil)
282 (done-any nil)
283 (visited (get-file-buffer file))
284 output-end)
285
286 ;; If the autoload section we create here uses an absolute
287 ;; file name for FILE in its header, and then Emacs is installed
288 ;; under a different path on another system,
289 ;; `update-autoloads-here' won't be able to find the files to be
290 ;; autoloaded. So, if FILE is in the same directory or a
291 ;; subdirectory of the current buffer's directory, we'll make it
292 ;; relative to the current buffer's directory.
293 (setq file (expand-file-name file))
294 (let* ((source-truename (file-truename file))
295 (dir-truename (file-name-as-directory
296 (file-truename default-directory)))
297 (len (length dir-truename)))
298 (if (and (< len (length source-truename))
299 (string= dir-truename (substring source-truename 0 len)))
300 (setq file (substring source-truename len))))
301
302 (message "Generating autoloads for %s..." file)
303 (save-excursion
304 (unwind-protect
305 (progn
306 (if visited
307 (set-buffer visited)
308 ;; It is faster to avoid visiting the file.
309 (set-buffer (get-buffer-create " *generate-autoload-file*"))
310 (kill-all-local-variables)
311 (erase-buffer)
312 (setq buffer-undo-list t
313 buffer-read-only nil)
314 (emacs-lisp-mode)
315 (insert-file-contents file nil))
316 (save-excursion
317 (save-restriction
318 (widen)
319 (goto-char (point-min))
320 (while (not (eobp))
321 (skip-chars-forward " \t\n\f")
322 (cond
323 ((looking-at (regexp-quote generate-autoload-cookie))
324 (search-forward generate-autoload-cookie)
325 (skip-chars-forward " \t")
326 (setq done-any t)
327 (if (eolp)
328 ;; Read the next form and make an autoload.
329 (let* ((form (prog1 (read (current-buffer))
330 (or (bolp) (forward-line 1))))
331 (autoload (make-autoload form load-name)))
332 (if autoload
333 (setq autoloads-done (cons (nth 1 form)
334 autoloads-done))
335 (setq autoload form))
336 (let ((autoload-print-form-outbuf outbuf))
337 (autoload-print-form autoload)))
338
339 ;; Copy the rest of the line to the output.
340 (princ (buffer-substring
341 (progn
342 ;; Back up over whitespace, to preserve it.
343 (skip-chars-backward " \f\t")
344 (if (= (char-after (1+ (point))) ? )
345 ;; Eat one space.
346 (forward-char 1))
347 (point))
348 (progn (forward-line 1) (point)))
349 outbuf)))
350 ((looking-at ";")
351 ;; Don't read the comment.
352 (forward-line 1))
353 (t
354 (forward-sexp 1)
355 (forward-line 1)))))))
356 (or visited
357 ;; We created this buffer, so we should kill it.
358 (kill-buffer (current-buffer)))
359 (set-buffer outbuf)
360 (setq output-end (point-marker))))
361 (if done-any
362 (progn
363 ;; Insert the section-header line
364 ;; which lists the file name and which functions are in it, etc.
365 (autoload-insert-section-header outbuf autoloads-done load-name file
366 (nth 5 (file-attributes file)))
367 (insert ";;; Generated autoloads from "
368 (autoload-trim-file-name file) "\n")
369 (goto-char output-end)
370 (insert generate-autoload-section-trailer)))
371 (message "Generating autoloads for %s...done" file)))
372 \f
373 ;;;###autoload
374 (defun update-file-autoloads (file &optional save-after)
375 "Update the autoloads for FILE in `generated-autoload-file'
376 \(which FILE might bind in its local variables).
377 If SAVE-AFTER is non-nil (which is always, when called interactively),
378 save the buffer too.
379
380 Return FILE if there was no autoload cookie in it, else nil."
381 (interactive "fUpdate autoloads for file: \np")
382 (let ((load-name (let ((name (file-name-nondirectory file)))
383 (if (string-match "\\.elc?\\(\\.\\|$\\)" name)
384 (substring name 0 (match-beginning 0))
385 name)))
386 (found nil)
387 (existing-buffer (get-file-buffer file))
388 (no-autoloads nil))
389 (save-excursion
390 ;; We want to get a value for generated-autoload-file from
391 ;; the local variables section if it's there.
392 (if existing-buffer
393 (set-buffer existing-buffer))
394 ;; We must read/write the file without any code conversion,
395 ;; but still decode EOLs.
396 (let ((coding-system-for-read 'raw-text))
397 (set-buffer (find-file-noselect
398 (autoload-ensure-default-file
399 (expand-file-name generated-autoload-file
400 (expand-file-name "lisp"
401 source-directory)))))
402 ;; This is to make generated-autoload-file have Unix EOLs, so
403 ;; that it is portable to all platforms.
404 (setq buffer-file-coding-system 'raw-text-unix))
405 (or (> (buffer-size) 0)
406 (error "Autoloads file %s does not exist" buffer-file-name))
407 (or (file-writable-p buffer-file-name)
408 (error "Autoloads file %s is not writable" buffer-file-name))
409 (save-excursion
410 (save-restriction
411 (widen)
412 (goto-char (point-min))
413 ;; Look for the section for LOAD-NAME.
414 (while (and (not found)
415 (search-forward generate-autoload-section-header nil t))
416 (let ((form (autoload-read-section-header)))
417 (cond ((string= (nth 2 form) load-name)
418 ;; We found the section for this file.
419 ;; Check if it is up to date.
420 (let ((begin (match-beginning 0))
421 (last-time (nth 4 form))
422 (file-time (nth 5 (file-attributes file))))
423 (if (and (or (null existing-buffer)
424 (not (buffer-modified-p existing-buffer)))
425 (listp last-time) (= (length last-time) 2)
426 (not (time-less-p last-time file-time)))
427 (progn
428 (if (interactive-p)
429 (message "\
430 Autoload section for %s is up to date."
431 file))
432 (setq found 'up-to-date))
433 (search-forward generate-autoload-section-trailer)
434 (delete-region begin (point))
435 (setq found t))))
436 ((string< load-name (nth 2 form))
437 ;; We've come to a section alphabetically later than
438 ;; LOAD-NAME. We assume the file is in order and so
439 ;; there must be no section for LOAD-NAME. We will
440 ;; insert one before the section here.
441 (goto-char (match-beginning 0))
442 (setq found 'new)))))
443 (or found
444 (progn
445 (setq found 'new)
446 ;; No later sections in the file. Put before the last page.
447 (goto-char (point-max))
448 (search-backward "\f" nil t)))
449 (or (eq found 'up-to-date)
450 (and (eq found 'new)
451 ;; Check that FILE has any cookies before generating a
452 ;; new section for it.
453 (save-excursion
454 (if existing-buffer
455 (set-buffer existing-buffer)
456 ;; It is faster to avoid visiting the file.
457 (set-buffer (get-buffer-create " *autoload-file*"))
458 (kill-all-local-variables)
459 (erase-buffer)
460 (setq buffer-undo-list t
461 buffer-read-only nil)
462 (emacs-lisp-mode)
463 (insert-file-contents file nil))
464 (save-excursion
465 (save-restriction
466 (widen)
467 (goto-char (point-min))
468 (prog1
469 (if (re-search-forward
470 (concat "^" (regexp-quote
471 generate-autoload-cookie))
472 nil t)
473 nil
474 (if (interactive-p)
475 (message "%s has no autoloads" file))
476 (setq no-autoloads t)
477 t)
478 (or existing-buffer
479 (kill-buffer (current-buffer))))))))
480 (generate-file-autoloads file))))
481 (and save-after
482 (buffer-modified-p)
483 (save-buffer))
484
485 (if no-autoloads file))))
486
487 (defun autoload-remove-section (begin)
488 (goto-char begin)
489 (search-forward generate-autoload-section-trailer)
490 (delete-region begin (point)))
491
492 ;;;###autoload
493 (defun update-directory-autoloads (&rest dirs)
494 "\
495 Update loaddefs.el with all the current autoloads from DIRS, and no old ones.
496 This uses `update-file-autoloads' (which see) to do its work.
497 In an interactive call, you must give one argument, the name
498 of a single directory. In a call from Lisp, you can supply multiple
499 directories as separate arguments, but this usage is discouraged.
500
501 The function does NOT recursively descend into subdirectories of the
502 directory or directories specified."
503 (interactive "DUpdate autoloads from directory: ")
504 (let* ((files-re (let ((tmp nil))
505 (dolist (suf load-suffixes
506 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
507 (unless (string-match "\\.elc" suf) (push suf tmp)))))
508 (files (apply 'nconc
509 (mapcar (lambda (dir)
510 (directory-files (expand-file-name dir)
511 t files-re))
512 dirs)))
513 (this-time (current-time))
514 (no-autoloads nil) ;files with no autoload cookies.
515 (autoloads-file
516 (expand-file-name generated-autoload-file
517 (expand-file-name "lisp" source-directory)))
518 (top-dir (file-name-directory autoloads-file)))
519
520 (with-current-buffer
521 (find-file-noselect (autoload-ensure-default-file autoloads-file))
522 (save-excursion
523
524 ;; Canonicalize file names and remove the autoload file itself.
525 (setq files (delete (autoload-trim-file-name buffer-file-name)
526 (mapcar 'autoload-trim-file-name files)))
527
528 (goto-char (point-min))
529 (while (search-forward generate-autoload-section-header nil t)
530 (let* ((form (autoload-read-section-header))
531 (file (nth 3 form)))
532 (cond ((and (consp file) (stringp (car file)))
533 ;; This is a list of files that have no autoload cookies.
534 ;; There shouldn't be more than one such entry.
535 ;; Remove the obsolete section.
536 (autoload-remove-section (match-beginning 0))
537 (let ((last-time (nth 4 form)))
538 (dolist (file file)
539 (let ((file-time (nth 5 (file-attributes file))))
540 (when (and file-time
541 (not (time-less-p last-time file-time)))
542 ;; file unchanged
543 (push file no-autoloads)
544 (setq files (delete file files)))))))
545 ((not (stringp file)))
546 ((not (file-exists-p (expand-file-name file top-dir)))
547 ;; Remove the obsolete section.
548 (autoload-remove-section (match-beginning 0)))
549 ((equal (nth 4 form) (nth 5 (file-attributes file)))
550 ;; File hasn't changed.
551 nil)
552 (t
553 (update-file-autoloads file)))
554 (setq files (delete file files)))))
555 ;; Elements remaining in FILES have no existing autoload sections yet.
556 (setq no-autoloads
557 (append no-autoloads
558 (delq nil (mapcar 'update-file-autoloads files))))
559 (when no-autoloads
560 ;; Sort them for better readability.
561 (setq no-autoloads (sort no-autoloads 'string<))
562 ;; Add the `no-autoloads' section.
563 (goto-char (point-max))
564 (search-backward "\f" nil t)
565 (autoload-insert-section-header
566 (current-buffer) nil nil no-autoloads this-time)
567 (insert generate-autoload-section-trailer))
568
569 (save-buffer))))
570
571 ;;;###autoload
572 (defun batch-update-autoloads ()
573 "Update loaddefs.el autoloads in batch mode.
574 Calls `update-directory-autoloads' on the command line arguments."
575 (apply 'update-directory-autoloads command-line-args-left)
576 (setq command-line-args-left nil))
577
578 (provide 'autoload)
579
580 ;; arch-tag: 00244766-98f4-4767-bf42-8a22103441c6
581 ;;; autoload.el ends here