b9b0a605aa42af471a1760481eb5e4bf8f608c8b
[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   (replace-in-string
118    (file-relative-name file (file-name-directory
119                              (directory-file-name
120                               (file-name-directory file))))
121    "\\\\" "/"))
122   
123 ;;;###autoload
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
129 are used."
130   (interactive "fGenerate autoloads for file: ")
131   (generate-file-autoloads-1 file funlist))
132
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
138 are used."
139   (let ((outbuf (current-buffer))
140         (autoloads-done '())
141         (load-name (replace-in-string (file-name-nondirectory file)
142                                       "\\.elc?$"
143                                       ""))
144         (trim-name (autoload-trim-file-name file))
145         (dofiles (not (null funlist)))
146         (print-length nil)
147         (print-readably t) ; XEmacs
148         (float-output-format nil)
149         ;; (done-any nil)
150         (visited (get-file-buffer file))
151         output-end)
152
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))
161
162     (save-excursion
163       (unwind-protect
164           (progn
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))
169             (save-excursion
170               (save-restriction
171                 (widen)
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))
176
177                 (message "Generating autoloads for %s..." trim-name)
178                 (goto-char (point-min))
179                 (while (if dofiles funlist (not (eobp)))
180                   (if (not dofiles)
181                       (skip-chars-forward " \t\n\f")
182                     (goto-char (point-min))
183                     (re-search-forward
184                      (concat "(def\\(un\\|var\\|const\\|macro\\) "
185                              (regexp-quote (symbol-name (car funlist)))
186                              "\\s "))
187                     (goto-char (match-beginning 0)))
188                   (cond
189                    ((or dofiles
190                         (looking-at (regexp-quote generate-autoload-cookie)))
191                     (if dofiles
192                         nil
193                       (search-forward generate-autoload-cookie)
194                       (skip-chars-forward " \t"))
195                     ;; (setq done-any 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)
202                                                     'doc-string-elt)))
203                           (if autoload
204                               (setq autoloads-done (cons (nth 1 form)
205                                                          autoloads-done))
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)
213                                                 autoload))
214                                      (elt (cdr p)))
215                                 (setcdr p nil)
216                                 (princ "\n(" outbuf)
217                                 ;; XEmacs change: don't let ^^L's get into
218                                 ;; the file or sorting is hard.
219                                 (let ((print-escape-newlines t)
220                                       (p (save-excursion
221                                            (set-buffer outbuf)
222                                            (point)))
223                                       p2)
224                                   (mapcar (function (lambda (elt)
225                                                       (prin1 elt outbuf)
226                                                       (princ " " outbuf)))
227                                           autoload)
228                                   (save-excursion
229                                     (set-buffer outbuf)
230                                     (setq p2 (point-marker))
231                                     (goto-char p)
232                                     (save-match-data
233                                       (while (search-forward "\^L" p2 t)
234                                         (delete-char -1)
235                                         (insert "\\^L")))
236                                     (goto-char p2)
237                                     ))
238                                 (princ "\"\\\n" outbuf)
239                                 (let ((begin (save-excursion
240                                                (set-buffer outbuf)
241                                                (point))))
242                                   (princ (substring
243                                           (prin1-to-string (car elt)) 1)
244                                          outbuf)
245                                   ;; Insert a backslash before each ( that
246                                   ;; appears at the beginning of a line in
247                                   ;; the doc string.
248                                   (save-excursion
249                                     (set-buffer outbuf)
250                                     (save-excursion
251                                       (while (search-backward "\n(" begin t)
252                                         (forward-char 1)
253                                         (insert "\\"))))
254                                   (if (null (cdr elt))
255                                       (princ ")" outbuf)
256                                     (princ " " outbuf)
257                                     (princ (substring
258                                             (prin1-to-string (cdr elt))
259                                             1)
260                                            outbuf))
261                                   (terpri outbuf)))
262                             ;; XEmacs change: another fucking ^L hack
263                             (let ((p (save-excursion
264                                        (set-buffer outbuf)
265                                        (point)))
266                                   (print-escape-newlines t)
267                                   p2)
268                               (print autoload outbuf)
269                               (save-excursion
270                                 (set-buffer outbuf)
271                                 (setq p2 (point-marker))
272                                 (goto-char p)
273                                 (save-match-data
274                                   (while (search-forward "\^L" p2 t)
275                                     (delete-char -1)
276                                     (insert "\\^L")))
277                                 (goto-char p2)
278                                 ))
279                             ))
280                       ;; Copy the rest of the line to the output.
281                       (let ((begin (point)))
282                         ;; (terpri outbuf)
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
289                                (forward-line 1)
290                                (setq begin (point))
291                                (forward-sexp)
292                                (forward-line 1))
293                               (t
294                                (forward-line 1)))
295                         (princ (buffer-substring begin (point)) outbuf))))
296                    ((looking-at ";")
297                     ;; Don't read the comment.
298                     (forward-line 1))
299                    (t
300                     (forward-sexp 1)
301                     (forward-line 1)))
302                   (if dofiles
303                       (setq funlist (cdr funlist)))))))
304         (unless visited
305             ;; We created this buffer, so we should kill it.
306             (kill-buffer (current-buffer)))
307         (set-buffer outbuf)
308         (setq output-end (point-marker))))
309     (if t ;; done-any
310         ;; XEmacs -- always do this so that we cache the information
311         ;; that we've processed the file already.
312         (progn
313           (insert generate-autoload-section-header)
314           (prin1 (list 'autoloads autoloads-done load-name trim-name)
315                  outbuf)
316           (terpri outbuf)
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)))
325               (end-of-line)
326               ;; Emacs -- I still haven't figured this one out.
327               ;; (if (> (- (point) beg) 900)
328                   ;; (progn
329                     ;; (message "A line is too long--over 900 characters")
330                     ;; (sleep-for 2)
331                     ;; (goto-char output-end)))
332               ;; )
333             (forward-line 1))
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))))
338
339 \f
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.")
343
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.")
347
348 (defvar generated-autoload-file
349   (expand-file-name (concat autoload-target-directory
350                             autoload-file-name)
351                     data-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.
355
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
358 updated.")
359
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.")
363
364 ;;;###autoload
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))))
374
375     (let ((load-name (replace-in-string (file-name-nondirectory file)
376                                         "\\.elc?$"
377                                         ""))
378           (trim-name (autoload-trim-file-name file))
379           section-begin form)
380       (save-excursion
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))))
394
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.
407
408         ;; Add in new sections for file
409         (generate-file-autoloads file))
410
411       (when (interactive-p) (save-buffer)))))
412
413 ;;;###autoload
414 (defun update-autoloads-here ()
415   "Update sections of the current buffer generated by `update-file-autoloads'."
416   (interactive)
417   (let ((generated-autoload-file (buffer-file-name)))
418     (save-excursion
419       (goto-char (point-min))
420       (while (search-forward generate-autoload-section-header nil t)
421         (let* ((form (condition-case ()
422                          (read (current-buffer))
423                        (end-of-file nil)))
424                (file (nth 3 form)))
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))))
430                 ((and (stringp file)
431                       (save-match-data
432                         (let ((loc (locate-file (file-name-nondirectory file)
433                                                 load-path)))
434                           (if (null loc)
435                               nil
436                             (setq loc (expand-file-name
437                                        (autoload-trim-file-name loc)
438                                        ".."))
439                             (if (or (get-file-buffer loc)
440                                     (file-exists-p loc))
441                                 (setq file loc)
442                               nil))))))
443                 (t
444                  (setq file
445                        (if (y-or-n-p
446                             (format
447                              "Can't find library `%s'; remove its autoloads? "
448                              (nth 2 form) file))
449                            t
450                          (condition-case ()
451                              (read-file-name
452                               (format "Find `%s' load file: "
453                                       (nth 2 form))
454                               nil nil t)
455                            (quit nil))))))
456           (if file
457               (let ((begin (match-beginning 0)))
458                 (search-forward generate-autoload-section-trailer)
459                 (delete-region begin (point))))
460           (if (stringp file)
461               (generate-file-autoloads file)))))))
462
463 ;;;###autoload
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))
476     (save-excursion
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))
484                        (end-of-file nil)))
485                (file (nth 3 form)))
486           (when (and (stringp file)
487                      (string= (file-name-directory file) simple-dir)
488                      (not (file-exists-p
489                            (expand-file-name
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
497         (save-buffer)))))
498
499 ;;;###autoload
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))
515       (cond
516        ((file-directory-p arg)
517         (message "Updating autoloads for directory %s..." arg)
518         (update-autoloads-from-directory arg))
519        ((file-exists-p 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))
525                                    "-autoloads"))
526     (save-some-buffers t)
527     ;; (message "Done")
528     (kill-emacs 0)))
529
530 (defun fixup-autoload-buffer (sym)
531   (save-excursion
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")))
536         (progn
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")))))
542
543 (defvar autoload-package-name nil)
544
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
549
550 ;;;###autoload
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
562                                                         arg)))
563         (cond
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))
571                                 "-autoloads"))
572         (save-some-buffers t))
573       ;; (message "Done")
574       ;; (kill-emacs 0)
575       )
576     (setq command-line-args-left nil)))
577
578 ;; #### i created the following.  this one and the last should be merged into
579 ;; batch-update-autoloads. --ben
580
581 ;;;###autoload
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
594                                                         arg)))
595         (cond
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))
603                                 "-autoloads"))
604         (save-some-buffers t))
605       ;; (message "Done")
606       )))
607
608 (provide 'autoload)
609
610 ;;; autoload.el ends here