1 " Powerline - The ultimate statusline utility
3 " Author: Kim Silkebækken <kim.silkebaekken+vim@gmail.com>
4 " Source repository: https://github.com/Lokaltog/vim-powerline
9 let g:Pl#THEME_CALLBACKS = []
12 " Cache revision, this must be incremented whenever the cache format is changed
13 let s:CACHE_REVISION = 5
15 " Script initialization {{{
16 function! Pl#LoadCache() " {{{
17 if filereadable(g:Powerline_cache_file) && g:Powerline_cache_enabled
18 exec 'source' escape(g:Powerline_cache_file, ' \')
20 if ! exists('g:Powerline_cache_revision') || g:Powerline_cache_revision != s:CACHE_REVISION
21 " Cache revision differs, cache is invalid
22 unlet! g:Powerline_cache_revision
27 " Create highlighting groups
33 for callback in g:Pl#THEME_CALLBACKS
34 " Substitute {{NEWLINE}} with newlines (strings must be
35 " stored without newlines characters to avoid vim errors)
36 exec substitute(callback[0], "{{NEWLINE}}", "\n", 'g')
37 exec substitute(callback[1], "{{NEWLINE}}", "\n", 'g')
45 function! Pl#ClearCache() " {{{
46 if filereadable(g:Powerline_cache_file)
47 " Delete the cache file
48 call delete(g:Powerline_cache_file)
51 echo 'Powerline cache cleared. Please restart vim for the changes to take effect.'
53 function! Pl#ReloadColorscheme() " {{{
56 " The colorscheme and theme files must be manually sourced because
57 " vim won't reload previously autoloaded files
59 " This is a bit hackish, but it works
60 unlet! g:Powerline#Colorschemes#{g:Powerline_colorscheme}#colorscheme
61 exec "source" split(globpath(&rtp, 'autoload/Powerline/Colorschemes/'. g:Powerline_colorscheme .'.vim', 1), '\n')[0]
63 unlet! g:Powerline#Themes#{g:Powerline_theme}#theme
64 exec "source" split(globpath(&rtp, 'autoload/Powerline/Themes/'. g:Powerline_theme .'.vim', 1), '\n')[0]
70 function! Pl#Load() " {{{
71 if empty(g:Pl#OLD_STL)
72 " Store old statusline
73 let g:Pl#OLD_STL = &statusline
78 " Autoload the theme dict first
79 let raw_theme = g:Powerline#Themes#{g:Powerline_theme}#theme
81 echom 'Invalid Powerline theme! Please check your theme and colorscheme settings.'
86 " Create list with parsed statuslines
87 for buffer_statusline in raw_theme
88 unlet! mode_statuslines
89 let mode_statuslines = Pl#Parser#GetStatusline(buffer_statusline.segments)
91 if ! empty(buffer_statusline.callback)
92 " The callback function passes its arguments on to
93 " Pl#StatuslineCallback along with the normal/current mode
95 let s:cb_func = "function! PowerlineStatuslineCallback_". buffer_statusline.callback[1] ."(...)\n"
96 let s:cb_func .= "return Pl#StatuslineCallback(". string(mode_statuslines['n']) .", a:000)\n"
97 let s:cb_func .= "endfunction"
99 " The callback expression should be used to initialize any
100 " variables that will use the callback function. The
101 " expression requires a %s which will be replaced by the
102 " callback function name.
103 let s:cb_expr = printf(buffer_statusline.callback[2], 'PowerlineStatuslineCallback_'. buffer_statusline.callback[1])
108 " Newlines must be substituted with another character
109 " because vim doesn't like newlines in strings
110 call add(g:Pl#THEME_CALLBACKS, [substitute(s:cb_func, "\n", "{{NEWLINE}}", 'g'), substitute(s:cb_expr, "\n", "{{NEWLINE}}", 'g')])
112 unlet! s:cb_func s:cb_expr
117 " Store the statuslines for matching specific buffers
118 call add(g:Pl#THEME, {
119 \ 'matches': buffer_statusline.matches,
120 \ 'mode_statuslines': mode_statuslines
124 if ! g:Powerline_cache_enabled
125 " Don't cache anything if caching is disabled or cache file isn't writeable
129 " Prepare commands and statuslines for caching
131 \ 'let g:Powerline_cache_revision = '. string(s:CACHE_REVISION),
132 \ 'let g:Pl#HL = '. string(g:Pl#HL),
133 \ 'let g:Pl#THEME = '. string(g:Pl#THEME),
134 \ 'let g:Pl#THEME_CALLBACKS = '. string(g:Pl#THEME_CALLBACKS),
137 call writefile(cache, g:Powerline_cache_file)
141 " Statusline updater {{{
142 function! Pl#Statusline(statusline, current) " {{{
146 let mode = 'N' " Normal (non-current)
147 elseif mode =~# '\v(v|V|
\16)'
148 let mode = 'v' " Visual mode
149 elseif mode =~# '\v(s|S|
\13)'
150 let mode = 's' " Select mode
151 elseif mode =~# '\vi'
152 let mode = 'i' " Insert mode
153 elseif mode =~# '\v(R|Rv)'
154 let mode = 'r' " Replace mode
156 " Fallback to normal mode
157 let mode = 'n' " Normal (current)
160 return g:Pl#THEME[a:statusline].mode_statuslines[mode]
162 function! Pl#StatuslineCallback(statusline, args) " {{{
163 " Replace %1, %2, etc. in the statusline with the callback args
167 \ '\=a:args[submatch(1)]',
170 function! Pl#UpdateStatusline(current) " {{{
172 " Load statuslines if they aren't loaded yet
176 for i in range(0, len(g:Pl#THEME) - 1)
177 if Pl#Match#Validate(g:Pl#THEME[i])
178 " Update window-local statusline
179 let &l:statusline = '%!Pl#Statusline('. i .','. a:current .')'