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