1 ;;; autoload.el --- maintain autoloads in loaddefs.el.
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
5 ;; Copyright (C) 1996 Ben Wing.
7 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
10 ;; This file is part of XEmacs.
12 ;; XEmacs is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; XEmacs is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with XEmacs; see the file COPYING. If not, write to the Free
24 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 ;;; Synched up with: Not synched with FSF.
31 ;; This code helps GNU Emacs maintainers keep the loaddefs.el file up to
32 ;; date. It interprets magic cookies of the form ";;;###autoload" in
33 ;; lisp source files in various useful ways. To learn more, read the
34 ;; source; if you're going to use this, you'd better be able to.
38 ;; Sep-26-1997: slb removed code dealing with customization.
42 (defun make-autoload (form file)
43 "Turn FORM, a defun or defmacro, into an autoload for source file FILE.
44 Returns nil if FORM is not a defun, define-skeleton or defmacro."
45 (let ((car (car-safe form)))
46 (if (memq car '(defun define-skeleton defmacro))
47 (let ((macrop (eq car 'defmacro))
51 ;; Ignore the arguments.
52 form (cdr (if (eq car 'define-skeleton)
57 (setq form (cdr form))
59 (list 'autoload (list 'quote name) file doc
60 (or (eq car 'define-skeleton)
61 (eq (car-safe (car form)) 'interactive))
62 (if macrop (list 'quote 'macro) nil)))
65 (put 'define-skeleton 'doc-string-elt 3)
67 (defvar generate-autoload-cookie ";;;###autoload"
68 "Magic comment indicating the following form should be autoloaded.
69 Used by `update-file-autoloads'. This string should be
70 meaningless to Lisp (e.g., a comment).
75 \(defun function-to-be-autoloaded () ...)
77 If this string appears alone on a line, the following form will be
78 read and an autoload made for it. If it is followed by the string
79 \"immediate\", then the form on the following line will be copied
80 verbatim. If there is further text on the line, that text will be
81 copied verbatim to `generated-autoload-file'.")
83 (defvar generate-autoload-section-header "\f\n;;;### "
84 "String inserted before the form identifying
85 the section of autoloads for a file.")
87 (defvar generate-autoload-section-trailer "\n;;;***\n"
88 "String which indicates the end of the section of autoloads for a file.")
90 ;;; Forms which have doc-strings which should be printed specially.
91 ;;; A doc-string-elt property of ELT says that (nth ELT FORM) is
92 ;;; the doc-string in FORM.
94 ;;; There used to be the following note here:
95 ;;; ;;; Note: defconst and defvar should NOT be marked in this way.
96 ;;; ;;; We don't want to produce defconsts and defvars that
97 ;;; ;;; make-docfile can grok, because then it would grok them twice,
98 ;;; ;;; once in foo.el (where they are given with ;;;###autoload) and
99 ;;; ;;; once in loaddefs.el.
101 ;;; Counter-note: Yes, they should be marked in this way.
102 ;;; make-docfile only processes those files that are loaded into the
103 ;;; dumped Emacs, and those files should never have anything
104 ;;; autoloaded here. The above-feared problem only occurs with files
105 ;;; which have autoloaded entries *and* are processed by make-docfile;
106 ;;; there should be no such files.
108 (put 'autoload 'doc-string-elt 3)
109 (put 'defun 'doc-string-elt 3)
110 (put 'defvar 'doc-string-elt 3)
111 (put 'defconst 'doc-string-elt 3)
112 (put 'defmacro 'doc-string-elt 3)
114 (defun autoload-trim-file-name (file)
115 "Returns a relative pathname of FILE including the last directory."
116 (setq file (expand-file-name file))
118 (file-relative-name file (file-name-directory
120 (file-name-directory file))))
124 (defun generate-file-autoloads (file &optional funlist)
125 "Insert at point a loaddefs autoload section for FILE.
126 autoloads are generated for defuns and defmacros in FILE
127 marked by `generate-autoload-cookie' (which see).
128 If FILE is being visited in a buffer, the contents of the buffer
130 (interactive "fGenerate autoloads for file: ")
131 (generate-file-autoloads-1 file funlist))
133 (defun* generate-file-autoloads-1 (file funlist)
134 "Insert at point a loaddefs autoload section for FILE.
135 autoloads are generated for defuns and defmacros in FILE
136 marked by `generate-autoload-cookie' (which see).
137 If FILE is being visited in a buffer, the contents of the buffer
139 (let ((outbuf (current-buffer))
141 (load-name (replace-in-string (file-name-nondirectory file)
144 (trim-name (autoload-trim-file-name file))
145 (dofiles (not (null funlist)))
147 (print-readably t) ; XEmacs
148 (float-output-format nil)
150 (visited (get-file-buffer file))
153 ;; If the autoload section we create here uses an absolute
154 ;; pathname for FILE in its header, and then Emacs is installed
155 ;; under a different path on another system,
156 ;; `update-autoloads-here' won't be able to find the files to be
157 ;; autoloaded. So, if FILE is in the same directory or a
158 ;; subdirectory of the current buffer's directory, we'll make it
159 ;; relative to the current buffer's directory.
160 (setq file (expand-file-name file))
165 (let ((find-file-hooks nil)
166 (enable-local-variables nil))
167 (set-buffer (or visited (find-file-noselect file)))
168 (set-syntax-table lisp-mode-syntax-table))
172 (goto-char (point-min))
173 (unless (search-forward generate-autoload-cookie nil t)
174 (message "No autoloads found in %s" trim-name)
175 (return-from generate-file-autoloads-1))
177 (message "Generating autoloads for %s..." trim-name)
178 (goto-char (point-min))
179 (while (if dofiles funlist (not (eobp)))
181 (skip-chars-forward " \t\n\f")
182 (goto-char (point-min))
184 (concat "(def\\(un\\|var\\|const\\|macro\\) "
185 (regexp-quote (symbol-name (car funlist)))
187 (goto-char (match-beginning 0)))
190 (looking-at (regexp-quote generate-autoload-cookie)))
193 (search-forward generate-autoload-cookie)
194 (skip-chars-forward " \t"))
196 (if (or dofiles (eolp))
197 ;; Read the next form and make an autoload.
198 (let* ((form (prog1 (read (current-buffer))
199 (or (bolp) (forward-line 1))))
200 (autoload (make-autoload form load-name))
201 (doc-string-elt (get (car-safe form)
204 (setq autoloads-done (cons (nth 1 form)
206 (setq autoload form))
207 (if (and doc-string-elt
208 (stringp (nth doc-string-elt autoload)))
209 ;; We need to hack the printing because the
210 ;; doc-string must be printed specially for
211 ;; make-docfile (sigh).
212 (let* ((p (nthcdr (1- doc-string-elt)
217 ;; XEmacs change: don't let ^^L's get into
218 ;; the file or sorting is hard.
219 (let ((print-escape-newlines t)
224 (mapcar (function (lambda (elt)
230 (setq p2 (point-marker))
233 (while (search-forward "\^L" p2 t)
238 (princ "\"\\\n" outbuf)
239 (let ((begin (save-excursion
243 (prin1-to-string (car elt)) 1)
245 ;; Insert a backslash before each ( that
246 ;; appears at the beginning of a line in
251 (while (search-backward "\n(" begin t)
258 (prin1-to-string (cdr elt))
262 ;; XEmacs change: another fucking ^L hack
263 (let ((p (save-excursion
266 (print-escape-newlines t)
268 (print autoload outbuf)
271 (setq p2 (point-marker))
274 (while (search-forward "\^L" p2 t)
280 ;; Copy the rest of the line to the output.
281 (let ((begin (point)))
283 (cond ((looking-at "immediate\\s *$") ; XEmacs
284 ;; This is here so that you can automatically
285 ;; have small hook functions copied to
286 ;; loaddefs.el so that it's not necessary to
287 ;; load a whole file just to get a two-line
288 ;; do-nothing find-file-hook... --Stig
295 (princ (buffer-substring begin (point)) outbuf))))
297 ;; Don't read the comment.
303 (setq funlist (cdr funlist)))))))
305 ;; We created this buffer, so we should kill it.
306 (kill-buffer (current-buffer)))
308 (setq output-end (point-marker))))
310 ;; XEmacs -- always do this so that we cache the information
311 ;; that we've processed the file already.
313 (insert generate-autoload-section-header)
314 (prin1 (list 'autoloads autoloads-done load-name trim-name)
317 ;;;; (insert ";;; Generated autoloads from "
318 ;;;; (autoload-trim-file-name file) "\n")
319 ;; Warn if we put a line in loaddefs.el
320 ;; that is long enough to cause trouble.
321 (when (< output-end (point))
322 (setq output-end (point-marker)))
323 (while (< (point) output-end)
324 ;; (let ((beg (point)))
326 ;; Emacs -- I still haven't figured this one out.
327 ;; (if (> (- (point) beg) 900)
329 ;; (message "A line is too long--over 900 characters")
331 ;; (goto-char output-end)))
334 (goto-char output-end)
335 (insert generate-autoload-section-trailer)))
336 (or noninteractive ; XEmacs: only need one line in -batch mode.
337 (message "Generating autoloads for %s...done" file))))
340 (defconst autoload-file-name "auto-autoloads.el"
341 "Generic filename to put autoloads into.
342 Unless you are an XEmacs maintainer, it is probably unwise to change this.")
344 (defvar autoload-target-directory "../lisp/"
345 "Directory to put autoload declaration file into.
346 Unless you know what you're doing, don't mess with this.")
348 (defvar generated-autoload-file
349 (expand-file-name (concat autoload-target-directory
352 "*File `update-file-autoloads' puts autoloads into.
353 A .el file can set this in its local variables section to make its
354 autoloads go somewhere else.
356 Note that `batch-update-directory' binds this variable to its own value,
357 generally the file named `autoload-file-name' in the directory being
360 (defconst cusload-file-name "custom-load.el"
361 "Generic filename ot put custom loads into.
362 Unless you are an XEmacs maintainr, it is probably unwise to change this.")
365 (defun update-file-autoloads (file)
366 "Update the autoloads for FILE in `generated-autoload-file'
367 \(which FILE might bind in its local variables).
368 This function refuses to update autoloads files."
369 (interactive "fUpdate autoloads for file: ")
370 (setq file (expand-file-name file))
371 (when (and (file-newer-than-file-p file generated-autoload-file)
372 (not (member (file-name-nondirectory file)
373 (list autoload-file-name))))
375 (let ((load-name (replace-in-string (file-name-nondirectory file)
378 (trim-name (autoload-trim-file-name file))
381 (let ((find-file-hooks nil))
382 (set-buffer (or (get-file-buffer generated-autoload-file)
383 (find-file-noselect generated-autoload-file))))
384 ;; Make sure we can scribble in it.
385 (setq buffer-read-only nil)
386 ;; First delete all sections for this file.
387 (goto-char (point-min))
388 (while (search-forward generate-autoload-section-header nil t)
389 (setq section-begin (match-beginning 0))
390 (setq form (read (current-buffer)))
391 (when (string= (nth 2 form) load-name)
392 (search-forward generate-autoload-section-trailer)
393 (delete-region section-begin (point))))
395 ;; Now find insertion point for new section
396 (block find-insertion-point
397 (goto-char (point-min))
398 (while (search-forward generate-autoload-section-header nil t)
399 (setq form (read (current-buffer)))
400 (when (string< trim-name (nth 3 form))
401 ;; Found alphabetically correct insertion point
402 (goto-char (match-beginning 0))
403 (return-from find-insertion-point))
404 (search-forward generate-autoload-section-trailer))
405 (when (eq (point) (point-min)) ; No existing entries?
406 (goto-char (point-max)))) ; Append.
408 ;; Add in new sections for file
409 (generate-file-autoloads file))
411 (when (interactive-p) (save-buffer)))))
414 (defun update-autoloads-here ()
415 "Update sections of the current buffer generated by `update-file-autoloads'."
417 (let ((generated-autoload-file (buffer-file-name)))
419 (goto-char (point-min))
420 (while (search-forward generate-autoload-section-header nil t)
421 (let* ((form (condition-case ()
422 (read (current-buffer))
425 ;; XEmacs change: if we can't find the file as specified, look
426 ;; around a bit more.
427 (cond ((and (stringp file)
428 (or (get-file-buffer file)
429 (file-exists-p file))))
432 (let ((loc (locate-file (file-name-nondirectory file)
436 (setq loc (expand-file-name
437 (autoload-trim-file-name loc)
439 (if (or (get-file-buffer loc)
447 "Can't find library `%s'; remove its autoloads? "
452 (format "Find `%s' load file: "
457 (let ((begin (match-beginning 0)))
458 (search-forward generate-autoload-section-trailer)
459 (delete-region begin (point))))
461 (generate-file-autoloads file)))))))
464 (defun update-autoloads-from-directory (dir)
465 "Update `generated-autoload-file' with all the current autoloads from DIR.
466 This runs `update-file-autoloads' on each .el file in DIR.
467 Obsolete autoload entries for files that no longer exist are deleted.
468 Note that, if this function is called from `batch-update-directory',
469 `generated-autoload-file' was rebound in that function."
470 (interactive "DUpdate autoloads for directory: ")
471 (setq dir (expand-file-name dir))
472 (let ((simple-dir (file-name-as-directory
473 (file-name-nondirectory
474 (directory-file-name dir))))
475 (enable-local-eval nil))
477 (let ((find-file-hooks nil))
478 (set-buffer (find-file-noselect generated-autoload-file)))
479 (goto-char (point-min))
480 (while (search-forward generate-autoload-section-header nil t)
481 (let* ((begin (match-beginning 0))
482 (form (condition-case ()
483 (read (current-buffer))
486 (when (and (stringp file)
487 (string= (file-name-directory file) simple-dir)
490 (file-name-nondirectory file) dir))))
491 ;; Remove the obsolete section.
492 (search-forward generate-autoload-section-trailer)
493 (delete-region begin (point)))))
494 ;; Update or create autoload sections for existing files.
495 (mapcar 'update-file-autoloads (directory-files dir t "^[^=].*\\.el$"))
496 (unless noninteractive
500 (defun batch-update-autoloads ()
501 "Update the autoloads for the files or directories on the command line.
502 Runs `update-file-autoloads' on files and `update-directory-autoloads'
503 on directories. Must be used only with -batch, and kills Emacs on completion.
504 Each file will be processed even if an error occurred previously.
505 For example, invoke `xemacs -batch -f batch-update-autoloads *.el'.
506 The directory to which the auto-autoloads.el file must be the first parameter
507 on the command line."
508 (unless noninteractive
509 (error "batch-update-autoloads is to be used only with -batch"))
510 (let ((defdir default-directory)
511 (enable-local-eval nil)) ; Don't query in batch mode.
512 ;; (message "Updating autoloads in %s..." generated-autoload-file)
513 (dolist (arg command-line-args-left)
514 (setq arg (expand-file-name arg defdir))
516 ((file-directory-p arg)
517 (message "Updating autoloads for directory %s..." arg)
518 (update-autoloads-from-directory arg))
520 (update-file-autoloads arg))
521 (t (error "No such file or directory: %s" arg))))
522 (fixup-autoload-buffer (concat (if autoload-package-name
523 autoload-package-name
524 (file-name-nondirectory defdir))
526 (save-some-buffers t)
530 (defun fixup-autoload-buffer (sym)
532 (set-buffer (find-file-noselect generated-autoload-file))
533 (goto-char (point-min))
534 (if (and (not (= (point-min) (point-max)))
535 (not (looking-at ";;; DO NOT MODIFY THIS FILE")))
537 (insert ";;; DO NOT MODIFY THIS FILE\n")
538 (insert "(if (featurep '" sym ")")
539 (insert " (error \"Already loaded\"))\n")
540 (goto-char (point-max))
541 (insert "\n(provide '" sym ")\n")))))
543 (defvar autoload-package-name nil)
545 ;; #### this function is almost identical, but subtly different,
546 ;; from batch-update-autoloads. Steve, it's your responsibility to
547 ;; clean this up. The two should be merged, but I'm not sure what
548 ;; package-creation scripts out there might be using this. --ben
551 (defun batch-update-directory ()
552 "Update the autoloads for the directories on the command line.
553 Runs `update-file-autoloads' on each file in the given directory, and must
554 be used only with -batch."
555 (unless noninteractive
556 (error "batch-update-directory is to be used only with -batch"))
557 (let ((defdir default-directory)
558 (enable-local-eval nil)) ; Don't query in batch mode.
559 (dolist (arg command-line-args-left)
560 (setq arg (expand-file-name arg defdir))
561 (let ((generated-autoload-file (expand-file-name autoload-file-name
564 ((file-directory-p arg)
565 (message "Updating autoloads in directory %s..." arg)
566 (update-autoloads-from-directory arg))
567 (t (error "No such file or directory: %s" arg)))
568 (fixup-autoload-buffer (concat (if autoload-package-name
569 autoload-package-name
570 (file-name-nondirectory arg))
572 (save-some-buffers t))
576 (setq command-line-args-left nil)))
578 ;; #### i created the following. this one and the last should be merged into
579 ;; batch-update-autoloads. --ben
582 (defun batch-update-one-directory ()
583 "Update the autoloads for a single directory on the command line.
584 Runs `update-file-autoloads' on each file in the given directory, and must
585 be used only with -batch."
586 (unless noninteractive
587 (error "batch-update-directory is to be used only with -batch"))
588 (let ((defdir default-directory)
589 (enable-local-eval nil)) ; Don't query in batch mode.
590 (let ((arg (car command-line-args-left)))
591 (setq command-line-args-left (cdr command-line-args-left))
592 (setq arg (expand-file-name arg defdir))
593 (let ((generated-autoload-file (expand-file-name autoload-file-name
596 ((file-directory-p arg)
597 (message "Updating autoloads in directory %s..." arg)
598 (update-autoloads-from-directory arg))
599 (t (error "No such file or directory: %s" arg)))
600 (fixup-autoload-buffer (concat (if autoload-package-name
601 autoload-package-name
602 (file-name-nondirectory arg))
604 (save-some-buffers t))
610 ;;; autoload.el ends here