From 24d0fb003a8ef486c37a53cb80d28872922e206c Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Thu, 19 Apr 2012 14:08:39 +0200 Subject: [PATCH 1/3] FIX: Replacement with priority and date doesn't replace existing date. --- tests/t1100-replace.sh | 8 ++++++++ todo.sh | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) 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 4837120..082c495 100755 --- a/todo.sh +++ b/todo.sh @@ -395,9 +395,9 @@ replaceOrPrepend() 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") - 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/^\((.) \)\{0,1\}\([0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\} \)\{0,1\}.*/\1\2/')" ]; then + # If the replaced text starts with a [priority +] date, it will replace + # the existing date, too. prepdate= fi From e0b93e48fb6af56c171b6b684681728ab552d45c Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Thu, 19 Apr 2012 14:12:35 +0200 Subject: [PATCH 2/3] Refactoring: Extract duplicated regexp for priority and date. --- todo.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/todo.sh b/todo.sh index 082c495..372024b 100755 --- a/todo.sh +++ b/todo.sh @@ -392,10 +392,11 @@ replaceOrPrepend() 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,1\}\([0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\} \)\{0,1\}.*/\1\2/')" ]; then + 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= From f45bbd1a1d33cd4700f3b90f3322b0c51e52828e Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Thu, 19 Apr 2012 14:19:30 +0200 Subject: [PATCH 3/3] Minor: Move cleaninput call closer to sed command. --- todo.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/todo.sh b/todo.sh index 372024b..5056e73 100755 --- a/todo.sh +++ b/todo.sh @@ -389,7 +389,6 @@ replaceOrPrepend() else input=$* fi - cleaninput "for sed" # Retrieve existing priority and prepended date local -r priAndDateExpr='^\((.) \)\{0,1\}\([0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\} \)\{0,1\}' @@ -405,6 +404,7 @@ replaceOrPrepend() # 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"