vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

cdl.vim (4406B)


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