zimbu.vim 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. " Vim indent file
  2. " Language: Zimbu
  3. " Maintainer: Bram Moolenaar <Bram@vim.org>
  4. " Last Change: 2021 Sep 26
  5. " Only load this indent file when no other was loaded.
  6. if exists("b:did_indent")
  7. finish
  8. endif
  9. let b:did_indent = 1
  10. setlocal ai nolisp nocin
  11. setlocal indentexpr=GetZimbuIndent(v:lnum)
  12. setlocal indentkeys=0{,0},!^F,o,O,0=ELSE,0=ELSEIF,0=CASE,0=DEFAULT,0=FINALLY
  13. " We impose recommended defaults: no Tabs, 'shiftwidth' = 2
  14. setlocal sw=2 et
  15. let b:undo_indent = "setl ai< cin< et< indentkeys< indentexpr< lisp< sw<"
  16. " Only define the function once.
  17. if exists("*GetZimbuIndent")
  18. finish
  19. endif
  20. let s:cpo_save = &cpo
  21. set cpo&vim
  22. " Come here when loading the script the first time.
  23. let s:maxoff = 50 " maximum number of lines to look backwards for ()
  24. func GetZimbuIndent(lnum)
  25. let prevLnum = prevnonblank(a:lnum - 1)
  26. if prevLnum == 0
  27. " This is the first non-empty line, use zero indent.
  28. return 0
  29. endif
  30. " Taken from Python indenting:
  31. " If the previous line is inside parenthesis, use the indent of the starting
  32. " line.
  33. " Trick: use the non-existing "dummy" variable to break out of the loop when
  34. " going too far back.
  35. call cursor(prevLnum, 1)
  36. let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW',
  37. \ "line('.') < " . (prevLnum - s:maxoff) . " ? dummy :"
  38. \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
  39. \ . " =~ '\\(Comment\\|String\\|Char\\)$'")
  40. if parlnum > 0
  41. let plindent = indent(parlnum)
  42. let plnumstart = parlnum
  43. else
  44. let plindent = indent(prevLnum)
  45. let plnumstart = prevLnum
  46. endif
  47. " When inside parenthesis: If at the first line below the parenthesis add
  48. " two 'shiftwidth', otherwise same as previous line.
  49. " i = (a
  50. " + b
  51. " + c)
  52. call cursor(a:lnum, 1)
  53. let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
  54. \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
  55. \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
  56. \ . " =~ '\\(Comment\\|String\\|Char\\)$'")
  57. if p > 0
  58. if p == prevLnum
  59. " When the start is inside parenthesis, only indent one 'shiftwidth'.
  60. let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
  61. \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
  62. \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
  63. \ . " =~ '\\(Comment\\|String\\|Char\\)$'")
  64. if pp > 0
  65. return indent(prevLnum) + shiftwidth()
  66. endif
  67. return indent(prevLnum) + shiftwidth() * 2
  68. endif
  69. if plnumstart == p
  70. return indent(prevLnum)
  71. endif
  72. return plindent
  73. endif
  74. let prevline = getline(prevLnum)
  75. let thisline = getline(a:lnum)
  76. " If this line is not a comment and the previous one is then move the
  77. " previous line further back.
  78. if thisline !~ '^\s*#'
  79. while prevline =~ '^\s*#'
  80. let prevLnum = prevnonblank(prevLnum - 1)
  81. if prevLnum == 0
  82. " Only comment lines before this, no indent
  83. return 0
  84. endif
  85. let prevline = getline(prevLnum)
  86. let plindent = indent(prevLnum)
  87. endwhile
  88. endif
  89. if prevline =~ '^\s*\(IF\|\|ELSEIF\|ELSE\|GENERATE_IF\|\|GENERATE_ELSEIF\|GENERATE_ELSE\|WHILE\|REPEAT\|TRY\|CATCH\|FINALLY\|FOR\|DO\|SWITCH\|CASE\|DEFAULT\|FUNC\|VIRTUAL\|ABSTRACT\|DEFINE\|REPLACE\|FINAL\|PROC\|MAIN\|NEW\|ENUM\|CLASS\|INTERFACE\|BITS\|MODULE\|SHARED\)\>'
  90. let plindent += shiftwidth()
  91. endif
  92. if thisline =~ '^\s*\(}\|ELSEIF\>\|ELSE\>\|CATCH\|FINALLY\|GENERATE_ELSEIF\>\|GENERATE_ELSE\>\|UNTIL\>\)'
  93. let plindent -= shiftwidth()
  94. endif
  95. if thisline =~ '^\s*\(CASE\>\|DEFAULT\>\)' && prevline !~ '^\s*SWITCH\>'
  96. let plindent -= shiftwidth()
  97. endif
  98. " line up continued comment that started after some code
  99. " String something # comment comment
  100. " # comment
  101. if a:lnum == prevLnum + 1 && thisline =~ '^\s*#' && prevline !~ '^\s*#'
  102. let n = match(prevline, '#')
  103. if n > 1
  104. let plindent = n
  105. endif
  106. endif
  107. return plindent
  108. endfunc
  109. let &cpo = s:cpo_save
  110. unlet s:cpo_save