dirhistory.plugin.zsh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. ##
  2. # Navigate directory history using ALT-LEFT and ALT-RIGHT. ALT-LEFT moves back to directories
  3. # that the user has changed to in the past, and ALT-RIGHT undoes ALT-LEFT.
  4. #
  5. dirhistory_past=($PWD)
  6. dirhistory_future=()
  7. export dirhistory_past
  8. export dirhistory_future
  9. export DIRHISTORY_SIZE=30
  10. # Pop the last element of dirhistory_past.
  11. # Pass the name of the variable to return the result in.
  12. # Returns the element if the array was not empty,
  13. # otherwise returns empty string.
  14. function pop_past() {
  15. eval "$1='$dirhistory_past[$#dirhistory_past]'"
  16. if [[ $#dirhistory_past -gt 0 ]]; then
  17. dirhistory_past[$#dirhistory_past]=()
  18. fi
  19. }
  20. function pop_future() {
  21. eval "$1='$dirhistory_future[$#dirhistory_future]'"
  22. if [[ $#dirhistory_future -gt 0 ]]; then
  23. dirhistory_future[$#dirhistory_future]=()
  24. fi
  25. }
  26. # Push a new element onto the end of dirhistory_past. If the size of the array
  27. # is >= DIRHISTORY_SIZE, the array is shifted
  28. function push_past() {
  29. if [[ $#dirhistory_past -ge $DIRHISTORY_SIZE ]]; then
  30. shift dirhistory_past
  31. fi
  32. if [[ $#dirhistory_past -eq 0 || $dirhistory_past[$#dirhistory_past] != "$1" ]]; then
  33. dirhistory_past+=($1)
  34. fi
  35. }
  36. function push_future() {
  37. if [[ $#dirhistory_future -ge $DIRHISTORY_SIZE ]]; then
  38. shift dirhistory_future
  39. fi
  40. if [[ $#dirhistory_future -eq 0 || $dirhistory_futuret[$#dirhistory_future] != "$1" ]]; then
  41. dirhistory_future+=($1)
  42. fi
  43. }
  44. # Called by zsh when directory changes
  45. chpwd_functions+=(chpwd_dirhistory)
  46. function chpwd_dirhistory() {
  47. push_past $PWD
  48. # If DIRHISTORY_CD is not set...
  49. if [[ -z "${DIRHISTORY_CD+x}" ]]; then
  50. # ... clear future.
  51. dirhistory_future=()
  52. fi
  53. }
  54. function dirhistory_cd(){
  55. DIRHISTORY_CD="1"
  56. cd $1
  57. unset DIRHISTORY_CD
  58. }
  59. # Move backward in directory history
  60. function dirhistory_back() {
  61. local cw=""
  62. local d=""
  63. # Last element in dirhistory_past is the cwd.
  64. pop_past cw
  65. if [[ "" == "$cw" ]]; then
  66. # Someone overwrote our variable. Recover it.
  67. dirhistory_past=($PWD)
  68. return
  69. fi
  70. pop_past d
  71. if [[ "" != "$d" ]]; then
  72. dirhistory_cd $d
  73. push_future $cw
  74. else
  75. push_past $cw
  76. fi
  77. }
  78. # Move forward in directory history
  79. function dirhistory_forward() {
  80. local d=""
  81. pop_future d
  82. if [[ "" != "$d" ]]; then
  83. dirhistory_cd $d
  84. push_past $d
  85. fi
  86. }
  87. # Bind keys to history navigation
  88. function dirhistory_zle_dirhistory_back() {
  89. # Erase current line in buffer
  90. zle kill-buffer
  91. dirhistory_back
  92. zle accept-line
  93. }
  94. function dirhistory_zle_dirhistory_future() {
  95. # Erase current line in buffer
  96. zle kill-buffer
  97. dirhistory_forward
  98. zle accept-line
  99. }
  100. zle -N dirhistory_zle_dirhistory_back
  101. # xterm in normal mode
  102. bindkey "\e[3D" dirhistory_zle_dirhistory_back
  103. bindkey "\e[1;3D" dirhistory_zle_dirhistory_back
  104. # Putty:
  105. bindkey "\e\e[D" dirhistory_zle_dirhistory_back
  106. # GNU screen:
  107. bindkey "\eO3D" dirhistory_zle_dirhistory_back
  108. zle -N dirhistory_zle_dirhistory_future
  109. bindkey "\e[3C" dirhistory_zle_dirhistory_future
  110. bindkey "\e[1;3C" dirhistory_zle_dirhistory_future
  111. bindkey "\e\e[C" dirhistory_zle_dirhistory_future
  112. bindkey "\eO3C" dirhistory_zle_dirhistory_future