Using tmux, emacs and vim, I create some bindings to allow you to send mouse clicks to the terminal programmatically.

I had attempted to do this with emacs lisp but it was overly complicated and had I succeeded only emacs would be able to understand the mouse codes.

Click at the cursor location

Make the tmux bindings

1
2
bind -n C-M-x run -b "tm mousedown"
bind -n C-M-z run -b "tm mouseup"

Make the tm script

 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
mousedown|mouseup) {
    while [ $# -gt 0 ]; do opt="$1"; case "$opt" in
        -t) {
            TMUX_PANE="$2"
            shift
            shift
        }
        ;;

        *) break;
    esac; done

    [ -n "$1" ] && x="$1" && shift
    [ -n "$1" ] && y="$1" && shift

    : ${TMUX_PANE:="$CALLER_PANE"}

    # tm cursor
    if test -z "$x"; then
        x="$(tmux display -t "$TMUX_PANE" -p "#{cursor_x}")"
    fi
    if test -z "$y"; then
        y="$(tmux display -t "$TMUX_PANE" -p "#{cursor_y}")"
    fi

    # ns "$f $TMUX_PANE $x $y" &>/dev/null

    if [ -n "$TMUX_PANE" ] && [ -n "$x" ] && [ -n "$y" ]; then
        case "$f" in
            mousedown) {
                tmux send-keys -t "$TMUX_PANE" -l "$(xterm-click -d $x $y)"
            }
            ;;

            mouseup) {
                tmux send-keys -t "$TMUX_PANE" -l "$(xterm-click -u $x $y)"
            }
            ;;

            click) {
                tmux send-keys -t "$TMUX_PANE" -l "$(xterm-click -d $x $y)"
                sleep 0.2
                tmux send-keys -t "$TMUX_PANE" -l "$(xterm-click -u $x $y)"
            }
            ;;

            *)
        esac
    fi
    exit 0
}

Make the xterm-click script

This generates the required ansi codes.

 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
#!/bin/bash
export TTY

UP=y
DOWN=y
EXTENDED_MODE=n
while [ $# -gt 0 ]; do opt="$1"; case "$opt" in
    -u) {
        DOWN=n
        UP=y
        shift
    }
    ;;

    -d) {
        DOWN=y
        UP=n
        shift
    }
    ;;

    -x) {
        EXTENDED_MODE=y
        shift
    }
    ;;

    *) break;
esac; done

x="$1"; : ${x:="1"}
y="$2"; : ${y:="1"}

if [ -n "$3" ]; then
    TTY="$3"
fi

: ${TTY:="$(tty)"}


# Make it faster
chr () {
      printf "\\$(printf '%03o' "$1")"
}

# Not sure how this works. How does tmux do it?

if test "$EXTENDED_MODE" = "y"; then
    mousedown=0
    mouseup=1

    mousedown_code="$(echo -n -e "\e[<$mousedown"; printf -- "%s" ";$y;${x}m")"
    mouseup_code="$(echo -n -e "\e[<$mouseup"; printf -- "%s" ";$y;${x}m")"

else
    # x="$(perl -e "print chr(33 + $x)")"
    # y="$(perl -e "print chr(33 + $y)")"
    x="$(chr $(( 33 + $x )) )"
    y="$(chr $(( 33 + $y )) )"
    mousedown="$(chr 32)" # ' '
    mouseup="$(chr 35)" # '#'

    mousedown_code="$(echo -n -e "\e[M$mousedown"; printf -- "%s" "$x$y")"
    mouseup_code="$(echo -n -e "\e[M$mouseup"; printf -- "%s" "$x$y")"
fi

if test "$UP" = "n"; then
    code="$mousedown_code"
elif test "$DOWN" = "n"; then
    code="$mouseup_code"
else
    code="$mousedown_code$mouseup_code"
fi

printf -- "%s" "$code"

Demonstration

asciinema recording

Click with easymotion

For the uninitiated, easymotion is a decision tree location selector for vim, similar to avy.

We can use it to select the coordinates of the mouse click.

The click sends a down followed by an up after 0.2 seconds.

Patch easymotion to save the cursor location

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
diff --git a/autoload/EasyMotion.vim b/autoload/EasyMotion.vim
index 7ccdcf8..799123a 100644
--- a/autoload/EasyMotion.vim
+++ b/autoload/EasyMotion.vim
@@ -1592,6 +1592,14 @@ function! s:EasyMotion(regexp, direction, visualmode, is_inclusive, ...) " {{{
             call EasyMotion#attach_active_autocmd() "}}}
         endif
     endtry
+
+    " coords will not exist if there is nothing in vim's buffer
+    if exists("coords")
+        call system("echo '".coords[0].' '.coords[1]."' > /tmp/em-click.txt")
+    else
+        call system("echo > /tmp/em-click.txt")
+    endif
+    " call Tv(coords[0].' '.coords[1])
 endfunction " }}}
 "}}}
 " }}}

Create the tm script

capture is for capturing the visible contents of the terminal.

cap is an alias to capture with sensible defaults.

em-click makes use of cap to provide a buffer to vim and easymotion for selecting the place you want to click.

  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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
capture) {
    mkdir -p ~/programs/tmux/capture

    x="$(tmux display -p "#{cursor_x}")"
    y="$(tmux display -p "#{cursor_y}")"

    ANSI=n
    ANSI=y
    DO_SEND_KEYS=n
    SCROLLBACK=-32768
    CAPTURE_OPTIONS=" -J "
    EDITOR="v"
    STDOUT= # no editor. no value relies on is_tty
    NOABORT=n
    while [ $# -gt 0 ]; do case $1 in
        -a|-ansi) {
            # printf -- "%s\n" "ansi"

            ANSI=y

            shift
        }
        ;;

        -c|-clean) {
            # printf -- "%s\n" "clean"

            ANSI=n

            shift
        }
        ;;

        -tty) {
            STDOUT=n

            shift
        }
        ;;

        -|-stdout) {
            STDOUT=y

            shift
        }
        ;;

        -f|-filter) {
            ns disabled $1 because ambiguous

            exit 0

            shift
        }
        ;;

        -p) {
            PREVIEW=y

            shift
        }
        ;;

        -noabort) {
            NOABORT=y

            shift
        }
        ;;

        -t) {
            CALLER_TARGET="$2"
            CALLER_PANE="$2"
            shift
            shift
        }
        ;;

        -nosendkeys) {
            DO_SEND_KEYS=n

            shift
        }
        ;;

        -sendkeys) {
            DO_SEND_KEYS=y

            shift
        }
        ;;

        -s|-session) {
            # Take a capture of the entire session, not just the window

            shift
        }
        ;;

        -ts|-top-session) {
            # Take a capture of the topmost session

            shift
        }
        ;;

        --pc|--preview-cmd) {
            PREVIEW=y
            PREVIEW_CMD=y

            shift
        }
        ;;

        -e|-emacs) {
            EDITOR="e c"

            shift
        }
        ;;

        -editor) {
            # EDITOR="$2 -e \"(linum-mode -1)\""
            EDITOR="$2"

            shift
            shift
        }
        ;;

        -nv) {
            EDITOR="nv -c 'nmap q :q!<CR>'"
            # EDITOR="nv"

            shift
        }
        ;;

        -v|-vim) {
            EDITOR="v"

            shift
        }
        ;;

        -session) {

            shift
        }
        ;;

        -wrap) { # The tmux wrapper session
            WRAP=y
            SCROLLBACK=0

            shift
        }
        ;;

        -nohist|-nohistory) {
            SCROLLBACK="0"

            shift
        }
        ;;

        -history) {
            SCROLLBACK=-32768

            shift
        }
        ;;

        -b|-bottom) { # start emacs/vim at bottom of buffer

            # bind M-f run -b 'tmux-capture.sh -f' # filters
            shift
        }
        ;;

        *) break;
    esac; done

    if test "$ANSI" = "y"; then
        CAPTURE_OPTIONS+=" -e "
        EDITOR=ansivim
    fi

    server="$(tm-get-server)"
    echo "$server" 1>&2
    unset TMUX

    # Create temporary file
    tf_tmcapture="$(ux tf tmcapture txt || echo /dev/null)"
    bn="$(basename "$tf_tmcapture")"
    np="$HOME/programs/tmux/capture/$bn"
    mv "$tf_tmcapture" "$np"

    echo "$WRAP" 1>&2
    if test "$WRAP" = "y"; then
        swidth="$(tmux display -t "$CALLER_TARGET" -p "#{session_width}")"
        sheight="$(tmux display -t "$CALLER_TARGET" -p "#{session_height}")"
        sheight="$(( sheight + 2 ))"

        # Don't use tm attach, to make this faster.
        tmux -L $server new-session -d -x "$swidth" -y "$sheight" -A -s wrap \; respawnp -k -t "wrap:1" "TMUX= tmux -L $server attach -t \"$CALLER_TARGET\""

        c=0

        grepstring="$CALLER_TARGET"
        if test "$grepstring" = "localhost"; then
            grepstring="$(hostname | s cap)"
        fi

        while ! grep -q "\[ $grepstring \]" "$np"; do
            ((++c))
            if test "$NOABORT" = "n" && [ "$c" -eq 10 ]; then
                notify-send "Leaving capture \"[ $grepstring ]\" because it took too long."
                break
            fi

            tmux -L $server capture-pane -t "wrap:1.0" \; save-buffer "$np"
            # exit 0
        done

        tmux -L $server kill-pane -t "wrap:1.0"
    else
        eval "tmux -L $server capture-pane -t "$CALLER_PANE" $CAPTURE_OPTIONS -S $SCROLLBACK"

        tmux -L $server save-buffer "$np"
    fi

    cat "$np" | {
        sed -e 's/โ–„/ /g' \
            -e 's/[โ”Œโ”โ”˜โ””โ”‚โ”œโ”€โ”คโ”ฌโ”ดโ”ผ]/ /g' \
            -e 's/\s\+$//g' ` # remove trailing whitespace `

        # piping into cat here breaks it. It creates a race
        # condition with the outer cat, or something. Do not do
        # this.
        # | cat
    } | sponge "$np"

    if test "$STDOUT" = ""; then
        if is_tty; then
            STDOUT=n
        fi
    fi

    if test "$STDOUT" = "y"; then
        cat "$np"
    else
        eval `resize` &>/dev/null
        full_lines="$(wc -l "$np" | awk '{print $1}')"
        final_x=$x
        final_y="$(( y + 1 ))"
        pane_id="$(tmux -L $server neww -F "#{pane_id}" -P -n capture "stty stop undef; stty start undef; GOTO_COLUMN=$final_x GOTO_LINE=$final_y $EDITOR \"$np\"")"

        if test $DO_SEND_KEYS = "f" && test "$EDITOR" = "v"; then
            tmux -L $server send-keys -t "$pane_id" "C-k"
        else
            :
        fi
    fi
}

cap) {
    tm -te -d capture -clean -wrap -noabort -
}
;;

em-click) { # easymotion click
    # Requires this patch: $HOME/notes2018/ws/tm/patches/em-click.diff

    cap_file="$(tm cap | tf txt; sleep 0.5)"
    if [ -s "$cap_file" ]; then
        tm -te nw -n em-click "v -c 'set foldcolumn=0 ls=0 | call EasyMotion#WB(0,2) | q!' \"$cap_file\""

        x="$(cat /tmp/em-click.txt | cut -d ' ' -f 1)"
        y="$(cat /tmp/em-click.txt | cut -d ' ' -f 2)"

        if [ -n "$x" ] && [ -n "$y" ]; then
            # notify-send "$loc"
            x=$((x - 1))
            y=$((y - 1))
            # cmd tm click-wrap $x $y | ns &>/dev/null
            tm click-wrap $x $y
        fi
    fi

    # Need to obtain coordinates here. Thene run: "tm click -t %570 0 0"
    # $HOME$VIMCONFIG/vim/pack/myplugins/start/vim-easymotion-new/autoload/vital/easymotion.vim

    exit 0
}
;;

click-wrap) { # start wrapper and then click on it
    swidth="$(tmux display -t "$CALLER_TARGET" -p "#{session_width}")"
    sheight="$(tmux display -t "$CALLER_TARGET" -p "#{session_height}")"
    sheight="$(( sheight + 2 ))"

    server="$(tm-get-server)"

    # Don't use tm attach, to make this faster.
    tmux new-session -d -x "$swidth" -y "$sheight" -A -s wrap \; respawnp -k -t "wrap:1" "TMUX= tmux -L $server attach -t \"$CALLER_TARGET\""

    [ -n "$1" ] && x="$1" && shift
    [ -n "$1" ] && y="$1" && shift

    # Wait for C-M-f to close
    while tmux has-session -t ":em-click"; do
        sleep 0.1
    done

    # sleep 1
    tm click -t "wrap:1" "$x" "$y"

    # sleep 0.2

    tmux kill-pane -t "wrap:1.0"
}
;;

click) {
    while [ $# -gt 0 ]; do opt="$1"; case "$opt" in
         -t) {
             TMUX_PANE="$2"
             shift
             shift
         }
         ;;

         *) break;
     esac; done

    [ -n "$1" ] && x="$1" && shift
    [ -n "$1" ] && y="$1" && shift

    : ${TMUX_PANE:="$CALLER_PANE"}

    # tm cursor
    if test -z "$x"; then
        x="$(tmux display -t "$TMUX_PANE" -p "#{cursor_x}")"
    fi
    if test -z "$y"; then
        y="$(tmux display -t "$TMUX_PANE" -p "#{cursor_y}")"
    fi

    ns "$TMUX_PANE $x $y" &>/dev/null

    if [ -n "$TMUX_PANE" ] && [ -n "$x" ] && [ -n "$y" ]; then
        # TODO Fix this -- it's backwards.
        tmux send-keys -t "$TMUX_PANE" -l "$(xterm-click -d $y $x)"
        sleep 0.2
        tmux send-keys -t "$TMUX_PANE" -l "$(xterm-click -u $y $x)"
    fi
    exit 0
}
;;

Demonstration

asciinema recording