Emacs config utilizing prelude as a base
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.6 KiB

  1. ;; erlang-flymake.el
  2. ;;
  3. ;; Syntax check erlang source code on the fly (integrates with flymake).
  4. ;;
  5. ;; Start using flymake with erlang by putting the following somewhere
  6. ;; in your .emacs file:
  7. ;;
  8. ;; (require 'erlang-flymake)
  9. ;;
  10. ;; Flymake is rather eager and does its syntax checks frequently by
  11. ;; default and if you are bothered by this, you might want to put the
  12. ;; following in your .emacs as well:
  13. ;;
  14. ;; (erlang-flymake-only-on-save)
  15. ;;
  16. ;; There are a couple of variables which control the compilation options:
  17. ;; * erlang-flymake-get-code-path-dirs-function
  18. ;; * erlang-flymake-get-include-dirs-function
  19. ;; * erlang-flymake-extra-opts
  20. ;;
  21. ;; This code is inspired by http://www.emacswiki.org/emacs/FlymakeErlang.
  22. (require 'flymake)
  23. (eval-when-compile
  24. (require 'cl))
  25. (defvar erlang-flymake-command
  26. "erlc"
  27. "The command that will be used to perform the syntax check")
  28. (defvar erlang-flymake-get-code-path-dirs-function
  29. 'erlang-flymake-get-code-path-dirs
  30. "Return a list of ebin directories to add to the code path.")
  31. (defvar erlang-flymake-get-include-dirs-function
  32. 'erlang-flymake-get-include-dirs
  33. "Return a list of include directories to add to the compiler options.")
  34. (defvar erlang-flymake-extra-opts
  35. (list "+warn_obsolete_guard"
  36. "+warn_unused_import"
  37. "+warn_shadow_vars"
  38. "+warn_export_vars"
  39. "+strong_validation"
  40. "+report")
  41. "A list of options that will be passed to the compiler")
  42. (defun erlang-flymake-only-on-save ()
  43. "Trigger flymake only when the buffer is saved (disables syntax
  44. check on newline and when there are no changes)."
  45. (interactive)
  46. ;; There doesn't seem to be a way of disabling this; set to the
  47. ;; largest int available as a workaround (most-positive-fixnum
  48. ;; equates to 8.5 years on my machine, so it ought to be enough ;-) )
  49. (setq flymake-no-changes-timeout most-positive-fixnum)
  50. (setq flymake-start-syntax-check-on-newline nil))
  51. (defun erlang-flymake-get-code-path-dirs ()
  52. (list (concat (erlang-flymake-get-app-dir) "ebin")))
  53. (defun erlang-flymake-get-include-dirs ()
  54. (list (concat (erlang-flymake-get-app-dir) "include")
  55. (concat (erlang-flymake-get-app-dir) "deps")))
  56. (defun erlang-flymake-get-app-dir ()
  57. (let ((src-path (file-name-directory (buffer-file-name))))
  58. (file-name-directory (directory-file-name src-path))))
  59. (defun erlang-flymake-init ()
  60. (let* ((temp-file
  61. (flet ((flymake-get-temp-dir () (erlang-flymake-temp-dir)))
  62. (flymake-init-create-temp-buffer-copy
  63. 'flymake-create-temp-with-folder-structure)))
  64. (code-dir-opts
  65. (erlang-flymake-flatten
  66. (mapcar (lambda (dir) (list "-pa" dir))
  67. (funcall erlang-flymake-get-code-path-dirs-function))))
  68. (inc-dir-opts
  69. (erlang-flymake-flatten
  70. (mapcar (lambda (dir) (list "-I" dir))
  71. (funcall erlang-flymake-get-include-dirs-function))))
  72. (compile-opts
  73. (append inc-dir-opts
  74. code-dir-opts
  75. erlang-flymake-extra-opts)))
  76. (list erlang-flymake-command (append compile-opts (list temp-file)))))
  77. (defun erlang-flymake-temp-dir ()
  78. ;; Squeeze the user's name in there in order to make sure that files
  79. ;; for two users who are working on the same computer (like a linux
  80. ;; box) don't collide
  81. (format "%s/flymake-%s" temporary-file-directory user-login-name))
  82. (defun erlang-flymake-flatten (list)
  83. (apply #'append list))
  84. (add-to-list 'flymake-allowed-file-name-masks
  85. '("\\.erl\\'" erlang-flymake-init))
  86. (add-hook 'erlang-mode-hook 'flymake-mode)
  87. (provide 'erlang-flymake)
  88. ;; erlang-flymake ends here