-
Notifications
You must be signed in to change notification settings - Fork 32
/
vlime.vim
521 lines (449 loc) · 15.1 KB
/
vlime.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
function! VlimeCloseCurConnection()
let conn = VlimeGetConnection()
if type(conn) == v:t_none
return
endif
call VlimeCloseConnection(conn)
endfunction
function! VlimeRenameCurConnection()
let conn = VlimeGetConnection()
if type(conn) == v:t_none
return
endif
let new_name = input('New name: ', conn.cb_data['name'])
call VlimeRenameConnection(conn, new_name)
endfunction
" VlimeConnectREPL(host, port[, name])
function! VlimeConnectREPL(host, port, ...)
if a:0 > 0
let conn = VlimeNewConnection(a:1)
else
let conn = VlimeNewConnection()
endif
try
call conn.Connect(a:host, a:port)
catch
call VlimeCloseConnection(conn)
call vlime#ui#ErrMsg(v:exception)
return
endtry
call s:CleanUpNullBufConnections()
let contribs = exists('g:vlime_contribs') ?
\ g:vlime_contribs : [
\ 'SWANK-ASDF', 'SWANK-PACKAGE-FU',
\ 'SWANK-PRESENTATIONS', 'SWANK-FANCY-INSPECTOR',
\ 'SWANK-C-P-C', 'SWANK-ARGLISTS', 'SWANK-REPL',
\ 'SWANK-FUZZY']
call vlime#ChainCallbacks(
\ function(conn.ConnectionInfo, [v:true]),
\ function('s:OnConnectionInfoComplete'),
\ function(conn.SwankRequire, [contribs]),
\ function('s:OnSwankRequireComplete'),
\ function('vlime#contrib#CallInitializers', [conn]),
\ function('s:OnCallInitializersComplete'))
endfunction
function! VlimeSelectCurConnection()
let conn = VlimeSelectConnection(v:false)
if type(conn) != v:t_none
" XXX: Cleanup buffers & windows for the old connection?
let b:vlime_conn = conn
endif
endfunction
function! VlimeSendCurThingToREPL(thing)
if a:thing == 'thing'
let str = vlime#ui#CurExpr()
if len(str) <= 0
let str = vlime#ui#CurAtom()
endif
elseif a:thing == 'expr'
let str = vlime#ui#CurExpr()
elseif a:thing == 'atom'
let str = vlime#ui#CurAtom()
elseif a:thing == 'selection'
let str = vlime#ui#CurSelection()
endif
if len(str) <= 0
return
endif
let conn = VlimeGetConnection()
if type(conn) == v:t_none
return
endif
call conn.ui.OnWriteString(conn, "--\n", {'name': 'REPL-SEP', 'package': 'KEYWORD'})
call conn.WithThread({'name': 'REPL-THREAD', 'package': 'KEYWORD'},
\ function(conn.ListenerEval, [str]))
endfunction
function! VlimeCompileCurThing(thing)
if a:thing == 'expr'
let [str, s_pos, e_pos] = vlime#ui#CurExpr(v:true)
elseif a:thing == 'selection'
let [str, s_pos, e_pos] = vlime#ui#CurSelection(v:true)
endif
if len(str) <= 0
return
endif
let conn = VlimeGetConnection()
if type(conn) == v:t_none
return
endif
call conn.ui.OnWriteString(conn, "--\n", {'name': 'REPL-SEP', 'package': 'KEYWORD'})
let [str_line, str_col] = s_pos
call conn.CompileStringForEmacs(
\ str, bufnr('%'),
\ line2byte(str_line) + str_col - 1,
\ expand('%:p'))
endfunction
function! VlimeInspectCurThing(thing)
if a:thing == 'thing'
let str = vlime#ui#CurExpr()
if len(str) <= 0
let str = vlime#ui#CurAtom()
endif
elseif a:thing == 'expr'
let str = vlime#ui#CurExpr()
elseif a:thing == 'atom'
let str = vlime#ui#CurAtom()
elseif a:thing == 'selection'
let str = vlime#ui#CurSelection()
endif
if len(str) <= 0
return
endif
let conn = VlimeGetConnection()
if type(conn) == v:t_none
return
endif
call conn.InitInspector(str,
\ {c, r -> c.ui.OnInspect(c, r, v:null, v:null)})
endfunction
function! VlimeCompileCurFile()
let fname = expand('%:p')
if len(fname) <= 0
return
endif
let conn = VlimeGetConnection()
if type(conn) == v:t_none
return
endif
call conn.ui.OnWriteString(conn, "--\n", {'name': 'REPL-SEP', 'package': 'KEYWORD'})
call conn.CompileFileForEmacs(fname)
endfunction
function! VlimeExpandCurMacro(expand_all)
let expr = vlime#ui#CurExpr()
if len(expr) <= 0
return
endif
let conn = VlimeGetConnection()
if type(conn) == v:t_none
return
endif
if a:expand_all
call conn.SwankMacroExpandAll(expr, function('s:ShowAsyncResult'))
else
call conn.SwankMacroExpandOne(expr, function('s:ShowAsyncResult'))
endif
endfunction
function! VlimeDisassembleCurForm()
let expr = vlime#ui#CurExpr()
if len(expr) <= 0
return
endif
let conn = VlimeGetConnection()
if type(conn) == v:t_none
return
endif
call conn.DisassembleForm(expr, function('s:ShowAsyncResult'))
endfunction
function! VlimeLoadCurFile()
let fname = expand('%:p')
if len(fname) > 0
let conn = VlimeGetConnection()
if type(conn) == v:t_none
return
endif
call conn.LoadFile(fname, function('s:OnLoadFileComplete', [fname]))
endif
endfunction
function! VlimeSetCurPackage()
let conn = VlimeGetConnection()
if type(conn) == v:t_none
return
endif
let pkg = conn.GetCurrentPackage()
let pkg = input('Set package: ', pkg[0])
if len(pkg) <= 0
call vlime#ui#ErrMsg('Canceled.')
return
endif
call conn.SetPackage(pkg)
endfunction
function! VlimeSwankRequire(contribs)
let conn = VlimeGetConnection()
if type(conn) == v:t_none
return
endif
call conn.SwankRequire(a:contribs, function('s:OnSwankRequireComplete'))
endfunction
function! VlimeCurOperatorArgList()
let conn = VlimeGetConnection(v:true)
if type(conn) == v:t_none
return
endif
let [s_line, s_col] = searchpairpos('(', '', ')', 'bnW')
if s_line <= 0 || s_col <= 0
return
endif
let old_cur = getcurpos()
try
call setpos('.', [0, s_line, s_col, 0])
let op = vlime#ui#CurOperator()
finally
call setpos('.', old_cur)
endtry
if len(op) > 0
call conn.OperatorArgList(op, function('s:OnOperatorArgListComplete', [op]))
endif
endfunction
function! VlimeDescribeCurSymbol(sym_type)
if a:sym_type == 'operator'
let sym = vlime#ui#CurOperator()
elseif a:sym_type == 'atom'
let sym = vlime#ui#CurAtom()
endif
if len(sym) > 0
let conn = VlimeGetConnection()
if type(conn) == v:t_none
return
endif
call conn.DescribeSymbol(sym, function('s:ShowAsyncResult'))
endif
endfunction
function! VlimeCompleteFunc(findstart, base)
let start_col = s:CompleteFindStart()
if a:findstart
return start_col
endif
let conn = VlimeGetConnection()
if type(conn) == v:t_none
return -1
endif
if s:ConnHasContrib(conn, 'SWANK-FUZZY')
call conn.FuzzyCompletions(a:base,
\ function('s:OnFuzzyCompletionsComplete', [start_col + 1]))
else
call conn.SimpleCompletions(a:base,
\ function('s:OnSimpleCompletionsComplete', [start_col + 1]))
endif
" Actual completions are found in s:OnFuzzyCompletionsComplete(...)
" XXX: The refresh option doesn't work, why?
return {'words': [], 'refresh': 'always'}
endfunction
function! VlimeKey(key)
if tolower(a:key) == 'space'
call VlimeCurOperatorArgList()
elseif tolower(a:key) == 'cr'
call VlimeCurOperatorArgList()
elseif tolower(a:key) == 'tab'
let line = getline('.')
let old_cur = getcurpos()
try
normal! ^
let old_first_col = virtcol('.')
finally
call setpos('.', old_cur)
endtry
let col = virtcol('.')
if col <= old_first_col
let indent = VlimeCalcCurIndent()
if old_first_col - 1 < indent
call vlime#ui#IndentCurLine(indent)
else
return "\<tab>"
endif
else
return "\<c-x>\<c-o>"
endif
else
throw 'VlimeKey: Unknown key: ' . a:key
endif
return ''
endfunction
function! VlimeCalcCurIndent()
let line_no = line('.')
let conn = VlimeGetConnection(v:true)
if type(conn) == v:t_none
return lispindent(line_no)
endif
let [s_line, s_col] = searchpairpos('(', '', ')', 'bnW')
if s_line <= 0 || s_col <= 0
return lispindent(line_no)
endif
let old_cur = getcurpos()
try
call setpos('.', [0, s_line, s_col, 0])
let vs_col = virtcol('.')
let s_op = vlime#ui#CurOperator()
finally
call setpos('.', old_cur)
endtry
let matches = matchlist(s_op, '\(\([^:|]\+\||[^|]\+|\):\{1,2}\)\?\([^:|]\+\||[^|]\+|\)$')
if len(matches) == 0
return lispindent(line_no)
endif
let op_pkg = toupper(s:NormalizeIdentifierForIndentInfo(matches[2]))
let op = tolower(s:NormalizeIdentifierForIndentInfo(matches[3]))
if len(op_pkg) == 0
let op_pkg = conn.GetCurrentPackage()
if type(op_pkg) == v:t_list
let op_pkg = op_pkg[0]
endif
endif
let indent_info = get(conn.cb_data, 'indent_info', {})
if has_key(indent_info, op) && index(indent_info[op][1], op_pkg) >= 0
return vs_col + 1
else
return lispindent(line_no)
endif
endfunction
" VlimeSetup([force])
function! VlimeSetup(...)
let force = vlime#GetNthVarArg(a:000, 0, v:false)
if !force && exists('b:vlime_setup') && b:vlime_setup
return
endif
let b:vlime_setup = v:true
let [host, port] = exists('g:vlime_address') ?
\ g:vlime_address : ['127.0.0.1', 7002]
setlocal omnifunc=VlimeCompleteFunc
setlocal filetype=lisp
setlocal indentexpr=VlimeCalcCurIndent()
inoremap <buffer> <space> <space><c-r>=VlimeKey('space')<cr>
inoremap <buffer> <cr> <cr><c-r>=VlimeKey("cr")<cr>
inoremap <buffer> <tab> <c-r>=VlimeKey("tab")<cr>
" Connection operations
execute 'nnoremap <buffer> <LocalLeader>cc :call VlimeConnectREPL(' . string(host) . ', ' . port . ')<cr>'
nnoremap <buffer> <LocalLeader>cs :call VlimeSelectCurConnection()<cr>
nnoremap <buffer> <LocalLeader>cd :call VlimeCloseCurConnection()<cr>
nnoremap <buffer> <LocalLeader>cr :call VlimeRenameCurConnection()<cr>
" Sending stuff to the REPL
nnoremap <buffer> <LocalLeader>ss :call VlimeSendCurThingToREPL('thing')<cr>
nnoremap <buffer> <LocalLeader>se :call VlimeSendCurThingToREPL('expr')<cr>
nnoremap <buffer> <LocalLeader>sa :call VlimeSendCurThingToREPL('atom')<cr>
vnoremap <buffer> <LocalLeader>s :<c-u>call VlimeSendCurThingToREPL('selection')<cr>
" Expanding macros
nnoremap <buffer> <LocalLeader>m1 :call VlimeExpandCurMacro(v:false)<cr>
nnoremap <buffer> <LocalLeader>ma :call VlimeExpandCurMacro(v:true)<cr>
" Compilation operations
nnoremap <buffer> <LocalLeader>oe :call VlimeCompileCurThing('expr')<cr>
nnoremap <buffer> <LocalLeader>of :call VlimeCompileCurFile()<cr>
vnoremap <buffer> <LocalLeader>o :<c-u>call VlimeCompileCurThing('selection')<cr>
" Describing things
nnoremap <buffer> <LocalLeader>do :call VlimeDescribeCurSymbol('operator')<cr>
nnoremap <buffer> <LocalLeader>da :call VlimeDescribeCurSymbol('atom')<cr>
" Inspection
nnoremap <buffer> <LocalLeader>II :call VlimeInspectCurThing('thing')<cr>
nnoremap <buffer> <LocalLeader>Ii :call VlimeInspectCurThing('thing')<cr>
nnoremap <buffer> <LocalLeader>IE :call VlimeInspectCurThing('expr')<cr>
nnoremap <buffer> <LocalLeader>Ie :call VlimeInspectCurThing('expr')<cr>
nnoremap <buffer> <LocalLeader>IA :call VlimeInspectCurThing('atom')<cr>
nnoremap <buffer> <LocalLeader>Ia :call VlimeInspectCurThing('atom')<cr>
vnoremap <buffer> <LocalLeader>I :<c-u>call VlimeInspectCurThing('selection')<cr>
" Other stuff
nnoremap <buffer> <LocalLeader>i :call VlimeInteractionMode()<cr>
nnoremap <buffer> <LocalLeader>l :call VlimeLoadCurFile()<cr>
nnoremap <buffer> <LocalLeader>a :call VlimeDisassembleCurForm()<cr>
nnoremap <buffer> <LocalLeader>p :call VlimeSetCurPackage()<cr>
endfunction
function! VlimeInteractionMode()
if getbufvar(bufnr('%'), 'vlime_interaction_mode', v:false)
let b:vlime_interaction_mode = v:false
nnoremap <buffer> <cr> <cr>
vnoremap <buffer> <cr> <cr>
else
let b:vlime_interaction_mode = v:true
nnoremap <buffer> <cr> :call VlimeSendCurThingToREPL('thing')<cr>
vnoremap <buffer> <cr> :<c-u>call VlimeSendCurThingToREPL('selection')<cr>
endif
endfunction
function! s:NormalizeIdentifierForIndentInfo(ident)
let ident_len = len(a:ident)
if ident_len >= 2 && a:ident[0] == '|' && a:ident[ident_len-1] == '|'
return strpart(a:ident, 1, ident_len - 2)
else
return a:ident
endif
endfunction
function! s:CompleteFindStart()
let col = col('.') - 1
let line = getline('.')
while col > 0 && match(line[col-1], '\_s\|[()#;"'']') < 0
let col -= 1
endwhile
return col
endfunction
function! s:ConnHasContrib(conn, contrib)
return has_key(a:conn.cb_data, 'contribs') &&
\ index(a:conn.cb_data['contribs'], a:contrib) >= 0
endfunction
function! s:OnCallInitializersComplete(conn)
echom a:conn.cb_data['name'] . ' established.'
endfunction
function! s:OnSwankRequireComplete(conn, result)
echom '-- OnSwankRequireComplete -------------------------'
let a:conn.cb_data['contribs'] = a:result
endfunction
function! s:OnConnectionInfoComplete(conn, result)
echom '-- OnConnectionInfoComplete -------------------------'
let a:conn.cb_data['version'] = a:result['VERSION']
let a:conn.cb_data['pid'] = a:result['PID']
endfunction
function! s:OnFuzzyCompletionsComplete(col, conn, result)
let comps = a:result[0]
if type(comps) == v:t_none
let comps = []
endif
let r_comps = []
for c in comps
let cobj = {'word': c[0],'menu': c[3]}
call add(r_comps, cobj)
endfor
call complete(a:col, r_comps)
endfunction
function! s:OnSimpleCompletionsComplete(col, conn, result)
let comps = a:result[0]
if type(comps) == v:t_none
let comps = []
endif
call complete(a:col, comps)
endfunction
function! s:OnOperatorArgListComplete(sym, conn, result)
if type(a:result) == v:t_none
return
endif
let old_pos = getcurpos()
try
call vlime#ui#ShowPreview(a:conn, a:result, v:false, 2)
finally
call setpos('.', old_pos)
endtry
endfunction
function! s:OnLoadFileComplete(fname, conn, result)
echom 'Loaded: ' . a:fname
endfunction
function! s:ShowAsyncResult(conn, result)
let old_pos = getcurpos()
try
call vlime#ui#ShowPreview(a:conn, a:result, v:false, 12)
finally
call setpos('.', old_pos)
endtry
endfunction
function! s:CleanUpNullBufConnections()
let old_buf = bufnr('%')
try
bufdo! if exists('b:vlime_conn') && type(b:vlime_conn) == v:t_none
\ | unlet b:vlime_conn | endif
finally
execute 'hide buffer ' . old_buf
endtry
endfunction