Mercurial > hg > xemacs-beta
comparison lisp/modes/scheme.el @ 0:376386a54a3c r19-14
Import from CVS: tag r19-14
author | cvs |
---|---|
date | Mon, 13 Aug 2007 08:45:50 +0200 |
parents | |
children | ac2d302a0011 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:376386a54a3c |
---|---|
1 ;;; scheme.el --- Scheme mode, and its idiosyncratic commands. | |
2 ;; Keywords: languages, lisp | |
3 | |
4 ;; Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc. | |
5 ;; Adapted from Lisp mode by Bill Rozas, jinx@prep. | |
6 | |
7 ;; This file is part of XEmacs. | |
8 | |
9 ;; XEmacs is free software; you can redistribute it and/or modify it | |
10 ;; under the terms of the GNU General Public License as published by | |
11 ;; the Free Software Foundation; either version 2, or (at your option) | |
12 ;; any later version. | |
13 | |
14 ;; XEmacs is distributed in the hope that it will be useful, but | |
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 ;; General Public License for more details. | |
18 | |
19 ;; You should have received a copy of the GNU General Public License | |
20 ;; along with XEmacs; see the file COPYING. If not, write to the Free | |
21 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 | |
23 | |
24 ;; Initially a query replace of Lisp mode, except for the indentation | |
25 ;; of special forms. Probably the code should be merged at some point | |
26 ;; so that there is sharing between both libraries. | |
27 | |
28 (provide 'scheme) | |
29 | |
30 (defvar scheme-mode-syntax-table nil "") | |
31 (if (not scheme-mode-syntax-table) | |
32 (let ((i 0)) | |
33 (setq scheme-mode-syntax-table (make-syntax-table)) | |
34 (set-syntax-table scheme-mode-syntax-table) | |
35 | |
36 ;; Default is atom-constituent. | |
37 (while (< i 256) | |
38 (modify-syntax-entry i "_ ") | |
39 (setq i (1+ i))) | |
40 | |
41 ;; Word components. | |
42 (setq i ?0) | |
43 (while (<= i ?9) | |
44 (modify-syntax-entry i "w ") | |
45 (setq i (1+ i))) | |
46 (setq i ?A) | |
47 (while (<= i ?Z) | |
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 | |
55 ;; Whitespace | |
56 (modify-syntax-entry ?\t " ") | |
57 (modify-syntax-entry ?\n "> ") | |
58 (modify-syntax-entry ?\f " ") | |
59 (modify-syntax-entry ?\r " ") | |
60 (modify-syntax-entry ? " ") | |
61 | |
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") | |
69 | |
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") | |
77 | |
78 ;; Special characters | |
79 (modify-syntax-entry ?, "_ p") | |
80 (modify-syntax-entry ?@ "_ p") | |
81 (modify-syntax-entry ?# "_ p14") | |
82 (modify-syntax-entry ?\\ "\\ "))) | |
83 | |
84 (defvar scheme-mode-abbrev-table nil "") | |
85 (define-abbrev-table 'scheme-mode-abbrev-table ()) | |
86 | |
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))) | |
107 | |
108 (defvar scheme-mode-line-process "") | |
109 | |
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)) | |
114 | |
115 (defvar scheme-mode-map nil) | |
116 (if (not scheme-mode-map) | |
117 (progn | |
118 (setq scheme-mode-map (make-sparse-keymap)) | |
119 (scheme-mode-commands scheme-mode-map))) | |
120 | |
121 ;;;###autoload | |
122 (defun scheme-mode () | |
123 "Major mode for editing Scheme code. | |
124 Editing commands are similar to those of lisp-mode. | |
125 | |
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. | |
132 | |
133 Commands: | |
134 Delete converts tabs to spaces as it moves back. | |
135 Blank lines separate paragraphs. Semicolons start comments. | |
136 \\{scheme-mode-map} | |
137 Entry to this mode calls the value of scheme-mode-hook | |
138 if that value is non-nil." | |
139 (interactive) | |
140 (kill-all-local-variables) | |
141 (scheme-mode-initialize) | |
142 (scheme-mode-variables) | |
143 (run-hooks 'scheme-mode-hook)) | |
144 | |
145 (defun scheme-mode-initialize () | |
146 (use-local-map scheme-mode-map) | |
147 (setq major-mode 'scheme-mode) | |
148 (setq mode-name "Scheme")) | |
149 | |
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.") | |
153 | |
154 (defun scheme-comment-indent (&optional pos) | |
155 (save-excursion | |
156 (if pos (goto-char pos)) | |
157 (cond ((looking-at ";;;") (current-column)) | |
158 ((looking-at ";;") | |
159 (let ((tem (calculate-scheme-indent))) | |
160 (if (listp tem) (car tem) tem))) | |
161 (t | |
162 (skip-chars-backward " \t") | |
163 (max (if (bolp) 0 (1+ (current-column))) | |
164 comment-column))))) | |
165 | |
166 (defvar scheme-indent-offset nil "") | |
167 (defvar scheme-indent-function 'scheme-indent-function "") | |
168 | |
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." | |
173 (interactive "P") | |
174 (let ((indent (calculate-scheme-indent)) shift-amt beg end | |
175 (pos (- (point-max) (point)))) | |
176 (beginning-of-line) | |
177 (setq beg (point)) | |
178 (skip-chars-forward " \t") | |
179 (if (looking-at "[ \t]*;;;") | |
180 ;; Don't alter indentation of a ;;; comment line. | |
181 nil | |
182 (if (listp indent) (setq indent (car indent))) | |
183 (setq shift-amt (- indent (current-column))) | |
184 (if (zerop shift-amt) | |
185 nil | |
186 (delete-region beg (point)) | |
187 (indent-to indent)) | |
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)) | |
194 (save-excursion | |
195 (goto-char beg) | |
196 (forward-sexp 1) | |
197 (setq end (point)) | |
198 (goto-char beg) | |
199 (forward-line 1) | |
200 (setq beg (point)) | |
201 (> end beg)) | |
202 (indent-code-rigidly beg end shift-amt))))) | |
203 | |
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." | |
212 (save-excursion | |
213 (beginning-of-line) | |
214 (let ((indent-point (point)) state paren-depth desired-indent (retry t) | |
215 last-sexp containing-sexp first-sexp-list-p) | |
216 (if parse-start | |
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)) | |
224 (setq retry nil) | |
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)))) | |
234 (if (not retry) | |
235 ;; Innermost containing sexp found | |
236 (progn | |
237 (goto-char (1+ containing-sexp)) | |
238 (if (not last-sexp) | |
239 ;; indent-point immediately follows open paren. | |
240 ;; Don't call hook. | |
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(")) | |
245 (cond | |
246 ((> (save-excursion (forward-line 1) (point)) | |
247 last-sexp) | |
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)) | |
257 (t | |
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) | |
261 (beginning-of-line) | |
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 | |
282 (not retry) | |
283 (setq desired-indent | |
284 (funcall scheme-indent-function | |
285 indent-point state))))) | |
286 ;; Use default indentation if not computed yet | |
287 (setq desired-indent (current-column)))) | |
288 desired-indent))) | |
289 | |
290 (defun scheme-indent-function (indent-point state) | |
291 (let ((normal-indent (current-column))) | |
292 (save-excursion | |
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)))) | |
298 method) | |
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)) | |
305 (method | |
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)))))))) | |
310 | |
311 (defvar scheme-body-indent 2 "") | |
312 | |
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 | |
320 ;; form. | |
321 (goto-char containing-form-start) | |
322 (setq containing-form-column (current-column)) | |
323 (setq body-indent (+ scheme-body-indent containing-form-column)) | |
324 (forward-char 1) | |
325 (forward-sexp 1) | |
326 ;; Now find the start of the last form. | |
327 (parse-partial-sexp (point) indent-point 1 t) | |
328 (while (and (< (point) indent-point) | |
329 (condition-case nil | |
330 (progn | |
331 (setq count (1- count)) | |
332 (forward-sexp 1) | |
333 (parse-partial-sexp (point) indent-point 1 t)) | |
334 (error nil)))) | |
335 ;; Point is sitting on first character of last (or count) sexp. | |
336 (cond ((> count 0) | |
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. | |
345 ((and (= count 0) | |
346 (or (= i 0) | |
347 (<= body-indent normal-indent))) | |
348 body-indent) | |
349 (t | |
350 normal-indent)))) | |
351 | |
352 (defun scheme-indent-defform (state indent-point) | |
353 (goto-char (car (cdr state))) | |
354 (forward-line 1) | |
355 (if (> (point) (car (cdr (cdr state)))) | |
356 (progn | |
357 (goto-char (car (cdr state))) | |
358 (+ scheme-body-indent (current-column))))) | |
359 | |
360 ;;; Let is different in Scheme | |
361 | |
362 (defun would-be-symbol (string) | |
363 (not (string-equal (substring string 0 1) "("))) | |
364 | |
365 (defun next-sexp-as-string () | |
366 ;; Assumes that protected by a save-excursion | |
367 (forward-sexp 1) | |
368 (let ((the-end (point))) | |
369 (backward-sexp 1) | |
370 (buffer-substring (point) the-end))) | |
371 | |
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))) | |
378 | |
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))) | |
384 | |
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). | |
388 | |
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) | |
398 | |
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) | |
405 | |
406 ;;;; MIT Scheme specific indentation. | |
407 | |
408 (if scheme-mit-dialect | |
409 (progn | |
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) | |
418 | |
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) | |
422 | |
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) | |
428 | |
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))) | |
449 | |
450 (defun scheme-indent-sexp () | |
451 "Indent each line of the list starting just after point." | |
452 (interactive) | |
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)) | |
456 (save-excursion | |
457 (setq outer-loop-done nil) | |
458 (while (not outer-loop-done) | |
459 (setq last-depth next-depth | |
460 innerloop-done nil) | |
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)) | |
464 nil nil state)) | |
465 (setq next-depth (car state)) | |
466 (if (car (nthcdr 4 state)) | |
467 (progn (indent-for-comment) | |
468 (end-of-line) | |
469 (setcar (nthcdr 4 state) nil))) | |
470 (if (car (nthcdr 3 state)) | |
471 (progn | |
472 (forward-line 1) | |
473 (setcar (nthcdr 5 state) nil)) | |
474 (setq innerloop-done t))) | |
475 (if (setq outer-loop-done (<= next-depth 0)) | |
476 nil | |
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))) | |
483 (forward-line 1) | |
484 (setq bol (point)) | |
485 (skip-chars-forward " \t") | |
486 (if (or (eobp) (looking-at "[;\n]")) | |
487 nil | |
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)))))) | |
493 (if (integerp val) | |
494 (setcar 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))))))))) |