summaryrefslogtreecommitdiffstats
path: root/autoload/focau/events.vim
blob: acc9293f27d6e41f04b4ec0120fe4dbb653feeea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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