Update FSF's address.
[bpt/emacs.git] / lisp / progmodes / scheme.el
1 ;;; scheme.el --- Scheme mode, and its idiosyncratic commands.
2
3 ;; Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
4
5 ;; Author: Bill Rozas <jinz@prep.ai.mit.edu>
6 ;; Keywords: languages, lisp
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; Adapted from Lisp mode by Bill Rozas, jinx@prep.
28 ;; Initially a query replace of Lisp mode, except for the indentation
29 ;; of special forms. Probably the code should be merged at some point
30 ;; so that there is sharing between both libraries.
31
32 ;;; Code:
33 \f
34 (defvar scheme-mode-syntax-table nil "")
35 (if (not scheme-mode-syntax-table)
36 (let ((i 0))
37 (setq scheme-mode-syntax-table (make-syntax-table))
38 (set-syntax-table scheme-mode-syntax-table)
39
40 ;; Default is atom-constituent.
41 (while (< i 256)
42 (modify-syntax-entry i "_ ")
43 (setq i (1+ i)))
44
45 ;; Word components.
46 (setq i ?0)
47 (while (<= i ?9)
48 (modify-syntax-entry i "w ")
49 (setq i (1+ i)))
50 (setq i ?A)
51 (while (<= i ?Z)
52 (modify-syntax-entry i "w ")
53 (setq i (1+ i)))
54 (setq i ?a)
55 (while (<= i ?z)
56 (modify-syntax-entry i "w ")
57 (setq i (1+ i)))
58
59 ;; Whitespace
60 (modify-syntax-entry ?\t " ")
61 (modify-syntax-entry ?\n "> ")
62 (modify-syntax-entry ?\f " ")
63 (modify-syntax-entry ?\r " ")
64 (modify-syntax-entry ? " ")
65
66 ;; These characters are delimiters but otherwise undefined.
67 ;; Brackets and braces balance for editing convenience.
68 (modify-syntax-entry ?[ "(] ")
69 (modify-syntax-entry ?] ")[ ")
70 (modify-syntax-entry ?{ "(} ")
71 (modify-syntax-entry ?} "){ ")
72 (modify-syntax-entry ?\| " 23")
73
74 ;; Other atom delimiters
75 (modify-syntax-entry ?\( "() ")
76 (modify-syntax-entry ?\) ")( ")
77 (modify-syntax-entry ?\; "< ")
78 (modify-syntax-entry ?\" "\" ")
79 (modify-syntax-entry ?' " p")
80 (modify-syntax-entry ?` " p")
81
82 ;; Special characters
83 (modify-syntax-entry ?, "_ p")
84 (modify-syntax-entry ?@ "_ p")
85 (modify-syntax-entry ?# "_ p14")
86 (modify-syntax-entry ?\\ "\\ ")))
87 \f
88 (defvar scheme-mode-abbrev-table nil "")
89 (define-abbrev-table 'scheme-mode-abbrev-table ())
90
91 (defun scheme-mode-variables ()
92 (set-syntax-table scheme-mode-syntax-table)
93 (setq local-abbrev-table scheme-mode-abbrev-table)
94 (make-local-variable 'paragraph-start)
95 (setq paragraph-start (concat "$\\|" page-delimiter))
96 (make-local-variable 'paragraph-separate)
97 (setq paragraph-separate paragraph-start)
98 (make-local-variable 'paragraph-ignore-fill-prefix)
99 (setq paragraph-ignore-fill-prefix t)
100 (make-local-variable 'indent-line-function)
101 (setq indent-line-function 'scheme-indent-line)
102 (make-local-variable 'parse-sexp-ignore-comments)
103 (setq parse-sexp-ignore-comments t)
104 (make-local-variable 'comment-start)
105 (setq comment-start ";")
106 (make-local-variable 'comment-start-skip)
107 (setq comment-start-skip ";+[ \t]*")
108 (make-local-variable 'comment-column)
109 (setq comment-column 40)
110 (make-local-variable 'comment-indent-function)
111 (setq comment-indent-function 'scheme-comment-indent)
112 (make-local-variable 'parse-sexp-ignore-comments)
113 (setq parse-sexp-ignore-comments t)
114 (setq mode-line-process '("" scheme-mode-line-process)))
115
116 (defvar scheme-mode-line-process "")
117
118 (defun scheme-mode-commands (map)
119 (define-key map "\t" 'scheme-indent-line)
120 (define-key map "\177" 'backward-delete-char-untabify)
121 (define-key map "\e\C-q" 'scheme-indent-sexp))
122
123 (defvar scheme-mode-map nil)
124 (if (not scheme-mode-map)
125 (progn
126 (setq scheme-mode-map (make-sparse-keymap))
127 (scheme-mode-commands scheme-mode-map)))
128 \f
129 ;;;###autoload
130 (defun scheme-mode ()
131 "Major mode for editing Scheme code.
132 Editing commands are similar to those of lisp-mode.
133
134 In addition, if an inferior Scheme process is running, some additional
135 commands will be defined, for evaluating expressions and controlling
136 the interpreter, and the state of the process will be displayed in the
137 modeline of all Scheme buffers. The names of commands that interact
138 with the Scheme process start with \"xscheme-\". For more information
139 see the documentation for xscheme-interaction-mode.
140
141 Commands:
142 Delete converts tabs to spaces as it moves back.
143 Blank lines separate paragraphs. Semicolons start comments.
144 \\{scheme-mode-map}
145 Entry to this mode calls the value of scheme-mode-hook
146 if that value is non-nil."
147 (interactive)
148 (kill-all-local-variables)
149 (scheme-mode-initialize)
150 (scheme-mode-variables)
151 (run-hooks 'scheme-mode-hook))
152
153 (defun scheme-mode-initialize ()
154 (use-local-map scheme-mode-map)
155 (setq major-mode 'scheme-mode)
156 (setq mode-name "Scheme"))
157
158 (defvar scheme-mit-dialect t
159 "If non-nil, scheme mode is specialized for MIT Scheme.
160 Set this to nil if you normally use another dialect.")
161 \f
162 (defun scheme-comment-indent (&optional pos)
163 (save-excursion
164 (if pos (goto-char pos))
165 (cond ((looking-at ";;;") (current-column))
166 ((looking-at ";;")
167 (let ((tem (calculate-scheme-indent)))
168 (if (listp tem) (car tem) tem)))
169 (t
170 (skip-chars-backward " \t")
171 (max (if (bolp) 0 (1+ (current-column)))
172 comment-column)))))
173
174 (defvar scheme-indent-offset nil "")
175 (defvar scheme-indent-function 'scheme-indent-function "")
176
177 (defun scheme-indent-line (&optional whole-exp)
178 "Indent current line as Scheme code.
179 With argument, indent any additional lines of the same expression
180 rigidly along with this one."
181 (interactive "P")
182 (let ((indent (calculate-scheme-indent)) shift-amt beg end
183 (pos (- (point-max) (point))))
184 (beginning-of-line)
185 (setq beg (point))
186 (skip-chars-forward " \t")
187 (if (looking-at "[ \t]*;;;")
188 ;; Don't alter indentation of a ;;; comment line.
189 nil
190 (if (listp indent) (setq indent (car indent)))
191 (setq shift-amt (- indent (current-column)))
192 (if (zerop shift-amt)
193 nil
194 (delete-region beg (point))
195 (indent-to indent))
196 ;; If initial point was within line's indentation,
197 ;; position after the indentation. Else stay at same point in text.
198 (if (> (- (point-max) pos) (point))
199 (goto-char (- (point-max) pos)))
200 ;; If desired, shift remaining lines of expression the same amount.
201 (and whole-exp (not (zerop shift-amt))
202 (save-excursion
203 (goto-char beg)
204 (forward-sexp 1)
205 (setq end (point))
206 (goto-char beg)
207 (forward-line 1)
208 (setq beg (point))
209 (> end beg))
210 (indent-code-rigidly beg end shift-amt)))))
211 \f
212 (defun calculate-scheme-indent (&optional parse-start)
213 "Return appropriate indentation for current line as scheme code.
214 In usual case returns an integer: the column to indent to.
215 Can instead return a list, whose car is the column to indent to.
216 This means that following lines at the same level of indentation
217 should not necessarily be indented the same way.
218 The second element of the list is the buffer position
219 of the start of the containing expression."
220 (save-excursion
221 (beginning-of-line)
222 (let ((indent-point (point)) state paren-depth desired-indent (retry t)
223 last-sexp containing-sexp first-sexp-list-p)
224 (if parse-start
225 (goto-char parse-start)
226 (beginning-of-defun))
227 ;; Find outermost containing sexp
228 (while (< (point) indent-point)
229 (setq state (parse-partial-sexp (point) indent-point 0)))
230 ;; Find innermost containing sexp
231 (while (and retry (setq paren-depth (car state)) (> paren-depth 0))
232 (setq retry nil)
233 (setq last-sexp (nth 2 state))
234 (setq containing-sexp (car (cdr state)))
235 ;; Position following last unclosed open.
236 (goto-char (1+ containing-sexp))
237 ;; Is there a complete sexp since then?
238 (if (and last-sexp (> last-sexp (point)))
239 ;; Yes, but is there a containing sexp after that?
240 (let ((peek (parse-partial-sexp last-sexp indent-point 0)))
241 (if (setq retry (car (cdr peek))) (setq state peek))))
242 (if (not retry)
243 ;; Innermost containing sexp found
244 (progn
245 (goto-char (1+ containing-sexp))
246 (if (not last-sexp)
247 ;; indent-point immediately follows open paren.
248 ;; Don't call hook.
249 (setq desired-indent (current-column))
250 ;; Move to first sexp after containing open paren
251 (parse-partial-sexp (point) last-sexp 0 t)
252 (setq first-sexp-list-p (looking-at "\\s("))
253 (cond
254 ((> (save-excursion (forward-line 1) (point))
255 last-sexp)
256 ;; Last sexp is on same line as containing sexp.
257 ;; It's almost certainly a function call.
258 (parse-partial-sexp (point) last-sexp 0 t)
259 (if (/= (point) last-sexp)
260 ;; Indent beneath first argument or, if only one sexp
261 ;; on line, indent beneath that.
262 (progn (forward-sexp 1)
263 (parse-partial-sexp (point) last-sexp 0 t)))
264 (backward-prefix-chars))
265 (t
266 ;; Indent beneath first sexp on same line as last-sexp.
267 ;; Again, it's almost certainly a function call.
268 (goto-char last-sexp)
269 (beginning-of-line)
270 (parse-partial-sexp (point) last-sexp 0 t)
271 (backward-prefix-chars)))))))
272 ;; If looking at a list, don't call hook.
273 (if first-sexp-list-p
274 (setq desired-indent (current-column)))
275 ;; Point is at the point to indent under unless we are inside a string.
276 ;; Call indentation hook except when overridden by scheme-indent-offset
277 ;; or if the desired indentation has already been computed.
278 (cond ((car (nthcdr 3 state))
279 ;; Inside a string, don't change indentation.
280 (goto-char indent-point)
281 (skip-chars-forward " \t")
282 (setq desired-indent (current-column)))
283 ((and (integerp scheme-indent-offset) containing-sexp)
284 ;; Indent by constant offset
285 (goto-char containing-sexp)
286 (setq desired-indent (+ scheme-indent-offset (current-column))))
287 ((not (or desired-indent
288 (and (boundp 'scheme-indent-function)
289 scheme-indent-function
290 (not retry)
291 (setq desired-indent
292 (funcall scheme-indent-function
293 indent-point state)))))
294 ;; Use default indentation if not computed yet
295 (setq desired-indent (current-column))))
296 desired-indent)))
297 \f
298 (defun scheme-indent-function (indent-point state)
299 (let ((normal-indent (current-column)))
300 (save-excursion
301 (goto-char (1+ (car (cdr state))))
302 (re-search-forward "\\sw\\|\\s_")
303 (if (/= (point) (car (cdr state)))
304 (let ((function (buffer-substring (progn (forward-char -1) (point))
305 (progn (forward-sexp 1) (point))))
306 method)
307 ;; Who cares about this, really?
308 ;(if (not (string-match "\\\\\\||" function)))
309 (setq function (downcase function))
310 (setq method (get (intern-soft function) 'scheme-indent-function))
311 (cond ((integerp method)
312 (scheme-indent-specform method state indent-point))
313 (method
314 (funcall method state indent-point))
315 ((and (> (length function) 3)
316 (string-equal (substring function 0 3) "def"))
317 (scheme-indent-defform state indent-point))))))))
318
319 (defvar scheme-body-indent 2 "")
320 \f
321 (defun scheme-indent-specform (count state indent-point)
322 (let ((containing-form-start (car (cdr state))) (i count)
323 body-indent containing-form-column)
324 ;; Move to the start of containing form, calculate indentation
325 ;; to use for non-distinguished forms (> count), and move past the
326 ;; function symbol. scheme-indent-function guarantees that there is at
327 ;; least one word or symbol character following open paren of containing
328 ;; form.
329 (goto-char containing-form-start)
330 (setq containing-form-column (current-column))
331 (setq body-indent (+ scheme-body-indent containing-form-column))
332 (forward-char 1)
333 (forward-sexp 1)
334 ;; Now find the start of the last form.
335 (parse-partial-sexp (point) indent-point 1 t)
336 (while (and (< (point) indent-point)
337 (condition-case nil
338 (progn
339 (setq count (1- count))
340 (forward-sexp 1)
341 (parse-partial-sexp (point) indent-point 1 t))
342 (error nil))))
343 ;; Point is sitting on first character of last (or count) sexp.
344 (cond ((> count 0)
345 ;; A distinguished form. Use double scheme-body-indent.
346 (list (+ containing-form-column (* 2 scheme-body-indent))
347 containing-form-start))
348 ;; A non-distinguished form. Use body-indent if there are no
349 ;; distinguished forms and this is the first undistinguished
350 ;; form, or if this is the first undistinguished form and
351 ;; the preceding distinguished form has indentation at least
352 ;; as great as body-indent.
353 ((and (= count 0)
354 (or (= i 0)
355 (<= body-indent normal-indent)))
356 body-indent)
357 (t
358 normal-indent))))
359
360 (defun scheme-indent-defform (state indent-point)
361 (goto-char (car (cdr state)))
362 (forward-line 1)
363 (if (> (point) (car (cdr (cdr state))))
364 (progn
365 (goto-char (car (cdr state)))
366 (+ scheme-body-indent (current-column)))))
367 \f
368 ;;; Let is different in Scheme
369
370 (defun would-be-symbol (string)
371 (not (string-equal (substring string 0 1) "(")))
372
373 (defun next-sexp-as-string ()
374 ;; Assumes that protected by a save-excursion
375 (forward-sexp 1)
376 (let ((the-end (point)))
377 (backward-sexp 1)
378 (buffer-substring (point) the-end)))
379
380 ;; This is correct but too slow.
381 ;; The one below works almost always.
382 ;;(defun scheme-let-indent (state indent-point)
383 ;; (if (would-be-symbol (next-sexp-as-string))
384 ;; (scheme-indent-specform 2 state indent-point)
385 ;; (scheme-indent-specform 1 state indent-point)))
386
387 (defun scheme-let-indent (state indent-point)
388 (skip-chars-forward " \t")
389 (if (looking-at "[-a-zA-Z0-9+*/?!@$%^&_:~]")
390 (scheme-indent-specform 2 state indent-point)
391 (scheme-indent-specform 1 state indent-point)))
392
393 ;; (put 'begin 'scheme-indent-function 0), say, causes begin to be indented
394 ;; like defun if the first form is placed on the next line, otherwise
395 ;; it is indented like any other form (i.e. forms line up under first).
396
397 (put 'begin 'scheme-indent-function 0)
398 (put 'case 'scheme-indent-function 1)
399 (put 'delay 'scheme-indent-function 0)
400 (put 'do 'scheme-indent-function 2)
401 (put 'lambda 'scheme-indent-function 1)
402 (put 'let 'scheme-indent-function 'scheme-let-indent)
403 (put 'let* 'scheme-indent-function 1)
404 (put 'letrec 'scheme-indent-function 1)
405 (put 'sequence 'scheme-indent-function 0)
406
407 (put 'call-with-input-file 'scheme-indent-function 1)
408 (put 'with-input-from-file 'scheme-indent-function 1)
409 (put 'with-input-from-port 'scheme-indent-function 1)
410 (put 'call-with-output-file 'scheme-indent-function 1)
411 (put 'with-output-to-file 'scheme-indent-function 1)
412 (put 'with-output-to-port 'scheme-indent-function 1)
413 \f
414 ;;;; MIT Scheme specific indentation.
415
416 (if scheme-mit-dialect
417 (progn
418 (put 'fluid-let 'scheme-indent-function 1)
419 (put 'in-package 'scheme-indent-function 1)
420 (put 'let-syntax 'scheme-indent-function 1)
421 (put 'local-declare 'scheme-indent-function 1)
422 (put 'macro 'scheme-indent-function 1)
423 (put 'make-environment 'scheme-indent-function 0)
424 (put 'named-lambda 'scheme-indent-function 1)
425 (put 'using-syntax 'scheme-indent-function 1)
426
427 (put 'with-input-from-string 'scheme-indent-function 1)
428 (put 'with-output-to-string 'scheme-indent-function 0)
429 (put 'with-values 'scheme-indent-function 1)
430
431 (put 'syntax-table-define 'scheme-indent-function 2)
432 (put 'list-transform-positive 'scheme-indent-function 1)
433 (put 'list-transform-negative 'scheme-indent-function 1)
434 (put 'list-search-positive 'scheme-indent-function 1)
435 (put 'list-search-negative 'scheme-indent-function 1)
436
437 (put 'access-components 'scheme-indent-function 1)
438 (put 'assignment-components 'scheme-indent-function 1)
439 (put 'combination-components 'scheme-indent-function 1)
440 (put 'comment-components 'scheme-indent-function 1)
441 (put 'conditional-components 'scheme-indent-function 1)
442 (put 'disjunction-components 'scheme-indent-function 1)
443 (put 'declaration-components 'scheme-indent-function 1)
444 (put 'definition-components 'scheme-indent-function 1)
445 (put 'delay-components 'scheme-indent-function 1)
446 (put 'in-package-components 'scheme-indent-function 1)
447 (put 'lambda-components 'scheme-indent-function 1)
448 (put 'lambda-components* 'scheme-indent-function 1)
449 (put 'lambda-components** 'scheme-indent-function 1)
450 (put 'open-block-components 'scheme-indent-function 1)
451 (put 'pathname-components 'scheme-indent-function 1)
452 (put 'procedure-components 'scheme-indent-function 1)
453 (put 'sequence-components 'scheme-indent-function 1)
454 (put 'unassigned\?-components 'scheme-indent-function 1)
455 (put 'unbound\?-components 'scheme-indent-function 1)
456 (put 'variable-components 'scheme-indent-function 1)))
457 \f
458 (defun scheme-indent-sexp ()
459 "Indent each line of the list starting just after point."
460 (interactive)
461 (let ((indent-stack (list nil)) (next-depth 0) bol
462 outer-loop-done inner-loop-done state this-indent)
463 (save-excursion (forward-sexp 1))
464 (save-excursion
465 (setq outer-loop-done nil)
466 (while (not outer-loop-done)
467 (setq last-depth next-depth
468 innerloop-done nil)
469 (while (and (not innerloop-done)
470 (not (setq outer-loop-done (eobp))))
471 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
472 nil nil state))
473 (setq next-depth (car state))
474 (if (car (nthcdr 4 state))
475 (progn (indent-for-comment)
476 (end-of-line)
477 (setcar (nthcdr 4 state) nil)))
478 (if (car (nthcdr 3 state))
479 (progn
480 (forward-line 1)
481 (setcar (nthcdr 5 state) nil))
482 (setq innerloop-done t)))
483 (if (setq outer-loop-done (<= next-depth 0))
484 nil
485 (while (> last-depth next-depth)
486 (setq indent-stack (cdr indent-stack)
487 last-depth (1- last-depth)))
488 (while (< last-depth next-depth)
489 (setq indent-stack (cons nil indent-stack)
490 last-depth (1+ last-depth)))
491 (forward-line 1)
492 (setq bol (point))
493 (skip-chars-forward " \t")
494 (if (or (eobp) (looking-at "[;\n]"))
495 nil
496 (if (and (car indent-stack)
497 (>= (car indent-stack) 0))
498 (setq this-indent (car indent-stack))
499 (let ((val (calculate-scheme-indent
500 (if (car indent-stack) (- (car indent-stack))))))
501 (if (integerp val)
502 (setcar indent-stack
503 (setq this-indent val))
504 (if (cdr val)
505 (setcar indent-stack (- (car (cdr val)))))
506 (setq this-indent (car val)))))
507 (if (/= (current-column) this-indent)
508 (progn (delete-region bol (point))
509 (indent-to this-indent)))))))))
510
511 (provide 'scheme)
512
513 ;;; scheme.el ends here