From 904cec3c4a329cf89fc3219d359239910d61f3f6 Mon Sep 17 00:00:00 2001 From: thing1 Date: Tue, 28 Jan 2025 09:14:32 +0000 Subject: init commit --- autoload/filetype/latex.kak | 144 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 autoload/filetype/latex.kak (limited to 'autoload/filetype/latex.kak') diff --git a/autoload/filetype/latex.kak b/autoload/filetype/latex.kak new file mode 100644 index 0000000..70c3767 --- /dev/null +++ b/autoload/filetype/latex.kak @@ -0,0 +1,144 @@ +# https://www.latex-project.org/ +# + +# Detection +# ‾‾‾‾‾‾‾‾‾ + +hook global BufCreate .*\.(tex|cls|sty|dtx) %{ + set-option buffer filetype latex +} + +# Initialization +# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ + +hook global WinSetOption filetype=latex %( + require-module latex + + hook window InsertChar \n -group latex-insert %{ latex-insert-on-newline } + hook window InsertChar \n -group latex-indent %{ latex-indent-newline } + hook window InsertChar \} -group latex-indent %{ latex-indent-closing-brace } + hook window ModeChange pop:insert:.* -group latex-indent %{ latex-trim-indent } + hook -once -always window WinSetOption filetype=.* %{ remove-hooks window latex-indent } + hook window InsertChar \n -group latex-insert latex-insert-on-new-line +) + +hook -group latex-highlight global WinSetOption filetype=latex %{ + add-highlighter window/latex ref latex + hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/latex } +} + +provide-module latex %~ + +# Highlighters +# ‾‾‾‾‾‾‾‾‾‾‾‾ + +add-highlighter shared/latex regions +add-highlighter shared/latex/content default-region group +# Region for control sequence (includes latex2e arguments and options) +# starting with unescaped \ and ending : +# - at eol, or +# - at word boundaries not preceded nor followed by @ : \ { } [ ] *, or +# - after an unescaped } +add-highlighter shared/latex/cs region '(? d } + } +} + +define-command -hidden latex-insert-on-newline %{ + # copy '%' comment prefix and following white spaces + try %{ execute-keys -draft kx s^\h*%\h* y jgh P } +} + +define-command -hidden latex-indent-newline %( + evaluate-commands -no-hooks -draft -itersel %( + # preserve previous line indent + try %{ execute-keys -draft K } + # cleanup trailing whitespaces from previous line + try %{ execute-keys -draft kx s\h+$ d } + # indent after line ending with { + try %( execute-keys -draft kx \{$ j ) + # deindent closing brace(s) when after cursor + try %( execute-keys -draft x ^\h*\} gh / \} m 1 ) + # indent after line ending with \begin{...}[...]{...}, with multiple + # sets of arguments possible + try %( + execute-keys -draft \ + kx \ + \\begin\h*\{[^\}]+\}(\h|\[.*\]|\{.*\})*$ \ + j + ) + ) +) + +define-command -hidden latex-indent-closing-brace %( + evaluate-commands -no-hooks -draft -itersel %( + # Align lone } with matching bracket + try %( execute-keys -draft x_ \A\}\z m1 ) + # Align \end{...} with corresponding \begin{...} + try %( + execute-keys -draft h 1s\\end\h*\{([^\}]+)\}\z \ + \\begin\s*\{.\} 1 + ) + ) +) + +define-command -hidden latex-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 + ) + ) + ) +) + +~ -- cgit v1.2.3