whitespace.vim 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. " MIT License. Copyright (c) 2013-2015 Bailey Ling.
  2. " vim: et ts=2 sts=2 sw=2
  3. " http://got-ravings.blogspot.com/2008/10/vim-pr0n-statusline-whitespace-flags.html
  4. " for backwards compatibility
  5. if exists('g:airline_detect_whitespace')
  6. let s:show_message = g:airline_detect_whitespace == 1
  7. else
  8. let s:show_message = get(g:, 'airline#extensions#whitespace#show_message', 1)
  9. endif
  10. let s:symbol = get(g:, 'airline#extensions#whitespace#symbol', g:airline_symbols.whitespace)
  11. let s:default_checks = ['indent', 'trailing']
  12. let s:trailing_format = get(g:, 'airline#extensions#whitespace#trailing_format', 'trailing[%s]')
  13. let s:mixed_indent_format = get(g:, 'airline#extensions#whitespace#mixed_indent_format', 'mixed-indent[%s]')
  14. let s:long_format = get(g:, 'airline#extensions#whitespace#long_format', 'long[%s]')
  15. let s:indent_algo = get(g:, 'airline#extensions#whitespace#mixed_indent_algo', 0)
  16. let s:max_lines = get(g:, 'airline#extensions#whitespace#max_lines', 20000)
  17. let s:enabled = get(g:, 'airline#extensions#whitespace#enabled', 1)
  18. function! s:check_mixed_indent()
  19. if s:indent_algo == 1
  20. " [<tab>]<space><tab>
  21. " spaces before or between tabs are not allowed
  22. let t_s_t = '(^\t* +\t\s*\S)'
  23. " <tab>(<space> x count)
  24. " count of spaces at the end of tabs should be less than tabstop value
  25. let t_l_s = '(^\t+ {' . &ts . ',}' . '\S)'
  26. return search('\v' . t_s_t . '|' . t_l_s, 'nw')
  27. elseif s:indent_algo == 2
  28. return search('\v(^\t* +\t\s*\S)', 'nw')
  29. else
  30. return search('\v(^\t+ +)|(^ +\t+)', 'nw')
  31. endif
  32. endfunction
  33. function! airline#extensions#whitespace#check()
  34. if &readonly || !&modifiable || !s:enabled || line('$') > s:max_lines
  35. return ''
  36. endif
  37. if !exists('b:airline_whitespace_check')
  38. let b:airline_whitespace_check = ''
  39. let checks = get(g:, 'airline#extensions#whitespace#checks', s:default_checks)
  40. let trailing = 0
  41. if index(checks, 'trailing') > -1
  42. let trailing = search('\s$', 'nw')
  43. endif
  44. let mixed = 0
  45. if index(checks, 'indent') > -1
  46. let mixed = s:check_mixed_indent()
  47. endif
  48. let long = 0
  49. if index(checks, 'long') > -1 && &tw > 0
  50. let long = search('\%>'.&tw.'v.\+', 'nw')
  51. endif
  52. if trailing != 0 || mixed != 0 || long != 0
  53. let b:airline_whitespace_check = s:symbol
  54. if s:show_message
  55. if trailing != 0
  56. let b:airline_whitespace_check .= (g:airline_symbols.space).printf(s:trailing_format, trailing)
  57. endif
  58. if mixed != 0
  59. let b:airline_whitespace_check .= (g:airline_symbols.space).printf(s:mixed_indent_format, mixed)
  60. endif
  61. if long != 0
  62. let b:airline_whitespace_check .= (g:airline_symbols.space).printf(s:long_format, long)
  63. endif
  64. endif
  65. endif
  66. endif
  67. return b:airline_whitespace_check
  68. endfunction
  69. function! airline#extensions#whitespace#toggle()
  70. if s:enabled
  71. augroup airline_whitespace
  72. autocmd!
  73. augroup END
  74. augroup! airline_whitespace
  75. let s:enabled = 0
  76. else
  77. call airline#extensions#whitespace#init()
  78. let s:enabled = 1
  79. endif
  80. if exists("g:airline#extensions#whitespace#enabled")
  81. let g:airline#extensions#whitespace#enabled = s:enabled
  82. if s:enabled && match(g:airline_section_warning, '#whitespace#check') < 0
  83. let g:airline_section_warning .= airline#section#create(['whitespace'])
  84. call airline#update_statusline()
  85. endif
  86. endif
  87. echo 'Whitespace checking: '.(s:enabled ? 'Enabled' : 'Disabled')
  88. endfunction
  89. function! airline#extensions#whitespace#init(...)
  90. call airline#parts#define_function('whitespace', 'airline#extensions#whitespace#check')
  91. unlet! b:airline_whitespace_check
  92. augroup airline_whitespace
  93. autocmd!
  94. autocmd CursorHold,BufWritePost * unlet! b:airline_whitespace_check
  95. augroup END
  96. endfunction