【发布时间】:2014-06-01 23:42:20
【问题描述】:
我正在寻求一些帮助,请进一步修改highlight-parentheses库中的以下已修改摘录:https://github.com/nschum/highlight-parentheses.el [Fn 1.]
目标:目标是使用mapcar或dolist之类的东西自动将INSERT-FACE-HERE替换为不同的面孔从变量my-parens-faces 每次 while 执行一个循环。视觉效果将是基于嵌套级别的括号的彩虹色。
我正在使用post-command-hook 和类似于remove-overlays 的函数删除覆盖,然后使用下面的parens 函数添加新的覆盖。我不会移动任何叠加层——只是创建和删除。最终版本将使用面部变量并针对特定的覆盖层进行移除,但这里是它的外观示例:(add-hook 'post-command-hook (lambda () (remove-overlays) (parens)))
每次while 执行循环时,我想插入与变量my-parens-faces 不同的面——按顺序插入,例如dolist。例如:
while执行循环 #1:(:foreground "black" :background "cyan")while执行循环 #2:(:foreground "blue" :background "purple")while执行循环 #3:(:foreground "green" :background "blue")while执行循环 #4:(:foreground "yellow" :background "purple")while执行循环 #5:(:foreground "orange" :background "yellow")while执行循环 #6:(:foreground "red" :background "green")while执行循环 #7:(:foreground "pink" :background "brown")while执行循环 #8:(:foreground "blue" :background "beige")
(defun parens ()
(let* (pos1 pos2)
(save-excursion
(condition-case err
(while (setq pos1 (cadr (syntax-ppss pos1)))
(overlay-put (make-overlay pos1 (1+ pos1)) 'face 'INSERT-FACE-HERE)
(when (setq pos2 (scan-sexps pos1 1))
(overlay-put (make-overlay (1- pos2) pos2) 'face 'INSERT-FACE-HERE)))
(error nil)) )))
(defvar my-parens-faces '(
(:foreground "black" :background "cyan")
(:foreground "blue" :background "purple")
(:foreground "green" :background "blue")
(:foreground "yellow" :background "purple")
(:foreground "orange" :background "yellow")
(:foreground "red" :background "green")
(:foreground "pink" :background "brown")
(:foreground "blue" :background "beige")))
[脚注 1:highlight-parentheses 库的引用是不需要来回答这个问题的,但引用被包括在内以便为在此问题中启发 parens 函数的作者(即 Nikolaj Schumacher)赋予适当的属性。]
【问题讨论】:
-
你有什么理由不只使用
rainbow-delimiters-mode? -
@Justin Wood -- 我正在编写一个不相关的自定义次要模式,由于使用函数
move-overlay而不是删除叠加层而导致杂乱的叠加层对象放置在周围而受到干扰。上面名为parens的函数可以工作,它只需要添加一个dolist- 但是,在处理while和dolist时我会感到困惑并且不知道如何处理它。我通过只在其中构建我需要的基本功能来消除我的次要模式中的冲突——在这种情况下,一旦我了解如何一起使用dolist和while,parens将满足我的需求。跨度> -
@lawlist 您的次要模式被任意叠加搞糊涂了?!为什么这特别是由
move-overlay引起的?难道你不能改变你的次要模式只在它自己创建的叠加层上工作吗? -
@lunaryorn -- 有两个可能的嫌疑人。使用库
highlight-parentheses,在point-min创建了多个覆盖对象,并且它们随着post-command-hook的每次连续运行而累积——这逐渐减慢了我将覆盖从window-start放置到window-end的无关函数。第二个可能的嫌疑人是当make-overlay在point而非overlay-put上执行时,完全删除该覆盖是有问题的。移除,而不是移动,效果很好;与overlay-put一起执行make-overlay也是如此。 -
@lawlist 我看不出有什么明显的理由说明为什么其中任何一个都应该与来自另一个库(如彩虹分隔符)的覆盖冲突。