blob: c308512eccd3edaf4b618af8c78619fa1adede73 [file] [log] [blame]
Bram Moolenaarbaca7f72013-09-22 14:42:24 +02001" Vim indent file
2" Language: J
Bram Moolenaar09c6f262019-11-17 15:55:14 +01003" Maintainer: David Bürgin <dbuergin@gluet.ch>
4" URL: https://gitlab.com/glts/vim-j
Bram Moolenaar83caecf2015-01-14 19:42:21 +01005" Last Change: 2015-01-11
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 Moolenaar83caecf2015-01-14 19:42:21 +010029 let l:prevlnum = prevnonblank(v:lnum - 1)
30 if l:prevlnum == 0
Bram Moolenaarbaca7f72013-09-22 14:42:24 +020031 return 0
32 endif
Bram Moolenaar83caecf2015-01-14 19:42:21 +010033 let l:indent = indent(l:prevlnum)
34 let l:prevline = getline(l:prevlnum)
35 if l:prevline =~# '^\s*\%(case\|catch[dt]\=\|do\|else\%(if\)\=\|fcase\|for\%(_\a\k*\)\=\|if\|select\|try\|whil\%(e\|st\)\)\.\%(\%(\<end\.\)\@!.\)*$'
Bram Moolenaar75b81562014-04-06 14:09:13 +020036 " Increase indentation after an initial control word that starts or
37 " continues a block and is not terminated by "end."
Bram Moolenaar83caecf2015-01-14 19:42:21 +010038 let l:indent += shiftwidth()
39 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*$')
Bram Moolenaar75b81562014-04-06 14:09:13 +020040 " Increase indentation in explicit definitions of adverbs, conjunctions,
41 " and verbs
Bram Moolenaar83caecf2015-01-14 19:42:21 +010042 let l: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 Moolenaar83caecf2015-01-14 19:42:21 +010047 let l:indent -= shiftwidth()
Bram Moolenaarbaca7f72013-09-22 14:42:24 +020048 endif
Bram Moolenaar83caecf2015-01-14 19:42:21 +010049 return l:indent
Bram Moolenaarbaca7f72013-09-22 14:42:24 +020050endfunction