From 766c27e48ec8f6e71fad0569a766ad981708a010 Mon Sep 17 00:00:00 2001 From: Klas Johansson Date: Wed, 26 May 2010 22:59:14 +0200 Subject: erlang-flymake: Syntax check erlang code on the fly (using flymake) --- lib/tools/emacs/erlang-flymake.el | 94 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 lib/tools/emacs/erlang-flymake.el (limited to 'lib') diff --git a/lib/tools/emacs/erlang-flymake.el b/lib/tools/emacs/erlang-flymake.el new file mode 100644 index 0000000000..a6284a25bc --- /dev/null +++ b/lib/tools/emacs/erlang-flymake.el @@ -0,0 +1,94 @@ +;; erlang-flymake.el +;; +;; Syntax check erlang source code on the fly (integrates with flymake). +;; +;; Start using flymake with erlang by putting the following somewhere +;; in your .emacs file: +;; +;; (require 'erlang-flymake) +;; +;; Flymake is rather eager and does its syntax checks frequently by +;; default and if you are bothered by this, you might want to put the +;; following in your .emacs as well: +;; +;; (erlang-flymake-only-on-save) +;; +;; There are a couple of variables which control the compilation options: +;; * erlang-flymake-get-code-path-dirs-function +;; * erlang-flymake-get-include-dirs-function +;; * erlang-flymake-extra-opts +;; +;; This code is inspired by http://www.emacswiki.org/emacs/FlymakeErlang. + +(require 'flymake) +(eval-when-compile + (require 'cl)) + +(defvar erlang-flymake-get-code-path-dirs-function + 'erlang-flymake-get-code-path-dirs + "Return a list of ebin directories to add to the code path.") + +(defvar erlang-flymake-get-include-dirs-function + 'erlang-flymake-get-include-dirs + "Return a list of include directories to add to the compiler options.") + +(defvar erlang-flymake-extra-opts + (list "+warn_obsolete_guard" + "+warn_unused_import" + "+warn_shadow_vars" + "+warn_export_vars" + "+strong_validation" + "+report") + "A list of options that will be passed to the compiler") + +(defun erlang-flymake-only-on-save () + "Trigger flymake only when the buffer is saved (disables syntax +check on newline and when there are no changes)." + (interactive) + ;; There doesn't seem to be a way of disabling this; set to the + ;; largest int available as a workaround (most-positive-fixnum + ;; equates to 8.5 years on my machine, so it ought to be enough ;-) ) + (setq flymake-no-changes-timeout most-positive-fixnum) + (setq flymake-start-syntax-check-on-newline nil)) + + +(defun erlang-flymake-get-code-path-dirs () + ()) + +(defun erlang-flymake-get-include-dirs () + ()) + +(defun erlang-flymake-init () + (let* ((temp-file + (flet ((flymake-get-temp-dir () (erlang-flymake-temp-dir))) + (flymake-init-create-temp-buffer-copy + 'flymake-create-temp-with-folder-structure))) + (code-dir-opts + (erlang-flymake-flatten + (mapcar (lambda (dir) (list "-pa" dir)) + (funcall erlang-flymake-get-code-path-dirs-function)))) + (inc-dir-opts + (erlang-flymake-flatten + (mapcar (lambda (dir) (list "-I" dir)) + (funcall erlang-flymake-get-include-dirs-function)))) + (compile-opts + (append inc-dir-opts + code-dir-opts + erlang-flymake-extra-opts))) + (list "erlc" (append compile-opts (list temp-file))))) + +(defun erlang-flymake-temp-dir () + ;; Squeeze the user's name in there in order to make sure that files + ;; for two users who are working on the same computer (like a linux + ;; box) don't collide + (format "%s/flymake-%s" temporary-file-directory user-login-name)) + +(defun erlang-flymake-flatten (list) + (apply #'append list)) + +(add-to-list 'flymake-allowed-file-name-masks + '("\\.erl\\'" erlang-flymake-init)) +(add-hook 'erlang-mode-hook 'flymake-mode) + +(provide 'erlang-flymake) +;; erlang-flymake ends here -- cgit v1.2.3 From 8f0dd817c4df80720ae5bb29d2c6604fe3dadd61 Mon Sep 17 00:00:00 2001 From: Klas Johansson Date: Sun, 30 May 2010 21:39:12 +0200 Subject: erlang-flymake: Include in Makefile --- lib/tools/emacs/Makefile | 1 + 1 file changed, 1 insertion(+) (limited to 'lib') diff --git a/lib/tools/emacs/Makefile b/lib/tools/emacs/Makefile index 0028df247c..8533488463 100644 --- a/lib/tools/emacs/Makefile +++ b/lib/tools/emacs/Makefile @@ -42,6 +42,7 @@ EMACS_FILES= \ erlang_appwiz \ erlang-start \ erlang-eunit \ + erlang-flymake \ erlang README_FILES= README -- cgit v1.2.3 From 418de1e35ab35809c4464cfab20e3b4b27426f04 Mon Sep 17 00:00:00 2001 From: Klas Johansson Date: Sun, 30 May 2010 22:18:52 +0200 Subject: erlang-flymake: By default pass /include and /ebin to compiler Hopefully this covers at least some of the common cases and makes the flymake support more usable as is. The purpose of including the ebin directory is to support things like behaviours and parse transforms. --- lib/tools/emacs/erlang-flymake.el | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/tools/emacs/erlang-flymake.el b/lib/tools/emacs/erlang-flymake.el index a6284a25bc..660e72a6a1 100644 --- a/lib/tools/emacs/erlang-flymake.el +++ b/lib/tools/emacs/erlang-flymake.el @@ -53,11 +53,15 @@ check on newline and when there are no changes)." (defun erlang-flymake-get-code-path-dirs () - ()) + (list (concat (erlang-flymake-get-app-dir) "ebin"))) (defun erlang-flymake-get-include-dirs () - ()) + (list (concat (erlang-flymake-get-app-dir) "include"))) +(defun erlang-flymake-get-app-dir () + (let ((src-path (file-name-directory (buffer-file-name)))) + (file-name-directory (directory-file-name src-path)))) + (defun erlang-flymake-init () (let* ((temp-file (flet ((flymake-get-temp-dir () (erlang-flymake-temp-dir))) -- cgit v1.2.3 From 704ce3e8e296704bd5c0dfddc3ee58e1ea0ecc45 Mon Sep 17 00:00:00 2001 From: Klas Johansson Date: Sun, 30 May 2010 22:27:56 +0200 Subject: erlang-flymake: Make the syntax check command configurable --- lib/tools/emacs/erlang-flymake.el | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/tools/emacs/erlang-flymake.el b/lib/tools/emacs/erlang-flymake.el index 660e72a6a1..bc368e9454 100644 --- a/lib/tools/emacs/erlang-flymake.el +++ b/lib/tools/emacs/erlang-flymake.el @@ -24,6 +24,10 @@ (eval-when-compile (require 'cl)) +(defvar erlang-flymake-command + "erlc" + "The command that will be used to perform the syntax check") + (defvar erlang-flymake-get-code-path-dirs-function 'erlang-flymake-get-code-path-dirs "Return a list of ebin directories to add to the code path.") @@ -58,10 +62,10 @@ check on newline and when there are no changes)." (defun erlang-flymake-get-include-dirs () (list (concat (erlang-flymake-get-app-dir) "include"))) -(defun erlang-flymake-get-app-dir () +(defun erlang-flymake-get-app-dir () (let ((src-path (file-name-directory (buffer-file-name)))) (file-name-directory (directory-file-name src-path)))) - + (defun erlang-flymake-init () (let* ((temp-file (flet ((flymake-get-temp-dir () (erlang-flymake-temp-dir))) @@ -79,7 +83,7 @@ check on newline and when there are no changes)." (append inc-dir-opts code-dir-opts erlang-flymake-extra-opts))) - (list "erlc" (append compile-opts (list temp-file))))) + (list erlang-flymake-command (append compile-opts (list temp-file))))) (defun erlang-flymake-temp-dir () ;; Squeeze the user's name in there in order to make sure that files -- cgit v1.2.3 From 54cda6a510a62927d2b295732bdd9d9afa51940d Mon Sep 17 00:00:00 2001 From: Klas Johansson Date: Sun, 30 May 2010 22:51:19 +0200 Subject: erlang-flymake: Document in README --- lib/tools/emacs/README | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/tools/emacs/README b/lib/tools/emacs/README index ca068d04c4..cc107dcd41 100644 --- a/lib/tools/emacs/README +++ b/lib/tools/emacs/README @@ -42,7 +42,14 @@ Files\erl-: (setq erlang-root-dir "C:/Program Files/erl") (setq exec-path (cons "C:/Program Files/erl/bin" exec-path)) (require 'erlang-start) - +Miscellaneous addons +-------------------- + +In order to check erlang source code on the fly, add the following +line to your .emacs file (after erlang-start, see above). See +erlang-flymake.el for more information on how to customize the syntax +check. + (require 'erlang-flymake) -- cgit v1.2.3