latex.kak (6937B)
1 # https://www.latex-project.org/ 2 # 3 4 # Detection 5 # ‾‾‾‾‾‾‾‾‾ 6 7 hook global BufCreate .*\.(tex|cls|sty|dtx) %{ 8 set-option buffer filetype latex 9 } 10 11 # Initialization 12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 13 14 hook global WinSetOption filetype=latex %( 15 require-module latex 16 17 hook window InsertChar \n -group latex-insert %{ latex-insert-on-newline } 18 hook window InsertChar \n -group latex-indent %{ latex-indent-newline } 19 hook window InsertChar \} -group latex-indent %{ latex-indent-closing-brace } 20 hook window ModeChange pop:insert:.* -group latex-indent %{ latex-trim-indent } 21 hook -once -always window WinSetOption filetype=.* %{ remove-hooks window latex-indent } 22 hook window InsertChar \n -group latex-insert latex-insert-on-new-line 23 ) 24 25 hook -group latex-highlight global WinSetOption filetype=latex %{ 26 add-highlighter window/latex ref latex 27 hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/latex } 28 } 29 30 provide-module latex %~ 31 32 # Highlighters 33 # ‾‾‾‾‾‾‾‾‾‾‾‾ 34 35 add-highlighter shared/latex regions 36 add-highlighter shared/latex/content default-region group 37 38 # Math modes 39 # $math$ must have at least one non-$ character inside. 40 # This is so that $$math$$ get matched properly. 41 add-highlighter shared/latex/dollar-math region '(?<!\\)(?:\\\\)*\K\$[^$]' '(?<!\\)(\\\\)*\$' fill meta 42 add-highlighter shared/latex/ddollar-math region '(?<!\\)(?:\\\\)*\K\$\$' '(?<!\\)(\\\\)*\$\$' fill meta 43 add-highlighter shared/latex/bracket-math region '(?<!\\)(?:\\\\)*\K\\\[' '(?<!\\)(\\\\)*\\\]' fill meta 44 add-highlighter shared/latex/paren-math region '(?<!\\)(?:\\\\)*\K\\\(' '(?<!\\)(\\\\)*\\\)' fill meta 45 46 # Region for control sequence (includes latex2e arguments and options) 47 # starting with unescaped \ and ending : 48 # - at eol, or 49 # - at word boundaries not preceded nor followed by @ : \ { } [ ] *, or 50 # - after an unescaped } 51 add-highlighter shared/latex/cs region '(?<!\\)(?:\\\\)*\K\\[@\w]' '\n|(?<![@:\\{}\[\]*])(?![@:\\{}\[\]*])\b|(?<!\\)(?:\\\\)*\K\}\K' group 52 add-highlighter shared/latex/comment region '(?<!\\)(?:\\\\)*\K%' '\n' fill comment 53 54 # Document and LaTeX2e control sequence 55 add-highlighter shared/latex/cs/ regex '(?:\\[a-zA-Z@]+)' 0:keyword 56 ## Options passed to LaTeX2e control sequences, between brackets 57 add-highlighter shared/latex/cs/ regex '\\[a-zA-Z@]+\b\[([^\]]+)\]' 1:value 58 ## Emphasized text 59 add-highlighter shared/latex/cs/ regex '\\(?:emph|textit|textsl)\{([^}]+)\}' 1:default+i 60 ## Underlined text 61 add-highlighter shared/latex/cs/ regex '\\underline\{([^}]+)\}' 1:default+u 62 ## Bold text 63 add-highlighter shared/latex/cs/ regex '\\textbf\{([^}]+)\}' 1:default+b 64 ## Section headings 65 add-highlighter shared/latex/cs/ regex '\\(part|section)\*?\{([^}]+)\}' 2:title 66 add-highlighter shared/latex/cs/ regex '\\(chapter|(sub)+section|(sub)*paragraph)\*?\{([^}]+)\}' 4:header 67 68 # LaTeX3 control sequence 69 ## Functions (expl3 doc) module_name:arguments_types. 70 add-highlighter shared/latex/cs/ regex '\\(?:__|@@_)?[a-zA-Z@]+_\w+(:[nNpTFDwcVvxefo]+)?' 0:function 1:+db@type 71 ## Variables (expl3 doc): scope_name_type 72 add-highlighter shared/latex/cs/ regex '\\([lgc]_)[a-zA-Z@]+_\w+' 0:variable 1:+db 73 ## l3kernel modules (l3kernel/doc/l3prefixes.csv) 74 add-highlighter shared/latex/cs/ regex '\\(alignment|alloc|ampersand|atsign|backslash|bitset|bool|box|catcode|cctab|char|chk|circumflex|clist|code|codedoc|coffin|colon|color|cs|debug|dim|document|dollar|driver|e|else|empty|etex|exp|expl|false|fi|file|flag|fp|group|hash|hbox|hcoffin|if|inf|initex|insert|int|intarray|ior|iow|job|kernel|keys|keyval|left|log|lua|luatex|mark|marks|math|max|minus|mode|msg|muskip|nan|nil|no|novalue|one|or|other|parameter|pdf|pdftex|peek|percent|pi|prg|prop|ptex|quark|recursion|ref|regex|reverse|right|scan|seq|skip|sort|space|stop|str|sys|tag|term|tex|text|tilde|tl|tmpa|tmpb|token|true|underscore|uptex|use|utex|vbox|vcoffin|xetex|zero)_' 0:+db 75 # LaTeX3 types (expl3 doc) 76 add-highlighter shared/latex/cs/ regex '_(bool|box|cctab|clist|coffin|dim|fp|ior|iow|int|muskip|prop|seq|skip|str|tl)\b' 0:+db 77 78 # This belongs to content group as the LaTeX3 convention is separating macros names, args and options 79 # with spaces and thus should not be catched by the cs region 80 ## macros arguments 81 add-highlighter shared/latex/content/ regex '(?<!\\)(?:\\\\)*\K#+[1-9]' 0:string 82 ## group containing words and numbers (list separated by ; , / or spaces) 83 add-highlighter shared/latex/content/ regex '(?<!\\)(?:\\\\)*\K\{([\s/;,.\w\d]+)\}' 1:string 84 85 # Indent 86 # ------ 87 88 define-command -hidden latex-trim-indent %{ 89 evaluate-commands -no-hooks -draft -itersel %{ 90 try %{ execute-keys x 1s^(\h+)$<ret> d } 91 } 92 } 93 94 define-command -hidden latex-insert-on-newline %{ 95 # copy '%' comment prefix and following white spaces 96 try %{ execute-keys -draft kx s^\h*%\h*<ret> y jgh P } 97 } 98 99 define-command -hidden latex-indent-newline %( 100 evaluate-commands -no-hooks -draft -itersel %( 101 # preserve previous line indent 102 try %{ execute-keys -draft K<a-&> } 103 # cleanup trailing whitespaces from previous line 104 try %{ execute-keys -draft kx s\h+$<ret> d } 105 # indent after line ending with { 106 try %( execute-keys -draft kx <a-k>\{$<ret> j<a-gt> ) 107 # deindent closing brace(s) when after cursor 108 try %( execute-keys -draft x <a-k> ^\h*\} <ret> gh / \} <ret> m <a-S> 1<a-&> ) 109 # indent after line ending with \begin{...}[...]{...}, with multiple 110 # sets of arguments possible 111 try %( 112 execute-keys -draft \ 113 kx \ 114 <a-k>\\begin\h*\{[^\}]+\}(\h|\[.*\]|\{.*\})*$<ret> \ 115 j<a-gt> 116 ) 117 ) 118 ) 119 120 define-command -hidden latex-indent-closing-brace %( 121 evaluate-commands -no-hooks -draft -itersel %( 122 # Align lone } with matching bracket 123 try %( execute-keys -draft x_ <a-k>\A\}\z<ret> m<a-S>1<a-&> ) 124 # Align \end{...} with corresponding \begin{...} 125 try %( 126 execute-keys -draft h<a-h> 1s\\end\h*\{([^\}]+)\}\z<ret> \ 127 <a-?>\\begin\s*\{<c-r>.\}<ret> <a-S>1<a-&> 128 ) 129 ) 130 ) 131 132 define-command -hidden latex-insert-on-new-line %( 133 evaluate-commands -no-hooks -draft -itersel %( 134 # Wisely add "\end{...}". 135 evaluate-commands -save-regs xz %( 136 # Save previous line indent in register x. 137 try %( execute-keys -draft kxs^\h+<ret>"xy ) catch %( reg x '' ) 138 # Save item of begin in register z. 139 try %( execute-keys -draft kxs\{.*\}<ret>"zy ) catch %( reg z '' ) 140 try %( 141 # Validate previous line and that it is not closed yet. 142 execute-keys -draft kx <a-k>^<c-r>x\h*\\begin\{.*\}<ret> J}iJx <a-K>^<c-r>x(\\end\<c-r>z<backspace>\})<ret> 143 # Auto insert "\end{...}". 144 execute-keys -draft o<c-r>x\end<c-r>z<esc> 145 ) 146 ) 147 ) 148 ) 149 150 ~