|
25 | 25 |
|
26 | 26 | (require 'ansi-color)
|
27 | 27 |
|
28 |
| -(defcustom elixir-format-elixir-path "elixir" |
29 |
| - "Path to the Elixir interpreter." |
30 |
| - :type 'string |
31 |
| - :group 'elixir-format) |
32 |
| - |
33 |
| -(defcustom elixir-format-mix-path "/usr/bin/mix" |
34 |
| - "Path to the 'mix' executable." |
35 |
| - :type 'string |
36 |
| - :group 'elixir-format) |
37 |
| - |
38 | 28 | (defcustom elixir-format-arguments nil
|
39 | 29 | "Additional arguments to 'mix format'"
|
40 | 30 | :type '(repeat string)
|
|
46 | 36 | :group 'elixir-format)
|
47 | 37 |
|
48 | 38 |
|
49 |
| -;;; Code |
| 39 | +;;; Code: |
50 | 40 |
|
51 |
| -(defun elixir-format--goto-line (line) |
52 |
| - (goto-char (point-min)) |
53 |
| - (forward-line (1- line))) |
| 41 | +(defun elixir-format--errbuff () |
| 42 | + (get-buffer-create "*elixir-format-errors*")) |
54 | 43 |
|
55 |
| -(defun elixir-format--delete-whole-line (&optional arg) |
56 |
| - "Delete the current line without putting it in the `kill-ring'. |
57 |
| -Derived from function `kill-whole-line'. ARG is defined as for that |
58 |
| -function. |
| 44 | +(defun elixir-format--outbuff () |
| 45 | + (get-buffer-create "*elixir-format-output*")) |
59 | 46 |
|
60 |
| -Shamelessly stolen from go-mode (https://github.com/dominikh/go-mode.el)" |
61 |
| - (setq arg (or arg 1)) |
62 |
| - (if (and (> arg 0) |
63 |
| - (eobp) |
64 |
| - (save-excursion (forward-visible-line 0) (eobp))) |
65 |
| - (signal 'end-of-buffer nil)) |
66 |
| - (if (and (< arg 0) |
67 |
| - (bobp) |
68 |
| - (save-excursion (end-of-visible-line) (bobp))) |
69 |
| - (signal 'beginning-of-buffer nil)) |
70 |
| - (cond ((zerop arg) |
71 |
| - (delete-region (progn (forward-visible-line 0) (point)) |
72 |
| - (progn (end-of-visible-line) (point)))) |
73 |
| - ((< arg 0) |
74 |
| - (delete-region (progn (end-of-visible-line) (point)) |
75 |
| - (progn (forward-visible-line (1+ arg)) |
76 |
| - (unless (bobp) |
77 |
| - (backward-char)) |
78 |
| - (point)))) |
79 |
| - (t |
80 |
| - (delete-region (progn (forward-visible-line 0) (point)) |
81 |
| - (progn (forward-visible-line arg) (point)))))) |
| 47 | +(defun elixir-format--elixir-executable () |
| 48 | + (executable-find "elixir")) |
| 49 | + |
| 50 | +(defun elixir-format--mix-executable () |
| 51 | + (executable-find "mix")) |
| 52 | + |
| 53 | +;;;###autoload |
| 54 | +(defun elixir-format (&optional called-interactively-p) |
| 55 | + (interactive "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) |
| 68 | + (with-current-buffer (elixir-format--errbuff) |
| 69 | + (setq buffer-read-only nil) |
| 70 | + (erase-buffer) |
| 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.") |
| 72 | + (setq buffer-read-only t) |
| 73 | + (ansi-color-apply-on-region (point-min) (point-max)) |
| 74 | + (special-mode) |
| 75 | + (if called-interactively-p |
| 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)) |
| 82 | + |
| 83 | + (with-current-buffer (elixir-format--errbuff) |
| 84 | + (setq buffer-read-only nil) |
| 85 | + (erase-buffer))) |
| 86 | + |
| 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"))) |
| 90 | + |
| 91 | + (write-region nil nil tmpfile) |
| 92 | + (run-hooks 'elixir-format-hook) |
| 93 | + |
| 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))) |
| 97 | + |
| 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)))) |
| 103 | + |
| 104 | +(defun elixir-format--call-format-command (tmpfile) |
| 105 | + (if (zerop (call-process-region (point-min) (point-max) "diff" nil (elixir-format--outbuff) nil "-n" "-" tmpfile)) |
| 106 | + (message "File is already formatted") |
| 107 | + (elixir-format--apply-rcs-patch (elixir-format--outbuff)) |
| 108 | + (message "elixir-format format applied")) |
| 109 | + (kill-buffer (elixir-format--errbuff))) |
| 110 | + |
| 111 | +(defun elixir-format--failed-to-format (called-interactively-p) |
| 112 | + (with-current-buffer (elixir-format--errbuff) |
| 113 | + (setq buffer-read-only t) |
| 114 | + (ansi-color-apply-on-region (point-min) (point-max)) |
| 115 | + (special-mode)) |
| 116 | + |
| 117 | + (if called-interactively-p |
| 118 | + (display-buffer (elixir-format--errbuff)) |
| 119 | + (error "elixir-format failed: see %s" (buffer-name (elixir-format--errbuff))))) |
82 | 120 |
|
83 | 121 | (defun elixir-format--apply-rcs-patch (patch-buffer)
|
84 | 122 | "Apply an RCS-formatted diff from PATCH-BUFFER to the current buffer.
|
@@ -122,57 +160,39 @@ Shamelessly stolen from go-mode (https://github.com/dominikh/go-mode.el)"
|
122 | 160 | (cl-incf line-offset len)
|
123 | 161 | (elixir-format--delete-whole-line len)))
|
124 | 162 | (t
|
125 |
| - (error "Invalid rcs patch or internal error in elixir-format--apply-rcs-patch")))))))) |
126 |
| - ) |
| 163 | + (error "Invalid rcs patch or internal error in elixir-format--apply-rcs-patch"))))))))) |
127 | 164 |
|
128 |
| -;;;###autoload |
129 |
| -(defun elixir-format (&optional is-interactive) |
130 |
| - (interactive "p") |
| 165 | +(defun elixir-format--goto-line (line) |
| 166 | + (goto-char (point-min)) |
| 167 | + (forward-line (1- line))) |
131 | 168 |
|
132 |
| - (let ((outbuff (get-buffer-create "*elixir-format-output*")) |
133 |
| - (errbuff (get-buffer-create "*elixir-format-errors*")) |
134 |
| - (tmpfile (make-temp-file "elixir-format" nil ".ex")) |
135 |
| - (our-elixir-format-arguments (list elixir-format-mix-path "format")) |
136 |
| - (output nil)) |
| 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. |
137 | 173 |
|
138 |
| - (unwind-protect |
139 |
| - (save-restriction |
140 |
| - (with-current-buffer outbuff |
141 |
| - (erase-buffer)) |
142 |
| - |
143 |
| - (with-current-buffer errbuff |
144 |
| - (setq buffer-read-only nil) |
145 |
| - (erase-buffer)) |
146 |
| - |
147 |
| - (write-region nil nil tmpfile) |
148 |
| - |
149 |
| - (run-hooks 'elixir-format-hook) |
150 |
| - |
151 |
| - (when elixir-format-arguments |
152 |
| - (setq our-elixir-format-arguments (append our-elixir-format-arguments elixir-format-arguments))) |
153 |
| - (setq our-elixir-format-arguments (append our-elixir-format-arguments (list tmpfile))) |
154 |
| - |
155 |
| - (if (zerop (apply #'call-process elixir-format-elixir-path nil errbuff nil our-elixir-format-arguments)) |
156 |
| - (progn |
157 |
| - (if (zerop (call-process-region (point-min) (point-max) "diff" nil outbuff nil "-n" "-" tmpfile)) |
158 |
| - (message "File is already formatted") |
159 |
| - (progn |
160 |
| - (elixir-format--apply-rcs-patch outbuff) |
161 |
| - (message "mix format applied"))) |
162 |
| - (kill-buffer errbuff)) |
163 |
| - |
164 |
| - (progn |
165 |
| - (with-current-buffer errbuff |
166 |
| - (setq buffer-read-only t) |
167 |
| - (ansi-color-apply-on-region (point-min) (point-max)) |
168 |
| - (special-mode)) |
169 |
| - |
170 |
| - (if is-interactive |
171 |
| - (display-buffer errbuff) |
172 |
| - (error "elixir-format failed: see %s" (buffer-name errbuff))))) |
173 |
| - |
174 |
| - (delete-file tmpfile) |
175 |
| - (kill-buffer outbuff))))) |
| 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)))))) |
176 | 196 |
|
177 | 197 | (provide 'elixir-format)
|
178 | 198 |
|
|
0 commit comments