Skip to content

Commit c42fcad

Browse files
committed
remove variables for elixir and mix paths
1 parent 4911345 commit c42fcad

File tree

2 files changed

+73
-131
lines changed

2 files changed

+73
-131
lines changed

README.md

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -151,41 +151,6 @@ You can use [web-mode.el](http://web-mode.org) to edit elixir templates (eex fil
151151

152152
## Elixir Format
153153

154-
### Setup of elixir-format
155-
Customize the elixir and mix paths
156-
157-
In Emacs, run following command to customize option
158-
``` elisp
159-
M-x customize-option
160-
161-
Customize-variable: elixir-format-elixir-path
162-
```
163-
and set your elixir executable path there. After that run:
164-
``` elisp
165-
M-x customize-option
166-
167-
Customize-variable: elixir-format-mix-path
168-
```
169-
and set your mix executable path there.
170-
171-
Your machine's elixir and mix executable paths can be found with `which` command as shown below
172-
173-
``` shell
174-
$ which elixir
175-
/usr/local/bin/elixir
176-
177-
$ which mix
178-
/usr/local/bin/mix
179-
```
180-
Alternavively you can define variables as below
181-
182-
``` elisp
183-
(setq elixir-format-elixir-path "/usr/local/bin/elixir")
184-
(setq elixir-format-mix-path "/usr/local/bin/mix")
185-
```
186-
187-
### Use it
188-
189154
``` elisp
190155
M-x elixir-format
191156
```
@@ -215,7 +180,8 @@ or you set `elixir-format-arguments` in a hook like this:
215180
(setq elixir-format-arguments nil))))
216181
```
217182

218-
In this example we use [Projectile](https://github.com/bbatsov/projectile) to get the project root and set `elixir-format-arguments` accordingly.
183+
In this example we use [Projectile](https://github.com/bbatsov/projectile) to determine if we are in a project and then set `elixir-format-arguments` accordingly.
184+
Please note that this code snippet may cause unhappiness if there is no `.formatter.exs` file available.
219185

220186

221187
## History

elixir-format.el

Lines changed: 71 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,6 @@
2525

2626
(require 'ansi-color)
2727

28-
(defcustom elixir-format-elixir-path ""
29-
"Path to the Elixir executable. Usually it is /usr/local/bin/elixir. You can type `which elixir` in terminal to find out the elixir binary path in your system.
30-
31-
Customize the elixir path
32-
33-
In Emacs, run following command to customize option
34-
35-
M-x customize-option
36-
37-
Customize-variable: elixir-format-elixir-path"
38-
:type 'string
39-
:group 'elixir-format)
40-
41-
(defcustom elixir-format-mix-path ""
42-
"Path to the 'mix' executable. Usually it is /usr/local/bin/mix. You can type `which mix` in terminal to find out the mix binary path in your system
43-
44-
Customize the mix path
45-
46-
In Emacs, run following command to customize option
47-
48-
M-x customize-option
49-
50-
Customize-variable: elixir-format-mix-path"
51-
:type 'string
52-
:group 'elixir-format)
53-
5428
(defcustom elixir-format-arguments nil
5529
"Additional arguments to 'mix format'"
5630
:type '(repeat string)
@@ -70,60 +44,62 @@ Customize-variable: elixir-format-mix-path"
7044
(defun elixir-format--outbuff ()
7145
(get-buffer-create "*elixir-format-output*"))
7246

73-
(defun elixir-format--clean-output-buffers ()
74-
(with-current-buffer (elixir-format--outbuff)
75-
(erase-buffer))
47+
(defun elixir-format--elixir-executable ()
48+
(executable-find "elixir"))
7649

77-
(with-current-buffer (elixir-format--errbuff)
78-
(setq buffer-read-only nil)
79-
(erase-buffer)))
50+
(defun elixir-format--mix-executable ()
51+
(executable-find "mix"))
8052

8153
;;;###autoload
8254
(defun elixir-format (&optional called-interactively-p)
8355
(interactive "p")
84-
85-
(let ((tmpfile (make-temp-file "elixir-format" nil ".ex"))
86-
(our-elixir-format-arguments (list elixir-format-mix-path "format"))
87-
(output nil))
88-
89-
(if (elixir-format--elixir-and-mix-path-not-set-p)
90-
(elixir-format--display-missing-configuration-error called-interactively-p)
91-
(unwind-protect
92-
(save-restriction
93-
(elixir-format--clean-output-buffers)
94-
(elixir-format--run-format called-interactively-p tmpfile))))))
95-
96-
(defun elixir-format--elixir-and-mix-path-not-set-p ()
97-
(or (= (length elixir-format-mix-path) 0)
98-
(= (length elixir-format-elixir-path) 0)))
99-
100-
(defun elixir-format--display-missing-configuration-error (called-interactively-p)
56+
(if (not (elixir-format--elixir-and-mix-path-set-p))
57+
(elixir-format--display-missing-executables-error called-interactively-p)
58+
(unwind-protect
59+
(save-restriction
60+
(elixir-format--clean-output-buffers)
61+
(elixir-format--run-format called-interactively-p)))))
62+
63+
(defun elixir-format--elixir-and-mix-path-set-p ()
64+
(and (elixir-format--elixir-executable)
65+
(elixir-format--mix-executable)))
66+
67+
(defun elixir-format--display-missing-executables-error (called-interactively-p)
10168
(with-current-buffer (elixir-format--errbuff)
10269
(setq buffer-read-only nil)
10370
(erase-buffer)
104-
(insert "Elixir or mix binary path not set, please check the documentation for `elixir-format-elixir-path` and `elixir-format-mix-path` with `C-h v <VARNAME> RET` to set the variables appropriately")
71+
(insert "Emacs is unable to find the executables for elixir and/or mix. Either they are not installed on your system or emacs' PATH is not as wide as it needs to be. The latter is most likely to happen on OSX, in which case the simplest answer may be to add the exec-path-from-shell package to your configuration.")
10572
(setq buffer-read-only t)
10673
(ansi-color-apply-on-region (point-min) (point-max))
10774
(special-mode)
10875
(if called-interactively-p
109-
(display-buffer elixir-format--errbuff)
110-
(error "elixir-format-configuration-missing: see %s" (buffer-name (elixir-format--errbuff))))))
76+
(display-buffer (elixir-format--errbuff))
77+
(error "Elixir Format error see %s" (elixir-format--errbuff)))))
78+
79+
(defun elixir-format--clean-output-buffers ()
80+
(with-current-buffer (elixir-format--outbuff)
81+
(erase-buffer))
11182

112-
(defun elixir-format--run-format (called-interactively-p tmpfile)
113-
(write-region nil nil tmpfile)
83+
(with-current-buffer (elixir-format--errbuff)
84+
(setq buffer-read-only nil)
85+
(erase-buffer)))
11486

115-
(run-hooks 'elixir-format-hook)
87+
(defun elixir-format--run-format (called-interactively-p)
88+
(let ((tmpfile (make-temp-file "elixir-format" nil ".ex"))
89+
(our-elixir-format-arguments (list (elixir-format--mix-executable) "format")))
11690

117-
(when elixir-format-arguments
118-
(setq our-elixir-format-arguments (append our-elixir-format-arguments elixir-format-arguments)))
119-
(setq our-elixir-format-arguments (append our-elixir-format-arguments (list tmpfile)))
91+
(write-region nil nil tmpfile)
92+
(run-hooks 'elixir-format-hook)
12093

121-
(if (zerop (apply #'call-process elixir-format-elixir-path nil (elixir-format--errbuff) nil our-elixir-format-arguments))
122-
(elixir-format--call-format-command tmpfile)
123-
(elixir-format--failed-to-format called-interactively-p))
94+
(when elixir-format-arguments
95+
(setq our-elixir-format-arguments (append our-elixir-format-arguments elixir-format-arguments)))
96+
(setq our-elixir-format-arguments (append our-elixir-format-arguments (list tmpfile)))
12497

125-
(delete-file tmpfile)
126-
(kill-buffer (elixir-format--outbuff)))
98+
(if (zerop (apply #'call-process (elixir-format--elixir-executable) nil (elixir-format--errbuff) nil our-elixir-format-arguments))
99+
(elixir-format--call-format-command tmpfile)
100+
(elixir-format--failed-to-format called-interactively-p))
101+
(delete-file tmpfile)
102+
(kill-buffer (elixir-format--outbuff))))
127103

128104
(defun elixir-format--call-format-command (tmpfile)
129105
(if (zerop (call-process-region (point-min) (point-max) "diff" nil (elixir-format--outbuff) nil "-n" "-" tmpfile))
@@ -142,38 +118,6 @@ Customize-variable: elixir-format-mix-path"
142118
(display-buffer (elixir-format--errbuff))
143119
(error "elixir-format failed: see %s" (buffer-name (elixir-format--errbuff)))))
144120

145-
(defun elixir-format--goto-line (line)
146-
(goto-char (point-min))
147-
(forward-line (1- line)))
148-
149-
(defun elixir-format--delete-whole-line (&optional arg)
150-
"Delete the current line without putting it in the `kill-ring'.
151-
Derived from function `kill-whole-line'. ARG is defined as for that
152-
function.
153-
154-
Shamelessly stolen from go-mode (https://github.com/dominikh/go-mode.el)"
155-
(setq arg (or arg 1))
156-
(if (and (> arg 0)
157-
(eobp)
158-
(save-excursion (forward-visible-line 0) (eobp)))
159-
(signal 'end-of-buffer nil))
160-
(if (and (< arg 0)
161-
(bobp)
162-
(save-excursion (end-of-visible-line) (bobp)))
163-
(signal 'beginning-of-buffer nil))
164-
(cond ((zerop arg)
165-
(delete-region (progn (forward-visible-line 0) (point))
166-
(progn (end-of-visible-line) (point))))
167-
((< arg 0)
168-
(delete-region (progn (end-of-visible-line) (point))
169-
(progn (forward-visible-line (1+ arg))
170-
(unless (bobp)
171-
(backward-char))
172-
(point))))
173-
(t
174-
(delete-region (progn (forward-visible-line 0) (point))
175-
(progn (forward-visible-line arg) (point))))))
176-
177121
(defun elixir-format--apply-rcs-patch (patch-buffer)
178122
"Apply an RCS-formatted diff from PATCH-BUFFER to the current buffer.
179123
Shamelessly stolen from go-mode (https://github.com/dominikh/go-mode.el)"
@@ -218,6 +162,38 @@ Shamelessly stolen from go-mode (https://github.com/dominikh/go-mode.el)"
218162
(t
219163
(error "Invalid rcs patch or internal error in elixir-format--apply-rcs-patch")))))))))
220164

165+
(defun elixir-format--goto-line (line)
166+
(goto-char (point-min))
167+
(forward-line (1- line)))
168+
169+
(defun elixir-format--delete-whole-line (&optional arg)
170+
"Delete the current line without putting it in the `kill-ring'.
171+
Derived from function `kill-whole-line'. ARG is defined as for that
172+
function.
173+
174+
Shamelessly stolen from go-mode (https://github.com/dominikh/go-mode.el)"
175+
(setq arg (or arg 1))
176+
(if (and (> arg 0)
177+
(eobp)
178+
(save-excursion (forward-visible-line 0) (eobp)))
179+
(signal 'end-of-buffer nil))
180+
(if (and (< arg 0)
181+
(bobp)
182+
(save-excursion (end-of-visible-line) (bobp)))
183+
(signal 'beginning-of-buffer nil))
184+
(cond ((zerop arg)
185+
(delete-region (progn (forward-visible-line 0) (point))
186+
(progn (end-of-visible-line) (point))))
187+
((< arg 0)
188+
(delete-region (progn (end-of-visible-line) (point))
189+
(progn (forward-visible-line (1+ arg))
190+
(unless (bobp)
191+
(backward-char))
192+
(point))))
193+
(t
194+
(delete-region (progn (forward-visible-line 0) (point))
195+
(progn (forward-visible-line arg) (point))))))
196+
221197
(provide 'elixir-format)
222198

223199
;;; elixir-format.el ends here

0 commit comments

Comments
 (0)