vim

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

karel.vim (4013B)


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