ld.vim 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. " Vim indent file
  2. " Language: ld(1) script
  3. " Previous Maintainer: Nikolai Weibull <now@bitwi.se>
  4. " Latest Revision: 2006-12-20
  5. if exists("b:did_indent")
  6. finish
  7. endif
  8. let b:did_indent = 1
  9. setlocal indentexpr=GetLDIndent()
  10. setlocal indentkeys=0{,0},!^F,o,O
  11. setlocal nosmartindent
  12. if exists("*GetLDIndent")
  13. finish
  14. endif
  15. function s:prevnonblanknoncomment(lnum)
  16. let lnum = a:lnum
  17. while lnum > 1
  18. let lnum = prevnonblank(lnum)
  19. let line = getline(lnum)
  20. if line =~ '\*/'
  21. while lnum > 1 && line !~ '/\*'
  22. let lnum -= 1
  23. endwhile
  24. if line =~ '^\s*/\*'
  25. let lnum -= 1
  26. else
  27. break
  28. endif
  29. else
  30. break
  31. endif
  32. endwhile
  33. return lnum
  34. endfunction
  35. function s:count_braces(lnum, count_open)
  36. let n_open = 0
  37. let n_close = 0
  38. let line = getline(a:lnum)
  39. let pattern = '[{}]'
  40. let i = match(line, pattern)
  41. while i != -1
  42. if synIDattr(synID(a:lnum, i + 1, 0), 'name') !~ 'ld\%(Comment\|String\)'
  43. if line[i] == '{'
  44. let n_open += 1
  45. elseif line[i] == '}'
  46. if n_open > 0
  47. let n_open -= 1
  48. else
  49. let n_close += 1
  50. endif
  51. endif
  52. endif
  53. let i = match(line, pattern, i + 1)
  54. endwhile
  55. return a:count_open ? n_open : n_close
  56. endfunction
  57. function GetLDIndent()
  58. let line = getline(v:lnum)
  59. if line =~ '^\s*\*'
  60. return cindent(v:lnum)
  61. elseif line =~ '^\s*}'
  62. return indent(v:lnum) - shiftwidth()
  63. endif
  64. let pnum = s:prevnonblanknoncomment(v:lnum - 1)
  65. if pnum == 0
  66. return 0
  67. endif
  68. let ind = indent(pnum) + s:count_braces(pnum, 1) * shiftwidth()
  69. let pline = getline(pnum)
  70. if pline =~ '}\s*$'
  71. let ind -= (s:count_braces(pnum, 0) - (pline =~ '^\s*}' ? 1 : 0)) * shiftwidth()
  72. endif
  73. return ind
  74. endfunction