-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_tmux_conf.sh
executable file
·721 lines (649 loc) · 16.9 KB
/
generate_tmux_conf.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
#!/usr/bin/env bash
#
# Copyright (c) 2024-2025: [email protected]
# License: MIT
#
# Part of https://github.com/jaclu/tmux-keybtest
#
# This will generate a tmux.conf adjusted to the version of tmux being used
#
#---------------------------------------------------------------
#
# Write to $tmux_conf
#
#---------------------------------------------------------------
writeln() {
printf "%s\n" "$1" >>"$tmux_conf"
}
bind_char() {
local output="bind -n "
[[ -z "$1" ]] && {
echo "ERROR: call to bind_char() with no param"
exit 1
}
if [[ -n "$skip_message" ]]; then
output="# ${mod}$1 - ## $skip_message"
else
case "$2" in
s | S) output+="'${mod}$1' display-message '${mod}$1'" ;;
d | D) output+="\"${mod}$1\" display-message \"${mod}$1\"" ;;
*) output+="${mod}$1 display-message \"${mod}$1\"" ;;
esac
fi
writeln "$output"
}
# # Not used ATM
# bind_mouse_script() {
# local output="bind -n "
# [[ -z "$1" ]] && {
# echo "ERROR: call to bind_run() with no param"
# exit 1
# }
# output+="'${mod}$1' run-shell -b '$f_mouse_event ${mod}$1'"
# writeln "$output"
# }
header_1() {
writeln
writeln
writeln "#==========================================================="
writeln "#"
writeln "# Segment covering modifier: $1"
writeln "#"
writeln "#==========================================================="
}
header_2() {
writeln
writeln "#"
writeln "# $1"
writeln "#"
}
header_3() {
writeln
writeln "# -- $1"
}
#---------------------------------------------------------------
#
# Handle stack of override messages
#
#---------------------------------------------------------------
push_skip_message() {
local msg="$1"
# writeln "# ><> push_skip_message($msg)"
skip_message_stack+=("$msg") # Push onto stack
skip_message="$msg" # Set skip_message
}
pop_skip_message() {
local len=${#skip_message_stack[@]}
# [[ "$len" -gt 0 ]] && {
# writeln "# ><> pop_skip_message() pre-size: $len msgs: ${skip_message_stack[*]}"
# }
if [[ "$len" -gt 0 ]]; then
unset "skip_message_stack[$((len - 1))]" # Remove last element
skip_message_stack=("${skip_message_stack[@]}") # Rebuild array to avoid gaps
fi
# Reset skip_message only once using array length check
if [[ "${#skip_message_stack[@]}" -gt 0 ]]; then
skip_message="${skip_message_stack[$((${#skip_message_stack[@]} - 1))]}"
# writeln "# ><> pop_skip_message() post skip_message: $skip_message"
else
skip_message="" # If stack is empty, reset skip_message
fi
}
clear_skip_mesages_stack() {
# local len=${#skip_message_stack[@]}
# local msg
# [[ "$len" -gt 0 ]] && {
# msg="# ><> clear_skip_mesages_stack()"
# msg+=" pre-size: $len msgs: ${skip_message_stack[*]}"
# writeln "$msg"
# }
skip_message_stack=() # Clear the stack
skip_message="" # Reset skip_message to empty
}
#---------------------------------------------------------------
#
# Define core tmux environment
#
#---------------------------------------------------------------
base_config() {
if tmux_vers_ok 3.0; then
unlimited=0
else
unlimited=999
fi
writeln "#==========================================================="
writeln "#"
writeln "# Tmux conf for teting keyboard implementation, and what"
writeln "# non standard sequences it can generate. - Arrows with modifiers etc"
# shellcheck disable=SC2154
writeln "# Created for tmux version: $tpt_current_vers"
writeln "#"
writeln "#==========================================================="
writeln "#"
writeln "# Base config"
writeln "#"
writeln "set-option -g prefix C-x"
writeln "bind C-c kill-server"
writeln "set-option -s escape-time 100"
writeln "set-option -g display-time 1000"
writeln "set-option -g monitor-activity off"
writeln "set-option -g visual-bell on"
# writeln "set-option -g focus-events on"
# to hopefully avoid filling the screen with a fancy prompt, use a minimal shell
writeln "set-option -g default-command '/bin/sh'"
$use_mouse && tmux_vers_ok "$mouse_vers_min" && writeln "set-option -g mouse on"
tmux_vers_ok 2.6 && writeln "set-option -g monitor-bell off"
tmux_vers_ok 2.8 && writeln "bind Any display 'This key is not bound to any action'"
tmux_vers_ok 3.2 && {
writeln "set-option -g extended-keys on"
writeln "set -g -a terminal-features '*:extkeys'"
}
if command -v showkey >/dev/null; then
cmd="showkey -a" # this is used to display keys tmux did not capure"
else
cmd="cat"
fi
keys="'$cmd # this is used to display keys tmux did not capure'"
writeln "run-shell -b \"sleep 0.1 ; tmux send-keys $keys C-M\""
}
define_status_bar() {
local prefix_color=" #[fg=colour231]#[bg=colour04]"
# local prefix_pressed="Prefix ${prefix_color}C-x#[default] - Press C-c to exit"
local prefix_pressed="Got C-x - Next Press ${prefix_color}C-c#[default] to exit"
local exit_procedure="To exit press C-x then C-c."
local serv_vers_info="tmux version:#[fg=green,bg=black] $tpt_current_vers #[default]"
local line_1="Displays recognized un-prefixed keys"
$use_mouse && line_1+=" and mouse events"
if tmux_vers_ok 2.9; then
#
# Use line 2 & 3 for general info
#
writeln
writeln "#"
writeln "# Display some hints in status bar left row 2 & 3"
writeln "#"
writeln "set-option -g status 3"
writeln "set-option -g status-format[1] '$line_1'"
writeln "set-option -g status-format[2] '$exit_procedure'"
writeln "set-option -g status-left-length $unlimited"
writeln "# set-option -g window-status-format ''"
writeln "set-option -g window-status-current-format ''"
writeln "set-option -g status-left ''" # Clear it to suppress ses name from showing
else
#
# For older version only one line is available
#
writeln
writeln "#"
writeln "# Display exit hint in Status bar left"
writeln "#"
writeln "set-option -g status-justify left"
writeln "set-option -g status-left-length $unlimited"
writeln "set-option -g window-status-format ''"
writeln "set-option -g window-status-current-format ''"
writeln "set-option -g status-left '$exit_procedure This displays recognized keys'"
fi
writeln
writeln "#"
writeln "# Display currently used tmux version & prefix key pressed in Status bar right"
writeln "#"
writeln "set-option -g status-right '#{?client_prefix,$prefix_pressed,$serv_vers_info}'"
}
setup_tmux_server() {
base_config
define_status_bar
}
#---------------------------------------------------------------
#
# Display various inputs
#
#---------------------------------------------------------------
mouse_handling() {
local old_ifs="$IFS"
local locations=(
Pane
Border
Status
)
local mouse_events=(
MouseDown
MouseUp
MouseDrag
MouseDragEnd
)
local buttons=(
1
2
3
)
local event button location
tmux_vers_ok 2.4 && {
mouse_events+=(
DoubleClick
TripleClick
)
}
tmux_vers_ok 2.9 && {
locations+=(
StatusLeft
StatusRight
StatusDefault
)
}
tmux_vers_ok 3.2 && {
mouse_events+=(
SecondClick
)
}
writeln
writeln
writeln "#==========================================================="
writeln "#"
writeln "# Mouse handling"
writeln "#"
writeln "#==========================================================="
writeln
old_ifs="$IFS"
IFS=$'\n'
for location in "${locations[@]}"; do
for event in "${mouse_events[@]}"; do
for button in "${buttons[@]}"; do
# bind_mouse_script "${event}${button}${location}"
bind_char "${event}${button}${location}" s
done
done
# Obly loop over location for Wheel events
bind_char "WheelUp$location"
bind_char "WheelDown$location"
done
IFS="$old_ifs"
}
lower_case_chars() {
clear_skip_mesages_stack
header_3 "Lower Case"
case "$mod" in
S- | C-S- | M-S- | C-M-S-)
writeln "# $mod - should be handled in Upper Case section"
return
;;
*) ;;
esac
bind_char a
bind_char b
bind_char c
bind_char d
bind_char e
bind_char f
bind_char g
bind_char h
case "$mod" in
C- | C-M-)
if tmux_vers_ok 3.5; then
bind_char i
else
writeln "# Prior to 3.5 ${mod}i overrides Tab"
fi
;;
*) bind_char i ;;
esac
bind_char j
bind_char k
bind_char l
case "$mod" in
C- | C-M-)
if tmux_vers_ok 3.5; then
bind_char m
else
writeln "# Prior to 3.5 ${mod}m overrides Enter"
fi
;;
*) bind_char m ;;
esac
bind_char n
bind_char o
bind_char p
bind_char q
bind_char r
bind_char s
bind_char t
bind_char u
bind_char v
bind_char w
case "$mod" in
C-)
# Special case don't bind this to the root table, in order to display prefix
# on second press
writeln "bind '${mod}x' display-message '${mod}x'"
;;
*) bind_char x ;;
esac
bind_char y
bind_char z
! tmux_vers_ok 2.2 && push_skip_message "Accents & umlauts can't be bound before 2.2"
bind_char å
bind_char ä
bind_char ö
header_3 "acute accent"
bind_char á
bind_char é
bind_char í
bind_char ó
bind_char ú
bind_char ý
}
upper_case_chars() {
clear_skip_mesages_stack
header_3 "Upper Case"
case "$mod" in
C- | C-M-)
if ! tmux_vers_ok 3.2; then
push_skip_message "Prior to 3.2 ${mod}Uppercase overrides ${mod}Lowercase"
elif ! tmux_vers_ok 3.5a; then
#region Blurb about using C-S-A instead of C-A
writeln "
#
# Versions 3.2 - 3.5 didn't handle C-uppercase properly.
# I will use a/A to make the samples easier to write but is the same for all uppercases.
#
# Any action bound to C-A will be associated with C-a, overwriting anything already
# bound to C-a. C-A is not recognized as input by tmux.
#
# What intuitively would be thought of as C-A does not exist in tmux at this time.
# It has to be referred to as C-S-a, C-S-A, S-C-a or S-C-A
#
# Same if you add M- as an additional prefix.
#
# thus C- / C-M- Uppercase is skipped for this tmux version.
#
"
#endregion
return
fi
;;
C-S- | C-M-S-)
if ! tmux_vers_ok 3.2 || tmux_vers_ok 3.5a; then
push_skip_message "${mod}Uppercase only meaningful 3.2 - 3.5"
fi
;;
S- | M-S-) push_skip_message "S- modifier pointless" ;;
*) ;;
esac
bind_char A
bind_char B
bind_char C
bind_char D
bind_char E
bind_char F
bind_char G
bind_char H
case "$mod" in
C- | C-M-)
if tmux_vers_ok 3.5; then
bind_char I
else
writeln "# Prior to 3.5 ${mod}I overrides Tab"
fi
;;
*) bind_char I ;;
esac
bind_char J
bind_char K
bind_char L
case "$mod" in
C- | C-M-)
if tmux_vers_ok 3.5; then
bind_char M
else
writeln "# Prior to 3.5 ${mod}M overrides Enter"
fi
;;
*) bind_char M ;;
esac
bind_char N
bind_char O
bind_char P
bind_char Q
bind_char R
bind_char S
bind_char T
bind_char U
bind_char V
bind_char W
bind_char X
bind_char Y
bind_char Z
[[ -z "$skip_message" ]] && ! tmux_vers_ok 2.2 && {
push_skip_message "Accents & umlauts can't be bound before 2.2"
}
bind_char Å
bind_char Ä
bind_char Ö
header_3 "acute accent"
bind_char Á
bind_char É
bind_char Í
bind_char Ó
bind_char Ú
bind_char Ý
}
non_letter_regular_cars() {
clear_skip_mesages_stack
header_3 "non-letter regular keys"
case "$mod" in
S- | C-S- | M-S- | C-M-S-)
writeln "# $mod - These don't differ between upper/lower case"
return
;;
*) ;;
esac
bind_char "1"
bind_char "2"
bind_char "3"
bind_char "4"
bind_char "5"
bind_char "6"
bind_char "7"
bind_char "8"
bind_char "9"
bind_char "0"
bind_char "!"
bind_char "@"
bind_char '#' s
bind_char "^"
bind_char "("
bind_char ")"
bind_char "-"
bind_char "_"
bind_char "="
bind_char "+"
case "$mod" in
C- | C-M-)
if tmux_vers_ok 3.5; then
bind_char "["
else
writeln "# Prior to 3.5 ${mod}[ overrides Escape"
fi
;;
*) bind_char "[" ;;
esac
bind_char "]"
bind_char "\\\\" d
bind_char ":"
bind_char "'" d
bind_char ","
bind_char "."
bind_char "<" d
bind_char ">" d
bind_char "?" d
if tmux_vers_ok 3.0; then
bind_char ";" d
else
writeln "# Prior to 3.0 ${mod}; not supported"
fi
! tmux_vers_ok 2.2 && push_skip_message "Not handled before 2.2"
bind_char §
bind_char ± # plus-minus sign
bind_char ° # degree symbol
bind_char £
bind_char €
bind_char "´"
pop_skip_message
tmux_vers_ok 3.3 || {
case "$mod" in
"C-" | "C-M-") push_skip_message "Not handled before 3.3" ;;
*) ;;
esac
}
bind_char "\`" d
bind_char "/"
tmux_vers_ok 3.5 || {
case "$mod" in
C- | C-M-) push_skip_message "Not handled before 3.5" ;;
*) ;;
esac
}
bind_char '~' s
bind_char '$' s
bind_char % d
bind_char "\&" d
bind_char '*' d
bind_char "{" d
bind_char "}" d
bind_char "|" d
bind_char '"' s
}
special_basic_keys() {
clear_skip_mesages_stack
header_2 "Special basic keys"
bind_char Tab
bind_char bTab
bind_char Enter
bind_char Space
bind_char BSpace
bind_char Up
bind_char Down
bind_char Left
bind_char Right
# tmux_vers_ok 3.1 || {
# case "$mod" in
# C- | C-S- | C-M- | C-M-S-)
# push_skip_message "Not handled before 3.2"
# ;;
# *) ;;
# esac
# }
case "$mod" in
C- | C-S- | C-M- | C-M-S-)
tmux_vers_ok 3.2a || {
push_skip_message "Not handled before 3.2a"
}
;;
*) ;;
esac
bind_char Escape
}
func_keys() {
clear_skip_mesages_stack
header_2 "Function keys"
bind_char F1
bind_char F2
bind_char F3
bind_char F4
bind_char F5
bind_char F6
bind_char F7
bind_char F8
bind_char F9
bind_char F10
bind_char F11
bind_char F12
}
above_arrows() {
clear_skip_mesages_stack
header_2 "Group normally above arrows"
bind_char IC # Insert
bind_char DC # Delete
bind_char Home
bind_char End
bind_char PgUp
bind_char PgDn
}
num_keyboard() {
clear_skip_mesages_stack
header_2 "Num Keyboard"
bind_char KP/
bind_char "KP*" d
bind_char KP-
bind_char KP7
bind_char KP8
bind_char KP9
bind_char KP+
bind_char KP4
bind_char KP5
bind_char KP6
bind_char KP1
bind_char KP2
bind_char KP3
bind_char KPEnter
bind_char KP0
bind_char KP.
}
regular_chars() {
header_2 "Regular keys"
lower_case_chars
upper_case_chars
non_letter_regular_cars
}
process_mod() {
mod="$1"
case "$mod" in
"") mod_long="No Prefix" ;;
S-) mod_long="Shift" ;;
C-) mod_long="Control" ;;
M-) mod_long="Meta" ;;
C-S-) mod_long="Control-Shift" ;;
M-S-) mod_long="Meta-Shift" ;;
C-M-) mod_long="Control-Meta" ;;
C-M-S-) mod_long="Control-Meta-Shift" ;;
*) mod_long="Unknown mod: $mod" ;;
esac
header_1 "$mod_long"
regular_chars
special_basic_keys
func_keys
above_arrows
num_keyboard
$use_mouse && tmux_vers_ok "$mouse_vers_min" && mouse_handling
}
#===============================================================
#
# Main
#
#===============================================================
d_tkbtst_location="$(dirname "$(realpath "$0")")"
# Stack to store messages
declare -a skip_message_stack=()
skip_message=""
use_mouse=false
mouse_vers_min="2.1"
# shellcheck source=utils.sh
. "$d_tkbtst_location"/utils.sh
[[ "$1" = "-m" ]] && {
tmux_vers_ok "$mouse_vers_min" || {
echo "ERROR: mouse can't be used in this app prior to tmux $mouse_vers_min"
exit 1
}
use_mouse=true
}
rm -f "$tmux_conf"
tpt_retrieve_running_tmux_vers
setup_tmux_server
process_mod ""
process_mod "S-"
process_mod "C-"
process_mod "C-S-"
process_mod "M-"
process_mod "M-S-"
process_mod "C-M-"
process_mod "C-M-S-"
exit 0