From 3c31e5303bff1e938fa42337b87342efef2b54e3 Mon Sep 17 00:00:00 2001 From: Daniel Fichtinger Date: Fri, 23 May 2025 19:15:14 -0400 Subject: [PATCH] AutoYADM commit: 2025-05-23 19:15:14 --- .config/kak/autoload/filetype/typst.kak | 78 ++++++++----------------- 1 file changed, 25 insertions(+), 53 deletions(-) diff --git a/.config/kak/autoload/filetype/typst.kak b/.config/kak/autoload/filetype/typst.kak index d00fd29a..95b0becf 100644 --- a/.config/kak/autoload/filetype/typst.kak +++ b/.config/kak/autoload/filetype/typst.kak @@ -16,12 +16,11 @@ hook global BufCreate .*[.](typ|typst) %{ hook global WinSetOption filetype=typst %( require-module typst - hook window InsertChar \n -group typst-insert %{ typst-insert-on-newline } - hook window InsertChar \n -group typst-indent %{ typst-indent-newline } - hook window InsertChar \} -group typst-indent %{ typst-indent-closing-brace } - hook window ModeChange pop:insert:.* -group typst-indent %{ typst-trim-indent } - hook -once -always window WinSetOption filetype=.* %{ remove-hooks window typst-indent } - hook window InsertChar \n -group typst-insert typst-insert-on-new-line + hook window ModeChange pop:insert:.* -group typst-trim-indent typst-trim-indent + hook window InsertChar .* -group typst-indent typst-indent-on-char + hook window InsertChar \n -group typst-indent typst-indent-on-new-line + + hook -once -always window WinSetOption filetype=.* %{ remove-hooks window typst-.+ } ) hook -group typst-highlight global WinSetOption filetype=typst %{ @@ -37,55 +36,28 @@ provide-module typst %~ # ------ define-command -hidden typst-trim-indent %{ - evaluate-commands -no-hooks -draft -itersel %{ - try %{ execute-keys x 1s^(\h+)$ d } - } + # remove trailing white spaces + try %{ execute-keys -draft -itersel x s \h+$ d } } -define-command -hidden typst-insert-on-newline %{ - # copy '//' comment prefix and following white spaces - try %{ execute-keys -draft kx s^\h*//\h* y jgh P } -} +define-command -hidden typst-indent-on-char %< + evaluate-commands -draft -itersel %< + # align closer token to its opener when alone on a line + try %< execute-keys -draft ^\h+[}])]$ m 1 > + > +> -define-command -hidden typst-indent-newline %( - evaluate-commands -no-hooks -draft -itersel %( - # preserve previous line indent - try %{ execute-keys -draft K } - - # remove trailing whitespace on previous line - try %{ execute-keys -draft kx s\h+$ d } - - # indent if previous line ends with "[" - try %( execute-keys -draft kx \[$ j ) - - # deindent if next line starts with "]" - try %( execute-keys -draft x ^\h*\] gh / \] m 1 ) - ) -) - -define-command -hidden typst-indent-closing-brace %( - evaluate-commands -no-hooks -draft -itersel %( - # align closing bracket ] to matching indentation - try %( execute-keys -draft x_ ^\h*\]\z m1 ) - ) -) - -define-command -hidden typst-insert-on-new-line %( - evaluate-commands -no-hooks -draft -itersel %( - # Wisely add "\end{...}". - evaluate-commands -save-regs xz %( - # Save previous line indent in register x. - try %( execute-keys -draft kxs^\h+"xy ) catch %( reg x '' ) - # Save item of begin in register z. - try %( execute-keys -draft kxs\{.*\}"zy ) catch %( reg z '' ) - try %( - # Validate previous line and that it is not closed yet. - execute-keys -draft kx ^x\h*\\begin\{.*\} J}iJx ^x(\\end\z\}) - # Auto insert "\end{...}". - execute-keys -draft ox\endz - ) - ) - ) -) +define-command -hidden typst-indent-on-new-line %< + evaluate-commands -draft -itersel %< + # preserve previous line indent + try %{ execute-keys -draft K } + # filter previous line + try %{ execute-keys -draft k : typst-trim-indent } + # indent after lines ending with opener token + try %< execute-keys -draft k x [({[]\h*$ j > + # deindent closer token(s) when after cursor + try %< execute-keys -draft x ^\h*[}])] gh / [}])] m 1 > + > +> ~