blob: 08369e42e55201f4ce6f99a94e37893016a2568f [file] [log] [blame]
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +02001" Vim indent file
2" Language: Zimbu
Christian Brabandte978b452023-08-13 10:33:05 +02003" Maintainer: The Vim Project <https://github.com/vim/vim>
4" Last Change: 2023 Aug 10
5" Former Maintainer: Bram Moolenaar <Bram@vim.org>
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +02006
7" Only load this indent file when no other was loaded.
8if exists("b:did_indent")
9 finish
10endif
11let b:did_indent = 1
12
13setlocal ai nolisp nocin
14setlocal indentexpr=GetZimbuIndent(v:lnum)
15setlocal indentkeys=0{,0},!^F,o,O,0=ELSE,0=ELSEIF,0=CASE,0=DEFAULT,0=FINALLY
16
17" We impose recommended defaults: no Tabs, 'shiftwidth' = 2
18setlocal sw=2 et
19
Bram Moolenaar6e649222021-10-04 21:32:54 +010020let b:undo_indent = "setl ai< cin< et< indentkeys< indentexpr< lisp< sw<"
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +020021
22" Only define the function once.
23if exists("*GetZimbuIndent")
24 finish
25endif
26
Bram Moolenaar8e52a592012-05-18 21:49:28 +020027let s:cpo_save = &cpo
28set cpo&vim
29
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +020030" Come here when loading the script the first time.
31
32let s:maxoff = 50 " maximum number of lines to look backwards for ()
33
34func GetZimbuIndent(lnum)
35 let prevLnum = prevnonblank(a:lnum - 1)
36 if prevLnum == 0
37 " This is the first non-empty line, use zero indent.
38 return 0
39 endif
40
41 " Taken from Python indenting:
42 " If the previous line is inside parenthesis, use the indent of the starting
43 " line.
44 " Trick: use the non-existing "dummy" variable to break out of the loop when
45 " going too far back.
46 call cursor(prevLnum, 1)
47 let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW',
48 \ "line('.') < " . (prevLnum - s:maxoff) . " ? dummy :"
49 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
50 \ . " =~ '\\(Comment\\|String\\|Char\\)$'")
51 if parlnum > 0
52 let plindent = indent(parlnum)
53 let plnumstart = parlnum
54 else
55 let plindent = indent(prevLnum)
56 let plnumstart = prevLnum
57 endif
58
59
60 " When inside parenthesis: If at the first line below the parenthesis add
61 " two 'shiftwidth', otherwise same as previous line.
62 " i = (a
63 " + b
64 " + c)
65 call cursor(a:lnum, 1)
66 let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
67 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
68 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
69 \ . " =~ '\\(Comment\\|String\\|Char\\)$'")
70 if p > 0
71 if p == prevLnum
72 " When the start is inside parenthesis, only indent one 'shiftwidth'.
73 let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
74 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
75 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
76 \ . " =~ '\\(Comment\\|String\\|Char\\)$'")
77 if pp > 0
Bram Moolenaar298b4402016-01-28 22:38:53 +010078 return indent(prevLnum) + shiftwidth()
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +020079 endif
Bram Moolenaar298b4402016-01-28 22:38:53 +010080 return indent(prevLnum) + shiftwidth() * 2
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +020081 endif
82 if plnumstart == p
83 return indent(prevLnum)
84 endif
85 return plindent
86 endif
87
88 let prevline = getline(prevLnum)
89 let thisline = getline(a:lnum)
90
91 " If this line is not a comment and the previous one is then move the
92 " previous line further back.
93 if thisline !~ '^\s*#'
94 while prevline =~ '^\s*#'
95 let prevLnum = prevnonblank(prevLnum - 1)
96 if prevLnum == 0
97 " Only comment lines before this, no indent
98 return 0
99 endif
100 let prevline = getline(prevLnum)
101 let plindent = indent(prevLnum)
102 endwhile
103 endif
104
Bram Moolenaard09acef2012-09-21 14:54:30 +0200105 if prevline =~ '^\s*\(IF\|\|ELSEIF\|ELSE\|GENERATE_IF\|\|GENERATE_ELSEIF\|GENERATE_ELSE\|WHILE\|REPEAT\|TRY\|CATCH\|FINALLY\|FOR\|DO\|SWITCH\|CASE\|DEFAULT\|FUNC\|VIRTUAL\|ABSTRACT\|DEFINE\|REPLACE\|FINAL\|PROC\|MAIN\|NEW\|ENUM\|CLASS\|INTERFACE\|BITS\|MODULE\|SHARED\)\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100106 let plindent += shiftwidth()
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200107 endif
108 if thisline =~ '^\s*\(}\|ELSEIF\>\|ELSE\>\|CATCH\|FINALLY\|GENERATE_ELSEIF\>\|GENERATE_ELSE\>\|UNTIL\>\)'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100109 let plindent -= shiftwidth()
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200110 endif
111 if thisline =~ '^\s*\(CASE\>\|DEFAULT\>\)' && prevline !~ '^\s*SWITCH\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100112 let plindent -= shiftwidth()
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200113 endif
114
115 " line up continued comment that started after some code
116 " String something # comment comment
117 " # comment
118 if a:lnum == prevLnum + 1 && thisline =~ '^\s*#' && prevline !~ '^\s*#'
119 let n = match(prevline, '#')
120 if n > 1
121 let plindent = n
122 endif
123 endif
124
125 return plindent
126endfunc
127
Bram Moolenaar8e52a592012-05-18 21:49:28 +0200128let &cpo = s:cpo_save
129unlet s:cpo_save