1 # Copy this file into /usr/share/zsh/site-functions/
2 # and add 'autoload n-list-input` to .zshrc
4 # This is an internal function not for direct use
12 # Compute first to show index
13 _nlist_compute_first_to_show_idx() {
14 from_what_idx_list_is_shown=0+((current_idx-1)/page_height)*page_height+1
17 _nlist_update_from_keywords() {
19 if [ "$nkeywords" -gt 0 ]; then
20 curkeyword=$(( (curkeyword+1) % (nkeywords+1) ))
21 if [ "$curkeyword" -eq "0" ]; then
24 buffer="${keywords[curkeyword]}"
29 _nlist_iterate_theme() {
31 if [ "$1" = "1" ]; then
32 curtheme=$(( (curtheme+1) % (nthemes+1) ))
35 [ "$curtheme" -lt 0 ] && curtheme=nthemes
38 if [ "$nthemes" -gt 0 ]; then
39 local theme=${themes[curtheme]}
40 [ "$curtheme" -eq "0" ] && theme="$backuptheme"
42 colorpair="${theme%/*}"
44 background="${colorpair#*/}"
45 zcurses bg main "$colorpair"
46 zcurses bg inner "$colorpair"
50 _nlist_rotate_buffer() {
51 setopt localoptions noglob
54 words=( ${(s: :)buffer} )
55 words=( ${words[-1]} ${words[1,-2]} )
58 [ "${buffer[-1]}" = " " ] && space=" "
60 buffer="${(j: :)words}$space"
65 integer current_idx="$1"
66 integer from_what_idx_list_is_shown="$2"
67 integer page_height="$3"
68 integer page_width="$4"
69 integer last_element="$5"
74 integer uniq_mode="$10"
81 if [ "$search" = "0" ]; then
85 # Are there any elements before the current one?
86 [ "$current_idx" -gt 1 ] && current_idx=current_idx-1;
87 _nlist_compute_first_to_show_idx
90 # Are there any elements after the current one?
91 [ "$current_idx" -lt "$last_element" ] && current_idx=current_idx+1;
92 _nlist_compute_first_to_show_idx
94 (PPAGE|$'\b'|$'\C-?'|BACKSPACE)
95 current_idx=current_idx-page_height
96 [ "$current_idx" -lt 1 ] && current_idx=1;
97 _nlist_compute_first_to_show_idx
100 current_idx=current_idx+page_height
101 [ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
102 _nlist_compute_first_to_show_idx
105 current_idx=current_idx-page_height/2
106 [ "$current_idx" -lt 1 ] && current_idx=1;
107 _nlist_compute_first_to_show_idx
110 current_idx=current_idx+page_height/2
111 [ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
112 _nlist_compute_first_to_show_idx
116 _nlist_compute_first_to_show_idx
119 current_idx=last_element
120 _nlist_compute_first_to_show_idx
123 # Is that element selectable?
124 # Check for this only when there is no search
125 if [[ "$NLIST_SEARCH_BUFFER" != "" || "$NLIST_IS_UNIQ_MODE" -eq 1 ||
126 ${NLIST_NONSELECTABLE_ELEMENTS[(r)$current_idx]} != $current_idx ]]
128 # Save current element in the result variable
129 reply=( $current_idx "SELECT" )
133 # This event needs to be enabled
134 if [[ "${NLIST_ENABLED_EVENTS[(r)HELP]}" = "HELP" ]]; then
139 # This event needs to be enabled
140 if [[ "${NLIST_ENABLED_EVENTS[(r)F1]}" = "F1" ]]; then
144 (F4|F5|F6|F7|F8|F9|F10|DC)
145 # ignore; F2, F3 are used below
152 _nlist_cursor_visibility 1
155 reply=( $current_idx "LEAVE" )
158 reply=( -1 "REDRAW" )
161 [[ "${(t)NLIST_HOP_INDEXES}" = "array" || "${(t)NLIST_HOP_INDEXES}" = "array-local" ]] &&
162 [ -z "$NLIST_SEARCH_BUFFER" ] && [ "$NLIST_IS_UNIQ_MODE" -eq 0 ] &&
163 for idx in "${(n)NLIST_HOP_INDEXES[@]}"; do
164 if [ "$idx" -gt "$current_idx" ]; then
166 _nlist_compute_first_to_show_idx
172 [[ "${(t)NLIST_HOP_INDEXES}" = "array" || "${(t)NLIST_HOP_INDEXES}" = "array-local" ]] &&
173 [ -z "$NLIST_SEARCH_BUFFER" ] && [ "$NLIST_IS_UNIQ_MODE" -eq 0 ] &&
174 for idx in "${(nO)NLIST_HOP_INDEXES[@]}"; do
175 if [ "$idx" -lt "$current_idx" ]; then
177 _nlist_compute_first_to_show_idx
184 [ "$hscroll" -lt 0 ] && hscroll=0
193 if [ "$search" = "1" ]; then
195 _nlist_cursor_visibility 0
198 _nlist_cursor_visibility 1
202 uniq_mode=1-uniq_mode
205 (( f_mode=(f_mode+1) % 3 ))
209 _nlist_cursor_visibility 1
210 _nlist_update_from_keywords
213 _nlist_iterate_theme 1
216 _nlist_iterate_theme 0
219 # This event needs to be enabled
220 if [[ "${NLIST_ENABLED_EVENTS[(r)EDIT]}" = "EDIT" ]]; then
235 if [ "$NLIST_INSTANT_SELECT" = "1" ]; then
236 if [[ "$NLIST_SEARCH_BUFFER" != "" || "$NLIST_IS_UNIQ_MODE" -eq 1 ||
237 ${NLIST_NONSELECTABLE_ELEMENTS[(r)$current_idx]} != $current_idx ]]
239 reply=( $current_idx "SELECT" )
243 _nlist_cursor_visibility 0
247 reply=( -1 "REDRAW" )
251 # Slightly limited navigation
255 [ "$current_idx" -gt 1 ] && current_idx=current_idx-1;
256 _nlist_compute_first_to_show_idx
259 [ "$current_idx" -lt "$last_element" ] && current_idx=current_idx+1;
260 _nlist_compute_first_to_show_idx
263 current_idx=current_idx-page_height
264 [ "$current_idx" -lt 1 ] && current_idx=1;
265 _nlist_compute_first_to_show_idx
268 current_idx=current_idx+page_height
269 [ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
270 _nlist_compute_first_to_show_idx
273 current_idx=current_idx-page_height/2
274 [ "$current_idx" -lt 1 ] && current_idx=1;
275 _nlist_compute_first_to_show_idx
278 current_idx=current_idx+page_height/2
279 [ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
280 _nlist_compute_first_to_show_idx
284 _nlist_compute_first_to_show_idx
287 current_idx=last_element
288 _nlist_compute_first_to_show_idx
292 [ "$hscroll" -lt 0 ] && hscroll=0
298 # This event needs to be enabled
299 if [[ "${NLIST_ENABLED_EVENTS[(r)F1]}" = "F1" ]]; then
303 (F4|F5|F6|F7|F8|F9|F10|DC)
304 # ignore; F2, F3 are used below
311 ($'\b'|$'\C-?'|BACKSPACE)
315 [ "$buffer" = "${buffer% *}" ] && buffer="" || buffer="${buffer% *}"
323 _nlist_cursor_visibility 0
326 if [ "$search" = "1" ]; then
328 _nlist_cursor_visibility 0
331 _nlist_cursor_visibility 1
335 uniq_mode=1-uniq_mode
338 (( f_mode=(f_mode+1) % 3 ))
341 _nlist_update_from_keywords
344 _nlist_iterate_theme 1
347 _nlist_iterate_theme 0
350 # This event needs to be enabled
351 if [[ "${NLIST_ENABLED_EVENTS[(r)EDIT]}" = "EDIT" ]]; then
359 if [[ $#key == 1 && $((#key)) -lt 31 ]]; then
360 # ignore all other control keys
369 reply[3]="$current_idx"
370 reply[4]="$from_what_idx_list_is_shown"
374 reply[8]="$uniq_mode"
377 # vim: set filetype=zsh: