vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
latex.lua
(1504B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- Latex LPeg lexer.
3 -- Modified by Brian Schott.
4 -- Modified by Robert Gieseke.
5
6 local lexer = lexer
7 local word_match = lexer.word_match
8 local P, S = lpeg.P, lpeg.S
9
10 local lex = lexer.new(...)
11
12 -- Comments.
13 local line_comment = lexer.to_eol('%')
14 local block_comment = lexer.range('\\begin' * P(' ')^0 * '{comment}',
15 '\\end' * P(' ')^0 * '{comment}')
16 lex:add_rule('comment', lex:tag(lexer.COMMENT, line_comment + block_comment))
17
18 -- Math environments.
19 local math_word = word_match('align displaymath eqnarray equation gather math multline')
20 local math_begin_end = (P('begin') + P('end')) * P(' ')^0 * '{' * math_word * P('*')^-1 * '}'
21 lex:add_rule('math', lex:tag('environment.math', '$' + '\\' * (S('[]()') + math_begin_end)))
22
23 -- LaTeX environments.
24 lex:add_rule('environment', lex:tag('environment', '\\' * (P('begin') + 'end') * P(' ')^0 * '{' *
25 lexer.word * P('*')^-1 * '}'))
26
27 -- Sections.
28 lex:add_rule('section', lex:tag('command.section', '\\' *
29 word_match('part chapter section subsection subsubsection paragraph subparagraph') * P('*')^-1))
30
31 -- Commands.
32 lex:add_rule('command', lex:tag('command', '\\' * (lexer.alpha^1 + S('#$&~_^%{}\\'))))
33
34 -- Operators.
35 lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('&#{}[]')))
36
37 -- Fold points.
38 lex:add_fold_point(lexer.COMMENT, '\\begin', '\\end')
39 lex:add_fold_point('environment', '\\begin', '\\end')
40 lex:add_fold_point(lexer.OPERATOR, '{', '}')
41
42 lexer.property['scintillua.comment'] = '%'
43
44 return lex