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:
- If a region has been selected, evaluate the region
- If the cursor is positioned on or directly after a closing parens, evaluate the preceeding s-expression
- If the cursor is positioned on or directly before an openning parens, evaluate the following s-expression
- Otherwise, evaluate the top level s-expression that the cursor is located inside of
(add-hook 'scheme-mode-hook (lambda () (interactive) (define-key scheme-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.(add-hook 'Info-mode-hook '(lambda () (interactive) (define-key Info-mode-map [(control c) (x)] 'scheme-send-dwim)))

