-
Notifications
You must be signed in to change notification settings - Fork 12
/
evil-terminal-cursor-changer.el
executable file
·308 lines (276 loc) · 10.7 KB
/
evil-terminal-cursor-changer.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
;;; evil-terminal-cursor-changer.el --- Change cursor shape and color by evil state in terminal -*- lexical-binding: t; coding: utf-8; -*-
;;
;; Filename: evil-terminal-cursor-changer.el
;; Description: Change cursor shape and color by evil state in terminal.
;; Author: 7696122
;; Maintainer: 7696122
;; Created: Sat Nov 2 12:17:13 2013 (+0900)
;; Version: 0.0.4
;; Package-Version: 20150819.907
;; Package-Requires: ()
;; Last-Updated: Sat May 14 11:56:23 2022 (+0900)
;; By: 7696122
;; Update #: 393
;; URL: https://github.com/7696122/evil-terminal-cursor-changer
;; Doc URL: https://github.com/7696122/evil-terminal-cursor-changer/blob/master/README.md
;; Keywords: evil, terminal, cursor
;; Compatibility: GNU Emacs: 24.x
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;; [![MELPA](http://melpa.org/packages/evil-terminal-cursor-changer-badge.svg)](http://melpa.org/#/evil-terminal-cursor-changer)
;; ## Introduce ##
;;
;; evil-terminal-cursor-changer is changing cursor shape and color by evil state for evil-mode.
;;
;; When running in terminal, It's especially helpful to recognize evil's state.
;;
;; ## Install ##
;;
;; 1. Config melpa: http://melpa.org/#/getting-started
;;
;; 2. M-x package-install RET evil-terminal-cursor-changer RET
;;
;; 3. Add code to your emacs config file:(for example: ~/.emacs):
;;
;; (unless (display-graphic-p)
;; (require 'evil-terminal-cursor-changer)
;; (evil-terminal-cursor-changer-activate) ; or (etcc-on)
;; )
;;
;; If want change cursor shape type, add below line. This is evil's setting.
;;
;; (setq evil-motion-state-cursor 'box) ; █
;; (setq evil-visual-state-cursor 'box) ; █
;; (setq evil-normal-state-cursor 'box) ; █
;; (setq evil-insert-state-cursor 'bar) ; ⎸
;; (setq evil-emacs-state-cursor 'hbar) ; _
;;
;; Now, works in XTerm, Gnome Terminal(Gnome Desktop), iTerm(Mac OS
;; X), Konsole(KDE Desktop), dumb(etc. mintty), Apple
;; Terminal.app(restrictive supporting). If using Apple Terminal.app,
;; must install SIMBL(http://www.culater.net/software/SIMBL/SIMBL.php)
;; and MouseTerm
;; plus(https://github.com/saitoha/mouseterm-plus/releases) to use
;; evil-terminal-cursor-changer. That makes to support VT's DECSCUSR
;; sequence.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change Log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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 this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(require 'color)
(defgroup evil-terminal-cursor-changer nil
"Cursor changer for evil on terminal."
:group 'cursor
:prefix "etcc-")
(defcustom etcc-use-color nil
"Whether to cursor color."
:type 'boolean
:group 'evil-terminal-cursor-changer)
(defcustom etcc-use-blink t
"Whether to cursor blink."
:type 'boolean
:group 'evil-terminal-cursor-changer)
(defcustom etcc-term-type-override nil
"The type of terminal sequence to send.
Set this if your terminal is not correctly detected."
:type `(choice (const :tag "(Autodetect)" ,nil)
(const :tag "Dumb" dumb)
(const :tag "Xterm" xterm)
(const :tag "iTerm" iterm)
(const :tag "Gnome Terminal" gnome)
(const :tag "Konsole" konsole)
(const :tag "Apple Terminal" apple)
(const :tag "Kitty" kitty))
:group 'evil-terminal-cursor-changer)
(defun etcc--in-dumb? ()
"Running in dumb."
(or (eq etcc-term-type-override 'dumb)
(string= (getenv "TERM") "dumb")))
(defun etcc--in-iterm? ()
"Running in iTerm."
(or (eq etcc-term-type-override 'iterm)
(string= (getenv "TERM_PROGRAM") "iTerm.app")))
(defun etcc--in-xterm? ()
"Runing in xterm."
(or (eq etcc-term-type-override 'xterm)
(getenv "XTERM_VERSION")))
(defun etcc--in-gnome-terminal? ()
"Running in gnome-terminal."
(or (eq etcc-term-type-override 'gnome)
(string= (getenv "COLORTERM") "gnome-terminal")))
(defun etcc--in-konsole? ()
"Running in konsole."
(or (eq etcc-term-type-override 'konsole)
(getenv "KONSOLE_PROFILE_NAME")))
(defun etcc--in-apple-terminal? ()
"Running in Apple Terminal."
(or (eq etcc-term-type-override 'apple)
(string= (getenv "TERM_PROGRAM") "Apple_Terminal")))
(defun etcc--in-kitty? ()
"Running in Kitty."
(or (eq etcc-term-type-override 'kitty)
(getenv "KITTY_PID")))
(defun etcc--get-current-gnome-profile-name ()
"Return Current profile name of Gnome Terminal."
;; https://github.com/helino/current-gnome-terminal-profile/blob/master/current-gnome-terminal-profile.sh
(if (etcc--in-gnome-terminal?)
(let ((cmd "#!/bin/sh
FNAME=$HOME/.current_gnome_profile
gnome-terminal --save-config=$FNAME
ENTRY=`grep ProfileID < $FNAME`
rm $FNAME
TERM_PROFILE=${ENTRY#*=}
echo -n $TERM_PROFILE"))
(shell-command-to-string cmd))
"Default"))
(defun etcc--color-name-to-hex (color)
"Convert color name to hex value."
(apply 'color-rgb-to-hex (color-name-to-rgb color)))
(defun etcc--make-konsole-cursor-shape-seq (shape)
"Make escape sequence for konsole."
(let ((prefix "\e]50;CursorShape=")
(suffix "\x7")
(box "0")
(bar "1")
(hbar "2")
(seq nil))
(unless (member shape '(box bar hbar))
(setq shape 'box))
(cond ((eq shape 'box)
(setq seq (concat prefix box suffix)))
((eq shape 'bar)
(setq seq (concat prefix bar suffix)))
((eq shape 'hbar)
(setq seq (concat prefix hbar suffix))))
seq))
(defun etcc--make-gnome-terminal-cursor-shape-seq (shape)
"Make escape sequence for gnome terminal."
(let* ((profile (etcc--get-current-gnome-profile-name))
(prefix (format "gconftool-2 --type string --set /apps/gnome-terminal/profiles/%s/cursor_shape "
profile))
(box "block")
(bar "ibeam")
(hbar "underline"))
(unless (member shape '(box bar hbar))
(setq shape 'box))
(cond ((eq shape 'box)
(concat prefix box))
((eq shape 'bar)
(concat prefix bar))
((eq shape 'hbar) hbar))))
(defun etcc--make-xterm-cursor-shape-seq (shape)
"Make escape sequence for XTerm."
(let ((prefix "\e[")
(suffix " q")
(box-blink "1")
(box "2")
(hbar-blink "3")
(hbar "4")
(bar-blink "5")
(bar "6"))
(unless (member shape '(box bar hbar))
(setq shape 'box))
(cond ((eq shape 'box)
(setq seq (concat prefix (if (and etcc-use-blink blink-cursor-mode) box-blink box) suffix)))
((eq shape 'bar)
(setq seq (concat prefix (if (and etcc-use-blink blink-cursor-mode) bar-blink bar) suffix)))
((eq shape 'hbar)
(setq seq (concat prefix (if (and etcc-use-blink blink-cursor-mode) hbar-blink hbar) suffix))))
seq))
(defun etcc--make-cursor-shape-seq (shape)
"Make escape sequence for cursor shape."
(cond ((or (etcc--in-xterm?)
(etcc--in-apple-terminal?)
(etcc--in-iterm?)
(etcc--in-kitty?))
(etcc--make-xterm-cursor-shape-seq shape))
((etcc--in-konsole?)
(etcc--make-konsole-cursor-shape-seq shape))
((etcc--in-dumb?)
(etcc--make-xterm-cursor-shape-seq shape))))
(defun etcc--make-cursor-color-seq (color)
"Make escape sequence for cursor color."
(let ((hex-color (etcc--color-name-to-hex color)))
(if hex-color
;; https://www.iterm2.com/documentation-escape-codes.html
(let ((prefix (if (etcc--in-iterm?)
"\e]Pl"
"\e]12;"))
(suffix (if (etcc--in-iterm?)
"\e\\"
"\a")))
(concat prefix
;; https://www.iterm2.com/documentation-escape-codes.html
;; Remove #, rr, gg, bb are 2-digit hex value for iTerm.
(if (and (etcc--in-iterm?)
(string-prefix-p "#" hex-color))
(substring hex-color 1)
hex-color)
suffix)))))
(defun etcc--apply-to-terminal (seq)
"Send to escape sequence to terminal."
(when (and seq
(stringp seq)
(not (display-graphic-p)))
(send-string-to-terminal seq)))
(defun etcc--evil-set-cursor-color (color)
"Set cursor color."
(etcc--apply-to-terminal (etcc--make-cursor-color-seq color)))
(defun etcc--evil-set-cursor ()
"Set cursor color type."
(unless (display-graphic-p)
(if (symbolp cursor-type)
(etcc--apply-to-terminal (etcc--make-cursor-shape-seq cursor-type))
(if (listp cursor-type)
(etcc--apply-to-terminal (etcc--make-cursor-shape-seq (car cursor-type)))))))
;; (defadvice evil-set-cursor-color (after etcc--evil-set-cursor (arg) activate)
;; (unless (display-graphic-p)
;; (etcc--evil-set-cursor-color arg)))
;; (defadvice evil-set-cursor (after etcc--evil-set-cursor (arg) activate)
;; (unless (display-graphic-p)
;; (etcc--evil-set-cursor)))
;;;###autoload
(defun evil-terminal-cursor-changer-activate ()
"Enable evil terminal cursor changer."
(interactive)
(if etcc-use-blink (add-hook 'blink-cursor-mode-hook #'etcc--evil-set-cursor))
(add-hook 'pre-command-hook 'etcc--evil-set-cursor)
(add-hook 'post-command-hook 'etcc--evil-set-cursor))
;;;###autoload
(defalias 'etcc-on 'evil-terminal-cursor-changer-activate)
;;;###autoload
(defun evil-terminal-cursor-changer-deactivate ()
"Disable evil terminal cursor changer."
(interactive)
(if etcc-use-blink (remove-hook 'blink-cursor-mode-hook 'etcc--evil-set-cursor))
(remove-hook 'pre-command-hook 'etcc--evil-set-cursor)
(remove-hook 'post-command-hook 'etcc--evil-set-cursor))
;;;###autoload
(defalias 'etcc-off 'evil-terminal-cursor-changer-deactivate)
(provide 'evil-terminal-cursor-changer)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; evil-terminal-cursor-changer.el ends here