Skip to content

Instantly share code, notes, and snippets.

@Altech
Created April 22, 2013 11:34
Show Gist options
  • Save Altech/5434087 to your computer and use it in GitHub Desktop.
Save Altech/5434087 to your computer and use it in GitHub Desktop.
commands for interactive programming with GHCi in emacs.
(defun altech-define-haskell-hook ()
(defun code-to-list (str)
(split-string
(replace-regexp-in-string "^\n+" ""
(replace-regexp-in-string "\n+$" "" str)) "\n"))
(defun send-line-to-ghci (line)
(insert line)
(eshell-send-input)
(sleep-for 0.2)
(goto-char (point-max)))
(defun send-code-to-ghci (with-let)
(let* ((is-use-region (use-region-p)) (code (if is-use-region (code-to-list (buffer-substring-no-properties (region-beginning) (region-end))))))
(unless (get-buffer "*ghci*")
(with-current-buffer (generate-new-buffer "*ghci*")
(eshell-mode)
(send-line-to-ghci "ghci")
(sleep-for 1)))
(progn
(with-current-buffer "*ghci*"
(goto-char (point-max))
(if (not is-use-region)
(send-line-to-ghci ":l test")
(princ code)
(send-line-to-ghci ":{")
(if with-let
(send-line-to-ghci "let"))
(dolist (line code)
(send-line-to-ghci line))
(send-line-to-ghci ":}"))))))
(defun send-code-to-ghci-and-switch (with-let)
(send-code-to-ghci with-let)
(other-window-or-split)
(switch-to-buffer "*ghci*")
(goto-char (point-max))
(end-of-buffer)
(recenter))
(local-set-key (kbd "M-RET") (lambda () (interactive) (send-code-to-ghci-and-switch t)))
(local-set-key (kbd "C-M-g") (lambda () (interactive) (send-code-to-ghci-and-switch nil))))
(add-hook 'haskell-mode-hook 'altech-define-haskell-hook)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment