-
Notifications
You must be signed in to change notification settings - Fork 3
/
haskell.vim
279 lines (221 loc) · 7.27 KB
/
haskell.vim
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
270
271
272
273
274
275
276
277
278
279
" Copyright (C) 2012 Alfredo Di Napoli
"
" Permission is hereby granted, free of charge, to any person obtaining a copy
" of this software and associated documentation files (the "Software"), to deal
" in the Software without restriction, including without limitation the rights
" to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
" copies of the Software, and to permit persons to whom the Software is
" furnished to do so, subject to the following conditions:
"
" The above copyright notice and this permission notice shall be included in
" all copies or substantial portions of the Software.
"
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
" IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
" FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
" AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
" LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
" OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
" SOFTWARE.
"
if exists("b:did_ftplugin")
finish
endif
let b:did_ftplugin=1
if !has("python")
finish
endif
" Default variables
if !exists("g:cumino_default_terminal")
let g:cumino_default_terminal = "xterm"
endif
if !exists("g:cumino_buffer_location")
let g:cumino_buffer_location = substitute(system("echo $HOME"), "\n", "", "g") . "/.cumino.buff"
endif
if !exists("g:cumino_ghci_args")
let g:cumino_ghci_args = ""
endif
if !exists("g:cumino_use_hsenv")
let g:cumino_use_hsenv = 1
endif
" Used to infer whether call :load or simply :r
let g:cumino_module_loaded = {}
python << EOF
import vim
import os
import subprocess
def write_to_buffer():
cumino_buff = vim.eval("g:cumino_buffer_location")
selected_lines = vim.eval("g:selected_text")
selected_lines = discard_function_declaration(selected_lines)
selected_lines = append_let_if_function(selected_lines)
selected_lines = wrap_if_multiline(selected_lines)
f = open(cumino_buff, "w")
for line in selected_lines:
f.write(line)
f.write(os.linesep)
f.close()
def discard_function_declaration(lines):
if contains_function_declaration(lines[0]):
return lines[1:]
return lines
def contains_function_declaration(line):
return line.split(" ")[1] == "::"
def append_let_if_function(lines):
#For now the dict will be created on-the-fly here.
#Potentially very slow.
not_prefixable_keywords = [
"import", "data", "instance",
"class", "type", "{-#"
]
is_prefixable = lines[0].split(" ")[0] not in not_prefixable_keywords
if is_prefixable:
lines[0] = "let " + lines[0]
return lines
def line_starts_with(line, keyword):
return keyword == line.split(" ")[0]
def wrap_if_multiline(lines):
# Decide whether wrapping the line with :{ :} or not.
# Some multilines, for example imports, don't require multiline
# wrapping
if len(lines) > 1 and not line_starts_with(lines[0], "import"):
return [":{"] + lines + [":}"]
return lines
def cumino_eval_visual():
write_to_buffer()
send_buffer_to_tmux()
def send_buffer_to_tmux():
cumino_buff = vim.eval("g:cumino_buffer_location")
subprocess.call(["tmux", "load-buffer", cumino_buff ])
subprocess.call(["tmux", "pasteb", "-t", "cumino"])
def cumino_show_type_under_the_cursor():
function_name = vim.eval("@z")
write_to_buffer_raw(":t " + function_name)
send_buffer_to_tmux()
def cumino_send_to_ghci():
expr = vim.eval("cmd")
write_to_buffer_raw(expr)
send_buffer_to_tmux()
def write_to_buffer_raw(content):
"""
Same of write_buffer, except that
write @content without checking it.
"""
cumino_buff = vim.eval("g:cumino_buffer_location")
f = open(cumino_buff, "w")
f.write(content)
f.write(os.linesep)
f.close()
def cumino_kill():
subprocess.call(["tmux", "kill-session", "-t", "cumino"])
EOF
"Connect to repl
fun! CuminoConnect()
" Allow nested tmux sessions.
let $TMUX=""
if CuminoSessionExists()
"Attach to an already running session
echo "Connecting to an already running cumino session..."
if (g:cumino_default_terminal == "urxvt")
call system(g:cumino_default_terminal ." -e -sh -c \"tmux attach-session -t cumino\" &")
else
call system(g:cumino_default_terminal ." -e \"tmux attach-session -t cumino\" &")
endif
echo "Connected."
else
"Change the cumino owner to be this one
let g:cumino_owner = getpid()
echo "Starting a new cumino session..."
let cmd = g:cumino_default_terminal
if (g:cumino_default_terminal == "urxvt")
let cmd .= " -e sh -c \"tmux new-session -s cumino "
else
let cmd .= " -e \"tmux new-session -s cumino "
endif
let sandbox = GetSandboxActivationStringIfPresent()
let cmd .= "'".sandbox."ghci ". g:cumino_ghci_args ."'\" &"
call system(cmd)
endif
endfun
fun! GetSandboxActivationStringIfPresent()
if($HSENV != "" && g:cumino_use_hsenv)
return "export GHC_PACKAGE_PATH=" . $GHC_PACKAGE_PATH . " && "
else
return ""
endif
endfun
fun! CuminoSessionExists()
let w:sessions = system("tmux list-sessions 2>&1 | grep cumino")
if (w:sessions != "")
return 1
else
return 0
endif
endfun
fun! CuminoEvalBuffer()
let b:buffer_name = expand("%:p")
let module_already_loaded = get(g:cumino_module_loaded, b:buffer_name)
if (!module_already_loaded)
let b:use_cmd = ":load \"". b:buffer_name ."\""
else
let b:use_cmd = ":r"
endif
call system("echo \"". escape(b:use_cmd,"\"") ."\" > ". g:cumino_buffer_location)
if CuminoSessionExists()
call system("tmux load-buffer ". g:cumino_buffer_location ."; tmux pasteb -t cumino")
let g:cumino_module_loaded[expand("%:p")] = 1
endif
endfun
function! s:NumSort(a, b)
return a:a>a:b ? 1 : a:a==a:b ? 0 : -1
endfunction
function! s:GetVisualSelection()
let [lnum1, col1] = getpos("'<")[1:2]
let [lnum2, col2] = getpos("'>")[1:2]
let lines = getline(lnum1, lnum2)
return lines
endfunction
fun! CuminoEvalVisual()
if CuminoSessionExists()
let g:selected_text = s:GetVisualSelection()
python cumino_eval_visual()
endif
endfun
fun! CuminoShowTypeUnderTheCursor()
if CuminoSessionExists()
normal! "zyw
python cumino_show_type_under_the_cursor()
endif
endfun
fun! CuminoSendToGhci()
if CuminoSessionExists()
call inputsave()
let cmd = input('Expr?: ')
call inputrestore()
python cumino_send_to_ghci()
endif
endfun
fun! CuminoCloseSession()
if CuminoSessionExists()
if g:cumino_owner == getpid()
python cumino_kill()
endif
endif
endfun
"Mnemonic: cumino Connect
map <LocalLeader>cc :call CuminoConnect()<RETURN>
"Mnemonic: cumino (Eval) Buffer
map <LocalLeader>cb :call CuminoEvalBuffer()<RETURN>
"Mnemonic: cumino (Eval) Visual (Selection)
"This is an ugly hack to cancel the visual boundaries from
"the ex prompt. By default, when in visual mode and you press
" ":" Vim will fill the prompt with <','>, but we want to cancel
" those to avoid repeating the call for every line!
map <LocalLeader>cv :<BS><BS><BS><BS><BS>call CuminoEvalVisual()<RETURN>
"Mnemonic: cumino (Show) Type
map <LocalLeader>ct :call CuminoShowTypeUnderTheCursor()<RETURN>
"Mnemonic: cumino Send
map <LocalLeader>cs :call CuminoSendToGhci()<RETURN>
"Kill cumino before exiting Vim
autocmd VimLeavePre * call CuminoCloseSession()
" vim:sw=2