aboutsummaryrefslogtreecommitdiff
path: root/plugin/cursorline_current.vim
blob: 64278cd9c89d724b077e93ca5375f26b1f5acad2 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"
" cursorline_current.vim: Set 'cursorline' and/or 'cursorcolumn' only in the
" current window, when not in insert mode, and/or when Vim has focus, with
" each of those being configurable with global variables that are checked at
" plugin load time.
"
" Author: Tom Ryder <tom@sanctum.geek.nz>
" License: Same as Vim itself
"
if exists('loaded_cursorline_current') || &compatible || v:version < 700
  finish
endif
let loaded_cursorline_current = 1

augroup cursorline_current
  autocmd!

  " If g:cursorline_current_line is true at plugin load time (defaults to on
  " if unset), set 'cursorline' to the user-configured window-global value on
  " entering a window, and unset it on leaving.
  "
  if get(g:, 'cursorline_current_line', 1)
    autocmd WinEnter *
          \ setlocal cursorline<
    autocmd WinLeave *
          \ setlocal nocursorline
  endif

  " Do the same for 'cursorcolumn' if g:cursorline_current_column is true at
  " load time, which again defaults to being on if unset.
  "
  if get(g:, 'cursorline_current_column', 1)
    autocmd WinEnter *
          \ setlocal cursorcolumn<
    autocmd WinLeave *
          \ setlocal nocursorcolumn
  endif

  " If g:cursorline_current_insert is set at plugin load time (defaults to on
  " if unset), also blank 'cursorline' even in the current window while in
  " insert mode.  Note that CTRL-C's default behaviour breaks this.
  "
  if get(g:, 'cursorline_current_insert', 1)
    autocmd InsertEnter *
          \ doautocmd cursorline_current WinLeave
    autocmd InsertLeave *
          \ doautocmd cursorline_current WinEnter
  endif

  " If g:cursorline_current_focus is set at plugin load time (defaults to on
  " if unset), also blank 'cursorline' even in the current window if Vim loses
  " focus.  This probably only works in the GUI.
  "
  if get(g:, 'cursorline_current_focus', 1)
    autocmd FocusGained *
          \ doautocmd cursorline_current WinEnter
    autocmd FocusLost *
          \ doautocmd cursorline_current WinLeave
  endif

  " Stack up BufEnter and BufLeave events to trigger the corresponding window
  " events, too; although this often means we set or unset the same option
  " twice, it correctly handles entering a new window for a buffer that had
  " already loaded.
  "
  autocmd BufEnter *
        \ doautocmd cursorline_current WinEnter
  autocmd BufLeave *
        \ doautocmd cursorline_current WinLeave

  " When Vim starts up, go through all of the windows in all of the tabs and
  " trigger the leave hooks to set 'cursorline' and/or 'cursorcolumn' locally
  " off, and then return to the first window of the first tab, and trigger the
  " enter hook to restore it to its user-configured window-global value again.
  " This is intended to correctly handle -o and -O options from the command
  " line, or vimrc files or plugins that open their own windows on Vim
  " startup.
  "
  autocmd VimEnter *
        \ tabdo windo doautocmd cursorline_current WinLeave
  autocmd VimEnter *
        \ tabfirst | 1 wincmd w | doautocmd WinEnter

  " If we just loaded a session, however, prevent those VimEnter hooks from
  " running by deleting them.  The session should have recorded values for
  " both options in each of the windows it saved, so we don't need to (and
  " shouldn't) mess with them.
  "
  autocmd SessionLoadPost *
        \ autocmd! cursorline_current VimEnter

augroup END