1 ;;; scheme48.el --- A major mode for Scheme48 development
3 ;; Copyright (C) 1992 Jonathan Rees
4 ;; Copyright (C) 2005, 2006 Jorgen Schaefer
7 ;; Author: Jonathan Rees (cmuscheme48.el)
8 ;; Jorgen Schaefer <forcer@forcix.cx> (scheme48-mode)
9 ;; URL: http://www.emacswiki.org/cgi-bin/emacs/Scheme48Mode
11 ;; Redistribution and use in source and binary forms, with or without
12 ;; modification, are permitted provided that the following conditions
14 ;; 1. Redistributions of source code must retain the above copyright
15 ;; notice, this list of conditions and the following disclaimer.
16 ;; 2. Redistributions in binary form must reproduce the above copyright
17 ;; notice, this list of conditions and the following disclaimer in the
18 ;; documentation and/or other materials provided with the distribution.
19 ;; 3. The name of the authors may not be used to endorse or promote products
20 ;; derived from this software without specific prior written permission.
22 ;; THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
23 ;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 ;; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 ;; IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 ;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 ;; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 ;; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 ;; This file provides `scheme48-mode', a major mode for improved
36 ;; interaction with Scheme48. It's the same as the canonical
37 ;; `scheme-mode', but provides some commands which tell Scheme48 from
38 ;; which a specific definition came from. This allows Scheme48 to put
39 ;; the definition in the correct package by itself.
41 ;; This is based om the cmuscheme48.el which comes with Scheme48.
43 ;; You can set a buffer-local variable named `scheme48-package' to
44 ;; send definitions to that package. This can be done by file
45 ;; variables, so the following works:
47 ;; -*- mode: scheme48; scheme48-package: mypackage -*-
49 ;; To use the special packages CONFIG, USER and EXEC, use the package
50 ;; name in parens, like this:
52 ;; -*- mode: scheme48; scheme48-package: (exec) -*-
56 ;; Thanks to Taylor Campbell (Riastradh on irc.freenode.net) for his
57 ;; extensive list of indentation settings and the idea with the
58 ;; scheme48-package local variable.
60 ;; Thanks to Emilio Lopes for the idea to highlight the new keywords
68 (defcustom scheme48-compatibility-bindings-p nil
69 "Use the compatbility bindings?
70 The old cmuscheme48.el provided a few non-standard bindings,
71 which can be re-enabled by setting this variable to a non-nil
72 value before loading this file."
76 (defcustom scheme48-keywords
83 (environment-define! 2 no-font-lock)
84 (environment-set! 2 no-font-lock)
87 (make-usual-resumer 2 no-font-lock)
90 (search-tree-modify! 2 no-font-lock)
91 (usual-resumer 0 no-font-lock)
92 (with-exception-handler 1)
94 (with-interaction-environment 1)
95 (with-nondeterminism 0)
98 (call-with-current-input-port 1)
99 (call-with-current-noise-port 1)
100 (call-with-current-output-port 1)
101 (call-with-string-output-port 0)
102 (limit-output 2 no-font-lock)
103 (recurring-write 2 no-font-lock)
105 (with-current-ports 3)
107 ;; Configuration language
112 ;; These don't improve (for some, even degrade) the readability.
113 ;; (modify 1 no-font-lock)
114 ;; (subset 1 no-font-lock)
116 ;; Concurrency-related
119 (call-ensuring-atomicity 0)
120 (call-ensuring-atomicity! 0)
122 (ensure-atomicity! 0)
123 (interrupt-thread 1 no-font-lock)
126 (spawn-on-scheduler 1 no-font-lock)
127 (with-new-proposal 1)
130 (with-current-input-port 2)
131 (with-current-output-port 2)
133 (close-after 2 no-font-lock)
139 (let-optionals scheme-let-indent)
140 (let-optionals* scheme-let-indent)
152 "A list of Scheme48-related keywords.
153 The list consists of lists of the form (KEYWORD INDENT [NO-FONT-LOCK].
154 The keywords named KEYWORD will be indented according to INDENT,
155 and will also be highlighted as keywords unless NO-FONT-LOCK is
158 :type '(repeat (list symbol sexp boolean)))
160 (defvar scheme48-package nil
161 "The name of the package definitions from this file should go to.")
162 (make-variable-buffer-local 'scheme48-package)
164 (put 'scheme48-package 'safe-local-variable 'scheme48-safe-variable)
166 (defun scheme48-safe-variable (var)
167 "Return non-nil when VAR is a valid value of `scheme48-package'."
171 (or (and (null (cdr var))
173 '(config user exec)))
174 (and (null (cddr var))
175 (eq 'for-syntax (car var))
176 (or (symbolp (cadr var))
177 (stringp (cadr var))))))))
179 (defvar scheme48-mode-map
180 (let ((map (make-sparse-keymap)))
181 (define-key map "\M-\C-x" 'scheme48-send-definition) ;gnu convention
182 (define-key map "\C-x\C-e" 'scheme48-send-last-sexp) ;gnu convention
183 (define-key map "\C-c\C-e" 'scheme48-send-definition)
184 (define-key map "\C-c\M-e" 'scheme48-send-definition-and-go)
185 (define-key map "\C-c\C-r" 'scheme48-send-region)
186 (define-key map "\C-c\M-r" 'scheme48-send-region-and-go)
187 (define-key map "\C-c\C-l" 'scheme48-load-file)
188 (when scheme48-compatibility-bindings-p
189 (define-key map "\C-ce" 'scheme48-send-definition)
190 (define-key map "\C-c\C-e" 'scheme48-send-definition-and-go)
191 (define-key map "\C-cr" 'scheme48-send-region)
192 (define-key map "\C-c\C-r" 'scheme48-send-region-and-go)
193 (define-key map "\C-cl" 'scheme48-load-file))
195 "The keymap used in `scheme48-mode'.")
197 (define-derived-mode scheme48-mode scheme-mode "Scheme48"
198 "Major mode for improved Scheme48 interaction.
199 This mode is derived from `scheme-mode', so see there for
202 The commands that send code to the Scheme48 process attach
203 information as to from which file the code comes from. This
204 allows Scheme48 to put the corresponding definitions in the
205 package associated with that file name.
207 \\{scheme48-mode-map}"
208 (scheme48-initialize)
209 (set (make-local-variable 'scheme-trace-command)
211 (set (make-local-variable 'scheme-untrace-command)
213 (set (make-local-variable 'scheme-macro-expand-command)
215 (when (boundp 'package)
216 (message "The `package' local variable is deprecated. Use `scheme48-package' instead.")
217 (when (not scheme48-package)
218 (setq scheme48-package package))))
220 (defvar scheme48-mode-initialized-p nil
221 "This is non-nil when `scheme48-mode' has been initialized.
222 Set it to nil if you want the next invocation of `scheme48-mode'
223 to re-read `scheme48-keywords'.")
225 (defun scheme48-initialize ()
226 "Initialize `scheme48-mode' from `scheme48-keywords'.
227 Only run when `scheme48-mode-initialized-p' is nil.
228 This is done so that the user can modify `scheme48-keywords'
229 before the first time the mode is run, but after this package has
231 (when (not scheme48-mode-initialized-p)
232 (mapc (lambda (entry)
234 'scheme-indent-function
236 (put (intern (upcase (symbol-name (car entry))))
237 'scheme-indent-function
240 (let ((regexp (concat "("
243 (mapcar (lambda (elt)
246 (symbol-name (car elt))))
250 (font-lock-add-keywords 'scheme48-mode
251 (list (list regexp 1 'font-lock-keyword-face))))
252 (setq scheme48-mode-initialized-p t)))
254 (defun scheme48-send-region (start end)
255 "Send the current region to the inferior Scheme process."
259 (scheme48-send-with-prefix (scheme48-package-sender scheme48-package)
262 ((buffer-file-name (current-buffer))
263 (comint-send-string (scheme-proc)
264 (concat ",from-file "
265 (scheme48-enough-scheme-file-name
266 (buffer-file-name (current-buffer)))
268 (comint-send-region (scheme-proc) start end)
269 (comint-send-string (scheme-proc) " ,end\n"))
271 (comint-send-region (scheme-proc) start end)
272 (comint-send-string (scheme-proc) "\n"))))
274 (defun scheme48-send-with-prefix (prefix start end)
275 "Send all Scheme definitions in the region to the Scheme process."
276 (let ((region (buffer-substring-no-properties start end))
277 (p prefix)) ; Emacs lossage - prefix is suddenly nil below
280 ;; `backward-sexp' relies on this. Thanks to Riastradh for
281 ;; finding it out :-)
282 (set-syntax-table scheme-mode-syntax-table)
283 (set (make-local-variable 'parse-sexp-ignore-comments) t)
286 (progn (backward-sexp)
289 (insert "\n" p " ")))
290 (comint-send-region (scheme-proc)
294 (defun scheme48-send-definition ()
295 "Send the current definition to the inferior Scheme48 process."
301 (scheme48-send-region (point) end))))
303 (defun scheme48-send-last-sexp ()
304 "Send the previous sexp to the inferior Scheme process."
306 (scheme48-send-region (save-excursion (backward-sexp) (point)) (point)))
308 (defun scheme48-send-region-and-go (start end)
309 "Send the current region to the inferior Scheme48 process,
310 and switch to the process buffer."
312 (scheme48-send-region start end)
313 (switch-to-scheme t))
315 (defun scheme48-send-definition-and-go ()
316 "Send the current definition to the inferior Scheme48,
317 and switch to the process buffer."
319 (scheme48-send-definition)
320 (switch-to-scheme t))
322 (defun scheme48-load-file (file-name)
323 "Load a Scheme file into the inferior Scheme48 process."
324 (interactive (comint-get-source "Load Scheme48 file: "
325 scheme-prev-l/c-dir/file
326 scheme-source-modes t)) ; T because LOAD
327 ; needs an exact name
328 (comint-check-source file-name) ; Check to see if buffer needs saved.
329 (setq scheme-prev-l/c-dir/file (cons (file-name-directory file-name)
330 (file-name-nondirectory file-name)))
333 (concat (if scheme48-package
334 (concat (scheme48-package-sender scheme48-package)
338 (scheme48-enough-scheme-file-name file-name)
341 (defun scheme48-package-sender (package)
342 "Return the prefix to send a definition to PACKAGE."
344 ((equal package '(config))
346 ((equal package '(user))
348 ((equal package '(exec))
350 ((and (consp package)
351 (eq 'for-syntax (car package)))
352 (format ",in %s ,for-syntax" (cadr package)))
354 (format ",in %s" package))))
356 ;;; This assumes that when you load things into Scheme 48, you type
357 ;;; names of files in your home directory using the syntax "~/".
358 ;;; Similarly for current directory. Maybe we ought to send multiple
359 ;;; file names to Scheme and let it look at all of them.
361 (defcustom scheme48-home-directory-kludge t
362 "*Whether the home directory should be simplified.
364 When telling Scheme48 about the file name, it's a difference
365 whether we send a file name beginning with \"~/\" or the actual
366 expanded path name. If this is non-nil and the file name for
367 `scheme48-enough-scheme-file-name' starts with the user's home
368 directory, that is replaced with \"~/\"."
372 (defun scheme48-enough-scheme-file-name (file)
373 "Return a canonical name for FILE.
374 This will trim off the directory used in the *scheme* buffer,
375 or replace a home directory at the beginning with ~/ if
376 `scheme48-home-directory-kludge' is non-nil."
377 (let ((file (expand-file-name file))
378 (scheme-dir (with-current-buffer scheme-buffer
379 (expand-file-name default-directory))))
380 (or (scheme48-replace-prefix file scheme-dir)
381 (and scheme48-home-directory-kludge
382 (scheme48-replace-prefix file (expand-file-name "~/") "~/"))
385 (defun scheme48-replace-prefix (file prefix &optional replace)
386 "Replace PREFIX at the beginning of FILE with REPLACE, or \"\"."
387 (let ((replace (or replace ""))
388 (len (length prefix)))
389 (if (and (> (length file)
391 (string-equal (substring file 0 len)
393 (concat replace (substring file len))
397 ;;; scheme48.el ends here