bst.vim 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. " Vim indent file
  2. " Language: bst
  3. " Author: Tim Pope <vimNOSPAM@tpope.info>
  4. " $Id: bst.vim,v 1.1 2007/05/05 18:11:12 vimboss Exp $
  5. if exists("b:did_indent")
  6. finish
  7. endif
  8. let b:did_indent = 1
  9. setlocal expandtab
  10. setlocal indentexpr=GetBstIndent(v:lnum)
  11. "setlocal smartindent
  12. setlocal cinkeys&
  13. setlocal cinkeys-=0#
  14. setlocal indentkeys&
  15. "setlocal indentkeys+=0%
  16. " Only define the function once.
  17. if exists("*GetBstIndent")
  18. finish
  19. endif
  20. function! s:prevgood(lnum)
  21. " Find a non-blank line above the current line.
  22. " Skip over comments.
  23. let lnum = a:lnum
  24. while lnum > 0
  25. let lnum = prevnonblank(lnum - 1)
  26. if getline(lnum) !~ '^\s*%.*$'
  27. break
  28. endif
  29. endwhile
  30. return lnum
  31. endfunction
  32. function! s:strip(lnum)
  33. let line = getline(a:lnum)
  34. let line = substitute(line,'"[^"]*"','""','g')
  35. let line = substitute(line,'%.*','','')
  36. let line = substitute(line,'^\s\+','','')
  37. return line
  38. endfunction
  39. function! s:count(string,char)
  40. let str = substitute(a:string,'[^'.a:char.']','','g')
  41. return strlen(str)
  42. endfunction
  43. function! GetBstIndent(lnum) abort
  44. if a:lnum == 1
  45. return 0
  46. endif
  47. let lnum = s:prevgood(a:lnum)
  48. if lnum <= 0
  49. return indent(a:lnum - 1)
  50. endif
  51. let line = s:strip(lnum)
  52. let cline = s:strip(a:lnum)
  53. if cline =~ '^}' && exists("b:current_syntax")
  54. call cursor(a:lnum,indent(a:lnum))
  55. if searchpair('{','','}','bW',"synIDattr(synID(line('.'),col('.'),1),'name') =~? 'comment\\|string'")
  56. if col('.')+1 == col('$')
  57. return indent('.')
  58. else
  59. return virtcol('.')-1
  60. endif
  61. endif
  62. endif
  63. let fakeline = substitute(line,'^}','','').matchstr(cline,'^}')
  64. let ind = indent(lnum)
  65. let ind = ind + shiftwidth() * s:count(line,'{')
  66. let ind = ind - shiftwidth() * s:count(fakeline,'}')
  67. return ind
  68. endfunction