-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffermaker
More file actions
executable file
·1450 lines (1325 loc) · 35 KB
/
buffermaker
File metadata and controls
executable file
·1450 lines (1325 loc) · 35 KB
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
buffermaker_version="0.12-WIP -3 opt_debug_parallel todo:opt_debug_theme2"
## Buffermaker
# Pure bash tui framework (wip)
shopt -s lastpipe # For syntax highlighting
shopt -s extglob # Ensure advanced pattern matching is available
shopt -s checkwinsize && (:;:) # Enable and then trigger a terminal size refresh
# submodules
source bxcommon
source bxinput
source bxformat
source bxplatform
# variables
[ -z "${DEFIFS}" ] && declare -r DEFIFS="$IFS"
declare -A highlight faces faces_raw options charmap hooks resources
declare -A keys_global
declare -a k_hex buffers_l modes=('keys_global')
declare -n buffer bf_s bf_e bf_d
declare -i ismenu comment current_count
declare reset readin
default_faces=(
reset '\e[m'
default "$(:weight normal)"
border-highlight "$(:mode inverse)"
border-shadow "$(:mode inverse :foreground gray)"
menu "$(:background yellow :foreground black)"
menu-divider "$(:underline t)"
menu-highlight "$(:weight bold :underline t)"
menu-enabled "$(:background yellow :foreground black)"
menu-selected "$(:background light-yellow :foreground black :weight bold)"
checkbox-disabled "$(:foreground light-gray)"
checkbox-enabled "$(:foreground yellow :weight bold)"
action "$(:foreground light-blue)"
argument "$(:foreground light-magenta)"
button "$(:mode inverse :underline t)"
link "$(:underline t :foreground light-blue)"
link-highlight "$(:weight bold :underline t :foreground light-blue)"
title "$(:weight bold)"
alt "$(:slant italic)"
highlight "\e[4m$(:weight bold)"
accent "$(:foreground yellow)"
background-highlight "$(:background black)"
dim "$(:weight dim)"
divider "$(:foreground gray)"
name "$(:foreground magenta)"
hint "$(:foreground light-yellow)"
hint-highlight "$(:underline t :foreground light-yellow :weight bold)"
line-number "$(:foreground gray)"
line-number-empty "$(:weight dim :foreground gray)"
line-number-current-line "$(:weight bold :foreground light-red)"
tab-face "$(:weight dim :foreground gray)"
region "$(:weight bold :background gray)"
minibuffer-prompt "$(:weight normal)"
#
TODO "$(:background magenta)"
NOTE "$(:background gray)"
# common colors for format stuff
black '\e[30m' gray '\e[90m'
red '\e[31m' light-red '\e[91m'
green '\e[32m' light-green '\e[92m'
yellow '\e[33m' light-yellow '\e[93m'
blue '\e[34m' light-blue '\e[94m'
magenta '\e[35m' light-magenta '\e[95m'
cyan '\e[36m' light-cyan '\e[96m'
light-gray '\e[37m' white '\e[97m'
bg-black '\e[40m' bg-gray '\e[100m'
bg-red '\e[41m' bg-light-red '\e[101m'
bg-green '\e[42m' bg-light-green '\e[102m'
bg-yellow '\e[43m' bg-light-yellow '\e[103m'
bg-blue '\e[44m' bg-light-blue '\e[104m'
bg-magenta '\e[45m' bg-light-magenta '\e[105m'
bg-cyan '\e[46m' bg-light-cyan '\e[106m'
bg-light-gray '\e[47m' bg-white '\e[107m'
)
resources=(
[&]='&'
[line]='─'
[bold-line]='━'
[column]='│'
[3right]='├'
[3left]='┤'
[3bottom]='┬'
[3top]='┴'
[1right]='╶'
[1left]='╴'
[1bottom]='╷'
[1top]='╵'
[block-full]='█'
[block-dark]='▓'
[block-medium]='▒'
[block-light]='░'
[block-none]=' '
[crux]='╳'
[cross]='┼'
[bold-column]='┃'
[angle-down-right]='┌'
[bold-angle-down-right]='┏'
[angle-down-left]='┐'
[bold-angle-down-left]='┓'
[angle-up-right]='└'
[bold-angle-up-right]='┗'
[angle-up-left]='┘'
[bold-angle-up-left]='┛'
[double-line]='═'
[double-column]='║'
[double-angle-down-right]='╔'
[double-angle-down-left]='╗'
[double-angle-up-right]='╚'
[double-angle-up-left]='╝'
[item]='*'
[item-number]='.'
[tab]=' '
[checkbox-disabled]='[ ]'
[checkbox-enabled]='[x]'
[bracket-open]='['
[bracket-close]=']'
[dot]='.'
[dashed-line]='┄'
[bold-dashed-line]='┅'
[dashed-column]='┆'
[bold-dashed-column]='┇'
[dotted-line]='┈'
[bold-dotted-line]='┉'
[dotted-column]='┊'
[bold-dotted-column]='┋'
[integral-open]='⌠'
[integral-close]='⌡'
[buffermaker]="[BufferMaker ${buffermaker_version}]"
[underscore]='_'
[lt]='<'
[gt]='>'
[amp]='&'
[quot]='"'
[apos]=\'
[cent]='¢'
[pound]='£'
[yen]='¥'
[euro]='€'
[copy]='©'
[reg]='®'
[tm]='™'
)
# specific common face sets, to be loaded when needed
filetype_faces=(
dir "$(:weight bold :foreground blue :underline t)"
exec "$(:weight bold :foreground green)"
symlink "$(:weight bold :foreground cyan)"
unreadable "$(:foreground gray)"
char "$(:weight bold :background black :foreground yellow)"
socket "$(:weight bold :foreground magenta)"
image "$(:weight bold :foreground magenta)"
video "$(:weight bold :foreground light-magenta)"
audio "$(:foreground cyan)"
archive "$(:weight bold :foreground )"
text "$(:weight bold :foreground light-magenta)"
)
function load-default-config {
options=(
[mouse]=0
[todonote]=0
[line-number-mode]=0
[empty-line-char]=''
[tty-linuxfb]=1 # enable truecolor support for framebuffer
[full-redraw]=0
[esc-to-meta]=0
[linux-foreground]='bbbbbb' [linux-background]='000000'
[linux-red]='bb2040' [linux-light-red]='ff4066'
[linux-green]='008040' [linux-light-green]='30aa60'
[linux-blue]='0050b0' [linux-light-blue]='3060ee'
[linux-black]='262626' [linux-light-black]='393939'
[linux-yellow]='b58900' [linux-light-yellow]='d5a920'
[linux-magenta]='a33682' [linux-light-magenta]='c356a2'
[linux-cyan]='00a198' [linux-light-cyan]='20c1b8'
[linux-white]='8a8a8a' [linux-light-white]='eee8d5'
)
add-mode empty; {
mode-options; {
:: else :
:: disable-global 1
}
}
add-mode menu
:: '[up]' menu.up
:: "$(kbd k)" menu.up
:: 'C-p' menu.up
:: '[down]' menu.down
:: "$(kbd j)" menu.down
:: 'C-n' menu.down
:: 'RET' menu.select
mode-options
:: else menu.exit
:: disable-global 1
add-mode basic
:: '[left]' format-left
# :: '[wheel-up]' format-up
:: '[up]' format-up
:: '[right]' format-right
# :: '[wheel-down]' format-down
:: '[down]' format-down
:: '[next]' scroll-down #pgdown
:: '[prior]' scroll-up #pgup
:: "$(kbd n)" scroll-left
:: "$(kbd m)" scroll-right
:: RET link-enter
:: C-c die
mode-options
:: else :
## Faces
# Defined in escape codes
# Helper functions:
# :weight
# normal
# bold
# :slant
# normal
# italic
# :underline
# t (enables)
# nil (do nothing)
# :background & :foreground
# either hex. colour, colour name, or c<index> for 256 indexed colours
# See extensions/gruvboxdark for another example
load-theme default_faces
#dired:load-mode
}
## syntaxy sugar sugary syntax
function @ifs { IFS="$DEFIFS"; }
function ::
case "$_last_add" in
add-menu) local-set-menu "$@";;
global-set) global-set-key "$@";;
set-options) define-option "$@";;
add-mode) local-set-key "$@";;
mode-options) local-set-mode-option "$@";;
set-buffer) local-set-buffer-data "$@"
esac
## focus set magic # function
function focus {
_last_add="$1"
}
## local-set-buffer-data
# set bf_d[] array of currently \"focused\" buffer
function local-set-buffer-data {
while [ -n "$2" ]; do
bf_d["$1"]="$2"
shift 2
done
}
## clear-screen clears screen
function clear-screen { printf '\033c'; }
## global-set Brings into \"focus\" global mode
function global-set { last_add=global-set; }
## define-option Defines options[] buffer
function define-option { options["$1"]="$2"; }
## set-options Brings into \"focus\" options setting
function set-options { _last_add=set-options; }
function menu.add {
# create new menu function & array
#eval "function ${1}.open { menu $1; }"
declare -ag "menu_${1}_0"
declare -ag "menu_${1}_1"
declare -ag "menu_${1}_2"
local -n menu_0="menu_${1}_0"
local -n menu_1="menu_${1}_1"
local -n menu_2="menu_${1}_2"
local -i c=-1
local type
local -n menu="$1"
for i in "${menu[@]}"; do
case "$i" in
't:'|'text:')
type=text
((c++))
continue;;
'c:'|'cmd:'|'command:')
type=command
continue;;
'f:'|'face:')
type=face
continue
esac
case "$type" in
'text')
menu_0[c]+=" $i"
;;
'command')
menu_1[c]+="$i "
;;
'face')
menu_2[c]+="$i"
esac
done
menu.layout "$1"
}
function menu.layout {
local -n menu_0="menu_${1}_0"
local -i max=0
for i in "${menu_0[@]}"; do
((${#i} > max)) && max="${#i}"
lax=${i}
done
for i in "${!menu_0[@]}"; do
# menu_0[i]+="a b c"
for ((l=$((max - ${#menu_0[i]})) + 1; l>0; l--)); do
menu_0[i]+=' '
done
done
}
## add gradient and its faces, hex code only
# syntax example:
# gradient ':background' name 50 ':foreground "#000000"' ff0000 ffff00 00ff00 00ffff 0000ff ff00ff
function gradient {
local name="$2"
local -n cugr="$2"
local steps="$3"
local before="$1"
local after="$4"
shift 4
local colors=("$@")
local num_segments=$(( ${#colors[@]} - 1 ))
local base=$(( (steps - 1) / num_segments ))
local extra=$(( (steps - 1) % num_segments ))
local seg_steps
local idx=0
for ((s=0; s<num_segments; s++)); do
seg_steps=$(( base + (s < extra ? 1 : 0) + 1 ))
local start=${colors[s]}
local end=${colors[s+1]}
[ "${start:0:1}" = '#' ] && start="${start:1}"
[ "${end:0:1}" = '#' ] && end="${end:1}"
local r1=$((16#${start:0:2}))
local g1=$((16#${start:2:2}))
local b1=$((16#${start:4:2}))
local r2=$((16#${end:0:2}))
local g2=$((16#${end:2:2}))
local b2=$((16#${end:4:2}))
declare -ag grloadfc
for ((i=0; i<seg_steps; i++)); do
if (( s < num_segments-1 && i == seg_steps-1 )); then
continue
fi
local r=$(( r1 + (r2 - r1) * i / (seg_steps - 1) ))
local g=$(( g1 + (g2 - g1) * i / (seg_steps - 1) ))
local b=$(( b1 + (b2 - b1) * i / (seg_steps - 1) ))
cugr+=("${name}_fc${s}${i}")
grloadfc+=("${name}_fc${s}${i}")
grloadfc+=("$(eval "${before} \"#$(printf "%02x%02x%02x\n" "$r" "$g" "$b")\" ${after}")")
done
done
load-theme grloadfc
}
## load-theme Loads theme from array
function load-theme {
local -i t=0
local k=''
local -n theme="$1"
for i in "${theme[@]}"; {
((t==0)) && {
k="$i"
t=1
} || {
faces["$k"]="$i"
printf -v faces_raw["$k"] '%b' "$i"
t=0
}
}
((t)) && errno=1 die
}
## rest of this garbageware
## switch-mode
# Change current mode of buffer into '$1'
function switch-mode {
bckmode="${bf_d[mode]}"
bf_d[mode]="$1"
}
## mode-back
# Restores previous mode
function mode-back {
bf_d[mode]="$bckmode"
unset bckmode
}
## read-command
# Works like normal "'read'" but for bottom commanline
function read-command {
local -i loc
[ -n "$4" ] && loc="$4" || loc=$((bf_d[size-y] + bf_d[loc-y]))
printf '\e[%s;0H' "$loc"
printf '\e[?25h'
read -re -p "$1" -i "$3" "$2" || return 1
}
### TODO: make this make sense
function declare-buffer {
_last_add='set-buffer'
set-buffer "${1}" # name
buffers_l+=("$current_buffer")
last_line=1
declare -Ag "${current_buffer}_bf_t"
declare -Ag "${current_buffer}_bf_h"
declare -Ag "${current_buffer}_bf_d"
bf_d=(
# some sane default values
[line]=0
[base]=1
[column]=0
[modified]=0
[loc-x]=1
[loc-y]=1
[size-x]=$COLUMNS
[size-y]=$LINES
[bxrender-function]='syntax1'
)
# weird debug
# bf_h=( [redraw.content begin]=linenum.begin [redraw.content loop-begin]=linenum.loop-begin [redraw.cursor pre-redraw]=linenum.update )
}
## set-buffer
# Switch to buffer \$1
function set-buffer {
current_buffer="$1"
declare -ng buffer="${current_buffer}_buffer" # source
declare -ng bf_s="${current_buffer}_bf_s" # syntax
declare -ng bf_e="${current_buffer}_bf_e" # charmap
declare -ng bf_d="${current_buffer}_bf_d" # data
declare -ng bf_c="${current_buffer}_bf_c" # commands/objects
declare -ng bf_h="${current_buffer}_bf_h" # hooks
declare -ng bf_t="${current_buffer}_bf_t" # tmp cache
}
function switch-buffer {
set-buffer "$1"
redraw
}
## add-to-list
# Adds a buffer into currently active buffer list
function add-to-list {
buffers_l+=("$current_buffer")
}
## clear-buffer
# Empties buffer bf_s bf_e \& bf_d
function clear-buffer {
buffer=()
for l in "${bf_s[@]}"; {
unset "$l"
}
bf_s=()
bf_e=()
for l in "${bf_c[@]}"; {
unset "$l"
}
bf_c=()
}
## goto Load array into buffer
function goto {
clear-buffer
bf_d[line]=1
bf_d[column]=0
copy-array "$1" buffer
redraw
}
function format-build {
[ "$2" = '{' ] || exit 1
declare -ng __ext_name="$1"
declare -g __ext_id=0
declare -ag __ext_type
__ext_type[0]='none'
__ext_type[1]='newline'
set +e
# PATHbak="$PATH"
# PATH=''
# trap 'eval "+ $BASH_COMMAND"' ERR
function + {
case "${__ext_type[-1]}" in
'newline') __data-new "$1";;
'oneline') __data-add "$1";;
*) : ;;
esac
}
function newline, { __data-new ''; }
function newline {
for ((i = 0; i < ${1::-1}; i++)); do
__data-new ''
done
}
function line {
__ext_id=0
__ext_num='-1'
__ext_name+=('')
__ext_type+=('oneline')
}
function title { __ext_type+=('title'); }
function subtitle { __ext_type+=('subtitle'); }
function __data-add {
if ((__ext_id==0)); then
__ext_name[-1]+="$*"
__ext_id=1
else
__ext_name[-1]+=" $*"
fi
}
function __data-new {
__ext_name+=("$*")
}
function }, {
case "${__ext_type[-1]}" in
'title')
case "$level" in
'1') __data-new "<h1> $label </h1>";;
'2') __data-new "<h2> $label </h2>";;
'3') __data-new "<h3> $label </h3>";;
*) __data-new "<h> $label </h>";;
esac
level='';;
'subtitle')
case "$level" in
*) __data-new "<subh1> $label </subh1>";;
esac
level='';;
esac
unset '__ext_type[-1]'
}
}
## Dumps current buffer into a file
function dump-buffer {
true >"$1" # Set the file to an empty text file
for ln in "${buffer[@]}"; do # Write in the buffer to the file
echo "$ln" >>"$1"
done
}
## die
# Closes Ebashs with exit code \$errno, if errno is empty exit with 0
# If \$nocleanup is 1, do not restore the terminal to sane mode and do not cleanup
# Else restore to sane mode
function die {
((nocleanup)) && exit "${errno:-0}"
echo -e "\e[?1000;1006;1015l" #disable mouse tracking
clear-screen # TODO fix the buffer switching
printf '\e[?25h\e[?7h\e[?1049l' # Reset terminal to sane mode
global-hook die
exit "${errno:-0}" # Assume that we are exiting without an error
}
# Sets \$nocleanup to 1 and dies
function die.force {
((nocleanup=1)) && die "$@"
}
function move.previous-line.check ((bf_d[line] > 1))
function move.next-line.check ((bf_d[line] < ${#buffer[@]}+bf_d[last-newline]))
# Moves up \$1 lines, if \$ is empty move up 1 line
function move.previous-line {
for ((i = 0; i < ${1:-1}; i++)); do
((bf_d[line] > 1)) && ((bf_d[line]--))
((bf_d[line] < bf_d[base] + 1)) && {
((bf_d[base]--)) # Push back the top if we need to
move_base=1 # Send signal to redraw.cursor that the buffer was moved, thus needs to be redraw fully
((bf_d[base] <= 0)) && bf_d[base]=1 # Don't push back if our base is at 1
}
[ -z "$bckrl" ] && bckrl="${bf_d[column]}"
(( ${#bf_e[bf_d[line]]} < bf_d[column] )) && bf_d[column]=${#bf_e[bf_d[line]]}
(( ${#bf_e[bf_d[line]]} > bckrl )) && bf_d[column]=${bckrl}
done
}
# Moves down \$1 lines, if \$ is empty move down 1 line
function move.next-line {
for ((i = 0; i < ${1:-1}; i++)); do
((bf_d[line] < ${#buffer[@]}+bf_d[last-newline])) && ((bf_d[line]++))
# Move window down if needed
((bf_d[line] > bf_d[base] + bf_d[size-y] - 3)) && {
((bf_d[base]++))
move_base=1
}
[ -z "$bckrl" ] && bckrl="${bf_d[column]}"
(( ${#bf_e[bf_d[line]]} < bf_d[column] )) && bf_d[column]=${#bf_e[bf_d[line]]}
(( ${#bf_e[bf_d[line]]} > bckrl )) && bf_d[column]=${bckrl}
done
}
function previous-line {
move.previous-line.check || return
hook pre-move
move.previous-line "$@"
hook move
redraw.cursor
}
function next-line {
move.next-line.check || return
hook pre-move
move.next-line "$@"
hook move
redraw.cursor
}
function scroll-down {
hook pre-move
move.previous-line $((bf_d[size-y] - 3))
hook move
redraw.cursor
}
function scroll-up {
hook pre-move
move.next-line $((bf_d[size-y] - 3))
hook move
redraw.cursor
}
function scroll-right {
((bf_d[basecolumn]++))
redraw
}
function scroll-left {
((bf_d[basecolumn]<=0)) && { bf_d[basecolumn]=0; return; }
((bf_d[basecolumn]--))
redraw
}
function beginning-of-buffer {
hook pre-move
bf_d[base]=1
bf_d[line]=1
hook move
redraw
}
function end-of-buffer {
hook pre-move
bf_d[base]=$((${#buffer[@]} - bf_d[size-y] + 4))
bf_d[line]=$((${#buffer[@]}))
hook move
redraw
}
function move-beginning-of-line { bf_d[column]=0; redraw; }
function move-end-of-line { bf_d[column]="${#bf_e[${bf_d[line]}]}"; redraw; }
function forward-char {
for ((i = 0; i < ${1:-1}; i++)); do
case "${bf_e[${bf_d[line]}]:${bf_d[column]}+1:1}" in
't') bf_d[column]=$(( bf_d[column]+${#resources[tab]}));;
*) ((bf_d[column]++));;
esac
((bf_d[column] > ${#bf_e[${bf_d[line]}]})) && {
bf_d[column]=0
next-line
return
}
bckrl=
done
redraw-line.cursor
}
function backward-char {
for ((i = 0; i < ${1:-1}; i++)); do
case "${bf_e[${bf_d[line]}]:${bf_d[column]}-1:1}" in
't') bf_d[column]=$(( bf_d[column]-${#resources[tab]}));;
*) ((bf_d[column]--));;
esac
((bf_d[column] < 0)) && ((bf_d[line]==1)) && {
bf_d[line]=1
bf_d[column]=0
}
((bf_d[column] < 0)) && {
bf_d[column]="${#bf_e[${bf_d[line]}-1]}"
previous-line
return
}
bckrl=
done
redraw-line.cursor
}
## forward-word
# Moves forward to next word
function forward-word {
((bf_d[column] == ${#bf_e[bf_d[line]]})) && {
next-line
move-beginning-of-line
}
while :; do
((bf_d[column] < ${#bf_e[bf_d[line]]})) && ((bf_d[column]++)) || break
case "${bf_e[bf_d[line]]:${bf_d[column]}:1}" in
't'|'s') break
esac
done
redraw
}
## backward-word
# Moves backward to previous word
function backward-word {
((bf_d[column] == 0)) && {
previous-line
move-end-of-line
}
while :; do
((bf_d[column] > 0)) && ((bf_d[column]--)) || break
case "${bf_e[bf_d[line]]:${bf_d[column]}:1}" in
't'|'s') break
esac
done
redraw
}
#Move cursor to line number $1 and move the line into middle of visible buffer area.
function to-line {
bf_d[line]="$1"
bf_d[base]=$((bf_d[line] - bf_d[size-y] / 2 ))
redraw
}
function menu.up {
((menuselection > 0)) && ((menuselection--));
redraw.menu;
}
function menu.down {
((menuselection < ${#menu_0[@]} -1)) && ((menuselection++));
redraw.menu;
}
# Execute selected item in menu
function menu.select
for i in "${!menu_1[@]}"; {
((i == menuselection)) && ${menu_1[i]}
}
# Closes a menu
function menu.exit {
unset bf_d[menu_x] bf_d[menu_y]
ismenu=0
bf_d[mode]="${modebackup}"
redraw
}
# Opens a menu \$1
function menu {
declare -ng menu_0=menu_${1}_0
declare -ng menu_1=menu_${1}_1
declare -ng menu_2=menu_${1}_2
((ismenu == 0 )) && modebackup="${bf_d[mode]}"
ismenu=1
declare -ig menuselection=0
bf_d[mode]='menu'
redraw
}
# deletes current buffer
function delete-buffer {
local -a copy
local i=0
local index
for b in "${buffers_l[@]}"
do
[[ "$b" != "$current_buffer" ]] && copy+=("$b")
[[ "$b" = "$current_buffer" ]] && index="$i"
((i++))
done
unset buffers_l
copy-array copy buffers_l
current_buffer="${buffers_l[index-1]}"
redraw
}
# Handle changing size of terminal
function change-size-screen {
bf_d[size-x]="$COLUMNS"
bf_d[size-y]=$((LINES-1))
redraw
}
# function scene {
# [ "$2" = '{' ] || exit 1
# declare -ng scene="sc_${1}"
# declare -g sc_i=0
# declare -ag sc_s
# sc_t[0]='none'
# sc_t[1]='newline'
# function buffer {
# scene+=('<buffer>')
# scene+=("${1}")
# sc_t+=('buffer')
# sc_s+=("${1}")
# }
# function scale: {
# scene+=('<scale>')
# scene+=('full')
# }
# function }, {
# case "${sc_t}" in
# 'buffer') scene+=('<commit>');;
# 'buffer') scene+=("${sc_s[-1]}");;
# esac
# unset 'sc_t[-1]'
# unset 'sc_s[-1]'
# scene+=('<close>')
# }
# }
# function scene.render {
# declare -ng scene="sc_${1}"
# print-array scene > d
# for pk in "${scene[@]}"; do
# case "${__expect}" in
# 'bf') set-buffer "$pk"; __expect='';;
# *) :;;
# esac
# case "${pk}" in
# '<buffer>') __expect="bf";;
# '<scale>') __expect="scale";;
# '<close>') __expect="close";;
# esac
# done
# }
## redraw
# Redraw screen
## hooks
# stored in bf_h ass. array
# naming scheme is '<function> <name>', function name is handled automatically when defining a hook via @hook
function @hook {
local name="${FUNCNAME[1]} $1"
shift
[ -n "${bf_h[${name}]}" ] && {
${bf_h[${name}]} "$@"
return 0
}
return 1
}
# common hooks, without function prefix
function hook {
local name="$1"
shift
[ -n "${bf_h[${name}]}" ] && {
${bf_h[${name}]} "$@"
return 0
}
return 1
}
# global hooks, shared in every buffer
function global-hook {
local name="$1"
shift
[ -n "${hooks[${name}]}" ] && {
${hooks[${name}]} "$@"
return 0
}
return 1
}
## insert-word
# Insert \$1 to buffer
function insert-word {
local -i rlr
unset ta
local ta
ta=${bf_e[${bf_d[line]}]:0:${bf_d[column]}}
ta=${ta//[^t]}
ta=${#ta}
rlr=$((bf_d[column] + ta / 4 - ta))
buffer[bf_d[line]]="${buffer[${bf_d[line]}]:0:${rlr}}$1${buffer[${bf_d[line]}]:${rlr}}"
#add at cursor position - amount of tabs * tab size
make-render-line
forward-char "${#1}"
redraw
bf_d[modified]=1
}
function init-var {
trap change-size-screen WINCH ALRM # Attach WINCH and ALRM to redraw the screen
trap die EXIT HUP USR1 # Attach most exit codes to cleanup and exit
trap 'k_hex=(3 0); keypress' INT ##MOVE: bxinput
trap die SIGTERM
reset='\e[0;0m'
charmap=(
#[0] is reserved for any unmapped char
[ ]='t' # tab
[ ]='s' # space
)
if ((options[esc-to-meta])); then esc_timeout=123456789 # a very big number ##MOVE: bxinput
else esc_timeout=0.01; fi ##MOVE: bxinput
nlchar='\n'
printf '\e[?1049h'
((options[mouse])) && echo -ne "\e[?1000;1006;1015h" #enable mouse tracking ##MOVE: bxinput
}
#RENDER
#bxrender_version="v11 internal"
# invalidate redraw cache for entire buffer
function refresh { clear-screen; bf_s=''; redraw; }
function mark-render-area {
for ((i=${1}; i<${2} ; i++)); {
unset bf_s[i]
}
}
#function make-render-area { mark-render-area "$@"; }
## make-render-line
# Syntax highlights single line '$1'
# Defaults to currently selected line if no argument is passed
function make-render-line {
local -i line=${1:-bf_d[line]}
local -n linearray="${current_buffer}_syntax${line}"
bf_s[line]="${current_buffer}_syntax${line}"
linearray=()
local -i comment=0 skip_next_space=0
local word=''
bf_e[line]=''
for _link in ${bf_c[line]}; do unset "$_link"; done; unset bf_c[line] # clear command buffer
column=0
bckeIFS="${IFS}"
IFS=''
"${bf_d[render-type]:-per-char}"
IFS="$bckeIFS"
}
function per-line {
IFS=' '; while read -rs word; do IFS=''; syntax-word; IFS=' '; done <<< "${buffer[line]}"
}
function per-word {
IFS=' '
newline=1
while read -rs -d' ' word; do
IFS=''
syntax-word
newline=0
((skip_next_space==1)) && skip_next_space=0 && continue
bf_e[line]+='s'
linearray+=(
"${faces_raw[reset]}${faces_raw[${facename:-${bf_d[background]:-default}}]} "
)
((column++))
IFS=' '
done <<< "${buffer[line]} "
}
## make-render-line-loop
# Also creates bf_e charmap for movenment
# "Tab is 't'"
# "Space is 's'"
# "Any unmaped char is '0'"
function per-char {
while read -rsn1 char; do