sh.vim 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. " Vim indent file
  2. " Language: Shell Script
  3. " Maintainer: Christian Brabandt <cb@256bit.org>
  4. " Original Author: Nikolai Weibull <now@bitwi.se>
  5. " Previous Maintainer: Peter Aronoff <telemachus@arpinum.org>
  6. " Latest Revision: 2019-10-24
  7. " License: Vim (see :h license)
  8. " Repository: https://github.com/chrisbra/vim-sh-indent
  9. " Changelog:
  10. " 20250318 - Detect local arrays in functions
  11. " 20241411 - Detect dash character in function keyword for
  12. " bash mode (issue #16049)
  13. " 20190726 - Correctly skip if keywords in syntax comments
  14. " (issue #17)
  15. " 20190603 - Do not indent in zsh filetypes with an `if` in comments
  16. " 20190428 - De-indent fi correctly when typing with
  17. " https://github.com/chrisbra/vim-sh-indent/issues/15
  18. " 20190325 - Indent fi; correctly
  19. " https://github.com/chrisbra/vim-sh-indent/issues/14
  20. " 20190319 - Indent arrays (only zsh and bash)
  21. " https://github.com/chrisbra/vim-sh-indent/issues/13
  22. " 20190316 - Make use of searchpairpos for nested if sections
  23. " fixes https://github.com/chrisbra/vim-sh-indent/issues/11
  24. " 20190201 - Better check for closing if sections
  25. " 20180724 - make check for zsh syntax more rigid (needs word-boundaries)
  26. " 20180326 - better support for line continuation
  27. " 20180325 - better detection of function definitions
  28. " 20180127 - better support for zsh complex commands
  29. " 20170808: - better indent of line continuation
  30. " 20170502: - get rid of buffer-shiftwidth function
  31. " 20160912: - preserve indentation of here-doc blocks
  32. " 20160627: - detect heredocs correctly
  33. " 20160213: - detect function definition correctly
  34. " 20160202: - use shiftwidth() function
  35. " 20151215: - set b:undo_indent variable
  36. " 20150728: - add foreach detection for zsh
  37. if exists("b:did_indent")
  38. finish
  39. endif
  40. let b:did_indent = 1
  41. setlocal indentexpr=GetShIndent()
  42. setlocal indentkeys+=0=then,0=do,0=else,0=elif,0=fi,0=esac,0=done,0=end,),0=;;,0=;&
  43. setlocal indentkeys+=0=fin,0=fil,0=fip,0=fir,0=fix
  44. setlocal indentkeys-=:,0#
  45. setlocal nosmartindent
  46. let b:undo_indent = 'setlocal indentexpr< indentkeys< smartindent<'
  47. if exists("*GetShIndent")
  48. finish
  49. endif
  50. let s:cpo_save = &cpo
  51. set cpo&vim
  52. let s:sh_indent_defaults = {
  53. \ 'default': function('shiftwidth'),
  54. \ 'continuation-line': function('shiftwidth'),
  55. \ 'case-labels': function('shiftwidth'),
  56. \ 'case-statements': function('shiftwidth'),
  57. \ 'case-breaks': 0 }
  58. function! s:indent_value(option)
  59. let Value = exists('b:sh_indent_options')
  60. \ && has_key(b:sh_indent_options, a:option) ?
  61. \ b:sh_indent_options[a:option] :
  62. \ s:sh_indent_defaults[a:option]
  63. if type(Value) == type(function('type'))
  64. return Value()
  65. endif
  66. return Value
  67. endfunction
  68. function! GetShIndent()
  69. let mode = mode()
  70. let curline = getline(v:lnum)
  71. let lnum = prevnonblank(v:lnum - 1)
  72. if lnum == 0
  73. return 0
  74. endif
  75. let line = getline(lnum)
  76. let pnum = prevnonblank(lnum - 1)
  77. let pline = getline(pnum)
  78. let ind = indent(lnum)
  79. " Check contents of previous lines
  80. " should not apply to e.g. commented lines
  81. if s:start_block(line)
  82. let ind += s:indent_value('default')
  83. elseif line =~ '^\s*\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>\($\|\s\)' ||
  84. \ (&ft is# 'zsh' && line =~ '^\s*\<\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>\($\|\s\)')
  85. if !s:is_end_expression(line)
  86. let ind += s:indent_value('default')
  87. endif
  88. elseif s:is_case_label(line, pnum)
  89. if !s:is_case_ended(line)
  90. let ind += s:indent_value('case-statements')
  91. endif
  92. " function definition
  93. elseif s:is_function_definition(line)
  94. if line !~ '}\s*\%(#.*\)\=$'
  95. let ind += s:indent_value('default')
  96. endif
  97. " array (only works for zsh or bash)
  98. elseif s:is_array(line) && line !~ ')\s*$' && (&ft is# 'zsh' || s:is_bash())
  99. let ind += s:indent_value('continuation-line')
  100. " end of array
  101. elseif curline =~ '^\s*)$'
  102. let ind -= s:indent_value('continuation-line')
  103. elseif s:is_continuation_line(line)
  104. if pnum == 0 || !s:is_continuation_line(pline)
  105. let ind += s:indent_value('continuation-line')
  106. endif
  107. elseif s:end_block(line) && !s:start_block(line)
  108. let ind -= s:indent_value('default')
  109. elseif pnum != 0 &&
  110. \ s:is_continuation_line(pline) &&
  111. \ !s:end_block(curline) &&
  112. \ !s:is_end_expression(curline)
  113. " only add indent, if line and pline is in the same block
  114. let i = v:lnum
  115. let ind2 = indent(s:find_continued_lnum(pnum))
  116. while !s:is_empty(getline(i)) && i > pnum
  117. let i -= 1
  118. endw
  119. if i == pnum && (s:is_continuation_line(line) || pline =~ '{\s*\(#.*\)\=$')
  120. let ind += ind2
  121. else
  122. let ind = ind2
  123. endif
  124. endif
  125. let pine = line
  126. " Check content of current line
  127. let line = curline
  128. " Current line is a endif line, so get indent from start of "if condition" line
  129. " TODO: should we do the same for other "end" lines?
  130. if curline =~ '^\s*\%(fi\);\?\s*\%(#.*\)\=$'
  131. let ind = indent(v:lnum)
  132. " in insert mode, try to place the cursor after the fi statement
  133. let endp = '\<fi\>' .. (mode ==? 'i' ? '\zs' : '')
  134. let startp = '^\s*\<if\>'
  135. let previous_line = searchpair(startp, '', endp , 'bnW',
  136. \ 'synIDattr(synID(line("."),col("."), 1),"name") =~? "comment\\|quote\\|option"')
  137. if previous_line > 0
  138. let ind = indent(previous_line)
  139. endif
  140. elseif line =~ '^\s*\%(then\|do\|else\|elif\|done\|end\)\>' || s:end_block(line)
  141. let ind -= s:indent_value('default')
  142. elseif line =~ '^\s*esac\>' && s:is_case_empty(getline(v:lnum - 1))
  143. let ind -= s:indent_value('default')
  144. elseif line =~ '^\s*esac\>'
  145. let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ?
  146. \ 0 : s:indent_value('case-statements')) +
  147. \ s:indent_value('case-labels')
  148. if s:is_case_break(pine)
  149. let ind += s:indent_value('case-breaks')
  150. endif
  151. elseif s:is_case_label(line, lnum)
  152. if s:is_case(pine)
  153. let ind = indent(lnum) + s:indent_value('case-labels')
  154. else
  155. let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ?
  156. \ 0 : s:indent_value('case-statements')) -
  157. \ s:indent_value('case-breaks')
  158. endif
  159. elseif s:is_case_break(line)
  160. let ind -= s:indent_value('case-breaks')
  161. elseif s:is_here_doc(line)
  162. let ind = 0
  163. " statements, executed within a here document. Keep the current indent
  164. elseif match(map(synstack(v:lnum, 1), 'synIDattr(v:val, "name")'), '\c\mheredoc') > -1
  165. return indent(v:lnum)
  166. elseif s:is_comment(line) && s:is_empty(getline(v:lnum-1))
  167. if s:is_in_block(v:lnum)
  168. " return indent of line in same block
  169. return indent(lnum)
  170. else
  171. " use indent of current line
  172. return indent(v:lnum)
  173. endif
  174. endif
  175. return ind > 0 ? ind : 0
  176. endfunction
  177. function! s:is_continuation_line(line)
  178. " Comment, cannot be a line continuation
  179. if a:line =~ '^\s*#'
  180. return 0
  181. else
  182. " start-of-line
  183. " \\ or && or || or |
  184. " followed optionally by { or #
  185. return a:line =~ '\%(\%(^\|[^\\]\)\\\|&&\|||\||\)' .
  186. \ '\s*\({\s*\)\=\(#.*\)\=$'
  187. endif
  188. endfunction
  189. function! s:find_continued_lnum(lnum)
  190. let i = a:lnum
  191. while i > 1 && s:is_continuation_line(getline(i - 1))
  192. let i -= 1
  193. endwhile
  194. return i
  195. endfunction
  196. function! s:is_function_definition(line)
  197. return a:line =~ '^\s*\<\k\+\>\s*()\s*{' ||
  198. \ a:line =~ '^\s*{' ||
  199. \ a:line =~ '^\s*function\s*\k\+\s*\%(()\)\?\s*{' ||
  200. \ ((&ft is# 'zsh' || s:is_bash()) &&
  201. \ a:line =~ '^\s*function\s*\S\+\s*\%(()\)\?\s*{' )
  202. endfunction
  203. function! s:is_array(line)
  204. return a:line =~ '^\s*\(\(declare\|typeset\|local\)\s\+\(-[Aalrtu]\+\s\+\)\?\)\?\<\k\+\>=('
  205. endfunction
  206. function! s:is_in_block(line)
  207. " checks whether a:line is whithin a
  208. " block e.g. a shell function
  209. " foo() {
  210. " ..
  211. " }
  212. let prevline = searchpair('{', '', '}', 'bnW', 'synIDattr(synID(line("."),col("."), 1),"name") =~? "comment\\|quote"')
  213. let nextline = searchpair('{', '', '}', 'nW', 'synIDattr(synID(line("."),col("."), 1),"name") =~? "comment\\|quote"')
  214. return a:line > prevline && a:line < nextline
  215. endfunction
  216. function! s:is_case_label(line, pnum)
  217. if a:line !~ '^\s*(\=.*)'
  218. return 0
  219. endif
  220. if a:pnum > 0
  221. let pine = getline(a:pnum)
  222. if !(s:is_case(pine) || s:is_case_ended(pine))
  223. return 0
  224. endif
  225. endif
  226. let suffix = substitute(a:line, '^\s*(\=', "", "")
  227. let nesting = 0
  228. let i = 0
  229. let n = strlen(suffix)
  230. while i < n
  231. let c = suffix[i]
  232. let i += 1
  233. if c == '\\'
  234. let i += 1
  235. elseif c == '('
  236. let nesting += 1
  237. elseif c == ')'
  238. if nesting == 0
  239. return 1
  240. endif
  241. let nesting -= 1
  242. endif
  243. endwhile
  244. return 0
  245. endfunction
  246. function! s:is_case(line)
  247. return a:line =~ '^\s*case\>'
  248. endfunction
  249. function! s:is_case_break(line)
  250. return a:line =~ '^\s*;[;&]'
  251. endfunction
  252. function! s:is_here_doc(line)
  253. if a:line =~ '^\w\+$'
  254. let here_pat = '<<-\?'. s:escape(a:line). '\$'
  255. return search(here_pat, 'bnW') > 0
  256. endif
  257. return 0
  258. endfunction
  259. function! s:is_case_ended(line)
  260. return s:is_case_break(a:line) || a:line =~ ';[;&]\s*\%(#.*\)\=$'
  261. endfunction
  262. function! s:is_case_empty(line)
  263. if a:line =~ '^\s*$' || a:line =~ '^\s*#'
  264. return s:is_case_empty(getline(v:lnum - 1))
  265. else
  266. return a:line =~ '^\s*case\>'
  267. endif
  268. endfunction
  269. function! s:escape(pattern)
  270. return '\V'. escape(a:pattern, '\\')
  271. endfunction
  272. function! s:is_empty(line)
  273. return a:line =~ '^\s*$'
  274. endfunction
  275. function! s:end_block(line)
  276. return a:line =~ '^\s*}'
  277. endfunction
  278. function! s:start_block(line)
  279. return a:line =~ '^[^#]*[{(]\s*\(#.*\)\?$'
  280. endfunction
  281. function! s:is_comment(line)
  282. return a:line =~ '^\s*#'
  283. endfunction
  284. function! s:is_end_expression(line)
  285. return a:line =~ '\<\%(fi\|esac\|done\|end\)\>\s*\%(#.*\)\=$'
  286. endfunction
  287. function! s:is_bash()
  288. if &ft is# 'bash' || getline(1) is# '#!/bin/bash'
  289. return v:true
  290. else
  291. return get(g:, 'is_bash', 0) || get(b:, 'is_bash', 0)
  292. endif
  293. endfunction
  294. let &cpo = s:cpo_save
  295. unlet s:cpo_save