Skip to content

Commit 89685b8

Browse files
authored
Prevent arguments from being interpreted as options (#80)
Prevent errors that can happen if the current token, selected path, or selected command, beings with a `-`. Examples where fzf.fish would error: - the cursor is over the token --something when the user executes search files - the cursor is over -z when the user executes search variables - the selected changed path from git status is -folder - the selected command is --function The solution is to delineate the end of options with --, which is a POSIX specification defined in https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02 (guideline 10), which is respected by Fish builtins. After --, the command will always interpret the arguments as positional arguments.
1 parent a734bbc commit 89685b8

File tree

4 files changed

+6
-6
lines changed

4 files changed

+6
-6
lines changed

functions/__fzf_search_current_dir.fish

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function __fzf_search_current_dir --description "Search the current directory. R
99

1010
# If the current token is a directory and has a trailing slash,
1111
# then use it as fd's base directory.
12-
if string match --quiet "*/" $token && test -d $token
12+
if string match --quiet -- "*/" $token && test -d $token
1313
set --append fd_arguments --base-directory=$token
1414
# use the directory name as fzf's prompt to indicate the search is limited to that directory
1515
set --append fzf_arguments --prompt=$token --preview="__fzf_preview_file $token{}"
@@ -34,7 +34,7 @@ function __fzf_search_current_dir --description "Search the current directory. R
3434
end
3535
end
3636

37-
commandline --current-token --replace (string escape $file_paths_selected | string join ' ')
37+
commandline --current-token --replace -- (string escape -- $file_paths_selected | string join ' ')
3838
end
3939

4040
commandline --function repaint

functions/__fzf_search_git_status.fish

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function __fzf_search_git_status --description "Search the output of git status.
2222
end
2323
end
2424

25-
commandline --current-token --replace (string escape $cleaned_paths | string join ' ')
25+
commandline --current-token --replace -- (string escape -- $cleaned_paths | string join ' ')
2626
end
2727
end
2828

functions/__fzf_search_history.fish

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function __fzf_search_history --description "Search command history. Replace the
1010

1111
if test $status -eq 0
1212
set command_selected (string split --max 1 " | " $command_with_ts)[2]
13-
commandline --replace $command_selected
13+
commandline --replace -- $command_selected
1414
end
1515

1616
commandline --function repaint

functions/__fzf_search_shell_variables.fish

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function __fzf_search_shell_variables --argument-names set_show_output set_names
2727
set current_token (commandline --current-token)
2828
# Use the current token to pre-populate fzf's query. If the current token begins
2929
# with a $, remove it from the query so that it will better match the variable names
30-
set cleaned_curr_token (string replace '$' '' $current_token)
30+
set cleaned_curr_token (string replace -- '$' '' $current_token)
3131

3232
set variable_name (
3333
printf '%s\n' $all_variable_names |
@@ -38,7 +38,7 @@ function __fzf_search_shell_variables --argument-names set_show_output set_names
3838
if test $status -eq 0
3939
# If the current token begins with a $, do not overwrite the $ when
4040
# replacing the current token with the selected variable.
41-
if string match --quiet '$*' $current_token
41+
if string match --quiet -- '$*' $current_token
4242
commandline --current-token --replace \$$variable_name
4343
else
4444
commandline --current-token --replace $variable_name

0 commit comments

Comments
 (0)