XEmacs 21.2.14.
[chise/xemacs-chise.git.1] / lisp / autoload.el
1 ;;; autoload.el --- maintain autoloads in loaddefs.el.
2
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.
6
7 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
8 ;; Keywords: maint
9
10 ;; This file is part of XEmacs.
11
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)
15 ;; any later version.
16
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.
21
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
25 ;; 02111-1307, USA.
26
27 ;;; Synched up with: Not synched with FSF.
28
29 ;;; Commentary:
30
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.
35
36 ;; ChangeLog:
37
38 ;; Sep-26-1997:  slb removed code dealing with customization.
39
40 ;;; Code:
41
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))
48               name doc)
49           (setq form (cdr form)
50                 name (car form)
51                 ;; Ignore the arguments.
52                 form (cdr (if (eq car 'define-skeleton)
53                               form
54                             (cdr form)))
55                 doc (car form))
56           (if (stringp doc)
57               (setq form (cdr form))
58             (setq doc nil))
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)))
63       nil)))
64
65 (put 'define-skeleton 'doc-string-elt 3)
66
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).
71
72 This string is used:
73
74 ;;;###autoload
75 \(defun function-to-be-autoloaded () ...)
76
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'.")
82
83 (defvar generate-autoload-section-header "\f\n;;;### "
84   "String inserted before the form identifying
85 the section of autoloads for a file.")
86
87 (defvar generate-autoload-section-trailer "\n;;;***\n"
88   "String which indicates the end of the section of autoloads for a file.")
89
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.
93 ;;;
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.
100 ;;;
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.
107
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)
113
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))
117   (file-relative-name file (file-name-directory
118                             (directory-file-name
119                              (file-name-directory file)))))
120
121 ;;;###autoload
122 (defun generate-file-autoloads (file &optional funlist)
123   "Insert at point a loaddefs autoload section for FILE.
124 autoloads are generated for defuns and defmacros in FILE
125 marked by `generate-autoload-cookie' (which see).
126 If FILE is being visited in a buffer, the contents of the buffer
127 are used."
128   (interactive "fGenerate autoloads for file: ")
129   (generate-file-autoloads-1 file funlist))
130
131 (defun* generate-file-autoloads-1 (file 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
136 are used."
137   (let ((outbuf (current-buffer))
138         (autoloads-done '())
139         (load-name (replace-in-string (file-name-nondirectory file)
140                                       "\\.elc?$"
141                                       ""))
142         (trim-name (autoload-trim-file-name file))
143         (dofiles (not (null funlist)))
144         (print-length nil)
145         (print-readably t) ; XEmacs
146         (float-output-format nil)
147         ;; (done-any nil)
148         (visited (get-file-buffer file))
149         output-end)
150
151     ;; If the autoload section we create here uses an absolute
152     ;; pathname for FILE in its header, and then Emacs is installed
153     ;; under a different path on another system,
154     ;; `update-autoloads-here' won't be able to find the files to be
155     ;; autoloaded.  So, if FILE is in the same directory or a
156     ;; subdirectory of the current buffer's directory, we'll make it
157     ;; relative to the current buffer's directory.
158     (setq file (expand-file-name file))
159
160     (save-excursion
161       (unwind-protect
162           (progn
163             (let ((find-file-hooks nil)
164                   (enable-local-variables nil))
165               (set-buffer (or visited (find-file-noselect file)))
166               (set-syntax-table lisp-mode-syntax-table))
167             (save-excursion
168               (save-restriction
169                 (widen)
170                 (goto-char (point-min))
171                 (unless (search-forward generate-autoload-cookie nil t)
172                   (message "No autoloads found in %s" trim-name)
173                   (return-from generate-file-autoloads-1))
174
175                 (message "Generating autoloads for %s..." trim-name)
176                 (goto-char (point-min))
177                 (while (if dofiles funlist (not (eobp)))
178                   (if (not dofiles)
179                       (skip-chars-forward " \t\n\f")
180                     (goto-char (point-min))
181                     (re-search-forward
182                      (concat "(def\\(un\\|var\\|const\\|macro\\) "
183                              (regexp-quote (symbol-name (car funlist)))
184                              "\\s "))
185                     (goto-char (match-beginning 0)))
186                   (cond
187                    ((or dofiles
188                         (looking-at (regexp-quote generate-autoload-cookie)))
189                     (if dofiles
190                         nil
191                       (search-forward generate-autoload-cookie)
192                       (skip-chars-forward " \t"))
193                     ;; (setq done-any t)
194                     (if (or dofiles (eolp))
195                         ;; Read the next form and make an autoload.
196                         (let* ((form (prog1 (read (current-buffer))
197                                        (or (bolp) (forward-line 1))))
198                                (autoload (make-autoload form load-name))
199                                (doc-string-elt (get (car-safe form)
200                                                     'doc-string-elt)))
201                           (if autoload
202                               (setq autoloads-done (cons (nth 1 form)
203                                                          autoloads-done))
204                             (setq autoload form))
205                           (if (and doc-string-elt
206                                    (stringp (nth doc-string-elt autoload)))
207                               ;; We need to hack the printing because the
208                               ;; doc-string must be printed specially for
209                               ;; make-docfile (sigh).
210                               (let* ((p (nthcdr (1- doc-string-elt)
211                                                 autoload))
212                                      (elt (cdr p)))
213                                 (setcdr p nil)
214                                 (princ "\n(" outbuf)
215                                 ;; XEmacs change: don't let ^^L's get into
216                                 ;; the file or sorting is hard.
217                                 (let ((print-escape-newlines t)
218                                       (p (save-excursion
219                                            (set-buffer outbuf)
220                                            (point)))
221                                       p2)
222                                   (mapcar (function (lambda (elt)
223                                                       (prin1 elt outbuf)
224                                                       (princ " " outbuf)))
225                                           autoload)
226                                   (save-excursion
227                                     (set-buffer outbuf)
228                                     (setq p2 (point-marker))
229                                     (goto-char p)
230                                     (save-match-data
231                                       (while (search-forward "\^L" p2 t)
232                                         (delete-char -1)
233                                         (insert "\\^L")))
234                                     (goto-char p2)
235                                     ))
236                                 (princ "\"\\\n" outbuf)
237                                 (let ((begin (save-excursion
238                                                (set-buffer outbuf)
239                                                (point))))
240                                   (princ (substring
241                                           (prin1-to-string (car elt)) 1)
242                                          outbuf)
243                                   ;; Insert a backslash before each ( that
244                                   ;; appears at the beginning of a line in
245                                   ;; the doc string.
246                                   (save-excursion
247                                     (set-buffer outbuf)
248                                     (save-excursion
249                                       (while (search-backward "\n(" begin t)
250                                         (forward-char 1)
251                                         (insert "\\"))))
252                                   (if (null (cdr elt))
253                                       (princ ")" outbuf)
254                                     (princ " " outbuf)
255                                     (princ (substring
256                                             (prin1-to-string (cdr elt))
257                                             1)
258                                            outbuf))
259                                   (terpri outbuf)))
260                             ;; XEmacs change: another fucking ^L hack
261                             (let ((p (save-excursion
262                                        (set-buffer outbuf)
263                                        (point)))
264                                   (print-escape-newlines t)
265                                   p2)
266                               (print autoload outbuf)
267                               (save-excursion
268                                 (set-buffer outbuf)
269                                 (setq p2 (point-marker))
270                                 (goto-char p)
271                                 (save-match-data
272                                   (while (search-forward "\^L" p2 t)
273                                     (delete-char -1)
274                                     (insert "\\^L")))
275                                 (goto-char p2)
276                                 ))
277                             ))
278                       ;; Copy the rest of the line to the output.
279                       (let ((begin (point)))
280                         ;; (terpri outbuf)
281                         (cond ((looking-at "immediate\\s *$") ; XEmacs
282                                ;; This is here so that you can automatically
283                                ;; have small hook functions copied to
284                                ;; loaddefs.el so that it's not necessary to
285                                ;; load a whole file just to get a two-line
286                                ;; do-nothing find-file-hook... --Stig
287                                (forward-line 1)
288                                (setq begin (point))
289                                (forward-sexp)
290                                (forward-line 1))
291                               (t
292                                (forward-line 1)))
293                         (princ (buffer-substring begin (point)) outbuf))))
294                    ((looking-at ";")
295                     ;; Don't read the comment.
296                     (forward-line 1))
297                    (t
298                     (forward-sexp 1)
299                     (forward-line 1)))
300                   (if dofiles
301                       (setq funlist (cdr funlist)))))))
302         (unless visited
303             ;; We created this buffer, so we should kill it.
304             (kill-buffer (current-buffer)))
305         (set-buffer outbuf)
306         (setq output-end (point-marker))))
307     (if t ;; done-any
308         ;; XEmacs -- always do this so that we cache the information
309         ;; that we've processed the file already.
310         (progn
311           (insert generate-autoload-section-header)
312           (prin1 (list 'autoloads autoloads-done load-name trim-name)
313                  outbuf)
314           (terpri outbuf)
315           ;;;; (insert ";;; Generated autoloads from "
316           ;;;;    (autoload-trim-file-name file) "\n")
317           ;; Warn if we put a line in loaddefs.el
318           ;; that is long enough to cause trouble.
319           (when (< output-end (point))
320             (setq output-end (point-marker)))
321           (while (< (point) output-end)
322             ;; (let ((beg (point)))
323               (end-of-line)
324               ;; Emacs -- I still haven't figured this one out.
325               ;; (if (> (- (point) beg) 900)
326                   ;; (progn
327                     ;; (message "A line is too long--over 900 characters")
328                     ;; (sleep-for 2)
329                     ;; (goto-char output-end)))
330               ;; )
331             (forward-line 1))
332           (goto-char output-end)
333           (insert generate-autoload-section-trailer)))
334     (or noninteractive ; XEmacs: only need one line in -batch mode.
335         (message "Generating autoloads for %s...done" file))))
336
337 \f
338 (defconst autoload-file-name "auto-autoloads.el"
339   "Generic filename to put autoloads into.
340 Unless you are an XEmacs maintainer, it is probably unwise to change this.")
341
342 (defvar autoload-target-directory "../lisp/prim/"
343   "Directory to put autoload declaration file into.
344 Unless you know what you're doing, don't mess with this.")
345
346 (defvar generated-autoload-file
347   (expand-file-name (concat autoload-target-directory
348                             autoload-file-name)
349                     data-directory)
350   "*File `update-file-autoloads' puts autoloads into.
351 A .el file can set this in its local variables section to make its
352 autoloads go somewhere else.")
353
354 (defconst cusload-file-name "custom-load.el"
355   "Generic filename ot put custom loads into.
356 Unless you are an XEmacs maintainr, it is probably unwise to change this.")
357
358 ;;;###autoload
359 (defun update-file-autoloads (file)
360   "Update the autoloads for FILE in `generated-autoload-file'
361 \(which FILE might bind in its local variables).
362 This functions refuses to update autoloads files."
363   (interactive "fUpdate autoloads for file: ")
364   (setq file (expand-file-name file))
365   (when (and (file-newer-than-file-p file generated-autoload-file)
366              (not (member (file-name-nondirectory file)
367                           (list autoload-file-name))))
368
369     (let ((load-name (replace-in-string (file-name-nondirectory file)
370                                         "\\.elc?$"
371                                         ""))
372           (trim-name (autoload-trim-file-name file))
373           section-begin form)
374       (save-excursion
375         (let ((find-file-hooks nil))
376           (set-buffer (or (get-file-buffer generated-autoload-file)
377                           (find-file-noselect generated-autoload-file))))
378         ;; Make sure we can scribble in it.
379         (setq buffer-read-only nil)
380         ;; First delete all sections for this file.
381         (goto-char (point-min))
382         (while (search-forward generate-autoload-section-header nil t)
383           (setq section-begin (match-beginning 0))
384           (setq form (read (current-buffer)))
385           (when (string= (nth 2 form) load-name)
386             (search-forward generate-autoload-section-trailer)
387             (delete-region section-begin (point))))
388
389         ;; Now find insertion point for new section
390         (block find-insertion-point
391           (goto-char (point-min))
392           (while (search-forward generate-autoload-section-header nil t)
393             (setq form (read (current-buffer)))
394             (when (string< trim-name (nth 3 form))
395               ;; Found alphabetically correct insertion point
396               (goto-char (match-beginning 0))
397               (return-from find-insertion-point))
398             (search-forward generate-autoload-section-trailer))
399           (when (eq (point) (point-min))        ; No existing entries?
400             (goto-char (point-max))))   ; Append.
401
402         ;; Add in new sections for file
403         (generate-file-autoloads file))
404
405       (when (interactive-p) (save-buffer)))))
406
407 ;;;###autoload
408 (defun update-autoloads-here ()
409   "Update sections of the current buffer generated by `update-file-autoloads'."
410   (interactive)
411   (let ((generated-autoload-file (buffer-file-name)))
412     (save-excursion
413       (goto-char (point-min))
414       (while (search-forward generate-autoload-section-header nil t)
415         (let* ((form (condition-case ()
416                          (read (current-buffer))
417                        (end-of-file nil)))
418                (file (nth 3 form)))
419           ;; XEmacs change: if we can't find the file as specified, look
420           ;; around a bit more.
421           (cond ((and (stringp file)
422                       (or (get-file-buffer file)
423                           (file-exists-p file))))
424                 ((and (stringp file)
425                       (save-match-data
426                         (let ((loc (locate-file (file-name-nondirectory file)
427                                                 load-path)))
428                           (if (null loc)
429                               nil
430                             (setq loc (expand-file-name
431                                        (autoload-trim-file-name loc)
432                                        ".."))
433                             (if (or (get-file-buffer loc)
434                                     (file-exists-p loc))
435                                 (setq file loc)
436                               nil))))))
437                 (t
438                  (setq file
439                        (if (y-or-n-p
440                             (format
441                              "Can't find library `%s'; remove its autoloads? "
442                              (nth 2 form) file))
443                            t
444                          (condition-case ()
445                              (read-file-name
446                               (format "Find `%s' load file: "
447                                       (nth 2 form))
448                               nil nil t)
449                            (quit nil))))))
450           (if file
451               (let ((begin (match-beginning 0)))
452                 (search-forward generate-autoload-section-trailer)
453                 (delete-region begin (point))))
454           (if (stringp file)
455               (generate-file-autoloads file)))))))
456
457 ;;;###autoload
458 (defun update-autoloads-from-directory (dir)
459   "Update `generated-autoload-file' with all the current autoloads from DIR.
460 This runs `update-file-autoloads' on each .el file in DIR.
461 Obsolete autoload entries for files that no longer exist are deleted."
462   (interactive "DUpdate autoloads for directory: ")
463   (setq dir (expand-file-name dir))
464   (let ((simple-dir (file-name-as-directory
465                      (file-name-nondirectory
466                      (directory-file-name dir))))
467         (enable-local-eval nil))
468     (save-excursion
469       (let ((find-file-hooks nil))
470         (set-buffer (find-file-noselect generated-autoload-file)))
471       (goto-char (point-min))
472       (while (search-forward generate-autoload-section-header nil t)
473         (let* ((begin (match-beginning 0))
474                (form (condition-case ()
475                          (read (current-buffer))
476                        (end-of-file nil)))
477                (file (nth 3 form)))
478           (when (and (stringp file)
479                      (string= (file-name-directory file) simple-dir)
480                      (not (file-exists-p
481                            (expand-file-name
482                             (file-name-nondirectory file) dir))))
483             ;; Remove the obsolete section.
484             (search-forward generate-autoload-section-trailer)
485             (delete-region begin (point)))))
486       ;; Update or create autoload sections for existing files.
487       (mapcar 'update-file-autoloads (directory-files dir t "^[^=].*\\.el$"))
488       (unless noninteractive
489         (save-buffer)))))
490
491 ;;;###autoload
492 (defun batch-update-autoloads ()
493   "Update the autoloads for the files or directories on the command line.
494 Runs `update-file-autoloads' on files and `update-directory-autoloads'
495 on directories.  Must be used only with -batch, and kills Emacs on completion.
496 Each file will be processed even if an error occurred previously.
497 For example, invoke `xemacs -batch -f batch-update-autoloads *.el'.
498 The directory to which the auto-autoloads.el file must be the first parameter
499 on the command line."
500   (unless noninteractive
501     (error "batch-update-autoloads is to be used only with -batch"))
502   (let ((defdir default-directory)
503         (enable-local-eval nil))        ; Don't query in batch mode.
504     ;; (message "Updating autoloads in %s..." generated-autoload-file)
505     (dolist (arg command-line-args-left)
506       (setq arg (expand-file-name arg defdir))
507       (cond
508        ((file-directory-p arg)
509         (message "Updating autoloads for directory %s..." arg)
510         (update-autoloads-from-directory arg))
511        ((file-exists-p arg)
512         (update-file-autoloads arg))
513        (t (error "No such file or directory: %s" arg))))
514     (fixup-autoload-buffer (concat (if autoload-package-name
515                                        autoload-package-name
516                                      (file-name-nondirectory defdir))
517                                    "-autoloads"))
518     (save-some-buffers t)
519     ;; (message "Done")
520     (kill-emacs 0)))
521
522 (defun fixup-autoload-buffer (sym)
523   (save-excursion
524     (set-buffer (find-file-noselect generated-autoload-file))
525     (goto-char (point-min))
526     (if (and (not (= (point-min) (point-max)))
527              (not (looking-at ";;; DO NOT MODIFY THIS FILE")))
528         (progn
529           (insert ";;; DO NOT MODIFY THIS FILE\n")
530           (insert "(if (featurep '" sym ")")
531           (insert " (error \"Already loaded\"))\n")
532           (goto-char (point-max))
533           (insert "\n(provide '" sym ")\n")))))
534
535 (defvar autoload-package-name nil)
536
537 ;;;###autoload
538 (defun batch-update-directory ()
539   "Update the autoloads for the directory on the command line.
540 Runs `update-file-autoloads' on each file in the given directory, must
541 be used only with -batch and kills XEmacs on completion."
542   (unless noninteractive
543     (error "batch-update-directory is to be used only with -batch"))
544   (let ((defdir default-directory)
545         (enable-local-eval nil))        ; Don't query in batch mode.
546     (dolist (arg command-line-args-left)
547       (setq arg (expand-file-name arg defdir))
548       (let ((generated-autoload-file (concat arg "/" autoload-file-name)))
549         (cond
550          ((file-directory-p arg)
551           (message "Updating autoloads in directory %s..." arg)
552           (update-autoloads-from-directory arg))
553          (t (error "No such file or directory: %s" arg)))
554         (fixup-autoload-buffer (concat (if autoload-package-name
555                                            autoload-package-name
556                                          (file-name-nondirectory arg))
557                                 "-autoloads"))
558         (save-some-buffers t))
559       ;; (message "Done")
560       ;; (kill-emacs 0)
561       )
562     (setq command-line-args-left nil)))
563
564 (provide 'autoload)
565
566 ;;; autoload.el ends here