a24a0b1a8e464fd4c6ab6c2f95ef5a8f025ff093
- Author
- MarkPochert <MarkPochert@users.noreply.github.com>
- Committer
- Marc Cornellà <marc.cornella@live.com>
- Date
Message
Diff
This diff is truncated to protect this page.
1diff --git a/plugins/z/z.1 b/plugins/z/z.1
2index bbc1bf5df046a4cd761b4d6d9209f4b06926c45d..d4cac1ac27c31732b5ac669a7544222f2a754f80 100644
3--- a/plugins/z/z.1
4+++ b/plugins/z/z.1
5@@ -22,6 +22,9 @@ OPTIONS
6 \fB\-c\fR
7 restrict matches to subdirectories of the current directory
8 .TP
9+\fB\-e\fR
10+echo the best match, don't cd
11+.TP
12 \fB\-h\fR
13 show a brief help message
14 .TP
15@@ -90,7 +93,8 @@ Set \fB$_Z_OWNER\fR to allow usage when in 'sudo -s' mode.
16 (These settings should go in .bashrc/.zshrc before the line added above.)
17 .RE
18 .RS
19-Install the provided man page \fBz.1\fR somewhere like \fB/usr/local/man/man1\fR.
20+Install the provided man page \fBz.1\fR somewhere in your \f$MANPATH, like
21+\fB/usr/local/man/man1\fR.
22 .RE
23 .SS
24 Aging:
25diff --git a/plugins/z/z.sh b/plugins/z/z.sh
26index d0eeb97ef62a42dd939a3fc2f92c2ac632566a98..4fc75dc6af295218d0872d3e96d3912f436ddf1e 100644
27--- a/plugins/z/z.sh
28+++ b/plugins/z/z.sh
29@@ -1,4 +1,4 @@
30-# Copyright (c) 2009 rupa deadwyler under the WTFPL license
31+# Copyright (c) 2009 rupa deadwyler. Licensed under the WTFPL license, Version 2
32
33 # maintains a jump-list of the directories you actually use
34 #
35@@ -21,6 +21,7 @@
36 # * z -r foo # cd to highest ranked dir matching foo
37 # * z -t foo # cd to most recently accessed dir matching foo
38 # * z -l foo # list matches instead of cd
39+# * z -e foo # echo the best match, don't cd
40 # * z -c foo # restrict matches to subdirs of $PWD
41
42 [ -d "${_Z_DATA:-$HOME/.z}" ] && {
43@@ -31,9 +32,21 @@ _z() {
44
45 local datafile="${_Z_DATA:-$HOME/.z}"
46
47+ # if symlink, dereference
48+ [ -h "$datafile" ] && datafile=$(readlink "$datafile")
49+
50 # bail if we don't own ~/.z and $_Z_OWNER not set
51 [ -z "$_Z_OWNER" -a -f "$datafile" -a ! -O "$datafile" ] && return
52
53+ _z_dirs () {
54+ local line
55+ while read line; do
56+ # only count directories
57+ [ -d "${line%%\|*}" ] && echo "$line"
58+ done < "$datafile"
59+ return 0
60+ }
61+
62 # add entries
63 if [ "$1" = "--add" ]; then
64 shift
65@@ -49,10 +62,7 @@ _z() {
66
67 # maintain the data file
68 local tempfile="$datafile.$RANDOM"
69- while read line; do
70- # only count directories
71- [ -d "${line%%\|*}" ] && echo $line
72- done < "$datafile" | awk -v path="$*" -v now="$(date +%s)" -F"|" '
73+ _z_dirs | awk -v path="$*" -v now="$(date +%s)" -F"|" '
74 BEGIN {
75 rank[path] = 1
76 time[path] = now
77@@ -75,7 +85,7 @@ _z() {
78 } else for( x in rank ) print x "|" rank[x] "|" time[x]
79 }
80 ' 2>/dev/null >| "$tempfile"
81- # do our best to avoid clobbering the datafile in a race condition
82+ # do our best to avoid clobbering the datafile in a race condition.
83 if [ $? -ne 0 -a -f "$datafile" ]; then
84 env rm -f "$tempfile"
85 else
86@@ -85,17 +95,15 @@ _z() {
87
88 # tab completion
89 elif [ "$1" = "--complete" -a -s "$datafile" ]; then
90- while read line; do
91- [ -d "${line%%\|*}" ] && echo $line
92- done < "$datafile" | awk -v q="$2" -F"|" '
93+ _z_dirs | awk -v q="$2" -F"|" '
94 BEGIN {
95- if( q == tolower(q) ) imatch = 1
96 q = substr(q, 3)
97- gsub(" ", ".*", q)
98+ if( q == tolower(q) ) imatch = 1
99+ gsub(/ /, ".*", q)
100 }
101 {
102 if( imatch ) {
103- if( tolower($1) ~ tolower(q) ) print $1
104+ if( tolower($1) ~ q ) print $1
105 } else if( $1 ~ q ) print $1
106 }
107 ' 2>/dev/null
108@@ -106,11 +114,12 @@ _z() {
109 --) while [ "$1" ]; do shift; local fnd="$fnd${fnd:+ }$1";done;;
110 -*) local opt=${1:1}; while [ "$opt" ]; do case ${opt:0:1} in
111 c) local fnd="^$PWD $fnd";;
112- h) echo "${_Z_CMD:-z} [-chlrtx] args" >&2; return;;
113- x) sed -i -e "\:^${PWD}|.*:d" "$datafile";;
114+ e) local echo=1;;
115+ h) echo "${_Z_CMD:-z} [-cehlrtx] args" >&2; return;;
116 l) local list=1;;
117 r) local typ="rank";;
118 t) local typ="recent";;
119+ x) sed -i -e "\:^${PWD}|.*:d" "$datafile";;
120 esac; opt=${opt:1}; done;;
121 *) local fnd="$fnd${fnd:+ }$1";;
122 esac; local last=$1; [ "$#" -gt 0 ] && shift; done
123@@ -119,16 +128,14 @@ _z() {
124 # if we hit enter on a completion just go there
125 case "$last" in
126 # completions will always start with /
127- /*) [ -z "$list" -a -d "$last" ] && cd "$last" && return;;
128+ /*) [ -z "$list" -a -d "$last" ] && builtin cd "$last" && return;;