diff --git a/README.md b/README.md index 4a56484..3325a20 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,8 @@ -[日本語はこちら](https://github.com/t9md/vim-choosewin/blob/master/README-JP.md) - -# Animated GIF +# ChooseWin ![gif](https://raw.githubusercontent.com/t9md/t9md/1675510eaa1b789aeffbc49c1ae3b1e8e7dceabe/img/vim-choosewin.gif) -# Navigate to the window you choose +## Navigate to the window you choose This plugin aims to mimic tmux's `display-pane` feature, which enables you to choose a window interactively. @@ -60,20 +58,20 @@ nmap - (choosewin) ### Move around tabs, and choose windows -First of all, open multiple windows and tabs. -Invoke choosewin by typing `-` in normal mode. -Then you can move around tabs by `]` and `[`, or you cand choose the target tab directly by typing the number labeled in the tabline. -After you chose a target tab, you can choose a target window by typing the letter which is labeled in the statusline and in the middle of each window (if you have enabled the overlay feature). +First of all, open multiple windows and tabs. +Invoke choosewin by typing `-` in normal mode. +Then you can move around tabs by `]` and `[`, or you cand choose the target tab directly by typing the number labeled in the tabline. +After you chose a target tab, you can choose a target window by typing the letter which is labeled in the statusline and in the middle of each window (if you have enabled the overlay feature). ### Choose the previous window -Type `-` again to invoke choosewin, then input `-` again to choose the previous window. The previous window you were on before you choose the current window. +Type `-` again to invoke choosewin, then input `-` again to choose the previous window. The previous window you were on before you choose the current window. ### Swap windows -Type `-` to invoke choosewin, then type `s` to swap windows. -Then type the label of a window to swap content(=buffer) of that window with your current window. -After you chose, the current window's buffer is swapped with the buffer shown in the window you chose. +Type `-` to invoke choosewin, then type `s` to swap windows. +Then type the label of a window to swap content(=buffer) of that window with your current window. +After you chose, the current window's buffer is swapped with the buffer shown in the window you chose. By combining "swap" and "previous window" features, you can easily swap any window with the previous window like so: `-s-`, invoking choosewin itself(`-`) then entering swapping mode(`s`), then instructing choosewin to swap the target window with the previous(`-`) window. Congratulations! ### NERDTree open file diff --git a/autoload/choosewin.vim b/autoload/choosewin.vim index ca4d21c..69a81c2 100644 --- a/autoload/choosewin.vim +++ b/autoload/choosewin.vim @@ -55,7 +55,7 @@ function! s:cw.start(wins, ...) "{{{1 call self.choose() catch /\v^(CHOSE \d+)$/ - if self.conf['noop'] + if self.conf['noop'] let tab = tabpagenr() let win = str2nr(matchstr(v:exception, '\v^CHOSE \zs\d+')) if tab isnot self.src.tab @@ -88,7 +88,6 @@ function! s:cw.start(wins, ...) "{{{1 endfunction function! s:cw.init(wins, conf) "{{{1 - call choosewin#color#init() let self.wins = s:wins.set(a:wins) let self.conf = extend(choosewin#config#get(), a:conf) let self.action = choosewin#action#init(self) @@ -96,6 +95,9 @@ function! s:cw.init(wins, conf) "{{{1 let self.tab_options = {} let self.statusline = {} let self.src = {'win': winnr(), 'tab': tabpagenr()} + if self.conf.colorscheme_enable + lua require("choosewin.color").set() + endif endfunction function! s:cw.setup() "{{{1 diff --git a/autoload/choosewin/color.vim b/autoload/choosewin/color.vim deleted file mode 100644 index e873759..0000000 --- a/autoload/choosewin/color.vim +++ /dev/null @@ -1,37 +0,0 @@ -let s:Color = {} - -function! s:Color.init() "{{{1 - if has_key(self, 'mgr') - return - endif - - let config = choosewin#config#get() - let self.mgr = choosewin#hlmanager#new('') - - let colors = { - \ "Label": config['color_label'], - \ "LabelCurrent": config['color_label_current'], - \ "Overlay": config['color_overlay'], - \ "OverlayCurrent": config['color_overlay_current'], - \ "Shade": config['color_shade'], - \ "Land": config['color_land'], - \ "Other": config[ config.label_fill ? "color_label" : "color_other" ], - \ } - for [color, spec] in items(colors) - call self.mgr.register("ChooseWin" . color, spec) - endfor -endfunction -"}}} - -" API: -function! choosewin#color#init() "{{{1 - return s:Color.init() -endfunction - -function! choosewin#color#refresh() "{{{1 - call s:Color.init() - call s:Color.mgr.refresh() -endfunction -"}}} - -" vim: foldmethod=marker diff --git a/autoload/choosewin/config.vim b/autoload/choosewin/config.vim index 95448cd..498a6d7 100644 --- a/autoload/choosewin/config.vim +++ b/autoload/choosewin/config.vim @@ -21,6 +21,7 @@ let s:default = { \ 'active': 0, \ 'debug': 0, \ 'label_fill': 0, + \ 'colorscheme_enable': 1, \ 'color_label': { 'gui': ['DarkGreen', 'white', 'bold'], 'cterm': [ 22, 15,'bold'] }, \ 'color_label_current': { 'gui': ['LimeGreen', 'black', 'bold'], 'cterm': [ 40, 16, 'bold'] }, \ 'color_overlay': { 'gui': ['DarkGreen', 'DarkGreen' ], 'cterm': [ 22, 22 ] }, diff --git a/autoload/choosewin/hlmanager.vim b/autoload/choosewin/hlmanager.vim deleted file mode 100644 index 6dea8d1..0000000 --- a/autoload/choosewin/hlmanager.vim +++ /dev/null @@ -1,194 +0,0 @@ -" Util: -function! s:define_type_checker() "{{{1 - " dynamically define s:is_Number(v) etc.. - let types = { - \ "Number": 0, - \ "String": 1, - \ "Funcref": 2, - \ "List": 3, - \ "Dictionary": 4, - \ "Float": 5, - \ } - - for [type, number] in items(types) - let s = '' - let s .= 'function! s:is_' . type . '(v)' . "\n" - let s .= ' return type(a:v) is ' . number . "\n" - let s .= 'endfunction' . "\n" - execute s - endfor -endfunction -"}}} -call s:define_type_checker() -unlet! s:define_type_checker - -" Copied from vim-airline's airline#init#gui_mode function -let s:SCREEN = (has('gui_running') || - \ (has('termtruecolor') && &guicolors == 1) || - \ (has('termguicolors') && &termguicolors == 1) || - \ (has('nvim') && exists('$NVIM_TUI_ENABLE_TRUE_COLOR') - \ && !exists('+termguicolors'))) ? 'gui' : 'cterm' - -" Main: -let s:hlmgr = {} - -function! s:hlmgr.new(prefix) "{{{1 - let R = deepcopy(self) - call R.init(a:prefix) - return R -endfunction - -function! s:hlmgr.init(prefix) "{{{1 - let self._colors = {} - let self._specs = {} - let self.prefix = a:prefix - return self -endfunction - -function! s:hlmgr.register_auto(spec) "{{{1 - if s:is_String(a:spec) - return a:spec - endif - - if s:is_Dictionary(a:spec) - let spec = string(a:spec[s:SCREEN]) - let name = get(self._specs, spec, '') - if !empty(name) - " If color is already defined for spec provided. - " return that color - return name - endif - endif - - return self.register(self.color_next(), a:spec) -endfunction - -function! s:hlmgr.register(name, spec) "{{{1 - call self.define(a:name, a:spec) - let self._colors[a:name] = a:spec - - " This might overwrite existing spec but its OK since _specs is simple color - " re-using mechanizm for performance. - let self._specs[string(a:spec[s:SCREEN])] = a:name - return a:name -endfunction - -function! s:hlmgr.define(name, color) "{{{1 - let command = printf('highlight %s %s', a:name, self.hl_defstr(a:color)) - silent execute command -endfunction - -function! s:hlmgr.refresh() "{{{1 - for [name, color] in items(self._colors) - call self.define(name, color) - endfor -endfunction - -function! s:hlmgr.color_next() "{{{1 - return printf(self.prefix . '%05d', len(self._colors)) -endfunction - -function! s:hlmgr.reset() "{{{1 - call self.clear() - call self.init(self.prefix) -endfunction - -function! s:hlmgr.spec_for(color) "{{{1 - return get(self._colors, a:color, '') -endfunction - -function! s:hlmgr.parse(spec, ...) "{{{1 - " return dictionary from string - " 'guifg=#25292c guibg=#afb0ae' => {'gui': ['#afb0ae', '#25292c']} - let R = {} - if empty(a:spec) - return R - endif - - let screens = empty(a:000) ? [s:SCREEN] : ['gui', 'cterm' ] - for screen in screens - let R[screen] = ['',''] - for def in split(a:spec) - let [key,val] = split(def, '=') - if key ==# screen . 'bg' | let R[screen][0] = val - elseif key ==# screen . 'fg' | let R[screen][1] = val - elseif key ==# screen | call add(R[screen], val) - endif - endfor - endfor - return R -endfunction - -function! s:hlmgr.parse_full(spec) "{{{1 - return self.parse(a:spec, 1) -endfunction - -function! s:hlmgr.capture(hlname) "{{{1 - let hlname = a:hlname - - if empty(hlID(hlname)) - " if hl not exists, return empty string - return '' - endif - - redir => HL_SAVE - execute 'silent! highlight ' . hlname - redir END - if !empty(matchstr(HL_SAVE, 'xxx cleared$')) - return '' - endif - - " follow highlight link - let link = matchstr(HL_SAVE, 'xxx links to \zs.*') - if !empty(link) - return self.capture(link) - endif - - return matchstr(HL_SAVE, 'xxx \zs.*') -endfunction - -function! s:hlmgr.clear() "{{{1 - for color in self.colors() - silent execute 'highlight clear' color - endfor -endfunction - -function! s:hlmgr.colors() "{{{1 - return keys(self._colors) -endfunction - -function! s:hlmgr.hl_defstr(color) "{{{1 - " return 'guibg=DarkGreen gui=bold' (Type: String) - let color = a:color[s:SCREEN] - let R = [] - "[NOTE] empty() is not appropriate, cterm color is specified with number - for [idx, s] in [[ 0, 'bg' ], [ 1, 'fg' ] ,[ 2, ''] ] - let c = get(color, idx, -1) - if (s:is_String(c) && empty(c)) || (s:is_Number(c) && c ==# -1) - continue - endif - call add(R, printf('%s%s=%s', s:SCREEN, s, color[idx])) - endfor - return join(R) -endfunction - -function! s:hlmgr.convert(hlname) "{{{1 - return self.parse(self.capture(a:hlname)) -endfunction - -function! s:hlmgr.convert_full(hlname) "{{{1 - return self.parse_full(self.capture(a:hlname)) -endfunction - -function! s:hlmgr.dump() "{{{1 - return PP(self) -endfunction -"}}} - -" API: -function! choosewin#hlmanager#new(prefix) "{{{1 - return s:hlmgr.new(a:prefix) -endfunction -"}}} - -" vim: foldmethod=marker diff --git a/doc/choosewin.txt b/doc/choosewin.txt index 8891d74..9ce37e8 100644 --- a/doc/choosewin.txt +++ b/doc/choosewin.txt @@ -65,6 +65,14 @@ All config options in this section are set in 'autoload/choosewin/config.vim'. Type: |Number| Control whether the window label is shown in the |&statusline|. +*g:choosewin_colorscheme_enable* + Default: 1 + Type: |Number| 0 or 1 + Control whether this plugin should setup its highlight groups. + If you set it to 0, you can use `choosewin.color.enable` to enable the + default highlight groups. + + *g:choosewin_color_label* Default: See 'autoload/choosewin/config.vim' Type: |Dictionary| @@ -431,7 +439,7 @@ CHANGELOG *choosewin-changelog* - [improve] Rewrite, cleanup overall program. - [enhance] add tab_close action with default keymap 'x'. 2015-01-25: - - [enhance] now swap don't stay same window, original swap is now + - [enhance] now swap don't stay same window, original swap is now swap_stay. 2015-01-20: v1.5 - verup again because of document mistake. diff --git a/lua/choosewin/color.lua b/lua/choosewin/color.lua new file mode 100644 index 0000000..8bd9e65 --- /dev/null +++ b/lua/choosewin/color.lua @@ -0,0 +1,46 @@ +local M = {} + +local function viml_spec_to_lua_spec(viml_spec) + return { + fg = viml_spec.gui[2], + bg = viml_spec.gui[1], + ctermfg = viml_spec.cterm[2], + ctermbg = viml_spec.cterm[1], + bold = viml_spec.gui[3] and viml_spec.gui[3]:find("bold") ~= nil, + underline = viml_spec.gui[3] and viml_spec.gui[3]:find("underline") ~= nil, + } +end + +-- Returns the highlight groups for Choosewin. +local function get_colors() + local config = vim.fn["choosewin#config#get"]() + local colors = { + ChooseWinLabel = config["color_label"], + ChooseWinLabelCurrent = config["color_label_current"], + ChooseWinOverlay = config["color_overlay"], + ChooseWinOverlayCurrent = config["color_overlay_current"], + ChooseWinShade = config["color_shade"], + ChooseWinLand = config["color_land"], + ChooseWinOther = config[config.label_fill and "color_label" or "color_other"], + } + for color, spec in pairs(colors) do + colors[color] = viml_spec_to_lua_spec(spec) + end + return colors +end + +-- Sets highlight groups. +function M.set() + for color, spec in pairs(get_colors()) do + vim.api.nvim_set_hl(0, color, spec) + end +end + +-- Clears highlight groups. +function M.clear() + for color, _ in pairs(get_colors()) do + vim.api.nvim_set_hl(0, color, {}) + end +end + +return M diff --git a/plugin/choosewin.vim b/plugin/choosewin.vim index c6de91d..5d64ad7 100644 --- a/plugin/choosewin.vim +++ b/plugin/choosewin.vim @@ -9,12 +9,6 @@ let g:loaded_choosewin = 1 let s:old_cpo = &cpo set cpo&vim -" Main: -augroup plugin-choosewin - autocmd! - autocmd ColorScheme,SessionLoadPost * call choosewin#color#refresh() -augroup END - " KeyMap: nnoremap (choosewin) \ :call choosewin#start(range(1, winnr('$')))