From 2de13ef932c4b7f56517af46dc851758b9341d21 Mon Sep 17 00:00:00 2001 From: Mike Dacre Date: Fri, 8 Aug 2014 13:09:31 -0700 Subject: [PATCH 1/5] Made plugin more compatible with oh-my-zsh --- plugins/cdbk/cdbk.plugin.zsh | 238 +++++++++++++++++++++++++++++++++++ 1 file changed, 238 insertions(+) create mode 100644 plugins/cdbk/cdbk.plugin.zsh diff --git a/plugins/cdbk/cdbk.plugin.zsh b/plugins/cdbk/cdbk.plugin.zsh new file mode 100644 index 000000000..798bf5436 --- /dev/null +++ b/plugins/cdbk/cdbk.plugin.zsh @@ -0,0 +1,238 @@ +# ----------------------------------------------------------------------------- +# +# Program: cdbk.zsh (cd bookmarking for zsh) +# +# Usage: cdbk {-a,-r,-d} [path] (run with no paramaters for info) +# +# Requirements: Needs to be sourced from a zsh startup file, or use oh-my-zsh +# +# Decription: cdbk() is a simple zsh function to make management of zsh +# named directories easier. It keeps all named directories in a +# file and uses grep, sed, echo, and perl to parse and modify +# this file in order to add, change or remove bookmarks. +# +# Because it uses the zsh named directory function, full zsh path +# completion is possible. Further, very simple cdbk completion is +# included to make replacing and deleting bookmarks easier. +# +# This program was heavily inspired by Stan Angeloff's function +# of the same name found here: +# http://blog.angeloff.name/post/1027007406/cd-with-bookmarks-and-auto-completion-for-zsh +# +# This file also provides the function folder_name(), which returns +# a formatted list of the names of the current folder for use in +# a prompt. To include the folder name in your prompt use e.g.: +# export PROMPT=$PROMPT $(folder_name) +# +# Bugs: None that I know of +# +# Created by: Mike Dacre +# Created on: 19-11-11 +# +# Revision #: 0.9.6-beta +# Updated: 23-11-11 10:11:27 +# +# License: MIT License - Open Source. Use as you wish +# +# ----------------------------------------------------------------------------- + +# Define location of bookmark file and source it every time this file is sourced +ZSH_BOOKMARKS="$HOME/.zshbookmarks"; + +if [ -e $ZSH_BOOKMARKS ]; then + source $ZSH_BOOKMARKS; +else + touch $ZSH_BOOKMARKS; +fi + +# ---------------------- +# Main Function +# ---------------------- +function cdbk () { + source $ZSH_BOOKMARKS; + + # Create local variables for function and global bkmk functions + local BKMKNAME; + local BKMKPATH; + local MYPATH; + CURBKMKS=(`grep -e "^hash -d" $ZSH_BOOKMARKS | sed 's#hash -d ##' | sed 's#=\(.*\)# \1#' `) + GLBLBKMKS=(`grep -e "^ *hash -d" $HOME/.zshrc | sed 's#hash -d ##' | sed 's#=\(.*\)# \1#' `) + + local GLBLTEST=GLBLBKMKS; + local HOSTTEST=HOSTBKMKS; + + # Define usage + local USAGE="------------------------------------------------------------------------------- + cdbk is a simple management tool for the built in zsh named directories + + cdbk -a [] : Create bookmark (uses current dir if no path) + cdbk -r [] : Replace bookmark (uses current dir if no path) + cdbk -d : Delete bookmark\n\n"; + + # Check first if bookmark file exists + if [[ ! -e $ZSH_BOOKMARKS ]]; then + touch $ZSH_BOOKMARKS; + printf "Bookmark file %s created\n" $ZSH_BOOKMARKS; + fi + + # Check if there are enough arguments + if [[ $# -lt 2 ]]; then + + # If no arguments, display all bookmarks and brief help + printf "$USAGE Current bookmarks:\n"; + print -aC 2 ${(kv)CURBKMKS} | sed 's/^/ /' | sort; + if [ $GLBLTEST ]; then + printf "\n Global Bookmarks:\n"; + print -aC 2 ${(kv)GLBLBKMKS} | sed 's/^/ /' | sort; + fi + if [ $HOSTTEST ]; then + printf "\n Host Specific Global Bookmarks:\n"; + print -aC 2 ${(kv)HOSTBKMKS} | sed 's/^/ /' | sort; + fi + printf "\n--------------------------------------------------------------------------------\n"; + + else + + # Look for existing version of query + BKMKNAME=$(grep "hash -d $2=" "$ZSH_BOOKMARKS" | sed 's#^hash -d ##' | sed 's#=.*$##'); + BKMKPATH=$(grep "hash -d $2=" "$ZSH_BOOKMARKS" | sed 's#^hash -d [^=]*=##'); + + # Add new bookmark + if [[ $1 == "-a" ]]; then + + # Check if path included, if not use current working directory + if [[ $# -eq 3 ]]; then + MYPATH=$3; + else + MYPATH=$PWD; + fi + + # Check that bookmark isn't in either global file + if [ $GLBLNAME ]; then + printf "Bookmark %s is already taken by global bookmark:\n %s\n\nYou need to remove this manually first.\n" $2 $GLBLNAME; + else + + # Check that name isn't already taken + if [ $BKMKNAME ]; then + printf "Bookmark %s is already taken (at %s), please use cdbk -r %s to replace.\n" $2 $BKMKPATH $2; + else + + # Check that path provided is actually a valid directory + if [ -d $MYPATH ]; then + + # Write bookmark to file, and do once off creation (will show error on fail) + echo "hash -d $2=$MYPATH" >> $ZSH_BOOKMARKS; + hash -d $2=$MYPATH; + printf "Bookmark %s created for %s\n" $2 $MYPATH; + else + printf "%s is not a valid path, please double-check\n" $3; + fi + fi + fi + + # Replace entry + elif [[ $1 == "-r" ]]; then + + # Check if path included, if not use current working directory + if [[ $# -eq 3 ]]; then + MYPATH=$3; + else + MYPATH=$PWD; + fi + + # Check that bookmark isn't in either global file + if [ $GLBLNAME ]; then + printf "Bookmark %s is a global bookmark and cannot be replaced:\n %s\n\nYou need to remove this manually first.\n" $2 $GLBLNAME; + else + + # Check that name definitely exists before proceeding + if [ $BKMKNAME ]; then + + # Check that path provided is actually a valid directory + if [ -d $MYPATH ]; then + + # Remove bookmark entry with perl and do once off manual rehash + unhash -d $2; + perl -pi -e "s#(hash -d $2=).*#\$1$MYPATH#g" $ZSH_BOOKMARKS; + hash -d $2=$MYPATH; + printf "Changed %s from %s to %s\n" $2 $BKMKPATH $MYPATH; + fi + + # If bookmark doesn't already exist, then just create a new one + else + + # Check that path provided is actually a valid directory + if [ -d $MYPATH ]; then + echo "hash -d $2=$MYPATH" >> $ZSH_BOOKMARKS; + hash -d $2=$MYPATH; + printf "Can't replace, because %s isn't in the bookmark file! Creating new...\n\n" $2; + printf "Bookmark %s created for %s\n" $2 $MYPATH; + else + printf "%s is not a valid directory, and %s isn't already in bookmark file\n" $MYPATH $2; + fi + fi + fi + + # Delete unwanted entry + elif [[ $1 == "-d" ]]; then + + # Check that name definitely exists before proceeding + if [ $BKMKNAME ]; then + + while true; do + echo "Do you really want to delete $2? (Y/n)"; + read YN_CHOICE; + case $YN_CHOICE in + [Yy]* ) # Remove bookmark entry with perl and do once off manual unhash + perl -pi -e "s#^hash -d $2=.*[\n\r]+##g" $ZSH_BOOKMARKS; + unhash -d $2; + printf "Deleted %s\n" $2; + break;; + [Nn]* ) printf "Did not delete %s\n" $2; break;; + * ) echo "Please answer yes or no.";; + esac + done + else + printf "Can't delete, because %s isn't in the bookmark file!\n" $2; + printf "Current bookmarks:\n\n"; + print -aC 2 ${(kv)CURBKMKS} | sed 's/^/ /'; + fi + + # If first argument isn't -a or -d then print help + else + printf "First argument must be either -a or -r or -d\n"; + printf "To add a bookmark use cdbk -a \n"; + printf "To replace a bookmark use cdbk -r \n"; + printf "To delete a bookmark, use cdbk -d \n"; + printf "Current bookmarks:\n\n"; + print -aC 2 ${(kv)CURBKMKS} | sed 's/^/ /'; + fi + + fi +} + +# ---------------------- +# Auto-complete function +# ---------------------- +function _cdbk() { + reply=($(cat "$ZSH_BOOKMARKS" | sed -e 's#^hash -d \(.*\)=.*$#\1#g') $(cat "$HOME/.zshrc" | grep "^hash -d" | sed -e 's#^hash -d \(.*\)=.*$#\1#g')); +} + +compctl -K _cdbk cdbk + +# --------------------------------------- +# folder_name function for custom prompt +# ---------------------- ---------------- +function folder_name { + FOLDERNAME=$(grep -e "hash -d.*=\"*\'*$PWD\"*\'*"$ {"$ZSH_BOOKMARKS","$HOME"/.zshrc} | sed 's#^.*hash -d \([^=]*\)=.*$#~\1#' | xargs echo); + if [ $FOLDERNAME ]; then + echo "${PR_LIGHT_CYAN}(${PR_LIGHT_WHITE}$FOLDERNAME${PR_LIGHT_CYAN})%{${reset_color}%}"; + elif [[ "$PWD" == "$MYZSH" ]]; then + echo "${PR_LIGHT_CYAN}(${PR_LIGHT_WHITE}~zsh${PR_LIGHT_CYAN})%{${reset_color}%}"; + elif [[ "$PWD" == "$HOME/.vim" ]]; then + echo "${PR_LIGHT_CYAN}(${PR_LIGHT_WHITE}~vim${PR_LIGHT_CYAN})%{${reset_color}%}"; + else + echo ""; + fi +} + From a9003004494f2c5f7d2467ab44b602fd8eaad20c Mon Sep 17 00:00:00 2001 From: Mike Dacre Date: Fri, 8 Aug 2014 13:23:23 -0700 Subject: [PATCH 2/5] Improved description --- plugins/cdbk/cdbk.plugin.zsh | 48 ++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/plugins/cdbk/cdbk.plugin.zsh b/plugins/cdbk/cdbk.plugin.zsh index 798bf5436..6905e2b74 100644 --- a/plugins/cdbk/cdbk.plugin.zsh +++ b/plugins/cdbk/cdbk.plugin.zsh @@ -1,38 +1,38 @@ # ----------------------------------------------------------------------------- # -# Program: cdbk.zsh (cd bookmarking for zsh) +# Program: cdbk.zsh (cd bookmarking for zsh) # -# Usage: cdbk {-a,-r,-d} [path] (run with no paramaters for info) +# Usage: cdbk {-a,-r,-d} [path] (run with no paramaters for info) # -# Requirements: Needs to be sourced from a zsh startup file, or use oh-my-zsh +# Requirements: Needs to be sourced from a zsh startup file, or use oh-my-zsh # -# Decription: cdbk() is a simple zsh function to make management of zsh -# named directories easier. It keeps all named directories in a -# file and uses grep, sed, echo, and perl to parse and modify -# this file in order to add, change or remove bookmarks. +# Revision #: 1.0 +# Last modified: 2014-08-08 13:23 # -# Because it uses the zsh named directory function, full zsh path -# completion is possible. Further, very simple cdbk completion is -# included to make replacing and deleting bookmarks easier. +# Decription: cdbk() is a simple zsh function to make management of zsh +# named directories easier. It keeps all named directories in a +# file and uses grep, sed, echo, and perl to parse and modify +# this file in order to add, change or remove bookmarks. +# +# Because it uses the zsh named directory function, full zsh path +# completion is possible. Further, very simple cdbk completion is +# included to make replacing and deleting bookmarks easier. # -# This program was heavily inspired by Stan Angeloff's function -# of the same name found here: -# http://blog.angeloff.name/post/1027007406/cd-with-bookmarks-and-auto-completion-for-zsh +# This program was heavily inspired by Stan Angeloff's function +# of the same name found here: +# http://blog.angeloff.name/post/1027007406/cd-with-bookmarks-and-auto-completion-for-zsh # -# This file also provides the function folder_name(), which returns -# a formatted list of the names of the current folder for use in -# a prompt. To include the folder name in your prompt use e.g.: -# export PROMPT=$PROMPT $(folder_name) +# This file also provides the function folder_name(), which returns +# a formatted list of the names of the current folder for use in +# a prompt. To include the folder name in your prompt use e.g.: +# export PROMPT=$PROMPT $(folder_name) # -# Bugs: None that I know of +# Bugs: None that I know of # -# Created by: Mike Dacre -# Created on: 19-11-11 +# Created by: Mike Dacre +# Created on: 19-11-11 # -# Revision #: 0.9.6-beta -# Updated: 23-11-11 10:11:27 -# -# License: MIT License - Open Source. Use as you wish +# License: MIT License - Open Source. Use as you wish # # ----------------------------------------------------------------------------- From 751a859085da6cf6ed25bbddb2353dbb2b6fa43d Mon Sep 17 00:00:00 2001 From: Mike Dacre Date: Fri, 8 Aug 2014 13:27:05 -0700 Subject: [PATCH 3/5] Changed instructions to use RPROMPT instead --- plugins/cdbk/cdbk.plugin.zsh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/cdbk/cdbk.plugin.zsh b/plugins/cdbk/cdbk.plugin.zsh index 6905e2b74..15b5be76e 100644 --- a/plugins/cdbk/cdbk.plugin.zsh +++ b/plugins/cdbk/cdbk.plugin.zsh @@ -7,7 +7,7 @@ # Requirements: Needs to be sourced from a zsh startup file, or use oh-my-zsh # # Revision #: 1.0 -# Last modified: 2014-08-08 13:23 +# Last modified: 2014-08-08 13:26 # # Decription: cdbk() is a simple zsh function to make management of zsh # named directories easier. It keeps all named directories in a @@ -25,7 +25,7 @@ # This file also provides the function folder_name(), which returns # a formatted list of the names of the current folder for use in # a prompt. To include the folder name in your prompt use e.g.: -# export PROMPT=$PROMPT $(folder_name) +# export RPROMPT=$(folder_name) $RPROMPT # # Bugs: None that I know of # From 6203b8e95797faa3afb000b00afe909a198ca26c Mon Sep 17 00:00:00 2001 From: Mike Dacre Date: Sun, 31 Aug 2014 21:50:51 -0700 Subject: [PATCH 4/5] Created README, and improved documentation --- plugins/cdbk/README.md | 48 ++++++++++++++++++++++ plugins/cdbk/cdbk.plugin.zsh | 79 +++++++++++------------------------- 2 files changed, 71 insertions(+), 56 deletions(-) create mode 100644 plugins/cdbk/README.md diff --git a/plugins/cdbk/README.md b/plugins/cdbk/README.md new file mode 100644 index 000000000..96952cf1f --- /dev/null +++ b/plugins/cdbk/README.md @@ -0,0 +1,48 @@ +cdbk +==== + +A simple zsh function to make management of zsh +named directories easier. It keeps all named directories in a +file and uses grep, sed, echo, and perl to parse and modify +this file in order to add, change or remove bookmarks. + +Because it uses the zsh named directory function, full zsh path +completion is possible. For example, if you wanted to access the +directory ``/Users/me/Work/Project13/Samples/FromEster/July/`` +and that directory had many subdirectories including the subdirectory +``./Revision6/pictures``, you could easily bookmark the parent directory +with the command: +``` +cdbk -a ejul /Users/me/Work/Project13/Samples/FromEster/July/ +``` +Then, to cd to the subdirectory, just type ``cd ~ejul/Revision6/pictures``, +autocompletion will work too. + +This file also provides the function ``folder_name()``, which returns +a formatted list of the names of the current folder for use in +a prompt. To include the folder name in your prompt use e.g.: +``export PROMPT=$PROMPT $(folder_name)`` + +Very simple cdbk completion is included to make replacing and +deleting bookmarks easier. + +This program was heavily inspired by Stan Angeloff's function +of the same name found here: +http://blog.angeloff.name/post/1027007406/cd-with-bookmarks-and-auto-completion-for-zsh + +``` +Usage: cdbk {-a,-r,-d,-l} [path] (run with no paramaters for info) + -a: Create bookmark (uses current dir if no path) + -r: Replace bookmark (uses current dir if no path) + -d: Delete bookmark + -l: List all currently enabled bookmarks +``` +Requirements: Needs to be sourced from a zsh startup file, or use oh-my-zsh + +Created by: Mike Dacre + +Created on: 19-11-11 + +Version: 1.1 + +License: MIT License - Open Source. Use as you wish diff --git a/plugins/cdbk/cdbk.plugin.zsh b/plugins/cdbk/cdbk.plugin.zsh index 15b5be76e..701c0393e 100644 --- a/plugins/cdbk/cdbk.plugin.zsh +++ b/plugins/cdbk/cdbk.plugin.zsh @@ -1,40 +1,5 @@ -# ----------------------------------------------------------------------------- -# -# Program: cdbk.zsh (cd bookmarking for zsh) -# -# Usage: cdbk {-a,-r,-d} [path] (run with no paramaters for info) -# -# Requirements: Needs to be sourced from a zsh startup file, or use oh-my-zsh -# -# Revision #: 1.0 -# Last modified: 2014-08-08 13:26 -# -# Decription: cdbk() is a simple zsh function to make management of zsh -# named directories easier. It keeps all named directories in a -# file and uses grep, sed, echo, and perl to parse and modify -# this file in order to add, change or remove bookmarks. -# -# Because it uses the zsh named directory function, full zsh path -# completion is possible. Further, very simple cdbk completion is -# included to make replacing and deleting bookmarks easier. -# -# This program was heavily inspired by Stan Angeloff's function -# of the same name found here: -# http://blog.angeloff.name/post/1027007406/cd-with-bookmarks-and-auto-completion-for-zsh -# -# This file also provides the function folder_name(), which returns -# a formatted list of the names of the current folder for use in -# a prompt. To include the folder name in your prompt use e.g.: -# export RPROMPT=$(folder_name) $RPROMPT -# -# Bugs: None that I know of -# -# Created by: Mike Dacre -# Created on: 19-11-11 -# -# License: MIT License - Open Source. Use as you wish -# -# ----------------------------------------------------------------------------- +# cdbk.plugin.zsh (cd bookmarking for zsh) +# Last modified: 2014-08-31 21:25 # Define location of bookmark file and source it every time this file is sourced ZSH_BOOKMARKS="$HOME/.zshbookmarks"; @@ -48,18 +13,15 @@ fi # ---------------------- # Main Function # ---------------------- -function cdbk () { +function cdbk1 () { source $ZSH_BOOKMARKS; # Create local variables for function and global bkmk functions local BKMKNAME; local BKMKPATH; local MYPATH; - CURBKMKS=(`grep -e "^hash -d" $ZSH_BOOKMARKS | sed 's#hash -d ##' | sed 's#=\(.*\)# \1#' `) - GLBLBKMKS=(`grep -e "^ *hash -d" $HOME/.zshrc | sed 's#hash -d ##' | sed 's#=\(.*\)# \1#' `) - - local GLBLTEST=GLBLBKMKS; - local HOSTTEST=HOSTBKMKS; + CURBKMKS=(`grep -e "^hash -d" $ZSH_BOOKMARKS | sed 's#hash -d ##' | sed 's#=\(.*\)# \1#'`) + GLBLBKMKS=(`grep -e "^ *hash -d" $HOME/.zshrc1 | sed 's#hash -d ##' | sed 's#=\(.*\)# \1#'`) # Define usage local USAGE="------------------------------------------------------------------------------- @@ -67,7 +29,8 @@ function cdbk () { cdbk -a [] : Create bookmark (uses current dir if no path) cdbk -r [] : Replace bookmark (uses current dir if no path) - cdbk -d : Delete bookmark\n\n"; + cdbk -d : Delete bookmark + cdbk -l : List currently enabled bookmarks\n\n"; # Check first if bookmark file exists if [[ ! -e $ZSH_BOOKMARKS ]]; then @@ -75,24 +38,28 @@ function cdbk () { printf "Bookmark file %s created\n" $ZSH_BOOKMARKS; fi - # Check if there are enough arguments - if [[ $# -lt 2 ]]; then - - # If no arguments, display all bookmarks and brief help + if [[ $1 == "-l" ]]; then + # Display all currently enabled bookmarks printf "$USAGE Current bookmarks:\n"; print -aC 2 ${(kv)CURBKMKS} | sed 's/^/ /' | sort; - if [ $GLBLTEST ]; then - printf "\n Global Bookmarks:\n"; + if [[ -n ${GLBLBKMKS} ]]; then + printf "\n Global Bookmarks: (from ~/.zshrc)\n"; print -aC 2 ${(kv)GLBLBKMKS} | sed 's/^/ /' | sort; fi - if [ $HOSTTEST ]; then - printf "\n Host Specific Global Bookmarks:\n"; - print -aC 2 ${(kv)HOSTBKMKS} | sed 's/^/ /' | sort; - fi printf "\n--------------------------------------------------------------------------------\n"; - else + # Check if there are enough arguments + elif [[ $# -lt 2 ]] || [[ $1 == "-h" ]]; then + printf "$USAGE" + printf "--------------------------------------------------------------------------------\n"; + + elif [[ $# -gt 3 ]]; then + printf "Too many arguments\n\n" + printf "Usage:\n\n" + printf "$USAGE" + printf "--------------------------------------------------------------------------------\n"; + else # Look for existing version of query BKMKNAME=$(grep "hash -d $2=" "$ZSH_BOOKMARKS" | sed 's#^hash -d ##' | sed 's#=.*$##'); BKMKPATH=$(grep "hash -d $2=" "$ZSH_BOOKMARKS" | sed 's#^hash -d [^=]*=##'); @@ -200,7 +167,7 @@ function cdbk () { # If first argument isn't -a or -d then print help else - printf "First argument must be either -a or -r or -d\n"; + printf "First argument must be either -a or -r or -d or -l\n"; printf "To add a bookmark use cdbk -a \n"; printf "To replace a bookmark use cdbk -r \n"; printf "To delete a bookmark, use cdbk -d \n"; From be0b59a9e1d1f0d9eed4793b2759211e33ec9503 Mon Sep 17 00:00:00 2001 From: Mike Dacre Date: Sun, 31 Aug 2014 21:51:42 -0700 Subject: [PATCH 5/5] Removed testing code --- plugins/cdbk/cdbk.plugin.zsh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/cdbk/cdbk.plugin.zsh b/plugins/cdbk/cdbk.plugin.zsh index 701c0393e..6650b0641 100644 --- a/plugins/cdbk/cdbk.plugin.zsh +++ b/plugins/cdbk/cdbk.plugin.zsh @@ -1,5 +1,5 @@ # cdbk.plugin.zsh (cd bookmarking for zsh) -# Last modified: 2014-08-31 21:25 +# Last modified: 2014-08-31 21:51 # Define location of bookmark file and source it every time this file is sourced ZSH_BOOKMARKS="$HOME/.zshbookmarks"; @@ -13,7 +13,7 @@ fi # ---------------------- # Main Function # ---------------------- -function cdbk1 () { +function cdbk () { source $ZSH_BOOKMARKS; # Create local variables for function and global bkmk functions @@ -21,7 +21,7 @@ function cdbk1 () { local BKMKPATH; local MYPATH; CURBKMKS=(`grep -e "^hash -d" $ZSH_BOOKMARKS | sed 's#hash -d ##' | sed 's#=\(.*\)# \1#'`) - GLBLBKMKS=(`grep -e "^ *hash -d" $HOME/.zshrc1 | sed 's#hash -d ##' | sed 's#=\(.*\)# \1#'`) + GLBLBKMKS=(`grep -e "^ *hash -d" $HOME/.zshrc | sed 's#hash -d ##' | sed 's#=\(.*\)# \1#'`) # Define usage local USAGE="-------------------------------------------------------------------------------