Erlang flymake with nested folders in src cannot find includes folder - emacs

Sorry for my poor English.
I'm configuring my emacs with erlang flymake. Source files in src's nested folders report 'can't find include file', but files in src/folder can find the include file.
My emacs settings for erlang:
;; erlang-mode
(setq load-path (cons "/usr/local/Cellar/erlang/R15B02/lib/erlang/lib/tools-2.6.8/emacs" load-path))
(setq erlang-root-dir "/usr/local/Cellar/erlang/R15B02/lib/erlang")
(setq exec-path (cons "/usr/local/Cellar/erlang/R15B02/lib/erlang/bin" exec-path))
(require 'erlang-start)
;; distel
(add-to-list 'load-path "/usr/local/share/distel/elisp/")
(require 'distel)
(distel-setup)
;; erlang-flymake
(require 'erlang-flymake)
(erlang-flymake-only-on-save)
My erlang application folder is like following:
app/src/ (source code)
src/mod
src/lib
app/include/ (hrls)
app/ebin/ (compiled code)
...etc

In erlang-flymake there are 2 variables (erlang-flymake-get-include-dirs-function and erlang-flymake-get-code-path-dirs-function), that specify functions to search include & ebin directories. Right now, they're pointing to the functions erlang-flymake-get-include-dirs and erlang-flymake-get-code-path-dirs that simply return current dir + include and ebin correspondingly. For example, you can use following code to do this:
(defun get-erlang-app-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)))
(setq erlang-flymake-get-code-path-dirs-function
(lambda ()
(concat (get-erlang-app-dir) "ebin")))
(setq erlang-flymake-get-code-include-dirs-function
(lambda ()
(concat (get-erlang-app-dir) "include")))
P.S. Are you using rebar to maintain your project?

If you use erlang-flymake you might want to look at https://github.com/ten0s/syntaxerl. It's a syntax checker for Erlang. It uses erlang-flymake under the hood, but instead of `erlc' it uses a custom syntax checker that can evaluate .erl, .hrl, .config, .rel, .app, .app.src, .escript files. The syntax checker is rebar aware, but also works with standard Erlang/OTP directory structure. Emacs's setup is also there.

Related

"malformed function" warning and require "Cannot open load file:" error

I am getting these two issues :
In toplevel form:
init.el:28:1:Warning: `(add-path (p) (add-to-list (quote load-path) (concat
emacs-root p)))' is a malformed function
init.el:42:1:Error: Cannot open load file: exec-path-from-shell
during compiling the following elisp:
(eval-when-compile (require 'cl))
;; root of all emacs-related stuff
(eval-when-compile
(defvar emacs-root
(if (or (eq system-type 'cygwin)
(eq system-type 'gnu/linux)
(eq system-type 'linux)
(eq system-type 'darwin))
"~/.emacs.d/" "z:/.emacs.d/")
"Path to where EMACS configuration root is."))
(eval-when-compile
(defvar emacs-root "~/.emacs.d"
"Path to where EMACS configuration root is."))
;; path to where plugins are kept
(defvar plugin-path (concat emacs-root "el-get")
"*Path to el-get plugins.")
;; for portability with < 24.3 EMACS
(unless (fboundp 'cl-labels) (fset 'cl-labels 'labels))
;; add paths to various configuration modes
(cl-labels
((add-path (p)
(add-to-list 'load-path
(concat emacs-root p))))
(add-path ".")
(add-path "settings")
(add-path "site-lisp")
(add-path "erlang")
(add-path "exec-path-from-shell"))
;; set PATH, because we don't load .bashrc
(require 'exec-path-from-shell) ;; <- Error: Cannot open load file: exec-path-from-shell
both these issues are very puzzling to me.
I don't see why this fund considered "malformed"
`(add-path (p) (add-to-list (quote load-path) (concat
emacs-root p)))'
and secondly why "require" is not able to load file.
these issues only happen during compilation, not compiled code works ok
would really appreciate any pointers
Regards, Roman
cl-labels is provided by cl-lib, not by cl. So you need (require 'cl-lib) (which you can also wrap in eval-when-compile).
You need to have "cl-lib" loaded at compile time:
(eval-when-compile (require 'cl-lib))
Also, as Stefan explained in his comments in your other question, you should not define variables in eval-when-compile. Just use defvar.

Use rebar in emacs?

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)
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.

How to load module only in linux?

Here is my try:
(if (eq system-type 'gnu/linux)
(load "/usr/share/emacs/site-lisp/site-gentoo")
(require 'site-gentoo))
But anyways I receive error on windows :
/.emacs':
File error: Cannot open load file, site-gentoo
Your problem is in the way you use if: its documentation says it's
(if COND THEN ELSE...)
I.e. your (require 'site-gentoo) gets executed if and only if it's not a GNU/Linux system.
Use when instead, that should do what you intend.
Also, there should actually no need to use both load and require, their usage should have the same result. The differences are mostly that require will search the load-path and don't load something again that was already loaded before.
It should be:
(if (eq system-type 'gnu/linux)
(progn
(load "/usr/share/emacs/site-lisp/site-gentoo")
(require 'site-gentoo)))
or
(when (eq system-type 'gnu/linux)
(load "/usr/share/emacs/site-lisp/site-gentoo")
(require 'site-gentoo))
Instead of (load "/usr/share/emacs/site-lisp/site-gentoo") you should add the folder containing the load file to the load-path:
(add-to-list 'load-path "/usr/share/emacs/site-lisp/")
That should do the trick. require only works for files on the load-path, load on the other hand simply evaluates the lisp file it was given as parameter.
Rörd and Bozhidar Batsov have already provided the answer as to how to resolve it, but just to add in the reason why your original code was failing.
(if COND THEN ELSE...) only accepts a single THEN command. To be able to have it evaluate multiple commands when it returns true you have to wrap the commands in (progn BODY...).
Your code was stating:
If on linux: (load "/usr/share/emacs/site-lisp/site-gentoo")
If not on linux: (require 'site-gentoo)
Using (when ...) or wrapping in (progn ...) will both provide the desired solution.

How can I specify a custom path to the annotation files in tuareg-mode emacs?

Is there anyway to specify the path to the annot files when using tuareg-mode in emacs?
I am trying to find out the type for my functions and the mode complains with
"not annotation file".
My build structure is:
lib
obj
*.o
*.cmi
*.cmx
*.annot
src
*.ml
*.mli
I don't think you can easily configure this: have a look at the caml-types-locate-type-file function in the caml-types.el file in your ocaml installation.
This is the function that searches for .annot files. You can probably edit it to replace the "_build" (which is where ocamlbuild puts the generated files) with obj and be done with that.
A much better option is to define a variable in your .emacs.el, and use it in the caml-types.el file. this way, you could even propose the patch to the ocaml people.
The following code suffices for me: it creates a new customization variable (which I haven't bound to a customization group, but you can if you want), then uses that variable as a list of directories to search.
(defcustom caml-types-annot-directories-search
'("_build" "obj" "../obj")
"List of directories to search for .annot files"
:type '(repeat string)
)
(defun or-list (f lst)
(if (null lst) nil
(if (apply f (car lst) nil)
(car lst)
(or-list f (cdr lst)))))
(add-hook 'tuareg-mode-hook
(lambda ()
(defun caml-types-locate-type-file (target-path)
(let ((sibling (concat (file-name-sans-extension target-path) ".annot")))
(if (file-exists-p sibling)
sibling
(let ((project-dir (file-name-directory sibling))
(test-dir (lambda (prefix)
(message "Prefix is %s" prefix)
(setq type-path
(expand-file-name
(file-relative-name sibling project-dir)
(expand-file-name prefix project-dir)))
(message "Testing %s" type-path)
(file-exists-p type-path)))
type-path)
(while (not (or-list test-dir caml-types-annot-directories-search))
(if (equal project-dir (caml-types-parent-dir project-dir))
(error (concat "No annotation file. "
"You should compile with option \"-annot\".")))
(setq project-dir (caml-types-parent-dir project-dir)))
type-path))))))
# soft link .annot files so that Emacs' tuareg-mode can find them
mkdir -p _build
for f in `find lib/obj -name *.annot` ; do ln -s ../$f _build/ ; done

How can I access the path to the current directory in an emacs directory variable file?

According to the Emacs documentation, Directory Variables apply to all files below a directory that contains an .dir-locals.el file.
How can I, in that file, set a variable to the full path that contains the file? For example:
((nil . ((indent-tabs-mode . t)
(my-project-path **THIS_DIRECTORY**))))
I asked myself the same question and found no solution on the web, so I think this answer may help. Actually, it turns out we can reuse dir-locals-find-file to get the directory containing the .dir-locals.el file. So here's what I found for, e.g, setting up an aspell personal dictionary dedicated to a whole directory:
((nil . ((eval . (setq ispell-personal-dictionary
(expand-file-name
".aspell_words"
(file-name-directory
(let ((d (dir-locals-find-file ".")))
(if (stringp d) d (car d))))))))))
Also, it seems entries are evaluated in the order they are specified, so the following code should work:
((nil . ((eval . (set (make-local-variable 'my-project-path)
(file-name-directory
(let ((d (dir-locals-find-file ".")))
(if (stringp d) d (car d))))))
(eval . (message "Project directory set to `%s'." my-project-path)))))
Emacs will complain about unsafe local variables (due to the eval construct), yet one can still permanently mark it safe.
Update: Since Emacs ≥ 26.3 (and maybe older versions as well), it appears that one needs to use (dir-locals-find-file "./") instead of (dir-locals-find-file ".").
I think (file-name-directory (or load-file-name buffer-file-name)) should give you the directory path.
See Link
Edit: Except it won't, because any eval expressions are evaluated in the context of the buffer whose variables are being hacked.
In my case, I wanted to locate a file that was relative to my current working directory for my repository, and the .dir-locals.el file was checked into the root, so a file "local" to the .dir-locals.el was also a file "local" to the project root.
pajato0's answer above worked for some cases, but it was also breaking other modes (like magit). I got around the issue by using the projectile package's projectile-project-root function to find my the base path for me:
((nil . ((eval . (setq cmake-ide-build-dir
(concat (projectile-project-root) "/build-make"))
))))
I've found the locate-dominating-file procedure, which comes out-of-the-box with Emacs, useful to retreive the current directory of a known file. The example below sets the guix-directory variable to the topmost directory of the project containing a .dir-locals.el file.
((nil . ((eval . (setq guix-directory
(locate-dominating-file default-directory
".dir-locals.el"))))))
It's not a safe .dir-locals.el setting, due to relying on eval, but it gets the job done.
In case it still matters, to the OP or some other, I would suggest you create a function to generate the .dir-locals.el file. Then one could write something like:
(let ((path default-directory)
file)
(setq file (format "%s/.dir-locals.el" path))
(with-temp-buffer
(insert (format "((nil . ((indent-tabs-mode . t)
(my-project-path \"%s\"))))" path))
(when (file-writable-p file)
(write-region (point-min)
(point-max)
file))))
to be executed within the project home directory.
hack-local-variables is the main function for processing all local variables, and it calls hack-dir-local-variables to deal with the .dir-locals.el file (or a dir local class variable, if you're not using that file).
The code for establishing the directory is not isolated in its own function, so we'll have to copy it out into a new function (this from GNU Emacs 24.0.95.1):
(defun my-dir-locals-dir ()
"Return the directory local variables directory.
Code taken from `hack-dir-local-variables'."
(let ((variables-file (dir-locals-find-file (or (buffer-file-name) default-directory)))
(dir-name nil))
(cond
((stringp variables-file)
(setq dir-name (file-name-directory variables-file)))
((consp variables-file)
(setq dir-name (nth 0 variables-file))))
dir-name))
If you are working on *nix, you might get the work directory by the following elisp code,
(defun get-working-directory ()
(getenv "PWD))
Btw, I have to mentioned that, (shell-command "pwd") will result in the directory where file (which is corresponding to the buffer you are currently editing).