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