default.vim 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. " MIT License. Copyright (c) 2013-2021 Bailey Ling et al.
  2. " vim: et ts=2 sts=2 sw=2
  3. scriptencoding utf-8
  4. let s:section_use_groups = get(g:, 'airline#extensions#default#section_use_groupitems', 1)
  5. let s:section_truncate_width = get(g:, 'airline#extensions#default#section_truncate_width', {
  6. \ 'b': 79,
  7. \ 'x': 60,
  8. \ 'y': 80,
  9. \ 'z': 45,
  10. \ 'warning': 80,
  11. \ 'error': 80,
  12. \ })
  13. let s:layout = get(g:, 'airline#extensions#default#layout', [
  14. \ [ 'a', 'b', 'c' ],
  15. \ [ 'x', 'y', 'z', 'warning', 'error' ]
  16. \ ])
  17. function! s:get_section(winnr, key, ...)
  18. if has_key(s:section_truncate_width, a:key)
  19. if airline#util#winwidth(a:winnr) < s:section_truncate_width[a:key]
  20. return ''
  21. endif
  22. endif
  23. let spc = g:airline_symbols.space
  24. if !exists('g:airline_section_{a:key}')
  25. return ''
  26. endif
  27. let text = airline#util#getwinvar(a:winnr, 'airline_section_'.a:key, g:airline_section_{a:key})
  28. let [prefix, suffix] = [get(a:000, 0, '%('.spc), get(a:000, 1, spc.'%)')]
  29. return empty(text) ? '' : prefix.text.suffix
  30. endfunction
  31. function! s:build_sections(builder, context, keys)
  32. for key in a:keys
  33. if (key == 'warning' || key == 'error') && !a:context.active
  34. continue
  35. endif
  36. call s:add_section(a:builder, a:context, key)
  37. endfor
  38. endfunction
  39. " There still is a highlighting bug when using groups %(%) in the statusline,
  40. " deactivate it, unless it is fixed (7.4.1511)
  41. if s:section_use_groups && (v:version >= 704 || (v:version >= 703 && has('patch81')))
  42. function! s:add_section(builder, context, key)
  43. let condition = (a:key is# "warning" || a:key is# "error") &&
  44. \ (v:version == 704 && !has("patch1511"))
  45. " i have no idea why the warning section needs special treatment, but it's
  46. " needed to prevent separators from showing up
  47. if ((a:key == 'error' || a:key == 'warning') && empty(s:get_section(a:context.winnr, a:key)))
  48. return
  49. endif
  50. if condition
  51. call a:builder.add_raw('%(')
  52. endif
  53. call a:builder.add_section('airline_'.a:key, s:get_section(a:context.winnr, a:key))
  54. if condition
  55. call a:builder.add_raw('%)')
  56. endif
  57. endfunction
  58. else
  59. " older version don't like the use of %(%)
  60. function! s:add_section(builder, context, key)
  61. if ((a:key == 'error' || a:key == 'warning') && empty(s:get_section(a:context.winnr, a:key)))
  62. return
  63. endif
  64. if a:key == 'warning'
  65. call a:builder.add_raw('%#airline_warning#'.s:get_section(a:context.winnr, a:key))
  66. elseif a:key == 'error'
  67. call a:builder.add_raw('%#airline_error#'.s:get_section(a:context.winnr, a:key))
  68. else
  69. call a:builder.add_section('airline_'.a:key, s:get_section(a:context.winnr, a:key))
  70. endif
  71. endfunction
  72. endif
  73. function! airline#extensions#default#apply(builder, context) abort
  74. let winnr = a:context.winnr
  75. let active = a:context.active
  76. if airline#util#getwinvar(winnr, 'airline_render_left', active || (!active && !g:airline_inactive_collapse))
  77. call s:build_sections(a:builder, a:context, s:layout[0])
  78. else
  79. let text = !empty(s:get_section(winnr, 'c')) ? s:get_section(winnr, 'c') : ' %f%m '
  80. call a:builder.add_section('airline_c'.(a:context.bufnr), text)
  81. endif
  82. call a:builder.split(s:get_section(winnr, 'gutter', '', ''))
  83. if airline#util#getwinvar(winnr, 'airline_render_right', 1)
  84. call s:build_sections(a:builder, a:context, s:layout[1])
  85. endif
  86. return 1
  87. endfunction