# 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 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 '(? 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 ) ) ) 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 ks^\h+"xy ) catch %( reg x '' ) # Save item of begin in register z. try %( execute-keys -draft ks\{.*\}"zy ) catch %( reg z '' ) try %( # Validate previous line and that it is not closed yet. execute-keys -draft k ^x\h*\\begin\{.*\} J}iJ ^x(\\end\z\}) # Auto insert "\end{...}". execute-keys -draft ox\endz ) ) ) ) ~