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