Mercurial > dotfiles.old
comparison zsh/z.sh @ 151:87498dbd779a
Move away from oh-my-zsh
author | zegervdv <zegervdv@me.com> |
---|---|
date | Mon, 20 Oct 2014 21:49:21 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
150:de8d4246c8fe | 151:87498dbd779a |
---|---|
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 > 9000 ) { | |
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 [ "$_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 q = substr(q, 3) | |
94 gsub(" ", ".*", q) | |
95 } | |
96 { | |
97 if( imatch ) { | |
98 if( tolower($1) ~ tolower(q) ) print $1 | |
99 } else if( $1 ~ q ) print $1 | |
100 } | |
101 ' 2>/dev/null | |
102 | |
103 else | |
104 # list/go | |
105 while [ "$1" ]; do case "$1" in | |
106 --) while [ "$1" ]; do shift; local fnd="$fnd${fnd:+ }$1";done;; | |
107 -*) local opt=${1:1}; while [ "$opt" ]; do case ${opt:0:1} in | |
108 c) local fnd="^$PWD $fnd";; | |
109 h) echo "${_Z_CMD:-z} [-chlrtx] args" >&2; return;; | |
110 x) sed -i -e "\:^${PWD}|.*:d" "$datafile";; | |
111 l) local list=1;; | |
112 r) local typ="rank";; | |
113 t) local typ="recent";; | |
114 esac; opt=${opt:1}; done;; | |
115 *) local fnd="$fnd${fnd:+ }$1";; | |
116 esac; local last=$1; shift; done | |
117 [ "$fnd" -a "$fnd" != "^$PWD " ] || local list=1 | |
118 | |
119 # if we hit enter on a completion just go there | |
120 case "$last" in | |
121 # completions will always start with / | |
122 /*) [ -z "$list" -a -d "$last" ] && cd "$last" && return;; | |
123 esac | |
124 | |
125 # no file yet | |
126 [ -f "$datafile" ] || return | |
127 | |
128 local cd | |
129 cd="$(while read line; do | |
130 [ -d "${line%%\|*}" ] && echo $line | |
131 done < "$datafile" | awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" ' | |
132 function frecent(rank, time) { | |
133 # relate frequency and time | |
134 dx = t - time | |
135 if( dx < 3600 ) return rank * 4 | |
136 if( dx < 86400 ) return rank * 2 | |
137 if( dx < 604800 ) return rank / 2 | |
138 return rank / 4 | |
139 } | |
140 function output(files, out, common) { | |
141 # list or return the desired directory | |
142 if( list ) { | |
143 cmd = "sort -n >&2" | |
144 for( x in files ) { | |
145 if( files[x] ) printf "%-10s %s\n", files[x], x | cmd | |
146 } | |
147 if( common ) { | |
148 printf "%-10s %s\n", "common:", common > "/dev/stderr" | |
149 } | |
150 } else { | |
151 if( common ) out = common | |
152 print out | |
153 } | |
154 } | |
155 function common(matches) { | |
156 # find the common root of a list of matches, if it exists | |
157 for( x in matches ) { | |
158 if( matches[x] && (!short || length(x) < length(short)) ) { | |
159 short = x | |
160 } | |
161 } | |
162 if( short == "/" ) return | |
163 # use a copy to escape special characters, as we want to return | |
164 # the original. yeah, this escaping is awful. | |
165 clean_short = short | |
166 gsub(/[\(\)\[\]\|]/, "\\\\&", clean_short) | |
167 for( x in matches ) if( matches[x] && x !~ clean_short ) return | |
168 return short | |
169 } | |
170 BEGIN { | |
171 gsub(" ", ".*", q) | |
172 hi_rank = ihi_rank = -9999999999 | |
173 } | |
174 { | |
175 if( typ == "rank" ) { | |
176 rank = $2 | |
177 } else if( typ == "recent" ) { | |
178 rank = $3 - t | |
179 } else rank = frecent($2, $3) | |
180 if( $1 ~ q ) { | |
181 matches[$1] = rank | |
182 } else if( tolower($1) ~ tolower(q) ) imatches[$1] = rank | |
183 if( matches[$1] && matches[$1] > hi_rank ) { | |
184 best_match = $1 | |
185 hi_rank = matches[$1] | |
186 } else if( imatches[$1] && imatches[$1] > ihi_rank ) { | |
187 ibest_match = $1 | |
188 ihi_rank = imatches[$1] | |
189 } | |
190 } | |
191 END { | |
192 # prefer case sensitive | |
193 if( best_match ) { | |
194 output(matches, best_match, common(matches)) | |
195 } else if( ibest_match ) { | |
196 output(imatches, ibest_match, common(imatches)) | |
197 } | |
198 } | |
199 ')" | |
200 [ $? -gt 0 ] && return | |
201 [ "$cd" ] && cd "$cd" | |
202 fi | |
203 } | |
204 | |
205 alias ${_Z_CMD:-z}='_z 2>&1' | |
206 | |
207 [ "$_Z_NO_RESOLVE_SYMLINKS" ] || _Z_RESOLVE_SYMLINKS="-P" | |
208 | |
209 if compctl >/dev/null 2>&1; then | |
210 # zsh | |
211 [ "$_Z_NO_PROMPT_COMMAND" ] || { | |
212 # populate directory list, avoid clobbering any other precmds. | |
213 if [ "$_Z_NO_RESOLVE_SYMLINKS" ]; then | |
214 _z_precmd() { | |
215 _z --add "${PWD:a}" | |
216 } | |
217 else | |
218 _z_precmd() { | |
219 _z --add "${PWD:A}" | |
220 } | |
221 fi | |
222 [[ -n "${precmd_functions[(r)_z_precmd]}" ]] || { | |
223 precmd_functions[$(($#precmd_functions+1))]=_z_precmd | |
224 } | |
225 } | |
226 _z_zsh_tab_completion() { | |
227 # tab completion | |
228 local compl | |
229 read -l compl | |
230 reply=(${(f)"$(_z --complete "$compl")"}) | |
231 } | |
232 compctl -U -K _z_zsh_tab_completion _z | |
233 elif complete >/dev/null 2>&1; then | |
234 # bash | |
235 # tab completion | |
236 complete -o filenames -C '_z --complete "$COMP_LINE"' ${_Z_CMD:-z} | |
237 [ "$_Z_NO_PROMPT_COMMAND" ] || { | |
238 # populate directory list. avoid clobbering other PROMPT_COMMANDs. | |
239 grep "_z --add" <<< "$PROMPT_COMMAND" >/dev/null || { | |
240 PROMPT_COMMAND="$PROMPT_COMMAND"$'\n''_z --add "$(command pwd '$_Z_RESOLVE_SYMLINKS' 2>/dev/null)" 2>/dev/null;' | |
241 } | |
242 } | |
243 fi |