-- lx format
local format = require("core.format")

format.register({
	name = "Rich Text Format",
	shortname = "RTF",
	author = "liqube",

	extensions = { "rtf" },

	headers = {
		"^{\\rtf%d+",
	},

	patterns = {
		-- Group open
		{ pattern = "{", type = "operator" },

		-- Group close
		{ pattern = "}", type = "operator" },

		-- Escaped hex byte
		{ pattern = "\\'[%da-fA-F][%da-fA-F]", type = "hex" },

		-- Escaped brace and slash
		{ pattern = "\\[%{%}\\]", type = "string" },

		-- Escaped special control symbol
		{ pattern = "\\[%~%-%_%*:%|]", type = "operator" },

		-- Control word with optional signed number
		{ pattern = "\\[%a]+%-?%d*", type = "keyword" },

		-- Control symbol fallback
		{ pattern = "\\[^%a%d%s]", type = "operator" },

		-- Line continuation backslash
		{ pattern = "(\\)%s*\n", type = { "meta" } },

		-- Plain decimal numbers
--		{ pattern = "%-?%d+", type = "number" },
	},

	line_patterns = {
		-- RTF header group
		{
			pattern = "({)()(\\rtf%d+)()",
			type = { "operator", "keyword2" },
			consume_to = 2,
		},

		-- Font table group
		{
			pattern = "({)()(\\fonttbl)()",
			type = { "operator", "preprocessor" },
			consume_to = 2,
		},

		-- Color table group
		{
			pattern = "({)()(\\colortbl)()",
			type = { "operator", "preprocessor" },
			consume_to = 2,
		},

		-- Stylesheet group
		{
			pattern = "({)()(\\stylesheet)()",
			type = { "operator", "preprocessor" },
			consume_to = 2,
		},

		-- Info group
		{
			pattern = "({)()(\\info)()",
			type = { "operator", "preprocessor" },
			consume_to = 2,
		},

		-- Picture group
		{
			pattern = "({)()(\\pict)()",
			type = { "operator", "preprocessor" },
			consume_to = 2,
		},

		-- Object group
		{
			pattern = "({)()(\\object)()",
			type = { "operator", "preprocessor" },
			consume_to = 2,
		},

		-- File table group
		{
			pattern = "({)()(\\filetbl)()",
			type = { "operator", "preprocessor" },
			consume_to = 2,
		},

		-- List table group
		{
			pattern = "({)()(\\listtable)()",
			type = { "operator", "preprocessor" },
			consume_to = 2,
		},

		-- List override table group
		{
			pattern = "({)()(\\listoverridetable)()",
			type = { "operator", "preprocessor" },
			consume_to = 2,
		},
	},

	symbols = {
		_expansion_flags = "r",

		-- Header and charset
		["\\rtf"] = "keyword2",
		["\\ansi"] = "keyword2",
		["\\mac"] = "keyword2",
		["\\pc"] = "keyword2",
		["\\pca"] = "keyword2",
		["\\ansicpg"] = "keyword2",
		["\\deff"] = "keyword2",
		["\\adeff"] = "keyword2",
		["\\deflang"] = "keyword2",
		["\\deflangfe"] = "keyword2",

		-- Destinations
		["\\fonttbl"] = "preprocessor",
		["\\filetbl"] = "preprocessor",
		["\\colortbl"] = "preprocessor",
		["\\stylesheet"] = "preprocessor",
		["\\listtable"] = "preprocessor",
		["\\listoverridetable"] = "preprocessor",
		["\\revtbl"] = "preprocessor",
		["\\info"] = "preprocessor",
		["\\pict"] = "preprocessor",
		["\\object"] = "preprocessor",
		["\\footer"] = "preprocessor",
		["\\footerl"] = "preprocessor",
		["\\footerr"] = "preprocessor",
		["\\footerf"] = "preprocessor",
		["\\header"] = "preprocessor",
		["\\headerl"] = "preprocessor",
		["\\headerr"] = "preprocessor",
		["\\headerf"] = "preprocessor",

		-- Structure
		["\\par"] = "control",
		["\\line"] = "control",
		["\\page"] = "control",
		["\\sect"] = "control",
		["\\tab"] = "control",
		["\\cell"] = "control",
		["\\row"] = "control",
		["\\nestcell"] = "control",
		["\\nestrow"] = "control",

		-- Character formatting
		["\\plain"] = "keyword",
		["\\b"] = "keyword",
		["\\i"] = "keyword",
		["\\ul"] = "keyword",
		["\\ulnone"] = "keyword",
		["\\strike"] = "keyword",
		["\\striked"] = "keyword",
		["\\scaps"] = "keyword",
		["\\caps"] = "keyword",
		["\\embo"] = "keyword",
		["\\impr"] = "keyword",
		["\\sub"] = "keyword",
		["\\super"] = "keyword",
		["\\nosupersub"] = "keyword",
		["\\fs"] = "keyword",
		["\\f"] = "keyword",
		["\\cf"] = "keyword",
		["\\cb"] = "keyword",
		["\\cbpat"] = "keyword",
		["\\highlight"] = "keyword",
		["\\lang"] = "keyword",
		["\\langfe"] = "keyword",

		-- Paragraph formatting
		["\\pard"] = "keyword",
		["\\ql"] = "keyword",
		["\\qr"] = "keyword",
		["\\qc"] = "keyword",
		["\\qj"] = "keyword",
		["\\li"] = "keyword",
		["\\ri"] = "keyword",
		["\\fi"] = "keyword",
		["\\sb"] = "keyword",
		["\\sa"] = "keyword",
		["\\sl"] = "keyword",
		["\\slmult"] = "keyword",
		["\\widctlpar"] = "keyword",
		["\\nowidctlpar"] = "keyword",
		["\\keep"] = "keyword",
		["\\keepn"] = "keyword",

		-- Metadata
		["\\title"] = "keyword2",
		["\\subject"] = "keyword2",
		["\\author"] = "keyword2",
		["\\manager"] = "keyword2",
		["\\company"] = "keyword2",
		["\\operator"] = "keyword2",
		["\\keywords"] = "keyword2",
		["\\comment"] = "keyword2",
		["\\doccomm"] = "keyword2",
		["\\creatim"] = "keyword2",
		["\\revtim"] = "keyword2",
		["\\printim"] = "keyword2",
		["\\buptim"] = "keyword2",

		-- Picture and binary payload markers
		["\\pngblip"] = "keyword2",
		["\\jpegblip"] = "keyword2",
		["\\emfblip"] = "keyword2",
		["\\wmetafile"] = "keyword2",
		["\\macpict"] = "keyword2",
		["\\bin"] = "keyword2",
		["\\objdata"] = "keyword2",
		["\\result"] = "keyword2",

		-- Unicode helpers
		["\\u"] = "literal",
		["\\uc"] = "literal",
	},
})
