vim

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

krl.vim (4356B)


      1 " Vim indent file
      2 " Language: Kuka Robot Language
      3 " Maintainer: Patrick Meiser-Knosowski <knosowski@graeffrobotics.de>
      4 " Version: 3.0.0
      5 " Last Change: 15. Apr 2022
      6 " Credits: Based on indent/vim.vim
      7 
      8 " Only load this indent file when no other was loaded.
      9 if exists("b:did_indent")
     10   finish
     11 endif
     12 let b:did_indent = 1
     13 
     14 setlocal nolisp
     15 setlocal nocindent
     16 setlocal nosmartindent
     17 setlocal autoindent
     18 setlocal indentexpr=GetKrlIndent()
     19 setlocal indentkeys=!^F,o,O,=~end,0=~else,0=~case,0=~default,0=~until,0=~continue,=~part
     20 let b:undo_indent = "setlocal lisp< cindent< smartindent< autoindent< indentexpr< indentkeys<"
     21 
     22 if get(g:,'krlSpaceIndent',1)
     23   " Use spaces, not tabs, for indention, 2 is enough. 
     24   " More or even tabs would waste valuable space on the teach pendant.
     25   setlocal softtabstop=2
     26   setlocal shiftwidth=2
     27   setlocal expandtab
     28   setlocal shiftround
     29   let b:undo_indent = b:undo_indent." softtabstop< shiftwidth< expandtab< shiftround<"
     30 endif
     31 
     32 " Only define the function once.
     33 if exists("*GetKrlIndent")
     34   finish
     35 endif
     36 let s:keepcpo = &cpo
     37 set cpo&vim
     38 
     39 function GetKrlIndent() abort
     40 
     41   let currentLine = getline(v:lnum)
     42   if  currentLine =~? '\v^;(\s*(end)?fold>)@!' && !get(g:, 'krlCommentIndent', 0)
     43     " If current line has a ; in column 1 and is no fold, keep zero indent.
     44     " This may be useful if code is commented out at the first column.
     45     return 0
     46   endif
     47 
     48   " Find a non-blank line above the current line.
     49   let preNoneBlankLineNum = s:KrlPreNoneBlank(v:lnum - 1)
     50   if  preNoneBlankLineNum == 0
     51     " At the start of the file use zero indent.
     52     return 0
     53   endif
     54 
     55   let preNoneBlankLine = getline(preNoneBlankLineNum)
     56   let ind = indent(preNoneBlankLineNum)
     57 
     58   " Define add 'shiftwidth' pattern
     59   let addShiftwidthPattern =           '\v^\s*('
     60   if get(g:, 'krlIndentBetweenDef', 1)
     61     let addShiftwidthPattern ..=               '(global\s+)?def(fct|dat)?\s+\$?\w'
     62     let addShiftwidthPattern ..=               '|'
     63   endif
     64   let addShiftwidthPattern   ..=               'if>|while>|for>|loop>'
     65   let addShiftwidthPattern   ..=               '|else>'
     66   let addShiftwidthPattern   ..=               '|case>|default>'
     67   let addShiftwidthPattern   ..=               '|repeat>'
     68   let addShiftwidthPattern   ..=               '|skip>|(ptp_)?spline>'
     69   let addShiftwidthPattern   ..=               '|time_block\s+(start|part)>'
     70   let addShiftwidthPattern   ..=               '|const_vel\s+start>'
     71   let addShiftwidthPattern   ..=             ')'
     72 
     73   " Define Subtract 'shiftwidth' pattern
     74   let subtractShiftwidthPattern =      '\v^\s*('
     75   if get(g:, 'krlIndentBetweenDef', 1)
     76     let subtractShiftwidthPattern ..=          'end(fct|dat)?>'
     77     let subtractShiftwidthPattern ..=          '|'
     78   endif
     79   let subtractShiftwidthPattern   ..=          'end(if|while|for|loop)>'
     80   let subtractShiftwidthPattern   ..=          '|else>'
     81   let subtractShiftwidthPattern   ..=          '|case>|default>|endswitch>'
     82   let subtractShiftwidthPattern   ..=          '|until>'
     83   let subtractShiftwidthPattern   ..=          '|end(skip|spline)>'
     84   let subtractShiftwidthPattern   ..=          '|time_block\s+(part|end)>'
     85   let subtractShiftwidthPattern   ..=          '|const_vel\s+end>'
     86   let subtractShiftwidthPattern   ..=        ')'
     87 
     88   " Add shiftwidth
     89   if preNoneBlankLine =~? addShiftwidthPattern
     90     let ind += &sw
     91   endif
     92 
     93   " Subtract shiftwidth
     94   if currentLine =~? subtractShiftwidthPattern
     95     let ind = ind - &sw
     96   endif
     97 
     98   " First case after a switch gets the indent of the switch.
     99   if currentLine =~? '\v^\s*case>'  
    100         \&& preNoneBlankLine =~? '\v^\s*switch>'
    101     let ind = ind + &sw
    102   endif
    103 
    104   " align continue with the following instruction
    105   if currentLine =~? '\v^\s*continue>'  
    106         \&& getline(v:lnum + 1) =~? subtractShiftwidthPattern
    107     let ind = ind - &sw
    108   endif
    109 
    110   return ind
    111 endfunction
    112 
    113 " This function works almost like prevnonblank() but handles &-headers,
    114 " comments and continue instructions like blank lines
    115 function s:KrlPreNoneBlank(lnum) abort
    116 
    117   let nPreNoneBlank = prevnonblank(a:lnum)
    118 
    119   while nPreNoneBlank > 0 && getline(nPreNoneBlank) =~? '\v^\s*(\&\w\+|;|continue>)'
    120     " Previous none blank line irrelevant. Look further aback.
    121     let nPreNoneBlank = prevnonblank(nPreNoneBlank - 1)
    122   endwhile
    123 
    124   return nPreNoneBlank
    125 endfunction
    126 
    127 let &cpo = s:keepcpo
    128 unlet s:keepcpo
    129 
    130 " vim:sw=2 sts=2 et