diff --git a/tests/t1100-replace.sh b/tests/t1100-replace.sh index 9336304..77ca9e1 100755 --- a/tests/t1100-replace.sh +++ b/tests/t1100-replace.sh @@ -164,4 +164,12 @@ TODO: Replaced task with: 1 (A) 2010-07-04 this also has a new date EOF +echo '2009-02-13 this is just a new one' > todo.txt +test_todo_session 'replace with prepended priority and date replaces existing date' <>> todo.sh replace 1 '(B) 2010-07-04 this also has a new date' +1 2009-02-13 this is just a new one +TODO: Replaced task with: +1 (B) 2010-07-04 this also has a new date +EOF + test_done diff --git a/todo.sh b/todo.sh index b685afb..2afe912 100755 --- a/todo.sh +++ b/todo.sh @@ -389,21 +389,22 @@ replaceOrPrepend() else input=$* fi - cleaninput "for sed" # Retrieve existing priority and prepended date - priority=$(sed -e "$item!d" -e $item's/^\((.) \)\{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/^\((.) \)\{0,1\}\([0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\} \)\{0,1\}.*/\2/' "$TODO_FILE") + local -r priAndDateExpr='^\((.) \)\{0,1\}\([0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\} \)\{0,1\}' + priority=$(sed -e "$item!d" -e "${item}s/${priAndDateExpr}.*/\\1/" "$TODO_FILE") + prepdate=$(sed -e "$item!d" -e "${item}s/${priAndDateExpr}.*/\\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. + if [ "$prepdate" -a "$action" = "replace" ] && [ "$(echo "$input"|sed -e "s/${priAndDateExpr}.*/\\1\\2/")" ]; then + # If the replaced text starts with a [priority +] date, it will replace + # the existing date, too. prepdate= fi # Temporarily remove any existing priority and prepended date, perform the # change (replace/prepend) and re-insert the existing priority and prepended # date again. + cleaninput "for sed" sed -i.bak -e "$item s/^${priority}${prepdate}//" -e "$item s|^.*|${priority}${prepdate}${input}${backref}|" "$TODO_FILE" if [ $TODOTXT_VERBOSE -gt 0 ]; then getNewtodo "$item"