diff --git a/src/awk/altertodo.awk b/src/awk/altertodo.awk index ebb797d..ae2e5eb 100644 --- a/src/awk/altertodo.awk +++ b/src/awk/altertodo.awk @@ -3,39 +3,43 @@ # If `delta` is specified using `-v`, then the priority value is increased by # `delta.` If `delta` is unspecified (or equal to 0), then the completeness # status is toggled. + +@include "lib/awk/icalendar.awk" + BEGIN { FS=":"; zulu = strftime("%Y%m%dT%H%M%SZ", systime(), 1); delta = delta + 0; # cast as integer } +{ gsub("\r", "") } /^END:VTODO/ && inside { # Print sequence and last-modified, if not yet printed - if (!seq) print "SEQUENCE:1"; - if (!lm) print "LAST-MODIFIED:" zulu; + if (!seq) print_cr("SEQUENCE:1"); + if (!lm) print_cr("LAST-MODIFIED:" zulu); # Print priority prio = prio ? prio + delta : 0 + delta; prio = prio < 0 ? 0 : prio; prio = prio > 9 ? 9 : prio; - print "PRIORITY:" prio; + print_cr("PRIORITY:" prio); # Print status (toggle if needed) bit_status = status == "COMPLETED" ? 1 : 0; bit_toggle = delta ? 0 : 1; percent = xor(bit_status, bit_toggle) ? 100 : 0; status = xor(bit_status, bit_toggle) ? "COMPLETED" : "NEEDS-ACTION"; - print "STATUS:" status - print "PERCENT-COMPLETE:" percent + print_cr("STATUS:" status) + print_cr("PERCENT-COMPLETE:" percent) # print rest inside = ""; - print $0; + print_cr($0); next } -/^BEGIN:VTODO/ { inside = 1; print; next } -/^SEQUENCE/ && inside { seq = 1; print "SEQUENCE:" $2+1; next } -/^LAST-MODIFIED/ && inside { lm = 1; print "LAST-MODIFIED:" zulu; next } -/^PRIORITY:/ && inside { prio = $2; next } -/^STATUS/ && inside { status = $2; next } -/^PERCENT-COMPLETE/ && inside { next } # ignore, we take STATUS:COMPLETED as reference -{ print } +/^BEGIN:VTODO/ { inside = 1; print_cr($0); next } +/^SEQUENCE/ && inside { seq = 1; print_cr("SEQUENCE:" $2+1); next } +/^LAST-MODIFIED/ && inside { lm = 1; print_cr("LAST-MODIFIED:" zulu); next } +/^PRIORITY:/ && inside { prio = $2; next } +/^STATUS/ && inside { status = $2; next } +/^PERCENT-COMPLETE/ && inside { next } # ignore, we take STATUS:COMPLETED as reference +{ print_cr($0) } diff --git a/src/awk/attach.awk b/src/awk/attach.awk index 410bb3e..d4261f5 100644 --- a/src/awk/attach.awk +++ b/src/awk/attach.awk @@ -15,21 +15,21 @@ function write_attachment( line, aline, fl) { fl = 1 while (getline aline = 73) { - print substr(line, 1, 73) - line = substr(line, 74) + if (fl && length(line) >= 72) { + print substr(line, 1, 72)"\r" + line = substr(line, 73) fl = 0 } - while (length(line) >= 72) { - print " "substr(line, 1, 72) - line = substr(line, 73) + while (length(line) >= 71) { + print " "substr(line, 1, 71)"\r" + line = substr(line, 72) } } if (line) - print " "line + print " "line"\r" } # AWK program -/^END:(VTODO|VJOURNAL)$/ { write_attachment() } +/^END:(VTODO|VJOURNAL)/ { write_attachment() } { print } diff --git a/src/awk/attachdd.awk b/src/awk/attachdd.awk index f6fc79f..c459b85 100644 --- a/src/awk/attachdd.awk +++ b/src/awk/attachdd.awk @@ -1,4 +1,5 @@ BEGIN { FS="[:;]" } +{ gsub("\r", "") } /^END:(VTODO|VJOURNAL)$/ { ins = 0; exit } /^[^ ]/ && a { a = 0 } /^ / && a && p { print substr($0, 2); } diff --git a/src/awk/attachls.awk b/src/awk/attachls.awk index d1e5b19..4ebdbe8 100644 --- a/src/awk/attachls.awk +++ b/src/awk/attachls.awk @@ -34,6 +34,7 @@ function att_info(i, str, cnt, k, info) { } BEGIN { FS="[:;]"; OFS="\t" } +{ gsub("\r", "") } /^END:(VTODO|VJOURNAL)$/ { ins = 0; exit } l && !r { att_info(i, l); l = "" } /^ / && r { l = l substr($0, 2); r = cont_reading($0) } diff --git a/src/awk/attachrm.awk b/src/awk/attachrm.awk index 2a8659e..81123cc 100644 --- a/src/awk/attachrm.awk +++ b/src/awk/attachrm.awk @@ -4,10 +4,10 @@ ## @assign id: Attachment number to remove BEGIN { FS="[:;]" } -/^END:(VTODO|VJOURNAL)$/ { ins = 0 } +/^END:(VTODO|VJOURNAL)/ { ins = 0 } /^[^ ]/ && a { a = 0 } /^ / && a { next } /^ATTACH/ && ins { i++; } /^ATTACH/ && ins && i == id { a = 1; next } -/^BEGIN:(VTODO|VJOURNAL)$/ { ins = 1 } +/^BEGIN:(VTODO|VJOURNAL)/ { ins = 1 } { print } diff --git a/src/awk/get.awk b/src/awk/get.awk index 7b0829a..edb4af2 100644 --- a/src/awk/get.awk +++ b/src/awk/get.awk @@ -16,6 +16,7 @@ # print content of field `field` BEGIN { FS = ":"; regex = "^" field; } BEGINFILE { type = ""; line = ""; } +{ gsub("\r", "") } /^BEGIN:(VJOURNAL|VTODO)/ { type = $2 } /^END:/ && $2 == type { nextfile } $0 ~ regex { line = $0; next; } diff --git a/src/awk/list.awk b/src/awk/list.awk index b0ad780..fa85a04 100644 --- a/src/awk/list.awk +++ b/src/awk/list.awk @@ -81,6 +81,8 @@ BEGINFILE { delete c; } +{ gsub("\r", "") } + /^BEGIN:(VJOURNAL|VTODO)/ { type = $2 } diff --git a/src/awk/new.awk b/src/awk/new.awk index 05b33c4..bf18dfc 100644 --- a/src/awk/new.awk +++ b/src/awk/new.awk @@ -37,33 +37,33 @@ END { } # print ical - print "BEGIN:VCALENDAR"; - print "VERSION:2.0"; - print "CALSCALE:GREGORIAN"; - print "PRODID:-//fzf-vjour//awk//EN"; - print "BEGIN:" type; - print "DTSTAMP:" zulu; - print "UID:" uid; - print "CLASS:PRIVATE"; - print "CREATED:" zulu; - print "SEQUENCE:1"; - print "LAST-MODIFIED:" zulu; + print_cr("BEGIN:VCALENDAR") + print_cr("VERSION:2.0") + print_cr("CALSCALE:GREGORIAN") + print_cr("PRODID:-//fzf-vjour//awk//EN") + print_cr("BEGIN:" type) + print_cr("DTSTAMP:" zulu) + print_cr("UID:" uid) + print_cr("CLASS:PRIVATE") + print_cr("CREATED:" zulu) + print_cr("SEQUENCE:1") + print_cr("LAST-MODIFIED:" zulu) if (type == "VTODO") { - print "STATUS:NEEDS-ACTION"; - print "PERCENT-COMPLETE:0"; + print_cr("STATUS:NEEDS-ACTION") + print_cr("PERCENT-COMPLETE:0") if (due) - print "DUE;VALUE=DATE:" due; + print_cr("DUE;VALUE=DATE:" due) } else { - print "STATUS:FINAL"; + print_cr("STATUS:FINAL") if (start) - print "DTSTART;VALUE=DATE:" start; + print_cr("DTSTART;VALUE=DATE:" start) } if (summary) print_fold("SUMMARY:", summary); if (categories) print_fold("CATEGORIES:", categories); if (desc) print_fold("DESCRIPTION:", desc); - print "END:" type; - print "END:VCALENDAR" + print_cr("END:" type) + print_cr("END:VCALENDAR") } diff --git a/src/awk/update.awk b/src/awk/update.awk index 021416c..e58d9d9 100644 --- a/src/awk/update.awk +++ b/src/awk/update.awk @@ -46,9 +46,9 @@ due && type == "VJOURNAL" { print "Notes and journal entries do not have /^SEQUENCE/ && type { seq = $2; next; } # store sequence number and skip /^END:/ && type == $2 { seq = seq ? seq + 1 : 1; - print "SEQUENCE:" seq; - print "LAST-MODIFIED:" zulu; - if (due) print "DUE;VALUE=DATE:" due; + print_cr("SEQUENCE:" seq); + print_cr("LAST-MODIFIED:" zulu); + if (due) print_cr("DUE;VALUE=DATE:" due); print_fold("SUMMARY:", summary); print_fold("CATEGORIES:", categories); print_fold("DESCRIPTION:", desc); diff --git a/src/lib/awk/icalendar.awk b/src/lib/awk/icalendar.awk index 9da0841..a432575 100644 --- a/src/lib/awk/icalendar.awk +++ b/src/lib/awk/icalendar.awk @@ -33,6 +33,14 @@ function escape_but_commas(str) return str } +# Print line using \r\n ending, as required by the RFC +# +# @input str: String +function print_cr(str) +{ + print str "\r" +} + # Print property with its content and fold according to the iCalendar # specification. # @@ -41,16 +49,16 @@ function escape_but_commas(str) # @input content: Escaped content function print_fold(nameparam, content, i, s) { - i = 74 - length(nameparam) + i = 73 - length(nameparam) s = substr(content, 1, i) - print nameparam s - s = substr(content, i+1, 73) - i = i + 73 + print_cr(nameparam s) + s = substr(content, i+1, 72) + i = i + 72 while (s) { - print " " s - s = substr(content, i+1, 73) - i = i + 73 + print_cr(" " s) + s = substr(content, i+1, 72) + i = i + 72 } }