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