elixir repl with emacs

There was one talk from Timothy Baldrige(core.async talk) where he was using fancy keyboard shortcut to send closure code to repl automatically and it looked so nice he would just put cursor somewhere and BAM.
Now getting that stuff to work for elixir is a bit tricky because it is tricky to get expression under cursor but next best thing is to get stuff that is selected and send it to repl.
so here is how to kinda do it:
The problem is that iex repl is struggling with newlines if we send one it will execute.

Following code removes newlines but then one needs to write code in somewhat ugly way with inserting semicolons between expressions.
But for debugging it might be good enough

(defun append-to-buffer ()
"Append the text of the region to BUFFER."
(interactive)
(let ((oldbuf (current-buffer)
              )
(p1 (region-beginning))
(p2 (region-end))
 (text (buffer-substring (region-beginning) (region-end)))
)
(set-buffer (get-buffer-create "*IEX*"))

(insert (replace-regexp-in-string "\n" " " (replace-regexp-in-string "#.*?\n" "" text)))(switch-to-buffer-other-window "*IEX*")
)
(global-set-key (kbd "M-`") 'append-to-buffer))

this is the code modifications that needs to be put in place in order for this to work
one has to put ; to separate expressions 🙁

defmodule Testapp do
  use Application;

  # See http://elixir-lang.org/docs/stable/elixir/Application.html
  # for more information on OTP Applications
  def start(_type, _args) do
    import Supervisor.Spec, warn: false ;

    children = [
      # Define workers and child supervisors to be supervised
      # worker(Testapp.Worker, [arg1, arg2, arg3])
    ];

    # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: Testapp.Supervisor]
   ; Supervisor.start_link(children, opts)
  end

then just type emacs command to type commands and then elixir-mode-iex
repl should pop up as emacs buffer and then it will work

here is my emacs setup

first I install cabbage
then my init.el is like this


(setq cabbage-repository (expand-file-name "/home/nikola/.cabbage/"))
(load (concat cabbage-repository "cabbage"))

(require 'package)
(add-to-list 'package-archives
'("melpa" . "http://melpa.milkbox.net/packages/") )
(add-to-list 'package-archives
'("marmelade" . "http://marmalade-repo.org/packages/"))
(package-refresh-contents)
(package-initialize)
(defvar my-packages '(starter-kit
starter-kit-lisp
clojure-mode
clojure-test-mode
cider))
(dolist (p my-packages)
( when (not (package-installed-p p ))
(package-install p)))
(defun append-to-buffer ()
"Append the text of the region to BUFFER."
(interactive)
(let ((oldbuf (current-buffer)
              )
(p1 (region-beginning))
(p2 (region-end))
 (text (buffer-substring (region-beginning) (region-end)))
)
(set-buffer (get-buffer-create "*IEX*"))

(insert (replace-regexp-in-string "\n" " " (replace-regexp-in-string "#.*?\n" "" text)))(switch-to-buffer-other-window "*IEX*")
)
(global-set-key (kbd "M-`") 'append-to-buffer))
(defun cider-eval-expression-at-point-in-repl ()
(interactive)
(let ((form (cider-sexp-at-point)))
;; Strip excess whitespace
(while (string-match "\\`\s+\\|\n+\\'" form)
(setq form (replace-match "" t t form)))
(set-buffer (cider-find-or-create-repl-buffer))
(goto-char (point-max))
(insert form)
(cider-repl-return)))
(global-set-key (kbd "C-`") 'cider-eval-expression-at-point-in-repl)


I had to install elixir packages with package-install like elixir-mix elixir-mode

here is version that send line under cursor to repl
https://github.com/NikolaMandic/dotfiles/blob/master/init.el

Leave a Comment