forked from tinmarino/mouse_xterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouse.sh
282 lines (230 loc) · 8.6 KB
/
mouse.sh
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
#!/usr/bin/env bash
: <<'END_DOC'
Mouse click to move cursor
DESCRIPTION :
Your readline cursor should move on mouse click
USAGE :
source mouse.sh && mouse_track
`ctrl+l` to renable track (automatically disable when you want to scrool)
DEPENDS :
xterm, readline
LICENSE :
Copyright 2019-2021 Tinmarino <[email protected]>
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.
END_DOC
# Keystrokes <string>: usually output by the terminal
g_key=''
# Mouse track status <int>: 1 tracking; 0 Not tracking
g_mouse_track_status=0
# Binding <dict>: seq -> bash function
declare -A g_binding=(
[<64;]=mouse_track_cb_scroll_up
[<65;]=mouse_track_cb_scroll_down
## Click (0) Begining of line + X click cb
[<0;]=mouse_track_cb_click
[<1;]=mouse_track_cb_click2
[<2;]=mouse_track_cb_click3
[<32;]=mouse_track_cb_drag1
# Dichotomically found (xterm, 67, 68 maybe too)
[32;]=mouse_track_cb_click
[33;]=mouse_track_cb_click2
[34;]=mouse_track_cb_click3
)
mouse_track_cb_scroll_up() { mouse_track_tmux_proxy 'tmux copy-mode -e \; send-keys -X -N 5 scroll-up'; }
mouse_track_cb_scroll_down() { mouse_track_tmux_proxy ''; }
mouse_track_cb_click2() { mouse_track_tmux_proxy 'tmux paste-buffer'; }
mouse_track_cb_click3() { mouse_track_tmux_proxy "
tmux display-menu -T '#[align=centre]#{pane_index} (#{pane_id})' \
'Horizontal Split' 'h' 'split-window -h' \
'Vertical Split' 'v' 'split-window -v' \
'Swap Up' 'u' 'swap-pane -U' \
'Swap Down' 'd' 'swap-pane -D' \
'#{?pane_marked_set,,-}Swap Marked' 's' 'swap-pane' \
'Kill' 'X' 'kill-pane' \
'Respawn' 'R' 'respawn-pane -k' \
'#{?pane_marked,Unmark,Mark}' 'm' 'select-pane -m'\
'#{?window_zoomed_flag,Unzoom,Zoom}' 'z' 'resize-pane -Z'
"; }
mouse_track_cb_drag1() { mouse_track_tmux_proxy 'tmux copy-mode -e \; send-keys -X begin-selection'; }
# Cursor position <string>: 50;1 (x;y) if click on line 1, column 50: starting at 1;1
g_cursor_pos='1;1'
# Tmux command to launch
g_tmux_cmd=''
# Escape sequences
s_echo_enable='\033[?1000;1002;1006;1015h'
s_echo_disable='\033[?1000;1002;1006;1015l'
s_echo_get_cursor_pos='\033[6n'
mouse_track_log() {
# Log for debug
:
printf "%b\n" "$*" >> /tmp/xterm_monitor
}
mouse_track_echo_enable() {
# Enable (high)
printf "%b" "$s_echo_enable"
g_mouse_track_status=1
}
mouse_track_echo_disable() {
# Disable (low)
printf "%b" "$s_echo_disable"
g_mouse_track_status=0
}
mouse_track_read_keys_remaining() {
# In: Stdin (until 'm')
# Out: $g_key
mouse_track_log "--------------- Reading keys"
g_key=""
# TODO ugly 0.001 sec timeout
while read -rt 0.001 -n 1 c; do
mouse_track_log "reading $c"
g_key="$g_key$c"
[[ $c == 'M' || $c == 'm' || $c == 'R' || $c == '' ]] && break
done
mouse_track_log "g_key = $g_key"
}
mouse_track_read_cursor_pos() {
# Read $cursor_pos <- xterm <- readline
# Out: 50;1 (x;y) if click on line 1, column 50: starting at 1;1
# Clean stdin
mouse_track_read_keys_remaining
# Ask cursor pos
printf "%b" "$s_echo_get_cursor_pos"
# Read it
read -srdR g_cursor_pos
g_cursor_pos=${g_cursor_pos#*[}
mouse_track_log "cursor_pos returns: $g_cursor_pos"
}
mouse_track_trap_disable_mouse() {
# Trap for stopping track at command spawn (like vim)
# Callback : traped to debug : Disable XTERM escape
# log
mouse_track_log "trap ($g_mouse_track_status) for : $BASH_COMMAND"
# Clauses: leave if ...
# -- mouse track disabled yet
[[ $g_mouse_track_status == 0 ]] \
|| [[ -n "$COMP_LINE" ]] \
|| [[ "$BASH_COMMAND" == "$PROMPT_COMMAND" ]] \
|| [[ "$BASH_COMMAND" =~ ^mouse_track* ]] \
&& { mouse_track_log "trap disregarded (clause)"; return; }
# -- bash is completing
# -- don't cause a preexec for $PROMPT_COMMAND
# -- bind from myself for example at scroll
# Disable mouse as callback
mouse_track_log "trap : Stoping mouse tracking"
mouse_track_stop
}
mouse_track_cb_click() {
# Callback for mouse button 0 click/release
local x0 y0 x1 y1 col readline_point
# Read rest
mouse_track_read_keys_remaining
mouse_track_log "---------------- Mouse click with $g_key"
# Log readline pre value
mouse_track_log "$(echo "Readline pre: $READLINE_POINT, $READLINE_LINE, $READLINE_MARK" | xxd)"
# Only work for M
local mode=${g_key: -1}
[[ "$mode" == m ]] && {
mouse_track_log "Release ignored"
return 0
}
# Get click (x1, y1)
local xy=${g_key:0:-1}
local x1=${xy%%;*}
local y1=${xy##*;}
# Get Cursor position (x0, y0)
mouse_track_read_cursor_pos
(( x0=${g_cursor_pos##*;} ))
(( y0=${g_cursor_pos%%;*} ))
mouse_track_log "x0 = $x0 && y0 = $y0 && g_cursor_pos = $g_cursor_pos"
mouse_track_log "x1 = $x1 && y1 = $y1"
# Calculate line position
(( col = y1 - y0 ))
(( col < 0 )) && (( col = 0 ))
readarray -t a_line <<< "$READLINE_LINE"
for i in "${a_line[@]}"
do
mouse_track_log "Array line: $i"
done
(( readline_point = x1 - x0 - 2 + col * COLUMNS ))
mouse_track_log "col = $col && readline_point = $readline_point"
# TODO if too low, put on last line
# Move cursor
export READLINE_POINT=$readline_point
# Log readline post value
mouse_track_log "Readline post: $READLINE_POINT, $READLINE_LINE, $READLINE_MARK"
}
mouse_track_cb_void() {
# Callback : clean xterm and disable mouse escape
mouse_track_read_keys_remaining
mouse_track_log "Cb: Void with: $g_key"
#mouse_track_stop
}
mouse_track_tmux_get_command(){
g_tmux_cmd="$(tmux list-keys -T root "$1" | sed "s/^[^W]*$1 /tmux /")"
#g_tmux_cmd="$(echo 'if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= \"#{pane_in_mode}\" \"send-keys -M\" \"copy-mode -et=\""')"
}
mouse_track_tmux_proxy() {
local s_tmux_cmd="$1"
mouse_track_read_keys_remaining
mouse_track_log "Cb: tmux proxy cmd: $s_tmux_cmd, keys remaining: $g_key"
# Tmux case
if command -v tmux &> /dev/null \
&& [[ -n "$TMUX" ]] \
; then
# Launch mux scroll:
# In job so that realine binding returns before => I can see the current line
# In subshell to avoid job control stderr
mouse_track_log "Cb: tmux proxy2 start job $s_tmux_cmd"
( {
sleep 0.01
#mouse_track_tmux_get_command "$s_tmux_cmd"
# shellcheck disable=SC2046,SC2086 # Quote this to prevent wor
eval "$s_tmux_cmd"
#tmux if-shell -F -t = "#{||:#{mouse_any_flag},#{pane_in_mode}}" "select-pane -t=; send-keys -M" "display-menu -t= -xM -yM -T \"#[align=centre]#{pane_index} (#{pane_id})\" '#{?mouse_word,Search For #[underscore]#{=/9/...:mouse_word},}' 'C-r' {copy-mode -t=; send -Xt= search-backward \"#{q:mouse_word}\"} '#{?mouse_word,Type #[underscore]#{=/9/...:mouse_word},}' 'C-y' {send-keys -l -- \"#{q:mouse_word}\"} '#{?mouse_word,Copy #[underscore]#{=/9/...:mouse_word},}' 'c' {set-buffer -- \"#{q:mouse_word}\"} '#{?mouse_line,Copy Line,}' 'l' {set-buffer -- \"#{q:mouse_line}\"} '' 'Horizontal Split' 'h' {split-window -h} 'Vertical Split' 'v' {split-window -v} '' 'Swap Up' 'u' {swap-pane -U} 'Swap Down' 'd' {swap-pane -D} '#{?pane_marked_set,,-}Swap Marked' 's' {swap-pane} '' 'Kill' 'X' {kill-pane} 'Respawn' 'R' {respawn-pane -k} '#{?pane_marked,Unmark,Mark}' 'm' {select-pane -m} '#{?window_zoomed_flag,Unzoom,Zoom}' 'z' {resize-pane -Z}"
mouse_track_log "Cb: Button 2, tmux async finish $s_tmux_cmd -> $g_tmux_cmd"
mouse_track_log "tmux $g_tmux_cmd"
} & )
return 0
fi
mouse_track_cb_void
}
mouse_track_cb_scroll_down() {
mouse_track_log 'Cb: Scroll Down'
mouse_track_read_keys_remaining
#printf "%b" "$s_echo_enable$s_bindx_4$g_key"
}
mouse_track_set_bindings() {
mouse_track_log 'Set bindings'
for s_keyseq in "${!g_binding[@]}"; do
local s_fct=${g_binding[$s_keyseq]}
bind -x "\"\033[$s_keyseq\":$s_fct"
done
}
mouse_track_unset_bindings() {
# Unset mouse event callback binding
mouse_track_log 'Unset bindings'
for s_keyseq in "${!g_binding[@]}"; do
local s_fct=${g_binding[s_keyseq]}
bind -r "$s_keyseq"
done
}
mouse_track_start() {
# Init : Enable mouse tracking
mouse_track_set_bindings
# Disable mouse tracking before each command
trap 'mouse_track_trap_disable_mouse' DEBUG
# Enable mouse tracking after command return
export PROMPT_COMMAND+='mouse_track_echo_enable;'
# Enable now anyway
mouse_track_echo_enable
}
mouse_track_stop() {
# Finish : Disable mouse tracking
# Disable mouse tracking
mouse_track_echo_disable
# Unset binding
mouse_track_unset_bindings
}