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