]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/autoload.el
(autoload-find-destination): Return nil
[gnu-emacs] / lisp / emacs-lisp / autoload.el
1 ;; autoload.el --- maintain autoloads in loaddefs.el
2
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007 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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, 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 (put 'generated-autoload-file 'safe-local-variable 'stringp)
45
46 ;; This feels like it should be a defconst, but MH-E sets it to
47 ;; ";;;###mh-autoload" for the autoloads that are to go into mh-loaddefs.el.
48 (defvar generate-autoload-cookie ";;;###autoload"
49 "Magic comment indicating the following form should be autoloaded.
50 Used by \\[update-file-autoloads]. This string should be
51 meaningless to Lisp (e.g., a comment).
52
53 This string is used:
54
55 \;;;###autoload
56 \(defun function-to-be-autoloaded () ...)
57
58 If this string appears alone on a line, the following form will be
59 read and an autoload made for it. If there is further text on the line,
60 that text will be copied verbatim to `generated-autoload-file'.")
61
62 (defconst generate-autoload-section-header "\f\n;;;### "
63 "String that marks the form at the start of a new file's autoload section.")
64
65 (defconst generate-autoload-section-trailer "\n;;;***\n"
66 "String which indicates the end of the section of autoloads for a file.")
67
68 (defconst generate-autoload-section-continuation ";;;;;; "
69 "String to add on each continuation of the section header form.")
70
71 (defvar autoload-modified-buffers) ;Dynamically scoped var.
72
73 (defun make-autoload (form file)
74 "Turn FORM into an autoload or defvar for source file FILE.
75 Returns nil if FORM is not a special autoload form (i.e. a function definition
76 or macro definition or a defcustom)."
77 (let ((car (car-safe form)) expand)
78 (cond
79 ;; For complex cases, try again on the macro-expansion.
80 ((and (memq car '(easy-mmode-define-global-mode define-global-minor-mode
81 define-globalized-minor-mode
82 easy-mmode-define-minor-mode define-minor-mode))
83 (setq expand (let ((load-file-name file)) (macroexpand form)))
84 (eq (car expand) 'progn)
85 (memq :autoload-end expand))
86 (let ((end (memq :autoload-end expand)))
87 ;; Cut-off anything after the :autoload-end marker.
88 (setcdr end nil)
89 (cons 'progn
90 (mapcar (lambda (form) (make-autoload form file))
91 (cdr expand)))))
92
93 ;; For special function-like operators, use the `autoload' function.
94 ((memq car '(defun define-skeleton defmacro define-derived-mode
95 define-compilation-mode define-generic-mode
96 easy-mmode-define-global-mode define-global-minor-mode
97 define-globalized-minor-mode
98 easy-mmode-define-minor-mode define-minor-mode
99 defun* defmacro*))
100 (let* ((macrop (memq car '(defmacro defmacro*)))
101 (name (nth 1 form))
102 (args (case car
103 ((defun defmacro defun* defmacro*) (nth 2 form))
104 ((define-skeleton) '(&optional str arg))
105 ((define-generic-mode define-derived-mode
106 define-compilation-mode) nil)
107 (t)))
108 (body (nthcdr (get car 'doc-string-elt) form))
109 (doc (if (stringp (car body)) (pop body))))
110 (when (listp args)
111 ;; Add the usage form at the end where describe-function-1
112 ;; can recover it.
113 (setq doc (help-add-fundoc-usage doc args)))
114 ;; `define-generic-mode' quotes the name, so take care of that
115 (list 'autoload (if (listp name) name (list 'quote name)) file doc
116 (or (and (memq car '(define-skeleton define-derived-mode
117 define-generic-mode
118 easy-mmode-define-global-mode
119 define-global-minor-mode
120 define-globalized-minor-mode
121 easy-mmode-define-minor-mode
122 define-minor-mode)) t)
123 (eq (car-safe (car body)) 'interactive))
124 (if macrop (list 'quote 'macro) nil))))
125
126 ;; Convert defcustom to less space-consuming data.
127 ((eq car 'defcustom)
128 (let ((varname (car-safe (cdr-safe form)))
129 (init (car-safe (cdr-safe (cdr-safe form))))
130 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form)))))
131 ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
132 )
133 `(progn
134 (defvar ,varname ,init ,doc)
135 (custom-autoload ',varname ,file
136 ,(condition-case nil
137 (null (cadr (memq :set form)))
138 (error nil))))))
139
140 ((eq car 'defgroup)
141 ;; In Emacs this is normally handled separately by cus-dep.el, but for
142 ;; third party packages, it can be convenient to explicitly autoload
143 ;; a group.
144 (let ((groupname (nth 1 form)))
145 `(let ((loads (get ',groupname 'custom-loads)))
146 (if (member ',file loads) nil
147 (put ',groupname 'custom-loads (cons ',file loads))))))
148
149 ;; nil here indicates that this is not a special autoload form.
150 (t nil))))
151
152 ;; Forms which have doc-strings which should be printed specially.
153 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
154 ;; the doc-string in FORM.
155 ;; Those properties are now set in lisp-mode.el.
156
157 (defun autoload-generated-file ()
158 (expand-file-name generated-autoload-file
159 (expand-file-name "lisp"
160 source-directory)))
161
162 (defun autoload-read-section-header ()
163 "Read a section header form.
164 Since continuation lines have been marked as comments,
165 we must copy the text of the form and remove those comment
166 markers before we call `read'."
167 (save-match-data
168 (let ((beginning (point))
169 string)
170 (forward-line 1)
171 (while (looking-at generate-autoload-section-continuation)
172 (forward-line 1))
173 (setq string (buffer-substring beginning (point)))
174 (with-current-buffer (get-buffer-create " *autoload*")
175 (erase-buffer)
176 (insert string)
177 (goto-char (point-min))
178 (while (search-forward generate-autoload-section-continuation nil t)
179 (replace-match " "))
180 (goto-char (point-min))
181 (read (current-buffer))))))
182
183 (defvar autoload-print-form-outbuf nil
184 "Buffer which gets the output of `autoload-print-form'.")
185
186 (defun autoload-print-form (form)
187 "Print FORM such that `make-docfile' will find the docstrings.
188 The variable `autoload-print-form-outbuf' specifies the buffer to
189 put the output in."
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 (outbuf autoload-print-form-outbuf))
198 (if (and doc-string-elt (stringp (nth doc-string-elt form)))
199 ;; We need to hack the printing because the
200 ;; doc-string must be printed specially for
201 ;; make-docfile (sigh).
202 (let* ((p (nthcdr (1- doc-string-elt) form))
203 (elt (cdr p)))
204 (setcdr p nil)
205 (princ "\n(" outbuf)
206 (let ((print-escape-newlines t)
207 (print-escape-nonascii t))
208 (dolist (elt form)
209 (prin1 elt outbuf)
210 (princ " " outbuf)))
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 (re-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
233 (defun autoload-ensure-default-file (file)
234 "Make sure that the autoload file FILE exists and if not create it."
235 (unless (file-exists-p file)
236 (write-region
237 (concat ";;; " (file-name-nondirectory file)
238 " --- automatically extracted autoloads\n"
239 ";;\n"
240 ";;; Code:\n\n"
241 "\f\n;; Local Variables:\n"
242 ";; version-control: never\n"
243 ";; no-byte-compile: t\n"
244 ";; no-update-autoloads: t\n"
245 ";; End:\n"
246 ";;; " (file-name-nondirectory file)
247 " ends here\n")
248 nil file))
249 file)
250
251 (defun autoload-insert-section-header (outbuf autoloads load-name file time)
252 "Insert the section-header line,
253 which lists the file name and which functions are in it, etc."
254 (insert generate-autoload-section-header)
255 (prin1 (list 'autoloads autoloads load-name file time)
256 outbuf)
257 (terpri outbuf)
258 ;; Break that line at spaces, to avoid very long lines.
259 ;; Make each sub-line into a comment.
260 (with-current-buffer outbuf
261 (save-excursion
262 (forward-line -1)
263 (while (not (eolp))
264 (move-to-column 64)
265 (skip-chars-forward "^ \n")
266 (or (eolp)
267 (insert "\n" generate-autoload-section-continuation))))))
268
269 (defun autoload-find-file (file)
270 "Fetch file and put it in a temp buffer. Return the buffer."
271 ;; It is faster to avoid visiting the file.
272 (setq file (expand-file-name file))
273 (with-current-buffer (get-buffer-create " *autoload-file*")
274 (kill-all-local-variables)
275 (erase-buffer)
276 (setq buffer-undo-list t
277 buffer-read-only nil)
278 (emacs-lisp-mode)
279 (setq default-directory (file-name-directory file))
280 (insert-file-contents file nil)
281 (let ((enable-local-variables :safe))
282 (hack-local-variables))
283 (current-buffer)))
284
285 (defvar no-update-autoloads nil
286 "File local variable to prevent scanning this file for autoload cookies.")
287
288 (defun autoload-file-load-name (file)
289 (let ((name (file-name-nondirectory file)))
290 (if (string-match "\\.elc?\\(\\.\\|\\'\\)" name)
291 (substring name 0 (match-beginning 0))
292 name)))
293
294 (defun generate-file-autoloads (file)
295 "Insert at point a loaddefs autoload section for FILE.
296 Autoloads are generated for defuns and defmacros in FILE
297 marked by `generate-autoload-cookie' (which see).
298 If FILE is being visited in a buffer, the contents of the buffer
299 are used.
300 Return non-nil in the case where no autoloads were added at point."
301 (interactive "fGenerate autoloads for file: ")
302 (autoload-generate-file-autoloads file (current-buffer)))
303
304 (defun autoload-generate-file-autoloads (file &optional outbuf)
305 "Insert an autoload section for FILE in the appropriate buffer.
306 Autoloads are generated for defuns and defmacros in FILE
307 marked by `generate-autoload-cookie' (which see).
308 If FILE is being visited in a buffer, the contents of the buffer are used.
309 OUTBUF is the buffer in which the autoload statements will be inserted.
310 If OUTBUF is nil, it will be determined by `autoload-generated-file'.
311
312 Return non-nil iff FILE adds no autoloads to OUTBUF."
313 (catch 'done
314 (let ((autoloads-done '())
315 (load-name (autoload-file-load-name file))
316 (print-length nil)
317 (print-readably t) ; This does something in Lucid Emacs.
318 (float-output-format nil)
319 (visited (get-file-buffer file))
320 (absfile (expand-file-name file))
321 relfile
322 ;; nil until we found a cookie.
323 output-start)
324
325 (with-current-buffer (or visited
326 ;; It is faster to avoid visiting the file.
327 (autoload-find-file file))
328 ;; Obey the no-update-autoloads file local variable.
329 (unless no-update-autoloads
330 (message "Generating autoloads for %s..." file)
331 (save-excursion
332 (save-restriction
333 (widen)
334 (goto-char (point-min))
335 (while (not (eobp))
336 (skip-chars-forward " \t\n\f")
337 (cond
338 ((looking-at (regexp-quote generate-autoload-cookie))
339 ;; If not done yet, figure out where to insert this text.
340 (unless output-start
341 (unless outbuf
342 (setq outbuf (autoload-find-destination absfile))
343 (unless outbuf
344 ;; The file has autoload cookies, but they're
345 ;; already up-to-date.
346 (throw 'done t)))
347 (with-current-buffer outbuf
348 (setq relfile (file-relative-name absfile))
349 (setq output-start (point)))
350 ;; (message "file=%S, relfile=%S, dest=%S"
351 ;; file relfile (autoload-generated-file))
352 )
353 (search-forward generate-autoload-cookie)
354 (skip-chars-forward " \t")
355 (if (eolp)
356 (condition-case err
357 ;; Read the next form and make an autoload.
358 (let* ((form (prog1 (read (current-buffer))
359 (or (bolp) (forward-line 1))))
360 (autoload (make-autoload form load-name)))
361 (if autoload
362 (push (nth 1 form) autoloads-done)
363 (setq autoload form))
364 (let ((autoload-print-form-outbuf outbuf))
365 (autoload-print-form autoload)))
366 (error
367 (message "Error in %s: %S" file err)))
368
369 ;; Copy the rest of the line to the output.
370 (princ (buffer-substring
371 (progn
372 ;; Back up over whitespace, to preserve it.
373 (skip-chars-backward " \f\t")
374 (if (= (char-after (1+ (point))) ? )
375 ;; Eat one space.
376 (forward-char 1))
377 (point))
378 (progn (forward-line 1) (point)))
379 outbuf)))
380 ((looking-at ";")
381 ;; Don't read the comment.
382 (forward-line 1))
383 (t
384 (forward-sexp 1)
385 (forward-line 1))))))
386
387 (when output-start
388 (with-current-buffer outbuf
389 (save-excursion
390 ;; Insert the section-header line which lists the file name
391 ;; and which functions are in it, etc.
392 (goto-char output-start)
393 (autoload-insert-section-header
394 outbuf autoloads-done load-name relfile
395 (nth 5 (file-attributes relfile)))
396 (insert ";;; Generated autoloads from " relfile "\n"))
397 (insert generate-autoload-section-trailer)))
398 (message "Generating autoloads for %s...done" file))
399 (or visited
400 ;; We created this buffer, so we should kill it.
401 (kill-buffer (current-buffer))))
402 (not output-start))))
403 \f
404 (defun autoload-save-buffers ()
405 (while autoload-modified-buffers
406 (with-current-buffer (pop autoload-modified-buffers)
407 (save-buffer))))
408
409 ;;;###autoload
410 (defun update-file-autoloads (file &optional save-after)
411 "Update the autoloads for FILE in `generated-autoload-file'
412 \(which FILE might bind in its local variables).
413 If SAVE-AFTER is non-nil (which is always, when called interactively),
414 save the buffer too.
415
416 Return FILE if there was no autoload cookie in it, else nil."
417 (interactive "fUpdate autoloads for file: \np")
418 (let* ((autoload-modified-buffers nil)
419 (no-autoloads (autoload-generate-file-autoloads file)))
420 (if autoload-modified-buffers
421 (if save-after (autoload-save-buffers))
422 (if (interactive-p)
423 (message "Autoload section for %s is up to date." file)))
424 (if no-autoloads file)))
425
426 (defun autoload-find-destination (file)
427 "Find the destination point of the current buffer's autoloads.
428 FILE is the file name of the current buffer.
429 Returns a buffer whose point is placed at the requested location.
430 Returns nil if the file's autoloads are uptodate, otherwise
431 removes any prior now out-of-date autoload entries.
432 The current buffer only matters if it is visiting a file or if it has a buffer-local
433 value for some variables such as `generated-autoload-file', so it's OK
434 to call it from a dummy buffer if FILE is not currently visited."
435 (catch 'up-to-date
436 (let ((load-name (autoload-file-load-name file))
437 (existing-buffer (if buffer-file-name (current-buffer)))
438 (found nil))
439 (with-current-buffer
440 ;; We must read/write the file without any code conversion,
441 ;; but still decode EOLs.
442 (let ((coding-system-for-read 'raw-text))
443 (find-file-noselect
444 (autoload-ensure-default-file (autoload-generated-file))))
445 ;; This is to make generated-autoload-file have Unix EOLs, so
446 ;; that it is portable to all platforms.
447 (setq buffer-file-coding-system 'raw-text-unix)
448 (or (> (buffer-size) 0)
449 (error "Autoloads file %s does not exist" buffer-file-name))
450 (or (file-writable-p buffer-file-name)
451 (error "Autoloads file %s is not writable" buffer-file-name))
452 (widen)
453 (goto-char (point-min))
454 ;; Look for the section for LOAD-NAME.
455 (while (and (not found)
456 (search-forward generate-autoload-section-header nil t))
457 (let ((form (autoload-read-section-header)))
458 (cond ((string= (nth 2 form) load-name)
459 ;; We found the section for this file.
460 ;; Check if it is up to date.
461 (let ((begin (match-beginning 0))
462 (last-time (nth 4 form))
463 (file-time (nth 5 (file-attributes file))))
464 (if (and (or (null existing-buffer)
465 (not (buffer-modified-p existing-buffer)))
466 (listp last-time) (= (length last-time) 2)
467 (not (time-less-p last-time file-time)))
468 (throw 'up-to-date nil)
469 (autoload-remove-section begin)
470 (setq found t))))
471 ((string< load-name (nth 2 form))
472 ;; We've come to a section alphabetically later than
473 ;; LOAD-NAME. We assume the file is in order and so
474 ;; there must be no section for LOAD-NAME. We will
475 ;; insert one before the section here.
476 (goto-char (match-beginning 0))
477 (setq found t)))))
478 (or found
479 (progn
480 ;; No later sections in the file. Put before the last page.
481 (goto-char (point-max))
482 (search-backward "\f" nil t)))
483 (unless (memq (current-buffer) autoload-modified-buffers)
484 (push (current-buffer) autoload-modified-buffers))
485 (current-buffer)))))
486
487 (defun autoload-remove-section (begin)
488 (goto-char begin)
489 (search-forward generate-autoload-section-trailer)
490 (delete-region begin (point)))
491
492 ;;;###autoload
493 (defun update-directory-autoloads (&rest dirs)
494 "\
495 Update loaddefs.el with all the current autoloads from DIRS, and no old ones.
496 This uses `update-file-autoloads' (which see) to do its work.
497 In an interactive call, you must give one argument, the name
498 of a single directory. In a call from Lisp, you can supply multiple
499 directories as separate arguments, but this usage is discouraged.
500
501 The function does NOT recursively descend into subdirectories of the
502 directory or directories specified."
503 (interactive "DUpdate autoloads from directory: ")
504 (let* ((files-re (let ((tmp nil))
505 (dolist (suf (get-load-suffixes)
506 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
507 (unless (string-match "\\.elc" suf) (push suf tmp)))))
508 (files (apply 'nconc
509 (mapcar (lambda (dir)
510 (directory-files (expand-file-name dir)
511 t files-re))
512 dirs)))
513 (this-time (current-time))
514 (no-autoloads nil) ;files with no autoload cookies.
515 (autoloads-file (autoload-generated-file))
516 (top-dir (file-name-directory autoloads-file)))
517
518 (with-current-buffer
519 (find-file-noselect (autoload-ensure-default-file autoloads-file))
520 (save-excursion
521
522 ;; Canonicalize file names and remove the autoload file itself.
523 (setq files (delete (file-relative-name buffer-file-name)
524 (mapcar 'file-relative-name files)))
525
526 (goto-char (point-min))
527 (while (search-forward generate-autoload-section-header nil t)
528 (let* ((form (autoload-read-section-header))
529 (file (nth 3 form)))
530 (cond ((and (consp file) (stringp (car file)))
531 ;; This is a list of files that have no autoload cookies.
532 ;; There shouldn't be more than one such entry.
533 ;; Remove the obsolete section.
534 (autoload-remove-section (match-beginning 0))
535 (let ((last-time (nth 4 form)))
536 (dolist (file file)
537 (let ((file-time (nth 5 (file-attributes file))))
538 (when (and file-time
539 (not (time-less-p last-time file-time)))
540 ;; file unchanged
541 (push file no-autoloads)
542 (setq files (delete file files)))))))
543 ((not (stringp file)))
544 ((not (file-exists-p (expand-file-name file top-dir)))
545 ;; Remove the obsolete section.
546 (autoload-remove-section (match-beginning 0)))
547 ((equal (nth 4 form) (nth 5 (file-attributes file)))
548 ;; File hasn't changed.
549 nil)
550 (t
551 (update-file-autoloads file)))
552 (setq files (delete file files)))))
553 ;; Elements remaining in FILES have no existing autoload sections yet.
554 (setq no-autoloads
555 (append no-autoloads
556 (delq nil (mapcar 'update-file-autoloads files))))
557 (when no-autoloads
558 ;; Sort them for better readability.
559 (setq no-autoloads (sort no-autoloads 'string<))
560 ;; Add the `no-autoloads' section.
561 (goto-char (point-max))
562 (search-backward "\f" nil t)
563 (autoload-insert-section-header
564 (current-buffer) nil nil no-autoloads this-time)
565 (insert generate-autoload-section-trailer))
566
567 (save-buffer))))
568
569 (define-obsolete-function-alias 'update-autoloads-from-directories
570 'update-directory-autoloads "22.1")
571
572 ;;;###autoload
573 (defun batch-update-autoloads ()
574 "Update loaddefs.el autoloads in batch mode.
575 Calls `update-directory-autoloads' on the command line arguments."
576 (apply 'update-directory-autoloads command-line-args-left)
577 (setq command-line-args-left nil))
578
579 (provide 'autoload)
580
581 ;; arch-tag: 00244766-98f4-4767-bf42-8a22103441c6
582 ;;; autoload.el ends here