From 4cd415bc5878c3387d4a6afef12acbe03decb7ea Mon Sep 17 00:00:00 2001 From: Eolien55 Date: Wed, 23 Nov 2022 22:57:30 +0100 Subject: [PATCH] Replaced man lexer by improved troff lexer --- lua/lexers/man.lua | 37 ----------------------------- lua/lexers/troff.lua | 50 ++++++++++++++++++++++++++++++++++++++++ lua/plugins/filetype.lua | 5 ++-- 3 files changed, 53 insertions(+), 39 deletions(-) delete mode 100644 lua/lexers/man.lua create mode 100644 lua/lexers/troff.lua diff --git a/lua/lexers/man.lua b/lua/lexers/man.lua deleted file mode 100644 index afeb2f2ed..000000000 --- a/lua/lexers/man.lua +++ /dev/null @@ -1,37 +0,0 @@ --- Copyright 2015-2017 David B. Lamkins . See LICENSE. --- man/roff LPeg lexer. - -local l = require('lexer') -local token, word_match = l.token, l.word_match -local P, R, S = lpeg.P, lpeg.R, lpeg.S - -local M = {_NAME = 'man'} - --- Whitespace. -local ws = token(l.WHITESPACE, l.space^1) - --- Markup. -local rule1 = token(l.STRING, - P('.') * (P('B') * P('R')^-1 + P('I') * P('PR')^-1) * - l.nonnewline^0) -local rule2 = token(l.NUMBER, P('.') * S('ST') * P('H') * l.nonnewline^0) -local rule3 = token(l.KEYWORD, - P('.br') + P('.DS') + P('.RS') + P('.RE') + P('.PD')) -local rule4 = token(l.LABEL, P('.') * (S('ST') * P('H') + P('.TP'))) -local rule5 = token(l.VARIABLE, - P('.B') * P('R')^-1 + P('.I') * S('PR')^-1 + P('.PP')) -local rule6 = token(l.TYPE, P('\\f') * S('BIPR')) -local rule7 = token(l.PREPROCESSOR, l.starts_line('.') * l.alpha^1) - -M._rules = { - {'whitespace', ws}, - {'rule1', rule1}, - {'rule2', rule2}, - {'rule3', rule3}, - {'rule4', rule4}, - {'rule5', rule5}, - {'rule6', rule6}, - {'rule7', rule7}, -} - -return M diff --git a/lua/lexers/troff.lua b/lua/lexers/troff.lua new file mode 100644 index 000000000..779f4b8ee --- /dev/null +++ b/lua/lexers/troff.lua @@ -0,0 +1,50 @@ +-- Copyright 2015-2017 David B. Lamkins . See LICENSE. +-- man/roff LPeg lexer. + +local l = require('lexer') +local token, word_match = l.token, l.word_match +local P, R, S = lpeg.P, lpeg.R, lpeg.S + +local M = {_NAME = 'troff'} + +-- Whitespace. +local ws = token(l.WHITESPACE, l.space^1) + +-- Markup. +local keywords = token(l.KEYWORD, (l.starts_line('.') * l.space^0 * (P('while') + P('break') + P('continue') + P('nr') + P('rr') + P('rnn') + P('aln') + P('\\}'))) + P('\\{')) + +local escape_sequences = token(l.VARIABLE, + P('\\') * ((P('s') * S('+-')) + S('*fgmnYV') + P("")) * + (P('(') * 2 + P('[') * (l.any-P(']'))^0 * P(']') + 1)) + +local headings = token(l.NUMBER, P('.') * (S('STN') * P('H')) * (l.space + P("\n")) * l.nonnewline^0) +local alignment = token(l.KEYWORD, + (P('.br') + P('.DS') + P('.RS') + P('.RE') + P('.PD')) * (l.space + P("\n"))) +local markup = token(l.VARIABLE, + (P('.B') * P('R')^-1 + P('.I') * S('PR')^-1 + P('.PP')) * (l.space + P("\n"))) + +local troff_plain_macros = token(l.VARIABLE, l.starts_line('.') * l.space^0 * l.lower^1) + +local any_macro = token(l.PREPROCESSOR, l.starts_line('.') * l.space^0 * (l.any-l.space)^0) + +local comment = token(l.COMMENT, (l.starts_line('.\\"') + P('\\"') + P('\\#')) * l.nonnewline^0) + +local in_dollars = token(l.EMBEDDED, P('$') * (l.any - P('$'))^0 * P('$')) + +local quoted = token(l.STRING, P('"') * (l.nonnewline-P('"'))^0 * (P('"') + (P("\n")))) + +M._rules = { + {'whitespace', ws}, + {'keywords', keywords}, + {'comment', comment}, + {'escape_sequences', escape_sequences}, + {'headings', headings}, + {'alignment', alignment}, + {'markup', markup}, + {'troff_plain_macros', troff_plain_macros}, + {'any_macro', any_macro}, + {'in_dollars', in_dollars}, + {'quoted', quoted}, +} + +return M diff --git a/lua/plugins/filetype.lua b/lua/plugins/filetype.lua index 90ebae3b2..2689820d1 100644 --- a/lua/plugins/filetype.lua +++ b/lua/plugins/filetype.lua @@ -258,11 +258,12 @@ vis.ftdetect.filetypes = { ext = { "%.iface$", "%.mak$", "%.mk$", "GNUmakefile", "makefile", "Makefile" }, mime = { "text/x-makefile" }, }, - man = { + troff = { ext = { "%.1$", "%.2$", "%.3$", "%.4$", "%.5$", "%.6$", "%.7$", "%.8$", "%.9$", "%.1x$", "%.2x$", "%.3x$", "%.4x$", - "%.5x$", "%.6x$", "%.7x$", "%.8x$", "%.9x$" + "%.5x$", "%.6x$", "%.7x$", "%.8x$", "%.9x$", + "%.ms$", "%.tr", "%.mm$", "%.mom$", "%.me$" }, }, markdown = {