├── README.md └── plugin └── airline-colornum.vim /README.md: -------------------------------------------------------------------------------- 1 | #Airline Color Num Plugin 2 | 3 | Sets the cursor line number to the same color as the current mode in the 4 | statusline set by the [Vim Airline](https://github.com/bling/vim-airline) plugin. 5 | 6 | ##Screenshots 7 | ![Normal Example](http://i.imgur.com/zU0MWWV.png) 8 | ![Insert Example](http://i.imgur.com/dxXZVLy.png) 9 | ![Visual Example](http://i.imgur.com/JqdpYFB.png) 10 | 11 | ##Installation 12 | There are a few ways you can go about installing this plugin: 13 | 14 | 1. If you have [Vundle](https://github.com/gmarik/Vundle.vim) you can simply add: 15 | ``` 16 | Bundle 'ntpeters/vim-airline-colornum' 17 | ``` 18 | to your `.vimrc` file then run: 19 | ``` 20 | :BundleInstall 21 | ``` 22 | 2. If you are using [Pathogen](https://github.com/tpope/vim-pathogen), you can just run the following command: 23 | ``` 24 | git clone git://github.com/ntpeters/vim-airline-colornum.git ~/.vim/bundle/vim-airline-colornum 25 | ``` 26 | 3. While this plugin can also be installed by copying its contents into your `~/.vim/` directory, I would highly recommend using one of the above methods as they make managing your Vim plugins painless. 27 | 28 | ##Usage 29 | This plugin is enabled by default. 30 | 31 | * To disable this plugin in your `.vimrc`: 32 | ``` 33 | let g:airline_colornum_enabled = 0 34 | ``` 35 | 36 | * To disable this plugin temporarily: 37 | ``` 38 | :DisableAirlineColorNum 39 | ``` 40 | 41 | * To enable this plugin: 42 | ``` 43 | :EnableAirlineColorNum 44 | ``` 45 | 46 | ##Troubleshooting 47 | If you are not seeing the highlight in certain modes, make sure your cursorline 48 | is enabled by adding this to your `.vimrc`: 49 | ``` 50 | set cursorline 51 | ``` 52 | Depending on your theme, this may cause your cursorline to be highlighted. 53 | If you do not like this behavior, then also add the following to your `.vimrc`: 54 | ``` 55 | hi clear CursorLine 56 | ``` 57 | 58 | ##Promotion 59 | If you like this plugin, please star it on Github and vote it up at Vim.org! 60 | 61 | Repository exists at: http://github.com/ntpeters/vim-airline-colornum 62 | 63 | Plugin also hosted at: http://www.vim.org/scripts/script.php?script_id=5219 64 | -------------------------------------------------------------------------------- /plugin/airline-colornum.vim: -------------------------------------------------------------------------------- 1 | " Author: Nate Peterson 2 | " Repository: http://github.com/ntpeters/vim-airline-colornum 3 | " 4 | " Syncs the color of the cursor line number with the current mode color used by 5 | " Airline. 6 | 7 | " Prevent loading the plugin multiple times 8 | if exists('g:loaded_airline_colornum_plugin') 9 | finish 10 | endif 11 | let g:loaded_airline_colornum_plugin = 1 12 | 13 | " Init 'enabled' var if not set by user 14 | if !exists('g:airline_colornum_enabled') 15 | let g:airline_colornum_enabled = 1 16 | endif 17 | 18 | " The current Vim mode - Airline Key 19 | let s:airline_mode = '' 20 | " The previous mode 21 | let s:last_airline_mode = '' 22 | " The last Airline theme 23 | let s:last_airline_theme = '' 24 | " The last Vim colorscheme 25 | let s:last_colorscheme = '' 26 | 27 | " Gets the current Vim mode as a key compatible with the Airline color map 28 | function! s:GetAirlineMode() 29 | " Only set mode if airline is active 30 | " Note: Do not set mode to 'inactive'. This causes an infinite loop when 31 | " opening a split. 32 | if get(w:, 'airline_active', 1) 33 | let l:mode = mode() 34 | " Convert mode to a compatible value 35 | if l:mode ==# "i" 36 | let s:airline_mode = 'insert' 37 | elseif l:mode ==# "R" 38 | let s:airline_mode = 'replace' 39 | elseif l:mode =~# '\v(v|V||s|S|)' 40 | let s:airline_mode = 'visual' 41 | else 42 | let s:airline_mode = 'normal' 43 | endif 44 | endif 45 | endfunction 46 | 47 | " Gets the statusline colors for the current mode from Airline 48 | function! s:GetAirlineModeColors() 49 | " Fallback value if unable to determine colors. This will reset highlight 50 | " to default values 51 | let l:mode_colors = [ 'NONE', 'NONE', 'NONE', 'NONE' ] 52 | 53 | " Ensures the current palette has colors for the current mode 54 | if has_key(g:airline#themes#{g:airline_theme}#palette, s:airline_mode) 55 | " Fetch colors from the current theme palette 56 | " If 'airline_z' is undefined, fallback to 'airline_a' 57 | " they should be equivalent 58 | if has_key(g:airline#themes#{g:airline_theme}#palette[s:airline_mode], 'airline_z') 59 | let l:mode_colors = deepcopy(g:airline#themes#{g:airline_theme}#palette[s:airline_mode]['airline_z']) 60 | elseif has_key(g:airline#themes#{g:airline_theme}#palette[s:airline_mode], 'airline_a') 61 | let l:mode_colors = deepcopy(g:airline#themes#{g:airline_theme}#palette[s:airline_mode]['airline_a']) 62 | else 63 | " This should never happen as long as airline is loaded... 64 | echom "Error getting colors from airline! - Section key undefined!" 65 | endif 66 | else 67 | echom "Error getting colors from airline! - Mode key undefined!" 68 | endif 69 | 70 | return l:mode_colors 71 | endfunction 72 | 73 | " Set the color of the cursor line number 74 | function! s:SetCursorLineNrColor() 75 | " Update cursor line number color 76 | let l:mode_colors = GetAirlineModeColors() 77 | if !empty(l:mode_colors) 78 | let l:resolve_index = [ 'guifg', 'guibg', 'ctermfg', 'ctermbg' ] 79 | let l:mode_colors_exec = [] 80 | for l:i in range(0, 3, 1) 81 | if !empty(l:mode_colors[l:i]) 82 | let l:mode_colors_exec = add(l:mode_colors_exec, 83 | \ l:resolve_index[l:i] . '=' . 84 | \ l:mode_colors[l:i]) 85 | endif 86 | endfor 87 | exec printf('highlight %s %s', 88 | \ 'CursorLineNr', 89 | \ join(l:mode_colors_exec, ' ')) 90 | endif 91 | endfunction 92 | 93 | " Determines when a redraw of the line number should occur: 94 | " ColorLineNr seems to only redraw on cursor moved events for visual mode? 95 | " Force a redraw when toggling Airline back on 96 | " Force a redraw when changing Airline theme 97 | function! s:ShouldRedrawCursorLineNr() 98 | if s:airline_mode == 'visual' || 99 | \ s:last_airline_mode == 'visual' || 100 | \ s:last_airline_mode == 'toggledoff' || 101 | \ g:airline_theme != s:last_airline_theme || 102 | \ s:last_colorscheme != g:colors_name 103 | return 1 104 | endif 105 | return 0 106 | endfunction 107 | 108 | " Determines when the color of the line number should be updated: 109 | " When the mode has changed 110 | " When the Airline theme has changed 111 | " When th Vim colorscheme has changed 112 | function! s:ShouldUpdateCursorLineNrColor() 113 | if s:airline_mode != s:last_airline_mode || 114 | \ g:airline_theme != s:last_airline_theme || 115 | \ s:last_colorscheme != g:colors_name 116 | return 1 117 | endif 118 | return 0 119 | endfunction 120 | 121 | " Sets the cursor line number to the color for the current mode from Airline 122 | function! UpdateCursorLineNr() 123 | " Ensure Airline is still enabled. Stop highlight updates if not 124 | if !exists('#airline') 125 | call AirlineToggledOff() 126 | else 127 | call AirlineToggledOn() 128 | 129 | " Do nothing if plugin is disabled 130 | if g:airline_colornum_enabled == 1 131 | " Update the current mode 132 | call GetAirlineMode() 133 | 134 | " Update cursor line number color 135 | if ShouldUpdateCursorLineNrColor() 136 | call SetCursorLineNrColor() 137 | 138 | " Cause the cursor line num to be redrawn to update color 139 | if ShouldRedrawCursorLineNr() 140 | if col('.') == 1 141 | call feedkeys("\\", 'n') 142 | else 143 | call feedkeys("\\", 'n') 144 | endif 145 | endif 146 | 147 | " Save last mode 148 | let s:last_airline_mode = s:airline_mode 149 | " Save last theme 150 | let s:last_airline_theme = g:airline_theme 151 | " Save last colorscheme 152 | let s:last_colorscheme = g:colors_name 153 | endif 154 | endif 155 | endif 156 | return '' 157 | endfunction 158 | 159 | " Ensure line number is update every time the status line is updated 160 | function! s:LoadCursorLineNrUpdates() 161 | " Only add to statusline if Airline is loaded and it has not been added 162 | if get(g:, 'loaded_airline', 0) == 1 163 | if exists('g:airline_section_z') && g:airline_section_z !~ 'UpdateCursorLineNr' 164 | let g:airline_section_z .= '%{UpdateCursorLineNr()}' 165 | " Force color to update now 166 | call UpdateCursorLineNr() 167 | endif 168 | endif 169 | endfunction 170 | 171 | " Unload the function for updating the line number 172 | function! s:UnloadCursorLineNrUpdates() 173 | if exists('g:airline_section_z') 174 | let g:airline_section_z = substitute(g:airline_section_z, '%{UpdateCursorLineNr()}', "", "g") 175 | endif 176 | endfunction 177 | 178 | " Resets colors for the cursor line number to default 179 | function! s:ResetCursorLineNrColor() 180 | " Restore original colors 181 | highlight CursorLineNr ctermfg=NONE ctermbg=NONE guifg=NONE guibg=NONE 182 | " Reset last mode 183 | let s:last_airline_mode = '' 184 | endfunction 185 | 186 | " When Airline is toggled off, restore status line and cursor line to original 187 | " states 188 | function! s:AirlineToggledOff() 189 | " Restore original statusline 190 | let &statusline = s:original_statusline 191 | " Restore cursor line color and redraw 192 | call ResetCursorLineNrColor() 193 | call feedkeys("\\", 'n') 194 | " Used to ensure this is re-enabled when Airline is toggled on 195 | let s:airline_toggled_off = 1 196 | " Ensures the cursor line number is redrawn correctly 197 | let s:last_airline_mode = 'toggledoff' 198 | endfunction 199 | 200 | " Saves the original status line and adds an update call to status line for 201 | " when Airline is toggled off 202 | function! s:AirlineToggledOn() 203 | if exists('#airline') 204 | " Only execute if this is the first time or if Airline was toggled off 205 | if !exists('s:airline_toggled_off') || s:airline_toggled_off == 1 206 | execute 'AirlineToggle' 207 | " Save original status line 208 | if !exists('s:original_statusline') 209 | let s:original_statusline = &statusline 210 | endif 211 | " Add call to status line to ensure an update when Airline is 212 | " toggled off 213 | if &statusline !~ 'UpdateCursorLineNr' 214 | set statusline+=%{UpdateCursorLineNr()} 215 | endif 216 | execute 'AirlineToggle' 217 | let s:airline_toggled_off = 0 218 | endif 219 | endif 220 | endfunction 221 | 222 | " Enables this plugin 223 | function! s:EnableAirlineColorNum() 224 | let g:airline_colornum_enabled = 1 225 | " Setup autocommands 226 | augroup AirlineColorNum 227 | au! 228 | " Ensure function has been loaded into the status line whenever entering a buffer 229 | autocmd BufWinEnter * call LoadCursorLineNrUpdates() 230 | autocmd ColorScheme * call SetCursorLineNrColor() 231 | augroup END 232 | " Attempt to load immediately 233 | call LoadCursorLineNrUpdates() 234 | endfunction 235 | 236 | " Disables this plugin 237 | function! s:DisableAirlineColorNum() 238 | let g:airline_colornum_enabled = 0 239 | " Delete autocommands 240 | augroup! AirlineColorNum 241 | call UnloadCursorLineNrUpdates() 242 | call ResetCursorLineNrColor() 243 | endfunction 244 | 245 | " Initial plugin setup 246 | function! s:SetupAirlineColorNum() 247 | " Setup plugin commands 248 | command! EnableAirlineColorNum call EnableAirlineColorNum() 249 | command! DisableAirlineColorNum call DisableAirlineColorNum() 250 | 251 | " Enable plugin if not disabled by user 252 | if g:airline_colornum_enabled == 1 253 | call EnableAirlineColorNum() 254 | endif 255 | endfunction 256 | 257 | " Perform plugin init 258 | call SetupAirlineColorNum() 259 | --------------------------------------------------------------------------------