ENH: 'prepend' and 'replace' actions keep prepended date.

Generalized and simplified the logic that already kept an existing priority to also keep a date added via todo.sh -t / TODOTXT_DATE_ON_ADD (unless the replaced text also starts with a date).
This commit is contained in:
Ingo Karkat
2010-07-05 11:24:19 +02:00
parent 2f6d9ae329
commit 5e44868261
3 changed files with 96 additions and 15 deletions

36
todo.sh
View File

@@ -288,8 +288,14 @@ replaceOrPrepend()
{
action=$1; shift
case "$action" in
replace) backref=;;
prepend) backref=' &';;
replace)
backref=
querytext="Replacement: "
;;
prepend)
backref=' &'
querytext="Prepend: "
;;
esac
shift; item=$1; shift
@@ -300,27 +306,27 @@ replaceOrPrepend()
[ -z "$todo" ] && die "$item: No such task."
if [[ -z "$1" && $TODOTXT_FORCE = 0 ]]; then
case "$action" in
replace) echo -n "Replacement: ";;
prepend) echo -n "Prepend: ";;
esac
echo -n "$querytext"
read input
else
input=$*
fi
cleaninput $input
# Test for then set priority
if [ `sed "$item!d" "$TODO_FILE"|grep -c "^(\\w)"` -eq 1 ]; then
priority=$(sed "$item!d" "$TODO_FILE" | awk -F '\\(|\\)' '{print $2}')
# Retrieve existing priority and prepended date
priority=$(sed -e "$item!d" -e $item's/^\(([A-Z]) \)\{0,1\}\([0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\} \)\{0,1\}.*/\1/' "$TODO_FILE")
prepdate=$(sed -e "$item!d" -e $item's/^\(([A-Z]) \)\{0,1\}\([0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\} \)\{0,1\}.*/\2/' "$TODO_FILE")
if [ "$prepdate" -a "$action" = "replace" ] && [ "$(echo "$input"|sed -e 's/^\([0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\}\)\{0,1\}.*/\1/')" ]; then
# If the replaced text starts with a date, it will replace the existing
# date, too.
prepdate=
fi
# If priority isn't set change task, if it is remove priority, change then add priority again
if [ -z $priority ]; then
sed -i.bak $item" s|^.*|${input}${backref}|" "$TODO_FILE"
else
sed -i.bak -e "$item s/^(.) //" -e "$item s|^.*|\($priority\) ${input}${backref}|" "$TODO_FILE"
fi
# Temporarily remove any existing priority and prepended date, perform the
# change (replace/prepend) and re-insert the existing priority and prepended
# date again.
sed -i.bak -e "$item s/^${priority}${prepdate}//" -e "$item s|^.*|${priority}${prepdate}${input}${backref}|" "$TODO_FILE"
if [ $TODOTXT_VERBOSE -gt 0 ]; then
newtodo=$(sed "$item!d" "$TODO_FILE")
case "$action" in