grace/dots

dotfiles/nix configs

git clone https://git.gae.moe/grace/dots

Grace Yoderi have got to be better about these things9c3d491

main
14.0 KiB431 linesraw
1# FIXME this can't be called in sequence in general case,
2# because of unsynchronized `commandline -f` and `commandline -C`
3
4function fish_helix_command
5    argparse h/help -- $argv
6    or return 1
7    if test -n "$_flag_help"
8        echo "Helper function to handle modal key bindings mostly outside of insert mode"
9        return
10    end
11
12    # TODO only single command allowed really yet,
13    #     because `commandline -f` queues actions, while `commandline -C` is immediate
14    for command in $argv
15        set -f count (fish_bind_count -r)
16        set -f count_defined $status
17
18        switch $command
19            case {move,extend}_char_left
20                commandline -C (math max\(0, (commandline -C) - $count\))
21                __fish_helix_extend_by_command $command
22            case {move,extend}_char_right
23                commandline -C (math (commandline -C) + $count)
24                __fish_helix_extend_by_command $command
25
26            case char_up
27                __fish_helix_char_up $fish_bind_mode $count
28            case char_down
29                __fish_helix_char_down $fish_bind_mode $count
30
31            case next_word_start
32                # https://regex101.com/r/KXrl1x/1
33                set -l regex (string join '' \
34                '(?:.?\\n+|' \
35                '[[:alnum:]_](?=[^[:alnum:]_\\s])|' \
36                '[^[:alnum:]_\\s](?=[[:alnum:]_])|' \
37                '[^\\S\\n](?=[\\S\\n])|)' \
38                '((?:[[:alnum:]_]+|[^[:alnum:]_\\s]+|)[^\\S\\n]*)' \
39                )
40                __fish_helix_next_word $fish_bind_mode $count $regex
41
42            case next_long_word_start
43                set -l regex (string join '' \
44                '(?:.?\\n+|' \
45                '[^\\S\\n](?=[\\S\\n])|)' \
46                '(\\S*[^\\S\\n]*)' \
47                )
48                __fish_helix_next_word $fish_bind_mode $count $regex
49
50            case next_word_end
51                # https://regex101.com/r/Gl0KP2/1
52                set -l regex ' (?:
53                .?\\n+ |
54                [[:alnum:]_](?=[^[:alnum:]_]) |
55                [^[:alnum:]_\\s](?=[[:alnum:]_\\s]) | )
56                ( [^\\S\\n]*
57                (?: [[:alnum:]_]+ | [^[:alnum:]_\\s]+ | ) ) '
58                __fish_helix_next_word $fish_bind_mode $count $regex
59
60            case next_long_word_end
61                set -l regex ' (?: .?\\n+ | \\S(?=\\s) | )
62            ( [^\\S\\n]* \\S* ) '
63                __fish_helix_next_word $fish_bind_mode $count $regex
64
65            case prev_word_start
66                set -l regex ' ( (?:
67                [[:alnum:]_]+ |
68                [^[:alnum:]_\\s]+ | )
69                [^\\S\\n]* )
70                (?: \\n+.? |
71                (?<=[^[:alnum:]_])[[:alnum:]_] |
72                (?<=[[:alnum:]_\\s])[^[:alnum:]_\\s] | ) '
73                __fish_helix_prev_word $fish_bind_mode $count $regex
74
75            case prev_long_word_start
76                set -l regex '
77                ( \\S* [^\\S\\n]* )
78                (?: \\n+.? | (?<=\\s)\\S | ) '
79                __fish_helix_prev_word $fish_bind_mode $count $regex
80
81            case till_next_char
82                __fish_helix_find_char $fish_bind_mode $count forward-jump-till forward-char
83            case find_next_char
84                __fish_helix_find_char $fish_bind_mode $count forward-jump
85            case till_prev_char
86                __fish_helix_find_char $fish_bind_mode $count backward-jump-till backward-char
87            case find_prev_char
88                __fish_helix_find_char $fish_bind_mode $count backward-jump
89
90            case till_next_cr
91                __fish_helix_find_next_cr $fish_bind_mode $count 2
92            case find_next_cr
93                __fish_helix_find_next_cr $fish_bind_mode $count 1
94            case till_prev_cr
95                __fish_helix_find_prev_cr $fish_bind_mode $count 1
96            case find_prev_cr
97                __fish_helix_find_prev_cr $fish_bind_mode $count 0
98
99            case goto_line_start
100                commandline -f beginning-of-line
101                __fish_helix_extend_by_mode
102            case goto_line_end
103                __fish_helix_goto_line_end
104                __fish_helix_extend_by_mode
105            case goto_first_nonwhitespace
106                __fish_helix_goto_first_nonwhitespace
107                __fish_helix_extend_by_mode
108
109            case goto_file_start
110                __fish_helix_goto_line $count
111            case goto_line
112                if test "$count_defined" = 0 # if true
113                    __fish_helix_goto_line $count
114                end
115            case goto_last_line
116                commandline -f end-of-buffer beginning-of-line
117                __fish_helix_extend_by_mode
118
119            case insert_mode
120                commandline -C (commandline -B || commandline -C)
121                set fish_bind_mode insert
122                commandline -f end-selection repaint-mode
123
124            case append_mode
125                commandline -C (commandline -E || commandline -C)
126                set fish_bind_mode insert
127                commandline -f end-selection repaint-mode
128
129            case prepend_to_line
130                __fish_helix_goto_first_nonwhitespace
131                set fish_bind_mode insert
132                commandline -f end-selection repaint-mode
133
134            case append_to_line
135                set fish_bind_mode insert
136                commandline -f end-selection end-of-line repaint-mode
137
138            case delete_selection
139                commandline -f kill-selection begin-selection
140            case delete_selection_noyank
141                __fish_helix_delete_selection
142
143            case yank
144                __fish_helix_yank
145            case paste_before
146                __fish_helix_paste_before "commandline -f yank"
147            case paste_after
148                __fish_helix_paste_after "commandline -f yank"
149            case replace_selection
150                __fish_helix_replace_selection "$fish_killring[1]" true
151
152            case paste_before_clip
153                __fish_helix_paste_before fish_clipboard_paste
154            case paste_after_clip
155                __fish_helix_paste_after fish_clipboard_paste --clip
156            case replace_selection_clip
157                __fish_helix_replace_selection "" fish_clipboard_paste --clip
158
159            case select_all
160                commandline -f beginning-of-buffer begin-selection end-of-buffer end-of-line backward-char
161
162            case '*'
163                echo "[fish-helix]" Unknown command $command >&2
164        end
165    end
166end
167
168function __fish_helix_extend_by_command -a piece
169    if not string match -qr extend_ $piece
170        commandline -f begin-selection
171    end
172end
173
174function __fish_helix_extend_by_mode
175    if test $fish_bind_mode = default
176        commandline -f begin-selection
177    end
178end
179
180function __fish_helix_find_char -a mode count fish_cmdline till
181    # FIXME don't reset selection if N/A
182    if test $mode = default
183        commandline -f begin-selection
184    end
185    commandline -f $till $fish_cmdline
186    for i in (seq 2 $count)
187        commandline -f $till repeat-jump
188    end
189end
190
191function __fish_helix_find_next_cr -a mode count skip
192    set -l cursor (commandline -C)
193    commandline | # Include endling newline intentionally
194        # Skip until cursor:
195        sed -z 's/^.\{'(math $cursor + $skip)'\}\(.*\)$/\\1/' |
196        # Count characters up to the target newline:
197        sed -z 's/^\(\([^\\n]*\\n\)\{0,'$count'\}\).*/\\1/' |
198        read -zl chars
199
200    if test $mode = default -a -n "$chars"
201        commandline -f begin-selection
202    end
203    for i in (seq 1 (string length -- "$chars"))
204        commandline -f forward-char
205    end
206end
207
208function __fish_helix_find_prev_cr -a mode count skip
209    set -l cursor (commandline -C)
210    commandline --cut-at-cursor |
211        sed -z 's/.\{'$skip'\}\n$//' |
212        read -zl buffer
213
214    echo -n $buffer |
215        # Drop characters up to the target newline:
216        sed -z 's/\(\(\\n[^\\n]*\)\{0,'$count'\}\)$//' |
217        read -zl chars
218    set -l n_chars (math (string length -- "$buffer") - (string length -- "$chars"))
219
220    if test $mode = default -a $n_chars != 0
221        commandline -f begin-selection
222    end
223    for i in (seq 1 $n_chars)
224        commandline -f backward-char
225    end
226end
227
228function __fish_helix_goto_line_end
229    # check if we are on an empty line first
230    commandline | sed -n (commandline -L)'!b;/^$/q;q5' && return
231    commandline -f end-of-line backward-char
232end
233
234function __fish_helix_goto_first_nonwhitespace
235    # check if we are on whitespace line first
236    commandline | sed -n (commandline -L)'!b;/^\\s*$/q;q5' && return
237    commandline -f beginning-of-line forward-bigword backward-bigword
238end
239
240function __fish_helix_goto_line -a number
241    set -l lines (math min\($number, (commandline | wc -l)\))
242    commandline -f beginning-of-buffer
243    for i in (seq 2 $lines)
244        commandline -f down-line
245    end
246    __fish_helix_extend_by_mode
247end
248
249function __fish_helix_char_up -a mode count
250    if commandline --paging-mode && not commandline --search-mode
251        for i in (seq 1 $count)
252            commandline -f up-line
253        end
254        return
255    end
256    set -l line (commandline -L)
257    if commandline --search-mode || test $line = 1
258        for i in (seq 1 (math min \($count, (count $history)\)))
259            commandline -f history-search-backward
260        end
261        return
262    end
263    set -l count (math min\($count, $line-1\))
264    for i in (seq 1 $count)
265        commandline -f up-line
266    end
267    __fish_helix_extend_by_mode
268end
269
270function __fish_helix_char_down -a mode count
271    if commandline --paging-mode && not commandline --search-mode
272        for i in (seq 1 $count)
273            commandline -f down-line
274        end
275        return
276    end
277    set -l line (commandline -L)
278    set -l total (count (commandline))
279    if commandline --search-mode || test $line = $total
280        for i in (seq 1 (math min \($count, (count $history)\)))
281            commandline -f history-search-forward
282        end
283        return
284    end
285    set -l count (math min\($count, $total - $line\))
286    for i in (seq 1 $count)
287        commandline -f down-line
288    end
289    __fish_helix_extend_by_mode
290end
291
292function __fish_helix_next_word -a mode count regex
293    set -f cursor (commandline -C)
294    commandline |
295        perl -e '
296        use open qw(:std :utf8);
297        do { local $/; substr <>, '$cursor' } =~ m/(?:'$regex'){0,'$count'}/ux;
298        print $-[1], " ", $+[1];' |
299        read -f left right
300    test "$left" = "$right" && return
301    if test $mode = default
302        commandline -C (math $cursor + $left)
303        commandline -f begin-selection
304        for i in (seq $left (math $right - 2))
305            commandline -f forward-char
306        end
307    else
308        commandline -C (math $cursor + $right - 1)
309    end
310end
311
312function __fish_helix_prev_word -a mode count regex
313    set -f left (math (commandline -C) + 1)
314    set -f updated 0
315    for i in (seq 1 $count)
316        commandline |
317            perl -e '
318            use open qw(:std :utf8);
319            do { local $/; substr <>, 0, '$left' } =~ /(?:'$regex')$/ux;
320            print $-[1], " ", $+[1];' |
321            read -l l r
322        test "$l" = "$r" -o "$l" = 0 -a "$r" = 1 && break
323        set -f left $l
324        set -f right $r
325        set -f updated 1
326    end
327    test $updated -eq 0; and return
328    if test $mode = default
329        commandline -C (math $right - 1)
330        commandline -f begin-selection
331        for i in (seq $left (math $right - 2))
332            commandline -f backward-char
333        end
334    else
335        commandline -C (math $left)
336    end
337end
338
339function __fish_helix_delete_selection
340    set start (commandline -B)
341    set end (commandline -E)
342    commandline |
343        sed -zE 's/^(.{'$start'})(.{0,'(math $end - $start)'})(.*)\\n$/\\1\\3/' |
344        read -l result
345
346    commandline "$result"
347    commandline -C $start
348    commandline -f begin-selection
349end
350
351function __fish_helix_yank
352    set -l end (commandline -E)
353    set -l cursor (commandline -C)
354    commandline -f kill-selection yank backward-char
355
356    for i in (seq $cursor (math $end - 2))
357        commandline -f backward-char
358    end
359end
360
361function __fish_helix_paste_before -a cmd_paste
362    set -l cmd_paste $(string split " " $cmd_paste)
363    set -l cursor (commandline -C)
364    set -l start (commandline -B || commandline -C)
365    set -l end (commandline -E)
366    commandline -C $start
367    $cmd_paste
368    commandline -f begin-selection
369    for i in (seq $start (math $end - 2))
370        commandline -f forward-char
371    end
372    if test $cursor = $start
373        commandline -f swap-selection-start-stop
374    end
375end
376
377function __fish_helix_paste_after -a cmd_paste
378    set -l cmd_paste $(string split " " $cmd_paste)
379    set -l cursor (commandline -C)
380    set -l start (commandline -B || commandline -C)
381    set -l end (commandline -E || commandline -C)
382    commandline -C $end
383    $cmd_paste
384
385    if test "$argv[2]" = --clip
386        commandline -C (math max (math $end - 1) , 0)
387    else
388        for i in (seq 0 (string length "$fish_killring[1]"))
389            commandline -f backward-char
390        end
391    end
392    commandline -f begin-selection
393    for i in (seq $start (math $end - 2))
394        commandline -f backward-char
395    end
396    if test $cursor != $start
397        commandline -f swap-selection-start-stop
398    end
399end
400
401function __fish_helix_replace_selection -a replacement cmd_paste
402    set -l cmd_paste $(string split " " $cmd_paste)
403    set cursor (commandline -C)
404    set start (commandline -B)
405    set end (commandline -E)
406    commandline |
407        sed -zE 's/^(.{'$start'})(.{0,'(math $end - $start)'})(.*)\\n$/\\1'"$(string escape --style=regex "$replacement")"'\\3/' |
408        read -l result
409
410    commandline "$result"
411    commandline -C $start
412    $cmd_paste
413
414    if test "$argv[3]" = --clip
415        commandline -f backward-char begin-selection
416        for i in (seq (math $start + 2) (commandline -C))
417            commandline -f backward-char
418        end
419        if test $cursor != $start
420            commandline -f swap-selection-start-stop
421        end
422    else
423        commandline -f begin-selection
424        for i in (seq 2 (string length "$replacement"))
425            commandline -f forward-char
426        end
427        if test $cursor = $start
428            commandline -f swap-selection-start-stop
429        end
430    end
431end