简体   繁体   English

在 Emacs 的正则表达式机制中突出显示整行(从左边框到右边框)

[英]Highlight whole line (from left border to right border) in Emacs' regexp mechanism

I'm using我在用着

(highlight-regexp ".*data id=\"[^\"]*\".*" 'hi-green)

to highlight lines containing some regexp ( data id="..." ), but it only does so from the left window border to the last char in the line .突出显示包含一些正则表达式( data id="..." )的行,但它仅从左 window 边界到该行中的最后一个字符

How to get the whole line highlighted, up to the right window border ?如何突出显示整行,直到右侧 window 边框

UPDATE -- I do need to have some highlights on the whole line, others not:更新——我确实需要在整条线上有一些亮点,其他的则不需要:

;; whole line
(highlight-regexp ".*data id=\"[^\"]*\".*" 'hi-green)
(highlight-lines-matching-regexp ".*panel.* id=\"[^\"]*\".*" 'hi-blue)

;; part of the line
(highlight-regexp "data=\"[^\"]*\"" 'hi-green)
(highlight-regexp "action id=\"[^\"]*\"" 'hi-pink)

I don't think this is possible using the font-lock mechanism, which highlight-regexp uses when font-lock-mode is active.认为使用 font-lock 机制是不可能的,当font-lock-mode处于活动状态时, highlight-regexp会使用该机制。 However, highlight-regexp falls back to using overlays otherwise, and the region of the overlays can be modified to include the whole visible line.但是, highlight-regexp会退回到使用叠加层,并且可以修改叠加层的区域以包括整个可见线。

This behaviour can be forced around calls to hi-lock-set-pattern with the following advice,这种行为可以通过以下建议强制调用hi-lock-set-pattern

(define-advice hi-lock-set-pattern (:around (orig &rest args) "whole-line")
  (let (font-lock-mode)
    (cl-letf* ((orig-make-overlay (symbol-function 'make-overlay))
               ((symbol-function 'make-overlay)
                (lambda (&rest _args)
                  (funcall orig-make-overlay
                           (line-beginning-position) ; could start from match
                           (line-beginning-position 2)))))
      (apply orig args))))

Comment follow-up: to only highlight entire visible lines when calling highlight-regexp and the regexp ends with .* , you could use the following (don't use the previous advice)评论跟进:在调用highlight-regexp并且正则表达式以.*结尾时仅突出显示整个可见行,您可以使用以下内容(不要使用之前的建议)

(define-advice highlight-regexp (:around (orig regexp &rest args) "maybe-whole-line")
  (if (string-suffix-p ".*" regexp)
      ;; use modified overlay bounds
      (let (font-lock-mode)
        (cl-letf* ((orig-make-overlay (symbol-function 'make-overlay))
                   ((symbol-function 'make-overlay)
                    (lambda (&rest _args)
                      (funcall orig-make-overlay
                               (line-beginning-position) ; could start from match
                               (line-beginning-position 2)))))
          (apply orig regexp args)))
    (apply orig regexp args)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM