z.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # Copyright (c) 2009 rupa deadwyler under the WTFPL license
  2. # maintains a jump-list of the directories you actually use
  3. #
  4. # INSTALL:
  5. # * put something like this in your .bashrc/.zshrc:
  6. # . /path/to/z.sh
  7. # * cd around for a while to build up the db
  8. # * PROFIT!!
  9. # * optionally:
  10. # set $_Z_CMD in .bashrc/.zshrc to change the command (default z).
  11. # set $_Z_DATA in .bashrc/.zshrc to change the datafile (default ~/.z).
  12. # set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution.
  13. # set $_Z_NO_PROMPT_COMMAND if you're handling PROMPT_COMMAND yourself.
  14. # set $_Z_EXCLUDE_DIRS to an array of directories to exclude.
  15. #
  16. # USE:
  17. # * z foo # cd to most frecent dir matching foo
  18. # * z foo bar # cd to most frecent dir matching foo and bar
  19. # * z -r foo # cd to highest ranked dir matching foo
  20. # * z -t foo # cd to most recently accessed dir matching foo
  21. # * z -l foo # list matches instead of cd
  22. # * z -c foo # restrict matches to subdirs of $PWD
  23. case $- in
  24. *i*) ;;
  25. *) echo 'ERROR: z.sh is meant to be sourced, not directly executed.'
  26. esac
  27. [ -d "${_Z_DATA:-$HOME/.z}" ] && {
  28. echo "ERROR: z.sh's datafile (${_Z_DATA:-$HOME/.z}) is a directory."
  29. }
  30. _z() {
  31. local datafile="${_Z_DATA:-$HOME/.z}"
  32. # bail out if we don't own ~/.z (we're another user but our ENV is still set)
  33. [ -f "$datafile" -a ! -O "$datafile" ] && return
  34. # add entries
  35. if [ "$1" = "--add" ]; then
  36. shift
  37. # $HOME isn't worth matching
  38. [ "$*" = "$HOME" ] && return
  39. # don't track excluded dirs
  40. local exclude
  41. for exclude in "${_Z_EXCLUDE_DIRS[@]}"; do
  42. [ "$*" = "$exclude" ] && return
  43. done
  44. # maintain the file
  45. local tempfile
  46. tempfile="$(mktemp "$datafile.XXXXXX")" || return
  47. while read line; do
  48. [ -d "${line%%\|*}" ] && echo $line
  49. done < "$datafile" | awk -v path="$*" -v now="$(date +%s)" -F"|" '
  50. BEGIN {
  51. rank[path] = 1
  52. time[path] = now
  53. }
  54. $2 >= 1 {
  55. if( $1 == path ) {
  56. rank[$1] = $2 + 1
  57. time[$1] = now
  58. } else {
  59. rank[$1] = $2
  60. time[$1] = $3
  61. }
  62. count += $2
  63. }
  64. END {
  65. if( count > 6000 ) {
  66. for( i in rank ) print i "|" 0.99*rank[i] "|" time[i] # aging
  67. } else for( i in rank ) print i "|" rank[i] "|" time[i]
  68. }
  69. ' 2>/dev/null >| "$tempfile"
  70. if [ $? -ne 0 -a -f "$datafile" ]; then
  71. env rm -f "$tempfile"
  72. else
  73. env mv -f "$tempfile" "$datafile"
  74. fi
  75. # tab completion
  76. elif [ "$1" = "--complete" ]; then
  77. while read line; do
  78. [ -d "${line%%\|*}" ] && echo $line
  79. done < "$datafile" | awk -v q="$2" -F"|" '
  80. BEGIN {
  81. if( q == tolower(q) ) nocase = 1
  82. split(substr(q,3),fnd," ")
  83. }
  84. {
  85. if( nocase ) {
  86. for( i in fnd ) tolower($1) !~ tolower(fnd[i]) && $1 = ""
  87. } else {
  88. for( i in fnd ) $1 !~ fnd[i] && $1 = ""
  89. }
  90. if( $1 ) print $1
  91. }
  92. ' 2>/dev/null
  93. else
  94. # list/go
  95. while [ "$1" ]; do case "$1" in
  96. --) while [ "$1" ]; do shift; local fnd="$fnd $1";done;;
  97. -*) local opt=${1:1}; while [ "$opt" ]; do case ${opt:0:1} in
  98. c) local fnd="^$PWD $fnd";;
  99. h) echo "${_Z_CMD:-z} [-chlrt] args" >&2; return;;
  100. l) local list=1;;
  101. r) local typ="rank";;
  102. t) local typ="recent";;
  103. esac; opt=${opt:1}; done;;
  104. *) local fnd="$fnd $1";;
  105. esac; local last=$1; shift; done
  106. [ "$fnd" -a "$fnd" != "^$PWD " ] || local list=1
  107. # if we hit enter on a completion just go there
  108. case "$last" in
  109. # completions will always start with /
  110. /*) [ -z "$list" -a -d "$last" ] && cd "$last" && return;;
  111. esac
  112. # no file yet
  113. [ -f "$datafile" ] || return
  114. local cd
  115. cd="$(while read line; do
  116. [ -d "${line%%\|*}" ] && echo $line
  117. done < "$datafile" | awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" '
  118. function frecent(rank, time) {
  119. dx = t-time
  120. if( dx < 3600 ) return rank*4
  121. if( dx < 86400 ) return rank*2
  122. if( dx < 604800 ) return rank/2
  123. return rank/4
  124. }
  125. function output(files, toopen, override) {
  126. if( list ) {
  127. cmd = "sort -n >&2"
  128. for( i in files ) if( files[i] ) printf "%-10s %s\n", files[i], i | cmd
  129. if( override ) printf "%-10s %s\n", "common:", override > "/dev/stderr"
  130. } else {
  131. if( override ) toopen = override
  132. print toopen
  133. }
  134. }
  135. function common(matches) {
  136. # shortest match
  137. for( i in matches ) {
  138. if( matches[i] && (!short || length(i) < length(short)) ) short = i
  139. }
  140. if( short == "/" ) return
  141. # shortest match must be common to each match. escape special characters in
  142. # a copy when testing, so we can return the original.
  143. clean_short = short
  144. gsub(/[\(\)\[\]\|]/, "\\\\&", clean_short)
  145. for( i in matches ) if( matches[i] && i !~ clean_short ) return
  146. return short
  147. }
  148. BEGIN { split(q, a, " "); oldf = noldf = -9999999999 }
  149. {
  150. if( typ == "rank" ) {
  151. f = $2
  152. } else if( typ == "recent" ) {
  153. f = $3-t
  154. } else f = frecent($2, $3)
  155. wcase[$1] = nocase[$1] = f
  156. for( i in a ) {
  157. if( $1 !~ a[i] ) delete wcase[$1]
  158. if( tolower($1) !~ tolower(a[i]) ) delete nocase[$1]
  159. }
  160. if( wcase[$1] && wcase[$1] > oldf ) {
  161. cx = $1
  162. oldf = wcase[$1]
  163. } else if( nocase[$1] && nocase[$1] > noldf ) {
  164. ncx = $1
  165. noldf = nocase[$1]
  166. }
  167. }
  168. END {
  169. if( cx ) {
  170. output(wcase, cx, common(wcase))
  171. } else if( ncx ) output(nocase, ncx, common(nocase))
  172. }
  173. ')"
  174. [ $? -gt 0 ] && return
  175. [ "$cd" ] && cd "$cd"
  176. fi
  177. }
  178. alias ${_Z_CMD:-z}='_z 2>&1'
  179. [ "$_Z_NO_RESOLVE_SYMLINKS" ] || _Z_RESOLVE_SYMLINKS="-P"
  180. if compctl &> /dev/null; then
  181. [ "$_Z_NO_PROMPT_COMMAND" ] || {
  182. # zsh populate directory list, avoid clobbering any other precmds
  183. if [ "$_Z_NO_RESOLVE_SYMLINKS" ]; then
  184. _z_precmd() {
  185. _z --add "${PWD:a}"
  186. }
  187. else
  188. _z_precmd() {
  189. _z --add "${PWD:A}"
  190. }
  191. fi
  192. precmd_functions+=(_z_precmd)
  193. }
  194. # zsh tab completion
  195. _z_zsh_tab_completion() {
  196. local compl
  197. read -l compl
  198. reply=(${(f)"$(_z --complete "$compl")"})
  199. }
  200. compctl -U -K _z_zsh_tab_completion _z
  201. elif complete &> /dev/null; then
  202. # bash tab completion
  203. complete -o filenames -C '_z --complete "$COMP_LINE"' ${_Z_CMD:-z}
  204. [ "$_Z_NO_PROMPT_COMMAND" ] || {
  205. # bash populate directory list. avoid clobbering other PROMPT_COMMANDs.
  206. echo $PROMPT_COMMAND | grep -q "_z --add" || {
  207. PROMPT_COMMAND='_z --add "$(pwd '$_Z_RESOLVE_SYMLINKS' 2>/dev/null)" 2>/dev/null;'"$PROMPT_COMMAND"
  208. }
  209. }
  210. fi