Hotloading erlang code from emacs

%3 cluster_86a75660_a674_4e8a_b8cc_481d468c73a1 Hotloading erlang code from emacs _f0dec2bf_20ec_4970_9f30_45f85d18ab6a Erlang _aa29be89_70e7_4465_91ed_361cf0ce62f2 Emacs __0:cluster_86a75660_a674_4e8a_b8cc_481d468c73a1->_f0dec2bf_20ec_4970_9f30_45f85d18ab6a __1:cluster_86a75660_a674_4e8a_b8cc_481d468c73a1->_aa29be89_70e7_4465_91ed_361cf0ce62f2

The following code defines a function (bound to C-c C-r C-e) that picks the current .erl file and makes a local erlang node (on local@<system-name>) hotload it.

Modify the first like (setq ERLANG_NODE ...) in case your node is named differently.

(setq-default ERLANG_NODE (concat "local@" (system-name)))

(defun erlang-commit-file (filepath)
  (save-buffer)
  (let* ((filename (file-name-nondirectory filepath))
         (module-name (file-name-sans-extension filename))
         (binary-name (concat module-name ".beam"))
         (result (shell-command-to-string (concat
                                           "erl -hidden -sname $RANDOM -remsh " ERLANG_NODE " "

                                           "-eval '"
                                           "io:fwrite(\"Compiling: ~p~n\", [rpc:call(" ERLANG_NODE ", compile, file, [\"" filepath "\"])]),"
                                           "io:fwrite(\"Loading: ~p~n\", [rpc:call(" ERLANG_NODE ", code, load_abs, [\"" module-name "\"])]),"
                                           "io:fwrite(\"Purging old (soft): ~p~n\", [rpc:call(" ERLANG_NODE  ", code, soft_purge, [" module-name "])]),"
                                           "io:fwrite(\"Purging old (hard): ~p~n\", [rpc:call(" ERLANG_NODE  ", code, purge, [" module-name "])]),"
                                           "io:fwrite(\"Deleting binary: ~p~n\", [rpc:call(" ERLANG_NODE  ", file, delete, [\"" binary-name "\"])]),"
                                           "erlang:halt().'"

                                           " -noinput -noshell"))))
    (message result)))

(defun erlang-commit-this-file ()
  (interactive)
  (erlang-commit-file (buffer-file-name(current-buffer))))

(add-hook 'erlang-mode-hook
          '(lambda ()
             (local-set-key (kbd "C-c C-r C-e") 'erlang-commit-this-file)))