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