cxreg / smartcd

Alter your bash (or zsh) environment as you cd
Other
682 stars 56 forks source link

add support for chpwd_functions #22

Open mpapis opened 10 years ago

mpapis commented 10 years ago

following https://github.com/wayneeseguin/rvm/issues/2819 - explicit support for smartcd was removed from RVM and replaced with generic handler chpwd_functions implemented in accordance to Zsh specification => http://zsh.sourceforge.net/Doc/Release/Functions.html#Hook-Functions

you can implement the same mechanism using this code:

  [[ -n "${ZSH_VERSION:-}" ]] ||
  {
    # On bash and old zsh we need to define chpwd_functions handler
    __zsh_like_cd()
    {
      typeset __zsh_like_cd_hook
      if
        builtin "$@"
      then
        shift || true # remove the called method
        for __zsh_like_cd_hook in chpwd "${chpwd_functions[@]}"
        do
          if typeset -v "$__zsh_like_cd_hook" >/dev/null 2>&1
          then "$__zsh_like_cd_hook" "$@" || break # finish on first failed hook
          fi
        done
        true
      else
        return $?
      fi
    }
    cd()    { __zsh_like_cd cd    "$@" ; }
    popd()  { __zsh_like_cd popd  "$@" ; }
    pushd() { __zsh_like_cd pushd "$@" ; }
  }

from now on adding code to execute after cd is as easy as:

export -a chpwd_functions
chpwd_functions=( "${chpwd_functions[@]}" smartcd )

and this will be compatible with all other software implementing the same mechanism

mpapis commented 10 years ago

I have created a common repository jst for the "bash zsh support" part, you can follow it here https://github.com/mpapis/bash_zsh_support

blueyed commented 10 years ago

Related: my PR to add (zsh's) chpwd to the config tool, and prefer it: https://github.com/cxreg/smartcd/pull/26

@mpapis

this will be compatible with all other software implementing the same mechanism

So for smartcd it could instead look for $chpwd_functions instead of testing for $ZSH_VERSION, and the user would have to provide the compatibility wrapper like in your example?

mpapis commented 10 years ago

user does not have to provide the wrapper, every tool can redefine it, there is no harm in defining the same functions many times, and yes you can simplify your code to load the wrappers when not in zsh and add hook to chpwd_functions

blueyed commented 10 years ago

Great. Then smartcd could use the same/better implementation for both bash and Zsh, dropping/deprecating the cd/pushd/popd approach.

@cxreg do you agree?