Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context-dependent patterns #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conf.d/_minimal_conf.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set -e fish_greeting
5 changes: 0 additions & 5 deletions functions/_pisces_append.fish

This file was deleted.

10 changes: 5 additions & 5 deletions functions/_pisces_bind_pair.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions functions/_pisces_match.fish
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions functions/_pisces_remove.fish
Original file line number Diff line number Diff line change
@@ -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" ]

Expand Down
2 changes: 1 addition & 1 deletion functions/_pisces_skip.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions functions/_pisces_surround.fish
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions key_bindings.fish
Original file line number Diff line number Diff line change
@@ -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 ⌫:
Expand Down