Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " 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 | |
| 5 | if exists("b:did_indent") |
| 6 | "finish |
| 7 | endif |
| 8 | let b:did_indent = 1 |
| 9 | |
| 10 | setlocal indentexpr=CdlGetIndent(v:lnum) |
| 11 | setlocal indentkeys& |
| 12 | setlocal indentkeys+==~else,=~endif,=~then,;,),= |
| 13 | |
| 14 | " Only define the function once. |
| 15 | if exists("*CdlGetIndent") |
| 16 | "finish |
| 17 | endif |
| 18 | |
Bram Moolenaar | 3e496b0 | 2016-09-25 22:11:48 +0200 | [diff] [blame] | 19 | " find out if an "...=..." expresion is an assignment (or a conditional) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 20 | " it scans 'line' first, and then the previos lines |
| 21 | fun! 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', |
| 36 | " 'memberis', 'childrenof' and other \k\+of funcions |
| 37 | 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 |
| 44 | endf |
| 45 | |
| 46 | fun! CdlGetIndent(lnum) |
| 47 | let thisline = getline(a:lnum) |
| 48 | if match(thisline, '^\s*\(\k\+\|\[[^]]*]\)\s*\(,\|;\s*$\)') >= 0 |
| 49 | " it's an attributes line |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 50 | return shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 51 | 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) |
| 65 | let f = -1 " wether a '=' is a conditional or a asignment, -1 means we don't know yet |
| 66 | " one 'closing' element at the beginning of the line has already reduced the |
| 67 | " indent, but 'else', 'elseif' & 'then' increment it for the next line |
| 68 | " '=' at the beginning has already de right indent (increased for asignments) |
| 69 | let inicio = matchend(line, '^\c\s*\(else\a*\|then\|endif\|/[*/]\|[);={]\)') |
| 70 | if inicio > 0 |
| 71 | let c = line[inicio-1] |
| 72 | " ')' and '=' don't change indent and are useless to set 'f' |
| 73 | if c == '{' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 74 | return shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 75 | elseif c != ')' && c != '=' |
| 76 | let f = 1 " all but 'elseif' are followed by a formula |
| 77 | if c ==? 'n' || c ==? 'e' " 'then', 'else' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 78 | let ind = ind + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 79 | elseif strpart(line, inicio-6, 6) ==? 'elseif' " elseif, set f to conditional |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 80 | let ind = ind + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 81 | let f = 0 |
| 82 | end |
| 83 | end |
| 84 | end |
| 85 | |
| 86 | " remove members [a] of [b]:[c]... (inicio remainds valid) |
| 87 | let line = substitute(line, '\c\(\[[^]]*]\(\s*of\s*\|:\)*\)\+', ' ', 'g') |
| 88 | while 1 |
| 89 | " search for the next interesting element |
| 90 | let inicio=matchend(line, '\c\<if\|endif\|[()=;]', inicio) |
| 91 | if inicio < 0 |
| 92 | break |
| 93 | end |
| 94 | |
| 95 | let c = line[inicio-1] |
| 96 | " 'expr(...)' containing the formula |
| 97 | if strpart(line, inicio-5, 5) ==? 'expr(' |
| 98 | let ind = 0 |
| 99 | let f = 1 |
| 100 | elseif c == ')' || c== ';' || strpart(line, inicio-5, 5) ==? 'endif' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 101 | let ind = ind - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 102 | elseif c == '(' || c ==? 'f' " '(' or 'if' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 103 | let ind = ind + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 104 | else " c == '=' |
| 105 | " if it is an asignment increase indent |
| 106 | if f == -1 " we don't know yet, find out |
| 107 | let f = CdlAsignment(lnum, strpart(line, 0, inicio)) |
| 108 | end |
| 109 | if f == 1 " formula increase it |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 110 | let ind = ind + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 111 | end |
| 112 | end |
| 113 | endw |
| 114 | |
| 115 | " CURRENT LINE, if it starts with a closing element, decrease indent |
| 116 | " or if it starts with '=' (asignment), increase indent |
| 117 | if match(thisline, '^\c\s*\(else\|then\|endif\|[);]\)') >= 0 |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 118 | let ind = ind - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 119 | elseif match(thisline, '^\s*=') >= 0 |
| 120 | if f == -1 " we don't know yet if is an asignment, find out |
| 121 | let f = CdlAsignment(lnum, "") |
| 122 | end |
| 123 | if f == 1 " formula increase it |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 124 | let ind = ind + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 125 | end |
| 126 | end |
| 127 | |
| 128 | return ind |
| 129 | endfun |