Clementson's Blog

Bits and pieces (mostly Lisp-related) that I collect from the ether.

December 2003
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Nov  Jan

A useful function when working with Scheme in Emacs - Eval DWIM

Thursday, December 4, 2003

When I'm working with Scheme in Emacs, I find the following function quite useful:

;; "Do What I Mean" evaluation
(defun scheme-send-dwim (arg)
  "Send the appropriate forms to Scheme to be evaluated."
  (interactive "P")
  (save-excursion
    (cond 
     ;;Region selected - evaluate region
     ((not (equal mark-active nil))
      (scheme-send-region (mark) (point)))
     ;; At/after sexp - evaluate last sexp
     ((or (looking-at "\s)")
	  (save-excursion
	    (backward-char 1)
	    (looking-at "\s)")))
      (if (looking-at "\s)")
	  (forward-char 1)) 
      (scheme-send-last-sexp))
     ;; At/before sexp - evaluate next sexp
     ((or (looking-at "\s(")
	  (save-excursion
	    (forward-char 1)
	    (looking-at "\s(")))
      (if (looking-at "\s(")
	  (forward-list 1)) 
      (scheme-send-last-sexp))
     ;; Default - evaluate enclosing top-level sexp
     (t (scheme-send-definition)))
    (if arg (switch-to-scheme t))))
It performs a Scheme evaluation based on the following criteria:I bind it to 'C-c x' in scheme-mode and in Info-mode with the following:
(add-hook 'scheme-mode-hook
	  (lambda ()
	    (interactive)
	    (define-key scheme-mode-map [(control c) (x)] 'scheme-send-dwim)))

(add-hook 'Info-mode-hook '(lambda () (interactive) (define-key Info-mode-map [(control c) (x)] 'scheme-send-dwim)))

I bind it to the same key in Info mode because I often find it useful to be able to evaluate Scheme code from within an info document (e.g. -- Info versions of SICP, R5RS, etc.). If you use a prefix argument (e.g. -- you press "C-u C-c x" instead of just "C-c x"), focus is also changed to the *scheme* buffer. Note: When using the function from Info-mode, Scheme needs to have already been started.

emacs Copyright © 2004 by Bill Clementson