Use rebar in emacs?

2020-03-04 18:22发布

问题:

There is a defun in .emacs to get erlang project path, how can I execute a shell-command to do the following:

cd *~/erlang-project-folder*
make

I'm using rebar to build my project, and there is a Makefile to do everything.

I can compile by overriding erlang-compile-function, but I'm not familiar with Emacs Lisp, please help.

Here is my .emacs:

(defun erlang-project-dir ()
  (let* ((src-path (file-name-directory (buffer-file-name)))
         (pos (string-match "/src/" src-path)))
    (if pos (substring src-path 0 (+ 1 pos)) src-path)))

;; there is an error: wrong type argument: commandp
(defun my-inferior-erlang-compile ()
  (shell-command.
    (concat (concat (concat "cd" erlang-project-dir) "; make"))))

(defvar erlang-compile-function 'my-inferior-erlang-compile)

回答1:

Instead of relying on directory structure, it's better to try to locate the rebar.config file that is in the root of your project. This could be achieved with following code:

(defun my-find-rebar-root ()
  (let ((dir (locate-dominating-file default-directory "rebar.config")))
    (or dir default-directory)))

and after that you can use this function for compilation:

(defun my-inferior-erlang-compile ()
  (interactive)
  (let ((default-directory (my-find-rebar-root)))
    (compile "make")))

Although, I'm not sure that the make is right command here - maybe it's better to use rebar compile instead?

P.S. I hope, that I'll find some free time, and will finish rebar support in EDE - in this case, it will be the same unified interface for work with projects.



标签: emacs erlang