diff --git a/conf.d/_minimal_conf.fish b/conf.d/_minimal_conf.fish new file mode 100644 index 0000000..b71bb7b --- /dev/null +++ b/conf.d/_minimal_conf.fish @@ -0,0 +1 @@ +set -e fish_greeting diff --git a/functions/_pisces_append.fish b/functions/_pisces_append.fish deleted file mode 100644 index 60e002f..0000000 --- a/functions/_pisces_append.fish +++ /dev/null @@ -1,5 +0,0 @@ -function _pisces_append -a text -d "Inserts a pair of strings (left-right) and puts the cursor between them" - - commandline --insert -- $text - and _pisces_jump -(string length -- $text) -end diff --git a/functions/_pisces_bind_pair.fish b/functions/_pisces_bind_pair.fish index 093a1ab..126fbde 100644 --- a/functions/_pisces_bind_pair.fish +++ b/functions/_pisces_bind_pair.fish @@ -3,15 +3,15 @@ function _pisces_bind_pair -a left right -d "Creates bindings for the given pair set l $left set r $right - set left (string escape -n $left) - set right (string escape -n $right) + set left (string escape -n -- $left) + set right (string escape -n -- $right) if [ $left = $right ] - bind $r "_pisces_skip $right; or _pisces_append $right" - else # if $some_special_setting + bind $r "_pisces_skip $right; or _pisces_surround '' $right" + else - bind $l "commandline -i -- $left; and _pisces_append $right" + bind $l "_pisces_surround $left $right" bind $r "_pisces_skip $right" end end diff --git a/functions/_pisces_match.fish b/functions/_pisces_match.fish new file mode 100644 index 0000000..cfbca2e --- /dev/null +++ b/functions/_pisces_match.fish @@ -0,0 +1,12 @@ +function _pisces_match -a l_ctx r_ctx -d "Matches the context around the cursor with the given patterns (returns true/false)" + + set buffer (commandline -b) + set cur (commandline -C) + + set left (string sub -l -- "$cur" "$buffer") + set right (string sub -s -- (math "$cur + 1") "$buffer") + + string match -q -r -- $l_ctx'$' " $left" + and \ + string match -q -r -- '^'$r_ctx "$right " +end diff --git a/functions/_pisces_remove.fish b/functions/_pisces_remove.fish index cd15a03..847b756 100644 --- a/functions/_pisces_remove.fish +++ b/functions/_pisces_remove.fish @@ -1,8 +1,8 @@ function _pisces_remove -a left right -d "Removes an empty pair (left-right) or returns false" - set left_len (string length $left) - set right_len (string length $right) - set length (math "$left_len + $right_len") + set left_len (string length -- $left) + set right_len (string length -- $right) + set length -- (math "$left_len + $right_len") if [ (_pisces_lookup -$left_len $length) = "$left$right" ] diff --git a/functions/_pisces_skip.fish b/functions/_pisces_skip.fish index 2ed8902..d3b018f 100644 --- a/functions/_pisces_skip.fish +++ b/functions/_pisces_skip.fish @@ -6,7 +6,7 @@ function _pisces_skip -a text -d "Skips given text if it's already under the cur _pisces_jump $length return 0 else - commandline -i -- $text + commandline --insert -- $text return 1 end end diff --git a/functions/_pisces_surround.fish b/functions/_pisces_surround.fish new file mode 100644 index 0000000..cdda72e --- /dev/null +++ b/functions/_pisces_surround.fish @@ -0,0 +1,5 @@ +function _pisces_surround -a left right -d "Inserts a pair of strings and puts the cursor between them" + + commandline --insert -- "$left$right" + and _pisces_jump -(string length -- $right) +end diff --git a/key_bindings.fish b/key_bindings.fish index badf58f..3e3c7e4 100644 --- a/key_bindings.fish +++ b/key_bindings.fish @@ -1,12 +1,42 @@ function key_bindings set -q pisces_pairs + # or set -U pisces_pairs '(,)' '[,]' '{,}' '","' "','" or set -U pisces_pairs '(,)' '[,]' '{,}' '","' "','" for pair in $pisces_pairs _pisces_bind_pair (string split -- ',' $pair) end + function pattern + string join -- \t $argv + end + + set -q pisces_patterns + or set -U pisces_patterns \ + (pattern '[^\w\\\]' '[' '\W' '[ ' ' ]') \ + (pattern '[^\w\\\]' '"' '\W' '"' '"') \ + (pattern '[^\w\\\]' "'" '\W' "'" "'") \ + (pattern 'for|if|switch' ';' '\W' ' ' '; end;') + # (pattern 'for' ';' '\W' "\n" "\nend") + # (pattern 'begin' ';' '\W' '; ' '; end;') \ + + function _pisces_bind_pattern -a l_ctx trg r_ctx l r + set lc (string escape -n -- $l_ctx) + set rc (string escape -n -- $r_ctx) + + set l (string escape -n -- $l) + set r (string escape -n -- $r) + + set t (string escape -n -- $trg) + + bind $trg "_pisces_match $lc $rc; and _pisces_surround $l $r; or commandline -i -- $t" + end + + for p in $pisces_patterns + _pisces_bind_pattern (string split -- \t $p) + end + # normal backspace, also known as \010 or ^H: bind \b _pisces_backspace # Terminal.app sends DEL code on ⌫: