简体   繁体   English

如何测试Emacs Lisp代码的`font-lock-keywords`值

[英]How to test `font-lock-keywords` values for Emacs Lisp code

I pose the question because I think both the question and possible answers might help Emacs users who write Lisp code that defines font-lock-keywords . 我提出这个问题,因为我认为问题和可能的答案可能会帮助编写定义font-lock-keywords Lisp代码的Emacs用户。 I'm providing one answer that I think helps. 我提供了一个我觉得有帮助的答案。 I'm also interested in other answers. 我也对其他答案感兴趣。

That variable's value is a list of expressions, each of which can specify one or more patterns to match or functions to perform matching, and one or more faces for highlighting the matching text. 该变量的值是表达式列表,每个表达式可以指定一个或多个匹配或用于执行匹配的模式,以及一个或多个用于突出显示匹配文本的面。 The possibilities for font-lock-keywords values are numerous and complicated. font-lock-keywords值的可能性很多且很复杂。 (The doc describing this is the Elisp manual, node Search-based Fontification .) (描述这个的文档是Elisp手册,节点Search-based Fontification 。)

In most cases the list has more than one element, which means more than one regexp pattern. 在大多数情况下,列表具有多个元素,这意味着不止一个正则表达式模式。 These can interact in different ways. 这些可以以不同方式相互作用。 Some can prevent others from taking effect, or they can alter the effect of others. 有些可以阻止他人生效,或者他们可以改变他人的影响。 My library Dired+ , for instance, defines font-lock-keywords in Dired mode with 31 entries (regexps), many of which interact. 例如,我的库Dired +在Dired模式下定义了带有31个条目(正则表达式)的font-lock-keywords ,其中许多条目是交互式的。

How to keep all of that straight? 如何保持这一切? How do you debug such a list when you are defining it or modifying it? 如何进行调试,当你定义它或修改它这样的名单? You might comment out all but one of the list items, to see its effect when alone. 您可以注释掉除列表项之外的所有项目,以便单独查看其效果。 And then repeat for another. 然后重复另一个。 And then perhaps add a few together, and maybe in different orders. 然后可能会添加一些,也许可以按不同的顺序添加。 There are various possibilities, I suppose, but just what do you do? 我想,有各种各样的可能性,但你做了什么?

(OK, I know that most Elisp coders do not write super complex font-lock-keywords definitions. But even for simple definitions this can become complicated. And perhaps if this process were easier then users would not unnecessarily limit themselves to only one or two entries.) (好吧,我知道大多数Elisp程序员都不会编写超级复杂的font-lock-keywords定义。但即使是简单的定义,这也会变得复杂。而且如果这个过程更容易,那么用户不会不必要地将自己限制在只有一两个项)。

You could use my newly released Font Lock Studio . 您可以使用我新发布的Font Lock Studio The following is from the readme-file: 以下内容来自自述文件:

font-lock-studio - interactive debugger for Font Lock keywords font-lock-studio - Font Lock关键字的交互式调试器

Font Lock Studio is an interactive debugger for Font Lock keywords (Emacs syntax highlighting rules). Font Lock Studio是Font Lock关键字的交互式调试器 (Emacs语法高亮规则)。

Introduction 介绍

Font Lock Studio lets you single-step Font Lock keywords -- matchers, highlights, and anchored rules, so that you can see what happens when a buffer is fontified. Font Lock Studio允许您单步执行字体锁定关键字 - 匹配器,突出显示和锚定规则,以便您可以看到缓冲区已经完成时会发生什么。 You can set breakpoints on or inside rules and run until one has been hit. 您可以在规则上或规则内设置断点 ,然后运行直到命中一个断点 When inside a rule, matches are visualized using a palette of background colors. 在规则内部时,使用背景颜色调色板可视化匹配。 The explainer can describe a rule in plain-text english. 解释者可以用纯文本英语描述规则。 Tight integration with Edebug allows you to step into Lisp expressions that are part of the Font Lock keywords. Edebug的紧密集成允许您进入作为Font Lock关键字一部分的Lisp表达式。

When using the debugger, an interface buffer is displayed, it contains all the keywords and is used for navigation and visalization of match data. 使用调试器时,会显示一个接口缓冲区 ,它包含所有关键字,用于匹配数据的导航和访问。

When Font Lock Studio is started, comments and strings are pre-colored, as they are part of the earlier syntactic phase (which isn't supported by Font Lock Studio). 当Font Lock Studio启动时,注释和字符串会预先着色,因为它们是早期语法阶段的一部分 (Font Lock Studio不支持)。

Start the debugger by typing "Mx font-lock-studio RET". 键入“Mx font-lock-studio RET”启动调试器。 Press ? ? or see the menu for available commands. 或查看可用命令的菜单。

Example

For a buffer using html-mode , the interface buffer looks the following. 对于使用html-mode的缓冲区,接口缓冲区看起来如下。 Other major modes typically have more and more complex rules. 其他主要模式通常具有越来越复杂的规则。 The arrow on the left indicates the current active location. 左侧的箭头表示当前的活动位置。 A corresponding arrow in the source buffer is placed at the current search location. 源缓冲区中的相应箭头位于当前搜索位置。

        ========================
        === Font Lock Studio ===
        ========================
    --------------------------------------------------
=>  "<\\([!?][_:[:alpha:]][-_.:[:alnum:]]*\\)"
      (1 font-lock-keyword-face)
    --------------------------------------------------
    "</?\\([_[:alpha:]][-_.[:alnum:]]*\\)\\(?::\\([_:[:alpha:]]
    [-_.:[:alnum:]]*\\)\\)?"
      (1
       (if
           (match-end 2)
           sgml-namespace-face font-lock-function-name-face))
      (2 font-lock-function-name-face nil t)
    --------------------------------------------------
    "\\(?:^\\|[ \t]\\)\\([_[:alpha:]][-_.[:alnum:]]*\\)\\(?::
    \\([_:[:alpha:]][-_.:[:alnum:]]*\\)\\)?=[\"']"
      (1
       (if
           (match-end 2)
           sgml-namespace-face font-lock-variable-name-face))
      (2 font-lock-variable-name-face nil t)
    --------------------------------------------------
    "[&%][_:[:alpha:]][-_.:[:alnum:]]*;?"
      (0 font-lock-variable-name-face)
    --------------------------------------------------
    "<\\(b\\(?:ig\\|link\\)\\|cite\\|em\\|h[1-6]\\|rev\\|s\\(?:
    mall\\|trong\\)\\|t\\(?:itle\\|t\\)\\|var\\|[bisu]\\)
    \\([ \t][^>]*\\)?>\\([^<]+\\)</\\1>"
      (3
       (cdr
        (assoc-string
         (match-string 1)
         sgml-tag-face-alist t))
       prepend)
    ==================================================
    Public state:
      Debug on error     : YES
      Debug on quit      : YES
      Explain rules      : YES
      Show compiled code : NO

Press space to single step through all the keywords. 按空格键以单步执行所有关键字。 "n" will go the the next keyword, "b" will set a breakpoint, "g" will run to the end (or to the next breakpoint) and "q" will quit. “n”将转到下一个关键字,“b”将设置断点,“g”将运行到结尾(或下一个断点),“q”将退出。

Features 特征

Stepping 步进

You can single step into , over , and out of Font Lock keywords. 您可以单步进入结束退出 Font Lock关键字。 Anchored rules are fully supported. 完全支持锚定规则。 In addition, you can run to the end or to the next breakpoint. 此外,您可以运行到结束或下一个断点。

Breakpoints 断点

You can set breakpoints on part of the keyword, like the matcher (eg the regexp), a highlight rule, or inside an anchored highlight rule. 您可以在关键字的一部分上设置断点,例如匹配器(例如正则表达式),突出显示规则或锚定突出​​显示规则内部。

If you want to step or run without stopping on breakpoints, prefix the command with Cu . 如果要在不停止断点的情况下步进或运行,请在命令前加上Cu

Note that in an anchored rule, you can set a breakpoints either on the entire rule or on an individual part. 请注意,在锚定规则中,您可以在整个规则或单个部件上设置断点。 In the former case, only the outer parentheses are highlighted. 在前一种情况下,仅突出显示外括号。

Match Data Visualization 匹配数据可视化

After the matcher of a keyword or anchored highlight has been executed, the match data (whatever the search found) is visualized using background colors in the source buffer, in the regexp, and over the corresponding highlight rule or rules. 在执行关键字或锚定突出​​显示的匹配器之后,使用源缓冲区,正则表达式中的背景颜色以及相应的突出显示规则或规则来显示匹配数据(无论搜索找到什么)。 If part of a regexp or a highlight didn't match, it is not colored, this can for example happen when the postfix regexp operator ? 如果正则表达式或突出显示的一部分不匹配,则它不会着色,例如,当后缀regexp运算符时会发生这种情况? is used. 用来。

Note that an inner match group gets precedence over an outer group. 请注意,内部匹配组优先于外部组。 This can lead to situations where a highlight rule gets a color that doesn't appear in the regexp or in the source buffer. 这可能导致突出显示规则获得未出现在正则表达式或源缓冲区中的颜色的情况。 For example, the matcher "\\(abc\\)" will be colored with the color for match 1, while the higlight rule `(0 a-face)' gets the color for match 0. 例如,匹配器“\\(abc \\)”将使用匹配1的颜色着色,而higlight规则“(0 a-face)”将获得匹配0的颜色。

Normalized keywords 规范化关键字

The keywords presented in the interface have been normalized. 界面中显示的关键字已经标准化。 For example, instead of 例如,而不是

     ("xyz" . font-lock-type-face)

the keyword 关键字

      ("xyz" (0 font-lock-type-face))

is shown. 显示。 See font-lock-studio-normalize-keywords for details. 有关详细信息,请参阅font-lock-studio-normalize-keywords

Explainer 解释器

The explainer echoes a human-readble description of the current part of the Font Lock keywords. 解释器回应了Font Lock关键字当前部分的人类可读描述。 This help you to understand that all those nil :s and t :s in the rules actually mean. 这有助于您理解规则中的所有nil :s和t :s实际上意味着什么。

When using the auto explainer , Font Lock Studio echoes the explanation after each command. 使用自动解释器时 ,Font Lock Studio会在每个命令后回复说明。

Edebug -- the Emacs Lisp debugger Edebug - Emacs Lisp调试器

Tight integration with Edebug allows you to single-step expressions embedded in the keywords in the interface buffer, and it allows you to instrument called functions for debugging in their source file. 与Edebug的紧密集成允许您在接口缓冲区中的关键字中嵌入单步表达式,并允许您在源文件中设置调用函数以进行调试。

Follow mode awareness 遵循模式意识

The search location in the source buffer is visualized by an overlay arrow and by updating the point. 源缓冲区中的搜索位置通过覆盖箭头和更新点进行可视化。 If the source buffer is visible in multiple side-by-side windows and Follow mode is enabled, the search location will be shown in a suitable windows to minimize scrolling. 如果源缓冲区在多个并排窗口中可见并且启用了跟随模式,则搜索位置将显示在合适的窗口中以最小化滚动。

To help with this problem, I coded up an Icicles multi-command , icicle-font-lock-keywords . 为了解决这个问题,我编写了一个Icicles 多命令icicle-font-lock-keywords It lets you do things like the following: 它允许您执行以下操作:

  • Cycle among the separate font-lock-keywords entries (patterns), applying them individually to see the effect of each alone. 在单独的font-lock-keywords条目(模式)之间循环,单独应用它们以查看每个条目的效果。

  • Pick individual entries and apply them separately, to see the same thing. 选择单个条目并单独应用它们以查看相同的内容。

  • Pick a set of entries and apply it, in the same order the entries appear in font-lock-keywords . 选择一条目并应用它,条目以相同的顺序出现在font-lock-keywords You can do this for any number of sets. 您可以为任意数量的集合执行此操作。

  • Accumulate the effect of multiple sets of entries, in the order you choose them. 按照您选择的顺序累计多组条目的效果。

  • Revert, to see the effect of all entries together, ie, all of font-lock-keywords . 还原,以查看所有条目的效果,即所有font-lock-keywords

And you can do all of that, in any order, in a single invocation of the command. 您可以在命令的单个调用中以任何顺序执行所有这些操作。

Mo is the prefix key for Facemenu and font-locking, so I put this command on key Mo I , when in Icicle mode. Mo是Facemenu和字体锁定的前缀键,因此我在Icicle模式下将此命令放在键Mo I

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM