blob: e268e1fcd3d95200df4d081473fa9c4db5745eae [file] [log] [blame]
Bram Moolenaarbaca7f72013-09-22 14:42:24 +02001" Vim indent file
2" Language: J
3" Maintainer: David Bürgin <676c7473@gmail.com>
Bram Moolenaara6878372014-03-22 21:02:50 +01004" URL: https://github.com/glts/vim-j
Bram Moolenaar75b81562014-04-06 14:09:13 +02005" Last Change: 2014-04-05
Bram Moolenaarbaca7f72013-09-22 14:42:24 +02006
Bram Moolenaara6878372014-03-22 21:02:50 +01007if exists('b:did_indent')
Bram Moolenaarbaca7f72013-09-22 14:42:24 +02008 finish
9endif
10let b:did_indent = 1
11
12setlocal indentexpr=GetJIndent()
Bram Moolenaar75b81562014-04-06 14:09:13 +020013setlocal indentkeys-=0{,0},:,0#
14setlocal indentkeys+=0),0<:>,=case.,=catch.,=catchd.,=catcht.,=do.,=else.,=elseif.,=end.,=fcase.
Bram Moolenaarbaca7f72013-09-22 14:42:24 +020015
Bram Moolenaara6878372014-03-22 21:02:50 +010016let b:undo_indent = 'setlocal indentkeys< indentexpr<'
Bram Moolenaarbaca7f72013-09-22 14:42:24 +020017
Bram Moolenaara6878372014-03-22 21:02:50 +010018if exists('*GetJIndent')
Bram Moolenaarbaca7f72013-09-22 14:42:24 +020019 finish
20endif
21
Bram Moolenaar75b81562014-04-06 14:09:13 +020022" If g:j_indent_definitions is true, the bodies of explicit definitions of
23" adverbs, conjunctions, and verbs will be indented. Default is false (0).
24if !exists('g:j_indent_definitions')
25 let g:j_indent_definitions = 0
26endif
27
28function GetJIndent() abort
Bram Moolenaarbaca7f72013-09-22 14:42:24 +020029 let prevlnum = prevnonblank(v:lnum-1)
30 if prevlnum == 0
31 return 0
32 endif
Bram Moolenaarbaca7f72013-09-22 14:42:24 +020033 let indent = indent(prevlnum)
Bram Moolenaar75b81562014-04-06 14:09:13 +020034 let prevline = getline(prevlnum)
35 if prevline =~# '^\s*\%(case\|catch[dt]\=\|do\|else\%(if\)\=\|fcase\|for\%(_\a\k*\)\=\|if\|select\|try\|whil\%(e\|st\)\)\.\%(\%(\<end\.\)\@!.\)*$'
36 " Increase indentation after an initial control word that starts or
37 " continues a block and is not terminated by "end."
38 let indent += shiftwidth()
39 elseif g:j_indent_definitions && (prevline =~# '\<\%([1-4]\|13\|adverb\|conjunction\|verb\|monad\|dyad\)\s\+\%(:\s*0\|def\s\+0\|define\)\>' || prevline =~# '^\s*:\s*$')
40 " Increase indentation in explicit definitions of adverbs, conjunctions,
41 " and verbs
42 let indent += shiftwidth()
Bram Moolenaarbaca7f72013-09-22 14:42:24 +020043 endif
Bram Moolenaar75b81562014-04-06 14:09:13 +020044 " Decrease indentation in lines that start with either control words that
45 " continue or end a block, or the special items ")" and ":"
46 if getline(v:lnum) =~# '^\s*\%()\|:\|\%(case\|catch[dt]\=\|do\|else\%(if\)\=\|end\|fcase\)\.\)'
Bram Moolenaara6878372014-03-22 21:02:50 +010047 let indent -= shiftwidth()
Bram Moolenaarbaca7f72013-09-22 14:42:24 +020048 endif
49 return indent
50endfunction