# 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-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 latex-indent } ) 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 add-highlighter shared/latex/comment region '(? 1s^(\h+)$ d } } } define-command -hidden latex-indent-newline %( evaluate-commands -no-hooks -draft -itersel %( # copy '%' comment prefix and following white spaces try %{ execute-keys -draft k s^\h*%\h* y jgh P } # preserve previous line indent try %{ execute-keys -draft K } # cleanup trailing whitespaces from previous line try %{ execute-keys -draft k s\h+$ d } # indent after line ending with { try %( execute-keys -draft k \{$ j ) # deindent closing brace(s) when after cursor try %( execute-keys -draft ^\h*\} gh / \} m 1 ) # indent after line ending with \begin{...}[...]{...}, with multiple # sets of arguments possible try %( execute-keys -draft \ k \ \\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 _ \A\}\z m1 ) # Align \end{...} with corresponding \begin{...} try %( execute-keys -draft h 1s\\end\h*\{([^\}]+)\}\z \ \\begin\s*\{.\} 1 ) ) ) ~