Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Description: Comshare Dimension Definition Language (CDL) |
Bram Moolenaar | 6e64922 | 2021-10-04 21:32:54 +0100 | [diff] [blame] | 2 | " Maintainer: Raul Segura Acevedo <raulseguraaceved@netscape.net> (Invalid email address) |
| 3 | " Doug Kearns <dougkearns@gmail.com> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4 | " Last Change: Fri Nov 30 13:35:48 2001 CST |
| 5 | |
| 6 | if exists("b:did_indent") |
| 7 | "finish |
| 8 | endif |
| 9 | let b:did_indent = 1 |
| 10 | |
| 11 | setlocal indentexpr=CdlGetIndent(v:lnum) |
| 12 | setlocal indentkeys& |
| 13 | setlocal indentkeys+==~else,=~endif,=~then,;,),= |
| 14 | |
| 15 | " Only define the function once. |
| 16 | if exists("*CdlGetIndent") |
| 17 | "finish |
| 18 | endif |
| 19 | |
Bram Moolenaar | 6c391a7 | 2021-09-09 21:55:11 +0200 | [diff] [blame] | 20 | " find out if an "...=..." expression is an assignment (or a conditional) |
| 21 | " it scans 'line' first, and then the previous lines |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 22 | fun! CdlAsignment(lnum, line) |
| 23 | let f = -1 |
| 24 | let lnum = a:lnum |
| 25 | let line = a:line |
| 26 | while lnum > 0 && f == -1 |
| 27 | " line without members [a] of [b]:[c]... |
| 28 | let inicio = 0 |
| 29 | while 1 |
| 30 | " keywords that help to decide |
| 31 | let inicio = matchend(line, '\c\<\(expr\|\a*if\|and\|or\|not\|else\|then\|memberis\|\k\+of\)\>\|[<>;]', inicio) |
| 32 | if inicio < 0 |
| 33 | break |
| 34 | endif |
| 35 | " it's formula if there's a ';', 'elsE', 'theN', 'enDif' or 'expr' |
| 36 | " conditional if there's a '<', '>', 'elseif', 'if', 'and', 'or', 'not', |
Bram Moolenaar | 6c391a7 | 2021-09-09 21:55:11 +0200 | [diff] [blame] | 37 | " 'memberis', 'childrenof' and other \k\+of functions |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | let f = line[inicio-1] =~? '[en;]' || strpart(line, inicio-4, 4) =~? 'ndif\|expr' |
| 39 | endw |
| 40 | let lnum = prevnonblank(lnum-1) |
| 41 | let line = substitute(getline(lnum), '\c\(\[[^]]*]\(\s*of\s*\|:\)*\)\+', ' ', 'g') |
| 42 | endw |
| 43 | " if we hit the start of the file then f = -1, return 1 (formula) |
| 44 | return f != 0 |
| 45 | endf |
| 46 | |
| 47 | fun! CdlGetIndent(lnum) |
| 48 | let thisline = getline(a:lnum) |
| 49 | if match(thisline, '^\s*\(\k\+\|\[[^]]*]\)\s*\(,\|;\s*$\)') >= 0 |
| 50 | " it's an attributes line |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 51 | return shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 52 | elseif match(thisline, '^\c\s*\([{}]\|\/[*/]\|dimension\|schedule\|group\|hierarchy\|class\)') >= 0 |
| 53 | " it's a header or '{' or '}' or a comment |
| 54 | return 0 |
| 55 | end |
| 56 | |
| 57 | let lnum = prevnonblank(a:lnum-1) |
| 58 | " Hit the start of the file, use zero indent. |
| 59 | if lnum == 0 |
| 60 | return 0 |
| 61 | endif |
| 62 | |
| 63 | " PREVIOUS LINE |
| 64 | let ind = indent(lnum) |
| 65 | let line = getline(lnum) |
Bram Moolenaar | dad4473 | 2021-03-31 20:07:33 +0200 | [diff] [blame] | 66 | |
| 67 | " Whether a '=' is a conditional or an assignment. -1 means we don't know |
| 68 | " yet. |
| 69 | " One 'closing' element at the beginning of the line has already reduced the |
| 70 | " indent, but 'else', 'elseif' & 'then' increment it for the next line. |
| 71 | " '=' at the beginning already has the right indent (increased for |
| 72 | " asignments). |
| 73 | let f = -1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 74 | let inicio = matchend(line, '^\c\s*\(else\a*\|then\|endif\|/[*/]\|[);={]\)') |
| 75 | if inicio > 0 |
| 76 | let c = line[inicio-1] |
| 77 | " ')' and '=' don't change indent and are useless to set 'f' |
| 78 | if c == '{' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 79 | return shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 80 | elseif c != ')' && c != '=' |
| 81 | let f = 1 " all but 'elseif' are followed by a formula |
| 82 | if c ==? 'n' || c ==? 'e' " 'then', 'else' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 83 | let ind = ind + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 84 | elseif strpart(line, inicio-6, 6) ==? 'elseif' " elseif, set f to conditional |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 85 | let ind = ind + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 86 | let f = 0 |
| 87 | end |
| 88 | end |
| 89 | end |
| 90 | |
| 91 | " remove members [a] of [b]:[c]... (inicio remainds valid) |
| 92 | let line = substitute(line, '\c\(\[[^]]*]\(\s*of\s*\|:\)*\)\+', ' ', 'g') |
| 93 | while 1 |
| 94 | " search for the next interesting element |
| 95 | let inicio=matchend(line, '\c\<if\|endif\|[()=;]', inicio) |
| 96 | if inicio < 0 |
| 97 | break |
| 98 | end |
| 99 | |
| 100 | let c = line[inicio-1] |
| 101 | " 'expr(...)' containing the formula |
| 102 | if strpart(line, inicio-5, 5) ==? 'expr(' |
| 103 | let ind = 0 |
| 104 | let f = 1 |
| 105 | elseif c == ')' || c== ';' || strpart(line, inicio-5, 5) ==? 'endif' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 106 | let ind = ind - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 107 | elseif c == '(' || c ==? 'f' " '(' or 'if' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 108 | let ind = ind + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 109 | else " c == '=' |
Bram Moolenaar | 6c391a7 | 2021-09-09 21:55:11 +0200 | [diff] [blame] | 110 | " if it is an assignment increase indent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 111 | if f == -1 " we don't know yet, find out |
| 112 | let f = CdlAsignment(lnum, strpart(line, 0, inicio)) |
| 113 | end |
| 114 | if f == 1 " formula increase it |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 115 | let ind = ind + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 116 | end |
| 117 | end |
| 118 | endw |
| 119 | |
| 120 | " CURRENT LINE, if it starts with a closing element, decrease indent |
Bram Moolenaar | 6c391a7 | 2021-09-09 21:55:11 +0200 | [diff] [blame] | 121 | " or if it starts with '=' (assignment), increase indent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 122 | if match(thisline, '^\c\s*\(else\|then\|endif\|[);]\)') >= 0 |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 123 | let ind = ind - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 124 | elseif match(thisline, '^\s*=') >= 0 |
Bram Moolenaar | 6c391a7 | 2021-09-09 21:55:11 +0200 | [diff] [blame] | 125 | if f == -1 " we don't know yet if is an assignment, find out |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 126 | let f = CdlAsignment(lnum, "") |
| 127 | end |
| 128 | if f == 1 " formula increase it |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 129 | let ind = ind + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 130 | end |
| 131 | end |
| 132 | |
| 133 | return ind |
| 134 | endfun |