-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatch-cursor.el
162 lines (129 loc) · 5.03 KB
/
watch-cursor.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
;;; watch-cursor.el --- Show all cursors from other windows -*- lexical-binding: t; -*-
;; Copyright (C) 2020-2025 Shen, Jen-Chieh
;; Created date 2020-10-27 13:25:37
;; Author: Shen, Jen-Chieh <[email protected]>
;; URL: https://github.com/jcs-elpa/watch-cursor
;; Version: 0.1.1
;; Package-Requires: ((emacs "24.3"))
;; Keywords: convenience cursor
;; This file is NOT part of GNU Emacs.
;; 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 of the License, 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 this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; This minor mode display all cursors when there are 2 or more windows
;; displaying the same buffer at a time.
;;
;; To enable this minor mode, please do the following execution
;;
;; `(watch-cursor-mode 1)`
;;
;; You can customize the fake cursors by customizing the face `watch-cursor-face'.
;;
;;; Code:
(defgroup watch-cursor nil
"Show all cursors from other windows."
:prefix "watch-cursor-"
:group 'tool
:link '(url-link :tag "Repository" "https://github.com/jcs-elpa/watch-cursor"))
(defface watch-cursor-face
'((t :background "#40FF40"))
"Face for fake cursor."
:group 'watch-cursor)
(defcustom watch-cursor-delay 0.2
"Seconds of delay before displaying fake cursors."
:type 'float
:group 'watch-cursor)
(defvar-local watch-cursor--buffer nil
"Buffer we are currently watching.")
(defvar-local watch-cursor--cursors '()
"List of cursor data.")
(defvar-local watch-cursor--overlays '()
"List of overlay represent as fake cursors.")
(defvar-local watch-cursor--timer nil
"Timer to display fake cursors.")
;;
;; (@* "Utility" )
;;
(defun watch-cursor--kill-timer (timer)
"Kill TIMER the safe way."
(when (timerp timer) (cancel-timer timer)))
(defmacro watch-cursor--walk-windows (&rest body)
"Macro for function `walk-windows', execute BODY for each window."
(declare (indent 0) (debug t))
`(walk-windows (lambda (win) (select-window win) (progn ,@body)) nil t))
(defun watch-cursor--watching-p ()
"Return non-nil if current buffer is the buffer we are watching."
(eq watch-cursor--buffer (current-buffer)))
(defun watch-cursor--delete-overlays ()
"Delete all overlays in list."
(dolist (ov watch-cursor--overlays) (delete-overlay ov))
(setq watch-cursor--overlays nil))
(defun watch-cursor--make-overlay (pt win owner-win)
"Create a overlay at PT inside WIN.
Argument OWNER-WIN is for echoing tip."
(let ((ol (make-overlay pt (1+ pt))))
(overlay-put ol 'face 'watch-cursor-face)
(overlay-put ol 'priority 0)
(overlay-put ol 'window win)
(overlay-put ol 'help-echo (format "Cursor from %s" owner-win))
(push ol watch-cursor--overlays) ; NOTE: Eventually get managed bt list.
ol))
(defun watch-cursor--make-overlays ()
"Make all fake cursors."
(when (< 1 (length watch-cursor--cursors))
(watch-cursor--walk-windows
(dolist (data watch-cursor--cursors)
(let ((win (car data)) (pt (cdr data)))
(unless (equal win (selected-window))
(watch-cursor--make-overlay pt (selected-window) win)))))))
;;
;; (@* "Core" )
;;
(defun watch-cursor--get-cursors-data ()
"Update cursor data once."
(setq watch-cursor--cursors '())
(watch-cursor--walk-windows
(when (watch-cursor--watching-p)
(push (cons (selected-window) (point)) watch-cursor--cursors))))
(defun watch-cursor--display ()
"Display all fake cursors."
(watch-cursor--get-cursors-data)
(watch-cursor--make-overlays))
(defun watch-cursor--pre-command ()
"Pre-command hook for `watch-cursor'."
(watch-cursor--delete-overlays))
(defun watch-cursor--post-command ()
"Post-command hook for `watch-cursor'."
(watch-cursor--kill-timer watch-cursor--timer)
(setq watch-cursor--timer (run-with-timer watch-cursor-delay nil
#'watch-cursor--display)))
;;
;; (@* "Entry" )
;;
(defun watch-cursor--enable ()
"Enable `watch-cursor' in current buffer."
(setq watch-cursor--buffer (current-buffer))
(add-hook 'pre-command-hook 'watch-cursor--pre-command nil t)
(add-hook 'post-command-hook 'watch-cursor--post-command nil t))
(defun watch-cursor--disable ()
"Disable `watch-cursor' in current buffer."
(watch-cursor--delete-overlays)
(remove-hook 'pre-command-hook 'watch-cursor--pre-command t)
(remove-hook 'post-command-hook 'watch-cursor--post-command t))
;;;###autoload
(define-minor-mode watch-cursor-mode
"Minor mode 'watch-cursor-mode'."
:lighter " WC"
:group watch-cursor
(if watch-cursor-mode (watch-cursor--enable) (watch-cursor--disable)))
(provide 'watch-cursor)
;;; watch-cursor.el ends here