| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | " Description:	Comshare Dimension Definition Language (CDL) | 
					
						
							| 
									
										
										
										
											2021-10-04 21:32:54 +01:00
										 |  |  | " Maintainer:	Raul Segura Acevedo <raulseguraaceved@netscape.net> (Invalid email address) | 
					
						
							|  |  |  | " 		Doug Kearns <dougkearns@gmail.com> | 
					
						
							| 
									
										
										
										
											2022-04-08 17:45:08 +01:00
										 |  |  | " Last Change:	2022 Apr 06 | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if exists("b:did_indent") | 
					
						
							|  |  |  |     "finish | 
					
						
							|  |  |  | endif | 
					
						
							|  |  |  | let b:did_indent = 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | setlocal indentexpr=CdlGetIndent(v:lnum) | 
					
						
							|  |  |  | setlocal indentkeys& | 
					
						
							|  |  |  | setlocal indentkeys+==~else,=~endif,=~then,;,),= | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-08 17:45:08 +01:00
										 |  |  | let b:undo_indent = "setl inde< indk<" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | " Only define the function once. | 
					
						
							|  |  |  | if exists("*CdlGetIndent") | 
					
						
							|  |  |  |     "finish | 
					
						
							|  |  |  | endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 21:55:11 +02:00
										 |  |  | " find out if an "...=..." expression is an assignment (or a conditional) | 
					
						
							|  |  |  | " it scans 'line' first, and then the previous lines | 
					
						
							| 
									
										
										
										
											2023-10-16 09:53:37 +02:00
										 |  |  | fun! CdlAssignment(lnum, line) | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |   let f = -1 | 
					
						
							|  |  |  |   let lnum = a:lnum | 
					
						
							|  |  |  |   let line = a:line | 
					
						
							|  |  |  |   while lnum > 0 && f == -1 | 
					
						
							|  |  |  |     " line without members [a] of [b]:[c]... | 
					
						
							|  |  |  |     let inicio = 0 | 
					
						
							|  |  |  |     while 1 | 
					
						
							|  |  |  |       " keywords that help to decide | 
					
						
							|  |  |  |       let inicio = matchend(line, '\c\<\(expr\|\a*if\|and\|or\|not\|else\|then\|memberis\|\k\+of\)\>\|[<>;]', inicio) | 
					
						
							|  |  |  |       if inicio < 0 | 
					
						
							|  |  |  | 	break | 
					
						
							|  |  |  |       endif | 
					
						
							|  |  |  |       " it's formula if there's a ';', 'elsE', 'theN', 'enDif' or 'expr' | 
					
						
							|  |  |  |       " conditional if there's a '<', '>', 'elseif', 'if', 'and', 'or', 'not', | 
					
						
							| 
									
										
										
										
											2021-09-09 21:55:11 +02:00
										 |  |  |       " 'memberis', 'childrenof' and other \k\+of functions | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |       let f = line[inicio-1] =~? '[en;]' || strpart(line, inicio-4, 4) =~? 'ndif\|expr' | 
					
						
							|  |  |  |     endw | 
					
						
							|  |  |  |     let lnum = prevnonblank(lnum-1) | 
					
						
							|  |  |  |     let line = substitute(getline(lnum), '\c\(\[[^]]*]\(\s*of\s*\|:\)*\)\+', ' ', 'g') | 
					
						
							|  |  |  |   endw | 
					
						
							|  |  |  |   " if we hit the start of the file then f = -1, return 1 (formula) | 
					
						
							|  |  |  |   return f != 0 | 
					
						
							|  |  |  | endf | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | fun! CdlGetIndent(lnum) | 
					
						
							|  |  |  |   let thisline = getline(a:lnum) | 
					
						
							|  |  |  |   if match(thisline, '^\s*\(\k\+\|\[[^]]*]\)\s*\(,\|;\s*$\)') >= 0 | 
					
						
							|  |  |  |     " it's an attributes line | 
					
						
							| 
									
										
										
										
											2017-06-13 18:12:01 +02:00
										 |  |  |     return shiftwidth() | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |   elseif match(thisline, '^\c\s*\([{}]\|\/[*/]\|dimension\|schedule\|group\|hierarchy\|class\)') >= 0 | 
					
						
							|  |  |  |     " it's a header or '{' or '}' or a comment | 
					
						
							|  |  |  |     return 0 | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let lnum = prevnonblank(a:lnum-1) | 
					
						
							|  |  |  |   " Hit the start of the file, use zero indent. | 
					
						
							|  |  |  |   if lnum == 0 | 
					
						
							|  |  |  |     return 0 | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   " PREVIOUS LINE | 
					
						
							|  |  |  |   let ind = indent(lnum) | 
					
						
							|  |  |  |   let line = getline(lnum) | 
					
						
							| 
									
										
										
										
											2021-03-31 20:07:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   " Whether a '=' is a conditional or an assignment. -1 means we don't know | 
					
						
							|  |  |  |   " yet. | 
					
						
							|  |  |  |   " One 'closing' element at the beginning of the line has already reduced the | 
					
						
							|  |  |  |   " indent, but 'else', 'elseif' & 'then' increment it for the next line. | 
					
						
							|  |  |  |   " '=' at the beginning already has the right indent (increased for | 
					
						
							|  |  |  |   " asignments). | 
					
						
							|  |  |  |   let f = -1 | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |   let inicio = matchend(line, '^\c\s*\(else\a*\|then\|endif\|/[*/]\|[);={]\)') | 
					
						
							|  |  |  |   if inicio > 0 | 
					
						
							|  |  |  |     let c = line[inicio-1] | 
					
						
							|  |  |  |     " ')' and '=' don't change indent and are useless to set 'f' | 
					
						
							|  |  |  |     if c == '{' | 
					
						
							| 
									
										
										
										
											2017-06-13 18:12:01 +02:00
										 |  |  |       return shiftwidth() | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |     elseif c != ')' && c != '=' | 
					
						
							|  |  |  |       let f = 1 " all but 'elseif' are followed by a formula | 
					
						
							|  |  |  |       if c ==? 'n' || c ==? 'e' " 'then', 'else' | 
					
						
							| 
									
										
										
										
											2017-06-13 18:12:01 +02:00
										 |  |  | 	let ind = ind + shiftwidth() | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |       elseif strpart(line, inicio-6, 6) ==? 'elseif' " elseif, set f to conditional | 
					
						
							| 
									
										
										
										
											2017-06-13 18:12:01 +02:00
										 |  |  | 	let ind = ind + shiftwidth() | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | 	let f = 0 | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-16 09:53:37 +02:00
										 |  |  |   " remove members [a] of [b]:[c]... (inicio remains valid) | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |   let line = substitute(line, '\c\(\[[^]]*]\(\s*of\s*\|:\)*\)\+', ' ', 'g') | 
					
						
							|  |  |  |   while 1 | 
					
						
							|  |  |  |     " search for the next interesting element | 
					
						
							|  |  |  |     let inicio=matchend(line, '\c\<if\|endif\|[()=;]', inicio) | 
					
						
							|  |  |  |     if inicio < 0 | 
					
						
							|  |  |  |       break | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let c = line[inicio-1] | 
					
						
							|  |  |  |     " 'expr(...)' containing the formula | 
					
						
							|  |  |  |     if strpart(line, inicio-5, 5) ==? 'expr(' | 
					
						
							|  |  |  |       let ind = 0 | 
					
						
							|  |  |  |       let f = 1 | 
					
						
							|  |  |  |     elseif c == ')' || c== ';' || strpart(line, inicio-5, 5) ==? 'endif' | 
					
						
							| 
									
										
										
										
											2017-06-13 18:12:01 +02:00
										 |  |  |       let ind = ind - shiftwidth() | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |     elseif c == '(' || c ==? 'f' " '(' or 'if' | 
					
						
							| 
									
										
										
										
											2017-06-13 18:12:01 +02:00
										 |  |  |       let ind = ind + shiftwidth() | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |     else " c == '=' | 
					
						
							| 
									
										
										
										
											2021-09-09 21:55:11 +02:00
										 |  |  |       " if it is an assignment increase indent | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |       if f == -1 " we don't know yet, find out | 
					
						
							| 
									
										
										
										
											2023-10-16 09:53:37 +02:00
										 |  |  | 	let f = CdlAssignment(lnum, strpart(line, 0, inicio)) | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |       if f == 1 " formula increase it | 
					
						
							| 
									
										
										
										
											2017-06-13 18:12:01 +02:00
										 |  |  | 	let ind = ind + shiftwidth() | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   endw | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   " CURRENT LINE, if it starts with a closing element, decrease indent | 
					
						
							| 
									
										
										
										
											2021-09-09 21:55:11 +02:00
										 |  |  |   " or if it starts with '=' (assignment), increase indent | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |   if match(thisline, '^\c\s*\(else\|then\|endif\|[);]\)') >= 0 | 
					
						
							| 
									
										
										
										
											2017-06-13 18:12:01 +02:00
										 |  |  |     let ind = ind - shiftwidth() | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |   elseif match(thisline, '^\s*=') >= 0 | 
					
						
							| 
									
										
										
										
											2021-09-09 21:55:11 +02:00
										 |  |  |     if f == -1 " we don't know yet if is an assignment, find out | 
					
						
							| 
									
										
										
										
											2023-10-16 09:53:37 +02:00
										 |  |  |       let f = CdlAssignment(lnum, "") | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |     if f == 1 " formula increase it | 
					
						
							| 
									
										
										
										
											2017-06-13 18:12:01 +02:00
										 |  |  |       let ind = ind + shiftwidth() | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ind | 
					
						
							|  |  |  | endfun |