pov.vim 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. " Vim indent file
  2. " Language: PoV-Ray Scene Description Language
  3. " Maintainer: David Necas (Yeti) <yeti@physics.muni.cz>
  4. " Last Change: 2017 Jun 13
  5. " 2022 April: b:undo_indent added by Doug Kearns
  6. " URI: http://trific.ath.cx/Ftp/vim/indent/pov.vim
  7. " Only load this indent file when no other was loaded.
  8. if exists("b:did_indent")
  9. finish
  10. endif
  11. let b:did_indent = 1
  12. " Some preliminary settings.
  13. setlocal nolisp " Make sure lisp indenting doesn't supersede us.
  14. setlocal indentexpr=GetPoVRayIndent()
  15. setlocal indentkeys+==else,=end,0]
  16. let b:undo_indent = "setl inde< indk< lisp<"
  17. " Only define the function once.
  18. if exists("*GetPoVRayIndent")
  19. finish
  20. endif
  21. " Counts matches of a regexp <rexp> in line number <line>.
  22. " Doesn't count matches inside strings and comments (as defined by current
  23. " syntax).
  24. function! s:MatchCount(line, rexp)
  25. let str = getline(a:line)
  26. let i = 0
  27. let n = 0
  28. while i >= 0
  29. let i = matchend(str, a:rexp, i)
  30. if i >= 0 && synIDattr(synID(a:line, i, 0), "name") !~? "string\|comment"
  31. let n = n + 1
  32. endif
  33. endwhile
  34. return n
  35. endfunction
  36. " The main function. Returns indent amount.
  37. function GetPoVRayIndent()
  38. " If we are inside a comment (may be nested in obscure ways), give up
  39. if synIDattr(synID(v:lnum, indent(v:lnum)+1, 0), "name") =~? "string\|comment"
  40. return -1
  41. endif
  42. " Search backwards for the first non-empty, non-comment line.
  43. let plnum = prevnonblank(v:lnum - 1)
  44. let plind = indent(plnum)
  45. while plnum > 0 && synIDattr(synID(plnum, plind+1, 0), "name") =~? "comment"
  46. let plnum = prevnonblank(plnum - 1)
  47. let plind = indent(plnum)
  48. endwhile
  49. " Start indenting from zero
  50. if plnum == 0
  51. return 0
  52. endif
  53. " Analyse previous nonempty line.
  54. let chg = 0
  55. let chg = chg + s:MatchCount(plnum, '[[{(]')
  56. let chg = chg + s:MatchCount(plnum, '#\s*\%(if\|ifdef\|ifndef\|switch\|while\|macro\|else\)\>')
  57. let chg = chg - s:MatchCount(plnum, '#\s*end\>')
  58. let chg = chg - s:MatchCount(plnum, '[]})]')
  59. " Dirty hack for people writing #if and #else on the same line.
  60. let chg = chg - s:MatchCount(plnum, '#\s*\%(if\|ifdef\|ifndef\|switch\)\>.*#\s*else\>')
  61. " When chg > 0, then we opened groups and we should indent more, but when
  62. " chg < 0, we closed groups and this already affected the previous line,
  63. " so we should not dedent. And when everything else fails, scream.
  64. let chg = chg > 0 ? chg : 0
  65. " Analyse current line
  66. " FIXME: If we have to dedent, we should try to find the indentation of the
  67. " opening line.
  68. let cur = s:MatchCount(v:lnum, '^\s*\%(#\s*\%(end\|else\)\>\|[]})]\)')
  69. if cur > 0
  70. let final = plind + (chg - cur) * shiftwidth()
  71. else
  72. let final = plind + chg * shiftwidth()
  73. endif
  74. return final < 0 ? 0 : final
  75. endfunction