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