c0b98cd6aca5b6148a5ee147b087772bfb9f5e21

Author
Jeff Williams <jeffsmessages@gmail.com>
Committer
Jeff Williams <jeffsmessages@gmail.com>
Date

Message

Added dirhistory plugin.

This plugin navigates directory history using ALT-LEFT and ALT-RIGHT.

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/dirhistory/dirhistory.plugin.zsh b/plugins/dirhistory/dirhistory.plugin.zsh
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..504d7ec14b3817631b2aff8d36fd8a6c8d227c24
  4--- /dev/null
  5+++ b/plugins/dirhistory/dirhistory.plugin.zsh
  6@@ -0,0 +1,132 @@
  7+## 
  8+#   Navigate directory history using ALT-LEFT and ALT-RIGHT. ALT-LEFT moves back to directories 
  9+#   that the user has changed to in the past, and ALT-RIGHT undoes ALT-LEFT.
 10+# 
 11+
 12+dirhistory_past=(`pwd`)
 13+dirhistory_future=()
 14+export dirhistory_past
 15+export dirhistory_future
 16+
 17+export DIRHISTORY_SIZE=30
 18+
 19+# Pop the last element of dirhistory_past. 
 20+# Pass the name of the variable to return the result in. 
 21+# Returns the element if the array was not empty,
 22+# otherwise returns empty string.
 23+function pop_past() {
 24+  eval "$1='$dirhistory_past[$#dirhistory_past]'"
 25+  if [[ $#dirhistory_past -gt 0 ]]; then
 26+    dirhistory_past[$#dirhistory_past]=()
 27+  fi
 28+}
 29+
 30+function pop_future() {
 31+  eval "$1='$dirhistory_future[$#dirhistory_future]'"
 32+  if [[ $#dirhistory_future -gt 0 ]]; then
 33+    dirhistory_future[$#dirhistory_future]=()
 34+  fi
 35+}
 36+
 37+# Push a new element onto the end of dirhistory_past. If the size of the array 
 38+# is >= DIRHISTORY_SIZE, the array is shifted
 39+function push_past() {
 40+  if [[ $#dirhistory_past -ge $DIRHISTORY_SIZE ]]; then
 41+    shift dirhistory_past
 42+  fi
 43+  if [[ $#dirhistory_past -eq 0 || $dirhistory_past[$#dirhistory_past] != "$1" ]]; then
 44+    dirhistory_past+=($1)
 45+  fi
 46+}
 47+
 48+function push_future() {
 49+  if [[ $#dirhistory_future -ge $DIRHISTORY_SIZE ]]; then
 50+    shift dirhistory_future
 51+  fi
 52+  if [[ $#dirhistory_future -eq 0 || $dirhistory_futuret[$#dirhistory_future] != "$1" ]]; then
 53+    dirhistory_future+=($1)
 54+  fi
 55+}
 56+
 57+# Called by zsh when directory changes
 58+function chpwd() {
 59+  push_past `pwd`
 60+  # If DIRHISTORY_CD is not set...
 61+  if [[ -z "${DIRHISTORY_CD+x}" ]]; then
 62+    # ... clear future.
 63+    dirhistory_future=()
 64+  fi
 65+}
 66+
 67+function dirhistory_cd(){
 68+  DIRHISTORY_CD="1"
 69+  cd $1
 70+  unset DIRHISTORY_CD
 71+}
 72+
 73+# Move backward in directory history
 74+function dirhistory_back() {
 75+  local cw=""
 76+  local d=""
 77+  # Last element in dirhistory_past is the cwd.
 78+
 79+  pop_past cw 
 80+  if [[ "" == "$cw" ]]; then
 81+    # Someone overwrote our variable. Recover it.
 82+    dirhistory_past=(`pwd`)
 83+    return
 84+  fi
 85+
 86+  pop_past d
 87+  if [[ "" != "$d" ]]; then
 88+    dirhistory_cd $d
 89+    push_future $cw
 90+  else
 91+    push_past $cw
 92+  fi
 93+}
 94+
 95+
 96+# Move forward in directory history
 97+function dirhistory_forward() {
 98+  local d=""
 99+
100+  pop_future d
101+  if [[ "" != "$d" ]]; then
102+    dirhistory_cd $d
103+    push_past $d
104+  fi
105+}