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