-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
operation.vim
136 lines (116 loc) · 3.29 KB
/
operation.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
let s:save_cpo = &cpo
set cpo&vim
let s:register = ''
function! s:eval(f) abort
let view = winsaveview()
let reg_save = @@
try
silent exe 'normal! `[v`]y'
let code = @@
if empty(code)
return iced#message#error('finding_code_error')
endif
call a:f(code)
finally
let @@ = reg_save
call winrestview(view)
endtry
endfunction
function! s:yank_and_out(resp) abort
let value = get(a:resp, 'value', '')
if ! empty(value)
call setreg(s:register, value)
endif
return iced#nrepl#eval#out(a:resp)
endfunction
function! iced#operation#eval(type) abort
let opt = {}
if s:register !=# '"'
let opt['callback'] = funcref('s:yank_and_out')
endif
return s:eval({code -> iced#repl#execute('eval_code', code, opt)})
endfunction
function! iced#operation#setup_eval() abort
let &operatorfunc = 'iced#operation#eval'
let s:register = v:register
endfunction
function! s:__eval_and_print(resp) abort
call iced#nrepl#eval#out(a:resp)
if has_key(a:resp, 'value')
call iced#buffer#stdout#append(a:resp['value'])
endif
endfunction
function! iced#operation#eval_and_print(type) abort
let opt = {
\ 'use-printer?': v:true,
\ 'callback': funcref('s:__eval_and_print'),
\}
return s:eval({code -> iced#nrepl#eval#code(code, opt)})
endfunction
function! iced#operation#eval_and_tap(type) abort
return s:eval({code -> iced#repl#execute('eval_code', printf('(clojure.core/tap> %s)', code))})
endfunction
function! s:replace_by_response_value(resp) abort
if has_key(a:resp, 'value')
let @@ = printf(';; %s', a:resp['value'])
silent normal! gv"0p
endif
if has_key(a:resp, 'ex') && !empty(a:resp['ex'])
call iced#message#error_str(a:resp['ex'])
endif
call iced#nrepl#eval#err(get(a:resp, 'err', ''))
endfunction
function! iced#operation#eval_and_replace(type) abort
return s:eval({code -> iced#repl#execute(
\ 'eval_raw',
\ iced#nrepl#eval#normalize_code(code),
\ funcref('s:replace_by_response_value'))})
endfunction
function! iced#operation#eval_and_comment(type) abort
let opt = {'callback': funcref('s:__eval_and_comment')}
return s:eval({code -> iced#nrepl#eval#code(code, opt)})
endfunction
function! s:__eval_and_comment(resp) abort
if has_key(a:resp, 'value')
let lnum = line('.')
let line = getline(lnum)
if line =~# ';; => .\+$'
let idx = strridx(line, ';; => ')
call setline(lnum, printf('%s;; => %s', line[0:idx-1], a:resp['value']))
else
call setline(lnum, printf('%s ;; => %s', line, a:resp['value']))
endif
endif
endfunction
function! iced#operation#macroexpand(type) abort
let view = winsaveview()
let reg_save = @@
try
silent exe 'normal! `[v`]y'
let code = @@
if empty(code)
return iced#message#error('finding_code_error')
endif
call iced#nrepl#macro#expand(code)
finally
let @@ = reg_save
call winrestview(view)
endtry
endfunction
function! iced#operation#macroexpand_1(type) abort
let view = winsaveview()
let reg_save = @@
try
silent exe 'normal! `[v`]y'
let code = @@
if empty(code)
return iced#message#error('finding_code_error')
endif
call iced#nrepl#macro#expand_1(code)
finally
let @@ = reg_save
call winrestview(view)
endtry
endfunction
let &cpo = s:save_cpo
unlet s:save_cpo