forked from suvratapte/dot-emacs-dot-d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cljstyle-mode.el
88 lines (73 loc) · 3.22 KB
/
cljstyle-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
;;; cljstyle-mode.el --- Reformat Clojure code using cljstyle
;; Author: Jeff Stokes ([email protected])
;; URL: https://github.com/jstokes/cljstyle-mode.el
;; Version: 0.1
;; Keywords: tools
;; Package-Requires: ((emacs "24.3"))
;; This file is NOT part of GNU Emacs.
;; cljstyle-mode.el is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; cljstyle-mode.el is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with cljstyle-mode.el.
;; If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Reformat Clojure code using cljstyle
;;; Code:
;;;###autoload
(defun cljstyle (&optional is-interactive)
"Reformat code using cljstyle.
If region is active, reformat it; otherwise reformat entire buffer.
When called interactively, or with prefix argument IS-INTERACTIVE,
show a buffer if the formatting fails"
(interactive)
(let* ((p (point))
(b (if mark-active (min p (mark)) (point-min)))
(e (if mark-active (max p (mark)) (point-max)))
(in-file (make-temp-file "cljstyle-in" nil ".clj"))
(err-file (make-temp-file "cljstyle-err"))
(output-buffer (get-buffer-create "*cljstyle-mode output*"))
(contents (buffer-substring-no-properties b e))
(cwd default-directory)
(_ (with-temp-file in-file (insert contents))))
(unwind-protect
(let* ((error-buffer (get-buffer-create "*cljstyle-mode errors*"))
(retcode
(with-current-buffer output-buffer
(erase-buffer)
(let* ((default-directory cwd))
(call-process "cljstyle"
in-file
(list t err-file)
nil
"pipe")))))
(with-current-buffer error-buffer
(read-only-mode 0)
(insert-file-contents err-file nil nil nil t)
(special-mode))
(if (eq retcode 0)
(save-restriction
(delete-region b e)
(insert-buffer output-buffer)
(goto-char p))
(message "cljstyle applied")
(if is-interactive
(display-buffer error-buffer)
(message "cljstyle failed: see %s" (buffer-name error-buffer)))))
(kill-buffer output-buffer)
(delete-file in-file)
(delete-file err-file))))
;;;###autoload
(define-minor-mode cljstyle-mode
"Minor mode for reformatting Clojure code using cljstyle"
:lighter " cljstyle"
(if cljstyle-mode
(add-hook 'before-save-hook 'cljstyle nil t)
(remove-hook 'before-save-hook 'cljstyle t)))
(provide 'cljstyle-mode)
;;; cljstyle-mode.el ends here