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, 2000 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 a definition generator FORM into an autoload for source file FILE.
44 Returns nil if FORM is not a defun, define-skeleton, define-derived-mode,
46 (let ((car (car-safe form)))
47 (if (memq car '(defun define-skeleton defmacro define-derived-mode))
48 (let ((macrop (eq car 'defmacro))
52 ;; Ignore the arguments.
53 form (cdr (cond ((eq car 'define-skeleton)
55 ((eq car 'define-derived-mode)
61 (setq form (cdr form))
63 (list 'autoload (list 'quote name) file doc
64 (or (eq car 'define-skeleton)
65 (eq car 'define-derived-mode)
66 (eq (car-safe (car form)) 'interactive))
67 (if macrop (list 'quote 'macro) nil)))
70 (defvar generate-autoload-cookie ";;;###autoload"
71 "Magic comment indicating the following form should be autoloaded.
72 Used by `update-file-autoloads'. This string should be
73 meaningless to Lisp (e.g., a comment).
78 \(defun function-to-be-autoloaded () ...)
80 If this string appears alone on a line, the following form will be
81 read and an autoload made for it. If it is followed by the string
82 \"immediate\", then the form on the following line will be copied
83 verbatim. If there is further text on the line, that text will be
84 copied verbatim to `generated-autoload-file'.")
86 (defvar generate-autoload-section-header "\f\n;;;### "
87 "String inserted before the form identifying
88 the section of autoloads for a file.")
90 (defvar generate-autoload-section-trailer "\n;;;***\n"
91 "String which indicates the end of the section of autoloads for a file.")
93 (defvar autoload-package-name nil)
95 ;;; Forms which have doc-strings which should be printed specially.
96 ;;; A doc-string-elt property of ELT says that (nth ELT FORM) is
97 ;;; the doc-string in FORM.
99 ;;; There used to be the following note here:
100 ;;; ;;; Note: defconst and defvar should NOT be marked in this way.
101 ;;; ;;; We don't want to produce defconsts and defvars that
102 ;;; ;;; make-docfile can grok, because then it would grok them twice,
103 ;;; ;;; once in foo.el (where they are given with ;;;###autoload) and
104 ;;; ;;; once in loaddefs.el.
106 ;;; Counter-note: Yes, they should be marked in this way.
107 ;;; make-docfile only processes those files that are loaded into the
108 ;;; dumped Emacs, and those files should never have anything
109 ;;; autoloaded here. The above-feared problem only occurs with files
110 ;;; which have autoloaded entries *and* are processed by make-docfile;
111 ;;; there should be no such files.
113 (put 'autoload 'doc-string-elt 3)
114 (put 'defun 'doc-string-elt 3)
115 (put 'defvar 'doc-string-elt 3)
116 (put 'defconst 'doc-string-elt 3)
117 (put 'defmacro 'doc-string-elt 3)
118 (put 'define-skeleton 'doc-string-elt 3)
119 (put 'define-derived-mode 'doc-string-elt 4)
121 (defun autoload-trim-file-name (file)
122 "Returns a relative pathname of FILE including the last directory."
123 (setq file (expand-file-name file))
125 (file-relative-name file (file-name-directory
127 (file-name-directory file))))
131 (defun generate-file-autoloads (file &optional funlist)
132 "Insert at point a loaddefs autoload section for FILE.
133 autoloads are generated for defuns and defmacros in FILE
134 marked by `generate-autoload-cookie' (which see).
135 If FILE is being visited in a buffer, the contents of the buffer
137 (interactive "fGenerate autoloads for file: ")
138 (generate-file-autoloads-1 file funlist))
140 (defun* generate-file-autoloads-1 (file funlist)
141 "Insert at point a loaddefs autoload section for FILE.
142 autoloads are generated for defuns and defmacros in FILE
143 marked by `generate-autoload-cookie' (which see).
144 If FILE is being visited in a buffer, the contents of the buffer
146 (let ((outbuf (current-buffer))
148 (load-name (replace-in-string (file-name-nondirectory file)
151 (trim-name (autoload-trim-file-name file))
152 (dofiles (not (null funlist)))
154 (print-readably t) ; XEmacs
155 (float-output-format nil)
157 (visited (get-file-buffer file))
160 ;; If the autoload section we create here uses an absolute
161 ;; pathname for FILE in its header, and then Emacs is installed
162 ;; under a different path on another system,
163 ;; `update-autoloads-here' won't be able to find the files to be
164 ;; autoloaded. So, if FILE is in the same directory or a
165 ;; subdirectory of the current buffer's directory, we'll make it
166 ;; relative to the current buffer's directory.
167 (setq file (expand-file-name file))
172 (let ((find-file-hooks nil)
173 (enable-local-variables nil))
174 (set-buffer (or visited (find-file-noselect file)))
175 (set-syntax-table emacs-lisp-mode-syntax-table))
179 (goto-char (point-min))
180 (unless (search-forward generate-autoload-cookie nil t)
181 (message "No autoloads found in %s" trim-name)
182 (return-from generate-file-autoloads-1))
184 (message "Generating autoloads for %s..." trim-name)
185 (goto-char (point-min))
186 (while (if dofiles funlist (not (eobp)))
188 (skip-chars-forward " \t\n\f")
189 (goto-char (point-min))
191 (concat "(def\\(un\\|var\\|const\\|macro\\) "
192 (regexp-quote (symbol-name (car funlist)))
194 (goto-char (match-beginning 0)))
197 (looking-at (regexp-quote generate-autoload-cookie)))
200 (search-forward generate-autoload-cookie)
201 (skip-chars-forward " \t"))
203 (if (or dofiles (eolp))
204 ;; Read the next form and make an autoload.
205 (let* ((form (prog1 (read (current-buffer))
206 (or (bolp) (forward-line 1))))
207 (autoload (make-autoload form load-name))
208 (doc-string-elt (get (car-safe form)
211 (setq autoloads-done (cons (nth 1 form)
213 (setq autoload form))
214 (if (and doc-string-elt
215 (stringp (nth doc-string-elt autoload)))
216 ;; We need to hack the printing because the
217 ;; doc-string must be printed specially for
218 ;; make-docfile (sigh).
219 (let* ((p (nthcdr (1- doc-string-elt)
224 ;; XEmacs change: don't let ^^L's get into
225 ;; the file or sorting is hard.
226 (let ((print-escape-newlines t)
231 (mapcar (function (lambda (elt)
237 (setq p2 (point-marker))
240 (while (search-forward "\^L" p2 t)
245 (princ "\"\\\n" outbuf)
246 (let ((begin (save-excursion
250 (prin1-to-string (car elt)) 1)
252 ;; Insert a backslash before each ( that
253 ;; appears at the beginning of a line in
258 (while (search-backward "\n(" begin t)
265 (prin1-to-string (cdr elt))
269 ;; XEmacs change: another fucking ^L hack
270 (let ((p (save-excursion
273 (print-escape-newlines t)
275 (print autoload outbuf)
278 (setq p2 (point-marker))
281 (while (search-forward "\^L" p2 t)
287 ;; Copy the rest of the line to the output.
288 (let ((begin (point)))
290 (cond ((looking-at "immediate\\s *$") ; XEmacs
291 ;; This is here so that you can automatically
292 ;; have small hook functions copied to
293 ;; loaddefs.el so that it's not necessary to
294 ;; load a whole file just to get a two-line
295 ;; do-nothing find-file-hook... --Stig
302 (princ (buffer-substring begin (point)) outbuf))))
304 ;; Don't read the comment.
310 (setq funlist (cdr funlist)))))))
312 ;; We created this buffer, so we should kill it.
313 (kill-buffer (current-buffer)))
315 (setq output-end (point-marker))))
317 ;; XEmacs -- always do this so that we cache the information
318 ;; that we've processed the file already.
320 (insert generate-autoload-section-header)
321 (prin1 (list 'autoloads autoloads-done load-name trim-name)
324 ;;;; (insert ";;; Generated autoloads from "
325 ;;;; (autoload-trim-file-name file) "\n")
326 ;; Warn if we put a line in loaddefs.el
327 ;; that is long enough to cause trouble.
328 (when (< output-end (point))
329 (setq output-end (point-marker)))
330 (while (< (point) output-end)
331 ;; (let ((beg (point)))
333 ;; Emacs -- I still haven't figured this one out.
334 ;; (if (> (- (point) beg) 900)
336 ;; (message "A line is too long--over 900 characters")
338 ;; (goto-char output-end)))
341 (goto-char output-end)
342 (insert generate-autoload-section-trailer)))
343 (or noninteractive ; XEmacs: only need one line in -batch mode.
344 (message "Generating autoloads for %s...done" file))))
347 (defconst autoload-file-name "auto-autoloads.el"
348 "Generic filename to put autoloads into.
349 Unless you are an XEmacs maintainer, it is probably unwise to change this.")
351 (defvar autoload-target-directory "../lisp/"
352 "Directory to put autoload declaration file into.
353 Unless you know what you're doing, don't mess with this.")
355 (defvar generated-autoload-file
356 (expand-file-name (concat autoload-target-directory
359 "*File `update-file-autoloads' puts autoloads into.
360 A .el file can set this in its local variables section to make its
361 autoloads go somewhere else.
363 Note that `batch-update-directory' binds this variable to its own value,
364 generally the file named `autoload-file-name' in the directory being
367 (defconst cusload-file-name "custom-load.el"
368 "Generic filename to put custom loads into.
369 Unless you are an XEmacs maintainer, it is probably unwise to change this.")
372 (defun update-file-autoloads (file)
373 "Update the autoloads for FILE in `generated-autoload-file'
374 \(which FILE might bind in its local variables).
375 This function refuses to update autoloads files."
376 (interactive "fUpdate autoloads for file: ")
377 (setq file (expand-file-name file))
378 (when (and (file-newer-than-file-p file generated-autoload-file)
379 (not (member (file-name-nondirectory file)
380 (list autoload-file-name))))
382 (let ((load-name (replace-in-string (file-name-nondirectory file)
385 (trim-name (autoload-trim-file-name file))
388 (let ((find-file-hooks nil))
389 (set-buffer (or (get-file-buffer generated-autoload-file)
390 (find-file-noselect generated-autoload-file))))
391 ;; Make sure we can scribble in it.
392 (setq buffer-read-only nil)
393 ;; First delete all sections for this file.
394 (goto-char (point-min))
395 (while (search-forward generate-autoload-section-header nil t)
396 (setq section-begin (match-beginning 0))
397 (setq form (read (current-buffer)))
398 (when (string= (nth 2 form) load-name)
399 (search-forward generate-autoload-section-trailer)
400 (delete-region section-begin (point))))
402 ;; Now find insertion point for new section
403 (block find-insertion-point
404 (goto-char (point-min))
405 (while (search-forward generate-autoload-section-header nil t)
406 (setq form (read (current-buffer)))
407 (when (string< trim-name (nth 3 form))
408 ;; Found alphabetically correct insertion point
409 (goto-char (match-beginning 0))
410 (return-from find-insertion-point))
411 (search-forward generate-autoload-section-trailer))
412 (when (eq (point) (point-min)) ; No existing entries?
413 (goto-char (point-max)))) ; Append.
415 ;; Add in new sections for file
416 (generate-file-autoloads file))
418 (when (interactive-p) (save-buffer)))))
421 (defun update-autoloads-here ()
422 "Update sections of the current buffer generated by `update-file-autoloads'."
424 (let ((generated-autoload-file (buffer-file-name)))
426 (goto-char (point-min))
427 (while (search-forward generate-autoload-section-header nil t)
428 (let* ((form (condition-case ()
429 (read (current-buffer))
432 ;; XEmacs change: if we can't find the file as specified, look
433 ;; around a bit more.
434 (cond ((and (stringp file)
435 (or (get-file-buffer file)
436 (file-exists-p file))))
439 (let ((loc (locate-file (file-name-nondirectory file)
443 (setq loc (expand-file-name
444 (autoload-trim-file-name loc)
446 (if (or (get-file-buffer loc)
454 "Can't find library `%s'; remove its autoloads? "
459 (format "Find `%s' load file: "
464 (let ((begin (match-beginning 0)))
465 (search-forward generate-autoload-section-trailer)
466 (delete-region begin (point))))
468 (generate-file-autoloads file)))))))
471 (defun update-autoloads-from-directory (dir)
472 "Update `generated-autoload-file' with all the current autoloads from DIR.
473 This runs `update-file-autoloads' on each .el file in DIR.
474 Obsolete autoload entries for files that no longer exist are deleted.
475 Note that, if this function is called from `batch-update-directory',
476 `generated-autoload-file' was rebound in that function."
477 (interactive "DUpdate autoloads for directory: ")
478 (setq dir (expand-file-name dir))
479 (let ((simple-dir (file-name-as-directory
480 (file-name-nondirectory
481 (directory-file-name dir))))
482 (enable-local-eval nil))
484 (let ((find-file-hooks nil))
485 (set-buffer (find-file-noselect generated-autoload-file)))
486 (goto-char (point-min))
487 (while (search-forward generate-autoload-section-header nil t)
488 (let* ((begin (match-beginning 0))
489 (form (condition-case ()
490 (read (current-buffer))
493 (when (and (stringp file)
494 (string= (file-name-directory file) simple-dir)
497 (file-name-nondirectory file) dir))))
498 ;; Remove the obsolete section.
499 (search-forward generate-autoload-section-trailer)
500 (delete-region begin (point)))))
501 ;; Update or create autoload sections for existing files.
502 (mapcar 'update-file-autoloads (directory-files dir t "^[^=].*\\.el$"))
503 (unless noninteractive
507 (defun batch-update-autoloads ()
508 "Update the autoloads for the files or directories on the command line.
509 Runs `update-file-autoloads' on files and `update-directory-autoloads'
510 on directories. Must be used only with -batch, and kills Emacs on completion.
511 Each file will be processed even if an error occurred previously.
512 For example, invoke `xemacs -batch -f batch-update-autoloads *.el'.
513 The directory to which the auto-autoloads.el file must be the first parameter
514 on the command line."
515 (unless noninteractive
516 (error "batch-update-autoloads is to be used only with -batch"))
517 (let ((defdir (directory-file-name default-directory))
518 (enable-local-eval nil)) ; Don't query in batch mode.
519 ;; (message "Updating autoloads in %s..." generated-autoload-file)
520 (dolist (arg command-line-args-left)
521 (setq arg (expand-file-name arg defdir))
523 ((file-directory-p arg)
524 (message "Updating autoloads for directory %s..." arg)
525 (update-autoloads-from-directory arg))
527 (update-file-autoloads arg))
528 (t (error "No such file or directory: %s" arg))))
529 (fixup-autoload-buffer (concat (if autoload-package-name
530 autoload-package-name
531 (file-name-nondirectory defdir))
533 (save-some-buffers t)
537 (defun fixup-autoload-buffer (sym)
539 (set-buffer (find-file-noselect generated-autoload-file))
540 (goto-char (point-min))
541 (if (and (not (= (point-min) (point-max)))
542 (not (looking-at ";;; DO NOT MODIFY THIS FILE")))
544 (insert ";;; DO NOT MODIFY THIS FILE\n")
545 (insert "(if (featurep '" sym ")")
546 (insert " (error \"Already loaded\"))\n")
547 (goto-char (point-max))
548 (insert "\n(provide '" sym ")\n")))))
550 (defvar autoload-package-name nil)
552 ;; #### this function is almost identical to, but subtly different from,
553 ;; batch-update-autoloads. Both of these functions, unfortunately, are
554 ;; used in various build scripts in xemacs-packages. They should be
555 ;; merged. (However, it looks like no scripts pass more than one arg,
556 ;; making merging easy.) --ben
559 (defun batch-update-directory ()
560 "Update the autoloads for the directories on the command line.
561 Runs `update-file-autoloads' on each file in the given directory, and must
562 be used only with -batch."
563 (unless noninteractive
564 (error "batch-update-directory is to be used only with -batch"))
565 (let ((defdir default-directory)
566 (enable-local-eval nil)) ; Don't query in batch mode.
567 (dolist (arg command-line-args-left)
568 (setq arg (expand-file-name arg defdir))
569 (let ((generated-autoload-file (expand-file-name autoload-file-name
572 ((file-directory-p arg)
573 (message "Updating autoloads in directory %s..." arg)
574 (update-autoloads-from-directory arg))
575 (t (error "No such file or directory: %s" arg)))
576 (fixup-autoload-buffer (concat (if autoload-package-name
577 autoload-package-name
578 (file-name-nondirectory arg))
580 (save-some-buffers t))
584 (setq command-line-args-left nil)))
586 ;; #### i created the following. this one and the last should be merged into
587 ;; batch-update-autoloads and batch-update-one-directory. --ben
590 (defun batch-update-one-directory ()
591 "Update the autoloads for a single directory on the command line.
592 Runs `update-file-autoloads' on each file in the given directory, and must
593 be used only with -batch."
594 (unless noninteractive
595 (error "batch-update-directory is to be used only with -batch"))
596 (let ((defdir default-directory)
597 (enable-local-eval nil)) ; Don't query in batch mode.
598 (let ((arg (car command-line-args-left)))
599 (setq command-line-args-left (cdr command-line-args-left))
600 (setq arg (expand-file-name arg defdir))
601 (let ((generated-autoload-file (expand-file-name autoload-file-name
604 ((file-directory-p arg)
605 (message "Updating autoloads in directory %s..." arg)
606 (update-autoloads-from-directory arg))
607 (t (error "No such file or directory: %s" arg)))
608 (fixup-autoload-buffer (concat (if autoload-package-name
609 autoload-package-name
610 (file-name-nondirectory arg))
612 (save-some-buffers t))
618 ;;; autoload.el ends here