Skip to content

Commit

Permalink
support !%
Browse files Browse the repository at this point in the history
  • Loading branch information
tenfyzhong committed Jan 30, 2024
1 parent a408c74 commit 129962f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
4 changes: 2 additions & 2 deletions conf.d/bang.fish
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# !-n
# !n
# !string
# !?string[?] (the last ? is optional)
# !?string?
# !#
#
# Part 2: Word Designators
Expand All @@ -28,7 +28,7 @@
# ^string1^string2^

# https://regex101.com/r/gYfhPu/2
set -gx _bang_regex '^!(?<cmdp>!|-?\d+|\w+|\?[^?]+\??|#)(?<wordp>:?\^|:?\$|:?\*|:?-|:?%|:\d+-\d+|:\d+\*|:-\d+|:\d+-|:s\^[^^]+\^[^^]+\^?)?$'
set -gx _bang_regex '^!(?<cmdp>!|-?\d+|\w+|\?[^?]+\?|#)(?<wordp>:?\^|:?\$|:?\*|:?-|:?%|:\d+-\d+|:\d+\*|:-\d+|:\d+-|:s\^[^^]+\^[^^]+\^?)?$'
abbr -a _bang -r $_bang_regex --position anywhere --function _bang

# https://regex101.com/r/V4nhLy/1
Expand Down
11 changes: 7 additions & 4 deletions functions/_bang.fish
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ function _bang -a pattern
_bang_find_word "$cmd" (string sub -s 2 $pattern)
return 0
case '!%'
# TODO
set tokens (string split ' ' $_bind_last_search)
echo $tokens[1]
case '^*^*' '^*^*^'
set cmd $history[1]
_bang_find_word "$cmd" "$pattern"
Expand Down Expand Up @@ -45,10 +46,11 @@ function _bang_find_cmd -a cmdp
return 0
end
end
else if string match -q -r -- '^\?(?<key>[^?]+)\??$' "$cmdp"
# !?string[?]
else if string match -q -r -- '^\?(?<key>[^?]+)\?$' "$cmdp"
# !?string?
for cmd in $history
if string match -q -r -- "$key" "$cmd"
set -gx _bind_last_search "$key"
echo $cmd
return 0
end
Expand Down Expand Up @@ -93,7 +95,8 @@ function _bang_find_word -a cmd -a wordp
echo (string join ' ' $token[1..-2])
else if test "$wordmatch" = '%'
# :%
# TODO
set tokens (string split ' ' $_bind_last_search)
echo $tokens[1]
else if string match -q -r -- '^(?<x>\d+)?-(?<y>\d+)?$' "$wordmatch"
# x-y
# x-
Expand Down
27 changes: 24 additions & 3 deletions tests/bang.fish
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ set output (_bang '!$')
set output (_bang '!*')
@test 'test !*' "$output" = 'three four five six seven'

@test 'test regex match !%' (string match -q -r $_bang_special_regex '!%') $status -eq 0
set output (_bang '!%')
@test 'test !%' "$output" = ''
_bang '!?bar foobar?'
set output (_bang '!%')
@test 'test !%' "$output" = 'bar'
_bang '!?foo bar foobar?'
set output (_bang '!%')
@test 'test !%' "$output" = 'foo'

@test 'test regex match !!' (string match -q -r $_bang_regex '!!') $status -eq 0
set output (_bang !!)
@test 'test !!' "$output" = 'cd three four five six seven'
Expand All @@ -77,13 +87,14 @@ set output (_bang !-1)
set output (_bang !ln)
@test 'test !ln' "$output" = 'ln -s hello world1'

@test 'test regex match !?foo' (string match -q -r $_bang_regex '!?foo') $status -eq 0
set output (_bang '!?foo')
@test 'test !?foo' "$output" = 'mv foo bar foobar barfoo foobarfoo'
# @test 'test regex match !?foo' (string match -q -r $_bang_regex '!?foo') $status -eq 0
# set output (_bang '!?foo')
# @test 'test !?foo' "$output" = 'mv foo bar foobar barfoo foobarfoo'

@test 'test regex match !?foo?' (string match -q -r $_bang_regex '!?foo?') $status -eq 0
set output (_bang '!?foo?')
@test 'test !?foo?' "$output" = 'mv foo bar foobar barfoo foobarfoo'
@test 'test _bind_last_search' "$_bind_last_search" = "foo"

@test 'test regex match !#' (string match -q -r $_bang_regex '!#') $status -eq 0
mock_commandline 'cd go !#'
Expand Down Expand Up @@ -257,6 +268,16 @@ set output (_bang '!mv*')
set output (_bang '!mv:-')
@test 'test !mv:-' "$output" = 'mv foo bar foobar barfoo'

@test 'test regex match !mv:%' (string match -q -r $_bang_regex '!mv:%') $status -eq 0
_bang '!?foo bar foobar?'
set output (_bang '!mv:%')
@test 'test !mv:%' "$output" = 'foo'

@test 'test regex match !mv%' (string match -q -r $_bang_regex '!mv%') $status -eq 0
_bang '!?foo bar foobar?'
set output (_bang '!mv%')
@test 'test !mv%' "$output" = 'foo'

@test 'test regex match !mv-' (string match -q -r $_bang_regex '!mv-') $status -eq 0
set output (_bang '!mv-')
@test 'test !mv-' "$output" = 'mv foo bar foobar barfoo'
Expand Down

0 comments on commit 129962f

Please sign in to comment.