This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ammonite-term-repl.el
269 lines (229 loc) · 9.58 KB
/
ammonite-term-repl.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
;;; ammonite-term-repl.el --- Scala Ammonite REPL in term mode.
;; Copyright (C) 2018-2019 Wei Zhao
;; Author: zwild <[email protected]>
;; Created: 2018-12-26T22:41:19+08:00
;; URL: https://github.com/zwild/ammonite-term-repl
;; Package-Requires: ((emacs "24.3") (s "1.12.0") (scala-mode "0.23"))
;; Version: 0.2
;; Keywords: processes, ammnite, term, scala
;;; License:
;; 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, 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Usage
;; (add-hook 'scala-mode-hook
;; (lambda ()
;; (ammonite-term-repl-minor-mode t)))
;; You can modify your arguments by
;; (setq ammonite-term-repl-program-args '("-s" "--no-default-predef"))
;;; Code:
(require 'term)
(require 'comint)
(require 'scala-mode)
(require 's)
(require 'subr-x)
(defgroup ammonite-term-repl nil
"A minor mode for a Ammonite REPL."
:group 'scala)
(defcustom ammonite-term-repl-buffer-name "*Ammonite*"
"Buffer name for ammonite."
:type 'string
:group 'ammonite-term-repl)
(defcustom ammonite-term-repl-program "amm"
"Program name for ammonite."
:type 'string
:group 'ammonite-term-repl)
(defcustom ammonite-term-repl-program-args '()
"Arguments for ammonite command."
:type '(repeat string)
:group 'ammonite-term-repl)
(defcustom ammonite-term-repl-prompt-regex "^@ "
"Regex for ammonite prompt."
:type 'string
:group 'ammonite-term-repl)
(defcustom ammonite-term-repl-run-hook nil
"Hook to run after starting an Ammonite REPL buffer."
:type 'hook
:group 'ammonite-term-repl)
(defcustom ammonite-term-repl-predef-sc-filename "predef.sc"
"'predef.sc' filename for ammonite."
:type 'string
:group 'ammonite-term-repl)
(defcustom ammonite-term-repl-default-predef-dir "~/.ammonite"
"The default dirtory of the 'predef.sc' file."
:type 'string
:group 'ammonite-term-repl)
(defcustom ammonite-term-repl-auto-detect-predef-file t
"Auto detect predef from project."
:type 'boolean
:group 'ammonite-term-repl)
(defcustom ammonite-term-repl-auto-config-mill-project t
"Auto config mill project.
This will change `ammonite-term-repl-program' to mill
and `ammonite-term-repl-program-args' to '(-i foo.repl)."
:type 'boolean
:group 'ammonite-term-repl)
(defvar ammonite-term-repl-program-local-args '()
"Local args for ammonite term repl program.")
(defun ammonite-term-repl-check-process ()
"Check if there is an active ammonite process."
(unless (comint-check-proc ammonite-term-repl-buffer-name)
(error "Ammonite is not running")))
(defun ammonite-term-repl-code-first-line (code)
"Get the first line of CODE."
(s-trim (car-safe (s-split "\n" code))))
;;;###autoload
(defun ammonite-term-repl-send-defun ()
"Send the definition to the ammonite buffer."
(interactive)
(ammonite-term-repl-check-process)
(save-mark-and-excursion
(let (start end)
(scala-syntax:beginning-of-definition)
(setq start (point))
(scala-syntax:end-of-definition)
(setq end (point))
(let ((code (buffer-substring-no-properties start end)))
(comint-send-string ammonite-term-repl-buffer-name code)
(comint-send-string ammonite-term-repl-buffer-name "\n")
(message
(format "Sent: %s..." (ammonite-term-repl-code-first-line code)))))))
;;;###autoload
(defun ammonite-term-repl-send-region (start end)
"Send the region to the ammonite buffer.
Argument START the start region.
Argument END the end region."
(interactive "r")
(ammonite-term-repl-check-process)
(let ((code (buffer-substring-no-properties start end)))
(comint-send-string ammonite-term-repl-buffer-name "{\n")
(comint-send-string ammonite-term-repl-buffer-name code)
(comint-send-string ammonite-term-repl-buffer-name "\n}")
(comint-send-string ammonite-term-repl-buffer-name "\n")
(message
(format "Sent: %s..." (ammonite-term-repl-code-first-line code)))))
;;;###autoload
(defun ammonite-term-repl-send-buffer ()
"Send the buffer to the ammonite buffer."
(interactive)
(save-mark-and-excursion
(goto-char (point-min))
(re-search-forward "^package .+\n+" nil t)
(ammonite-term-repl-send-region (point) (point-max))))
;;;###autoload
(defun ammonite-term-repl-load-file (file-name)
"Load a file to the ammonite buffer.
Argument FILE-NAME the file name."
(interactive (comint-get-source "Load Scala file: " nil '(scala-mode) t))
(comint-check-source file-name)
(with-temp-buffer
(insert-file-contents file-name)
(ammonite-term-repl-send-buffer)))
;;;###autoload
(defun ammonite-term-repl ()
"Run an Ammonite REPL."
(interactive)
(unless (executable-find ammonite-term-repl-program)
(error (format "%s is not found." ammonite-term-repl-program)))
(unless (comint-check-proc ammonite-term-repl-buffer-name)
(ignore-errors (kill-buffer ammonite-term-repl-buffer-name))
(setq ammonite-term-repl-program-local-args ammonite-term-repl-program-args)
(when-let ((_ ammonite-term-repl-auto-detect-predef-file)
(path (or (locate-dominating-file default-directory ammonite-term-repl-predef-sc-filename)
ammonite-term-repl-default-predef-dir))
(file (expand-file-name ammonite-term-repl-predef-sc-filename path)))
(setq ammonite-term-repl-program-local-args
(append ammonite-term-repl-program-args `("-p" ,file))))
(when-let ((_ ammonite-term-repl-auto-config-mill-project)
(target "build.sc")
(path (locate-dominating-file default-directory target))
(file (expand-file-name target path)))
(setq default-directory path)
(setq-local ammonite-term-repl-program (if path "mill" "amm"))
(with-temp-buffer
(insert-file-contents file)
(goto-char (point-min))
(let ((res))
(while (re-search-forward "^object[ ]+\\([^ ]+\\)[ ]+extends" nil t)
(add-to-list 'res (match-string 1) t))
(let* ((project (completing-read "Project: " res))
(full (concat project ".repl")))
(setq ammonite-term-repl-program-local-args
(append ammonite-term-repl-program-args `("-i" ,full)))))))
(message (format "Run: %s %s"
ammonite-term-repl-program
(s-join " " ammonite-term-repl-program-local-args)))
(with-current-buffer
(apply 'term-ansi-make-term
ammonite-term-repl-buffer-name
ammonite-term-repl-program
nil
ammonite-term-repl-program-local-args)
(term-char-mode)
(term-set-escape-char ?\C-x)
(setq-local term-prompt-regexp ammonite-term-repl-prompt-regex)
(setq-local term-scroll-show-maximum-output t)
(setq-local term-scroll-to-bottom-on-output t)
(run-hooks 'ammonite-term-repl-run-hook)))
(pop-to-buffer ammonite-term-repl-buffer-name))
;;;###autoload
(defalias 'run-ammonite 'ammonite-term-repl)
(defun ammonite-term-repl--send-string (string)
"Send the code to the ammonite buffer.
Argument STRING the code to send."
(ammonite-term-repl-check-process)
(comint-send-string ammonite-term-repl-buffer-name "{\n")
(comint-send-string ammonite-term-repl-buffer-name string)
(comint-send-string ammonite-term-repl-buffer-name "\n}")
(comint-send-string ammonite-term-repl-buffer-name "\n")
(message
(format "Sent: %s..." (ammonite-term-repl-code-first-line string))))
;;;###autoload
(defun ammonite-term-repl-import-ivy-dependencies-from-sbt ()
"Try to import ivy dependencies from sbt file.
Currently only form like
libraryDependencies += \"com.typesafe.akka\" %% \"akka-actor\" % \"2.5.21\"
is available."
(interactive)
(when-let* ((file-name "build.sbt")
(path (locate-dominating-file default-directory file-name))
(file (concat path file-name)))
(with-temp-buffer
(insert-file-contents file)
(goto-char (point-min))
(let ((regex "libraryDependencies[ ]+\\+=[ ]+\"\\(.+?\\)\"[ ]+%\\{1,2\\}[ ]+\"\\(.+?\\)\"[ ]+%\\{1,2\\}[ ]+\"\\(.+?\\)\"")
(res))
(while (re-search-forward regex nil t)
(add-to-list
'res
(format "import $ivy.`%s::%s:%s`" (match-string 1) (match-string 2) (match-string 3))
t))
(ammonite-term-repl--send-string (s-join "\n" res))))))
(defvar ammonite-term-repl-minor-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-`") 'ammonite-term-repl)
(define-key map (kbd "C-c C-z") 'ammonite-term-repl)
(define-key map (kbd "C-c C-e") 'ammonite-term-repl-send-defun)
(define-key map (kbd "C-c C-r") 'ammonite-term-repl-send-region)
(define-key map (kbd "C-c C-b") 'ammonite-term-repl-send-buffer)
(define-key map (kbd "C-c C-l") 'ammonite-term-repl-load-file)
map)
"Keymap while function ‘ammonite-term-repl-minor-mode’ is active.")
;;;###autoload
(define-minor-mode ammonite-term-repl-minor-mode
"Minor mode for interacting with an Ammonite REPL."
:keymap ammonite-term-repl-minor-mode-map)
(provide 'ammonite-term-repl)
;;; ammonite-term-repl.el ends here