blob: da675698cf718e07a4f89746f4e345fc6c36a265 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Description: Comshare Dimension Definition Language (CDL)
Bram Moolenaar6e649222021-10-04 21:32:54 +01002" Maintainer: Raul Segura Acevedo <raulseguraaceved@netscape.net> (Invalid email address)
3" Doug Kearns <dougkearns@gmail.com>
Bram Moolenaarcbaff5e2022-04-08 17:45:08 +01004" Last Change: 2022 Apr 06
Bram Moolenaar071d4272004-06-13 20:20:40 +00005
6if exists("b:did_indent")
7 "finish
8endif
9let b:did_indent = 1
10
11setlocal indentexpr=CdlGetIndent(v:lnum)
12setlocal indentkeys&
13setlocal indentkeys+==~else,=~endif,=~then,;,),=
14
Bram Moolenaarcbaff5e2022-04-08 17:45:08 +010015let b:undo_indent = "setl inde< indk<"
16
Bram Moolenaar071d4272004-06-13 20:20:40 +000017" Only define the function once.
18if exists("*CdlGetIndent")
19 "finish
20endif
21
Bram Moolenaar6c391a72021-09-09 21:55:11 +020022" find out if an "...=..." expression is an assignment (or a conditional)
23" it scans 'line' first, and then the previous lines
Viktor Szépedbf749b2023-10-16 09:53:37 +020024fun! CdlAssignment(lnum, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025 let f = -1
26 let lnum = a:lnum
27 let line = a:line
28 while lnum > 0 && f == -1
29 " line without members [a] of [b]:[c]...
30 let inicio = 0
31 while 1
32 " keywords that help to decide
33 let inicio = matchend(line, '\c\<\(expr\|\a*if\|and\|or\|not\|else\|then\|memberis\|\k\+of\)\>\|[<>;]', inicio)
34 if inicio < 0
35 break
36 endif
37 " it's formula if there's a ';', 'elsE', 'theN', 'enDif' or 'expr'
38 " conditional if there's a '<', '>', 'elseif', 'if', 'and', 'or', 'not',
Bram Moolenaar6c391a72021-09-09 21:55:11 +020039 " 'memberis', 'childrenof' and other \k\+of functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000040 let f = line[inicio-1] =~? '[en;]' || strpart(line, inicio-4, 4) =~? 'ndif\|expr'
41 endw
42 let lnum = prevnonblank(lnum-1)
43 let line = substitute(getline(lnum), '\c\(\[[^]]*]\(\s*of\s*\|:\)*\)\+', ' ', 'g')
44 endw
45 " if we hit the start of the file then f = -1, return 1 (formula)
46 return f != 0
47endf
48
49fun! CdlGetIndent(lnum)
50 let thisline = getline(a:lnum)
51 if match(thisline, '^\s*\(\k\+\|\[[^]]*]\)\s*\(,\|;\s*$\)') >= 0
52 " it's an attributes line
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020053 return shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000054 elseif match(thisline, '^\c\s*\([{}]\|\/[*/]\|dimension\|schedule\|group\|hierarchy\|class\)') >= 0
55 " it's a header or '{' or '}' or a comment
56 return 0
57 end
58
59 let lnum = prevnonblank(a:lnum-1)
60 " Hit the start of the file, use zero indent.
61 if lnum == 0
62 return 0
63 endif
64
65 " PREVIOUS LINE
66 let ind = indent(lnum)
67 let line = getline(lnum)
Bram Moolenaardad44732021-03-31 20:07:33 +020068
69 " Whether a '=' is a conditional or an assignment. -1 means we don't know
70 " yet.
71 " One 'closing' element at the beginning of the line has already reduced the
72 " indent, but 'else', 'elseif' & 'then' increment it for the next line.
73 " '=' at the beginning already has the right indent (increased for
74 " asignments).
75 let f = -1
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 let inicio = matchend(line, '^\c\s*\(else\a*\|then\|endif\|/[*/]\|[);={]\)')
77 if inicio > 0
78 let c = line[inicio-1]
79 " ')' and '=' don't change indent and are useless to set 'f'
80 if c == '{'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020081 return shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 elseif c != ')' && c != '='
83 let f = 1 " all but 'elseif' are followed by a formula
84 if c ==? 'n' || c ==? 'e' " 'then', 'else'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020085 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 elseif strpart(line, inicio-6, 6) ==? 'elseif' " elseif, set f to conditional
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020087 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 let f = 0
89 end
90 end
91 end
92
Viktor Szépedbf749b2023-10-16 09:53:37 +020093 " remove members [a] of [b]:[c]... (inicio remains valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 let line = substitute(line, '\c\(\[[^]]*]\(\s*of\s*\|:\)*\)\+', ' ', 'g')
95 while 1
96 " search for the next interesting element
97 let inicio=matchend(line, '\c\<if\|endif\|[()=;]', inicio)
98 if inicio < 0
99 break
100 end
101
102 let c = line[inicio-1]
103 " 'expr(...)' containing the formula
104 if strpart(line, inicio-5, 5) ==? 'expr('
105 let ind = 0
106 let f = 1
107 elseif c == ')' || c== ';' || strpart(line, inicio-5, 5) ==? 'endif'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200108 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 elseif c == '(' || c ==? 'f' " '(' or 'if'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200110 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 else " c == '='
Bram Moolenaar6c391a72021-09-09 21:55:11 +0200112 " if it is an assignment increase indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113 if f == -1 " we don't know yet, find out
Viktor Szépedbf749b2023-10-16 09:53:37 +0200114 let f = CdlAssignment(lnum, strpart(line, 0, inicio))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 end
116 if f == 1 " formula increase it
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200117 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 end
119 end
120 endw
121
122 " CURRENT LINE, if it starts with a closing element, decrease indent
Bram Moolenaar6c391a72021-09-09 21:55:11 +0200123 " or if it starts with '=' (assignment), increase indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 if match(thisline, '^\c\s*\(else\|then\|endif\|[);]\)') >= 0
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200125 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126 elseif match(thisline, '^\s*=') >= 0
Bram Moolenaar6c391a72021-09-09 21:55:11 +0200127 if f == -1 " we don't know yet if is an assignment, find out
Viktor Szépedbf749b2023-10-16 09:53:37 +0200128 let f = CdlAssignment(lnum, "")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129 end
130 if f == 1 " formula increase it
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200131 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 end
133 end
134
135 return ind
136endfun