85d949550bdefe96fa7cb371c6e0fa5886c63f74
- Author
- Marc Cornellà <marc.cornella@live.com>
- Committer
- Marc Cornellà <marc.cornella@live.com>
- Date
Message
Diff
This diff is truncated to protect this page.
1diff --git a/plugins/z/README b/plugins/z/README
2index ec5abc6f53d5d02f0cf131137afbfbfb69e504af..7de82a4c7a506f4a2764e6cf048aba7f7673eeeb 100644
3--- a/plugins/z/README
4+++ b/plugins/z/README
5@@ -6,7 +6,7 @@ NAME
6 z - jump around
7
8 SYNOPSIS
9- z [-chlrt] [regex1 regex2 ... regexn]
10+ z [-chlrtx] [regex1 regex2 ... regexn]
11
12 AVAILABILITY
13 bash, zsh
14@@ -15,10 +15,13 @@ DESCRIPTION
15 Tracks your most used directories, based on 'frecency'.
16
17 After a short learning phase, z will take you to the most 'frecent'
18- directory that matches ALL of the regexes given on the command line.
19+ directory that matches ALL of the regexes given on the command line, in
20+ order.
21+
22+ For example, z foo bar would match /foo/bar but not /bar/foo.
23
24 OPTIONS
25- -c restrict matches to subdirectories of the current directory.
26+ -c restrict matches to subdirectories of the current directory
27
28 -h show a brief help message
29
30@@ -28,10 +31,12 @@ OPTIONS
31
32 -t match by recent access only
33
34+ -x remove the current directory from the datafile
35+
36 EXAMPLES
37 z foo cd to most frecent dir matching foo
38
39- z foo bar cd to most frecent dir matching foo and bar
40+ z foo bar cd to most frecent dir matching foo, then bar
41
42 z -r foo cd to highest ranked dir matching foo
43
44@@ -55,8 +60,9 @@ NOTES
45 Set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution.
46 Set $_Z_NO_PROMPT_COMMAND to handle PROMPT_COMMAND/precmd your-
47 self.
48- Set $_Z_EXCLUDE_DIRS to an array of directories to exclude.
49- (These settings should go in .bashrc/.zshrc before the lines
50+ Set $_Z_EXCLUDE_DIRS to an array of directory trees to exclude.
51+ Set $_Z_OWNER to allow usage when in 'sudo -s' mode.
52+ (These settings should go in .bashrc/.zshrc before the line
53 added above.)
54 Install the provided man page z.1 somewhere like
55 /usr/local/man/man1.
56@@ -64,12 +70,12 @@ NOTES
57 Aging:
58 The rank of directories maintained by z undergoes aging based on a sim-
59 ple formula. The rank of each entry is incremented every time it is
60- accessed. When the sum of ranks is greater than 6000, all ranks are
61- multiplied by 0.99. Entries with a rank lower than 1 are forgotten.
62+ accessed. When the sum of ranks is over 9000, all ranks are multiplied
63+ by 0.99. Entries with a rank lower than 1 are forgotten.
64
65 Frecency:
66- Frecency is a portmantaeu of 'recent' and 'frequency'. It is a weighted
67- rank that depends on how often and how recently something occured. As
68+ Frecency is a portmanteau of 'recent' and 'frequency'. It is a weighted
69+ rank that depends on how often and how recently something occurred. As
70 far as I know, Mozilla came up with the term.
71
72 To z, a directory that has low ranking but has been accessed recently
73@@ -107,20 +113,23 @@ ENVIRONMENT
74 resolving of symlinks. If it is not set, symbolic links will be
75 resolved when added to the datafile.
76
77- In bash, z prepends a command to the PROMPT_COMMAND environment vari-
78- able to maintain its database. In zsh, z appends a function _z_precmd
79- to the precmd_functions array.
80+ In bash, z appends a command to the PROMPT_COMMAND environment variable
81+ to maintain its database. In zsh, z appends a function _z_precmd to the
82+ precmd_functions array.
83
84 The environment variable $_Z_NO_PROMPT_COMMAND can be set if you want
85 to handle PROMPT_COMMAND or precmd yourself.
86
87 The environment variable $_Z_EXCLUDE_DIRS can be set to an array of
88- directories to exclude from tracking. $HOME is always excluded. Direc-
89- tories must be full paths without trailing slashes.
90+ directory trees to exclude from tracking. $HOME is always excluded.
91+ Directories must be full paths without trailing slashes.
92+
93+ The environment variable $_Z_OWNER can be set to your username, to
94+ allow usage of z when your sudo enviroment keeps $HOME set.
95
96 FILES
97- Data is stored in $HOME/.z. This can be overridden by setting the
98- $_Z_DATA environment variable. When initialized, z will raise an error
99+ Data is stored in $HOME/.z. This can be overridden by setting the
100+ $_Z_DATA environment variable. When initialized, z will raise an error
101 if this path is a directory, and not function correctly.
102
103 A man page (z.1) is provided.
104diff --git a/plugins/z/z.1 b/plugins/z/z.1
105index 022a4b35d3121c32d88209a52bc0db0c4ba93be6..cc99910bf6cf1e946b2987303b6a695377b352b7 100644
106--- a/plugins/z/z.1
107+++ b/plugins/z/z.1
108@@ -4,7 +4,7 @@ NAME
109 z \- jump around
110 .SH
111 SYNOPSIS
112-z [\-chlrt] [regex1 regex2 ... regexn]
113+z [\-chlrtx] [regex1 regex2 ... regexn]
114 .SH
115 AVAILABILITY
116 bash, zsh
117@@ -13,12 +13,14 @@ DESCRIPTION
118 Tracks your most used directories, based on 'frecency'.
119 .P
120 After a short learning phase, \fBz\fR will take you to the most 'frecent'
121-directory that matches ALL of the regexes given on the command line.
122+directory that matches ALL of the regexes given on the command line, in order.
123+
124+For example, \fBz foo bar\fR would match \fB/foo/bar\fR but not \fB/bar/foo\fR.
125 .SH
126 OPTIONS
127 .TP
128 \fB\-c\fR
129-restrict matches to subdirectories of the current directory.
130+restrict matches to subdirectories of the current directory
131 .TP
132 \fB\-h\fR
133 show a brief help message
134@@ -31,13 +33,16 @@ match by rank only
135 .TP
136 \fB\-t\fR
137 match by recent access only
138+.TP
139+\fB\-x\fR
140+remove the current directory from the datafile
141 .SH EXAMPLES
142 .TP 14
143 \fBz foo\fR
144 cd to most frecent dir matching foo
145 .TP 14
146 \fBz foo bar\fR
147-cd to most frecent dir matching foo and bar
148+cd to most frecent dir matching foo, then bar
149 .TP 14
150 \fBz -r foo\fR
151 cd to highest ranked dir matching foo
152@@ -76,10 +81,13 @@ Set \fB$_Z_NO_RESOLVE_SYMLINKS\fR to prevent symlink resolution.
153 Set \fB$_Z_NO_PROMPT_COMMAND\fR to handle \fBPROMPT_COMMAND/precmd\fR yourself.
154 .RE
155 .RS
156-Set \fB$_Z_EXCLUDE_DIRS\fR to an array of directories to exclude.
157+Set \fB$_Z_EXCLUDE_DIRS\fR to an array of directory trees to exclude.
158+.RE
159+.RS
160+Set \fB$_Z_OWNER\fR to allow usage when in 'sudo -s' mode.
161 .RE
162 .RS
163-(These settings should go in .bashrc/.zshrc before the lines added above.)
164+(These settings should go in .bashrc/.zshrc before the line added above.)
165 .RE
166 .RS
167 Install the provided man page \fBz.1\fR somewhere like \fB/usr/local/man/man1\fR.
168@@ -88,12 +96,12 @@ Install the provided man page \fBz.1\fR somewhere like \fB/usr/local/man/man1\fR
169 Aging:
170 The rank of directories maintained by \fBz\fR undergoes aging based on a simple
171 formula. The rank of each entry is incremented every time it is accessed. When
172-the sum of ranks is greater than 6000, all ranks are multiplied by 0.99. Entries
173-with a rank lower than 1 are forgotten.
174+the sum of ranks is over 9000, all ranks are multiplied by 0.99. Entries with a
175+rank lower than 1 are forgotten.
176 .SS
177 Frecency:
178-Frecency is a portmantaeu of 'recent' and 'frequency'. It is a weighted rank
179-that depends on how often and how recently something occured. As far as I
180+Frecency is a portmanteau of 'recent' and 'frequency'. It is a weighted rank
181+that depends on how often and how recently something occurred. As far as I
182 know, Mozilla came up with the term.
183 .P
184 To \fBz\fR, a directory that has low ranking but has been accessed recently
185@@ -131,7 +139,7 @@ The environment variable \fB$_Z_NO_RESOLVE_SYMLINKS\fR can be set to prevent
186 resolving of symlinks. If it is not set, symbolic links will be resolved when
187 added to the datafile.
188 .P
189-In bash, \fBz\fR prepends a command to the \fBPROMPT_COMMAND\fR environment
190+In bash, \fBz\fR appends a command to the \fBPROMPT_COMMAND\fR environment
191 variable to maintain its database. In zsh, \fBz\fR appends a function
192 \fB_z_precmd\fR to the \fBprecmd_functions\fR array.
193 .P
194@@ -139,8 +147,11 @@ The environment variable \fB$_Z_NO_PROMPT_COMMAND\fR can be set if you want to
195 handle \fBPROMPT_COMMAND\fR or \fBprecmd\fR yourself.
196 .P
197 The environment variable \fB$_Z_EXCLUDE_DIRS\fR can be set to an array of
198-directories to exclude from tracking. \fB$HOME\fR is always excluded.
199+directory trees to exclude from tracking. \fB$HOME\fR is always excluded.
200 Directories must be full paths without trailing slashes.
201+.P
202+The environment variable \fB$_Z_OWNER\fR can be set to your username, to
203+allow usage of \fBz\fR when your sudo enviroment keeps \fB$HOME\fR set.
204 .SH
205 FILES
206 Data is stored in \fB$HOME/.z\fR. This can be overridden by setting the
207diff --git a/plugins/z/z.sh b/plugins/z/z.sh
208index 7e444ef4690363187c2c350a9dd63f10c076c63f..d0eeb97ef62a42dd939a3fc2f92c2ac632566a98 100644
209--- a/plugins/z/z.sh
210+++ b/plugins/z/z.sh
211@@ -3,29 +3,25 @@
212 # maintains a jump-list of the directories you actually use
213 #
214 # INSTALL:
215-# * put something like this in your .bashrc/.zshrc:
216-# . /path/to/z.sh
217-# * cd around for a while to build up the db
218-# * PROFIT!!
219-# * optionally:
220-# set $_Z_CMD in .bashrc/.zshrc to change the command (default z).
221-# set $_Z_DATA in .bashrc/.zshrc to change the datafile (default ~/.z).
222-# set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution.
223-# set $_Z_NO_PROMPT_COMMAND if you're handling PROMPT_COMMAND yourself.
224-# set $_Z_EXCLUDE_DIRS to an array of directories to exclude.
225+# * put something like this in your .bashrc/.zshrc:
226+# . /path/to/z.sh
227+# * cd around for a while to build up the db
228+# * PROFIT!!
229+# * optionally:
230+# set $_Z_CMD in .bashrc/.zshrc to change the command (default z).
231+# set $_Z_DATA in .bashrc/.zshrc to change the datafile (default ~/.z).
232+# set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution.
233+# set $_Z_NO_PROMPT_COMMAND if you're handling PROMPT_COMMAND yourself.
234+# set $_Z_EXCLUDE_DIRS to an array of directories to exclude.
235+# set $_Z_OWNER to your username if you want use z while sudo with $HOME kept
236 #
237 # USE:
238-# * z foo # cd to most frecent dir matching foo
239-# * z foo bar # cd to most frecent dir matching foo and bar
240-# * z -r foo # cd to highest ranked dir matching foo
241-# * z -t foo # cd to most recently accessed dir matching foo
242-# * z -l foo # list matches instead of cd
243-# * z -c foo # restrict matches to subdirs of $PWD
244-
245-case $- in
246- *i*) ;;
247- *) echo 'ERROR: z.sh is meant to be sourced, not directly executed.'
248-esac
249+# * z foo # cd to most frecent dir matching foo
250+# * z foo bar # cd to most frecent dir matching foo and bar
251+# * z -r foo # cd to highest ranked dir matching foo
252+# * z -t foo # cd to most recently accessed dir matching foo
253+# * z -l foo # list matches instead of cd
254+# * z -c foo # restrict matches to subdirs of $PWD
255
256 [ -d "${_Z_DATA:-$HOME/.z}" ] && {
257 echo "ERROR: z.sh's datafile (${_Z_DATA:-$HOME/.z}) is a directory."
258@@ -33,196 +29,215 @@ esac
259
260 _z() {
261
262- local datafile="${_Z_DATA:-$HOME/.z}"
263-
264- # bail out if we don't own ~/.z (we're another user but our ENV is still set)
265- [ -f "$datafile" -a ! -O "$datafile" ] && return
266-
267- # add entries
268- if [ "$1" = "--add" ]; then
269- shift
270-
271- # $HOME isn't worth matching
272- [ "$*" = "$HOME" ] && return
273-
274- # don't track excluded dirs
275- local exclude
276- for exclude in "${_Z_EXCLUDE_DIRS[@]}"; do
277- [ "$*" = "$exclude" ] && return
278- done
279-
280- # maintain the file
281- local tempfile
282- tempfile="$(mktemp "$datafile.XXXXXX")" || return
283- while read line; do
284- [ -d "${line%%\|*}" ] && echo $line
285- done < "$datafile" | awk -v path="$*" -v now="$(date +%s)" -F"|" '
286- BEGIN {
287- rank[path] = 1
288- time[path] = now
289- }
290- $2 >= 1 {
291- if( $1 == path ) {
292- rank[$1] = $2 + 1
293- time[$1] = now
294- } else {
295- rank[$1] = $2
296- time[$1] = $3
297- }
298- count += $2
299- }
300- END {
301- if( count > 6000 ) {
302- for( i in rank ) print i "|" 0.99*rank[i] "|" time[i] # aging
303- } else for( i in rank ) print i "|" rank[i] "|" time[i]
304- }
305- ' 2>/dev/null >| "$tempfile"
306- if [ $? -ne 0 -a -f "$datafile" ]; then
307- env rm -f "$tempfile"
308- else
309- env mv -f "$tempfile" "$datafile"
310- fi