ld.vim 1.8 KB

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