Skip to content

Commit

Permalink
incorporated jons safe purge_line function
Browse files Browse the repository at this point in the history
  • Loading branch information
huy committed Mar 2, 2011
2 parents 07d8daa + e1a1181 commit a68eb2d
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions bashmarks.sh
Expand Up @@ -43,8 +43,7 @@ function s {
check_help $1
_bookmark_name_valid "$@"
if [ -z "$exit_message" ]; then
cat $SDIRS | grep -v "export DIR_$1=" > $SDIRS.tmp
mv $SDIRS.tmp $SDIRS
_purge_line "$SDIRS" "export DIR_$1="
CURDIR=$(echo $PWD| sed "s#^$HOME#\$HOME#g")
echo "export DIR_$1=\"$CURDIR\"" >> $SDIRS
fi
Expand All @@ -69,8 +68,7 @@ function d {
check_help $1
_bookmark_name_valid "$@"
if [ -z "$exit_message" ]; then
cat $SDIRS | grep -v "export DIR_$1=" > $SDIRS.tmp
mv $SDIRS.tmp $SDIRS
_purge_line "$SDIRS" "export DIR_$1="
unset "DIR_$1"
fi
}
Expand Down Expand Up @@ -131,6 +129,23 @@ function _compzsh {
reply=($(_l))
}

# safe delete line from sdirs
function _purge_line {
if [ -s "$1" ]; then
# safely create a temp file
t=$(mktemp -t bashmarks) || exit 1
trap "rm -f -- '$t'" EXIT

# purge line
sed "/$2/d" "$1" > "$t"
mv "$t" "$1"

# cleanup temp file
rm -f -- "$t"
trap - EXIT
fi
}

# bind completion command for g,p,d to _comp
if [ $ZSH_VERSION ]; then
compctl -K _compzsh g
Expand Down

0 comments on commit a68eb2d

Please sign in to comment.