XEmacs 21.4.9 "Informed Management".
[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, 2000 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 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,
45 or defmacro."
46   (let ((car (car-safe form)))
47     (if (memq car '(defun define-skeleton defmacro define-derived-mode))
48         (let ((macrop (eq car 'defmacro))
49               name doc)
50           (setq form (cdr form)
51                 name (car form)
52                 ;; Ignore the arguments.
53                 form (cdr (cond ((eq car 'define-skeleton)
54                                  form)
55                                 ((eq car 'define-derived-mode)
56                                  (cddr form))
57                                 (t
58                                  (cdr form))))
59                 doc (car form))
60           (if (stringp doc)
61               (setq form (cdr form))
62             (setq doc nil))
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)))
68       nil)))
69
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).
74
75 This string is used:
76
77 ;;;###autoload
78 \(defun function-to-be-autoloaded () ...)
79
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'.")
85
86 (defvar generate-autoload-section-header "\f\n;;;### "
87   "String inserted before the form identifying
88 the section of autoloads for a file.")
89
90 (defvar generate-autoload-section-trailer "\n;;;***\n"
91   "String which indicates the end of the section of autoloads for a file.")
92
93 (defvar autoload-package-name nil)
94
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.
98 ;;;
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.
105 ;;;
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.
112
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)
120
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))
124   (replace-in-string
125    (file-relative-name file (file-name-directory
126                              (directory-file-name
127                               (file-name-directory file))))
128    "\\\\" "/"))
129
130 ;;;###autoload
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
136 are used."
137   (interactive "fGenerate autoloads for file: ")
138   (generate-file-autoloads-1 file funlist))
139
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
145 are used."
146   (let ((outbuf (current-buffer))
147         (autoloads-done '())
148         (load-name (replace-in-string (file-name-nondirectory file)
149                                       "\\.elc?$"
150                                       ""))
151         (trim-name (autoload-trim-file-name file))
152         (dofiles (not (null funlist)))
153         (print-length nil)
154         (print-readably t) ; XEmacs
155         (float-output-format nil)
156         ;; (done-any nil)
157         (visited (get-file-buffer file))
158         output-end)
159
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))
168
169     (save-excursion
170       (unwind-protect
171           (progn
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))
176             (save-excursion
177               (save-restriction
178                 (widen)
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))
183
184                 (message "Generating autoloads for %s..." trim-name)
185                 (goto-char (point-min))
186                 (while (if dofiles funlist (not (eobp)))
187                   (if (not dofiles)
188                       (skip-chars-forward " \t\n\f")
189                     (goto-char (point-min))
190                     (re-search-forward
191                      (concat "(def\\(un\\|var\\|const\\|macro\\) "
192                              (regexp-quote (symbol-name (car funlist)))
193                              "\\s "))
194                     (goto-char (match-beginning 0)))
195                   (cond
196                    ((or dofiles
197                         (looking-at (regexp-quote generate-autoload-cookie)))
198                     (if dofiles
199                         nil
200                       (search-forward generate-autoload-cookie)
201                       (skip-chars-forward " \t"))
202                     ;; (setq done-any 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)
209                                                     'doc-string-elt)))
210                           (if autoload
211                               (setq autoloads-done (cons (nth 1 form)
212                                                          autoloads-done))
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)
220                                                 autoload))
221                                      (elt (cdr p)))
222                                 (setcdr p nil)
223                                 (princ "\n(" outbuf)
224                                 ;; XEmacs change: don't let ^^L's get into
225                                 ;; the file or sorting is hard.
226                                 (let ((print-escape-newlines t)
227                                       (p (save-excursion
228                                            (set-buffer outbuf)
229                                            (point)))
230                                       p2)
231                                   (mapcar (function (lambda (elt)
232                                                       (prin1 elt outbuf)
233                                                       (princ " " outbuf)))
234                                           autoload)
235                                   (save-excursion
236                                     (set-buffer outbuf)
237                                     (setq p2 (point-marker))
238                                     (goto-char p)
239                                     (save-match-data
240                                       (while (search-forward "\^L" p2 t)
241                                         (delete-char -1)
242                                         (insert "\\^L")))
243                                     (goto-char p2)
244                                     ))
245                                 (princ "\"\\\n" outbuf)
246                                 (let ((begin (save-excursion
247                                                (set-buffer outbuf)
248                                                (point))))
249                                   (princ (substring
250                                           (prin1-to-string (car elt)) 1)
251                                          outbuf)
252                                   ;; Insert a backslash before each ( that
253                                   ;; appears at the beginning of a line in
254                                   ;; the doc string.
255                                   (save-excursion
256                                     (set-buffer outbuf)
257                                     (save-excursion
258                                       (while (search-backward "\n(" begin t)
259                                         (forward-char 1)
260                                         (insert "\\"))))
261                                   (if (null (cdr elt))
262                                       (princ ")" outbuf)
263                                     (princ " " outbuf)
264                                     (princ (substring
265                                             (prin1-to-string (cdr elt))
266                                             1)
267                                            outbuf))
268                                   (terpri outbuf)))
269                             ;; XEmacs change: another fucking ^L hack
270                             (let ((p (save-excursion
271                                        (set-buffer outbuf)
272                                        (point)))
273                                   (print-escape-newlines t)
274                                   p2)
275                               (print autoload outbuf)
276                               (save-excursion
277                                 (set-buffer outbuf)
278                                 (setq p2 (point-marker))
279                                 (goto-char p)
280                                 (save-match-data
281                                   (while (search-forward "\^L" p2 t)
282                                     (delete-char -1)
283                                     (insert "\\^L")))
284                                 (goto-char p2)
285                                 ))
286                             ))
287                       ;; Copy the rest of the line to the output.
288                       (let ((begin (point)))
289                         ;; (terpri outbuf)
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
296                                (forward-line 1)
297                                (setq begin (point))
298                                (forward-sexp)
299                                (forward-line 1))
300                               (t
301                                (forward-line 1)))
302                         (princ (buffer-substring begin (point)) outbuf))))
303                    ((looking-at ";")
304                     ;; Don't read the comment.
305                     (forward-line 1))
306                    (t
307                     (forward-sexp 1)
308                     (forward-line 1)))
309                   (if dofiles
310                       (setq funlist (cdr funlist)))))))
311         (unless visited
312             ;; We created this buffer, so we should kill it.
313             (kill-buffer (current-buffer)))
314         (set-buffer outbuf)
315         (setq output-end (point-marker))))
316     (if t ;; done-any
317         ;; XEmacs -- always do this so that we cache the information
318         ;; that we've processed the file already.
319         (progn
320           (insert generate-autoload-section-header)
321           (prin1 (list 'autoloads autoloads-done load-name trim-name)
322                  outbuf)
323           (terpri outbuf)
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)))
332               (end-of-line)
333               ;; Emacs -- I still haven't figured this one out.
334               ;; (if (> (- (point) beg) 900)
335                   ;; (progn
336                     ;; (message "A line is too long--over 900 characters")
337                     ;; (sleep-for 2)
338                     ;; (goto-char output-end)))
339               ;; )
340             (forward-line 1))
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))))
345
346 \f
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.")
350
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.")
354
355 (defvar generated-autoload-file
356   (expand-file-name (concat autoload-target-directory
357                             autoload-file-name)
358                     data-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.
362
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
365 updated.")
366
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.")
370
371 ;;;###autoload
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))))
381
382     (let ((load-name (replace-in-string (file-name-nondirectory file)
383                                         "\\.elc?$"
384                                         ""))
385           (trim-name (autoload-trim-file-name file))
386           section-begin form)
387       (save-excursion
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))))
401
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.
414
415         ;; Add in new sections for file
416         (generate-file-autoloads file))
417
418       (when (interactive-p) (save-buffer)))))
419
420 ;;;###autoload
421 (defun update-autoloads-here ()
422   "Update sections of the current buffer generated by `update-file-autoloads'."
423   (interactive)
424   (let ((generated-autoload-file (buffer-file-name)))
425     (save-excursion
426       (goto-char (point-min))
427       (while (search-forward generate-autoload-section-header nil t)
428         (let* ((form (condition-case ()
429                          (read (current-buffer))
430                        (end-of-file nil)))
431                (file (nth 3 form)))
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))))
437                 ((and (stringp file)
438                       (save-match-data
439                         (let ((loc (locate-file (file-name-nondirectory file)
440                                                 load-path)))
441                           (if (null loc)
442                               nil
443                             (setq loc (expand-file-name
444                                        (autoload-trim-file-name loc)
445                                        ".."))
446                             (if (or (get-file-buffer loc)
447                                     (file-exists-p loc))
448                                 (setq file loc)
449                               nil))))))
450                 (t
451                  (setq file
452                        (if (y-or-n-p
453                             (format
454                              "Can't find library `%s'; remove its autoloads? "
455                              (nth 2 form) file))
456                            t
457                          (condition-case ()
458                              (read-file-name
459                               (format "Find `%s' load file: "
460                                       (nth 2 form))
461                               nil nil t)
462                            (quit nil))))))
463           (if file
464               (let ((begin (match-beginning 0)))
465                 (search-forward generate-autoload-section-trailer)
466                 (delete-region begin (point))))
467           (if (stringp file)
468               (generate-file-autoloads file)))))))
469
470 ;;;###autoload
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))
483     (save-excursion
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))
491                        (end-of-file nil)))
492                (file (nth 3 form)))
493           (when (and (stringp file)
494                      (string= (file-name-directory file) simple-dir)
495                      (not (file-exists-p
496                            (expand-file-name
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
504         (save-buffer)))))
505
506 ;;;###autoload
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))
522       (cond
523        ((file-directory-p arg)
524         (message "Updating autoloads for directory %s..." arg)
525         (update-autoloads-from-directory arg))
526        ((file-exists-p 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))
532                                    "-autoloads"))
533     (save-some-buffers t)
534     ;; (message "Done")
535     (kill-emacs 0)))
536
537 (defun fixup-autoload-buffer (sym)
538   (save-excursion
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")))
543         (progn
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")))))
549
550 (defvar autoload-package-name nil)
551
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
557
558 ;;;###autoload
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
570                                                         arg)))
571         (cond
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))
579                                 "-autoloads"))
580         (save-some-buffers t))
581       ;; (message "Done")
582       ;; (kill-emacs 0)
583       )
584     (setq command-line-args-left nil)))
585
586 ;; #### i created the following.  this one and the last should be merged into
587 ;; batch-update-autoloads and batch-update-one-directory. --ben
588
589 ;;;###autoload
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
602                                                         arg)))
603         (cond
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))
611                                 "-autoloads"))
612         (save-some-buffers t))
613       ;; (message "Done")
614       )))
615
616 (provide 'autoload)
617
618 ;;; autoload.el ends here