config.vim 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. " Vim indent file
  2. " Language: Autoconf configure.{ac,in} file
  3. " Maintainer: Doug Kearns <dougkearns@gmail.com>
  4. " Previous Maintainer: Nikolai Weibull <now@bitwi.se>
  5. " Last Change: 24 Sep 2021
  6. " TODO: how about nested [()]'s in one line what's wrong with '\\\@!'?
  7. " Only load this indent file when no other was loaded.
  8. if exists("b:did_indent")
  9. finish
  10. endif
  11. runtime! indent/sh.vim " will set b:did_indent
  12. setlocal indentexpr=GetConfigIndent()
  13. setlocal indentkeys=!^F,o,O,=then,=do,=else,=elif,=esac,=fi,=fin,=fil,=done
  14. setlocal nosmartindent
  15. let b:undo_indent = "setl inde< indk< si<"
  16. " Only define the function once.
  17. if exists("*GetConfigIndent")
  18. finish
  19. endif
  20. " get the offset (indent) of the end of the match of 'regexp' in 'line'
  21. function s:GetOffsetOf(line, regexp)
  22. let end = matchend(a:line, a:regexp)
  23. let width = 0
  24. let i = 0
  25. while i < end
  26. if a:line[i] != "\t"
  27. let width = width + 1
  28. else
  29. let width = width + &ts - (width % &ts)
  30. endif
  31. let i = i + 1
  32. endwhile
  33. return width
  34. endfunction
  35. function GetConfigIndent()
  36. " Find a non-blank line above the current line.
  37. let lnum = prevnonblank(v:lnum - 1)
  38. " Hit the start of the file, use zero indent.
  39. if lnum == 0
  40. return 0
  41. endif
  42. " where to put this
  43. let ind = GetShIndent()
  44. let line = getline(lnum)
  45. " if previous line has unmatched, unescaped opening parentheses,
  46. " indent to its position. TODO: not failsafe if multiple ('s
  47. if line =~ '\\\@<!([^)]*$'
  48. let ind = s:GetOffsetOf(line, '\\\@!(')
  49. endif
  50. " if previous line has unmatched opening bracket,
  51. " indent to its position. TODO: same as above
  52. if line =~ '\[[^]]*$'
  53. let ind = s:GetOffsetOf(line, '\[')
  54. endif
  55. " if previous line had an unmatched closing parentheses,
  56. " indent to the matching opening parentheses
  57. if line =~ '[^(]\+\\\@<!)$'
  58. call search(')', 'bW')
  59. let lnum = searchpair('\\\@<!(', '', ')', 'bWn')
  60. let ind = indent(lnum)
  61. endif
  62. " if previous line had an unmatched closing bracket,
  63. " indent to the matching opening bracket
  64. if line =~ '[^[]\+]$'
  65. call search(']', 'bW')
  66. let lnum = searchpair('\[', '', ']', 'bWn')
  67. let ind = indent(lnum)
  68. endif
  69. return ind
  70. endfunction