From 525ee5081583f5c7681104d21796e35fe41afd5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Wed, 10 Aug 2016 13:35:40 +0200 Subject: [PATCH 1/6] Clean up comments in pj plugin file and reorganise --- plugins/pj/pj.plugin.zsh | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/plugins/pj/pj.plugin.zsh b/plugins/pj/pj.plugin.zsh index 1572e936..8b1bc883 100644 --- a/plugins/pj/pj.plugin.zsh +++ b/plugins/pj/pj.plugin.zsh @@ -1,19 +1,6 @@ -#!/bin/zsh +alias pjo="pj open" -# -# Original idea by DefV (Jan De Poorter) -# Source: https://gist.github.com/pjaspers/368394#comment-1016 -# -# Usage: -# - Set `$PROJECT_PATHS` in your ~/.zshrc -# e.g.: PROJECT_PATHS=(~/src ~/work) -# - In ZSH you now can open a project directory with the command: `pj my-project` -# the plugin will locate the `my-project` directory in one of the $PROJECT_PATHS -# Also tab completion is supported. -# - `pjo my-project` will open the directory in $EDITOR -# - -function pj() { +function pj () { cmd="cd" file=$1 @@ -36,8 +23,6 @@ function pj() { echo "No such project $1" } -alias pjo="pj open" - function _pj () { # might be possible to improve this using glob, without the basename trick typeset -a projects @@ -45,5 +30,4 @@ function _pj () { projects=$projects:t _arguments "*:file:($projects)" } - compdef _pj pj From 26bef0942ba8acbe3ad2005500efd8f47e1edc36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Thu, 11 Aug 2016 01:19:19 +0200 Subject: [PATCH 2/6] Add Readme to pj plugin --- plugins/pj/README.md | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 plugins/pj/README.md diff --git a/plugins/pj/README.md b/plugins/pj/README.md new file mode 100644 index 00000000..27e5638e --- /dev/null +++ b/plugins/pj/README.md @@ -0,0 +1,45 @@ +# pj + +The `pj` plugin (short for `Project Jump`) allows you to define several +folders where you store your projects, so that you can jump there directly +by just using the name of the project directory. + +Original idea and code by Jan De Poorter ([@DefV](https://github.com/DefV)) +Source: https://gist.github.com/pjaspers/368394#gistcomment-1016 + +## Usage + +1. Enable the `pj` plugin: + + ```zsh + plugins=(... pj) + ``` + +2. Set `$PROJECT_PATHS` in your ~/.zshrc: + + ```zsh + PROJECT_PATHS=(~/src ~/work ~/"dir with spaces") + ``` + +You can now use one of the following commands: + +##### `pj my-project`: + +`cd` to the directory named "my-project" found in one of the `$PROJECT_PATHS` +directories. If there are several directories named the same, the first one +to appear in `$PROJECT_PATHS` has preference. + +For example: +```zsh +PROJECT_PATHS=(~/code ~/work) +$ ls ~/code # ~/code/blog ~/code/react +$ ls ~/work # ~/work/blog ~/work/project +$ pj blog # <-- will cd to ~/code/blog +``` + +##### `pjo my-project` + +Open the project directory with your defined `$EDITOR`. This follows the same +directory rules as the `pj` command above. + +Note: `pjo` is an alias of `pj open`. From c9c11d605f28c0a93ad34ba2a40120ea6e0f9dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Thu, 11 Aug 2016 01:35:45 +0200 Subject: [PATCH 3/6] Fix _pj completion function - `$PROJECT_PATHS/*` wasn't working correctly. You have to iterate over its elements in order to use globbing with it. - The `$projects:t` line wasn't necessary if we used `compadd`. - `compadd` better supports destructuring an array with spaces in some of its elements. --- plugins/pj/pj.plugin.zsh | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/plugins/pj/pj.plugin.zsh b/plugins/pj/pj.plugin.zsh index 8b1bc883..1d89af00 100644 --- a/plugins/pj/pj.plugin.zsh +++ b/plugins/pj/pj.plugin.zsh @@ -24,10 +24,13 @@ function pj () { } function _pj () { - # might be possible to improve this using glob, without the basename trick + emulate -L zsh + typeset -a projects - projects=($PROJECT_PATHS/*) - projects=$projects:t - _arguments "*:file:($projects)" + for basedir ($PROJECT_PATHS); do + projects+=(${basedir}/*(/N)) + done + + compadd ${projects:t} } compdef _pj pj From 7f8851f52f9ebb03bc5be0246ee2b591196bdd16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Thu, 11 Aug 2016 01:33:36 +0200 Subject: [PATCH 4/6] Refactor pj function - Use `emulate -L zsh` to make all variables local. - Use `shwordsplit` to interpret `$cmd` spaces correctly. - Rename `$project` and `$file` variables to the more appropriate `$basedir` and `$project`. --- plugins/pj/pj.plugin.zsh | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/plugins/pj/pj.plugin.zsh b/plugins/pj/pj.plugin.zsh index 1d89af00..f9d7f8e2 100644 --- a/plugins/pj/pj.plugin.zsh +++ b/plugins/pj/pj.plugin.zsh @@ -1,29 +1,31 @@ alias pjo="pj open" -function pj () { - cmd="cd" - file=$1 +pj () { + emulate -L zsh + setopt shwordsplit - if [[ "open" == "$file" ]] then + cmd="cd" + project=$1 + + if [[ "open" == "$project" ]]; then shift - file=$* - cmd=(${(s: :)EDITOR}) + project=$* + cmd=$EDITOR else - file=$* + project=$* fi - for project in $PROJECT_PATHS; do - if [[ -d $project/$file ]] then - $cmd "$project/$file" - unset project # Unset project var + for basedir ($PROJECT_PATHS); do + if [[ -d "$basedir/$project" ]]; then + $cmd "$basedir/$project" return fi done - echo "No such project $1" + echo "No such project '${project}'." } -function _pj () { +_pj () { emulate -L zsh typeset -a projects From 7d298a3059f4df72866b4ef649675b594120f453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Thu, 11 Aug 2016 01:44:14 +0200 Subject: [PATCH 5/6] Fix pj() function when no project has been specified --- plugins/pj/pj.plugin.zsh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugins/pj/pj.plugin.zsh b/plugins/pj/pj.plugin.zsh index f9d7f8e2..bb3d9c05 100644 --- a/plugins/pj/pj.plugin.zsh +++ b/plugins/pj/pj.plugin.zsh @@ -15,6 +15,11 @@ pj () { project=$* fi + if [[ -z "$project" ]]; then + echo "You have to specify a project name." + return + fi + for basedir ($PROJECT_PATHS); do if [[ -d "$basedir/$project" ]]; then $cmd "$basedir/$project" From bec53135e4e826e204c3d11df9854a53403d11e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Thu, 11 Aug 2016 01:55:38 +0200 Subject: [PATCH 6/6] Fix shwordsplit bug when a basedir contains spaces The `shwordsplit` option affects all variables and we only need to split the `$EDITOR` variable. Because of that, using `${=spec}` is a much better alternative. More info at http://zsh.sourceforge.net/Doc/Release/Expansion.html#index-SH_005fWORD_005fSPLIT_002c-toggle --- plugins/pj/pj.plugin.zsh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/pj/pj.plugin.zsh b/plugins/pj/pj.plugin.zsh index bb3d9c05..84d0cfa6 100644 --- a/plugins/pj/pj.plugin.zsh +++ b/plugins/pj/pj.plugin.zsh @@ -2,7 +2,6 @@ alias pjo="pj open" pj () { emulate -L zsh - setopt shwordsplit cmd="cd" project=$1 @@ -10,7 +9,7 @@ pj () { if [[ "open" == "$project" ]]; then shift project=$* - cmd=$EDITOR + cmd=${=EDITOR} else project=$* fi