Skip to content

Commit 5e0a255

Browse files
committed
Add mix-format function to format Elixir 1.6 files.
1 parent 8617881 commit 5e0a255

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

mix-format.el

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
;;; mix-format.el --- Emacs plugin to mix format Elixir files
2+
3+
;; Copyright (C) 2017 Anil Wadghule
4+
5+
;; Author: Anil Wadghule <[email protected]>
6+
;; URL: https://github.com/anildigital/mix-format
7+
8+
;; This file is NOT part of GNU Emacs.
9+
10+
;; This program is free software; you can redistribute it and/or modify
11+
;; it under the terms of the GNU General Public License as published by
12+
;; the Free Software Foundation; either version 2, or (at your option)
13+
;; any later version.
14+
15+
;; This program is distributed in the hope that it will be useful,
16+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
;; GNU General Public License for more details.
19+
20+
;;; Commentary:
21+
22+
;; The mix-format function formats the elixir files with Elixir's `mix format`
23+
;; command
24+
25+
;; e.g.
26+
;;
27+
;; (require 'mix-format)
28+
;; M-x mix-format
29+
;;
30+
31+
;;; Code
32+
(defun mix-format (&optional is-interactive)
33+
(interactive "p")
34+
35+
(unwind-protect
36+
(let* (
37+
(in-file (make-temp-file "mix-format"))
38+
(out-file (make-temp-file "mix-format"))
39+
(err-file (make-temp-file "mix-format"))
40+
(contents (buffer-substring-no-properties (point-min) (point-max)))
41+
(_ (with-temp-file in-file (insert contents))))
42+
43+
44+
(let* ((command "mix")
45+
(error-buffer (get-buffer-create "*mix-format errors*"))
46+
(retcode (with-temp-buffer
47+
(call-process command
48+
nil (list out-file err-file)
49+
nil
50+
"format"
51+
"--print"
52+
in-file))))
53+
54+
(with-current-buffer error-buffer
55+
(read-only-mode 0)
56+
(insert-file-contents err-file nil nil nil t)
57+
(ansi-color-apply-on-region (point-min) (point-max))
58+
(special-mode))
59+
60+
(if (zerop retcode )
61+
(let ((p (point)))
62+
(save-excursion
63+
(erase-buffer)
64+
(insert-buffer-substring out-file)
65+
(message "mix format applied"))
66+
(goto-char p))
67+
68+
(if is-interactive
69+
(display-buffer error-buffer)
70+
(message "mix-format failed: see %s" (buffer-name error-buffer)))
71+
))
72+
(delete-file in-file)
73+
(delete-file err-file)
74+
(delete-file out-file))))
75+
76+
(provide 'mix-format)
77+
78+
;;; mix-format.el ends here

0 commit comments

Comments
 (0)