j.vim 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. " Vim indent file
  2. " Language: J
  3. " Maintainer: David Bürgin <dbuergin@gluet.ch>
  4. " URL: https://gitlab.com/glts/vim-j
  5. " Last Change: 2015-01-11
  6. if exists('b:did_indent')
  7. finish
  8. endif
  9. let b:did_indent = 1
  10. setlocal indentexpr=GetJIndent()
  11. setlocal indentkeys-=0{,0},:,0#
  12. setlocal indentkeys+=0),0<:>,=case.,=catch.,=catchd.,=catcht.,=do.,=else.,=elseif.,=end.,=fcase.
  13. let b:undo_indent = 'setlocal indentkeys< indentexpr<'
  14. if exists('*GetJIndent')
  15. finish
  16. endif
  17. " If g:j_indent_definitions is true, the bodies of explicit definitions of
  18. " adverbs, conjunctions, and verbs will be indented. Default is false (0).
  19. if !exists('g:j_indent_definitions')
  20. let g:j_indent_definitions = 0
  21. endif
  22. function GetJIndent() abort
  23. let l:prevlnum = prevnonblank(v:lnum - 1)
  24. if l:prevlnum == 0
  25. return 0
  26. endif
  27. let l:indent = indent(l:prevlnum)
  28. let l:prevline = getline(l:prevlnum)
  29. if l:prevline =~# '^\s*\%(case\|catch[dt]\=\|do\|else\%(if\)\=\|fcase\|for\%(_\a\k*\)\=\|if\|select\|try\|whil\%(e\|st\)\)\.\%(\%(\<end\.\)\@!.\)*$'
  30. " Increase indentation after an initial control word that starts or
  31. " continues a block and is not terminated by "end."
  32. let l:indent += shiftwidth()
  33. elseif g:j_indent_definitions && (l:prevline =~# '\<\%([1-4]\|13\|adverb\|conjunction\|verb\|monad\|dyad\)\s\+\%(:\s*0\|def\s\+0\|define\)\>' || l:prevline =~# '^\s*:\s*$')
  34. " Increase indentation in explicit definitions of adverbs, conjunctions,
  35. " and verbs
  36. let l:indent += shiftwidth()
  37. endif
  38. " Decrease indentation in lines that start with either control words that
  39. " continue or end a block, or the special items ")" and ":"
  40. if getline(v:lnum) =~# '^\s*\%()\|:\|\%(case\|catch[dt]\=\|do\|else\%(if\)\=\|end\|fcase\)\.\)'
  41. let l:indent -= shiftwidth()
  42. endif
  43. return l:indent
  44. endfunction