Prevent endless recusion with alias cd on fish

- Create a custom __fish_cd function for use in __zoxide_cd.
- The __fish_cd function is set to be either a copy of the fish cd
  wrapper function, or an alias for `builtin cd` as fallback if the
  wrapper function does not exist for some reason.
- This prevents an endless recusion when using `alias cd=z` that would
  occur because __zoxide_cd would call the `cd` alias which would in
  turn call `z` and so on.
This commit is contained in:
Daniel M 2021-05-09 19:40:40 +02:00
parent ba8a5f3167
commit b1f7f7d839
1 changed files with 18 additions and 2 deletions

View File

@ -5,6 +5,21 @@
# Utility functions for zoxide. # Utility functions for zoxide.
# #
# provide a custom __fish_cd function that is either a copy of the default fish
# cd wrapper function, or an alias for the `builtin cd` to prevent recursively
# calling cd when using `alias cd=z`.
#
# only create the function `__fish_cd` if it doesn't yet exist
if ! functions -q __fish_cd
if functions -q cd
# use the fish wrapper function for __fish_cd if it exists
functions -c cd __fish_cd
else
# use `builtin cd` as fallback if the cd wrapper function does not exist
alias __fish_cd="builtin cd"
end
end
# pwd based on the value of _ZO_RESOLVE_SYMLINKS. # pwd based on the value of _ZO_RESOLVE_SYMLINKS.
function __zoxide_pwd function __zoxide_pwd
{%- if resolve_symlinks %} {%- if resolve_symlinks %}
@ -18,8 +33,9 @@ end
function __zoxide_cd function __zoxide_cd
{#- We can't use `builtin cd` over here, because fish wraps its builtin cd with {#- We can't use `builtin cd` over here, because fish wraps its builtin cd with
a function that adds extra features (such as `cd -`). Using the builtin a function that adds extra features (such as `cd -`). Using the builtin
would make those features stop working. #} would make those features stop working. Instead use a custom function that is
cd $argv either a copy of the cd function or an alias for `builtin cd` as fallback #}
__fish_cd $argv
{%- if echo %} {%- if echo %}
and __zoxide_pwd and __zoxide_pwd
{%- endif %} {%- endif %}