-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzui-list-input
862 lines (803 loc) · 31.7 KB
/
zui-list-input
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
emulate -L zsh
zmodload zsh/curses
setopt typesetsilent extendedglob
# Compute first to show index - exact page
-zui_listin_compute_first_to_show_idx() {
from_what_idx_list_is_shown=0+((current_idx-1)/page_height)*page_height+1
}
# Conditional, fully robust page-to-show computation
-zui_listin_compute_exact_page_if_needed() {
# Fallback: in case of any problems compute exact page
if [[ "$from_what_idx_list_is_shown" -lt "1" ||
"$from_what_idx_list_is_shown" -gt "$last_element" ]]
then
from_what_idx_list_is_shown=0+((current_idx-1)/page_height)*page_height+1
fi
}
# Compute first to show index - page is
# scrolled to center to show given index
-zui_listin_compute_first_to_show_idx_center() {
from_what_idx_list_is_shown=current_idx-page_height/2
-zui_listin_compute_exact_page_if_needed
}
# Calling this function means passing information
# that we move up in the list (to lower index)
# This means we can move page start as few as 3
# elements up.
-zui_listin_compute_first_to_show_idx_up_many() {
if [[ "$current_idx" -lt "$from_what_idx_list_is_shown" ||
"$current_idx" -gt "$(( from_what_idx_list_is_shown + page_height -1 ))" ]]
then
from_what_idx_list_is_shown=from_what_idx_list_is_shown-3
-zui_listin_compute_exact_page_if_needed
fi
}
# Like above, but in opposite direction
-zui_listin_compute_first_to_show_idx_down_many() {
if [[ "$current_idx" -lt "$from_what_idx_list_is_shown" ||
"$current_idx" -gt "$(( from_what_idx_list_is_shown + page_height -1 ))" ]]
then
from_what_idx_list_is_shown=from_what_idx_list_is_shown+3
-zui_listin_compute_exact_page_if_needed
fi
}
# Searches for hyperlink in current line,
# sets current_segment to first link found
-zui_listin_search_for_hyperlink() {
local -a segments
segments=( "${(Pz@)list_var_current}" )
# Hide global $nseg
local nseg="${#segments}"
[[ "$current_segment" -gt "$nseg" ]] && current_segment="$nseg"
[[ "$current_segment" -le 0 ]] && current_segment="1"
integer left=current_segment right=current_segment
# Left
while ! -zui_std_is_hyperlink "${segments[left]}" && ! -zui_std_is_text_field "${segments[left]}" && ! -zui_std_is_list_box "${segments[left]}"; do
(( -- left ))
[[ "$left" -le 0 ]] && break
done
# Right
while ! -zui_std_is_hyperlink "${segments[right]}" && ! -zui_std_is_text_field "${segments[right]}" && ! -zui_std_is_list_box "${segments[right]}"; do
(( ++ right ))
[[ "$right" -gt "$nseg" ]] && break
done
# Choose smaller distance
if (( left >= 1 && right <= nseg && (current_segment - left) <= (right - current_segment) )); then
current_segment=left
elif (( right <= nseg )); then
current_segment=right
elif (( left >= 1 )); then
current_segment=left
fi
}
# Searches for link that is closest graphically to previous
# current segment
-zui_listin_search_for_hyperlink_graphical() {
[[ "$current_idx" = "$prev_idx" ]] && { -zui_listin_search_for_hyperlink; return $?; }
local -a segments prev_segments stri_segs stri_prev_segs
local line prev_line s ss
# Position and size of current segment in previous line
integer target_offset target_size
# Position of examined segment, if proper, then stored
# as *_left or *_right
integer try try_left=0 try_left_size=0 try_right=0
# Index of examined segment, if proper, then stored
# as *_left or *_right
integer candidate_idx=0 candidate_left=0 candidate_right=0
segments=( "${(Pz@)list_var_current}" )
prev_segments=( "${(Pz@)list_var_prev}" )
-zui_std_strip_meta_data "${(P)list_var_current}"
line="$REPLY"
-zui_std_strip_meta_data "${(P)list_var_prev}"
prev_line="$REPLY"
-zui_std_strip_meta_data "${prev_segments[current_segment]}"
-zui_std_strip_color_codes "$REPLY"
target_size="${#REPLY}"
REPLY="${REPLY//\[/\\[}"
REPLY="${REPLY//\]/\\]}"
target_offset="${prev_line[(i)$REPLY]}"
for s in "${segments[@]}"; do
candidate_idx+=1
if ! -zui_std_is_any_hyperlink "$s"; then
continue
fi
-zui_std_strip_meta_data "$s"
-zui_std_strip_color_codes "$REPLY"
ss="$REPLY"
REPLY="${REPLY//\[/\\[}"
REPLY="${REPLY//\]/\\]}"
try="${line[(i)$REPLY]}"
if (( try <= target_offset )); then
candidate_left=candidate_idx
try_left=try
try_left_size=${#ss}
elif (( try > target_offset )); then
candidate_right=candidate_idx
try_right=try
break
fi
done
# Left (and possibly right) in middle of the examined button
if (( try_left != 0 && try_left + try_left_size >= target_offset )); then
if (( try_right != 0 && try_right < target_offset + target_size )); then
# Which does more pertrude
if (( target_offset + target_size - try_right > try_left + try_left_size - target_offset )); then
current_segment=candidate_right
else
current_segment=candidate_left
fi
else
current_segment=candidate_left
fi
# Right in middle of examined button
elif (( try_right != 0 && try_right < target_offset + target_size )); then
current_segment=candidate_right
# No in middle of the button, so distance to target button
# from right edge (left candidate) and left edge (right candidate)
elif (( try_left != 0 && try_right != 0 && target_offset - try_left - try_left_size < try_right - target_offset )); then
current_segment=candidate_left
# As above, opposite result
elif (( try_left != 0 && try_right != 0 && target_offset - try_left - try_left_size >= try_right - target_offset )); then
current_segment=candidate_right
# Single candidate - left
elif (( try_left != 0 )); then
current_segment=candidate_left
# Single candidate - right
elif (( try_right != 0 )); then
current_segment=candidate_right
else
# Fallback method
-zui_listin_search_for_hyperlink; return $?
fi
return 0
}
# Testable, searches for next (excluding current
# one) hyperlink, returns 1 if not found, 0 if
# found (this implies update of current_segment)
#
# $1 - direction: 0 - left, other - right
-zui_listin_set_next_hyperlink() {
local -a segments
segments=( "${(Pz@)list_var_current}" )
# Hide global $nseg
local nseg="${#segments}"
[[ "$current_segment" -gt "$nseg" ]] && current_segment="$nseg"
[[ "$current_segment" -le 0 ]] && current_segment="1"
integer left=current_segment right=current_segment
# Left
[[ "$1" = "0" ]] && {
while ! -zui_std_is_hyperlink "${segments[left]}" && ! -zui_std_is_text_field "${segments[left]}" && ! -zui_std_is_list_box "${segments[left]}" || (( left == current_segment )); do
(( -- left ))
[[ "$left" -le 0 ]] && break
done
if (( left >= 1 )); then
current_segment=left
return 0
fi
} || {
# Right
while ! -zui_std_is_hyperlink "${segments[right]}" && ! -zui_std_is_text_field "${segments[right]}" && ! -zui_std_is_list_box "${segments[right]}" || (( right == current_segment )); do
(( ++ right ))
[[ "$right" -gt "$nseg" ]] && break
done
if (( right <= nseg )); then
current_segment=right
return 0
fi
}
return 1
}
typeset -ga reply
reply=( -1 '' )
integer current_idx="$1"
integer current_segment="$2"
integer from_what_idx_list_is_shown="$3"
integer page_height="$4"
integer page_width="$5"
integer last_element="$6"
integer nseg="$7"
integer hscroll="$8"
local key="$9"
integer search="$10"
local buffer="$11"
integer uniq_mode="$12"
integer disable_search="$13"
local list_var_current="${14}[\$current_idx]"
local list_var_prev="${14}[\$prev_idx]"
integer prev_idx="$current_idx"
local text
local -a wrk_reply
# Trigger nearest hyperlink searching even
# when ZUI[text_mode] yields no search
integer search_for_hyperlink=0
#
# Listening for input
#
if [[ -n "${ZUI[current_tfield]}" ]]; then
-zui_util_get_segment "${(P)list_var_current}" "$current_segment"
if -zui_std_decode_text_field "$REPLY" wrk_reply; then
case "$key" in
($'\n')
ZUI[current_tfield]=""
reply=( $current_idx TFIELD_LEFT )
;;
($'\E')
: ${(P)wrk_reply[3]::=${ZUI[current_tfield_initial_sidx]}}
: ${(P)wrk_reply[4]::=${ZUI[current_tfield_initial_data]}}
ZUI[current_tfield]=""
reply=( $current_idx TFIELD_CANCELLED )
;;
(DOWN|NPAGE)
text="${(P)wrk_reply[2]}"
(( text = text > 5 ? text - 1 : text ))
: ${(P)wrk_reply[2]::=$text}
;;
(UP|PPAGE)
text="${(P)wrk_reply[2]}"
(( text ++ ))
: ${(P)wrk_reply[2]::=$text}
;;
($'\C-K'|$'\C-U')
: ${(P)wrk_reply[4]::=}
: ${(P)wrk_reply[3]::=1}
ZUI[cursor_x]=$(( ZUI[current_tfield_cidx_start] ))
;;
(HOME|$'\C-A')
ZUI[cursor_x]="${ZUI[current_tfield_cidx_start]}"
: ${(P)wrk_reply[3]::=1}
;;
(END|$'\C-E')
ZUI[cursor_x]="${ZUI[current_tfield_cidx_end]}"
text="${(P)wrk_reply[4]}"
-zui_sys_index_tail_width "$text" "${(P)wrk_reply[2]}" text
: ${(P)wrk_reply[3]::=$text}
;;
(LEFT)
text="${(P)wrk_reply[4]}"
if (( ZUI[cursor_x] - ${(m)#text[${ZUI[current_tfield_instr_idx]}]} >= ZUI[current_tfield_cidx_start] )); then
(( ZUI[cursor_x] -= ${(m)#text[${ZUI[current_tfield_instr_idx]}]} ))
else
text="${(P)wrk_reply[3]}"
(( text = text > 1 ? text - 1 : text ))
: ${(P)wrk_reply[3]::=$text}
fi
;;
(RIGHT)
text="${(P)wrk_reply[4]}"
if [[ "${(m)#text[${ZUI[current_tfield_instr_idx]}+1]}" -ne 0 ]] && (( ZUI[cursor_x] + ${(m)#text[${ZUI[current_tfield_instr_idx]}+1]} <= ZUI[current_tfield_cidx_end] )); then
(( ZUI[cursor_x] += ${(m)#text[${ZUI[current_tfield_instr_idx]}+1]} ))
else
# Dont advance cursor when we're at right boundary, advance
# index so that whole string is shown. Also, allow further
# advancement of the index, to scroll string left.
# string index + width - 1 < #string + width/2
if (( ${(P)wrk_reply[3]} + ${(P)wrk_reply[2]} - 1 < ${(P)#wrk_reply[4]} + ${(P)wrk_reply[2]} / 2 )); then
text="${(P)wrk_reply[3]}"
(( ++ text ))
: ${(P)wrk_reply[3]::=$text}
fi
fi
;;
($'\b'|$'\C-?'|BACKSPACE)
if (( ZUI[cursor_x] > ZUI[current_tfield_cidx_start] )); then
text="${(P)wrk_reply[4]}"
(( ZUI[cursor_x] -= ${(m)#text[${ZUI[current_tfield_instr_idx]}]} ))
text[${ZUI[current_tfield_instr_idx]}]=""
: ${(P)wrk_reply[4]::=$text}
fi
;;
(DC)
if (( ZUI[cursor_x] < ZUI[current_tfield_cidx_end] )); then
text="${(P)wrk_reply[4]}"
text[${ZUI[current_tfield_instr_idx]}+1]=""
: ${(P)wrk_reply[4]::=$text}
fi
;;
(??*|$'\t')
;;
(*)
text="${(P)wrk_reply[4]}"
if (( ZUI[current_tfield_instr_idx] )); then
text[${ZUI[current_tfield_instr_idx]}]="${text[${ZUI[current_tfield_instr_idx]}]}$key"
: ${(P)wrk_reply[4]::=$text}
# Scroll text right if string protrudes from
# right boundary of text field
if (( ${(m)#text[${(P)wrk_reply[3]},-1]} > ${(P)wrk_reply[2]} )); then
text="${(P)wrk_reply[3]}"
(( text += ${(m)#key} ))
: ${(P)wrk_reply[3]::=$text}
else
(( ZUI[cursor_x] += ${(m)#key} ))
fi
else
# Prepend
text="$key$text"
: ${(P)wrk_reply[4]::=$text}
(( ZUI[cursor_x] += ${(m)#key} ))
fi
;;
esac
-zui_sys_get_tfield_cursor_boundaries 'ZUI[current_tfield_cidx_start]' 'ZUI[current_tfield_cidx_end]' "${(P)list_var_current}" "${wrk_reply[1]}"
(( ZUI[cursor_x] = ZUI[cursor_x] > ZUI[current_tfield_cidx_end] ? ZUI[current_tfield_cidx_end] : ZUI[cursor_x] ))
-zui_sys_map_cursor_on_string "${(P)wrk_reply[3]}" "${(P)wrk_reply[4]}" $(( ZUI[cursor_x] - ZUI[current_tfield_cidx_start] - 1 )) 'ZUI[current_tfield_instr_idx]'
else
ZUI[current_tfield]=""
fi
elif [[ "$search" = "0" ]]; then
case "$key" in
(UP|k)
# Are there any elements before the current one?
[[ "$current_idx" -gt 1 ]] && current_idx=current_idx-1
while [[ -z "${(P)list_var_current}" && "$current_idx" -gt 1 ]]; do
current_idx=current_idx-1
done
-zui_listin_compute_first_to_show_idx_up_many
;;
(DOWN|j)
# Are there any elements after the current one?
[[ "$current_idx" -lt "$last_element" ]] && current_idx=current_idx+1
while [[ -z "${(P)list_var_current}" && "$current_idx" -lt "$last_element" ]]; do
current_idx=current_idx+1
done
-zui_listin_compute_first_to_show_idx_down_many
;;
($'\C-P')
[[ "$current_idx" -gt 1 ]] && current_idx=current_idx-1
while [[ -z "${(P)list_var_current}" && "$current_idx" -gt 1 ]]; do
current_idx=current_idx-1
done
-zui_listin_compute_first_to_show_idx_center
;;
($'\C-N')
# Are there any elements after the current one?
[[ "$current_idx" -lt "$last_element" ]] && current_idx=current_idx+1
while [[ -z "${(P)list_var_current}" && "$current_idx" -lt "$last_element" ]]; do
current_idx=current_idx+1
done
-zui_listin_compute_first_to_show_idx_center
;;
(PPAGE|$'\b'|$'\C-?'|BACKSPACE)
current_idx=current_idx-page_height
[[ "$current_idx" -lt 1 ]] && current_idx=1;
-zui_listin_compute_first_to_show_idx
;;
(NPAGE|" ")
current_idx=current_idx+page_height
[[ "$current_idx" -gt "$last_element" ]] && current_idx=last_element;
-zui_listin_compute_first_to_show_idx
;;
($'\C-U')
current_idx=current_idx-page_height/2
[[ "$current_idx" -lt 1 ]] && current_idx=1;
-zui_listin_compute_first_to_show_idx
;;
($'\C-D')
current_idx=current_idx+page_height/2
[[ "$current_idx" -gt "$last_element" ]] && current_idx=last_element;
-zui_listin_compute_first_to_show_idx
;;
(HOME|g)
current_idx=1
-zui_listin_compute_first_to_show_idx
;;
(END|G)
current_idx=last_element
-zui_listin_compute_first_to_show_idx
;;
($'\n'|ENTER)
# Is that element selectable?
# Check for this only when there is no search
if [[ -n "$buffer" || "$uniq" -eq 1 || -z ${ZUILIST_NONSELECTABLE_ELEMENTS[(r)$current_idx]} ]]
then
# Check if we are at hyperlink. Only then allow selection
-zui_util_get_segment "${(P)list_var_current}" "$current_segment"
local -a data
reply=( )
if -zui_std_decode_hyperlink "$REPLY"; then
if [[ "${reply[1]}" = zuianchor* || "${reply[1]}" = zuiiaction* ]]; then
# This is to run internal action,
# i.e. action without leaving list
reply=( $current_idx HYPERLINK )
else
# This hyperlink path is through
# quitting list and restarting it
reply=( $current_idx SELECT )
fi
elif -zui_std_decode_text_field "$REPLY"; then
ZUI[current_tfield]="${reply[1]}"
ZUI[current_tfield_initial_sidx]="${(P)reply[3]}"
ZUI[current_tfield_initial_data]="${(P)reply[4]}"
-zui_sys_get_tfield_cursor_boundaries 'ZUI[current_tfield_cidx_start]' 'ZUI[current_tfield_cidx_end]' "${(P)list_var_current}" "${reply[1]}"
ZUI[cursor_y]=$(( current_idx - from_what_idx_list_is_shown + 1 ))
ZUI[cursor_x]=$(( ZUI[current_tfield_cidx_end] ))
-zui_sys_map_cursor_on_string "${(P)reply[3]}" "${(P)reply[4]}" $(( ZUI[cursor_x] - ZUI[current_tfield_cidx_start] - 1 )) 'ZUI[current_tfield_instr_idx]'
reply=( $current_idx HYPERLINK )
elif -zui_std_decode_list_box "$REPLY"; then
local min_x max_x y_pos
-zui_sys_get_tfield_cursor_boundaries min_x max_x "${(P)list_var_current}" "${reply[1]}"
y_pos=$(( current_idx - from_what_idx_list_is_shown + 1 ))
-zui-list-box-loop "$page_height" "$page_width" "$y_pos" $(( min_x + 6 )) "${reply[@]}" && { reply=( $current_idx LBOX_LEFT ); } || { reply=( -1 '' ); }
else
# Selecting non-hyperlink is allowed?
(( ${ZUI[text_select]} )) && reply=( $current_idx SELECT ) || reply=( -1 '' )
fi
fi
;;
(q|Q)
reply=( -1 QUIT )
;;
(/)
if [[ "$disable_search" = "0" ]]; then
search=1
-zui_list_cursor_visibility "status" 1
fi
;;
($'\C-L')
reply=( -1 REDRAW )
;;
(\])
if [[ "${ZUILIST_ENABLED_EVENTS[(r)BR_MOVE_RIGHT]}" = "BR_MOVE_RIGHT" ]]; then
reply=( "$current_segment" "BR_MOVE_RIGHT" )
else
[[ "${(t)ZUILIST_HOP_INDICES}" = "array" || "${(t)ZUILIST_HOP_INDICES}" = "array-local" ]] && [[ -z "$buffer" && "$uniq" -eq 0 ]] &&
for idx in "${(n)ZUILIST_HOP_INDICES[@]}"; do
if [[ "$idx" -gt "$current_idx" ]]; then
current_idx=$idx
-zui_listin_compute_first_to_show_idx_center
break
fi
done
fi
;;
(\[)
if [[ "${ZUILIST_ENABLED_EVENTS[(r)BR_MOVE_LEFT]}" = "BR_MOVE_LEFT" ]]; then
reply=( "$current_segment" "BR_MOVE_LEFT" )
else
[[ "${(t)ZUILIST_HOP_INDICES}" = "array" || "${(t)ZUILIST_HOP_INDICES}" = "array-local" ]] && [[ -z "$buffer" && "$uniq" -eq 0 ]] &&
for idx in "${(nO)ZUILIST_HOP_INDICES[@]}"; do
if [[ "$idx" -lt "$current_idx" ]]; then
current_idx=$idx
-zui_listin_compute_first_to_show_idx_center
break
fi
done
fi
;;
('<'|'{')
hscroll=hscroll-7
[[ "$hscroll" -lt 0 ]] && hscroll=0
;;
('>'|'}')
hscroll+=7
;;
(LEFT|'h'|BTAB)
if [[ "$key" = "BTAB" && ${ZUILIST_NONSELECTABLE_ELEMENTS[(r)$current_idx]} = $current_idx ]]; then
[[ "$current_idx" -gt "1" ]] && {
current_idx=current_idx-1
current_segment="100"
}
while [[ -z "${(P)list_var_current}" && "$current_idx" -gt 1 ]]; do
current_idx=current_idx-1
done
-zui_listin_compute_first_to_show_idx_up_many
else
# Establish exact right-edge segment
if [[ "$current_segment" -gt "$nseg" ]]; then
(( current_segment = nseg ))
[[ "$current_segment" -le 0 ]] && current_segment=1
fi
if [[ "$key" = "BTAB" ]]; then
if ! -zui_listin_set_next_hyperlink 0; then
# No link left in current line - move
# one line up, set segment to far-most,
# verification at bottom will find the
# nearest hyperlink
[[ "$current_idx" -gt "1" ]] && {
current_idx=current_idx-1
current_segment="100"
search_for_hyperlink=1
}
while [[ -z "${(P)list_var_current}" && "$current_idx" -gt 1 ]]; do
current_idx=current_idx-1
done
-zui_listin_compute_first_to_show_idx_up_many
fi
else
if [[ "${ZUI[text_mode]}" = "off" ]]; then
# Search left accepting lack of success
-zui_listin_set_next_hyperlink 0
elif -zui_std_has_any_hyperlinks "${(P)list_var_current}"; then
if [[ "${ZUI[text_mode]}" = (hyp|all) ]]; then
(( -- current_segment ))
else
-zui_listin_set_next_hyperlink 0
fi
else
if [[ "${ZUI[text_mode]}" = (nohyp|all) ]]; then
(( -- current_segment ))
else
-zui_listin_set_next_hyperlink 0
fi
fi
fi
fi
;;
(RIGHT|'l'|$'\t')
if [[ "$key" = $'\t' && ${ZUILIST_NONSELECTABLE_ELEMENTS[(r)$current_idx]} = $current_idx ]]; then
[[ "$current_idx" -lt "$last_element" ]] && {
current_idx=current_idx+1
current_segment=1
}
while [[ -z "${(P)list_var_current}" && "$current_idx" -lt "$last_element" ]]; do
current_idx=current_idx+1
done
-zui_listin_compute_first_to_show_idx_down_many
else
if [[ "$key" = $'\t' ]]; then
# Search hyperlinks right
if ! -zui_listin_set_next_hyperlink 1; then
# Move to next line, set current_segment
# to 1, verification at bottom will find
# nearest hyperlink if it exists in the
# next line
[[ "$current_idx" -lt "$last_element" ]] && {
current_idx=current_idx+1
current_segment="1"
search_for_hyperlink=1
}
while [[ -z "${(P)list_var_current}" && "$current_idx" -lt "$last_element" ]]; do
current_idx=current_idx+1
done
-zui_listin_compute_first_to_show_idx_down_many
fi
else
if [[ "${ZUI[text_mode]}" = "off" ]]; then
# Search right accepting lack of success
-zui_listin_set_next_hyperlink 1
elif -zui_std_has_any_hyperlinks "${(P)list_var_current}"; then
if [[ "${ZUI[text_mode]}" = (hyp|all) ]]; then
(( ++ current_segment ))
else
-zui_listin_set_next_hyperlink 1
fi
else
if [[ "${ZUI[text_mode]}" = (nohyp|all) ]]; then
(( ++ current_segment ))
else
-zui_listin_set_next_hyperlink 1
fi
fi
fi
[[ "$current_segment" -gt "$nseg" ]] && current_segment="$nseg"
fi
;;
(F1|F2|F3|F4)
reply=( -1 "$key" )
;;
($'\E')
buffer=""
;;
(o|$'\C-O')
uniq_mode=1-uniq_mode
;;
(*)
;;
esac
else
case "$key" in
($'\n'|ENTER)
search=0
-zui_list_cursor_visibility "status" 0
;;
($'\C-L')
reply=( -1 REDRAW )
;;
#
# Slightly limited navigation
#
(UP)
[[ "$current_idx" -gt 1 ]] && current_idx=current_idx-1
while [[ -z "${(P)list_var_current}" && "$current_idx" -gt 1 ]]; do
current_idx=current_idx-1
done
-zui_listin_compute_first_to_show_idx_up_many
;;
(DOWN)
[[ "$current_idx" -lt "$last_element" ]] && current_idx=current_idx+1
while [[ -z "${(P)list_var_current}" && "$current_idx" -lt "$last_element" ]]; do
current_idx=current_idx+1
done
-zui_listin_compute_first_to_show_idx_down_many
;;
($'\C-P')
[[ "$current_idx" -gt 1 ]] && current_idx=current_idx-1
while [[ -z "${(P)list_var_current}" && "$current_idx" -gt 1 ]]; do
current_idx=current_idx-1
done
-zui_listin_compute_first_to_show_idx_center
;;
($'\C-N')
# Are there any elements after the current one?
[[ "$current_idx" -lt "$last_element" ]] && current_idx=current_idx+1
while [[ -z "${(P)list_var_current}" && "$current_idx" -lt "$last_element" ]]; do
current_idx=current_idx+1
done
-zui_listin_compute_first_to_show_idx_center
;;
(PPAGE)
current_idx=current_idx-page_height
[[ "$current_idx" -lt 1 ]] && current_idx=1;
-zui_listin_compute_first_to_show_idx
;;
(NPAGE)
current_idx=current_idx+page_height
[[ "$current_idx" -gt "$last_element" ]] && current_idx=last_element;
-zui_listin_compute_first_to_show_idx
;;
($'\C-U')
current_idx=current_idx-page_height/2
[[ "$current_idx" -lt 1 ]] && current_idx=1;
-zui_listin_compute_first_to_show_idx
;;
($'\C-D')
current_idx=current_idx+page_height/2
[[ "$current_idx" -gt "$last_element" ]] && current_idx=last_element;
-zui_listin_compute_first_to_show_idx
;;
(HOME)
current_idx=1
-zui_listin_compute_first_to_show_idx
;;
(END)
current_idx=last_element
-zui_listin_compute_first_to_show_idx
;;
(LEFT|BTAB)
if [[ "$key" = "BTAB" && ${ZUILIST_NONSELECTABLE_ELEMENTS[(r)$current_idx]} = $current_idx ]]; then
[[ "$current_idx" -gt "1" ]] && {
current_idx=current_idx-1
current_segment="100"
}
while [[ -z "${(P)list_var_current}" && "$current_idx" -gt 1 ]]; do
current_idx=current_idx-1
done
-zui_listin_compute_first_to_show_idx_up_many
else
# Establish exact right-edge segment
if [[ "$current_segment" -gt "$nseg" ]]; then
(( current_segment = nseg ))
[[ "$current_segment" -le 0 ]] && current_segment=1
fi
if [[ "$key" = "BTAB" ]]; then
if ! -zui_listin_set_next_hyperlink 0; then
# No link to the left in current line - move
# one line up, set segment to far-most, the
# verification at bottom will find the nearest
# hyperlink
[[ "$current_idx" -gt "1" ]] && {
current_idx=current_idx-1
current_segment="100"
search_for_hyperlink=1
}
while [[ -z "${(P)list_var_current}" && "$current_idx" -gt 1 ]]; do
current_idx=current_idx-1
done
-zui_listin_compute_first_to_show_idx_up_many
fi
else
if [[ "${ZUI[text_mode]}" = "off" ]]; then
# Search left accepting lack of success
-zui_listin_set_next_hyperlink 0
elif -zui_std_has_any_hyperlinks "${(P)list_var_current}"; then
if [[ "${ZUI[text_mode]}" = (hyp|all) ]]; then
(( -- current_segment ))
else
-zui_listin_set_next_hyperlink 0
fi
else
if [[ "${ZUI[text_mode]}" = (nohyp|all) ]]; then
(( -- current_segment ))
else
-zui_listin_set_next_hyperlink 0
fi
fi
fi
fi
;;
(RIGHT|$'\t')
if [[ "$key" = $'\t' && ${ZUILIST_NONSELECTABLE_ELEMENTS[(r)$current_idx]} = $current_idx ]]; then
[[ "$current_idx" -lt "$last_element" ]] && {
current_idx=current_idx+1
current_segment=1
}
while [[ -z "${(P)list_var_current}" && "$current_idx" -lt "$last_element" ]]; do
current_idx=current_idx+1
done
-zui_listin_compute_first_to_show_idx_down_many
else
if [[ "$key" = $'\t' ]]; then
if ! -zui_listin_set_next_hyperlink 1; then
# Move to next line, set current_segment
# to 1, verification at bottom will find
# nearest hyperlink if it exists in the
# next line
[[ "$current_idx" -lt "$last_element" ]] && {
current_idx=current_idx+1
current_segment="1"
search_for_hyperlink=1
}
while [[ -z "${(P)list_var_current}" && "$current_idx" -lt "$last_element" ]]; do
current_idx=current_idx+1
done
-zui_listin_compute_first_to_show_idx_down_many
fi
else
if [[ "${ZUI[text_mode]}" = "off" ]]; then
# Search right accepting lack of success
-zui_listin_set_next_hyperlink 1
elif -zui_std_has_any_hyperlinks "${(P)list_var_current}"; then
if [[ "${ZUI[text_mode]}" = (hyp|all) ]]; then
(( ++ current_segment ))
else
-zui_listin_set_next_hyperlink 1
fi
else
if [[ "${ZUI[text_mode]}" = (nohyp|all) ]]; then
(( ++ current_segment ))
else
-zui_listin_set_next_hyperlink 1
fi
fi
fi
[[ "$current_segment" -gt "$nseg" ]] && current_segment="$nseg"
fi
;;
(F1|F2|F3|F4)
reply=( -1 "$key" )
;;
(F4|F5|F6|F7|F8|F9|F10)
# ignore
;;
#
# The input
#
($'\b'|$'\C-?'|BACKSPACE)
buffer="${buffer%?}"
;;
($'\C-W')
[[ "$buffer" = "${buffer% *}" ]] && buffer="" || buffer="${buffer% *}"
;;
($'\C-K')
buffer=""
;;
($'\E')
buffer=""
search=0
-zui_list_cursor_visibility "status" 0
;;
($'\C-O')
uniq_mode=1-uniq_mode
;;
(*)
if [[ $#key == 1 && $((#key)) -lt 31 ]]; then
# ignore all other control keys
else
buffer+="$key"
fi
;;
esac
fi
# Make current_segment point to hyperlink, if it exists in line
if -zui_std_has_any_hyperlinks "${(P)list_var_current}"; then
[[ "${ZUI[text_mode]}" = (hyp|all) && "$search_for_hyperlink" = "0" ]] || -zui_listin_search_for_hyperlink_graphical
else
[[ "${ZUI[text_mode]}" = (nohyp|all) && "$search_for_hyperlink" = "0" ]] || -zui_listin_search_for_hyperlink_graphical
fi
reply[3]="$current_idx"
reply[4]="$current_segment"
reply[5]="$from_what_idx_list_is_shown"
reply[6]="$hscroll"
reply[7]="$search"
reply[8]="$buffer"
reply[9]="$uniq_mode"
# vim: set filetype=zsh: