-
Notifications
You must be signed in to change notification settings - Fork 6
/
kibit-mode.el
140 lines (115 loc) · 5.01 KB
/
kibit-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
;;; kibit-mode.el --- Enhance clojure-mode with Kibit analysis
;; Copyright (C) 2012 Alex Redington <http://www.holychao.com>
;; Authors: Alex Redington
;; Peter Vasil <[email protected]>
;; Created: 2012
;; Version: 0.1
;; Keywords: clojure kibit
;; Package-Requires: ((clojure-mode "1.11.5") (mode-compile "2.29"))
;;; Commentary:
;;
;; This file is NOT part of GNU Emacs.
;;
;; Copyright (C) 2012 Alex Redington
;; Permission is hereby granted, free of charge, to any person
;; obtaining a copy of this software and associated documentation
;; files (the "Software"), to deal in the Software without
;; restriction, including without limitation the rights to use,
;; copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the
;; Software is furnished to do so, subject to the following
;; conditions:
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
;; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
;; OTHER DEALINGS IN THE SOFTWARE.
;;; Documentation:
;;
;; This minor mode acts as a compilation mechanism for interactively replacing
;; clojure s-expressions by their more idiomatic representations. It provides
;; the following capabilities:
;;
;; * Run a check over the currently open file (bound to `\C-c \C-n`). This will
;; open a compilation mode buffer showing the kibit replacements.
;;
;; * Implement a suggested replacement (bound to `r`). This will destroy the
;; extant formatting when the replacement is inserted
;;; Dependencies:
;; This minor mode depends on `mode-compile` and `clojure-mode`.
;;; Change Log:
;;
;; 0.1 - First cut of kibit-mode
;;; Code:
(require 'clojure-mode)
(require 'compile)
(defconst kibit-mode-keymap (make-sparse-keymap) "Keymap used in kibit mode")
(define-key kibit-mode-keymap (kbd "C-c C-n") 'kibit-check)
(defgroup kibit-mode nil
"Kibit minor mode.")
(eval-and-compile
(defvar kibit-mode-path
(let ((path (or (locate-library "kibit-mode") load-file-name)))
(and path (file-name-directory path)))
"Directory containing the kibit-mode package.
This is used to execute the supporting kibit analysis execution environment.
The default value is automatically computed from the location of the
Emacs Lisp package."))
;;;###autoload
(define-minor-mode kibit-mode
"Minor mode for kibit compilation support"
:lighter " kibit"
:keymap kibit-mode-keymap)
(defun kibit-check ()
"Runs the current file through kibit check"
(interactive)
(let ((default-directory kibit-mode-path))
(compile (concat "lein run -m kibit-mode.core "
(buffer-file-name)))))
(add-to-list 'compilation-error-regexp-alist-alist
'(kibit-mode "\\([0-9A-Za-z_./\:-]+\\.clj\\):\\([0-9]+\\):" 1 2))
(add-to-list 'compilation-error-regexp-alist 'kibit-mode)
(require 'flymake)
(defun flymake-kibit-init ()
(flymake-simple-make-init-impl
'flymake-create-temp-with-folder-structure nil nil
buffer-file-name
'flymake-get-kibit-cmdline))
(defun flymake-get-kibit-cmdline (source base-dir)
(list (concat kibit-mode-path "bin/kibit-flymake.sh") (list source) kibit-mode-path))
(push '(".+\\.clj$" flymake-kibit-init) flymake-allowed-file-name-masks)
(push '(".+\\.cljs$" flymake-kibit-init) flymake-allowed-file-name-masks)
(push '("\\(.*\\):\\([0-9]+\\): \\(ERROR: .* CORRECTION: .*\\)"
1 2 nil 3)
flymake-err-line-patterns)
(eval-after-load 'flycheck
'(progn
(cond
;; New macro definition
((fboundp 'flycheck-define-checker)
(progn
(add-to-list 'exec-path (concat kibit-mode-path "bin"))
(flycheck-define-checker clojure-kibit
"A Clojure code analyzer using the kibit utility."
:command ("kibit-flymake.sh" source)
:error-patterns ((error line-start (file-name) ":" line ": ERROR: " (message) line-end ))
:modes (clojure-mode clojurescript-mode))))
;; Backwards compatibility
((fboundp 'flycheck-declare-checker)
(flycheck-declare-checker
clojure-kibit
"A Clojure code analyzer using the kibit utility."
:command `(,(concat kibit-mode-path "bin/kibit-flymake.sh") source)
:error-patterns '(("\\(?1:.*\\):\\(?2:[0-9]+\\): \\(?4:ERROR: .* CORRECTION: .*\\)" error))
:modes 'clojure-mode)))
(add-to-list 'flycheck-checkers 'clojure-kibit)))
(eval-after-load 'exec-path-from-shell
'(defadvice exec-path-from-shell-initialize (after reset-kibit-path activate)
(add-to-list 'exec-path (concat kibit-mode-path "bin"))))
(provide 'kibit-mode)
;;; kibit-mode.el ends here