]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/autoload.el
(make-autoload): Add the :setter for defcustoms corresponding to minor modes.
[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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, 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)
179
180 (defun autoload-print-form (form)
181 "Print FORM such that `make-docfile' will find the docstrings.
182 The variable `autoload-print-form-outbuf' specifies the buffer to
183 put the output in."
184 (cond
185 ;; If the form is a sequence, recurse.
186 ((eq (car form) 'progn) (mapcar 'autoload-print-form (cdr form)))
187 ;; Symbols at the toplevel are meaningless.
188 ((symbolp form) nil)
189 (t
190 (let ((doc-string-elt (get (car-safe form) 'doc-string-elt))
191 (outbuf autoload-print-form-outbuf))
192 (if (and doc-string-elt (stringp (nth doc-string-elt form)))
193 ;; We need to hack the printing because the
194 ;; doc-string must be printed specially for
195 ;; make-docfile (sigh).
196 (let* ((p (nthcdr (1- doc-string-elt) form))
197 (elt (cdr p)))
198 (setcdr p nil)
199 (princ "\n(" outbuf)
200 (let ((print-escape-newlines t)
201 (print-escape-nonascii t))
202 (dolist (elt form)
203 (prin1 elt outbuf)
204 (princ " " outbuf)))
205 (princ "\"\\\n" outbuf)
206 (let ((begin (with-current-buffer outbuf (point))))
207 (princ (substring (prin1-to-string (car elt)) 1)
208 outbuf)
209 ;; Insert a backslash before each ( that
210 ;; appears at the beginning of a line in
211 ;; the doc string.
212 (with-current-buffer outbuf
213 (save-excursion
214 (while (re-search-backward "\n[[(]" begin t)
215 (forward-char 1)
216 (insert "\\"))))
217 (if (null (cdr elt))
218 (princ ")" outbuf)
219 (princ " " outbuf)
220 (princ (substring (prin1-to-string (cdr elt)) 1)
221 outbuf))
222 (terpri outbuf)))
223 (let ((print-escape-newlines t)
224 (print-escape-nonascii t))
225 (print form outbuf)))))))
226
227 (defun autoload-ensure-default-file (file)
228 "Make sure that the autoload file FILE exists and if not create it."
229 (unless (file-exists-p file)
230 (write-region
231 (concat ";;; " (file-name-nondirectory file)
232 " --- automatically extracted autoloads\n"
233 ";;\n"
234 ";;; Code:\n\n"
235 "\f\n;; Local Variables:\n"
236 ";; version-control: never\n"
237 ";; no-byte-compile: t\n"
238 ";; no-update-autoloads: t\n"
239 ";; End:\n"
240 ";;; " (file-name-nondirectory file)
241 " ends here\n")
242 nil file))
243 file)
244
245 (defun autoload-insert-section-header (outbuf autoloads load-name file time)
246 "Insert the section-header line,
247 which lists the file name and which functions are in it, etc."
248 (insert generate-autoload-section-header)
249 (prin1 (list 'autoloads autoloads load-name
250 (if (stringp file) (autoload-trim-file-name file) file)
251 time)
252 outbuf)
253 (terpri outbuf)
254 ;; Break that line at spaces, to avoid very long lines.
255 ;; Make each sub-line into a comment.
256 (with-current-buffer outbuf
257 (save-excursion
258 (forward-line -1)
259 (while (not (eolp))
260 (move-to-column 64)
261 (skip-chars-forward "^ \n")
262 (or (eolp)
263 (insert "\n" generate-autoload-section-continuation))))))
264
265 (defun generate-file-autoloads (file)
266 "Insert at point a loaddefs autoload section for FILE.
267 autoloads are generated for defuns and defmacros in FILE
268 marked by `generate-autoload-cookie' (which see).
269 If FILE is being visited in a buffer, the contents of the buffer
270 are used."
271 (interactive "fGenerate autoloads for file: ")
272 (let ((outbuf (current-buffer))
273 (autoloads-done '())
274 (load-name (let ((name (file-name-nondirectory file)))
275 (if (string-match "\\.elc?\\(\\.\\|$\\)" name)
276 (substring name 0 (match-beginning 0))
277 name)))
278 (print-length nil)
279 (print-readably t) ; This does something in Lucid Emacs.
280 (float-output-format nil)
281 (done-any nil)
282 (visited (get-file-buffer file))
283 output-end)
284
285 ;; If the autoload section we create here uses an absolute
286 ;; file name for FILE in its header, and then Emacs is installed
287 ;; under a different path on another system,
288 ;; `update-autoloads-here' won't be able to find the files to be
289 ;; autoloaded. So, if FILE is in the same directory or a
290 ;; subdirectory of the current buffer's directory, we'll make it
291 ;; relative to the current buffer's directory.
292 (setq file (expand-file-name file))
293 (let* ((source-truename (file-truename file))
294 (dir-truename (file-name-as-directory
295 (file-truename default-directory)))
296 (len (length dir-truename)))
297 (if (and (< len (length source-truename))
298 (string= dir-truename (substring source-truename 0 len)))
299 (setq file (substring source-truename len))))
300
301 (message "Generating autoloads for %s..." file)
302 (save-excursion
303 (unwind-protect
304 (progn
305 (if visited
306 (set-buffer visited)
307 ;; It is faster to avoid visiting the file.
308 (set-buffer (get-buffer-create " *generate-autoload-file*"))
309 (kill-all-local-variables)
310 (erase-buffer)
311 (setq buffer-undo-list t
312 buffer-read-only nil)
313 (emacs-lisp-mode)
314 (insert-file-contents file nil))
315 (save-excursion
316 (save-restriction
317 (widen)
318 (goto-char (point-min))
319 (while (not (eobp))
320 (skip-chars-forward " \t\n\f")
321 (cond
322 ((looking-at (regexp-quote generate-autoload-cookie))
323 (search-forward generate-autoload-cookie)
324 (skip-chars-forward " \t")
325 (setq done-any t)
326 (if (eolp)
327 ;; Read the next form and make an autoload.
328 (let* ((form (prog1 (read (current-buffer))
329 (or (bolp) (forward-line 1))))
330 (autoload (make-autoload form load-name)))
331 (if autoload
332 (setq autoloads-done (cons (nth 1 form)
333 autoloads-done))
334 (setq autoload form))
335 (let ((autoload-print-form-outbuf outbuf))
336 (autoload-print-form autoload)))
337
338 ;; Copy the rest of the line to the output.
339 (princ (buffer-substring
340 (progn
341 ;; Back up over whitespace, to preserve it.
342 (skip-chars-backward " \f\t")
343 (if (= (char-after (1+ (point))) ? )
344 ;; Eat one space.
345 (forward-char 1))
346 (point))
347 (progn (forward-line 1) (point)))
348 outbuf)))
349 ((looking-at ";")
350 ;; Don't read the comment.
351 (forward-line 1))
352 (t
353 (forward-sexp 1)
354 (forward-line 1)))))))
355 (or visited
356 ;; We created this buffer, so we should kill it.
357 (kill-buffer (current-buffer)))
358 (set-buffer outbuf)
359 (setq output-end (point-marker))))
360 (if done-any
361 (progn
362 ;; Insert the section-header line
363 ;; which lists the file name and which functions are in it, etc.
364 (autoload-insert-section-header outbuf autoloads-done load-name file
365 (nth 5 (file-attributes file)))
366 (insert ";;; Generated autoloads from "
367 (autoload-trim-file-name file) "\n")
368 (goto-char output-end)
369 (insert generate-autoload-section-trailer)))
370 (message "Generating autoloads for %s...done" file)))
371 \f
372 ;;;###autoload
373 (defun update-file-autoloads (file &optional save-after)
374 "Update the autoloads for FILE in `generated-autoload-file'
375 \(which FILE might bind in its local variables).
376 If SAVE-AFTER is non-nil (which is always, when called interactively),
377 save the buffer too.
378
379 Return FILE if there was no autoload cookie in it, else nil."
380 (interactive "fUpdate autoloads for file: \np")
381 (let ((load-name (let ((name (file-name-nondirectory file)))
382 (if (string-match "\\.elc?\\(\\.\\|$\\)" name)
383 (substring name 0 (match-beginning 0))
384 name)))
385 (found nil)
386 (existing-buffer (get-file-buffer file))
387 (no-autoloads nil))
388 (save-excursion
389 ;; We want to get a value for generated-autoload-file from
390 ;; the local variables section if it's there.
391 (if existing-buffer
392 (set-buffer existing-buffer))
393 ;; We must read/write the file without any code conversion,
394 ;; but still decode EOLs.
395 (let ((coding-system-for-read 'raw-text))
396 (set-buffer (find-file-noselect
397 (autoload-ensure-default-file
398 (expand-file-name generated-autoload-file
399 (expand-file-name "lisp"
400 source-directory)))))
401 ;; This is to make generated-autoload-file have Unix EOLs, so
402 ;; that it is portable to all platforms.
403 (setq buffer-file-coding-system 'raw-text-unix))
404 (or (> (buffer-size) 0)
405 (error "Autoloads file %s does not exist" buffer-file-name))
406 (or (file-writable-p buffer-file-name)
407 (error "Autoloads file %s is not writable" buffer-file-name))
408 (save-excursion
409 (save-restriction
410 (widen)
411 (goto-char (point-min))
412 ;; Look for the section for LOAD-NAME.
413 (while (and (not found)
414 (search-forward generate-autoload-section-header nil t))
415 (let ((form (autoload-read-section-header)))
416 (cond ((string= (nth 2 form) load-name)
417 ;; We found the section for this file.
418 ;; Check if it is up to date.
419 (let ((begin (match-beginning 0))
420 (last-time (nth 4 form))
421 (file-time (nth 5 (file-attributes file))))
422 (if (and (or (null existing-buffer)
423 (not (buffer-modified-p existing-buffer)))
424 (listp last-time) (= (length last-time) 2)
425 (not (time-less-p last-time file-time)))
426 (progn
427 (if (interactive-p)
428 (message "\
429 Autoload section for %s is up to date."
430 file))
431 (setq found 'up-to-date))
432 (search-forward generate-autoload-section-trailer)
433 (delete-region begin (point))
434 (setq found t))))
435 ((string< load-name (nth 2 form))
436 ;; We've come to a section alphabetically later than
437 ;; LOAD-NAME. We assume the file is in order and so
438 ;; there must be no section for LOAD-NAME. We will
439 ;; insert one before the section here.
440 (goto-char (match-beginning 0))
441 (setq found 'new)))))
442 (or found
443 (progn
444 (setq found 'new)
445 ;; No later sections in the file. Put before the last page.
446 (goto-char (point-max))
447 (search-backward "\f" nil t)))
448 (or (eq found 'up-to-date)
449 (and (eq found 'new)
450 ;; Check that FILE has any cookies before generating a
451 ;; new section for it.
452 (save-excursion
453 (if existing-buffer
454 (set-buffer existing-buffer)
455 ;; It is faster to avoid visiting the file.
456 (set-buffer (get-buffer-create " *autoload-file*"))
457 (kill-all-local-variables)
458 (erase-buffer)
459 (setq buffer-undo-list t
460 buffer-read-only nil)
461 (emacs-lisp-mode)
462 (insert-file-contents file nil))
463 (save-excursion
464 (save-restriction
465 (widen)
466 (goto-char (point-min))
467 (prog1
468 (if (re-search-forward
469 (concat "^" (regexp-quote
470 generate-autoload-cookie))
471 nil t)
472 nil
473 (if (interactive-p)
474 (message "%s has no autoloads" file))
475 (setq no-autoloads t)
476 t)
477 (or existing-buffer
478 (kill-buffer (current-buffer))))))))
479 (generate-file-autoloads file))))
480 (and save-after
481 (buffer-modified-p)
482 (save-buffer))
483
484 (if no-autoloads file))))
485
486 (defun autoload-remove-section (begin)
487 (goto-char begin)
488 (search-forward generate-autoload-section-trailer)
489 (delete-region begin (point)))
490
491 ;;;###autoload
492 (defun update-directory-autoloads (&rest dirs)
493 "\
494 Update loaddefs.el with all the current autoloads from DIRS, and no old ones.
495 This uses `update-file-autoloads' (which see) do its work.
496 In an interactive call, you must give one argument, the name
497 of a single directory. In a call from Lisp, you can supply multiple
498 directories as separate arguments, but this usage is discouraged.
499
500 The function does NOT recursively descend into subdirectories of the
501 directory or directories specified."
502 (interactive "DUpdate autoloads from directory: ")
503 (let* ((files-re (let ((tmp nil))
504 (dolist (suf load-suffixes
505 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
506 (unless (string-match "\\.elc" suf) (push suf tmp)))))
507 (files (apply 'nconc
508 (mapcar (lambda (dir)
509 (directory-files (expand-file-name dir)
510 t files-re))
511 dirs)))
512 (this-time (current-time))
513 (no-autoloads nil) ;files with no autoload cookies.
514 (autoloads-file
515 (expand-file-name generated-autoload-file
516 (expand-file-name "lisp" source-directory)))
517 (top-dir (file-name-directory autoloads-file)))
518
519 (with-current-buffer
520 (find-file-noselect (autoload-ensure-default-file autoloads-file))
521 (save-excursion
522
523 ;; Canonicalize file names and remove the autoload file itself.
524 (setq files (delete (autoload-trim-file-name buffer-file-name)
525 (mapcar 'autoload-trim-file-name files)))
526
527 (goto-char (point-min))
528 (while (search-forward generate-autoload-section-header nil t)
529 (let* ((form (autoload-read-section-header))
530 (file (nth 3 form)))
531 (cond ((and (consp file) (stringp (car file)))
532 ;; This is a list of files that have no autoload cookies.
533 ;; There shouldn't be more than one such entry.
534 ;; Remove the obsolete section.
535 (autoload-remove-section (match-beginning 0))
536 (let ((last-time (nth 4 form)))
537 (dolist (file file)
538 (let ((file-time (nth 5 (file-attributes file))))
539 (when (and file-time
540 (not (time-less-p last-time file-time)))
541 ;; file unchanged
542 (push file no-autoloads)
543 (setq files (delete file files)))))))
544 ((not (stringp file)))
545 ((not (file-exists-p (expand-file-name file top-dir)))
546 ;; Remove the obsolete section.
547 (autoload-remove-section (match-beginning 0)))
548 ((equal (nth 4 form) (nth 5 (file-attributes file)))
549 ;; File hasn't changed.
550 nil)
551 (t
552 (update-file-autoloads file)))
553 (setq files (delete file files)))))
554 ;; Elements remaining in FILES have no existing autoload sections yet.
555 (setq no-autoloads
556 (append no-autoloads
557 (delq nil (mapcar 'update-file-autoloads files))))
558 (when no-autoloads
559 ;; Sort them for better readability.
560 (setq no-autoloads (sort no-autoloads 'string<))
561 ;; Add the `no-autoloads' section.
562 (goto-char (point-max))
563 (search-backward "\f" nil t)
564 (autoload-insert-section-header
565 (current-buffer) nil nil no-autoloads this-time)
566 (insert generate-autoload-section-trailer))
567
568 (save-buffer))))
569
570 ;;;###autoload
571 (defun batch-update-autoloads ()
572 "Update loaddefs.el autoloads in batch mode.
573 Calls `update-directory-autoloads' on the command line arguments."
574 (apply 'update-directory-autoloads command-line-args-left)
575 (setq command-line-args-left nil))
576
577 (provide 'autoload)
578
579 ;; arch-tag: 00244766-98f4-4767-bf42-8a22103441c6
580 ;;; autoload.el ends here