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