vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

texinfo.lua

(9338B)


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