可选部分的 Emacs 语法高亮

标签 emacs elisp font-lock emacs-faces

我正在尝试从基本模式编写派生模式。假设我有这个正则表达式:A ((foo)bar)? B,我如何告诉 emacs 使用以下面?

  • font-lock-keyword-face on A
  • font-lock-warning-face on foo(但不是 bar )
  • font-lock-constant-face on B

  • 我尝试使用以下代码:
    (defvar myregexp
      "\\(A\\) \\(?:\\(foo\\)bar \\)?\\(B\\)")
    
    (setq mylang-font-lock-keywords `(
      (, myregex 1 font-lock-keyword-face)
      (, myregex 2 font-lock-warning-face)
      (, myregex 3 font-lock-constant-face)
    ))
    

    但它不适用于字符串 A B (emacs 报告丢失的捕获)。

    最佳答案

    使用启用了 laxmatch 的 subexp 荧光笔让字体锁定模式忽略匹配中的缺失组:

    (setq mylang-font-lock-keywords
          `((,myregexp (1 font-lock-keyword-face)
                  (2 font-lock-warning-face nil t)
                  (3 font-lock-constant-face))))
    

    每个 subexp 突出显示的第四个元素是 laxmatch 参数。如果在 t 的匹配结果中找不到第一个元素中的相应组,则 myregexp font-lock-mode 忽略此荧光笔。

    有关更多信息,请参阅 Emacs Lisp 手册中的 Search-based Fontification

    关于可选部分的 Emacs 语法高亮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13216058/

    相关文章:

    由于缺少 libpng 无法配置 lubuntu 16.04 上的 Emacs 25.1

    regex - Lisp 正则表达式匹配带有或不带有常量类型标识符( 3.2 或 3.2f )的数字

    c++ - 匹配成员变量的正则表达式

    emacs - 我怎样才能在 emacs 中并排执行 "svn diff"

    emacs - 在 GNU emacs 中如何比较 cd、cd-absolute 和 default-directory?

    emacs - 为什么 Emacs 中应该有行号的地方有空格?

    emacs - 将数字转换为 base-2(二进制)字符串表示形式

    emacs - 为什么 font-lock-keyword-face 在 Emacs 中突出显示双引号内的文本?

    emacs - 从 emacs 中的 rgrep/grep 输出中删除 header 信息?

    emacs - Emacs 新手,Emacs 似乎无法正确解释我的按键?