Emacs - 创建自定义突出显示括号功能 [英] Emacs -- creating a custom highlight parentheses function
问题描述
我正在寻找一些帮助,请进一步修改高亮括号中的以下已修改摘录
库: https://github.com/nschum/highlight-parentheses.el [Fn 1。]
目标:目标是使用类似 mapcar
或 dolist
自动替换 INSERT-FACE-HERE
/ em> face从变量 my-parens-faces
每次 而
。视觉效果将是基于嵌套级别的括号中的彩虹着色。
我正在使用后命令钩来删除叠加层
和类似于 remove-overlays
的函数,然后随后添加新的覆盖层与 parens
功能如下。我不会移动任何覆盖 - 只是创建和删除。最终版本将使用面部和目标特定重叠的变量进行删除,但下面是一个示例:(add-hook'post-command-hook(lambda()(remove - code code code code code code code code code code code code $我想从变量
my-parens-faces
中插入一个不同的面,就像 dolist
一样。例如:
-
while
执行循环#1:(:前景黑色:背景青色)
-
while
do loop#2:(:foregroundblue:backgroundpurple)
- while do loop#3:
(:foregroundgreen:backgroundblue)
$>
-
while
do loop#4:(:foregroundyellow:backgroundpurple )
-
while
执行循环#5:(:前景橙色:背景黄色)
-
while
做循环#6:(:前景红色:背景绿色)
-
while
do loop#7:(:foregroundpink:backgroundbrown)
(defun parens()
pre>
(let *(pos1 pos2)
(save-excursion
(条件案例错误
(while(setq pos1(cadr(syntax-ppss pos1 ))
(覆盖放置(make-overlay pos1(1+ pos1))'face'INSERT-FACE-HERE)
(当(setq pos2(scan-sexps pos1 1))
(overlay-put(make-overlay(1-pos2)pos2)'face'INSERT-FACE-HERE)))
(error nil)))))
(defvar
(:前景黑色:背景青色)
(:前景蓝色:背景紫色)
(:前景绿色
(:前景黄色:背景紫色)
(:前景橙色:背景黄色)
(:前景红色:背景绿色)
(:前景粉红色:背景棕色)
(:前景蓝色:背景米色)))
[脚注编号1 :引用高亮括号库不是 >需要ans提到这个问题,但是这个引用被包括在内,以便对作者(即Nikolaj Schumacher)作出正确的属性,这个属性激发了这个问题中的
括号
函数。 p>
解决方案
(defvar parens-mode-command-exclusions' mwheel-scroll scroll-up scroll-down)
从触发函数括号中排除的函数列表)
(defvar parens-mode-syntax-table
(let((st-make-syntax-table)))
st)
执行函数parens时使用的语法表
(defgroup
:group'parens)
(defface parens-one-face
'(( t
(defface parens-two-face)
((前景洋红色)))
'((t(:foregroundred)))
面对`accords-two-face'。
:group'parens)
(defface parens-three-face
'((t(:foregroundyellow))
(defface parens-four-face
'((t(:foregroundgreen)))$ b $ b
(defface parens-five-face
'((t(:foregroundcyan) )b)
(defface parens-six-face
'( (t(:前景橙色)))
面对括号六面。
:组'括号)
(defface parens-seven-
'((t(:前景紫色)))
面对括号七面。
:组'括号)
(defface parens-eight-face
'((t(:foregroundblue)))
面对`parens-eight-face'。
:group'parens)
(defface parens-nine-face
'((t(:foregroundbrown)))
面对parens-nine-face
:group括号)
(defface parens-ten-face
'((t(:foregroundwhite)))
面对parens-ten-face。
:group'parens)
(defvar parens-overlays-exist-p nil
简单的测试,看看括号是否已经被放置。)
(make-variable-buffer-local'parens-overlays-exists-p)
(defun parens()
这个函数的一部分从库借来
`highlight由尼古拉·舒马赫(Nikolaj Schumacher)撰写。
https://github.com/nschum/highlight-parentheses.el
(except(memq this-command parens-mode-command-exclusions)
(with-syntax-table parens -mode-syntax-table
(let *(
(pt(point))
(pos1(如果
(或
(= pt(point-min) )
(eq(previous-char)40);;开放括号
(eq(previous-char)91);; open-squre-bracket
(eq(previous-char) 123));; open-wavy-bracket
pt
(1-pt)))
pos2
selected-face
(i 0))
(remove-parens-overlays)
(save-excursion
(condition-case nil
(while(setq pos1(cadr(syntax-ppss pos1)))
(= i 10)
(setq i 1)
(setq i(1+ i)))
(cond
((= i 1)
(setq selected-face'parens-one-face))
((= i 2)
(setq selected-face'parens-two-face))
((= i 3)
(setq selected-face'parens-three-face))
((= i 4)
(setq selected-face'parens-four-face))
(( = i 5)
(setq selected-face'parens-five-face))
((= i 6)
(setq selected-face'parens-six-face))
((= i 7)
(setq selected-face'parens-seven-face))
((= i 8)
(setq selected-face'parens-eight-face )
((= i 9)
(setq selected-face'parens-nine-face))
((= i 10)
(setq selected-face' -ten-face)))
(overlay-put(make-overlay pos1(1+ pos1))'face selected-face)
(当(setq pos2(scan-sexps pos1 1))
(覆盖(make-overlay(1-pos2)pos2)'face selected-face)))
(error nil)))
(setq parens-overlays-exists-p t)))))
(defun remove-parens-overlays()
(当parens-overlays-exists-p
(dolist(face'(
parens-one-face
parens-two-face
parens-three-face
parens-four-face
parens -five-face
parens-six-face
parens-seven-face
parens-eight-face
parens-nine-face
parens-ten-face) )
(remove-overlays nil nil'face face))
(setq parens-overlays-exist-p nil))
(defun turn-off-parens-mode ()
(parens-mode -1))
(define-minor-mode parens-mode
用于突出显示括号的次模式
: init-value nil
:light<>
:keymap nil
:global nil
:group'parens
(c ond
(accords-mode
(add-hook'post-command-hook'parens tt)
(add-hook'change-major-mode-hook'turn-off-parens-模式nil t)
(当(称为交互式p'任何)
(消息Turned ON`parens-mode`。)))
(t
-hook'post-command-hook'括号t
(remove-hook'change-major-mode-hook'turn-off-parens-mode t)
(remove-parens-overlays)
(当(称为交互式p'任何)
(消息Turned OFF`parens-mode`。)))))
I'm looking for some assistance, please, further modifying the following already modified excerpt from the
highlight-parentheses
library: https://github.com/nschum/highlight-parentheses.el [Fn 1.]GOAL: The goal is to use something like
mapcar
ordolist
to automatically replaceINSERT-FACE-HERE
with a different face from the variablemy-parens-faces
each timewhile
does a loop. The visual effect will be a rainbow coloring of parentheses based on the level of nesting.I am removing the overlays with a
post-command-hook
and a function similar toremove-overlays
, and then subsequently adding new overlays with theparens
function below. I will not be moving any overlays -- just creating and deleting. The final version will use variables for the faces and target specific overlays for removal, but here is a sample of what it will look like:(add-hook 'post-command-hook (lambda () (remove-overlays) (parens)))
Each time
while
does a loop, I want to insert a different face from the variablemy-parens-faces
-- going in order, likedolist
. For example:
while
doing loop # 1:(:foreground "black" :background "cyan")
while
doing loop # 2:(:foreground "blue" :background "purple")
while
doing loop # 3:(:foreground "green" :background "blue")
while
doing loop # 4:(:foreground "yellow" :background "purple")
while
doing loop # 5:(:foreground "orange" :background "yellow")
while
doing loop # 6:(:foreground "red" :background "green")
while
doing loop # 7:(:foreground "pink" :background "brown")
while
doing loop # 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")))
[Footnote number 1: Reference to the highlight-parentheses library is not needed to answer this question, but the reference is being included so that proper attribute is made to the author (i.e., Nikolaj Schumacher) who inspired the parens
function in this question.]
(defvar parens-mode-command-exclusions '(mwheel-scroll scroll-up scroll-down)
"List of functions that are excluded from triggering the function `parens'.")
(defvar parens-mode-syntax-table
(let ((st (make-syntax-table)))
st)
"Syntax table used while executing the function `parens'.")
(defgroup parens nil
"Faces for highlighting parentheses in `parens-mode'."
:group 'parens)
(defface parens-one-face
'((t (:foreground "magenta")))
"Face for `parens-one-face'."
:group 'parens)
(defface parens-two-face
'((t (:foreground "red")))
"Face for `parens-two-face'."
:group 'parens)
(defface parens-three-face
'((t (:foreground "yellow")))
"Face for `parens-three-face'."
:group 'parens)
(defface parens-four-face
'((t (:foreground "green")))
"Face for `parens-four-face'."
:group 'parens)
(defface parens-five-face
'((t (:foreground "cyan")))
"Face for `parens-five-face'."
:group 'parens)
(defface parens-six-face
'((t (:foreground "orange")))
"Face for `parens-six-face'."
:group 'parens)
(defface parens-seven-face
'((t (:foreground "purple")))
"Face for `parens-seven-face'."
:group 'parens)
(defface parens-eight-face
'((t (:foreground "blue")))
"Face for `parens-eight-face'."
:group 'parens)
(defface parens-nine-face
'((t (:foreground "brown")))
"Face for `parens-nine-face'."
:group 'parens)
(defface parens-ten-face
'((t (:foreground "white")))
"Face for `parens-ten-face'."
:group 'parens)
(defvar parens-overlays-exist-p nil
"Simple test to see whether the parens overlays have been placed.")
(make-variable-buffer-local 'parens-overlays-exist-p)
(defun parens ()
"Portions of this function were borrowed from the library
`highlight-parentheses` written by Nikolaj Schumacher.
https://github.com/nschum/highlight-parentheses.el"
(unless (memq this-command parens-mode-command-exclusions)
(with-syntax-table parens-mode-syntax-table
(let* (
(pt (point))
(pos1 (if
(or
(= pt (point-min))
(eq (preceding-char) 40) ;; open-parentheses
(eq (preceding-char) 91) ;; open-squre-bracket
(eq (preceding-char) 123)) ;; open-wavy-bracket
pt
(1- pt)))
pos2
selected-face
(i 0) )
(remove-parens-overlays)
(save-excursion
(condition-case nil
(while (setq pos1 (cadr (syntax-ppss pos1)))
(if (= i 10)
(setq i 1)
(setq i (1+ i)))
(cond
((= i 1)
(setq selected-face 'parens-one-face))
((= i 2)
(setq selected-face 'parens-two-face))
((= i 3)
(setq selected-face 'parens-three-face))
((= i 4)
(setq selected-face 'parens-four-face))
((= i 5)
(setq selected-face 'parens-five-face))
((= i 6)
(setq selected-face 'parens-six-face))
((= i 7)
(setq selected-face 'parens-seven-face))
((= i 8)
(setq selected-face 'parens-eight-face))
((= i 9)
(setq selected-face 'parens-nine-face))
((= i 10)
(setq selected-face 'parens-ten-face)) )
(overlay-put (make-overlay pos1 (1+ pos1)) 'face selected-face)
(when (setq pos2 (scan-sexps pos1 1))
(overlay-put (make-overlay (1- pos2) pos2) 'face selected-face)))
(error nil) ))
(setq parens-overlays-exist-p t)))))
(defun remove-parens-overlays ()
(when parens-overlays-exist-p
(dolist (face '(
parens-one-face
parens-two-face
parens-three-face
parens-four-face
parens-five-face
parens-six-face
parens-seven-face
parens-eight-face
parens-nine-face
parens-ten-face))
(remove-overlays nil nil 'face face))
(setq parens-overlays-exist-p nil)))
(defun turn-off-parens-mode ()
(parens-mode -1))
(define-minor-mode parens-mode
"A minor-mode for highlighting parentheses."
:init-value nil
:lighter " ‹›"
:keymap nil
:global nil
:group 'parens
(cond
(parens-mode
(add-hook 'post-command-hook 'parens t t)
(add-hook 'change-major-mode-hook 'turn-off-parens-mode nil t)
(when (called-interactively-p 'any)
(message "Turned ON `parens-mode`.")))
(t
(remove-hook 'post-command-hook 'parens t)
(remove-hook 'change-major-mode-hook 'turn-off-parens-mode t)
(remove-parens-overlays)
(when (called-interactively-p 'any)
(message "Turned OFF `parens-mode`.")))))
这篇关于Emacs - 创建自定义突出显示括号功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!