diff --git a/rc/base/fish.kak b/rc/base/fish.kak index 3df3d793..3e2b844c 100644 --- a/rc/base/fish.kak +++ b/rc/base/fish.kak @@ -34,29 +34,41 @@ addhl -group /fish/code regex \b(and|begin|bg|bind|block|break|breakpoint|builti # ‾‾‾‾‾‾‾‾ def -hidden _fish_filter_around_selections %{ - # remove trailing white spaces - try %{ exec -draft -itersel s \h+$ d } + eval -no-hooks -draft -itersel %{ + # remove trailing white spaces + try %{ exec -draft s\h+$d } + } } def -hidden _fish_indent_on_char %{ - eval -draft -itersel %{ - # deindent on (else|end) command insertion - try %{ exec -draft w (else|end) } + eval -no-hooks -draft -itersel %{ + # align middle and end structures to start and indent when necessary + try %{ exec -draft ^\h*(else)$^\h*(if)s\A|\Z' } + try %{ exec -draft ^\h*(end)$^\h*(begin|for|function|if|switch|while)s\A|\Z' } + try %{ exec -draft ^\h*(case)$^\h*(switch)s\A|\Z'' } } } def -hidden _fish_indent_on_new_line %{ - eval -draft -itersel %{ + eval -no-hooks -draft -itersel %{ # preserve previous line indent - try %{ exec -draft K } + try %{ exec -draft K } # filter previous line - try %{ exec -draft k : _fish_filter_around_selections } - # copy '#' comment prefix and following white spaces - try %{ exec -draft k x s ^\h*\K#\h* y j p } - # indent after (case|else) commands - try %{ exec -draft k x (case|else) j } - # indent after (begin|for|function|if|switch|while) commands and add 'end' command - try %{ exec -draft k x (begin|for|function|(? x y p j a end k } + try %{ exec -draft k:_fish_filter_around_selections } + # indent after start structure + try %{ exec -draft kx^\h*(begin|case|else|for|function|if|switch|while)\bj } + } +} + +def -hidden _fish_insert_on_new_line %{ + eval -no-hooks -draft -itersel %{ + # copy _#_ comment prefix and following white spaces + try %{ exec -draft kxs^\h*\K#\h*yjp } + # wisely add end structure + eval -save-regs x %{ + try %{ exec -draft kxs^\h+"xy } catch %{ reg x '' } + try %{ exec -draft kx^x(begin|for|function|if|switch|while)jiXK^x(begin|for|function|if|switch|while).*\nxend$jxypjaend } + } } } @@ -66,14 +78,14 @@ def -hidden _fish_indent_on_new_line %{ hook -group fish-highlight global WinSetOption filetype=fish %{ addhl ref fish } hook global WinSetOption filetype=fish %{ - hook window InsertEnd .* -group fish-hooks _fish_filter_around_selections hook window InsertChar .* -group fish-indent _fish_indent_on_char hook window InsertChar \n -group fish-indent _fish_indent_on_new_line + hook window InsertChar \n -group fish-insert _fish_insert_on_new_line } hook -group fish-highlight global WinSetOption filetype=(?!fish).* %{ rmhl fish } hook global WinSetOption filetype=(?!fish).* %{ rmhooks window fish-indent - rmhooks window fish-hooks + rmhooks window fish-insert }