Re-enable code giving word syntax to certain japanese-jisx0208 characters.
[bpt/emacs.git] / lisp / emacs-lisp / autoload.el
CommitLineData
e8af40ee 1;;; autoload.el --- maintain autoloads in loaddefs.el
c0274f38 2
789b6186
GM
3;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2001
4;; Free Software Foundation, Inc.
b578f267 5
5762abec 6;; Author: Roland McGrath <roland@gnu.org>
e9571d2a 7;; Keywords: maint
e5167999 8
b578f267
EN
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
0231f2dc 25
07b3798c 26;;; Commentary:
e41b2db1 27
00ee57f3 28;; This code helps GNU Emacs maintainers keep the loaddefs.el file up to
e41b2db1
ER
29;; date. It interprets magic cookies of the form ";;;###autoload" in
30;; lisp source files in various useful ways. To learn more, read the
31;; source; if you're going to use this, you'd better be able to.
32
e5167999
ER
33;;; Code:
34
b0daab9a
SM
35(require 'lisp-mode) ;for `doc-string-elt' properties.
36
37
0ceb5fe0
RS
38(defvar generated-autoload-file "loaddefs.el"
39 "*File \\[update-file-autoloads] puts autoloads into.
40A `.el' file can set this in its local variables section to make its
c8aca7d1
KH
41autoloads go somewhere else. The autoload file is assumed to contain a
42trailer starting with a FormFeed character.")
0ceb5fe0
RS
43
44(defconst generate-autoload-cookie ";;;###autoload"
45 "Magic comment indicating the following form should be autoloaded.
46Used by \\[update-file-autoloads]. This string should be
47meaningless to Lisp (e.g., a comment).
48
49This string is used:
50
51;;;###autoload
52\(defun function-to-be-autoloaded () ...)
53
54If this string appears alone on a line, the following form will be
55read and an autoload made for it. If there is further text on the line,
56that text will be copied verbatim to `generated-autoload-file'.")
57
58(defconst generate-autoload-section-header "\f\n;;;### "
2336b6a9 59 "String that marks the form at the start of a new file's autoload section.")
0ceb5fe0
RS
60
61(defconst generate-autoload-section-trailer "\n;;;***\n"
62 "String which indicates the end of the section of autoloads for a file.")
63
2336b6a9
KH
64(defconst generate-autoload-section-continuation ";;;;;; "
65 "String to add on each continuation of the section header form.")
66
0231f2dc 67(defun make-autoload (form file)
ceaa3695 68 "Turn FORM into an autoload or defvar for source file FILE.
e8139c11
SM
69Returns nil if FORM is not a special autoload form (i.e. a function definition
70or macro definition or a defcustom)."
71 (let ((car (car-safe form)) expand)
72 (cond
73 ;; For complex cases, try again on the macro-expansion.
74 ((and (memq car '(easy-mmode-define-global-mode
75 easy-mmode-define-minor-mode define-minor-mode))
76 (setq expand (let ((load-file-name file)) (macroexpand form)))
77 (eq (car expand) 'progn)
78 (memq :autoload-end expand))
79 (let ((end (memq :autoload-end expand)))
80 ;; Cut-off anything after the :autoload-end marker.
81 (setcdr end nil)
82 (cons 'progn
83 (mapcar (lambda (form) (make-autoload form file))
84 (cdr expand)))))
85
86 ;; For special function-like operators, use the `autoload' function.
87 ((memq car '(defun define-skeleton defmacro define-derived-mode
88 define-generic-mode easy-mmode-define-minor-mode
89 easy-mmode-define-global-mode
74312ddc
CW
90 define-minor-mode defun* defmacro*))
91 (let* ((macrop (memq car '(defmacro defmacro*)))
e8139c11
SM
92 (name (nth 1 form))
93 (body (nthcdr (get car 'doc-string-elt) form))
94 (doc (if (stringp (car body)) (pop body))))
95 ;; `define-generic-mode' quotes the name, so take care of that
96 (list 'autoload (if (listp name) name (list 'quote name)) file doc
97 (or (and (memq car '(define-skeleton define-derived-mode
98 define-generic-mode
99 easy-mmode-define-global-mode
100 easy-mmode-define-minor-mode
101 define-minor-mode)) t)
102 (eq (car-safe (car body)) 'interactive))
103 (if macrop (list 'quote 'macro) nil))))
104
105 ;; Convert defcustom to a simpler (and less space-consuming) defvar,
106 ;; but add some extra stuff if it uses :require.
107 ((eq car 'defcustom)
108 (let ((varname (car-safe (cdr-safe form)))
109 (init (car-safe (cdr-safe (cdr-safe form))))
110 (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form)))))
111 (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form))))))
112 (if (not (plist-get rest :require))
113 `(defvar ,varname ,init ,doc)
114 `(progn
115 (defvar ,varname ,init ,doc)
116 (custom-add-to-group ,(plist-get rest :group)
117 ',varname 'custom-variable)
118 (custom-add-load ',varname
119 ,(plist-get rest :require))))))
120
121 ;; nil here indicates that this is not a special autoload form.
122 (t nil))))
0231f2dc 123
daa37602
JB
124;;; Forms which have doc-strings which should be printed specially.
125;;; A doc-string-elt property of ELT says that (nth ELT FORM) is
126;;; the doc-string in FORM.
b0daab9a 127;;; Those properties are now set in lisp-mode.el.
fc89daee 128
0231f2dc 129
72c19d97 130(defun autoload-trim-file-name (file)
1eb0a345
RS
131 ;; Returns a relative pathname of FILE
132 ;; starting from the directory that loaddefs.el is in.
133 ;; That is normally a directory in load-path,
134 ;; which means Emacs will be able to find FILE when it looks.
135 ;; Any extra directory names here would prevent finding the file.
72c19d97
RM
136 (setq file (expand-file-name file))
137 (file-relative-name file
1eb0a345 138 (file-name-directory generated-autoload-file)))
72c19d97 139
2336b6a9
KH
140(defun autoload-read-section-header ()
141 "Read a section header form.
142Since continuation lines have been marked as comments,
143we must copy the text of the form and remove those comment
144markers before we call `read'."
145 (save-match-data
146 (let ((beginning (point))
147 string)
148 (forward-line 1)
149 (while (looking-at generate-autoload-section-continuation)
150 (forward-line 1))
151 (setq string (buffer-substring beginning (point)))
152 (with-current-buffer (get-buffer-create " *autoload*")
153 (erase-buffer)
154 (insert string)
155 (goto-char (point-min))
156 (while (search-forward generate-autoload-section-continuation nil t)
157 (replace-match " "))
158 (goto-char (point-min))
159 (read (current-buffer))))))
160
a8add29d
SM
161;; !! Requires OUTBUF to be bound !!
162(defun autoload-print-form (form)
163 "Print FORM such that make-docfile will find the docstrings."
164 (cond
165 ;; If the form is a sequence, recurse.
166 ((eq (car form) 'progn) (mapcar 'autoload-print-form (cdr form)))
167 ;; Symbols at the toplevel are meaningless.
168 ((symbolp form) nil)
169 (t
170 (let ((doc-string-elt (get (car-safe form) 'doc-string-elt)))
171 (if (and doc-string-elt (stringp (nth doc-string-elt form)))
172 ;; We need to hack the printing because the
173 ;; doc-string must be printed specially for
174 ;; make-docfile (sigh).
175 (let* ((p (nthcdr (1- doc-string-elt) form))
176 (elt (cdr p)))
177 (setcdr p nil)
178 (princ "\n(" outbuf)
179 (let ((print-escape-newlines t)
180 (print-escape-nonascii t))
181 (mapcar (lambda (elt)
182 (prin1 elt outbuf)
183 (princ " " outbuf))
184 form))
185 (princ "\"\\\n" outbuf)
186 (let ((begin (with-current-buffer outbuf (point))))
187 (princ (substring (prin1-to-string (car elt)) 1)
188 outbuf)
189 ;; Insert a backslash before each ( that
190 ;; appears at the beginning of a line in
191 ;; the doc string.
192 (with-current-buffer outbuf
193 (save-excursion
194 (while (search-backward "\n(" begin t)
195 (forward-char 1)
196 (insert "\\"))))
197 (if (null (cdr elt))
198 (princ ")" outbuf)
199 (princ " " outbuf)
200 (princ (substring (prin1-to-string (cdr elt)) 1)
201 outbuf))
202 (terpri outbuf)))
203 (let ((print-escape-newlines t)
204 (print-escape-nonascii t))
205 (print form outbuf)))))))
206
a273d3e0
GM
207(defun autoload-ensure-default-file (file)
208 "Make sure that the autoload file FILE exists and if not create it."
209 (unless (file-exists-p file)
210 (write-region
211 (concat ";;; " (file-name-nondirectory file)
212 " --- automatically extracted autoloads\n"
213 ";;\n"
214 ";;; Code:\n\n"
215 "\f\n;; Local Variables:\n"
216 ";; version-control: never\n"
217 ";; no-byte-compile: t\n"
218 ";; no-update-autoloads: t\n"
219 ";; End:\n"
220 ";;; " (file-name-nondirectory file)
221 "ends here\n")
222 nil file))
223 file)
224
225(defun autoload-insert-section-header (outbuf autoloads load-name file time)
226 "Insert the section-header line,
227which lists the file name and which functions are in it, etc."
228 (insert generate-autoload-section-header)
229 (prin1 (list 'autoloads autoloads load-name
230 (if (stringp file) (autoload-trim-file-name file) file)
231 time)
232 outbuf)
233 (terpri outbuf)
234 ;; Break that line at spaces, to avoid very long lines.
235 ;; Make each sub-line into a comment.
236 (with-current-buffer outbuf
237 (save-excursion
238 (forward-line -1)
239 (while (not (eolp))
240 (move-to-column 64)
241 (skip-chars-forward "^ \n")
242 (or (eolp)
243 (insert "\n" generate-autoload-section-continuation))))))
244
0231f2dc
JB
245(defun generate-file-autoloads (file)
246 "Insert at point a loaddefs autoload section for FILE.
247autoloads are generated for defuns and defmacros in FILE
da8826b4 248marked by `generate-autoload-cookie' (which see).
0231f2dc
JB
249If FILE is being visited in a buffer, the contents of the buffer
250are used."
251 (interactive "fGenerate autoloads for file: ")
252 (let ((outbuf (current-buffer))
0231f2dc
JB
253 (autoloads-done '())
254 (load-name (let ((name (file-name-nondirectory file)))
255 (if (string-match "\\.elc?$" name)
256 (substring name 0 (match-beginning 0))
257 name)))
258 (print-length nil)
72c19d97 259 (print-readably t) ; This does something in Lucid Emacs.
aa7ea8bd 260 (float-output-format nil)
0231f2dc 261 (done-any nil)
7e4263eb 262 (visited (get-file-buffer file))
0231f2dc 263 output-end)
daa37602
JB
264
265 ;; If the autoload section we create here uses an absolute
266 ;; pathname for FILE in its header, and then Emacs is installed
267 ;; under a different path on another system,
268 ;; `update-autoloads-here' won't be able to find the files to be
269 ;; autoloaded. So, if FILE is in the same directory or a
e5d77022 270 ;; subdirectory of the current buffer's directory, we'll make it
daa37602
JB
271 ;; relative to the current buffer's directory.
272 (setq file (expand-file-name file))
1265394f
RM
273 (let* ((source-truename (file-truename file))
274 (dir-truename (file-name-as-directory
275 (file-truename default-directory)))
276 (len (length dir-truename)))
277 (if (and (< len (length source-truename))
278 (string= dir-truename (substring source-truename 0 len)))
279 (setq file (substring source-truename len))))
daa37602 280
0231f2dc 281 (message "Generating autoloads for %s..." file)
6798a385
JB
282 (save-excursion
283 (unwind-protect
284 (progn
66fc2bf5
RM
285 (if visited
286 (set-buffer visited)
287 ;; It is faster to avoid visiting the file.
288 (set-buffer (get-buffer-create " *generate-autoload-file*"))
289 (kill-all-local-variables)
290 (erase-buffer)
b59c7256
RM
291 (setq buffer-undo-list t
292 buffer-read-only nil)
293 (emacs-lisp-mode)
66fc2bf5 294 (insert-file-contents file nil))
6798a385
JB
295 (save-excursion
296 (save-restriction
297 (widen)
298 (goto-char (point-min))
299 (while (not (eobp))
300 (skip-chars-forward " \t\n\f")
301 (cond
302 ((looking-at (regexp-quote generate-autoload-cookie))
303 (search-forward generate-autoload-cookie)
304 (skip-chars-forward " \t")
305 (setq done-any t)
9ed4d64f
RM
306 (if (eolp)
307 ;; Read the next form and make an autoload.
308 (let* ((form (prog1 (read (current-buffer))
a8add29d
SM
309 (or (bolp) (forward-line 1))))
310 (autoload (make-autoload form load-name)))
9ed4d64f
RM
311 (if autoload
312 (setq autoloads-done (cons (nth 1 form)
313 autoloads-done))
314 (setq autoload form))
a8add29d
SM
315 (autoload-print-form autoload))
316
317 ;; Copy the rest of the line to the output.
6e21af56
RM
318 (princ (buffer-substring
319 (progn
320 ;; Back up over whitespace, to preserve it.
321 (skip-chars-backward " \f\t")
322 (if (= (char-after (1+ (point))) ? )
323 ;; Eat one space.
324 (forward-char 1))
325 (point))
326 (progn (forward-line 1) (point)))
327 outbuf)))
72c19d97
RM
328 ((looking-at ";")
329 ;; Don't read the comment.
330 (forward-line 1))
331 (t
332 (forward-sexp 1)
333 (forward-line 1)))))))
6798a385
JB
334 (or visited
335 ;; We created this buffer, so we should kill it.
336 (kill-buffer (current-buffer)))
337 (set-buffer outbuf)
338 (setq output-end (point-marker))))
0231f2dc
JB
339 (if done-any
340 (progn
2336b6a9
KH
341 ;; Insert the section-header line
342 ;; which lists the file name and which functions are in it, etc.
a273d3e0
GM
343 (autoload-insert-section-header outbuf autoloads-done load-name file
344 (nth 5 (file-attributes file)))
72c19d97
RM
345 (insert ";;; Generated autoloads from "
346 (autoload-trim-file-name file) "\n")
0231f2dc
JB
347 (goto-char output-end)
348 (insert generate-autoload-section-trailer)))
349 (message "Generating autoloads for %s...done" file)))
e2b6138f 350\f
0231f2dc
JB
351;;;###autoload
352(defun update-file-autoloads (file)
353 "Update the autoloads for FILE in `generated-autoload-file'
a273d3e0
GM
354\(which FILE might bind in its local variables).
355Return FILE if there was no autoload cookie in it."
0231f2dc
JB
356 (interactive "fUpdate autoloads for file: ")
357 (let ((load-name (let ((name (file-name-nondirectory file)))
358 (if (string-match "\\.elc?$" name)
359 (substring name 0 (match-beginning 0))
360 name)))
d55e3ff0 361 (found nil)
a273d3e0
GM
362 (existing-buffer (get-file-buffer file))
363 (no-autoloads nil))
0231f2dc
JB
364 (save-excursion
365 ;; We want to get a value for generated-autoload-file from
366 ;; the local variables section if it's there.
b59c7256
RM
367 (if existing-buffer
368 (set-buffer existing-buffer))
9e3366e4
EZ
369 ;; We must read/write the file without any code conversion,
370 ;; but still decode EOLs.
371 (let ((coding-system-for-read 'raw-text))
68521d61 372 (set-buffer (find-file-noselect
a273d3e0
GM
373 (autoload-ensure-default-file
374 (expand-file-name generated-autoload-file
375 (expand-file-name "lisp"
376 source-directory)))))
9e3366e4
EZ
377 ;; This is to make generated-autoload-file have Unix EOLs, so
378 ;; that it is portable to all platforms.
379 (setq buffer-file-coding-system 'raw-text-unix))
0b33ec46
RS
380 (or (> (buffer-size) 0)
381 (error "Autoloads file %s does not exist" buffer-file-name))
382 (or (file-writable-p buffer-file-name)
383 (error "Autoloads file %s is not writable" buffer-file-name))
0231f2dc
JB
384 (save-excursion
385 (save-restriction
386 (widen)
387 (goto-char (point-min))
d55e3ff0
RM
388 ;; Look for the section for LOAD-NAME.
389 (while (and (not found)
390 (search-forward generate-autoload-section-header nil t))
2336b6a9 391 (let ((form (autoload-read-section-header)))
d55e3ff0
RM
392 (cond ((string= (nth 2 form) load-name)
393 ;; We found the section for this file.
394 ;; Check if it is up to date.
395 (let ((begin (match-beginning 0))
396 (last-time (nth 4 form))
397 (file-time (nth 5 (file-attributes file))))
398 (if (and (or (null existing-buffer)
399 (not (buffer-modified-p existing-buffer)))
400 (listp last-time) (= (length last-time) 2)
a273d3e0 401 (not (autoload-before-p last-time file-time)))
d55e3ff0 402 (progn
d5aa62ec
RM
403 (if (interactive-p)
404 (message "\
405Autoload section for %s is up to date."
406 file))
d55e3ff0
RM
407 (setq found 'up-to-date))
408 (search-forward generate-autoload-section-trailer)
409 (delete-region begin (point))
410 (setq found t))))
411 ((string< load-name (nth 2 form))
412 ;; We've come to a section alphabetically later than
413 ;; LOAD-NAME. We assume the file is in order and so
414 ;; there must be no section for LOAD-NAME. We will
415 ;; insert one before the section here.
416 (goto-char (match-beginning 0))
72c19d97 417 (setq found 'new)))))
0f09bac6
RM
418 (or found
419 (progn
420 (setq found 'new)
421 ;; No later sections in the file. Put before the last page.
422 (goto-char (point-max))
5a161ab5 423 (search-backward "\f" nil t)))
72c19d97 424 (or (eq found 'up-to-date)
0f09bac6 425 (and (eq found 'new)
72c19d97
RM
426 ;; Check that FILE has any cookies before generating a
427 ;; new section for it.
428 (save-excursion
b59c7256
RM
429 (if existing-buffer
430 (set-buffer existing-buffer)
431 ;; It is faster to avoid visiting the file.
432 (set-buffer (get-buffer-create " *autoload-file*"))
433 (kill-all-local-variables)
434 (erase-buffer)
435 (setq buffer-undo-list t
436 buffer-read-only nil)
437 (emacs-lisp-mode)
438 (insert-file-contents file nil))
72c19d97 439 (save-excursion
b59c7256
RM
440 (save-restriction
441 (widen)
442 (goto-char (point-min))
443 (prog1
f0646ca0
RM
444 (if (re-search-forward
445 (concat "^" (regexp-quote
446 generate-autoload-cookie))
b59c7256
RM
447 nil t)
448 nil
449 (if (interactive-p)
75632953 450 (message "%s has no autoloads" file))
a273d3e0 451 (setq no-autoloads t)
b59c7256
RM
452 t)
453 (or existing-buffer
454 (kill-buffer (current-buffer))))))))
57cf354d 455 (generate-file-autoloads file))))
2336b6a9
KH
456 (and (interactive-p)
457 (buffer-modified-p)
a273d3e0
GM
458 (save-buffer))
459
460 (if no-autoloads file))))
461
462(defun autoload-before-p (time1 time2)
463 (or (< (car time1) (car time2))
464 (and (= (car time1) (car time2))
465 (< (nth 1 time1) (nth 1 time2)))))
466
467(defun autoload-remove-section (begin)
468 (goto-char begin)
469 (search-forward generate-autoload-section-trailer)
470 (delete-region begin (point)))
0231f2dc
JB
471
472;;;###autoload
3fbca58a 473(defun update-autoloads-from-directories (&rest dirs)
d08589bf 474 "\
3fbca58a 475Update loaddefs.el with all the current autoloads from DIRS, and no old ones.
b59c7256
RM
476This uses `update-file-autoloads' (which see) do its work."
477 (interactive "DUpdate autoloads from directory: ")
a273d3e0
GM
478 (let* ((files (apply 'nconc
479 (mapcar (lambda (dir)
480 (directory-files (expand-file-name dir)
481 ;; FIXME: add .gz etc...
482 t "^[^=.].*\\.el\\'"))
483 dirs)))
484 (this-time (current-time))
485 (no-autoloads nil) ;files with no autoload cookies.
486 (autoloads-file
0e193890 487 (expand-file-name generated-autoload-file
a273d3e0
GM
488 (expand-file-name "lisp" source-directory)))
489 (top-dir (file-name-directory autoloads-file)))
490
491 (with-current-buffer
492 (find-file-noselect (autoload-ensure-default-file autoloads-file))
e2b6138f 493 (save-excursion
a273d3e0
GM
494
495 ;; Canonicalize file names and remove the autoload file itself.
496 (setq files (delete (autoload-trim-file-name buffer-file-name)
497 (mapcar 'autoload-trim-file-name files)))
498
b59c7256
RM
499 (goto-char (point-min))
500 (while (search-forward generate-autoload-section-header nil t)
2336b6a9 501 (let* ((form (autoload-read-section-header))
b59c7256 502 (file (nth 3 form)))
a273d3e0
GM
503 (cond ((and (consp file) (stringp (car file)))
504 ;; This is a list of files that have no autoload cookies.
505 ;; There shouldn't be more than one such entry.
506 ;; Remove the obsolete section.
507 (autoload-remove-section (match-beginning 0))
508 (let ((last-time (nth 4 form)))
509 (dolist (file file)
510 (let ((file-time (nth 5 (file-attributes file))))
511 (when (and file-time
512 (not (autoload-before-p last-time
513 file-time)))
514 ;; file unchanged
515 (push file no-autoloads)
516 (setq files (delete file files)))))))
517 ((not (stringp file)))
3fbca58a 518 ((not (file-exists-p (expand-file-name file top-dir)))
b59c7256 519 ;; Remove the obsolete section.
a273d3e0
GM
520 (autoload-remove-section (match-beginning 0)))
521 ((equal (nth 4 form) (nth 5 (file-attributes file)))
522 ;; File hasn't changed.
523 nil)
b59c7256
RM
524 (t
525 (update-file-autoloads file)))
526 (setq files (delete file files)))))
a273d3e0
GM
527 ;; Elements remaining in FILES have no existing autoload sections yet.
528 (setq no-autoloads
529 (append no-autoloads
530 (delq nil (mapcar 'update-file-autoloads files))))
531 (when no-autoloads
532 ;; Add the `no-autoloads' section.
533 (goto-char (point-max))
534 (search-backward "\f" nil t)
535 (autoload-insert-section-header
536 (current-buffer) nil nil no-autoloads this-time)
537 (insert generate-autoload-section-trailer))
538
b59c7256 539 (save-buffer))))
0231f2dc
JB
540
541;;;###autoload
542(defun batch-update-autoloads ()
b59c7256 543 "Update loaddefs.el autoloads in batch mode.
3fbca58a
RS
544Calls `update-autoloads-from-directories' on the command line arguments."
545 (apply 'update-autoloads-from-directories command-line-args-left)
b59c7256 546 (setq command-line-args-left nil))
0231f2dc
JB
547
548(provide 'autoload)
ffd56f97 549
c0274f38 550;;; autoload.el ends here