blob: fb4fe2731085b75b869ad6aa16c087beec991559 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Description: Comshare Dimension Definition Language (CDL)
2" Author: Raul Segura Acevedo <raulseguraaceved@netscape.net>
3" Last Change: Fri Nov 30 13:35:48 2001 CST
4
5if exists("b:did_indent")
6 "finish
7endif
8let b:did_indent = 1
9
10setlocal indentexpr=CdlGetIndent(v:lnum)
11setlocal indentkeys&
12setlocal indentkeys+==~else,=~endif,=~then,;,),=
13
14" Only define the function once.
15if exists("*CdlGetIndent")
16 "finish
17endif
18
Bram Moolenaar6c391a72021-09-09 21:55:11 +020019" find out if an "...=..." expression is an assignment (or a conditional)
20" it scans 'line' first, and then the previous lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000021fun! CdlAsignment(lnum, line)
22 let f = -1
23 let lnum = a:lnum
24 let line = a:line
25 while lnum > 0 && f == -1
26 " line without members [a] of [b]:[c]...
27 let inicio = 0
28 while 1
29 " keywords that help to decide
30 let inicio = matchend(line, '\c\<\(expr\|\a*if\|and\|or\|not\|else\|then\|memberis\|\k\+of\)\>\|[<>;]', inicio)
31 if inicio < 0
32 break
33 endif
34 " it's formula if there's a ';', 'elsE', 'theN', 'enDif' or 'expr'
35 " conditional if there's a '<', '>', 'elseif', 'if', 'and', 'or', 'not',
Bram Moolenaar6c391a72021-09-09 21:55:11 +020036 " 'memberis', 'childrenof' and other \k\+of functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000037 let f = line[inicio-1] =~? '[en;]' || strpart(line, inicio-4, 4) =~? 'ndif\|expr'
38 endw
39 let lnum = prevnonblank(lnum-1)
40 let line = substitute(getline(lnum), '\c\(\[[^]]*]\(\s*of\s*\|:\)*\)\+', ' ', 'g')
41 endw
42 " if we hit the start of the file then f = -1, return 1 (formula)
43 return f != 0
44endf
45
46fun! CdlGetIndent(lnum)
47 let thisline = getline(a:lnum)
48 if match(thisline, '^\s*\(\k\+\|\[[^]]*]\)\s*\(,\|;\s*$\)') >= 0
49 " it's an attributes line
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020050 return shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000051 elseif match(thisline, '^\c\s*\([{}]\|\/[*/]\|dimension\|schedule\|group\|hierarchy\|class\)') >= 0
52 " it's a header or '{' or '}' or a comment
53 return 0
54 end
55
56 let lnum = prevnonblank(a:lnum-1)
57 " Hit the start of the file, use zero indent.
58 if lnum == 0
59 return 0
60 endif
61
62 " PREVIOUS LINE
63 let ind = indent(lnum)
64 let line = getline(lnum)
Bram Moolenaardad44732021-03-31 20:07:33 +020065
66 " Whether a '=' is a conditional or an assignment. -1 means we don't know
67 " yet.
68 " One 'closing' element at the beginning of the line has already reduced the
69 " indent, but 'else', 'elseif' & 'then' increment it for the next line.
70 " '=' at the beginning already has the right indent (increased for
71 " asignments).
72 let f = -1
Bram Moolenaar071d4272004-06-13 20:20:40 +000073 let inicio = matchend(line, '^\c\s*\(else\a*\|then\|endif\|/[*/]\|[);={]\)')
74 if inicio > 0
75 let c = line[inicio-1]
76 " ')' and '=' don't change indent and are useless to set 'f'
77 if c == '{'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020078 return shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 elseif c != ')' && c != '='
80 let f = 1 " all but 'elseif' are followed by a formula
81 if c ==? 'n' || c ==? 'e' " 'then', 'else'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020082 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 elseif strpart(line, inicio-6, 6) ==? 'elseif' " elseif, set f to conditional
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020084 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 let f = 0
86 end
87 end
88 end
89
90 " remove members [a] of [b]:[c]... (inicio remainds valid)
91 let line = substitute(line, '\c\(\[[^]]*]\(\s*of\s*\|:\)*\)\+', ' ', 'g')
92 while 1
93 " search for the next interesting element
94 let inicio=matchend(line, '\c\<if\|endif\|[()=;]', inicio)
95 if inicio < 0
96 break
97 end
98
99 let c = line[inicio-1]
100 " 'expr(...)' containing the formula
101 if strpart(line, inicio-5, 5) ==? 'expr('
102 let ind = 0
103 let f = 1
104 elseif c == ')' || c== ';' || strpart(line, inicio-5, 5) ==? 'endif'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200105 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 elseif c == '(' || c ==? 'f' " '(' or 'if'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200107 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108 else " c == '='
Bram Moolenaar6c391a72021-09-09 21:55:11 +0200109 " if it is an assignment increase indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110 if f == -1 " we don't know yet, find out
111 let f = CdlAsignment(lnum, strpart(line, 0, inicio))
112 end
113 if f == 1 " formula increase it
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200114 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 end
116 end
117 endw
118
119 " CURRENT LINE, if it starts with a closing element, decrease indent
Bram Moolenaar6c391a72021-09-09 21:55:11 +0200120 " or if it starts with '=' (assignment), increase indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 if match(thisline, '^\c\s*\(else\|then\|endif\|[);]\)') >= 0
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200122 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 elseif match(thisline, '^\s*=') >= 0
Bram Moolenaar6c391a72021-09-09 21:55:11 +0200124 if f == -1 " we don't know yet if is an assignment, find out
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 let f = CdlAsignment(lnum, "")
126 end
127 if f == 1 " formula increase it
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200128 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129 end
130 end
131
132 return ind
133endfun