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