gpt4 book ai didi

emacs - emacs 中的上下文相关字体锁定

转载 作者:行者123 更新时间:2023-12-04 22:48:22 26 4
gpt4 key购买 nike

基本上,我试图以我想要的方式突出显示以下 CoffeeScript 代码。 coffeescript函数的语法解释可以看here .

nameHere = (tstamp, moo, boo) ->
...

名称 tstamp、moo 和 boo 应该是粉红色的(没有别的,不是逗号,也不是括号),因为它们是 lambda 函数的参数。
highOrderFun ((x) -> x * x) someList

这里是第一个 x 是参数。参数可以有默认参数:
class Foo
meth: (msg = "Hello", bar = "foo") ->
....

默认参数本身可以是变量:
defColor = "red"
print = (msg, color = defColor) ->
...

所以 msgcolor上面应该突出显示,但不是 defColor .一个更棘手的情况是带有默认参数的函数,它们本身就是函数。我认为 emacs 的 font-lock 很难正确突出显示,但我还是把它包括在内:
funTakingFuns = (f1 = ((a, b) -> a*b), f2 = ((c, d) -> c/d)) ->
...

这似乎在 emacs 中实现起来相当复杂,因为您希望突出显示是上下文敏感的。我已经阅读了有关 font-lock 的文档,但无法弄清楚。

如果有人能告诉我要设置什么,我将不胜感激 font-lock-defaults使其语法突出显示我想要的方式。

更新 显示更多 CoffeeScript 语法示例。

最佳答案

font-lock-keywords允许 MATCHER 中的函数值 field :

where MATCHER can be either the regexp to search for, or the function name to call to make the search (called with one argument, the limit of the search; it should return non-nil, move point, and set match-data appropriately if it succeeds; like re-search-forward would).



所以我们需要编写一个函数来搜索缓冲区中的下一个函数参数。

像这样的东西:

    (defun coffee-match-next-argument (limit)
(let ((start (point)))
;; Look for the arrow.
(when (re-search-forward ") *->" limit t)
;; Save the position of the closing paren.
(let ((stop (point)))
(goto-char (match-beginning 0))
;; Go to the opening paren.
(goto-char (nth 1 (syntax-ppss)))
;; If we're before our initial position, go forward.
;; We don't want to find the same symbols again.
(when (> start (point))
(goto-char start))
;; Look for the next symbol until the arrow.
(or (re-search-forward "\\((\\|,\\) *\\(\\(\\sw\\|_\\)+\\)" stop 'mv)
(coffee-match-next-argument limit))))))

以及用于现有 coffee-mode 的设置:
(font-lock-add-keywords
'coffee-mode
'((coffee-match-next-argument 2 font-lock-variable-name-face)))

您也可以在 font-lock-defaults 中使用它, 当然。

这可能会使用粉色以外的其他颜色,但这很容易改变。

关于emacs - emacs 中的上下文相关字体锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14668225/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com