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