]> git.r.bdr.sh - rbdr/dotfiles/blame_incremental - vim/autoload/Pl.vim
Move EasyMotion and LargeFile to pathogen bundle
[rbdr/dotfiles] / vim / autoload / Pl.vim
... / ...
CommitLineData
1" Powerline - The ultimate statusline utility
2"
3" Author: Kim Silkebækken <kim.silkebaekken+vim@gmail.com>
4" Source repository: https://github.com/Lokaltog/vim-powerline
5
6" Script variables {{{
7 let g:Pl#OLD_STL = ''
8 let g:Pl#THEME = []
9 let g:Pl#THEME_CALLBACKS = []
10 let g:Pl#HL = []
11
12 " Cache revision, this must be incremented whenever the cache format is changed
13 let s:CACHE_REVISION = 5
14" }}}
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, ' \')
19
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
23
24 return 0
25 endif
26
27 " Create highlighting groups
28 for hi_cmd in g:Pl#HL
29 exec hi_cmd
30 endfor
31
32 " Run theme callbacks
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')
38 endfor
39
40 return 1
41 endif
42
43 return 0
44 endfunction " }}}
45 function! Pl#ClearCache() " {{{
46 if filereadable(g:Powerline_cache_file)
47 " Delete the cache file
48 call delete(g:Powerline_cache_file)
49 endif
50
51 echo 'Powerline cache cleared. Please restart vim for the changes to take effect.'
52 endfunction " }}}
53 function! Pl#ReloadColorscheme() " {{{
54 call Pl#ClearCache()
55
56 " The colorscheme and theme files must be manually sourced because
57 " vim won't reload previously autoloaded files
58 "
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]
62
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]
65
66 let g:Pl#THEME = []
67
68 call Pl#Load()
69 endfunction " }}}
70 function! Pl#Load() " {{{
71 if empty(g:Pl#OLD_STL)
72 " Store old statusline
73 let g:Pl#OLD_STL = &statusline
74 endif
75
76 if ! Pl#LoadCache()
77 try
78 " Autoload the theme dict first
79 let raw_theme = g:Powerline#Themes#{g:Powerline_theme}#theme
80 catch
81 echom 'Invalid Powerline theme! Please check your theme and colorscheme settings.'
82
83 return
84 endtry
85
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)
90
91 if ! empty(buffer_statusline.callback)
92 " The callback function passes its arguments on to
93 " Pl#StatuslineCallback along with the normal/current mode
94 " statusline.
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"
98
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])
104
105 exec s:cb_func
106 exec s:cb_expr
107
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')])
111
112 unlet! s:cb_func s:cb_expr
113
114 continue
115 endif
116
117 " Store the statuslines for matching specific buffers
118 call add(g:Pl#THEME, {
119 \ 'matches': buffer_statusline.matches,
120 \ 'mode_statuslines': mode_statuslines
121 \ })
122 endfor
123
124 if ! g:Powerline_cache_enabled
125 " Don't cache anything if caching is disabled or cache file isn't writeable
126 return
127 endif
128
129 " Prepare commands and statuslines for caching
130 let cache = [
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),
135 \ ]
136
137 call writefile(cache, g:Powerline_cache_file)
138 endif
139 endfunction " }}}
140" }}}
141" Statusline updater {{{
142 function! Pl#Statusline(statusline, current) " {{{
143 let mode = mode()
144
145 if ! a: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
155 else
156 " Fallback to normal mode
157 let mode = 'n' " Normal (current)
158 endif
159
160 return g:Pl#THEME[a:statusline].mode_statuslines[mode]
161 endfunction " }}}
162 function! Pl#StatuslineCallback(statusline, args) " {{{
163 " Replace %1, %2, etc. in the statusline with the callback args
164 return substitute(
165 \ a:statusline,
166 \ '\v\%(\d+)',
167 \ '\=a:args[submatch(1)]',
168 \ 'g')
169 endfunction " }}}
170 function! Pl#UpdateStatusline(current) " {{{
171 if empty(g:Pl#THEME)
172 " Load statuslines if they aren't loaded yet
173 call Pl#Load()
174 endif
175
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 .')'
180 endif
181 endfor
182 endfunction " }}}
183" }}}