我正在尝试为日志文件编写自定义语法突出显示。每个日志行都以定义明确的标题开头,例如:
TRACE: text text text
DEBUG: text text text
TRACE 和 DEBUG 行将有不同的颜色,例如:
:syn match logTrace "^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|TRACE|.*"
:syn match logDebug "^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|DEBUG|.*"
hi def logTrace ctermfg=darkgray
hi def logDebug ctermfg=lightgray
问题是在某些情况下日志条目会跨越多行,例如:
TRACE: Here's a list of files:
file1.ext
file2.ext
file3.ext
DEBUG: text text text
因此我的问题是:有什么方法可以告诉语法高亮引擎继续使用与最后检测到的行(上例中的 TRACE 行)相同的颜色为未识别的行(例如上例中的 file1.ext)着色)?
谢谢你。
编辑:
上面的例子是一个简化的例子。我的日志实际上还包含 INFO、ERROR、EXCEPTION 和 FATAL 日志类型,并且每一行都以时间戳开头。日志行看起来像这样:
2013-04-11 13:36:05|TRACE|texttexttexttexttext
2013-04-11 13:36:07|INFO|texttexttexttexttexttext
another line
another line
2013-04-11 13:36:10|DEBUG|texttexttexttext
2013-04-11 13:36:10|ERROR|texttexttexttexttexttext
another line
another line
another line
2013-04-11 13:36:10|DEBUG|texttexttexttext
我最终实现了 Ineo 的区域理念。这是它的外观:
:syn region logTrace start=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|TRACE|+ end=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|+me=e-22
:syn region logDebug start=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|DEBUG|+ end=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|+me=e-22
:syn region logInfo start=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|INFO|+ end=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|+me=e-22
:syn region logError start=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|ERROR|+ end=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|+me=e-22
:syn region logException start=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|EXCEPTION|+ end=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|+me=e-22
:syn region logFatal start=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|FATAL|+ end=+^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}|+me=e-22
- 虽然它看起来有点乱——它工作得很好(我宁愿将日期/时间模式表示为一个变量,但目前这超出了我的 VIM 技能)。