[英]Emacs: highlight 2 or more empty lines
I have entered the following code in my .emacs file to highlight unwanted white spaces. 我在.emacs文件中输入了以下代码,以突出显示不需要的空格。
(require 'whitespace)
(setq whitespace-style '(face empty tabs lines-tail trailing))
(global-whitespace-mode t)
This shows (1) empty lines at the beginning & end of buffer (2) tabs (3) lines which go over the 80 character limit (4) trailing white spaces 这显示(1)缓冲区(2)标签(3)行的开头和结尾处的空行超过80个字符限制(4)尾随空格
I would like emacs to automatically highlight '2 or more empty lines'. 我希望emacs能够自动突出显示“2个或更多空行”。 Any ideas on how to implement this?
有关如何实现这一点的任何想法? I did find a blog post explaining a way to do this with the help of regexp, but I am not sure how to implement this in .emacs file.
我找到了一篇博文 ,在regexp的帮助下解释了一种方法,但我不知道如何在.emacs文件中实现它。
Edit 1: Found a way to delete extra blank lines but this still doesn't help me with highlighting multiple blank lines automatically. 编辑1:找到一种删除多余空行的方法,但这仍然无法自动突出显示多个空白行。 delete extra blank lines in emacs
删除emacs中的额外空白行
Edit 2: Adding the following to .emacs seems to work, but only after I save and reopen file in a buffer. 编辑2:将以下内容添加到.emacs似乎可以工作,但只有在我保存并重新打开缓冲区中的文件之后。
(add-hook 'change-major-mode-hook '(lambda () (highlight-regexp "\\(^\\s-*$\\)\n" 'hi-yellow)))
Edit 3: After adding (global-hi-lock-mode 1)
to .emacs file just before the line in Edit 2, it seems to highlight 1 or more empty lines within the buffer. 编辑3:在编辑2中的行之前添加
(global-hi-lock-mode 1)
到.emacs文件后,它似乎突出显示缓冲区中的一个或多个空行。 I am not sure how to modify the regexp so that it will only accept 2 or more empty lines. 我不知道如何修改正则表达式,以便它只接受2个或更多空行。
Your highlight-regexp
-solution can be made into a minor-mode with the following elisp (eg, in your .emacs
file). 您的
highlight-regexp
-solution可以使用以下elisp(例如,在.emacs
文件中)制作为次要模式。
You can activate the minor mode by right-clicking onto one of the mode-names in the mode-line and then selecting nl2-mode
. 您可以通过右键单击模式行中的某个模式名称然后选择
nl2-mode
来nl2-mode
。 You can deactivate the minor mode by clicking on nl2
in the mode line and selecting Turn off minor mode
. 您可以通过单击模式行中的
nl2
并选择Turn off minor mode
来Turn off minor mode
。
To understand the code see the help for define-minor-mode
and define-key
(eg, Ch f define-minor-mode
RET ). 要理解代码,请参阅
define-minor-mode
和define-key
(例如, Ch f define-minor-mode
RET )。 Note, that in emacs also mouse clicks in menus count as key strokes. 请注意,在emacs中,菜单中的鼠标单击也算作击键。
(define-minor-mode nl2-mode
"Highlight two successive newlines."
:global t
:lighter " nl2"
(if nl2-mode
(highlight-regexp "\\(^\\s-*$\\)\n" 'hi-yellow)
(unhighlight-regexp "\\(^\\s-*$\\)\n")))
(define-key mode-line-mode-menu [nl2-mode]
`(menu-item ,(purecopy "nl2-mode") nl2-mode
:help "Highlight two succesive newlines."
:button (:toggle . (bound-and-true-p nl2-mode))))
There are several facts that make highlighting two consecutive empty lines more complicated ( font-lock
tends to only highlight non-empty regions, linebreaks are limits for the region to re-fontify, re-fontification after buffer changes are required). 有几个事实突出显示两个连续的空行更复杂(
font-lock
倾向于仅突出显示非空区域,换行符是区域重新定义的限制,需要缓冲区更改后重新确定)。 The following code shows one way. 以下代码显示了一种方法。 Maybe, there are easier ways.
也许,有更简单的方法。
(require 'font-lock)
(global-font-lock-mode)
(defface jit-lock-nl2-face '((default :background "yellow"))
"Face to indicate two or more successive newlines."
:group 'jit-lock)
(defun jit-nl2-extend (start end &optional old)
"Extend region to be re-fontified"
(save-excursion
(save-match-data
;; trailing:
(goto-char end)
(skip-chars-forward "[[:blank:]]\n")
(setq jit-lock-end (point))
;; leading:
(goto-char start)
(beginning-of-line)
(skip-chars-backward "[[:blank:]]\n")
(unless (bolp) (forward-line))
(setq jit-lock-start (point)))))
(defun jit-nl2 (jit-lock-start jit-lock-end)
"Highlight two or more successive newlines."
(save-excursion
(save-match-data
(jit-nl2-extend jit-lock-start jit-lock-end)
;; cleanup
(remove-text-properties jit-lock-start jit-lock-end '(font-lock-face jit-lock-nl2-face))
;; highlight
(while (< (point) jit-lock-end)
(if (looking-at "[[:blank:]]*\n\\([[:blank:]]*\n\\)+")
(progn (put-text-property (match-beginning 0) (match-end 0) 'font-lock-face 'jit-lock-nl2-face)
(goto-char (match-end 0)))
(forward-line))))))
(add-hook 'after-change-major-mode-hook (lambda ()
(add-hook 'jit-lock-after-change-extend-region-functions 'jit-nl2-extend)
(jit-lock-register 'jit-nl2)
(jit-lock-mode 1)
))
Just use library Highlight ( highlight.el
). 只需使用库突出显示 (
highlight.el
)。 That's what it's for. 这就是它的用途。
Use command hlt-highlight-regexp-region
( Cx X hx
) or hlt-highlight-regexp-to-end
( Cx X he
). 使用命令
hlt-highlight-regexp-region
( Cx X hx
)或hlt-highlight-regexp-to-end
( Cx X he
)。 (To unhighlight a regexp, use Cx X ux
or Cx X ue
.) (要取消高亮显示正则表达式,请使用
Cx X ux
或Cx X ue
。)
Interactively, you input the regexp to use as usual in Emacs (with Cq Cj
to match a newline character, and no need for double backslashes), so you type \\(^\\s-*$\\) Cq Cj
. 交互式地,您输入正则表达式以在Emacs中使用(使用
Cq Cj
匹配换行符,并且不需要双反斜杠),因此键入\\(^\\s-*$\\) Cq Cj
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.