行首每组空格的正则表达式

标签 regex emacs elisp

我正在尝试修复 highlight indentation 的正则表达式emacs 的模块。它目前的工作方式是每隔 %s 个空格突出显示一次:

"\\( \\) \\{%s\\}"

下面是一些 Verilog 代码行为的示例结果:

enter image description here

当前的正则表达式适用于每行开头的缩进。但是,存在一个不受欢迎的伪影,即不在行首的空格序列也被正则表达式匹配。 我想修改正则表达式以匹配上述行为,但仅限于行首。这就是我想要的 (我不得不手动绘制它):

enter image description here

这是我尝试过的。从结果中可以看出行为不对。
"^\\(\\( \\) \\{%s\\}\\) \\{1,\\}"

结果:

enter image description here

也试过
"^\\(\\( \\) \\{%s\\}\\)\\{1,\\}"

结果:

enter image description here

这是相关代码,但我很抱歉这不是一个工作示例。请使用上面的链接作为一个工作示例。
(set (make-local-variable 'highlight-indentation-current-regex)
     (format "\\( \\) \\{%s\\}" (- highlight-indentation-offset 1)))
(font-lock-add-keywords nil `((,highlight-indentation-current-regex
                               (1 'highlight-indentation-face))))

最佳答案

假设您当前的字体锁定规则如下所示:

(,highlight-indentation-current-regex (1 'highlight-indentation-face))

你可以使用
(,highlight-indentation-current-regex (1 (if (save-excursion (skip-chars-backward " \t") (bolp)) 'highlight-indentation-face)))

关于行首每组空格的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17521201/

相关文章:

javascript - 正则表达式的替代方案 $1 进行替换

javascript - 行为不当的正则表达式

git - Emacs 可以在不需要 git.el 的情况下显示文件的完整 git 历史记录吗?

emacs:无法杀死 Minibuffer

regex - 在 awk 中使用 Perl 正则表达式属性

c# - 如何从字符串的开头和结尾删除 char (")?

emacs - 检测文件,使其加载填充列值为 67

emacs - 使用 ido 插入文件名

emacs - 在 Emacs Paredit 中交换括号和方括号

emacs - 在 emacs evil 中的视觉垂直 block 选择中增加数字