-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathidle-highlight-mode.el
executable file
·114 lines (92 loc) · 3.71 KB
/
idle-highlight-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
;;; idle-highlight-mode.el --- highlight the word the point is on
;; Copyright (C) 2008-2011 Phil Hagelberg, Cornelius Mika
;; Author: Phil Hagelberg, Cornelius Mika
;; URL: http://www.emacswiki.org/cgi-bin/wiki/IdleHighlight
;; Version: 1.1.3
;; Created: 2008-05-13
;; Keywords: convenience
;; EmacsWiki: IdleHighlight
;; This file is NOT part of GNU Emacs.
;;; License:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Based on some snippets by fledermaus from the #emacs channel.
;; M-x idle-highlight-mode sets an idle timer that highlights all
;; occurences in the buffer of the word under the point.
;; Enabling it in a hook is recommended. But you don't want it enabled
;; for all buffers, just programming ones.
;;
;; Example:
;;
;; (defun my-coding-hook ()
;; (make-local-variable 'column-number-mode)
;; (column-number-mode t)
;; (if window-system (hl-line-mode t))
;; (idle-highlight-mode t))
;;
;; (add-hook 'emacs-lisp-mode-hook 'my-coding-hook)
;; (add-hook 'ruby-mode-hook 'my-coding-hook)
;; (add-hook 'js2-mode-hook 'my-coding-hook)
;;; Code:
(require 'thingatpt)
(defgroup idle-highlight nil
"Highlight other occurrences of the word at point."
:group 'faces)
(defface idle-highlight
'((t (:inherit region)))
"Face used to highlight other occurrences of the word at point."
:group 'idle-highlight)
(defcustom idle-highlight-exceptions '("end")
"List of words to be excepted from highlighting."
:group 'idle-highlight
:type '(repeat string))
(defcustom idle-highlight-idle-time 0.5
"Time after which to highlight the word at point."
:group 'idle-highlight
:type 'float)
(defvar idle-highlight-regexp nil
"Buffer-local regexp to be idle-highlighted.")
(defvar idle-highlight-global-timer nil
"Timer to trigger highlighting.")
(defun idle-highlight-word-at-point ()
"Highlight the word under the point."
(if idle-highlight-mode
(let* ((target-symbol (symbol-at-point))
(target (symbol-name target-symbol)))
(idle-highlight-unhighlight)
(when (and target-symbol
(not (in-string-p))
(looking-at-p "\\s_\\|\\sw") ;; Symbol characters
(not (member target idle-highlight-exceptions)))
(setq idle-highlight-regexp (concat "\\<" (regexp-quote target) "\\>"))
(highlight-regexp idle-highlight-regexp 'idle-highlight)))))
(defsubst idle-highlight-unhighlight ()
(when idle-highlight-regexp
(unhighlight-regexp idle-highlight-regexp)
(setq idle-highlight-regexp nil)))
;;;###autoload
(define-minor-mode idle-highlight-mode
"Idle-Highlight Minor Mode"
:group 'idle-highlight
(if idle-highlight-mode
(progn (unless idle-highlight-global-timer
(setq idle-highlight-global-timer
(run-with-idle-timer idle-highlight-idle-time
:repeat 'idle-highlight-word-at-point)))
(set (make-local-variable 'idle-highlight-regexp) nil))
(idle-highlight-unhighlight)))
(provide 'idle-highlight-mode)
;;; idle-highlight-mode.el ends here