whitespace.vim 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. " MIT License. Copyright (c) 2013-2021 Bailey Ling et al.
  2. " vim: et ts=2 sts=2 sw=2
  3. " http://got-ravings.blogspot.com/2008/10/vim-pr0n-statusline-whitespace-flags.html
  4. scriptencoding utf-8
  5. let s:show_message = get(g:, 'airline#extensions#whitespace#show_message', 1)
  6. let s:symbol = get(g:, 'airline#extensions#whitespace#symbol', g:airline_symbols.whitespace)
  7. let s:default_checks = ['indent', 'trailing', 'mixed-indent-file', 'conflicts']
  8. let s:enabled = get(g:, 'airline#extensions#whitespace#enabled', 1)
  9. let s:skip_check_ft = {'make': ['indent', 'mixed-indent-file'],
  10. \ 'csv': ['indent', 'mixed-indent-file'],
  11. \ 'mail': ['trailing']}
  12. function! s:check_mixed_indent()
  13. let indent_algo = get(g:, 'airline#extensions#whitespace#mixed_indent_algo', 0)
  14. if indent_algo == 1
  15. " [<tab>]<space><tab>
  16. " spaces before or between tabs are not allowed
  17. let t_s_t = '(^\t* +\t\s*\S)'
  18. " <tab>(<space> x count)
  19. " count of spaces at the end of tabs should be less than tabstop value
  20. let t_l_s = '(^\t+ {' . &ts . ',}' . '\S)'
  21. return search('\v' . t_s_t . '|' . t_l_s, 'nw')
  22. elseif indent_algo == 2
  23. return search('\v(^\t* +\t\s*\S)', 'nw', 0, 500)
  24. else
  25. return search('\v(^\t+ +)|(^ +\t+)', 'nw', 0, 500)
  26. endif
  27. endfunction
  28. function! s:check_mixed_indent_file()
  29. let c_like_langs = get(g:, 'airline#extensions#c_like_langs',
  30. \ [ 'arduino', 'c', 'cpp', 'cuda', 'go', 'javascript', 'ld', 'php' ])
  31. if index(c_like_langs, &ft) > -1
  32. " for C-like languages: allow /** */ comment style with one space before the '*'
  33. let head_spc = '\v(^ +\*@!)'
  34. else
  35. let head_spc = '\v(^ +)'
  36. endif
  37. let indent_tabs = search('\v(^\t+)', 'nw')
  38. let indent_spc = search(head_spc, 'nw')
  39. if indent_tabs > 0 && indent_spc > 0
  40. return printf("%d:%d", indent_tabs, indent_spc)
  41. else
  42. return ''
  43. endif
  44. endfunction
  45. function! s:conflict_marker()
  46. " Checks for git conflict markers
  47. let annotation = '\%([0-9A-Za-z_.:]\+\)\?'
  48. if match(['rst', 'markdown', 'rmd'], &ft) >= 0
  49. " rst filetypes use '=======' as header
  50. let pattern = '^\%(\%(<\{7} '.annotation. '\)\|\%(>\{7\} '.annotation.'\)\)$'
  51. else
  52. let pattern = '^\%(\%(<\{7} '.annotation. '\)\|\%(=\{7\}\)\|\%(>\{7\} '.annotation.'\)\)$'
  53. endif
  54. return search(pattern, 'nw')
  55. endfunction
  56. function! airline#extensions#whitespace#check()
  57. let max_lines = get(g:, 'airline#extensions#whitespace#max_lines', 20000)
  58. if &readonly || !&modifiable || !s:enabled || line('$') > max_lines
  59. \ || get(b:, 'airline_whitespace_disabled', 0)
  60. return ''
  61. endif
  62. let skip_check_ft = extend(s:skip_check_ft,
  63. \ get(g:, 'airline#extensions#whitespace#skip_indent_check_ft', {}), 'force')
  64. if !exists('b:airline_whitespace_check')
  65. let b:airline_whitespace_check = ''
  66. let checks = get(b:, 'airline_whitespace_checks', get(g:, 'airline#extensions#whitespace#checks', s:default_checks))
  67. let trailing = 0
  68. let check = 'trailing'
  69. if index(checks, check) > -1 && index(get(skip_check_ft, &ft, []), check) < 0
  70. try
  71. let regexp = get(b:, 'airline_whitespace_trailing_regexp',
  72. \ get(g:, 'airline#extensions#whitespace#trailing_regexp', '\s$'))
  73. let trailing = search(regexp, 'nw')
  74. catch
  75. call airline#util#warning(printf('Whitespace: error occurred evaluating "%s"', regexp))
  76. echomsg v:exception
  77. return ''
  78. endtry
  79. endif
  80. let mixed = 0
  81. let check = 'indent'
  82. if index(checks, check) > -1 && index(get(skip_check_ft, &ft, []), check) < 0
  83. let mixed = s:check_mixed_indent()
  84. endif
  85. let mixed_file = ''
  86. let check = 'mixed-indent-file'
  87. if index(checks, check) > -1 && index(get(skip_check_ft, &ft, []), check) < 0
  88. let mixed_file = s:check_mixed_indent_file()
  89. endif
  90. let long = 0
  91. if index(checks, 'long') > -1 && &tw > 0
  92. let long = search('\%>'.&tw.'v.\+', 'nw')
  93. endif
  94. let conflicts = 0
  95. if index(checks, 'conflicts') > -1
  96. let conflicts = s:conflict_marker()
  97. endif
  98. if trailing != 0 || mixed != 0 || long != 0 || !empty(mixed_file) || conflicts != 0
  99. let b:airline_whitespace_check = s:symbol
  100. if strlen(s:symbol) > 0
  101. let space = (g:airline_symbols.space)
  102. else
  103. let space = ''
  104. endif
  105. if s:show_message
  106. if trailing != 0
  107. let trailing_fmt = get(g:, 'airline#extensions#whitespace#trailing_format', '[%s]trailing')
  108. let b:airline_whitespace_check .= space.printf(trailing_fmt, trailing)
  109. endif
  110. if mixed != 0
  111. let mixed_indent_fmt = get(g:, 'airline#extensions#whitespace#mixed_indent_format', '[%s]mixed-indent')
  112. let b:airline_whitespace_check .= space.printf(mixed_indent_fmt, mixed)
  113. endif
  114. if long != 0
  115. let long_fmt = get(g:, 'airline#extensions#whitespace#long_format', '[%s]long')
  116. let b:airline_whitespace_check .= space.printf(long_fmt, long)
  117. endif
  118. if !empty(mixed_file)
  119. let mixed_indent_file_fmt = get(g:, 'airline#extensions#whitespace#mixed_indent_file_format', '[%s]mix-indent-file')
  120. let b:airline_whitespace_check .= space.printf(mixed_indent_file_fmt, mixed_file)
  121. endif
  122. if conflicts != 0
  123. let conflicts_fmt = get(g:, 'airline#extensions#whitespace#conflicts_format', '[%s]conflicts')
  124. let b:airline_whitespace_check .= space.printf(conflicts_fmt, conflicts)
  125. endif
  126. endif
  127. endif
  128. endif
  129. return airline#util#shorten(b:airline_whitespace_check, 120, 9)
  130. endfunction
  131. function! airline#extensions#whitespace#toggle()
  132. if s:enabled
  133. augroup airline_whitespace
  134. autocmd!
  135. augroup END
  136. augroup! airline_whitespace
  137. let s:enabled = 0
  138. else
  139. call airline#extensions#whitespace#init()
  140. let s:enabled = 1
  141. endif
  142. if exists("g:airline#extensions#whitespace#enabled")
  143. let g:airline#extensions#whitespace#enabled = s:enabled
  144. if s:enabled && match(g:airline_section_warning, '#whitespace#check') < 0
  145. let g:airline_section_warning .= airline#section#create(['whitespace'])
  146. call airline#update_statusline()
  147. endif
  148. endif
  149. call airline#util#warning(printf('Whitespace checking: %s',(s:enabled ? 'Enabled' : 'Disabled')))
  150. endfunction
  151. function! airline#extensions#whitespace#disable()
  152. if s:enabled
  153. call airline#extensions#whitespace#toggle()
  154. endif
  155. endfunction
  156. function! airline#extensions#whitespace#init(...)
  157. call airline#parts#define_function('whitespace', 'airline#extensions#whitespace#check')
  158. unlet! b:airline_whitespace_check
  159. augroup airline_whitespace
  160. autocmd!
  161. autocmd CursorHold,BufWritePost * call <sid>ws_refresh()
  162. augroup END
  163. endfunction
  164. function! s:ws_refresh()
  165. if !exists('#airline')
  166. " airline disabled
  167. return
  168. endif
  169. if get(b:, 'airline_ws_changedtick', 0) == b:changedtick
  170. return
  171. endif
  172. unlet! b:airline_whitespace_check
  173. if get(g:, 'airline_skip_empty_sections', 0)
  174. exe ':AirlineRefresh!'
  175. endif
  176. let b:airline_ws_changedtick = b:changedtick
  177. endfunction