about summary refs log tree commit diff
path: root/Functions/Calendar/calendar_add
diff options
context:
space:
mode:
authorPeter Stephenson <pws@users.sourceforge.net>2006-12-01 10:23:06 +0000
committerPeter Stephenson <pws@users.sourceforge.net>2006-12-01 10:23:06 +0000
commit6b1b34d1da6b0db599c026e17df011ad6c6b3a30 (patch)
tree3559a344ebd62a8b0af17c70b9b0fdaeae5e2299 /Functions/Calendar/calendar_add
parentab8b8026dcc17f7c3d8dcfba7dba046b1ac7c42b (diff)
downloadzsh-6b1b34d1da6b0db599c026e17df011ad6c6b3a30.tar.gz
zsh-6b1b34d1da6b0db599c026e17df011ad6c6b3a30.tar.xz
zsh-6b1b34d1da6b0db599c026e17df011ad6c6b3a30.zip
c.f. 23023: new calendar function system.
Diffstat (limited to 'Functions/Calendar/calendar_add')
-rw-r--r--Functions/Calendar/calendar_add69
1 files changed, 69 insertions, 0 deletions
diff --git a/Functions/Calendar/calendar_add b/Functions/Calendar/calendar_add
new file mode 100644
index 000000000..2a00811fd
--- /dev/null
+++ b/Functions/Calendar/calendar_add
@@ -0,0 +1,69 @@
+#!/bin/env zsh
+
+# All arguments are joined with spaces and inserted into the calendar
+# file at the appropriate point.
+#
+# While the function compares the date of the new entry with dates in the
+# existing calendar file, it does not do any sorting; it inserts the new
+# entry before the first existing entry with a later date and time.
+
+emulate -L zsh
+setopt extendedglob
+
+local calendar newfile REPLY lastline
+local -a calendar_entries lockfiles
+integer newdate done rstat
+
+autoload -U calendar_{read,lockfiles}
+
+# Read the calendar file from the calendar-file style
+zstyle -s ':datetime:calendar_add:' calendar-file calendar ||
+  calendar=~/calendar
+newfile=$calendar.new.$HOST.$$
+
+if ! calendar_scandate -a "$*"; then
+  print "$0: failed to parse date/time" >&2
+  return 1
+fi
+(( newdate = $REPLY ))
+
+# $calendar doesn't necessarily exist yet.
+
+# start of block for following always to clear up lockfiles.
+{
+  calendar_lockfiles $calendar || return 1
+
+  if [[ -f $calendar ]]; then
+    calendar_read $calendar
+
+    {
+      for line in $calendar_entries; do
+	if (( ! done )) && calendar_scandate -a $line && (( REPLY > newdate )); then
+	  print -r -- "$*"
+	  (( done = 1 ))
+	elif [[ $REPLY -eq $newdate && $line = "$*" ]]; then
+	  (( done = 1 ))
+	fi
+	print -r -- $line
+	done
+	(( done )) || print -r -- "$*"
+    } >$newfile
+    if ! mv $calendar $calendar.old; then
+      print "Couldn't back up $calendar to $calendar.old.
+New calendar left in $newfile." >&2
+      (( rstat = 1 ))
+    fi
+  else
+    print -r -- $line >$newfile
+  fi
+
+  if (( !rstat )) && ! mv $newfile $calendar; then
+    print "Failed to rename $newfile to $calendar.
+Old calendar left in $calendar.old." >&2
+    (( rstat = 1 ))
+  fi
+} always {
+  (( ${#lockfiles} )) && rm -f $lockfiles
+}
+
+return $rstat