diff --git a/rc/filetype/hare.kak b/rc/filetype/hare.kak new file mode 100644 index 00000000..47d0da41 --- /dev/null +++ b/rc/filetype/hare.kak @@ -0,0 +1,136 @@ +# detection +hook global BufCreate .*[.]ha %{ + set-option buffer filetype hare +} + +# initialisation +hook global WinSetOption filetype=hare %{ + require-module hare + hook window ModeChange pop:insert:.* -group hare-trim-indent hare-trim-indent + hook window InsertChar \n -group hare-indent hare-indent-on-new-line + hook window InsertChar \n -group hare-insert hare-insert-on-new-line + hook window InsertChar \{ -group hare-indent hare-indent-on-opening-curly-brace + hook window InsertChar \} -group hare-indent hare-indent-on-closing-curly-brace +} + +hook -group hare-highlight global WinSetOption filetype=hare %{ + add-highlighter window/hare ref hare + hook -once -always window WinSetOption filetype=*. %{ remove-highlighter window/hare } +} + +# highlighters +provide-module hare %§ + add-highlighter shared/hare regions + add-highlighter shared/hare/code default-region group + add-highlighter shared/hare/comment region // $ fill comment + + add-highlighter shared/hare/rawstring region ` ` group + add-highlighter shared/hare/rawstring/ fill string + + add-highlighter shared/hare/string region '"' (? K } + # indent after lines ending with { or ( + try %[ execute-keys -draft k [{(]\h*$ j i ] + # cleanup trailing white spaces on the previous line + execute-keys -draft k :hare-trim-indent + # indent after match/switch's case statements + try %[ execute-keys -draft k case\h.*=>\h*$ j ] + # deindent closing brace(s) when after cursor + try %[ execute-keys -draft ^\h*[})] gh / [})] m 1 ] + } } + + define-command -hidden hare-insert-on-new-line %{ evaluate-commands -draft -itersel %{ + try %{ evaluate-commands -draft -save-regs '/"' %{ + # copy the comment prefix + execute-keys -save-regs '' k s ^\h*\K//\h* y + try %{ + # paste the comment prefix + execute-keys j s ^\h* P + } + } } + try %{ + # remove trailing whitespace on the above line + execute-keys -draft k :hare-trim-indent + } + } } + + define-command -hidden hare-indent-on-opening-curly-brace %[ + # align indent with opening paren when { is entered on a new line after the closing paren + try %[ execute-keys -draft -itersel h)M \A\(.*\)\h*\n\h*\{\z s \A|.\z 1 ] + ] + + define-command -hidden hare-indent-on-closing-curly-brace %[ + # align to opening curly brace when alone on a line + try %[ execute-keys -itersel -draft ^\h+\}$hms\A|.\z1 ] + ] + + define-command -hidden hare-trim-indent %{ evaluate-commands -draft -itersel %{ + # remove trailing whitespace + try %{ execute-keys -draft s \h+$ d } + } } +§ diff --git a/rc/tools/comment.kak b/rc/tools/comment.kak index 1094fbba..803a96fe 100644 --- a/rc/tools/comment.kak +++ b/rc/tools/comment.kak @@ -118,7 +118,7 @@ hook global BufSetOption filetype=perl %{ set-option buffer comment_block_end ']' } -hook global BufSetOption filetype=(pug|zig|cue) %{ +hook global BufSetOption filetype=(pug|zig|cue|hare) %{ set-option buffer comment_line '//' }