prolog.vim 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. " vim: set sw=4 sts=4:
  2. " Language: Prolog
  3. " Maintainer: Gergely Kontra <kgergely@mcl.hu> (Invalid email address)
  4. " Doug Kearns <dougkearns@gmail.com>
  5. " Revised on: 2002.02.18. 23:34:05
  6. " Last change by: Takuya Fujiwara, 2018 Sep 23
  7. " 2022 April: b:undo_indent added by Doug Kearns
  8. " TODO:
  9. " checking with respect to syntax highlighting
  10. " ignoring multiline comments
  11. " detecting multiline strings
  12. " Only load this indent file when no other was loaded.
  13. if exists("b:did_indent")
  14. finish
  15. endif
  16. let b:did_indent = 1
  17. setlocal indentexpr=GetPrologIndent()
  18. setlocal indentkeys-=:,0#
  19. setlocal indentkeys+=0%,-,0;,>,0)
  20. let b:undo_indent = "setl inde< indk<"
  21. " Only define the function once.
  22. "if exists("*GetPrologIndent")
  23. " finish
  24. "endif
  25. function! GetPrologIndent()
  26. " Find a non-blank line above the current line.
  27. let pnum = prevnonblank(v:lnum - 1)
  28. " Hit the start of the file, use zero indent.
  29. if pnum == 0
  30. return 0
  31. endif
  32. let line = getline(v:lnum)
  33. let pline = getline(pnum)
  34. let ind = indent(pnum)
  35. " Previous line was comment -> use previous line's indent
  36. if pline =~ '^\s*%'
  37. return ind
  38. endif
  39. " Previous line was the start of block comment -> +1 after '/*' comment
  40. if pline =~ '^\s*/\*'
  41. return ind + 1
  42. endif
  43. " Previous line was the end of block comment -> -1 after '*/' comment
  44. if pline =~ '^\s*\*/'
  45. return ind - 1
  46. endif
  47. " Check for clause head on previous line
  48. if pline =~ '\%(:-\|-->\)\s*\(%.*\)\?$'
  49. let ind = ind + shiftwidth()
  50. " Check for end of clause on previous line
  51. elseif pline =~ '\.\s*\(%.*\)\?$'
  52. let ind = ind - shiftwidth()
  53. endif
  54. " Check for opening conditional on previous line
  55. if pline =~ '^\s*\([(;]\|->\)'
  56. let ind = ind + shiftwidth()
  57. endif
  58. " Check for closing an unclosed paren, or middle ; or ->
  59. if line =~ '^\s*\([);]\|->\)'
  60. let ind = ind - shiftwidth()
  61. endif
  62. return ind
  63. endfunction