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