-
Notifications
You must be signed in to change notification settings - Fork 102
/
alchemist-iex.el
219 lines (178 loc) · 7.75 KB
/
alchemist-iex.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
;;; alchemist-help.el --- Interaction with an Elixir IEx process
;; Copyright © 2014-2017 Samuel Tonini
;; Author: Samuel Tonini <tonini.samuel@gmail.com
;; This file is not part of GNU Emacs.
;; This program 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.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Interaction with an Elixir IEx process
;;; Code:
(require 'comint)
(require 'company)
(require 'alchemist-key)
(require 'alchemist-scope)
(require 'alchemist-project)
(defgroup alchemist-iex nil
"Interaction with an Elixir IEx process."
:prefix "alchemist-iex-"
:group 'alchemist)
(defcustom alchemist-iex-program-name "iex"
"The shell command for iex."
:type 'string
:group 'alchemist-iex)
(defvar alchemist-iex-prompt-regexp "^\\(iex\\|\\.\\.\\.\\)(.+)>"
"Prompt regex pattern of IEx interpreter.
Should match prompts that looks like these:
iex(1)>
...(1)>")
(defcustom alchemist-iex-prompt-read-only t
"If non-nil, the prompt will be read-only."
:type 'boolean
:group 'alchemist-iex)
(defvar alchemist-iex-buffer nil
"The buffer in which the Elixir IEx process is running.")
(defvar alchemist-iex-mode-hook nil
"Hook for customizing `alchemist-iex-mode'.")
(defvar alchemist-iex-mode-map
(let ((map (nconc (make-sparse-keymap) comint-mode-map)))
(define-key map "\t" 'company-complete)
(define-key map (kbd "TAB") 'company-complete)
(define-key map (kbd (format "%s i r" alchemist-key-command-prefix)) 'alchemist-iex-open-input-ring)
(define-key map (kbd (format "%s i c" alchemist-key-command-prefix)) 'alchemist-iex-clear-buffer)
(define-key map (kbd (format "%s h e" alchemist-key-command-prefix)) 'alchemist-help-search-at-point)
(define-key map (kbd "M-.") 'alchemist-goto-definition-at-point)
map))
(define-derived-mode alchemist-iex-mode comint-mode "Alchemist-IEx"
"Major mode for interacting with an Elixir IEx process.
\\<alchemist-iex-mode-map>"
nil "Alchemist-IEx"
(set (make-local-variable 'comint-prompt-regexp) alchemist-iex-prompt-regexp)
(set (make-local-variable 'comint-prompt-read-only) alchemist-iex-prompt-read-only)
(set (make-local-variable 'comint-input-autoexpand) nil)
(set (make-local-variable 'comint-input-sender) 'alchemist-iex--send-command)
(add-hook 'comint-output-filter-functions 'alchemist-iex-spot-prompt nil t))
(defun alchemist-iex-command (arg)
(split-string-and-unquote
(if (null arg) alchemist-iex-program-name
(read-string "Command to run Elixir IEx: " (concat alchemist-iex-program-name arg)))))
(defun alchemist-iex-start-process (command)
"Start an IEX process.
With universal prefix \\[universal-argument], prompts for a COMMAND,
otherwise uses `alchemist-iex-program-name'.
It runs the hook `alchemist-iex-mode-hook' after starting the process and
setting up the IEx buffer."
(interactive (list (alchemist-iex-command current-prefix-arg)))
(setq alchemist-iex-buffer
(apply 'make-comint "Alchemist-IEx" (car command) nil (cdr command)))
(with-current-buffer alchemist-iex-buffer
(alchemist-iex-mode)
(run-hooks 'alchemist-iex-mode-hook)))
(defun alchemist-iex-process (&optional arg)
(or (if (buffer-live-p alchemist-iex-buffer)
(get-buffer-process alchemist-iex-buffer))
(progn
(let ((current-prefix-arg arg))
(call-interactively 'alchemist-iex-start-process))
(alchemist-iex-process arg))))
(defun alchemist-iex--remove-newlines (string)
(replace-regexp-in-string "\n" " " string))
(defun alchemist-iex-send-last-sexp ()
"Send the previous sexp to the inferior IEx process."
(interactive)
(alchemist-iex-send-region (save-excursion (backward-sexp) (point)) (point)))
(defun alchemist-iex-send-current-line ()
"Sends the current line to the IEx process."
(interactive)
(let ((str (thing-at-point 'line)))
(alchemist-iex--send-command (alchemist-iex-process) str)))
(defun alchemist-iex-send-current-line-and-go ()
"Sends the current line to the inferior IEx process
and jump to the buffer."
(interactive)
(call-interactively 'alchemist-iex-send-current-line)
(pop-to-buffer (process-buffer (alchemist-iex-process))))
(defun alchemist-iex-send-region-and-go ()
"Sends the marked region to the inferior IEx process
and jump to the buffer."
(interactive)
(call-interactively 'alchemist-iex-send-region)
(pop-to-buffer (process-buffer (alchemist-iex-process))))
(defun alchemist-iex-send-region (beg end)
"Sends the marked region to the IEx process."
(interactive (list (point) (mark)))
(unless (and beg end)
(error "The mark is not set now, so there is no region"))
(let* ((region (buffer-substring-no-properties beg end)))
(alchemist-iex--send-command (alchemist-iex-process) region)))
(defun alchemist-iex-compile-this-buffer ()
"Compiles the current buffer in the IEx process."
(interactive)
(let* ((path (if (alchemist-project-p)
(format "%s/_build/dev/" (alchemist-project-root))
"."))
(str (format "c(\"%s\", \"%s\")" (buffer-file-name) path)))
(alchemist-iex--send-command (alchemist-iex-process) str)))
(defun alchemist-iex-compile-this-buffer-and-go ()
"Compiles the current buffer in the IEx process and jump to the buffer."
(interactive)
(alchemist-iex-compile-this-buffer)
(pop-to-buffer (process-buffer (alchemist-iex-process))))
(defun alchemist-iex-reload-module ()
"Recompiles and reloads the current module in the IEx process."
(interactive)
(let ((str (format "r(%s)" (alchemist-scope-module))))
(alchemist-iex--send-command (alchemist-iex-process) str)))
(defun alchemist-iex--send-command (proc str)
(let ((lines (split-string str "\n" nil)))
(with-current-buffer (process-buffer proc)
(-map (lambda (line)
(goto-char (process-mark proc))
(insert-before-markers (concat line "\n"))
(move-marker comint-last-input-end (point))
(comint-send-string proc (concat line "\n"))) lines))))
(defun alchemist-iex-spot-prompt (_string)
(let ((proc (get-buffer-process (current-buffer))))
(when proc
(save-excursion
(goto-char (process-mark proc))))))
(defun alchemist-iex-clear-buffer ()
"Clear the current iex process buffer."
(interactive)
(let ((comint-buffer-maximum-size 0))
(comint-truncate-buffer)))
(defun alchemist-iex-open-input-ring ()
"Open the buffer containing the input history."
(interactive)
(progn
(comint-dynamic-list-input-ring)
(other-window 1)))
;;;###autoload
(defalias 'run-elixir 'alchemist-iex-run)
(defalias 'inferior-elixir 'alchemist-iex-run)
;;;###autoload
(defun alchemist-iex-run (&optional arg)
"Start an IEx process.
Show the IEx buffer if an IEx process is already run."
(interactive "P")
(let ((proc (alchemist-iex-process arg)))
(pop-to-buffer (process-buffer proc))))
;;;###autoload
(defun alchemist-iex-project-run ()
"Start an IEx process with mix 'iex -S mix' in the
context of an Elixir project.
Show the IEx buffer if an IEx process is already run."
(interactive)
(if (alchemist-project-p)
(let ((default-directory (alchemist-project-root)))
(pop-to-buffer (process-buffer (alchemist-iex-process " -S mix"))))
(message "No mix.exs file available. Please use `alchemist-iex-run' instead.")))
(provide 'alchemist-iex)
;;; alchemist-iex.el ends here