Skip to content

Commit 0437e45

Browse files
committed
Add history Unite source
1 parent ed4c2dc commit 0437e45

File tree

2 files changed

+270
-30
lines changed

2 files changed

+270
-30
lines changed
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
if exists('g:loaded_history_ipython')
2+
finish
3+
endif
4+
let g:loaded_history_ipython = 1
5+
6+
let s:save_cpo = &cpo
7+
set cpo&vim
8+
9+
function! unite#sources#history_ipython#define()
10+
return s:source
11+
endfunction
12+
13+
let s:source = {
14+
\ 'name' : 'history/ipython',
15+
\ 'description' : 'candidates from IPython history',
16+
\ 'action_table' : {},
17+
\ 'hooks' : {},
18+
\ 'default_action' : 'send',
19+
\ 'default_kind' : 'word',
20+
\ 'syntax' : 'uniteSource__Python',
21+
\ 'max_candidates' : 100,
22+
\}
23+
24+
function! s:source.hooks.on_syntax(args, context)
25+
let save_current_syntax = get(b:, 'current_syntax', '')
26+
unlet! b:current_syntax
27+
28+
try
29+
silent! syntax include @Python syntax/python.vim
30+
syntax region uniteSource__IPythonPython
31+
\ start=' ' end='$' contains=@Python containedin=uniteSource__IPython
32+
let &l:iskeyword = substitute(&l:iskeyword, ',!$\|!,', '', '')
33+
finally
34+
let b:current_syntax = save_current_syntax
35+
endtry
36+
endfunction
37+
38+
function! s:source.hooks.on_init(args, context)
39+
if !exists('*IPythonHistory')
40+
call unite#print_source_error(
41+
\ 'IPythonHistory() does not exist', s:source.name)
42+
return
43+
endif
44+
45+
let args = unite#helper#parse_source_args(a:args)
46+
let a:context.source__session = get(a:context, 'source__session', -1)
47+
if a:context.source__session == -1
48+
let a:context.source__session = get(args, 0, -1)
49+
endif
50+
let a:context.source__input = a:context.input
51+
if a:context.source__input == '' || a:context.unite__is_restart
52+
let a:context.source__input = unite#util#input('Pattern: ',
53+
\ a:context.source__input,
54+
\ 'customlist,IPythonCmdComplete')
55+
endif
56+
57+
call unite#print_source_message('Pattern: '
58+
\ . a:context.source__input, s:source.name)
59+
endfunction
60+
61+
function! s:source.gather_candidates(args, context)
62+
if !exists('*IPythonHistory')
63+
return []
64+
endif
65+
66+
return map(IPythonHistory(a:context.source__input,
67+
\ a:context.source__session), '{
68+
\ "word" : v:val.code,
69+
\ "abbr" : printf("'''''' %d/%d '''''' %s", v:val.session, v:val.line,
70+
\ v:val.code =~ "\n" ?
71+
\ "\n" . join(split(v:val.code, "\n")[:50], "\n") : v:val.code),
72+
\ "is_multiline" : 1,
73+
\ "source__session" : v:val.session,
74+
\ "source__line" : v:val.line,
75+
\ "source__context" : a:context,
76+
\ "action__regtype" : "V",
77+
\ }')
78+
endfunction
79+
80+
let s:source.action_table.send = {
81+
\ 'description' : 'run in IPython',
82+
\ 'is_selectable' : 1,
83+
\ }
84+
function! s:source.action_table.send.func(candidates)
85+
for candidate in a:candidates
86+
let g:ipy_input = candidate.word
87+
Python2or3 run_ipy_input()
88+
silent! unlet g:ipy_input
89+
endfor
90+
endfunction
91+
92+
let s:source.action_table.session = {
93+
\ 'description' : "get history for candidate's session",
94+
\ 'is_quit' : 0,
95+
\ 'is_invalidate_cache' : 1,
96+
\ }
97+
function! s:source.action_table.session.func(candidate)
98+
let context = a:candidate.source__context
99+
let context.source__input = unite#util#input('Pattern: ',
100+
\ context.source__input,
101+
\ 'customlist,IPythonCmdComplete')
102+
let context.source__session = a:candidate.source__session
103+
endfunction
104+
105+
let s:source.action_table.session_info = {
106+
\ 'description' : "print information about a session",
107+
\ 'is_quit' : 0,
108+
\ }
109+
function! s:source.action_table.session_info.func(candidate)
110+
let store_history = get(g:, 'ipython_store_history', 1)
111+
try
112+
let g:ipython_store_history = 0
113+
let session_info = [
114+
\ "from IPython import get_ipython",
115+
\ "def _session_info(session=0):",
116+
\ " def date(d):",
117+
\ " return d.strftime('%a %d%b%Y %T')",
118+
\ " session_id, start, end, cmds, remark = " .
119+
\ " get_ipython().history_manager.get_session_info(session)",
120+
\ " val = 'start: {0}'.format(date(start))",
121+
\ " if end:",
122+
\ " val += '; end: {0}; {1} commands'.format(date(end), cmds)",
123+
\ " return val",
124+
\ ]
125+
let g:ipy_input = join(session_info, "\n")
126+
silent Python2or3 run_ipy_input(silent=True)
127+
let g:ipy_input = printf('_session_info(%d)', a:candidate.source__session)
128+
silent! unlet g:ipy_result
129+
Python2or3 eval_ipy_input('g:ipy_result')
130+
echomsg printf('session %d: %s',
131+
\ a:candidate.source__session, g:ipy_result)
132+
finally
133+
let g:ipython_store_history = store_history
134+
endtry
135+
endfunction
136+
137+
let s:source.action_table.macro = {
138+
\ 'description' : 'create IPython macro',
139+
\ 'is_selectable' : 1,
140+
\ }
141+
function! s:source.action_table.macro.func(candidates)
142+
let g:ipy_input = printf('%%macro %s %s',
143+
\ unite#util#input('Macro name: '),
144+
\ join(map(a:candidates,
145+
\ 'printf("%s/%s", v:val.source__session, v:val.source__line)'))
146+
\ )
147+
Python2or3 run_ipy_input()
148+
silent! unlet g:ipy_input
149+
endfunction
150+
151+
let s:source.action_table.yank = {
152+
\ 'description' : 'yank candidates',
153+
\ 'is_selectable' : 1,
154+
\ 'is_quit' : 1,
155+
\ }
156+
function! s:source.action_table.yank.func(candidates)
157+
if len(a:candidates) == 1 && a:candidates[0].word !~ "\n"
158+
let text = a:candidates[0].word
159+
let mode = 'v'
160+
else
161+
let text = join(map(copy(a:candidates), 'v:val.word'), "\n\n")
162+
let mode = 'V'
163+
endif
164+
call setreg('"', text, mode)
165+
if has('clipboard')
166+
if &clipboard =~# '\<unnamed\>'
167+
call setreg('*', text, mode)
168+
endif
169+
if &clipboard =~# '\<unnamedplus\>'
170+
call setreg('+', text, mode)
171+
endif
172+
endif
173+
174+
echohl Question | echo 'Yanked:' | echohl Normal
175+
echo text
176+
endfunction
177+
178+
let s:source.action_table.append = {
179+
\ 'description' : 'append candidates',
180+
\ 'is_selectable' : 1,
181+
\ }
182+
function! s:source.action_table.append.func(candidates)
183+
put = join(map(copy(a:candidates), 'v:val.word'), \"\n\n\")
184+
endfunction
185+
186+
let s:source.action_table.insert = {
187+
\ 'description' : 'insert candidates',
188+
\ 'is_selectable' : 1,
189+
\ }
190+
function! s:source.action_table.insert.func(candidates)
191+
put! = join(map(copy(a:candidates), 'v:val.word'), \"\n\n\")
192+
endfunction
193+
194+
let &cpo = s:save_cpo
195+
unlet s:save_cpo
196+
197+
" vim:set et ts=2 sts=2 sw=2:

doc/ipython.txt

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ Contents *vim-ipython-contents*
55

66
1. Completion Metadata |vim-ipython-metadata|
77
2. IPython Monitor |vim-ipython-monitor|
8-
3. Variables |vim-ipython-variables|
9-
3.1. g:ipy_autostart |g:ipy_autostart|
10-
3.2. g:ipy_completefunc |g:ipy_completefunc|
11-
3.3. g:ipy_input |g:ipy_input|
12-
3.4. g:ipy_perform_mappings |g:ipy_perform_mappings|
13-
3.5. g:ipython_completion_timeout |g:ipython_completion_timeout|
14-
3.6. g:ipython_dictionary_completion |g:ipython_dictionary_completion|
15-
3.7. g:ipython_greedy_matching |g:ipython_greedy_matching|
16-
3.8. g:ipython_history_len |g:ipython_history_len|
17-
3.9. g:ipython_history_raw |g:ipython_history_raw|
18-
3.10. g:ipython_history_timeout |g:ipython_history_timeout|
19-
3.11. g:ipython_history_unique |g:ipython_history_unique|
20-
3.12. g:ipython_run_flags |g:ipython_run_flags|
21-
3.13. g:ipython_store_history |g:ipython_store_history|
22-
3.14. g:ipython_timeout |g:ipython_timeout|
8+
3. IPython History Unite Source |vim-ipython-history|
9+
4. Variables |vim-ipython-variables|
10+
4.1. g:ipy_autostart |g:ipy_autostart|
11+
4.2. g:ipy_completefunc |g:ipy_completefunc|
12+
4.3. g:ipy_input |g:ipy_input|
13+
4.4. g:ipy_perform_mappings |g:ipy_perform_mappings|
14+
4.5. g:ipython_completion_timeout |g:ipython_completion_timeout|
15+
4.6. g:ipython_dictionary_completion |g:ipython_dictionary_completion|
16+
4.7. g:ipython_greedy_matching |g:ipython_greedy_matching|
17+
4.8. g:ipython_history_len |g:ipython_history_len|
18+
4.9. g:ipython_history_raw |g:ipython_history_raw|
19+
4.10. g:ipython_history_timeout |g:ipython_history_timeout|
20+
4.11. g:ipython_history_unique |g:ipython_history_unique|
21+
4.12. g:ipython_run_flags |g:ipython_run_flags|
22+
4.13. g:ipython_store_history |g:ipython_store_history|
23+
4.14. g:ipython_timeout |g:ipython_timeout|
2324

2425
==============================================================================
2526
1. Completion Metadata *vim-ipython-metadata*
@@ -69,48 +70,90 @@ connect to the first connection file it finds matching the glob pattern
6970
notebook kernels by design.
7071

7172
==============================================================================
72-
3. Variables *vim-ipython-variables*
73+
3. IPython History Unite Source *vim-ipython-history*
74+
75+
Note: Requires unite.vim: https://github.com/Shougo/unite.vim
76+
77+
The plugin includes a Unite source named "history/ipython" providing an
78+
interface to the history messaging in IPython. The source will prompt for a
79+
glob pattern to search for. If no pattern is provided, the search results in
80+
up to |g:ipython_history_len| of the most recent IPython commands. If the
81+
pattern begins or ends with a '*', the other end of the pattern is anchored at
82+
the start or end of the match. For example, >
83+
84+
Pattern: def *
85+
86+
will return results that start with a function definition and >
87+
88+
Pattern: *)
89+
90+
will return results ending with ')'. Otherwise a '*' is both prepended
91+
and appended to the pattern, so >
92+
93+
Pattern: sys
94+
95+
will return results containing "sys" anywhere.
96+
97+
The input prompt allows completion from the IPython namespace.
98+
99+
After selecting a history entry, the available actions are (in addition to
100+
Unite's common actions):
101+
102+
- `append` (insert the entry after the cursor line)
103+
- `insert` (insert the entry before the cursor line)
104+
- `macro` (prompt for a macro name and create an IPython macro to repeat
105+
the commands)
106+
- `send` (repeat the command in IPython)
107+
- `session` (restart the history/ipython source showing entries only from
108+
the same session as the selected entry)
109+
- `session_info` (print session date and start time)
110+
- `yank` (yank entries to unnamed and clipboard if 'clipboard' is set)
111+
112+
Multiple history entries may be selected for all of the actions.
113+
114+
==============================================================================
115+
4. Variables *vim-ipython-variables*
73116

74117
------------------------------------------------------------------------------
75-
3.1. `g:ipy_autostart` *g:ipy_autostart*
118+
4.1. `g:ipy_autostart` *g:ipy_autostart*
76119

77120
------------------------------------------------------------------------------
78-
3.2. `g:ipy_completefunc` *g:ipy_completefunc*
121+
4.2. `g:ipy_completefunc` *g:ipy_completefunc*
79122

80123
------------------------------------------------------------------------------
81-
3.3. `g:ipy_input` *g:ipy_input*
124+
4.3. `g:ipy_input` *g:ipy_input*
82125

83126
------------------------------------------------------------------------------
84-
3.4. `g:ipy_perform_mappings` *g:ipy_perform_mappings*
127+
4.4. `g:ipy_perform_mappings` *g:ipy_perform_mappings*
85128

86129
------------------------------------------------------------------------------
87-
3.5. `g:ipython_completion_timeout` *g:ipython_completion_timeout*
130+
4.5. `g:ipython_completion_timeout` *g:ipython_completion_timeout*
88131

89132
------------------------------------------------------------------------------
90-
3.6. `g:ipython_dictionary_completion` *g:ipython_dictionary_completion*
133+
4.6. `g:ipython_dictionary_completion` *g:ipython_dictionary_completion*
91134

92135
------------------------------------------------------------------------------
93-
3.7. `g:ipython_greedy_matching` *g:ipython_greedy_matching*
136+
4.7. `g:ipython_greedy_matching` *g:ipython_greedy_matching*
94137

95138
------------------------------------------------------------------------------
96-
3.8. `g:ipython_history_len` *g:ipython_history_len*
139+
4.8. `g:ipython_history_len` *g:ipython_history_len*
97140

98141
------------------------------------------------------------------------------
99-
3.9. `g:ipython_history_raw` *g:ipython_history_raw*
142+
4.9. `g:ipython_history_raw` *g:ipython_history_raw*
100143

101144
------------------------------------------------------------------------------
102-
3.10. `g:ipython_history_timeout` *g:ipython_history_timeout*
145+
4.10. `g:ipython_history_timeout` *g:ipython_history_timeout*
103146

104147
------------------------------------------------------------------------------
105-
3.11. `g:ipython_history_unique` *g:ipython_history_unique*
148+
4.11. `g:ipython_history_unique` *g:ipython_history_unique*
106149

107150
------------------------------------------------------------------------------
108-
3.12. `g:ipython_run_flags` *g:ipython_run_flags*
151+
4.12. `g:ipython_run_flags` *g:ipython_run_flags*
109152

110153
------------------------------------------------------------------------------
111-
3.13. `g:ipython_store_history` *g:ipython_store_history*
154+
4.13. `g:ipython_store_history` *g:ipython_store_history*
112155

113156
------------------------------------------------------------------------------
114-
3.14. `g:ipython_timeout` *g:ipython_timeout*
157+
4.14. `g:ipython_timeout` *g:ipython_timeout*
115158

116159
vim: textwidth=78 et filetype=help:norightleft:

0 commit comments

Comments
 (0)