@@ -62,7 +62,85 @@ Customize-variable: elixir-format-mix-path"
62
62
:group 'elixir-format )
63
63
64
64
65
- ; ;; Code
65
+ ; ;; Code:
66
+
67
+ (defun elixir-format--errbuff ()
68
+ (get-buffer-create " *elixir-format-errors*" ))
69
+
70
+ (defun elixir-format--outbuff ()
71
+ (get-buffer-create " *elixir-format-output*" ))
72
+
73
+ (defun elixir-format--clean-output-buffers ()
74
+ (with-current-buffer (elixir-format--outbuff)
75
+ (erase-buffer ))
76
+
77
+ (with-current-buffer (elixir-format--errbuff)
78
+ (setq buffer-read-only nil )
79
+ (erase-buffer )))
80
+
81
+ ;;;### autoload
82
+ (defun elixir-format (&optional called-interactively-p )
83
+ (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 )
101
+ (with-current-buffer (elixir-format--errbuff)
102
+ (setq buffer-read-only nil )
103
+ (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" )
105
+ (setq buffer-read-only t )
106
+ (ansi-color-apply-on-region (point-min ) (point-max ))
107
+ (special-mode )
108
+ (if called-interactively-p
109
+ (display-buffer elixir-format--errbuff)
110
+ (error " elixir-format-configuration-missing: see %s " (buffer-name (elixir-format--errbuff))))))
111
+
112
+ (defun elixir-format--run-format (called-interactively-p tmpfile )
113
+ (write-region nil nil tmpfile)
114
+
115
+ (run-hooks 'elixir-format-hook )
116
+
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)))
120
+
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))
124
+
125
+ (delete-file tmpfile)
126
+ (kill-buffer (elixir-format--outbuff)))
127
+
128
+ (defun elixir-format--call-format-command (tmpfile )
129
+ (if (zerop (call-process-region (point-min ) (point-max ) " diff" nil (elixir-format--outbuff) nil " -n" " -" tmpfile))
130
+ (message " File is already formatted " )
131
+ (elixir-format--apply-rcs-patch (elixir-format--outbuff))
132
+ (message " elixir-format format applied " ))
133
+ (kill-buffer (elixir-format--errbuff)))
134
+
135
+ (defun elixir-format--failed-to-format (called-interactively-p )
136
+ (with-current-buffer (elixir-format--errbuff)
137
+ (setq buffer-read-only t )
138
+ (ansi-color-apply-on-region (point-min ) (point-max ))
139
+ (special-mode ))
140
+
141
+ (if called-interactively-p
142
+ (display-buffer (elixir-format--errbuff))
143
+ (error " elixir-format failed: see %s " (buffer-name (elixir-format--errbuff)))))
66
144
67
145
(defun elixir-format--goto-line (line )
68
146
(goto-char (point-min ))
@@ -138,78 +216,7 @@ Shamelessly stolen from go-mode (https://github.com/dominikh/go-mode.el)"
138
216
(cl-incf line-offset len)
139
217
(elixir-format--delete-whole-line len)))
140
218
(t
141
- (error " Invalid rcs patch or internal error in elixir-format--apply-rcs-patch " ))))))))
142
- )
143
-
144
- (defun elixir-format-elixir-and-mix-path-not-set-p ()
145
- (or (= (length elixir-format-mix-path) 0 ) (= (length elixir-format-elixir-path) 0 ))
146
- )
147
-
148
- (defun elixir-format-display-missing-configuration-error (errbuff is-interactive )
149
- (with-current-buffer errbuff
150
- (progn
151
- (setq buffer-read-only nil )
152
- (erase-buffer )
153
- (insert " elixir or mix binary path not set. Please run `C-h v a-variable RET` for `elixir-format-elixir-path` and `elixir-format-mix-path` variables to see docs to customize necessary variables for elixir and mix." )
154
- (setq buffer-read-only t )
155
- (ansi-color-apply-on-region (point-min ) (point-max ))
156
- (special-mode )
157
- (if is-interactive
158
- (display-buffer errbuff)
159
- (error " elixir-format-configuration-missing: see %s " (buffer-name errbuff))))
160
- ))
161
-
162
- ;;;### autoload
163
- (defun elixir-format (&optional is-interactive )
164
- (interactive " p" )
165
-
166
- (let ((outbuff (get-buffer-create " *elixir-format-output*" ))
167
- (errbuff (get-buffer-create " *elixir-format-errors*" ))
168
- (tmpfile (make-temp-file " elixir-format" nil " .ex" ))
169
- (our-elixir-format-arguments (list elixir-format-mix-path " format" ))
170
- (output nil ))
171
-
172
- (unwind-protect
173
- (save-restriction
174
- (with-current-buffer outbuff
175
- (erase-buffer ))
176
-
177
- (with-current-buffer errbuff
178
- (setq buffer-read-only nil )
179
- (erase-buffer ))
180
-
181
- (if (elixir-format-elixir-and-mix-path-not-set-p)
182
- (elixir-format-display-missing-configuration-error errbuff is-interactive)
183
-
184
- (write-region nil nil tmpfile)
185
-
186
- (run-mode-hooks 'elixir-format-hook )
187
-
188
- (when elixir-format-arguments
189
- (setq our-elixir-format-arguments (append our-elixir-format-arguments elixir-format-arguments)))
190
- (setq our-elixir-format-arguments (append our-elixir-format-arguments (list tmpfile)))
191
-
192
- (if (zerop (apply #'call-process elixir-format-elixir-path nil errbuff nil our-elixir-format-arguments))
193
- (progn
194
- (if (zerop (call-process-region (point-min ) (point-max ) " diff" nil outbuff nil " -n" " -" tmpfile))
195
- (message " File is already formatted " )
196
- (progn
197
- (elixir-format--apply-rcs-patch outbuff)
198
- (message " elixir-format format applied " )))
199
- (kill-buffer errbuff))
200
-
201
- (progn
202
- (with-current-buffer errbuff
203
- (setq buffer-read-only t )
204
- (ansi-color-apply-on-region (point-min ) (point-max ))
205
- (special-mode ))
206
-
207
- (if is-interactive
208
- (display-buffer errbuff)
209
- (error " elixir-format failed: see %s " (buffer-name errbuff)))))
210
-
211
- (delete-file tmpfile)
212
- (kill-buffer outbuff))))))
219
+ (error " Invalid rcs patch or internal error in elixir-format--apply-rcs-patch " )))))))))
213
220
214
221
(provide 'elixir-format )
215
222
0 commit comments