|
|
@@ -1,34 +1,81 @@ |
|
|
# shell bookmarks; s <name> to save, l to list and g <name> to go |
|
|
# optimize some cases, include zsh completion, and change the bookmark storage format (_BOOKMARK_ != DIR_ to avoid namespace pollution) |
|
|
function _uh { [[ -z $DEV ]]; return $? } |
|
|
|
|
|
function userroot { |
|
|
if ! _uh; then echo $DEV; |
|
|
else echo $HOME; fi |
|
|
} |
|
|
|
|
|
function _sdirname { |
|
|
echo "$(userroot)/.sdirs" |
|
|
} |
|
|
|
|
|
function _sglobalname { |
|
|
echo "${HOME}/.sdirs" |
|
|
} |
|
|
|
|
|
function _ss_for_arg { |
|
|
local d n; |
|
|
if [[ -z $1 ]]; then return; fi |
|
|
if [[ "${1[0,1]}" == "+" ]]; then d="$(_sglobalname)"; n="${1[2,-1]}"; |
|
|
else d="$(_sdirname)"; n="$1"; fi |
|
|
echo "local sdir arg; sdir=\"$d\"; arg=\"$n\";" |
|
|
} |
|
|
|
|
|
# Shell Bookmarks (inspired by rpetrich.) |
|
|
function dbg { |
|
|
if [[ -z $1 ]]; then return; fi |
|
|
eval $(_ss_for_arg $1); |
|
|
echo $sdir; |
|
|
echo $arg; |
|
|
} |
|
|
function s { |
|
|
if [[ -z $1 ]]; then return; fi |
|
|
if [[ -f ~/.sdirs ]]; then |
|
|
grep -v "^export _BOOKMARK_$1" ~/.sdirs > ~/.sdirs1 |
|
|
mv ~/.sdirs1 ~/.sdirs |
|
|
fi |
|
|
echo "export _BOOKMARK_$1=$PWD" >> ~/.sdirs |
|
|
if [[ -z $1 ]]; then return; fi |
|
|
eval $(_ss_for_arg $1); |
|
|
if [[ -f "$sdir" ]]; then |
|
|
grep -v "^export _BOOKMARK_$arg" "${sdir}" > "${sdir}1" |
|
|
mv "${sdir}1" "${sdir}" |
|
|
fi |
|
|
echo "export _BOOKMARK_$arg=$PWD" >> "${sdir}" |
|
|
} |
|
|
|
|
|
function l { |
|
|
if [[ -s ~/.sdirs ]]; then |
|
|
sed -e 's/^export _BOOKMARK_\([^=]*\)=\(.*\)$/\1 (\2)/g' ~/.sdirs | sort |
|
|
else |
|
|
echo "No bookmarks." |
|
|
fi |
|
|
if [[ -s $(_sdirname) ]]; then |
|
|
sed -e 's/^export _BOOKMARK_\([^=]*\)=\(.*\)$/\1 (\2)/g' "$(_sdirname)" | sort |
|
|
fi |
|
|
if ! _uh; then |
|
|
if [[ -s $(_sglobalname) ]]; then |
|
|
sed -e 's/^export _BOOKMARK_\([^=]*\)=\(.*\)$/+\1 (\2)/g' "$(_sglobalname)" | sort |
|
|
fi |
|
|
fi |
|
|
} |
|
|
|
|
|
function g { |
|
|
. ~/.sdirs |
|
|
cd $(eval echo $(echo \$_BOOKMARK_$1)) |
|
|
eval $(_ss_for_arg $1) |
|
|
. "$sdir" |
|
|
cd $(eval echo $(echo \$_BOOKMARK_$arg)) |
|
|
} |
|
|
|
|
|
function d { |
|
|
if [[ -z $1 ]]; then return; fi |
|
|
if [[ -f ~/.sdirs ]]; then |
|
|
grep -v "^export _BOOKMARK_$1" ~/.sdirs > ~/.sdirs1 |
|
|
mv ~/.sdirs1 ~/.sdirs |
|
|
fi |
|
|
if [[ -z $1 ]]; then return; fi |
|
|
eval $(_ss_for_arg $1) |
|
|
if [[ -f "$sdir" ]]; then |
|
|
grep -v "^export _BOOKMARK_$arg" "${sdir}" > "${sdir}1" |
|
|
mv "${sdir}1" "${sdir}" |
|
|
fi |
|
|
} |
|
|
|
|
|
function _gcomp { reply=(`sed -e 's/^export _BOOKMARK_\([^=]*\)=.*$/\1/g' ~/.sdirs | sort`) } |
|
|
compctl -K _gcomp g |
|
|
function _gcomp { |
|
|
local descs globals; |
|
|
descs=(); |
|
|
globals=(); |
|
|
sed -e 's/^export _BOOKMARK_\([^=]*\)=\(.*\)$/\1:\2/g' "$(_sdirname)" | sort | while read line; do |
|
|
descs[$(($#descs+1))]="$line" |
|
|
done |
|
|
if ! _uh; then |
|
|
sed -e 's/^export _BOOKMARK_\([^=]*\)=\(.*\)$/+\1:\2/g' "$(_sglobalname)" | sort | while read line; do |
|
|
globals[$(($#globals+1))]="$line" |
|
|
done |
|
|
fi |
|
|
_describe -t bookmarks "shell bookmark" descs; _describe -t bookmarks "global bookmark" globals |
|
|
} |
|
|
compdef _gcomp g |