Skip to content

Instantly share code, notes, and snippets.

@xuchunyang
Created May 31, 2016 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuchunyang/1eb3217be2e63a124f4e3d8a154f4db8 to your computer and use it in GitHub Desktop.
Save xuchunyang/1eb3217be2e63a124f4e3d8a154f4db8 to your computer and use it in GitHub Desktop.
Reply to https://emacs-china.org/t/topic/454/7 - 高亮特定字符串的一个方法
;; 用绿色高亮「申请人」、黄色高亮「被申请人」
(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)))))))))
@HomelandScenery
Copy link

大大,无效果啊。
screenshot - 2016_5_31 23_29_50
已经放到配置里。
screenshot - 2016_5_31 23_30_40

@twlz0ne
Copy link

twlz0ne commented Feb 19, 2017

@HomelandScenery 你要开启 hi-lock-mode 才有效。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment