send code snippets to scheme repl in emacs

Recently I am learning scheme for reading the book sicp. And using emacs for practicing the grammar, though the run-scheme function:

(helpful-callable 'run-scheme)

open the scheme repl that we can interactively input snippets. But still it's quite hard to use. I used geiser but still wanna a simple way to just send some code snippets from a org file buffer to the scheme repl buffer opened via run-scheme.

After doing some research on the internet1, I got a function process-send-region that may help, and implemented the the function as:

(defun scheme/send-form-to-repl (beg end)
  (interactive "r")
  (process-send-region "*scheme*" beg end)
  (process-send-string "*scheme*" "\n" ))

(defun scheme/send-defun ()
  (interactive)
  (save-excursion
    (mark-defun)
    (call-interactively 'scheme/send-form-to-repl)
    (deactivate-mark)))

(defun scheme/send-restart-1 ()
  (interactive)
  (process-send-string "*scheme*" "(restart 1)\n" ))

(global-set-key (kbd "C-c K R") 'scheme/send-defun)
(global-set-key (kbd "C-c K r") 'scheme/send-form-to-repl)
(global-set-key (kbd "C-c K C-r") 'scheme/send-restart-1)

Notes

Sometimes it's helpful to send content to some other buffer too (process-send-region can do this too). And even there are some tools helping us to send content to some other programs, for example vim-dispatch can send content from vim to tmux. emamux is a similar tool for emacs. This pattern should not be hard to implemented and should be useful in quite a time.

Footnotes


  1. Send region to shell in another buffer↩︎