krl.vim 4.3 KB

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