PageRenderTime 1238ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Plugins/Shell/Plugin/autoload/vimshell/mappings.vim

https://bitbucket.org/WscriChy/vim-configuration
Vim Script | 669 lines | 513 code | 68 blank | 88 comment | 59 complexity | da463254192d12f140a3055af2d84959 MD5 | raw file
  1. "=============================================================================
  2. " FILE: mappings.vim
  3. " AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com>
  4. " License: MIT license {{{
  5. " Permission is hereby granted, free of charge, to any person obtaining
  6. " a copy of this software and associated documentation files (the
  7. " "Software"), to deal in the Software without restriction, including
  8. " without limitation the rights to use, copy, modify, merge, publish,
  9. " distribute, sublicense, and/or sell copies of the Software, and to
  10. " permit persons to whom the Software is furnished to do so, subject to
  11. " the following conditions:
  12. "
  13. " The above copyright notice and this permission notice shall be included
  14. " in all copies or substantial portions of the Software.
  15. "
  16. " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. " OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. " MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. " IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  20. " CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. " TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  22. " SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. " }}}
  24. "=============================================================================
  25. " Define default mappings.
  26. function! vimshell#mappings#define_default_mappings() "{{{
  27. " Plugin keymappings "{{{
  28. nnoremap <buffer><silent> <Plug>(vimshell_enter)
  29. \ :<C-u>call vimshell#execute_current_line(0)<CR><ESC>
  30. nnoremap <buffer><silent> <Plug>(vimshell_previous_prompt)
  31. \ :<C-u>call <SID>previous_prompt()<CR>
  32. nnoremap <buffer><silent> <Plug>(vimshell_next_prompt)
  33. \ :<C-u>call <SID>next_prompt()<CR>
  34. nnoremap <buffer><silent> <Plug>(vimshell_delete_previous_output)
  35. \ :<C-u>call <SID>delete_previous_output()<CR>
  36. nnoremap <buffer><silent> <Plug>(vimshell_paste_prompt)
  37. \ :<C-u>call vimshell#mappings#_paste_prompt()<CR>
  38. nnoremap <buffer><silent> <Plug>(vimshell_move_end_argument)
  39. \ :<C-u>call <SID>move_end_argument()<CR>
  40. nnoremap <buffer><silent> <Plug>(vimshell_hide)
  41. \ :<C-u>call <SID>hide()<CR>
  42. nnoremap <buffer><silent> <Plug>(vimshell_exit)
  43. \ :<C-u>call <SID>exit()<CR>
  44. nnoremap <buffer><expr> <Plug>(vimshell_change_line)
  45. \ vimshell#check_prompt() ?
  46. \ printf('0%dlc$', vimshell#util#strchars(
  47. \ matchstr(getline('.'), b:vimshell.context.prompt_pattern))) : 'ddO'
  48. nmap <buffer> <Plug>(vimshell_delete_line)
  49. \ <Plug>(vimshell_change_line)<ESC>
  50. nnoremap <buffer><silent> <Plug>(vimshell_hangup)
  51. \ :<C-u>call <SID>hangup(0)<CR>
  52. nnoremap <buffer><silent> <Plug>(vimshell_interrupt)
  53. \ :<C-u>call <SID>interrupt(0)<CR>
  54. nnoremap <buffer><silent> <Plug>(vimshell_insert_enter)
  55. \ :<C-u>call <SID>insert_enter()<CR>
  56. nnoremap <buffer><silent> <Plug>(vimshell_insert_head)
  57. \ :<C-u>call <SID>insert_head()<CR>
  58. nnoremap <buffer><silent> <Plug>(vimshell_append_enter)
  59. \ :<C-u>call <SID>append_enter()<CR>
  60. nnoremap <buffer><silent> <Plug>(vimshell_append_end)
  61. \ :<C-u>call <SID>append_end()<CR>
  62. nnoremap <buffer><silent> <Plug>(vimshell_clear)
  63. \ :<C-u>call <SID>clear(0)<CR>
  64. nnoremap <buffer><silent> <Plug>(vimshell_move_head)
  65. \ :<C-u>call <SID>move_head()<CR>
  66. nnoremap <buffer><silent> <Plug>(vimshell_execute_by_background)
  67. \ :<C-u>call <SID>execute_by_background(0)<CR>
  68. inoremap <buffer><silent><expr> <Plug>(vimshell_command_complete)
  69. \ pumvisible() ?
  70. \ "\<C-n>" :
  71. \ !empty(b:vimshell.continuation) ?
  72. \ "\<C-o>:call vimshell#int_mappings#command_complete()\<CR>" :
  73. \ vimshell#parser#check_wildcard() ?
  74. \ <SID>expand_wildcard() : vimshell#complete#start()
  75. inoremap <buffer><silent><expr> <Plug>(vimshell_zsh_complete)
  76. \ unite#sources#vimshell_zsh_complete#start_complete(!0)
  77. inoremap <buffer><silent> <Plug>(vimshell_push_current_line)
  78. \ <ESC>:call <SID>push_current_line()<CR>
  79. inoremap <buffer><silent> <Plug>(vimshell_insert_last_word)
  80. \ <ESC>:call <SID>insert_last_word()<CR>
  81. inoremap <buffer><silent> <Plug>(vimshell_move_head)
  82. \ <ESC>:call <SID>insert_head()<CR>
  83. inoremap <buffer><silent><expr> <Plug>(vimshell_delete_backward_line)
  84. \ <SID>delete_backward_line()
  85. inoremap <buffer><silent><expr> <Plug>(vimshell_delete_backward_word)
  86. \ vimshell#get_cur_text() == '' ? '' : "\<C-w>"
  87. inoremap <buffer><silent> <Plug>(vimshell_enter)
  88. \ <C-g>u<C-o>:call vimshell#execute_current_line(1)<CR>
  89. inoremap <buffer><silent> <Plug>(vimshell_interrupt)
  90. \ <C-o>:call <SID>interrupt(1)<CR>
  91. inoremap <buffer><silent> <Plug>(vimshell_move_previous_window)
  92. \ <ESC><C-w>p
  93. inoremap <buffer><silent><expr> <Plug>(vimshell_delete_backward_char)
  94. \ <SID>delete_backward_char()
  95. inoremap <buffer><silent><expr> <Plug>(vimshell_another_delete_backward_char)
  96. \ <SID>delete_another_backward_char()
  97. inoremap <buffer><silent><expr> <Plug>(vimshell_delete_forward_line)
  98. \ col('.') == col('$') ? "" : "\<ESC>lDa"
  99. inoremap <buffer><silent> <Plug>(vimshell_clear)
  100. \ <ESC>:call <SID>clear(1)<CR>
  101. inoremap <buffer><silent> <Plug>(vimshell_execute_by_background)
  102. \ <ESC>:call <SID>execute_by_background(1)<CR>
  103. inoremap <buffer><silent> <Plug>(vimshell_exit)
  104. \ <ESC>:call <SID>exit()<CR>
  105. inoremap <buffer><silent> <Plug>(vimshell_hide)
  106. \ <ESC>:call <SID>hide()<CR>
  107. inoremap <expr><buffer><silent> <Plug>(vimshell_history_unite)
  108. \ unite#sources#vimshell_history#start_complete(!0)
  109. inoremap <expr><buffer><silent> <Plug>(vimshell_history_neocomplete)
  110. \ neocomplete#start_manual_complete('vimshell/history')
  111. "}}}
  112. if get(g:, 'vimshell_no_default_keymappings', 0)
  113. return
  114. endif
  115. " Normal mode key-mappings.
  116. " Execute command.
  117. nmap <buffer> <CR> <Plug>(vimshell_enter)
  118. " Hide vimshell.
  119. nmap <buffer> q <Plug>(vimshell_hide)
  120. " Exit vimshell.
  121. nmap <buffer> Q <Plug>(vimshell_exit)
  122. " Move to previous prompt.
  123. nmap <buffer> <C-p> <Plug>(vimshell_previous_prompt)
  124. " Move to next prompt.
  125. nmap <buffer> <C-n> <Plug>(vimshell_next_prompt)
  126. " Paste this prompt.
  127. nmap <buffer> <C-y> <Plug>(vimshell_paste_prompt)
  128. " Search end argument.
  129. nmap <buffer> E <Plug>(vimshell_move_end_argument)
  130. " Change line.
  131. nmap <buffer> cc <Plug>(vimshell_change_line)
  132. " Delete line.
  133. nmap <buffer> dd <Plug>(vimshell_delete_line)
  134. " Start insert.
  135. nmap <buffer> I <Plug>(vimshell_insert_head)
  136. nmap <buffer> A <Plug>(vimshell_append_end)
  137. nmap <buffer> i <Plug>(vimshell_insert_enter)
  138. nmap <buffer> a <Plug>(vimshell_append_enter)
  139. nmap <buffer> ^ <Plug>(vimshell_move_head)
  140. " Interrupt.
  141. nmap <buffer> <C-c> <Plug>(vimshell_interrupt)
  142. nmap <buffer> <C-k> <Plug>(vimshell_hangup)
  143. " Clear.
  144. nmap <buffer> <C-l> <Plug>(vimshell_clear)
  145. " Execute background.
  146. nmap <buffer> <C-z> <Plug>(vimshell_execute_by_background)
  147. " Insert mode key-mappings.
  148. " Execute command.
  149. inoremap <expr> <SID>(bs-ctrl-])
  150. \ getline('.')[col('.') - 2] ==# "\<C-]>" ? "\<BS>" : ''
  151. imap <buffer> <C-]> <C-]><SID>(bs-ctrl-])
  152. imap <buffer> <CR> <C-]><Plug>(vimshell_enter)
  153. " History completion.
  154. imap <buffer> <C-l> <Plug>(vimshell_history_unite)
  155. inoremap <buffer><expr> <C-p> pumvisible() ? "\<C-p>" :
  156. \ <SID>start_history_complete()
  157. inoremap <buffer><expr> <C-n> pumvisible() ? "\<C-n>" :
  158. \ <SID>start_history_complete()
  159. inoremap <buffer><expr> <Up> pumvisible() ? "\<C-p>" :
  160. \ <SID>start_history_complete()
  161. inoremap <buffer><expr> <Down> pumvisible() ? "\<C-n>" :
  162. \ <SID>start_history_complete()
  163. " Command completion.
  164. imap <buffer> <TAB> <Plug>(vimshell_command_complete)
  165. " Move to Beginning of command.
  166. imap <buffer> <C-a> <Plug>(vimshell_move_head)
  167. " Delete all entered characters in the current line.
  168. imap <buffer> <C-u> <Plug>(vimshell_delete_backward_line)
  169. " Delete previous word characters in the current line.
  170. imap <buffer> <C-w> <Plug>(vimshell_delete_backward_word)
  171. " Push current line to stack.
  172. imap <silent><buffer><expr> <C-z> vimshell#mappings#smart_map(
  173. \ "\<Plug>(vimshell_push_current_line)",
  174. \ "\<Plug>(vimshell_execute_by_background)")
  175. " Insert last word.
  176. imap <buffer> <C-t> <Plug>(vimshell_insert_last_word)
  177. " Interrupt.
  178. imap <buffer> <C-c> <Plug>(vimshell_interrupt)
  179. " Delete char.
  180. imap <buffer> <C-h> <Plug>(vimshell_delete_backward_char)
  181. imap <buffer> <BS> <Plug>(vimshell_delete_backward_char)
  182. " Delete line.
  183. imap <buffer> <C-k> <Plug>(vimshell_delete_forward_line)
  184. endfunction"}}}
  185. function! vimshell#mappings#smart_map(vimshell_map, execute_map)
  186. return empty(b:vimshell.continuation) ? a:vimshell_map : a:execute_map
  187. endfunction
  188. " VimShell key-mappings functions.
  189. function! s:push_current_line() "{{{
  190. " Check current line.
  191. if !vimshell#check_prompt()
  192. return
  193. endif
  194. call add(b:vimshell.commandline_stack, getline('.'))
  195. " Todo:
  196. " Print command line stack.
  197. " let stack = map(deepcopy(b:vimshell.commandline_stack),
  198. " \ 'vimshell#view#_get_prompt_command(v:val)')
  199. " call append('.', stack)
  200. " Set prompt line.
  201. call setline('.', vimshell#get_prompt())
  202. call vimshell#view#_simple_insert()
  203. endfunction"}}}
  204. function! s:push_and_execute(command) "{{{
  205. " Check current line.
  206. if !vimshell#check_prompt()
  207. return
  208. endif
  209. call add(b:vimshell.commandline_stack, getline('.'))
  210. " Set prompt line.
  211. call setline('.', vimshell#get_prompt() . a:command)
  212. call vimshell#mappings#execute_line(1)
  213. endfunction"}}}
  214. function! vimshell#mappings#execute_line(is_insert) "{{{
  215. let oldpos = getpos('.')
  216. let b:interactive.output_pos = getpos('.')
  217. if !empty(b:vimshell.continuation)
  218. if line('.') != line('$')
  219. " History execution.
  220. call vimshell#int_mappings#_paste_prompt()
  221. endif
  222. call vimshell#util#disable_auto_complete()
  223. call vimshell#interactive#execute_pty_inout(a:is_insert)
  224. try
  225. call vimshell#parser#execute_continuation(a:is_insert)
  226. catch
  227. " Error.
  228. let context = b:vimshell.continuation.context
  229. if v:exception !~# '^Vim\%((\a\+)\)\?:Interrupt'
  230. call vimshell#error_line(
  231. \ context.fd, v:exception . ' ' . v:throwpoint)
  232. endif
  233. let b:vimshell.continuation = {}
  234. call vimshell#print_prompt(context)
  235. call vimshell#start_insert(a:is_insert)
  236. endtry
  237. else
  238. if vimshell#check_prompt() && line('.') != line('$')
  239. " History execution.
  240. call vimshell#mappings#_paste_prompt()
  241. endif
  242. if line('.') == line('$')
  243. call s:execute_command_line(a:is_insert, oldpos)
  244. endif
  245. endif
  246. endfunction"}}}
  247. function! s:execute_command_line(is_insert, oldpos) "{{{
  248. " Get command line.
  249. let line = vimshell#view#_get_prompt_command()
  250. let context = {
  251. \ 'has_head_spaces' : line =~ '^\s\+',
  252. \ 'is_interactive' : 1,
  253. \ 'is_insert' : a:is_insert,
  254. \ 'fd' : { 'stdin' : '', 'stdout': '', 'stderr': ''},
  255. \}
  256. if line =~ '^\s*-\s*$'
  257. " Popd.
  258. call vimshell#helpers#execute_internal_command('cd', ['-'], {})
  259. elseif line =~ '^\s*$'
  260. " Call emptycmd filter.
  261. let line = vimshell#hook#call_filter('emptycmd', context, line)
  262. endif
  263. if line =~ '^\s*$\|^\s*-\s*$'
  264. call vimshell#print_prompt(context)
  265. call vimshell#start_insert(a:is_insert)
  266. return
  267. endif
  268. " Move to line end.
  269. call cursor(0, col('$'))
  270. try
  271. call vimshell#parser#check_script(line)
  272. catch /^Exception: Quote\|^Exception: Join to next line/
  273. call vimshell#print_secondary_prompt()
  274. call vimshell#start_insert(a:is_insert)
  275. return
  276. endtry
  277. if g:vimshell_enable_transient_user_prompt
  278. \ && vimshell#view#_check_user_prompt()
  279. " Delete previous user prompt.
  280. silent execute vimshell#view#_check_user_prompt().',-1 delete _'
  281. endif
  282. " Call preparse filter.
  283. let line = vimshell#hook#call_filter('preparse', context, line)
  284. " Save cmdline.
  285. let b:vimshell.cmdline = line
  286. try
  287. " Not append history if starts spaces or dups.
  288. if line !~ '^\s'
  289. call vimshell#history#append(line)
  290. endif
  291. let ret = vimshell#parser#eval_script(line, context)
  292. catch /File ".*" is not found./
  293. " Command not found.
  294. let oldline = line
  295. let line = vimshell#hook#call_filter('notfound', context, line)
  296. if line != '' && line !=# oldline
  297. " Retry.
  298. call setpos('.', a:oldpos)
  299. call vimshell#view#_set_prompt_command(line)
  300. return s:execute_command_line(a:is_insert, a:oldpos)
  301. endif
  302. " Error.
  303. call vimshell#error_line(
  304. \ context.fd, 'command not found: ' . matchstr(v:exception,
  305. \ 'File "\zs.*\ze" is not found.'))
  306. call vimshell#next_prompt(context, a:is_insert)
  307. call vimshell#start_insert(a:is_insert)
  308. return
  309. catch
  310. " Error.
  311. call vimshell#error_line(
  312. \ context.fd, v:exception . ' ' . v:throwpoint)
  313. call vimshell#next_prompt(context, a:is_insert)
  314. call vimshell#start_insert(a:is_insert)
  315. return
  316. endtry
  317. if ret == 0
  318. call vimshell#next_prompt(context, a:is_insert)
  319. endif
  320. call vimshell#start_insert(a:is_insert)
  321. endfunction"}}}
  322. function! s:previous_prompt() "{{{
  323. if empty(b:vimshell.continuation)
  324. call s:search_prompt('bWn')
  325. else
  326. let prompts = sort(filter(map(keys(b:interactive.prompt_history),
  327. \ 'str2nr(v:val)'), 'v:val < line(".")'))
  328. if !empty(prompts)
  329. call cursor(prompts[-1], len(vimshell#interactive#get_prompt()) + 1)
  330. endif
  331. endif
  332. endfunction"}}}
  333. function! s:next_prompt() "{{{
  334. if empty(b:vimshell.continuation)
  335. call s:search_prompt('Wn')
  336. else
  337. let prompts = sort(filter(map(keys(b:interactive.prompt_history),
  338. \ 'str2nr(v:val)'), 'v:val > line(".")'))
  339. if !empty(prompts)
  340. call cursor(prompts[0], len(vimshell#interactive#get_prompt()) + 1)
  341. endif
  342. endif
  343. endfunction"}}}
  344. function! s:search_prompt(flag) "{{{
  345. let col = col('.')
  346. call cursor(0, 1)
  347. let pos = searchpos(vimshell#get_context().prompt_pattern . '.\?', a:flag)
  348. if pos[0] != 0
  349. call cursor(pos[0], matchend(getline(pos[0]),
  350. \ vimshell#get_context().prompt_pattern . '.\?'))
  351. else
  352. call cursor(0, col)
  353. endif
  354. endfunction"}}}
  355. function! s:delete_previous_output() "{{{
  356. let prompt_pattern = vimshell#get_context().prompt_pattern
  357. let nprompt = vimshell#get_user_prompt() != '' ?
  358. \ '^\[%\] ' : prompt_pattern
  359. let pprompt = prompt_pattern
  360. " Search next prompt.
  361. if getline('.') =~ nprompt
  362. let next_line = line('.')
  363. elseif vimshell#get_user_prompt() != '' && getline('.') =~ prompt_pattern
  364. let next_line = searchpos(nprompt, 'bWn')[0]
  365. else
  366. let next_line = searchpos(nprompt, 'Wn')[0]
  367. endif
  368. while getline(next_line-1) =~ nprompt
  369. let next_line -= 1
  370. endwhile
  371. call cursor(0, 1)
  372. let prev_line = searchpos(pprompt, 'bWn')[0]
  373. if prev_line > 0 && next_line - prev_line > 1
  374. silent execute printf('%s,%sdelete', prev_line+1, next_line-1)
  375. call append(line('.')-1, "* Output was deleted *")
  376. endif
  377. call s:next_prompt()
  378. call vimshell#terminal#clear_highlight()
  379. endfunction"}}}
  380. function! s:insert_last_word() "{{{
  381. let word = ''
  382. let histories = vimshell#history#read()
  383. if !empty(histories)
  384. for w in reverse(split(histories[-1], '[^\\]\zs\s'))
  385. if w =~ '[[:alpha:]_/\\]\{2,}'
  386. let word = w
  387. break
  388. endif
  389. endfor
  390. endif
  391. call setline(line('.'), getline('.') . word)
  392. startinsert!
  393. endfunction"}}}
  394. function! vimshell#mappings#_paste_prompt() "{{{
  395. if !empty(b:vimshell.continuation)
  396. return vimshell#int_mappings#_paste_prompt()
  397. endif
  398. let prompt_pattern = vimshell#get_context().prompt_pattern
  399. if getline('.') !~# prompt_pattern
  400. return
  401. endif
  402. let command = getline('.')[vimshell#get_prompt_length(getline('.')) :]
  403. call cursor('$', 0)
  404. " Set prompt line.
  405. call vimshell#view#_set_prompt_command(command)
  406. endfunction"}}}
  407. function! s:move_head() "{{{
  408. if !vimshell#check_prompt()
  409. normal! ^
  410. return
  411. endif
  412. call cursor(0, vimshell#get_prompt_length() + 1)
  413. endfunction"}}}
  414. function! s:move_end_argument() "{{{
  415. let pos = searchpos('\\\@<!\s\zs[^[:space:]]*$', '', line('.'), 'n')
  416. call cursor(0, pos[1])
  417. endfunction"}}}
  418. function! s:delete_line() "{{{
  419. let col = col('.')
  420. let mcol = col('$')
  421. call setline(line('.'), vimshell#get_prompt() . getline('.')[col :])
  422. call s:move_head()
  423. if col == mcol-1
  424. startinsert!
  425. endif
  426. endfunction"}}}
  427. function! s:clear(is_insert) "{{{
  428. if vimshell#is_interactive()
  429. return vimshell#int_mappings#clear()
  430. endif
  431. let lines = split(vimshell#view#_get_prompt_command(), "\<NL>", 1)
  432. " Hangup current process.
  433. call s:hangup(a:is_insert)
  434. " Clean up the screen.
  435. % delete _
  436. call vimshell#terminal#clear_highlight()
  437. call vimshell#terminal#init()
  438. call vimshell#print_prompt()
  439. call vimshell#view#_set_prompt_command(lines[0])
  440. call append('$', map(lines[1:],
  441. \ string(vimshell#get_secondary_prompt()).'.v:val'))
  442. call vimshell#start_insert(a:is_insert)
  443. endfunction"}}}
  444. function! s:expand_wildcard() "{{{
  445. " Wildcard.
  446. if empty(vimshell#helpers#get_current_args())
  447. return ''
  448. endif
  449. let wildcard = vimshell#helpers#get_current_args()[-1]
  450. let expanded = vimproc#parser#expand_wildcard(wildcard)
  451. return (pumvisible() ? "\<C-e>" : '')
  452. \ . repeat("\<BS>", len(wildcard)) . join(expanded)
  453. endfunction"}}}
  454. function! s:hide() "{{{
  455. " Switch buffer.
  456. if winnr('$') != 1
  457. close
  458. else
  459. call vimshell#util#alternate_buffer()
  460. endif
  461. endfunction"}}}
  462. function! s:exit() "{{{
  463. let context = deepcopy(vimshell#get_context())
  464. call vimshell#interactive#quit_buffer()
  465. if context.tab
  466. tabclose
  467. endif
  468. endfunction"}}}
  469. function! s:delete_backward_char() "{{{
  470. if !pumvisible()
  471. let prefix = ''
  472. elseif vimshell#util#is_auto_select()
  473. let prefix = "\<C-e>"
  474. else
  475. let prefix = "\<C-y>"
  476. endif
  477. " Prevent backspace over prompt
  478. let cur_text = vimshell#get_cur_line()
  479. if cur_text !~# vimshell#get_context().prompt_pattern . '$'
  480. return prefix . "\<BS>"
  481. else
  482. return prefix
  483. endif
  484. endfunction"}}}
  485. function! s:delete_another_backward_char() "{{{
  486. return vimshell#get_cur_text() != '' ?
  487. \ s:delete_backward_char() :
  488. \ winnr('$') != 1 ?
  489. \ "\<ESC>:close\<CR>" :
  490. \ "\<ESC>:buffer #\<CR>"
  491. endfunction"}}}
  492. function! s:delete_backward_line() "{{{
  493. if !pumvisible()
  494. let prefix = ''
  495. elseif vimshell#util#is_auto_select()
  496. let prefix = "\<C-e>"
  497. else
  498. let prefix = "\<C-y>"
  499. endif
  500. let len = len(substitute(vimshell#get_cur_text(), '.', 'x', 'g'))
  501. return prefix . repeat("\<BS>", len)
  502. endfunction"}}}
  503. function! s:hangup(is_insert) "{{{
  504. if empty(b:vimshell.continuation)
  505. call vimshell#print_prompt()
  506. call vimshell#start_insert(a:is_insert)
  507. return
  508. endif
  509. " Kill process.
  510. call vimshell#interactive#hang_up(bufname('%'))
  511. let context = {
  512. \ 'has_head_spaces' : 0,
  513. \ 'is_interactive' : 1,
  514. \ 'is_insert' : a:is_insert,
  515. \ 'fd' : { 'stdin' : '', 'stdout' : '', 'stderr' : '' },
  516. \ }
  517. call vimshell#print_prompt(context)
  518. call vimshell#start_insert(a:is_insert)
  519. endfunction"}}}
  520. function! s:interrupt(is_insert) "{{{
  521. if empty(b:vimshell.continuation)
  522. call vimshell#print_prompt()
  523. call vimshell#start_insert(a:is_insert)
  524. return
  525. endif
  526. call vimshell#interactive#send_char(3)
  527. if !a:is_insert
  528. stopinsert
  529. endif
  530. endfunction"}}}
  531. function! s:insert_enter() "{{{
  532. if !vimshell#check_prompt() || !empty(b:vimshell.continuation)
  533. startinsert
  534. return
  535. endif
  536. if line('.') != line('$')
  537. " Paste prompt line.
  538. let save_col = col('.')
  539. call vimshell#mappings#_paste_prompt()
  540. call cursor('$', save_col)
  541. endif
  542. let prompt_len = vimshell#get_prompt_length()
  543. if col('.') <= prompt_len
  544. if prompt_len + 1 >= col('$')
  545. startinsert!
  546. return
  547. else
  548. call cursor(0, vimshell#get_prompt_length() + 1)
  549. endif
  550. endif
  551. startinsert
  552. endfunction"}}}
  553. function! s:insert_head() "{{{
  554. if !empty(b:vimshell.continuation)
  555. return vimshell#int_mappings#_insert_head()
  556. endif
  557. call cursor(0, 1)
  558. call s:insert_enter()
  559. endfunction"}}}
  560. function! s:append_enter() "{{{
  561. if vimshell#helpers#check_cursor_is_end()
  562. call s:append_end()
  563. else
  564. call cursor(0, col('.')+1)
  565. call s:insert_enter()
  566. endif
  567. endfunction"}}}
  568. function! s:append_end() "{{{
  569. call s:insert_enter()
  570. startinsert!
  571. endfunction"}}}
  572. function! s:execute_by_background(is_insert) "{{{
  573. if empty(b:vimshell.continuation)
  574. return
  575. endif
  576. let interactive = b:interactive
  577. let interactive.type = 'interactive'
  578. let context = b:vimshell.continuation.context
  579. let b:vimshell.continuation = {}
  580. let b:interactive = {
  581. \ 'type' : 'vimshell',
  582. \ 'syntax' : 'vimshell',
  583. \ 'process' : {},
  584. \ 'fd' : context.fd,
  585. \ 'encoding' : &encoding,
  586. \ 'is_pty' : 0,
  587. \ 'echoback_linenr' : -1,
  588. \ 'stdout_cache' : '',
  589. \ 'stderr_cache' : '',
  590. \ 'hook_functions_table' : {},
  591. \}
  592. let [new_pos, old_pos] = vimshell#helpers#split(g:vimshell_split_command)
  593. call vimshell#commands#iexe#init(context, interactive,
  594. \ new_pos, old_pos, a:is_insert)
  595. endfunction"}}}
  596. function! s:start_history_complete() "{{{
  597. return exists('*neocomplete#start_manual_complete') ?
  598. \ neocomplete#start_manual_complete('vimshell/history') :
  599. \ ''
  600. endfunction"}}}
  601. " vim: foldmethod=marker