-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathhistory_ipython.vim
197 lines (177 loc) · 5.99 KB
/
history_ipython.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
if exists('g:loaded_history_ipython')
finish
endif
let g:loaded_history_ipython = 1
let s:save_cpo = &cpo
set cpo&vim
function! unite#sources#history_ipython#define()
return s:source
endfunction
let s:source = {
\ 'name' : 'history/ipython',
\ 'description' : 'candidates from IPython history',
\ 'action_table' : {},
\ 'hooks' : {},
\ 'default_action' : 'send',
\ 'default_kind' : 'word',
\ 'syntax' : 'uniteSource__Python',
\ 'max_candidates' : 100,
\}
function! s:source.hooks.on_syntax(args, context)
let save_current_syntax = get(b:, 'current_syntax', '')
unlet! b:current_syntax
try
silent! syntax include @Python syntax/python.vim
syntax region uniteSource__IPythonPython
\ start=' ' end='$' contains=@Python containedin=uniteSource__IPython
let &l:iskeyword = substitute(&l:iskeyword, ',!$\|!,', '', '')
finally
let b:current_syntax = save_current_syntax
endtry
endfunction
function! s:source.hooks.on_init(args, context)
if !exists('*IPythonHistory')
call unite#print_source_error(
\ 'IPythonHistory() does not exist', s:source.name)
return
endif
let args = unite#helper#parse_source_args(a:args)
let a:context.source__session = get(a:context, 'source__session', -1)
if a:context.source__session == -1
let a:context.source__session = get(args, 0, -1)
endif
let a:context.source__input = a:context.input
if a:context.source__input == '' || a:context.unite__is_restart
let a:context.source__input = unite#util#input('Pattern: ',
\ a:context.source__input,
\ 'customlist,IPythonCmdComplete')
endif
call unite#print_source_message('Pattern: '
\ . a:context.source__input, s:source.name)
endfunction
function! s:source.gather_candidates(args, context)
if !exists('*IPythonHistory')
return []
endif
return map(IPythonHistory(a:context.source__input,
\ a:context.source__session), '{
\ "word" : v:val.code,
\ "abbr" : printf("'''''' %d/%d '''''' %s", v:val.session, v:val.line,
\ v:val.code =~ "\n" ?
\ "\n" . join(split(v:val.code, "\n")[:50], "\n") : v:val.code),
\ "is_multiline" : 1,
\ "source__session" : v:val.session,
\ "source__line" : v:val.line,
\ "source__context" : a:context,
\ "action__regtype" : "V",
\ }')
endfunction
let s:source.action_table.send = {
\ 'description' : 'run in IPython',
\ 'is_selectable' : 1,
\ }
function! s:source.action_table.send.func(candidates)
for candidate in a:candidates
let g:ipy_input = candidate.word
Python2or3 run_ipy_input()
silent! unlet g:ipy_input
endfor
endfunction
let s:source.action_table.session = {
\ 'description' : "get history for candidate's session",
\ 'is_quit' : 0,
\ 'is_invalidate_cache' : 1,
\ }
function! s:source.action_table.session.func(candidate)
let context = a:candidate.source__context
let context.source__input = unite#util#input('Pattern: ',
\ context.source__input,
\ 'customlist,IPythonCmdComplete')
let context.source__session = a:candidate.source__session
endfunction
let s:source.action_table.session_info = {
\ 'description' : "print information about a session",
\ 'is_quit' : 0,
\ }
function! s:source.action_table.session_info.func(candidate)
let store_history = get(g:, 'ipython_store_history', 1)
try
let g:ipython_store_history = 0
let session_info = [
\ "from IPython import get_ipython",
\ "def _session_info(session=0):",
\ " def date(d):",
\ " return d.strftime('%a %d%b%Y %T')",
\ " session_id, start, end, cmds, remark = " .
\ " get_ipython().history_manager.get_session_info(session)",
\ " val = 'start: {0}'.format(date(start))",
\ " if end:",
\ " val += '; end: {0}; {1} commands'.format(date(end), cmds)",
\ " return val",
\ ]
let g:ipy_input = join(session_info, "\n")
silent Python2or3 run_ipy_input(silent=True)
let g:ipy_input = printf('_session_info(%d)', a:candidate.source__session)
silent! unlet g:ipy_result
Python2or3 eval_ipy_input('g:ipy_result')
echomsg printf('session %d: %s',
\ a:candidate.source__session, g:ipy_result)
finally
let g:ipython_store_history = store_history
endtry
endfunction
let s:source.action_table.macro = {
\ 'description' : 'create IPython macro',
\ 'is_selectable' : 1,
\ }
function! s:source.action_table.macro.func(candidates)
let g:ipy_input = printf('%%macro %s %s',
\ unite#util#input('Macro name: '),
\ join(map(a:candidates,
\ 'printf("%s/%s", v:val.source__session, v:val.source__line)'))
\ )
Python2or3 run_ipy_input()
silent! unlet g:ipy_input
endfunction
let s:source.action_table.yank = {
\ 'description' : 'yank candidates',
\ 'is_selectable' : 1,
\ 'is_quit' : 1,
\ }
function! s:source.action_table.yank.func(candidates)
if len(a:candidates) == 1 && a:candidates[0].word !~ "\n"
let text = a:candidates[0].word
let mode = 'v'
else
let text = join(map(copy(a:candidates), 'v:val.word'), "\n\n")
let mode = 'V'
endif
call setreg('"', text, mode)
if has('clipboard')
if &clipboard =~# '\<unnamed\>'
call setreg('*', text, mode)
endif
if &clipboard =~# '\<unnamedplus\>'
call setreg('+', text, mode)
endif
endif
echohl Question | echo 'Yanked:' | echohl Normal
echo text
endfunction
let s:source.action_table.append = {
\ 'description' : 'append candidates',
\ 'is_selectable' : 1,
\ }
function! s:source.action_table.append.func(candidates)
put = join(map(copy(a:candidates), 'v:val.word'), \"\n\n\")
endfunction
let s:source.action_table.insert = {
\ 'description' : 'insert candidates',
\ 'is_selectable' : 1,
\ }
function! s:source.action_table.insert.func(candidates)
put! = join(map(copy(a:candidates), 'v:val.word'), \"\n\n\")
endfunction
let &cpo = s:save_cpo
unlet s:save_cpo
" vim:set et ts=2 sts=2 sw=2: