Check changes for focus event, improves #638

This commit is contained in:
Adam Stankiewicz
2020-12-30 00:48:12 +01:00
parent 2fcb15afa0
commit 44e79fc935
4 changed files with 169 additions and 1 deletions

55
autoload/focau/cursor.vim Normal file
View File

@@ -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

64
autoload/focau/events.vim Normal file
View File

@@ -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

33
autoload/focau/init.vim Normal file
View File

@@ -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

View File

@@ -13,6 +13,22 @@ if exists('g:loaded_sensible')
finish
endif
if !has('gui_running') && (!has('nvim') || $TERM =~? '^rxvt')
let g:focau = extend({
\ 'auto': 1,
\ 'active': 1,
\ 'events' : {'<F25>': "\e[I", '<F26>': "\e[O"},
\ 'focuses': ["\e[?1004h", "\e[?1004l"],
\ 'screens': ["\e[?1049h", "\e[?1049l"],
\ 'cursors': ['', '', ''],
\ 'colors' : ['white', 'cyan'],
\ 'widgets': [],
\ 'clipregs':['+"p', '"+'],
\}, get(g:, 'focau', {}))
call focau#init#main()
endif
" Code taken from https://github.com/tpope/vim-sensible
" and (mostly comments) from https://github.com/sheerun/vimrc
"
@@ -74,7 +90,7 @@ if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'sensible') == -
if match(capture, 'checktime') == -1
augroup polyglot-sensible
au!
au CursorHold * silent! checktime
au CursorHold,FocusGained * silent! checktime
augroup END
endif