airline.vim 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. " MIT License. Copyright (c) 2013-2015 Bailey Ling.
  2. " vim: et ts=2 sts=2 sw=2
  3. let g:airline_statusline_funcrefs = get(g:, 'airline_statusline_funcrefs', [])
  4. let s:sections = ['a','b','c','gutter','x','y','z','warning']
  5. let s:inactive_funcrefs = []
  6. function! airline#add_statusline_func(name)
  7. call airline#add_statusline_funcref(function(a:name))
  8. endfunction
  9. function! airline#add_statusline_funcref(function)
  10. if index(g:airline_statusline_funcrefs, a:function) >= 0
  11. echohl WarningMsg
  12. echo 'The airline statusline funcref '.string(a:function).' has already been added.'
  13. echohl NONE
  14. return
  15. endif
  16. call add(g:airline_statusline_funcrefs, a:function)
  17. endfunction
  18. function! airline#remove_statusline_func(name)
  19. let i = index(g:airline_statusline_funcrefs, function(a:name))
  20. if i > -1
  21. call remove(g:airline_statusline_funcrefs, i)
  22. endif
  23. endfunction
  24. function! airline#add_inactive_statusline_func(name)
  25. call add(s:inactive_funcrefs, function(a:name))
  26. endfunction
  27. function! airline#load_theme()
  28. if exists('*airline#themes#{g:airline_theme}#refresh')
  29. call airline#themes#{g:airline_theme}#refresh()
  30. endif
  31. let palette = g:airline#themes#{g:airline_theme}#palette
  32. call airline#themes#patch(palette)
  33. if exists('g:airline_theme_patch_func')
  34. let Fn = function(g:airline_theme_patch_func)
  35. call Fn(palette)
  36. endif
  37. call airline#highlighter#load_theme()
  38. call airline#extensions#load_theme()
  39. call airline#update_statusline()
  40. endfunction
  41. function! airline#switch_theme(name)
  42. try
  43. let palette = g:airline#themes#{a:name}#palette "also lazy loads the theme
  44. let g:airline_theme = a:name
  45. catch
  46. echohl WarningMsg | echo 'The specified theme cannot be found.' | echohl NONE
  47. if exists('g:airline_theme')
  48. return
  49. else
  50. let g:airline_theme = 'dark'
  51. endif
  52. endtry
  53. let w:airline_lastmode = ''
  54. call airline#load_theme()
  55. " this is required to prevent clobbering the startup info message, i don't know why...
  56. call airline#check_mode(winnr())
  57. endfunction
  58. function! airline#switch_matching_theme()
  59. if exists('g:colors_name')
  60. try
  61. let palette = g:airline#themes#{g:colors_name}#palette
  62. call airline#switch_theme(g:colors_name)
  63. return 1
  64. catch
  65. for map in items(g:airline_theme_map)
  66. if match(g:colors_name, map[0]) > -1
  67. call airline#switch_theme(map[1])
  68. return 1
  69. endif
  70. endfor
  71. endtry
  72. endif
  73. return 0
  74. endfunction
  75. function! airline#update_statusline()
  76. if airline#util#getwinvar(winnr(), 'airline_disabled', 0)
  77. return
  78. endif
  79. for nr in filter(range(1, winnr('$')), 'v:val != winnr()')
  80. if airline#util#getwinvar(nr, 'airline_disabled', 0)
  81. continue
  82. endif
  83. call setwinvar(nr, 'airline_active', 0)
  84. let context = { 'winnr': nr, 'active': 0, 'bufnr': winbufnr(nr) }
  85. call s:invoke_funcrefs(context, s:inactive_funcrefs)
  86. endfor
  87. unlet! w:airline_render_left
  88. unlet! w:airline_render_right
  89. for section in s:sections
  90. unlet! w:airline_section_{section}
  91. endfor
  92. let w:airline_active = 1
  93. let context = { 'winnr': winnr(), 'active': 1, 'bufnr': winbufnr(winnr()) }
  94. call s:invoke_funcrefs(context, g:airline_statusline_funcrefs)
  95. endfunction
  96. let s:contexts = {}
  97. let s:core_funcrefs = [
  98. \ function('airline#extensions#apply'),
  99. \ function('airline#extensions#default#apply') ]
  100. function! s:invoke_funcrefs(context, funcrefs)
  101. let builder = airline#builder#new(a:context)
  102. let err = airline#util#exec_funcrefs(a:funcrefs + s:core_funcrefs, builder, a:context)
  103. if err == 1
  104. let a:context.line = builder.build()
  105. let s:contexts[a:context.winnr] = a:context
  106. call setwinvar(a:context.winnr, '&statusline', '%!airline#statusline('.a:context.winnr.')')
  107. endif
  108. endfunction
  109. function! airline#statusline(winnr)
  110. if has_key(s:contexts, a:winnr)
  111. return '%{airline#check_mode('.a:winnr.')}'.s:contexts[a:winnr].line
  112. endif
  113. " in rare circumstances this happens...see #276
  114. return ''
  115. endfunction
  116. function! airline#check_mode(winnr)
  117. let context = s:contexts[a:winnr]
  118. if get(w:, 'airline_active', 1)
  119. let l:m = mode()
  120. if l:m ==# "i"
  121. let l:mode = ['insert']
  122. elseif l:m ==# "R"
  123. let l:mode = ['replace']
  124. elseif l:m =~# '\v(v|V||s|S|)'
  125. let l:mode = ['visual']
  126. elseif l:m ==# "t"
  127. let l:mode = ['terminal']
  128. else
  129. let l:mode = ['normal']
  130. endif
  131. let w:airline_current_mode = get(g:airline_mode_map, l:m, l:m)
  132. else
  133. let l:mode = ['inactive']
  134. let w:airline_current_mode = get(g:airline_mode_map, '__')
  135. endif
  136. if g:airline_detect_modified && &modified
  137. call add(l:mode, 'modified')
  138. endif
  139. if g:airline_detect_paste && &paste
  140. call add(l:mode, 'paste')
  141. endif
  142. if g:airline_detect_crypt && exists("+key") && !empty(&key)
  143. call add(l:mode, 'crypt')
  144. endif
  145. if &readonly || ! &modifiable
  146. call add(l:mode, 'readonly')
  147. endif
  148. let mode_string = join(l:mode)
  149. if get(w:, 'airline_lastmode', '') != mode_string
  150. call airline#highlighter#highlight_modified_inactive(context.bufnr)
  151. call airline#highlighter#highlight(l:mode)
  152. let w:airline_lastmode = mode_string
  153. endif
  154. return ''
  155. endfunction