diff --git a/.config/kak/autoload/filetype/typst.kak b/.config/kak/autoload/filetype/typst.kak new file mode 100644 index 00000000..32d309a5 --- /dev/null +++ b/.config/kak/autoload/filetype/typst.kak @@ -0,0 +1,90 @@ +# https://www.latex-project.org/ +# + +# Detection +# ‾‾‾‾‾‾‾‾‾ + +hook global BufCreate .*[.](typ|typst) %{ + set-option buffer filetype typst + set-option buffer formatcmd "typstyle --wrap-text" +} + +# Initialization +# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ + +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 -group typst-highlight global WinSetOption filetype=typst %{ + add-highlighter window/typst ref typst + hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/typst } +} + +provide-module typst %~ + +# not setting highlights b/c it's handled by treesitter + +# Indent +# ------ + +define-command -hidden typst-trim-indent %{ + evaluate-commands -no-hooks -draft -itersel %{ + try %{ execute-keys x 1s^(\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-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 + ) + ) + ) +) + +~