vis: implement :set cursorline
[vis.git] / lexers / texinfo.lua
blobf9644249c485c93383566c313f59bfad402c38c8
1 -- Copyright 2014-2015 stef@ailleurs.land. See LICENSE.
2 -- Plain Texinfo version 5.2 LPeg lexer
3 -- Freely inspired from Mitchell work and valuable help from him too !
5 -- Directives are processed (more or less) in the Reference Card Texinfo order
6 -- Reference Card page for each directive group is in comment for reference
8 --[[
9 Note: Improving Fold Points use with Texinfo
11 At the very beginning of your Texinfo file, it could be wised to insert theses
12 alias :
14 @alias startchapter = comment
15 @alias endchapter = comment
17 Then use this to begin each chapter :
19 @endchapter --------------------------------------------------------------------
20 @chapter CHAPTER TITLE
21 @startchapter ------------------------------------------------------------------
23 With the use of Scintilla's `SCI_FOLDALL(SC_FOLDACTION_TOGGLE)` or Textadept's
24 `buffer:fold_all(buffer.FOLDACTION_TOGGLE)`, you have then a nice chapter
25 folding, useful with large documents.
28 local l = require('lexer')
29 local token, word_match = l.token, l.word_match
30 local P, R, S = lpeg.P, lpeg.R, lpeg.S
32 local M = {_NAME = 'texinfo'}
34 -------------------------------------------------------------------------------
35 -- Common processing
36 -------------------------------------------------------------------------------
38 -- Whitespace.
39 local ws = token(l.WHITESPACE, l.space^1)
41 -- Comments.
42 local line_comment = '@c' * l.nonnewline_esc^0
43 --local line_comment_long = '@comment' * l.nonnewline_esc^0
44 local block_comment = '@ignore' * (l.any - '@end ignore')^0 *
45 P('@end ignore')^-1
46 local comment = token(l.COMMENT, line_comment + block_comment)
48 -- Strings.
49 local string = token(l.STRING, l.delimited_range('{}', false, true, true))
51 -- Numbers.
52 local number = token(l.NUMBER, l.float + l.integer)
54 -- Identifiers
55 local identifier = token(l.IDENTIFIER, l.word)
57 -------------------------------------------------------------------------------
58 -- Common keywords
59 -------------------------------------------------------------------------------
61 local keyword_base = word_match({
62 'end',
63 -- Beginning a Texinfo document (page 1, column 1)
64 'setfilename', 'settitle', 'insertcopying',
65 -- Beginning a Texinfo document > Internationlization (page 1, column 1)
66 'documentencoding', 'documentlanguage', 'frenchspacing',
67 -- Beginning a Texinfo document > Info directory specification and HTML
68 -- document description (page 1, column 1)
69 'dircategory', 'direntry', 'documentdescription',
70 -- Beginning a Texinfo document > Titre pages (page 1, column 1)
71 'shorttitlepage', 'center', 'titlefont', 'title', 'subtitle', 'author',
72 -- Beginning a Texinfo document > Tables of contents (page 1, column 2)
73 'shortcontents', 'summarycontents', 'contents', 'setcontentsaftertitlepage',
74 'setshortcontentsaftertitlepage',
75 -- Nodes (page 1, column 2)
76 'node', 'top', 'anchor', 'novalidate',
77 -- Menus (page 1, column 2)
78 'menu', 'detailmenu',
79 -- Cross references > Within the Info system (page 1, column 3)
80 'xref', 'pxref', 'ref', 'inforef', 'xrefautomaticsectiontitle',
81 -- Cross references > Outside of info (page 1, column 3)
82 'url', 'cite',
83 -- Marking text > Markup for regular text (page 1, column 3)
84 'var', 'dfn', 'acronym', 'abbr',
85 -- Marking text > Markup for litteral text (page 1, column 3)
86 'code', 'file', 'command', 'env', 'option', 'kbd', 'key', 'email',
87 'indicateurl', 'samp', 'verb',
88 -- Marking text > GUI sequences (page 2, column 1)
89 'clicksequence', 'click', 'clickstyle', 'arrow',
90 -- Marking text > Math (page 2, column 1)
91 'math', 'minus', 'geq', 'leq',
92 -- Marking text > Explicit font selection (page 2, column 1)
93 'sc', 'r', 'i', 'slanted', 'b', 'sansserif', 't',
94 -- Block environments (page 2, column 1)
95 'noindent', 'indent', 'exdent',
96 -- Block environments > Normally filled displays using regular text fonts
97 -- (page 2, column 1)
98 'quotation', 'smallquotation', 'indentedblock', 'smallindentedblock',
99 'raggedright',
100 -- Block environments > Line-for-line displays using regular test fonts (page
101 -- 2, column 2)
102 'format', 'smallformat', 'display', 'smalldisplay', 'flushleft', 'flushright',
103 -- Block environments > Displays using fixed-width fonts (page 2, column 2)
104 'lisp', 'smalllisp', 'verbatim',
105 -- List and tables (page 2, column 2)
106 'table', 'ftable', 'vtable', 'tab', 'item', 'itemx', 'headitem',
107 'headitemfont', 'asis',
108 -- Indices (page 2, column 3)
109 'cindex', 'findex', 'vindex', 'kindex', 'pindex', 'tindex', 'defcodeindex',
110 'syncodeindex', 'synindex', 'printindex',
111 -- Insertions within a paragraph > Characters special to Texinfo (page 2,
112 -- column 3)
113 '@', '{', '}', 'backslashcar', 'comma', 'hashcar', ':', '.', '?', '!', 'dmn',
114 -- Insertions within a paragraph > Accents (page 3, column 1)
115 -- not implemented
116 -- Insertions within a paragraph > Non-English characters (page 3, column 1)
117 -- not implemented
118 -- Insertions within a paragraph > Other text characters an logos (page 3,
119 -- column 1)
120 'bullet', 'dots', 'enddots', 'euro', 'pounds', 'textdegree', 'copyright',
121 'registeredsymbol', 'TeX', 'LaTeX', 'today',
122 'guillemetleft', 'guillementright', 'guillemotleft', 'guillemotright',
123 -- Insertions within a paragraph > Glyphs for code examples (page 3, column 2)
124 'equiv', 'error', 'expansion', 'point', 'print', 'result',
125 -- Making and preventing breaks (page 3, column 2)
126 '*', '/', '-', 'hyphenation', 'tie', 'w', 'refill',
127 -- Vertical space (page 3, column 2)
128 'sp', 'page', 'need', 'group', 'vskip'
129 -- Definition commands (page 3, column 2)
130 -- not implemented
131 }, nil, true)
133 local keyword = token(l.KEYWORD, ('@end' * l.space^1 + '@') * keyword_base)
135 -------------------------------------------------------------------------------
136 -- Chapter structuring Keywords
137 -------------------------------------------------------------------------------
139 local chapters_base = word_match({
140 -- Chapter structuring (page 1, column 2)
141 'lowersections', 'raisesections', 'part',
142 -- Chapter structuring > Numbered, included in contents (page 1, column 2)
143 'chapter', 'centerchap',
144 -- Chapter structuring > Context-dependent, included in contents (page 1,
145 -- column 2)
146 'section', 'subsection', 'subsubsection',
147 -- Chapter structuring > Unumbered, included in contents (page 1, column 2)
148 'unnumbered', 'unnumberedsec', 'unnumberedsubsec', 'unnumberedsubsection',
149 'unnumberedsubsubsec', 'unnumberedsubsubsection',
150 -- Chapter structuring > Letter and numbered, included in contents (page 1,
151 -- column 2)
152 'appendix', 'appendixsec', 'appendixsection', 'appendixsubsec',
153 'appendixsubsection', 'appendixsubsubsec', 'appendixsubsubsection',
154 -- Chapter structuring > Unumbered, not included in contents, no new page
155 -- (page 1, column 3)
156 'chapheading', 'majorheading', 'heading', 'subheading', 'subsubheading'
157 }, nil, true)
159 local chapters = token('chapters', ('@end' * l.space^1 + '@') * chapters_base)
161 -------------------------------------------------------------------------------
162 -- Directives Keywords
163 -------------------------------------------------------------------------------
165 local directives_base = word_match({
166 'end',
167 -- Custom keywords for chapter folding
168 'startchapter', 'endchapter',
169 -- List and tables (page 2, column 2)
170 'itemize', 'enumerate',
171 -- Beginning a Texinfo document (page 1, column 1)
172 'titlepage', 'copying',
173 -- Block environments (page 2, column 1)
174 'cartouche',
175 -- Block environments > Displays using fixed-width fonts (page 2, column 2)
176 'example', 'smallexample',
177 -- List and tables (page 2, column 2)
178 'multitable',
179 -- Floating Displays (page 2, column 3)
180 'float', 'listoffloats', 'caption', 'shortcaption', 'image',
181 -- Floating Displays > Footnotes (page 2, column 3)
182 'footnote', 'footnotestyle',
183 -- Conditionally (in)visible text > Output formats (page 3, column 3)
184 'ifdocbook', 'ifhtml', 'ifinfo', 'ifplaintext', 'iftex', 'ifxml',
185 'ifnotdocbook', 'ifnothtml', 'ifnotplaintext',
186 'ifnottex', 'ifnotxml', 'ifnotinfo', 'inlinefmt', 'inlinefmtifelse',
187 -- Conditionally (in)visible text > Raw formatter text (page 4, column 1)
188 'docbook', 'html', 'tex', 'xml', 'inlineraw',
189 -- Conditionally (in)visible text > Documents variables (page 4, column 1)
190 'set', 'clear', 'value', 'ifset', 'ifclear', 'inlineifset', 'inlineifclear',
191 -- Conditionally (in)visible text > Testing for commands (page 4, column 1)
192 'ifcommanddefined', 'ifcommandnotdefined', 'end',
193 -- Defining new Texinfo commands (page 4, column 1)
194 'alias', 'macro', 'unmacro', 'definfounclose',
195 -- File inclusion (page 4, column 1)
196 'include', 'verbatiminclude',
197 -- Formatting and headers footers for TeX (page 4, column 1)
198 'allowcodebreaks', 'finalout', 'fonttextsize',
199 -- Formatting and headers footers for TeX > paper size (page 4, column 2)
200 'smallbook', 'afourpaper', 'afivepaper', 'afourlatex', 'afourwide',
201 'pagesizes',
202 -- Formatting and headers footers for TeX > Page headers and footers (page 4,
203 -- column 2)
204 -- not implemented
205 -- Document preferences (page 4, column 2)
206 -- not implemented
207 -- Ending a Texinfo document (page 4, column 2)
208 'bye'
209 }, nil, case_insensitive_tags)
211 local directives = token('directives',
212 ('@end' * l.space^1 + '@') * directives_base)
214 -------------------------------------------------------------------------------
215 -- Special keywords
216 -------------------------------------------------------------------------------
218 -- Italics
219 local emph = token('emph', '@emph' * l.delimited_range('{}', false, true, true))
221 -- Bold
222 local strong = token('strong',
223 '@strong' * l.delimited_range('{}', false, true, true))
225 -------------------------------------------------------------------------------
226 -- Rules, Tokenstyles and Folds points
227 -------------------------------------------------------------------------------
229 M._rules = {
230 {'whitespace', ws},
231 {'directives', directives},
232 {'chapters', chapters},
233 {'keyword', keyword},
234 {'emph', emph},
235 {'strong', strong},
236 {'identifier', identifier},
237 {'string', string},
238 {'number', number},
239 {'comment', comment},
242 M._tokenstyles = {
243 directives = l.STYLE_FUNCTION,
244 chapters = l.STYLE_CLASS,
245 emph = l.STYLE_STRING..',italics',
246 strong = l.STYLE_STRING..',bold'
249 M._foldsymbols = {
250 _patterns = {'@end %l+', '@%l+'},
251 directives = {
252 ['@titlepage'] = 1, ['@end titlepage'] = -1,
253 ['@copying'] = 1, ['@end copying'] = -1,
255 ['@ifset'] = 1, ['@end ifset'] = -1,
256 ['@tex'] = 1, ['@end tex'] = -1,
258 ['@itemize'] = 1, ['@end itemize'] = -1,
259 ['@enumerate'] = 1, ['@end enumerate'] = -1,
260 ['@multitable'] = 1, ['@end multitable'] = -1,
262 ['@example'] = 1, ['@end example'] = -1,
263 ['@smallexample'] = 1, ['@end smallexample'] = -1,
264 ['@cartouche'] = 1, ['@end cartouche'] = -1,
266 ['@startchapter'] = 1, ['@endchapter'] = -1,
270 return M