Merge pull request #5296 from mcornella/fix-pj-plugin
Fix pj plugin
Diff
1diff --git a/plugins/pj/README.md b/plugins/pj/README.md
2new file mode 100644
3index 0000000000000000000000000000000000000000..27e5638ec67ce178f860c34dc981446c4a318961
4--- /dev/null
5+++ b/plugins/pj/README.md
6@@ -0,0 +1,45 @@
7+# pj
8+
9+The `pj` plugin (short for `Project Jump`) allows you to define several
10+folders where you store your projects, so that you can jump there directly
11+by just using the name of the project directory.
12+
13+Original idea and code by Jan De Poorter ([@DefV](https://github.com/DefV))
14+Source: https://gist.github.com/pjaspers/368394#gistcomment-1016
15+
16+## Usage
17+
18+1. Enable the `pj` plugin:
19+
20+ ```zsh
21+ plugins=(... pj)
22+ ```
23+
24+2. Set `$PROJECT_PATHS` in your ~/.zshrc:
25+
26+ ```zsh
27+ PROJECT_PATHS=(~/src ~/work ~/"dir with spaces")
28+ ```
29+
30+You can now use one of the following commands:
31+
32+##### `pj my-project`:
33+
34+`cd` to the directory named "my-project" found in one of the `$PROJECT_PATHS`
35+directories. If there are several directories named the same, the first one
36+to appear in `$PROJECT_PATHS` has preference.
37+
38+For example:
39+```zsh
40+PROJECT_PATHS=(~/code ~/work)
41+$ ls ~/code # ~/code/blog ~/code/react
42+$ ls ~/work # ~/work/blog ~/work/project
43+$ pj blog # <-- will cd to ~/code/blog
44+```
45+
46+##### `pjo my-project`
47+
48+Open the project directory with your defined `$EDITOR`. This follows the same
49+directory rules as the `pj` command above.
50+
51+Note: `pjo` is an alias of `pj open`.
52diff --git a/plugins/pj/pj.plugin.zsh b/plugins/pj/pj.plugin.zsh
53index 1572e9363402906636bcad77e77d61b103c77290..84d0cfa65849fc07a5a668e270b4728b711c75bd 100644
54--- a/plugins/pj/pj.plugin.zsh
55+++ b/plugins/pj/pj.plugin.zsh
56@@ -1,49 +1,42 @@
57-#!/bin/zsh
58-
59-#
60-# Original idea by DefV (Jan De Poorter)
61-# Source: https://gist.github.com/pjaspers/368394#comment-1016
62-#
63-# Usage:
64-# - Set `$PROJECT_PATHS` in your ~/.zshrc
65-# e.g.: PROJECT_PATHS=(~/src ~/work)
66-# - In ZSH you now can open a project directory with the command: `pj my-project`
67-# the plugin will locate the `my-project` directory in one of the $PROJECT_PATHS
68-# Also tab completion is supported.
69-# - `pjo my-project` will open the directory in $EDITOR
70-#
71-
72-function pj() {
73+alias pjo="pj open"
74+
75+pj () {
76+ emulate -L zsh
77+
78 cmd="cd"
79- file=$1
80+ project=$1
81 82- if [[ "open" == "$file" ]] then
83+ if [[ "open" == "$project" ]]; then
84 shift
85- file=$*
86- cmd=(${(s: :)EDITOR})
87+ project=$*
88+ cmd=${=EDITOR}
89 else
90- file=$*
91+ project=$*
92+ fi
93+
94+ if [[ -z "$project" ]]; then
95+ echo "You have to specify a project name."
96+ return
97 fi
98 99- for project in $PROJECT_PATHS; do
100- if [[ -d $project/$file ]] then
101- $cmd "$project/$file"
102- unset project # Unset project var
103+ for basedir ($PROJECT_PATHS); do
104+ if [[ -d "$basedir/$project" ]]; then
105+ $cmd "$basedir/$project"
106 return
107 fi
108 done
109110- echo "No such project $1"
111+ echo "No such project '${project}'."
112 }
113114-alias pjo="pj open"
115+_pj () {
116+ emulate -L zsh
117118-function _pj () {
119- # might be possible to improve this using glob, without the basename trick
120 typeset -a projects
121- projects=($PROJECT_PATHS/*)
122- projects=$projects:t
123- _arguments "*:file:($projects)"
124-}
125+ for basedir ($PROJECT_PATHS); do
126+ projects+=(${basedir}/*(/N))
127+ done
128129+ compadd ${projects:t}
130+}
131 compdef _pj pj