From 5f1c8e1ab67c929eccdf9c3fb9e8b4d33900bdec Mon Sep 17 00:00:00 2001
From: Don Roberts <roberts@refactory.com>
Date: Sat, 30 Jul 2011 09:20:09 -0500
Subject: [PATCH] org-mac-ical: Clean up org-mac-iCal-range checking to
 eliminate end-of-year special cases and include repeating
 events in diary file

---
 contrib/lisp/org-mac-iCal.el |   28 +++++++++++++++-------------
 1 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/contrib/lisp/org-mac-iCal.el b/contrib/lisp/org-mac-iCal.el
index 83804c1..ee1c073 100644
--- a/contrib/lisp/org-mac-iCal.el
+++ b/contrib/lisp/org-mac-iCal.el
@@ -162,23 +162,25 @@ date range so that Emacs calendar view doesn't grind to a halt"
 	(re-search-forward "\\(^DTSTART;.*:\\)\\([0-9][0-9][0-9][0-9]\\)\\([0-9][0-9]\\)" nil t)
 	(if (or (eq (match-string 2) nil) (eq (match-string 3) nil))
 	    (progn
-	      (setq yearEntry 0)
-	      (setq monthEntry 0))
+	      (setq yearEntry 1)
+	      (setq monthEntry 1))
 	  (setq yearEntry (string-to-number (match-string 2)))
 	  (setq monthEntry (string-to-number (match-string 3))))
 	(setq year (string-to-number (format-time-string "%Y")))
 	(setq month (string-to-number (format-time-string "%m")))
-	(when (or
-	       (and
-		(= yearEntry year)
-		(or (< monthEntry (- month (/ org-mac-iCal-range 2))) (> monthEntry (+ month (/ org-mac-iCal-range 2)))))
-	       (< yearEntry (- year 1))
-	       (> yearEntry (+ year 1))
-	       (and
-		(= yearEntry (- year 1)) (/= monthEntry 12))
-	       (and
-		(= yearEntry (+ year 1)) (/= monthEntry 1)))
-	  (delete-region startEntry endEntry))))
+        (setq now (list month 1 year))
+        (setq entryDate (list monthEntry 1 yearEntry))
+        ;; Check to see if this is a repeating event
+        (goto-char (point-min))
+        (setq isRepeating (re-search-forward "^RRULE:" nil t))
+	;; Delete if outside range and not repeating
+        (when (and
+               (not isRepeating)
+               (> (abs (- (calendar-absolute-from-gregorian now)
+                          (calendar-absolute-from-gregorian entryDate)))
+                  (* (/ org-mac-iCal-range 2) 30))
+	  (delete-region startEntry endEntry)))
+          (goto-char (point-max))))
     (while 
 	(re-search-forward "^END:VEVENT$" nil t)
       (delete-blank-lines))
-- 
1.7.6

