chaiscript.vim 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. " Vim indent file
  2. " Language: ChaiScript
  3. " Maintainer: Jason Turner <lefticus 'at' gmail com>
  4. " Last Change: 2022 Apr 06
  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 indentexpr=GetChaiScriptIndent()
  11. setlocal autoindent
  12. let b:undo_indent = "setl ai< inde<"
  13. " Only define the function once.
  14. if exists("*GetChaiScriptIndent")
  15. finish
  16. endif
  17. function! GetChaiScriptIndent()
  18. " Find a non-blank line above the current line.
  19. let lnum = prevnonblank(v:lnum - 1)
  20. " Hit the start of the file, use zero indent.
  21. if lnum == 0
  22. return 0
  23. endif
  24. " Add a 'shiftwidth' after lines that start a block:
  25. " lines containing a {
  26. let ind = indent(lnum)
  27. let flag = 0
  28. let prevline = getline(lnum)
  29. if prevline =~ '^.*{.*'
  30. let ind = ind + shiftwidth()
  31. let flag = 1
  32. endif
  33. " Subtract a 'shiftwidth' after lines containing a { followed by a }
  34. " to keep it balanced
  35. if flag == 1 && prevline =~ '.*{.*}.*'
  36. let ind = ind - shiftwidth()
  37. endif
  38. " Subtract a 'shiftwidth' on lines ending with }
  39. if getline(v:lnum) =~ '^\s*\%(}\)'
  40. let ind = ind - shiftwidth()
  41. endif
  42. return ind
  43. endfunction