dirhistory.plugin.zsh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. function chpwd() {
  46. push_past `pwd`
  47. # If DIRHISTORY_CD is not set...
  48. if [[ -z "${DIRHISTORY_CD+x}" ]]; then
  49. # ... clear future.
  50. dirhistory_future=()
  51. fi
  52. }
  53. function dirhistory_cd(){
  54. DIRHISTORY_CD="1"
  55. cd $1
  56. unset DIRHISTORY_CD
  57. }
  58. # Move backward in directory history
  59. function dirhistory_back() {
  60. local cw=""
  61. local d=""
  62. # Last element in dirhistory_past is the cwd.
  63. pop_past cw
  64. if [[ "" == "$cw" ]]; then
  65. # Someone overwrote our variable. Recover it.
  66. dirhistory_past=(`pwd`)
  67. return
  68. fi
  69. pop_past d
  70. if [[ "" != "$d" ]]; then
  71. dirhistory_cd $d
  72. push_future $cw
  73. else
  74. push_past $cw
  75. fi
  76. }
  77. # Move forward in directory history
  78. function dirhistory_forward() {
  79. local d=""
  80. pop_future d
  81. if [[ "" != "$d" ]]; then
  82. dirhistory_cd $d
  83. push_past $d
  84. fi
  85. }
  86. # Bind keys to history navigation
  87. function dirhistory_zle_dirhistory_back() {
  88. # Erase current line in buffer
  89. zle kill-buffer
  90. dirhistory_back
  91. zle accept-line
  92. }
  93. function dirhistory_zle_dirhistory_future() {
  94. # Erase current line in buffer
  95. zle kill-buffer
  96. dirhistory_forward
  97. zle accept-line
  98. }
  99. zle -N dirhistory_zle_dirhistory_back
  100. # xterm in normal mode
  101. bindkey "\e[3D" dirhistory_zle_dirhistory_back
  102. bindkey "\e[1;3D" dirhistory_zle_dirhistory_back
  103. # Putty:
  104. bindkey "\e\e[D" dirhistory_zle_dirhistory_back
  105. # GNU screen:
  106. bindkey "\eO3D" dirhistory_zle_dirhistory_back
  107. zle -N dirhistory_zle_dirhistory_future
  108. bindkey "\e[3C" dirhistory_zle_dirhistory_future
  109. bindkey "\e[1;3C" dirhistory_zle_dirhistory_future
  110. bindkey "\e\e[C" dirhistory_zle_dirhistory_future
  111. bindkey "\eO3C" dirhistory_zle_dirhistory_future