diff options
author | Adam Stankiewicz <sheerun@sher.pl> | 2020-12-30 00:48:12 +0100 |
---|---|---|
committer | Adam Stankiewicz <sheerun@sher.pl> | 2020-12-30 00:48:12 +0100 |
commit | 44e79fc935004c2e67fc39a77266d0f06e29d920 (patch) | |
tree | 8e4bd8aac7de50604d36acaff139df866c87a9cd /autoload | |
parent | 2fcb15afa09c6c5b53a3f37404a28b57a3f43d83 (diff) | |
download | vim-polyglot-44e79fc935004c2e67fc39a77266d0f06e29d920.tar.gz vim-polyglot-44e79fc935004c2e67fc39a77266d0f06e29d920.zip |
Check changes for focus event, improves #638
Diffstat (limited to 'autoload')
-rw-r--r-- | autoload/focau/cursor.vim | 55 | ||||
-rw-r--r-- | autoload/focau/events.vim | 64 | ||||
-rw-r--r-- | autoload/focau/init.vim | 33 |
3 files changed, 152 insertions, 0 deletions
diff --git a/autoload/focau/cursor.vim b/autoload/focau/cursor.vim new file mode 100644 index 00000000..d3da5c49 --- /dev/null +++ b/autoload/focau/cursor.vim @@ -0,0 +1,55 @@ +function! focau#cursor#auto_shape() + if $TERM =~? '\v^%(dumb|linux)' + " echom "Shape escape codes: can't autodetect for $TERM=" . $TERM + return ['', '', ''] + elseif $TERM =~? '^konsole' || exists('$ITERM_PROFILE') + return ["\e]50;CursorShape=0\x7", "\e]50;CursorShape=1\x7", "\e]50;CursorShape=2\x7"] + endif + + "" DISABLED: to reduce startup time, and it will not work through ssh + " let uver = eval(substitute(system('urxvt -h 2>&1'), + " \ '\v.*v(\d+)\.(\d+).*', '\1+\2.0/100', '')) + " return ["\e[2 q", (l:uver<9.21? "\e[4 q": "\e[6 q"), "\e[4 q"] + " let l:uver = substitute(split(system('urxvt -help 2>&1'), '\n')[0], + " \ '.*v\([0-9.]\+\).*', '\1', '') + " return ["\e[2 q", (9.21 <= l:uver ? "\e[6 q" : "\e[4 q")] + + " [1,2] -> [blinking,solid] block + " [3,4] -> [blinking,solid] underscore + " [5,6] -> [blinking,solid] vbar/I-beam (only in xterm > 282), + " urxvt got I-beam only in v9.21 2014-12-31, build from recent git. + + " if $TERM =~? '\v^%(xterm|rxvt|st|screen|tmux|nvim)' + " NOTE:(old xterm) ["\e[2 q", "\e[6 q", ''] + return ["\e[2 q", "\e[6 q", "\e[4 q"] +endfunction + + +function! focau#cursor#auto_color(idx) + " SEE: http://stackoverflow.com/questions/11494029/how-do-i-change-the-color-of-current-cursor-position-indicator + if !g:focau.auto + let colors = g:focau.colors + elseif $TERM =~? '\v^%(xterm|st|screen|tmux|rxvt)' + "" ALT: ["\e]12;white\x9c", "\e]12;orange\x9c"] + " use default \003]12;gray\007 for gnome-terminal + let colors = [ "\e]12;". g:focau.colors[0] ."\x7", + \ "\e]12;". g:focau.colors[1] ."\x7" ] + else + " echom "Err: can't detect esc codes for cursor colors in $TERM=".$TERM + let colors = ['', ''] + endif + return l:colors[a:idx] +endfunction + + +function! focau#cursor#shape_preserve() + "" Preserve previous cursor state and restore upon exit + let s:old_SI=&t_SI | let s:old_EI=&t_EI | if exists('&t_SR') + \| let s:old_RS=&t_SR | endif + " BUG: has no effect on restoring color after exit. + "" There are sequence to change color, but not the one to restore to default + " SEE Maybe save/restore the screen -- works for cursor? -- seems NO. + au focau VimLeave * let &t_SI = s:old_SI | let &t_EI = s:old_EI + \| if exists('&t_SR') | let &t_SR = s:old_RS | endif +endfunction + diff --git a/autoload/focau/events.vim b/autoload/focau/events.vim new file mode 100644 index 00000000..acc9293f --- /dev/null +++ b/autoload/focau/events.vim @@ -0,0 +1,64 @@ +"" WARNING: For focus-events to work in tmux, you need to set this option +" inside your tmux.conf: set -g focus-events on +" WARNING: must be outside this 'if' in s:focau_modes_enable, as it will not +" work in tmux on_disable! + +if exists('$TMUX') || $TERM =~? 'screen\|tmux' " FIXED for [tmux -> ssh | vim] + " Disable bkgd color erase and don't truncate highlighting + " So highlighted line does go all the way across screen + set t_ut= + function! s:wrap(s) + return "\ePtmux;". substitute(escape(a:s, ' '), + \ "\e\\|\<Esc>", "\e\e", 'g') ."\e\\" + endfunction +else + function! s:wrap(s) + return escape(a:s, ' ') + endfunction +endif + + +function! focau#events#auto_choose() + " if $TERM =~? '\v^%(dumb|linux)' + " echom "Err: your terminal don't support focus events $TERM=" . $TERM + " echom "Err: Can't auto-derive termfocus codes for your $TERM=" . $TERM + " echom "Disabled. If you disagree, set escape codes by yourself:)" + if $TERM =~? '^rxvt' + return ["\e]777;focus;on\x7", "\e]777;focus;off\x7"] + endif + " if $TERM =~? '\v^%(xterm|st|screen|tmux)' || exists('$ITERM_PROFILE') + return ["\e[?1004h", "\e[?1004l"] +endfunction + + +" ALT: +" augroup focau_focus +" autocmd! +" if a:state +" au VimEnter * call s:focau_cso('on') +" au VimLeavePre * call s:focau_cso('off') +" endif +" augroup END +function! focau#events#enable(state) + let g:focau.active = a:state + if !g:focau.active | let &t_ti = '' | let &t_te = '' | return | endif + " CHECK: Is it necessary to be able toggle dynamically? + " exec 'silent !echo -ne "'. g:focau.focuses[1] .g:focau.screens[1] .'"' + + "" FIX: add two color groups based on language, not permanent. See xkb. + let color = focau#cursor#auto_color(0) + let codes = ['t_EI', 't_SI', 't_SR'] + " Install insert mode autohooks -- on enter/leave + for i in range(3)| if exists('&'.codes[i]) + exe 'set '.codes[i].'='.s:wrap(g:focau.cursors[i] . l:color) + endif | endfor + + let on_init = &t_EI . s:wrap(g:focau.focuses[0]) . g:focau.screens[0] + let on_exit = s:wrap(g:focau.focuses[1]) . g:focau.screens[1] + + " Install focus autohooks -- on startup/shutdown + let &t_ti = on_init | let &t_te = on_exit + + " CHECK: Is it necessary to be able toggle dynamically? + " exec 'silent !echo -ne "'.&t_EI.g:focau.focuses[0].g:focau.screens[0].'"' +endfunction diff --git a/autoload/focau/init.vim b/autoload/focau/init.vim new file mode 100644 index 00000000..7c9320ec --- /dev/null +++ b/autoload/focau/init.vim @@ -0,0 +1,33 @@ +" WARNING: seems like we can't use <expr> maps, because we need side-effects +" ALT: {s: <C-g>...<C-g>, o: <Esc>, i: <C-o>, c: <C-\>e} +function! s:map_triggers(e) + for [a, i] in items({'FocusGained': 0, 'FocusLost': 1}) + let key = keys(a:e)[i] | exe 'set '.key.'='.a:e[key] + exe "fun! s:F".i."()\nif g:focau.active|sil! doau ".a."|en|return''\nendf" + for [ms, prf] in items({'nv': '@=', 'o': ':call ', 'ic': '<C-r>='}) + for m in split(ms, '\zs') + exe m.'noremap <silent> '.key.' '.prf.'<SID>F'.i.'()<CR>' + endfor | endfor | endfor +endfunction + + +function! focau#init#main() + augroup focau + autocmd! + augroup END + + call focau#cursor#shape_preserve() + + if g:focau.auto + let g:focau.focuses = focau#events#auto_choose() + let g:focau.cursors = focau#cursor#auto_shape() + "" FIX: must be dynamic and check curr lang before each mode/lang-switch + " call s:focau_lang_choose() "primary/secondary + endif + + " Wrap choosen keys in event triggers + call s:map_triggers(g:focau.events) + " Start + call focau#events#enable(g:focau.active) + au focau VimEnter * if g:focau.active | sil! doau FocusGained | endif +endfunction |