1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
# (C) 2002-2003 Dan Allen and Stefan Kamphausen
# Written by Dan Allen <dan@mojavelinux.com>
# - small additions by Stefan Kamphausen
# - better completion code by Robi Malik robi.malik@nexgo.de
# - trailing path support by Damon Harper <ds+dev-cdargs@usrbin.ca> Feb 2006
# Globals
CDARGS_SORT=0 # set to 1 if you want mark to sort the list
CDARGS_NODUPS=1 # set to 1 if you want mark to delete dups
# --------------------------------------------- #
# Run the cdargs program to get the target #
# directory to be used in the various context #
# This is the fundamental core of the #
# bookmarking idea. An alias or substring is #
# expected and upon receiving one it either #
# resolves the alias if it exists or opens a #
# curses window with the narrowed down options #
# waiting for the user to select one. #
# #
# @param string alias #
# #
# @access private #
# @return 0 on success, >0 on failure #
# --------------------------------------------- #
function _cdargs_get_dir ()
{
local bookmark extrapath
# if there is one exact match (possibly with extra path info after it),
# then just use that match without calling cdargs
if [ -e "$HOME/.cdargs" ]; then
dir=`/bin/grep "^$1 " "$HOME/.cdargs"`
if [ -z "$dir" ]; then
bookmark="${1/\/*/}"
if [ "$bookmark" != "$1" ]; then
dir=`/bin/grep "^$bookmark " "$HOME/.cdargs"`
extrapath=`echo "$1" | /bin/sed 's#^[^/]*/#/#'`
fi
fi
[ -n "$dir" ] && dir=`echo "$dir" | /bin/sed 's/^[^ ]* //'`
fi
if [ -z "$dir" -o "$dir" != "${dir/
/}" ]; then
# okay, we need cdargs to resolve this one.
# note: intentionally retain any extra path to add back to selection.
dir=
if cdargs --noresolve "${1/\/*/}"; then
dir=`cat "$HOME/.cdargsresult"`
/bin/rm -f "$HOME/.cdargsresult";
fi
fi
if [ -z "$dir" ]; then
echo "Aborted: no directory selected" >&2
return 1
fi
[ -n "$extrapath" ] && dir="$dir$extrapath"
if [ ! -d "$dir" ]; then
echo "Failed: no such directory '$dir'" >&2
return 2
fi
}
# --------------------------------------------- #
# Perform the command (cp or mv) using the #
# cdargs bookmark alias as the target directory #
# #
# @param string command argument list #
# #
# @access private #
# @return void #
# --------------------------------------------- #
function _cdargs_exec ()
{
local arg dir i last call_with_browse
# Get the last option which will be the bookmark alias
eval last=\${$#};
# Resolve the bookmark alias. If it cannot resolve, the
# curses window will come up at which point a directory
# will need to be choosen. After selecting a directory,
# the function will continue and $_cdargs_dir will be set.
if [ -e $last ]; then
last=
fi
if _cdargs_get_dir "$last"; then
# For each argument save the last, move the file given in
# the argument to the resolved cdargs directory
i=1;
for arg; do
if [ $i -lt $# ]; then
$command "$arg" "$dir";
fi
let i=$i+1;
done
fi
}
# --------------------------------------------- #
# Prepare to move file list into the cdargs #
# target directory #
# #
# @param string command argument list #
# #
# @access public #
# @return void #
# --------------------------------------------- #
function mvb ()
{
local command
command='mv -i';
_cdargs_exec $*;
}
# --------------------------------------------- #
# Prepare to copy file list into the cdargs #
# target directory #
# #
# @param string command argument list #
# #
# @access public #
# @return void #
# --------------------------------------------- #
function cpb ()
{
local command
command='cp -i';
_cdargs_exec $*;
}
# --------------------------------------------- #
# Change directory to the cdargs target #
# directory #
# #
# @param string alias #
# #
# @access public #
# @return void #
# --------------------------------------------- #
function cdb ()
{
local dir
_cdargs_get_dir "$1" && cd "$dir" && echo `pwd`;
}
alias cb='cdb'
alias cv='cdb'
# --------------------------------------------- #
# Mark the current directory with alias #
# provided and store as a cdargs bookmark #
# directory #
# #
# @param string alias #
# #
# @access public #
# @return void #
# --------------------------------------------- #
function mark ()
{
local tmpfile
# first clear any bookmarks with this same alias, if file exists
if [ "$CDARGS_NODUPS" -a -e "$HOME/.cdargs" ]; then
tmpfile=`echo ${TEMP:-${TMPDIR:-/tmp}} | /bin/sed -e "s/\\/$//"`
tmpfile=$tmpfile/cdargs.$USER.$$.$RANDOM
/bin/grep -v "^$1 " "$HOME/.cdargs" > $tmpfile && 'mv' -f $tmpfile "$HOME/.cdargs";
fi
# add the alias to the list of bookmarks
cdargs --add=":$1:`pwd`";
# sort the resulting list
if [ "$CDARGS_SORT" ]; then
sort -o "$HOME/.cdargs" "$HOME/.cdargs";
fi
}
# Oh, no! Not overwrite 'm' for stefan! This was
# the very first alias I ever wrote in my un*x
# carreer and will always be aliased to less...
# alias m='mark'
# --------------------------------------------- #
# Mark the current directory with alias #
# provided and store as a cdargs bookmark #
# directory but do not overwrite previous #
# bookmarks with same name #
# #
# @param string alias #
# #
# @access public #
# @return void #
# author: SKa #
# --------------------------------------------- #
function ca ()
{
# add the alias to the list of bookmarks
cdargs --add=":$1:`pwd`";
}
# --------------------------------------------- #
# Bash programming completion for cdargs #
# Sets the $COMPREPLY list for complete #
# #
# @param string substring of alias #
# #
# @access private #
# @return void #
# --------------------------------------------- #
function _cdargs_aliases ()
{
local cur bookmark dir strip oldIFS
COMPREPLY=()
if [ -e "$HOME/.cdargs" ]; then
cur=${COMP_WORDS[COMP_CWORD]}
if [ "$cur" != "${cur/\//}" ]; then # if at least one /
bookmark="${cur/\/*/}"
dir=`/bin/grep "^$bookmark " "$HOME/.cdargs" | /bin/sed 's#^[^ ]* ##'`
if [ -n "$dir" -a "$dir" = "${dir/
/}" -a -d "$dir" ]; then
strip="${dir//?/.}"
oldIFS="$IFS"
IFS='
'
COMPREPLY=( $(
compgen -d "$dir`echo "$cur" | /bin/sed 's#^[^/]*##'`" \
| /bin/sed -e "s/^$strip/$bookmark/" -e "s/\([^\/a-zA-Z0-9#%_+\\\\,.-]\)/\\\\\\1/g" ) )
IFS="$oldIFS"
fi
else
COMPREPLY=( $( (echo $cur ; cat "$HOME/.cdargs") | \
awk 'BEGIN {first=1}
{if (first) {cur=$0; l=length(cur); first=0}
else if (substr($1,1,l) == cur) {print $1}}' ) )
fi
fi
return 0
}
# --------------------------------------------- #
# Bash programming completion for cdargs #
# Set up completion (put in a function just so #
# `nospace' can be a local variable) #
# #
# @param none #
# #
# @access private #
# @return void #
# --------------------------------------------- #
_cdargs_complete() {
local nospace=
[ "${BASH_VERSINFO[0]}" -ge 3 -o \( "${BASH_VERSINFO[0]}" = 2 -a \( "${BASH_VERSINFO[1]}" = 05a -o "${BASH_VERSINFO[1]}" = 05b \) \) ] && nospace='-o nospace'
complete $nospace -S / -X '*/' -F _cdargs_aliases cv cb cdb
}
# Support ZSH via its BASH completion emulation
if [ -n "$ZSH_VERSION" ]; then
autoload bashcompinit
bashcompinit
elif [ -z "${BASH_VERSION}" ]; then
# we do not support anything besides bash completion
# (however patches to support other shells are very welcome)
return 100
fi
_cdargs_complete
|