【问题标题】:Context-sensitive discontinuous font-locking in EmacsEmacs 中上下文相关的不连续字体锁定
【发布时间】:2013-08-08 00:15:45
【问题描述】:

我正在尝试为主要模式设置字体锁定。下面是一些示例代码:

USING: foo bar bar ;

IN: fuelcolors

TUPLE: font < super-class
    name
    size
    bold?
    italic?
    { foreground initial: COLOR: black }
    { background initial: COLOR: white } ;

TUPLE: rgb red green blue ;

: foobar ( seq -- seq )
  hello there { 1 2 3 } ;

以下符号应该用一些面孔突出显示(没关系,我的问题是匹配部分):名称,大小,粗体?,斜体?,前景,背景,红色,绿色,蓝色。它们代表元组中槽的名称。

我知道正则表达式不会这样做,因为匹配的区域不是连续的。斜体?和前景应该匹配,但 不是 这些符号之间的 { 字符。因此,我认为我可以编写一个字体锁定匹配器功能,类似于 Dmitri 在这里提供的功能:Context-sensitive font-locking in emacs 用于非常相似的问题。

但是,他的解决方案利用了这样一个事实,即要突出显示的项目的“序列”在括号内,而这里不是这种情况。

Font-lock 在此类情况下会遇到麻烦 (Unknown number of matches in regex and font-lock),但我仍然希望有一些“足够好”的解决方案,即使它需要破解 font-lock 内部结构。

【问题讨论】:

  • 我不明白应该遵循什么规则。例如。为什么不barfoofuelcolors,...?
  • 只有 TUPLE: .... ; 表达式中的单词应该被突出显示。

标签: regex emacs elisp syntax-highlighting font-lock


【解决方案1】:

匹配器函数似乎很适合这个。

我会使用两个函数,一个用于匹配TUPLE 并设置成员搜索的限制,另一个用于查找每个元素的内部匹配器。可以编写内部函数以了解 { name ... } 构造。

诀窍在于内部函数为每个元素调用一次,因此永远不会出现匹配数未知的情况。

规则如下所示:

'(my-match-a-tuple
  (1 'font-lock-keyword-name-face)    ;; Highlight the word TUPLE
  (my-match-tuple-member
   ;; Pre-match form (can be used move the point around and to set search limits)
   nil
   ;; Post-match form (can be used to move point around afterwords)
   nil
   (1 'font-lock-variable-face)))

您可以查看我的用于字体化 cmake 脚本的包,它大量使用了匹配器功能:https://github.com/Lindydancer/cmake-font-lock

【讨论】:

  • 太好了,解决方案。 :) 但是让锚定匹配器正确对我来说太难了。
【解决方案2】:

这是我最终得到的解决方案:

(,"\\(TUPLE\\):[ \n]+\\(\\(?:\\sw\\|\\s_\\)+\\)\\(?:[ \n]+<[ \n]+\\(\\(?:\\sw\\|\\s_\\)+\\)\\)?"
     (1 'factor-font-lock-parsing-word)
     (2 'factor-font-lock-type-name)
     (3 'factor-font-lock-type-name nil t)
     ("\\(\\(?:\\sw\\|\\s_\\)+\\)\\|\\(?:{[ \n]+\\(\\(?:\\sw\\|\\s_\\)+\\)[^}]+\\)"
      ((lambda (&rest foo)
         (save-excursion
           (re-search-forward " ;" nil t)
           (1- (point)))))
      nil
      (1 'factor-font-lock-symbol nil t)
      (2 'factor-font-lock-symbol nil t)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多