summaryrefslogtreecommitdiff
path: root/plugins/zsh-navigation-tools/n-list-input
blob: 9bafc51b6bfcad5228d03d0ff924acdf517496c8 (plain)
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
# Copy this file into /usr/share/zsh/site-functions/
# and add 'autoload n-list-input` to .zshrc
#
# This is an internal function not for direct use

emulate -L zsh

zmodload zsh/curses

setopt typesetsilent

# Compute first to show index
_nlist_compute_first_to_show_idx() {
    from_what_idx_list_is_shown=0+((current_idx-1)/page_height)*page_height+1
}

_nlist_update_from_keywords() {
    keywordisfresh="1"
    if [ "$nkeywords" -gt 0 ]; then
        curkeyword=$(( (curkeyword+1) % (nkeywords+1) ))
        if [ "$curkeyword" -eq "0" ]; then
            buffer=""
        else
            buffer="${keywords[curkeyword]}"
        fi
    fi
}

_nlist_iterate_theme() {
    themeisfresh="1"
    if [ "$1" = "1" ]; then
        curtheme=$(( (curtheme+1) % (nthemes+1) ))
    else
        curtheme=curtheme-1
        [ "$curtheme" -lt 0 ] && curtheme=nthemes
    fi

    if [ "$nthemes" -gt 0 ]; then
        local theme=${themes[curtheme]}
        [ "$curtheme" -eq "0" ] && theme="$backuptheme"

        colorpair="${theme%/*}"
        bold="${theme##*/}"
        background="${colorpair#*/}"
        zcurses bg main "$colorpair"
        zcurses bg inner "$colorpair"
    fi
}

_nlist_rotate_buffer() {
    setopt localoptions noglob

    local -a words
    words=( ${(s: :)buffer} )
    words=( ${words[-1]} ${words[1,-2]} )

    local space=""
    [ "${buffer[-1]}" = " " ] && space=" "

    buffer="${(j: :)words}$space"
}

typeset -ga reply
reply=( -1 '' )
integer current_idx="$1"
integer from_what_idx_list_is_shown="$2"
integer page_height="$3"
integer page_width="$4"
integer last_element="$5"
integer hscroll="$6"
local key="$7"
integer search="$8"
local buffer="$9"
integer uniq_mode="$10"
integer f_mode="$11"

#
# Listening for input
#

if [ "$search" = "0" ]; then

case "$key" in
    (UP|k|$'\C-P')
        # Are there any elements before the current one?
        [ "$current_idx" -gt 1 ] && current_idx=current_idx-1;
        _nlist_compute_first_to_show_idx
        ;;
    (DOWN|j|$'\C-N')
        # Are there any elements after the current one?
        [ "$current_idx" -lt "$last_element" ] && current_idx=current_idx+1;
        _nlist_compute_first_to_show_idx
        ;;
    (PPAGE|$'\b'|$'\C-?'|BACKSPACE)
        current_idx=current_idx-page_height
        [ "$current_idx" -lt 1 ] && current_idx=1;
        _nlist_compute_first_to_show_idx
        ;;
    (NPAGE|" ")
        current_idx=current_idx+page_height
        [ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
        _nlist_compute_first_to_show_idx
        ;;
    ($'\C-U')
        current_idx=current_idx-page_height/2
        [ "$current_idx" -lt 1 ] && current_idx=1;
        _nlist_compute_first_to_show_idx
        ;;
    ($'\C-D')
        current_idx=current_idx+page_height/2
        [ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
        _nlist_compute_first_to_show_idx
        ;;
    (HOME|g)
        current_idx=1
        _nlist_compute_first_to_show_idx
        ;;
    (END|G)
        current_idx=last_element
        _nlist_compute_first_to_show_idx
        ;;
    ($'\n'|ENTER)
        # Is that element selectable?
        # Check for this only when there is no search
        if [[ "$NLIST_SEARCH_BUFFER" != "" || "$NLIST_IS_UNIQ_MODE" -eq 1 ||
            ${NLIST_NONSELECTABLE_ELEMENTS[(r)$current_idx]} != $current_idx ]]
        then
            # Save current element in the result variable
            reply=( $current_idx "SELECT" )
        fi
        ;;
    (H|'?')
        # This event needs to be enabled
        if [[ "${NLIST_ENABLED_EVENTS[(r)HELP]}" = "HELP" ]]; then
            reply=( -1 "HELP" )
        fi
        ;;
    (F1)
        # This event needs to be enabled
        if [[ "${NLIST_ENABLED_EVENTS[(r)F1]}" = "F1" ]]; then
            reply=( -1 "$key" )
        fi
        ;;
    (F4|F5|F6|F7|F8|F9|F10|DC)
        # ignore; F2, F3 are used below
        ;;
    (q)
        reply=( -1 "QUIT" )
        ;;
    (/)
        search=1
        _nlist_cursor_visibility 1
        ;;
    ($'\t')
        reply=( $current_idx "LEAVE" )
        ;;
    ($'\C-L')
        reply=( -1 "REDRAW" )
        ;;
    (\])
        [[ "${(t)NLIST_HOP_INDEXES}" = "array" || "${(t)NLIST_HOP_INDEXES}" = "array-local" ]] &&
        [ -z "$NLIST_SEARCH_BUFFER" ] && [ "$NLIST_IS_UNIQ_MODE" -eq 0 ] &&
        for idx in "${(n)NLIST_HOP_INDEXES[@]}"; do
            if [ "$idx" -gt "$current_idx" ]; then
                current_idx=$idx
                _nlist_compute_first_to_show_idx
                break
            fi
        done
        ;;
    (\[)
        [[ "${(t)NLIST_HOP_INDEXES}" = "array" || "${(t)NLIST_HOP_INDEXES}" = "array-local" ]] &&
        [ -z "$NLIST_SEARCH_BUFFER" ] && [ "$NLIST_IS_UNIQ_MODE" -eq 0 ] &&
        for idx in "${(nO)NLIST_HOP_INDEXES[@]}"; do
            if [ "$idx" -lt "$current_idx" ]; then
                current_idx=$idx
                _nlist_compute_first_to_show_idx
                break
            fi
        done
        ;;
    ('<'|'{'|LEFT|'h')
        hscroll=hscroll-7
        [ "$hscroll" -lt 0 ] && hscroll=0
        ;;
    ('>'|'}'|RIGHT|'l')
        hscroll+=7
        ;;
    ($'\E')
        buffer=""
        ;;
    (F3)
        if [ "$search" = "1" ]; then
            search=0
            _nlist_cursor_visibility 0
        else
            search=1
            _nlist_cursor_visibility 1
        fi
        ;;
    (o|$'\C-O')
        uniq_mode=1-uniq_mode
        ;;
    (f|$'\C-F')
        (( f_mode=(f_mode+1) % 3 ))
        ;;
    ($'\x1F'|F2|$'\C-X')
        search=1
        _nlist_cursor_visibility 1
        _nlist_update_from_keywords
        ;;
    ($'\C-T')
        _nlist_iterate_theme 1
        ;;
    ($'\C-G')
        _nlist_iterate_theme 0
        ;;
    ($'\C-E'|e)
        # This event needs to be enabled
        if [[ "${NLIST_ENABLED_EVENTS[(r)EDIT]}" = "EDIT" ]]; then
            reply=( -1 "EDIT" )
        fi
        ;;
    ($'\C-A')
        _nlist_rotate_buffer
        ;;
    (*)
        ;;
esac

else

case "$key" in
    ($'\n'|ENTER)
        if [ "$NLIST_INSTANT_SELECT" = "1" ]; then
            if [[ "$NLIST_SEARCH_BUFFER" != "" || "$NLIST_IS_UNIQ_MODE" -eq 1 ||
                ${NLIST_NONSELECTABLE_ELEMENTS[(r)$current_idx]} != $current_idx ]]
            then
                reply=( $current_idx "SELECT" )
            fi
        else
            search=0
            _nlist_cursor_visibility 0
        fi
        ;;
    ($'\C-L')
        reply=( -1 "REDRAW" )
        ;;

    #
    # Slightly limited navigation
    #

    (UP|$'\C-P')
        [ "$current_idx" -gt 1 ] && current_idx=current_idx-1;
        _nlist_compute_first_to_show_idx
        ;;
    (DOWN|$'\C-N')
        [ "$current_idx" -lt "$last_element" ] && current_idx=current_idx+1;
        _nlist_compute_first_to_show_idx
        ;;
    (PPAGE)
        current_idx=current_idx-page_height
        [ "$current_idx" -lt 1 ] && current_idx=1;
        _nlist_compute_first_to_show_idx
        ;;
    (NPAGE)
        current_idx=current_idx+page_height
        [ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
        _nlist_compute_first_to_show_idx
        ;;
    ($'\C-U')
        current_idx=current_idx-page_height/2
        [ "$current_idx" -lt 1 ] && current_idx=1;
        _nlist_compute_first_to_show_idx
        ;;
    ($'\C-D')
        current_idx=current_idx+page_height/2
        [ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
        _nlist_compute_first_to_show_idx
        ;;
    (HOME)
        current_idx=1
        _nlist_compute_first_to_show_idx
        ;;
    (END)
        current_idx=last_element
        _nlist_compute_first_to_show_idx
        ;;
    (LEFT)
        hscroll=hscroll-7
        [ "$hscroll" -lt 0 ] && hscroll=0
        ;;
    (RIGHT)
        hscroll+=7
        ;;
    (F1)
        # This event needs to be enabled
        if [[ "${NLIST_ENABLED_EVENTS[(r)F1]}" = "F1" ]]; then
            reply=( -1 "$key" )
        fi
        ;;
    (F4|F5|F6|F7|F8|F9|F10|DC)
        # ignore; F2, F3 are used below
        ;;

    #
    # The input
    #

    ($'\b'|$'\C-?'|BACKSPACE)
        buffer="${buffer%?}"
        ;;
    ($'\C-W')
        [ "$buffer" = "${buffer% *}" ] && buffer="" || buffer="${buffer% *}"
        ;;
    ($'\C-K')
        buffer=""
        ;;
    ($'\E')
        buffer=""
        search=0
        _nlist_cursor_visibility 0
        ;;
    (F3)
        if [ "$search" = "1" ]; then
            search=0
            _nlist_cursor_visibility 0
        else
            search=1
            _nlist_cursor_visibility 1
        fi
        ;;
    ($'\C-O')
        uniq_mode=1-uniq_mode
        ;;
    ($'\C-F')
        (( f_mode=(f_mode+1) % 3 ))
        ;;
    ($'\x1F'|F2|$'\C-X')
        _nlist_update_from_keywords
        ;;
    ($'\C-T')
        _nlist_iterate_theme 1
        ;;
    ($'\C-G')
        _nlist_iterate_theme 0
        ;;
    ($'\C-E')
        # This event needs to be enabled
        if [[ "${NLIST_ENABLED_EVENTS[(r)EDIT]}" = "EDIT" ]]; then
            reply=( -1 "EDIT" )
        fi
        ;;
    ($'\C-A')
        _nlist_rotate_buffer
        ;;
    (*)
        if [[ $#key == 1 && $((#key)) -lt 31 ]]; then
            # ignore all other control keys
        else
            buffer+="$key"
        fi
        ;;
esac

fi

reply[3]="$current_idx"
reply[4]="$from_what_idx_list_is_shown"
reply[5]="$hscroll"
reply[6]="$search"
reply[7]="$buffer"
reply[8]="$uniq_mode"
reply[9]="$f_mode"

# vim: set filetype=zsh: