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