diff --git a/autoload/SpaceVim/layers/shell.vim b/autoload/SpaceVim/layers/shell.vim index ae9ad77a1..9da6a50f3 100644 --- a/autoload/SpaceVim/layers/shell.vim +++ b/autoload/SpaceVim/layers/shell.vim @@ -11,9 +11,16 @@ " @parentsection layers " SpaceVim uses deol.nvim for shell support in neovim and uses vimshell for " vim. For more info, read |deol| and |vimshell|. -" @subsection variable -" default_shell " +" @subsection variable +" +" default_shell: config the default shell to be used by shell layer. +" +" @subsection key bindings +" > +" SPC ' Open or switch to terminal windows +" q Hide terminal windows in normal mode +" < let s:SYSTEM = SpaceVim#api#import('system') @@ -60,8 +67,7 @@ endfunction " FIXME: func! SpaceVim#layers#shell#terminal() abort let line = getline('$') - let pwd = getcwd() - if line ==# pwd . '>' + if isdirectory(line[:-2]) return "exit\" endif return "\" @@ -70,7 +76,6 @@ func! SpaceVim#layers#shell#ctrl_u() abort let line = getline('$') let prompt = getcwd() . '>' return repeat("\", len(line) - len(prompt) + 2) - return "\" endfunction func! SpaceVim#layers#shell#ctrl_w() abort @@ -84,11 +89,16 @@ endfunction let s:default_shell = 'terminal' let s:default_position = 'top' let s:default_height = 30 +" the shell should be cached base on the root of a project, cache the terminal +" buffer id in: s:shell_cached_br +let s:enable_project_shell = 1 +let s:shell_cached_br = {} function! SpaceVim#layers#shell#set_variable(var) abort let s:default_shell = get(a:var, 'default_shell', 'terminal') let s:default_position = get(a:var, 'default_position', 'top') let s:default_height = get(a:var, 'default_height', 30) + let s:enable_project_shell = get(a:var, 'enable_project_shell', 1) endfunction function! SpaceVim#layers#shell#get_options() abort @@ -97,15 +107,26 @@ function! SpaceVim#layers#shell#get_options() abort endfunction -let s:shell_win_nr = 0 +let s:shell_win_nr = -1 +let s:term_buf_nr = -1 +" shell windows shoud be toggleable, and can be hide. function! s:open_default_shell() abort if s:shell_win_nr != 0 && getwinvar(s:shell_win_nr, '&buftype') ==# 'terminal' && &buftype !=# 'terminal' exe s:shell_win_nr . 'wincmd w' - startinsert + " fuck gvim bug, startinsert do not work in gvim + if has('nvim') + startinsert + else + normal! a + endif return endif if &buftype ==# 'terminal' - bwipeout! % + if has('nvim') + startinsert + else + normal! a + endif return endif let cmd = s:default_position ==# 'top' ? @@ -119,6 +140,16 @@ function! s:open_default_shell() abort if lines < winheight(0) && (s:default_position ==# 'top' || s:default_position ==# 'bottom') exe 'resize ' . lines endif + if bufexists(s:term_buf_nr) + exe 'silent b' . s:term_buf_nr + " clear the message + if has('nvim') + startinsert + else + normal! a + endif + return + endif if s:default_shell ==# 'terminal' if exists(':terminal') if has('nvim') @@ -134,6 +165,8 @@ function! s:open_default_shell() abort stopinsert startinsert endif + let s:term_buf_nr = bufnr('%') + call extend(s:shell_cached_br, {getcwd() : s:term_buf_nr}) else if s:SYSTEM.isWindows let shell = empty($SHELL) ? 'cmd.exe' : $SHELL @@ -144,11 +177,11 @@ function! s:open_default_shell() abort endif let s:shell_win_nr = winnr() let w:shell_layer_win = 1 - setlocal nobuflisted - " use q to close terminal buffer in vim, if vimcompatible mode is not + setlocal nobuflisted nonumber norelativenumber + " use q to hide terminal buffer in vim, if vimcompatible mode is not " enabled, and smart quit is on. if g:spacevim_windows_smartclose == 0 && !g:spacevim_vimcompatible - nnoremap q :bd! + nnoremap q :hide endif startinsert else @@ -159,3 +192,9 @@ function! s:open_default_shell() abort imap exit(vimshell_enter) endif endfunction + +function! SpaceVim#layers#shell#close_terminal() + if bufexists(s:term_buf_nr) + exe 'silent bd!' . s:term_buf_nr + endif +endfunction diff --git a/config/plugins/vimfiler.vim b/config/plugins/vimfiler.vim index 1a02a9bf8..93e9447a8 100644 --- a/config/plugins/vimfiler.vim +++ b/config/plugins/vimfiler.vim @@ -23,10 +23,10 @@ let g:vimfiler_ignore_pattern = get(g:, 'vimfiler_ignore_pattern', [ \]) if has('mac') - let g:vimfiler_quick_look_command = - \ get(g:, 'vimfiler_quick_look_command', 'qlmanage -p') + let g:vimfiler_quick_look_command = + \ get(g:, 'vimfiler_quick_look_command', 'qlmanage -p') else - let g:vimfiler_quick_look_command = + let g:vimfiler_quick_look_command = \ get(g:, 'vimfiler_quick_look_command', 'gloobus-preview') endif @@ -65,8 +65,16 @@ augroup vfinit au! autocmd FileType vimfiler call s:vimfilerinit() autocmd BufEnter * nested if (!has('vim_starting') && winnr('$') == 1 && &filetype ==# 'vimfiler') | - \ q | endif + \ call s:close_last_vimfiler_windows() | endif augroup END + +" in this function, we should check if shell terminal still exists, +" then close the terminal job before close vimfiler +function! s:close_last_vimfiler_windows() abort + call SpaceVim#layers#shell#close_terminal() + q +endfunction + function! s:vimfilerinit() setl nonumber setl norelativenumber diff --git a/doc/SpaceVim.txt b/doc/SpaceVim.txt index 5cb8f5869..394200463 100644 --- a/doc/SpaceVim.txt +++ b/doc/SpaceVim.txt @@ -1324,9 +1324,17 @@ SHELL *SpaceVim-layer-shell* SpaceVim uses deol.nvim for shell support in neovim and uses vimshell for vim. For more info, read |deol| and |vimshell|. -VARIABLE -default_shell +VARIABLE + +default_shell: config the default shell to be used by shell layer. + +KEY BINDINGS + +> + SPC ' Open or switch to terminal windows + q Hide terminal windows in normal mode +< ============================================================================== TMUX *SpaceVim-layer-tmux* diff --git a/docs/cn/layers/shell.md b/docs/cn/layers/shell.md index cd68e3aef..813b9ed18 100644 --- a/docs/cn/layers/shell.md +++ b/docs/cn/layers/shell.md @@ -61,7 +61,7 @@ The default shell is quickly accessible via a the default shortcut key `SPC '`. | -------- | ---------------------------------- | | `SPC '` | 打开或跳至已打开的终端窗口 | | `Ctrl-d` | 输入模式下关闭终端窗口 | -| `q` | Normal 模式下关闭终端窗口 | +| `q` | Normal 模式下隐藏终端窗口 | | `` | 从 Terminal 模式切换到 Normal 模式 | | `Ctrl-h` | 切换到左侧窗口 | | `Ctrl-j` | 切换到上方窗口 | diff --git a/docs/layers/shell.md b/docs/layers/shell.md index edf9b914c..dd4f90fb7 100644 --- a/docs/layers/shell.md +++ b/docs/layers/shell.md @@ -62,7 +62,7 @@ in percents with the variable `default_height`. Default value is 30. | ----------- | ---------------------------------------- | | `SPC '` | Open or switch to the terminal windows | | `Ctrl-d` | Close terminal windows in terminal mode | -| `q` | Close terminal windows in Normal mode | +| `q` | Hide terminal windows in Normal mode | | `` | Switch to Normal mode from terminal mode | | `Ctrl-h` | Switch to the windows on the left | | `Ctrl-j` | Switch to the windows below |