Guest User

Untitled

a guest
Sep 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. if exists("b:did_indent")
  2. finish
  3. endif
  4.  
  5.  
  6. function! s:CountMatches(line, pats)
  7. let matches = 0
  8. for p in a:pats
  9. let idx = 0
  10. while idx >= 0
  11. let idx = match(a:line, p, idx)
  12. if idx > 0
  13. let matches = matches + 1
  14. let idx = idx + strlen(p)
  15. endif
  16. endwhile
  17. endfor
  18. return matches
  19. endfunction
  20.  
  21. function! GetMyrIndent(ln)
  22. let ln = a:ln
  23.  
  24. if ln == 1
  25. let ind = 0
  26. else
  27. let prevln = getline(ln - 1)
  28. let curln = getline(ln)
  29.  
  30. let inpat = ['if','while','for',
  31. \ 'pkg','trait',
  32. \ 'struct', 'union',
  33. \ '{', ':\s*$']
  34. let outpat = ['}',';;']
  35. let outalone = '^\(\s*}\s*\|\s*;;\s*\)$'
  36. let ind = indent(ln - 1)
  37. let width = &tabstop
  38.  
  39. let n_in = s:CountMatches(prevln, inpat)
  40. let n_out = s:CountMatches(prevln, outpat)
  41. if curln =~ outalone
  42. let ind = ind - &tabstop
  43. elseif prevln !~ outalone
  44. let ind = ind + (n_in - n_out) * &tabstop
  45. endif
  46. endif
  47. return ind
  48. endfunction
  49.  
  50. set indentkeys+=0},;
  51. set indentexpr=GetMyrIndent(v:lnum)
  52.  
  53. let b:did_indent = 1
Add Comment
Please, Sign In to add comment