n-cd 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copy this file into /usr/share/zsh/site-functions/
  2. # and add 'autoload n-cd` to .zshrc
  3. #
  4. # This function allows to choose a directory from pushd stack
  5. #
  6. # Uses n-list
  7. emulate -L zsh
  8. setopt extendedglob pushdignoredups
  9. zmodload zsh/curses
  10. local IFS="
  11. "
  12. # Unset before configuration is read
  13. unset NLIST_COLORING_PATTERN
  14. [ -f ~/.config/znt/n-list.conf ] && builtin source ~/.config/znt/n-list.conf
  15. [ -f ~/.config/znt/n-cd.conf ] && builtin source ~/.config/znt/n-cd.conf
  16. local list
  17. local selected
  18. NLIST_REMEMBER_STATE=0
  19. list=( `dirs -p` )
  20. list=( "${(@M)list:#(#i)*$1*}" )
  21. local NLIST_GREP_STRING="$1"
  22. [ "$#list" -eq 0 ] && echo "No matching directories"
  23. if [ "$#hotlist" -ge 1 ]; then
  24. typeset -a NLIST_NONSELECTABLE_ELEMENTS NLIST_HOP_INDEXES
  25. local tmp_list_size="$#list"
  26. NLIST_NONSELECTABLE_ELEMENTS=( $(( tmp_list_size+1 )) $(( tmp_list_size+2 )) )
  27. list=( "$list[@]" "" $'\x1b[00;31m'"Hotlist"$'\x1b[00;00m': "$hotlist[@]" )
  28. (( tmp_list_size+=3 ))
  29. local middle_hop=$(( (tmp_list_size+$#list) / 2 ))
  30. [[ "$middle_hop" -eq $tmp_list_size || "$middle_hop" -eq $#list ]] && middle_hop=""
  31. [ "$tmp_list_size" -eq $#list ] && tmp_list_size=""
  32. NLIST_HOP_INDEXES=( 1 $tmp_list_size $middle_hop $#list )
  33. else
  34. [ "$#list" -eq 0 ] && return 1
  35. fi
  36. n-list "${list[@]}"
  37. if [ "$REPLY" -gt 0 ]; then
  38. selected="$reply[REPLY]"
  39. selected="${selected/#\~/$HOME}"
  40. (( NCD_DONT_PUSHD )) && setopt NO_AUTO_PUSHD
  41. cd "$selected"
  42. local code=$?
  43. (( NCD_DONT_PUSHD )) && setopt AUTO_PUSHD
  44. if [ "$code" -eq "0" ]; then
  45. # ZLE?
  46. if [ "${(t)CURSOR}" = "integer-local-special" ]; then
  47. zle -M "You have selected $selected"
  48. else
  49. echo "You have selected $selected"
  50. fi
  51. fi
  52. else
  53. [ "${(t)CURSOR}" = "integer-local-special" ] && zle redisplay
  54. fi
  55. # vim: set filetype=zsh: