|
| 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: |
0 commit comments