# 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 ) ) ) ) ~