Created
May 31, 2016 10:10
-
-
Save xuchunyang/1eb3217be2e63a124f4e3d8a154f4db8 to your computer and use it in GitHub Desktop.
Reply to https://emacs-china.org/t/topic/454/7 - 高亮特定字符串的一个方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 用绿色高亮「申请人」、黄色高亮「被申请人」 | |
(defun my-highlight-some-text () | |
(hi-lock-set-pattern+ "被申请人" 0 'hi-yellow) | |
(hi-lock-set-pattern+ "[^被]\\(申请人\\)" 1 'hi-green)) | |
(add-hook 'org-mode-hook #'my-highlight-some-text) | |
(defun hi-lock-set-pattern+ (regexp group face) | |
"Highlight REGEXP with face FACE." | |
;; Hashcons the regexp, so it can be passed to remove-overlays later. | |
(setq regexp (hi-lock--hashcons regexp)) | |
(let ((pattern (list regexp (list 0 (list 'quote face) 'prepend)))) | |
;; Refuse to highlight a text that is already highlighted. | |
(unless (assoc regexp hi-lock-interactive-patterns) | |
(push pattern hi-lock-interactive-patterns) | |
(if (and font-lock-mode (font-lock-specified-p major-mode)) | |
(progn | |
(font-lock-add-keywords nil (list pattern) t) | |
(font-lock-flush)) | |
(let* ((range-min (- (point) (/ hi-lock-highlight-range 2))) | |
(range-max (+ (point) (/ hi-lock-highlight-range 2))) | |
(search-start | |
(max (point-min) | |
(- range-min (max 0 (- range-max (point-max)))))) | |
(search-end | |
(min (point-max) | |
(+ range-max (max 0 (- (point-min) range-min)))))) | |
(save-excursion | |
(goto-char search-start) | |
(while (re-search-forward regexp search-end t) | |
(let ((overlay (make-overlay (match-beginning group) (match-end group)))) | |
(overlay-put overlay 'hi-lock-overlay t) | |
(overlay-put overlay 'hi-lock-overlay-regexp regexp) | |
(overlay-put overlay 'face face)) | |
(goto-char (match-end group))))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
大大,无效果啊。


已经放到配置里。