From 13a41f8e0a722f4491ad6256718227e7161b5896 Mon Sep 17 00:00:00 2001
From: Lukas Fleischer <calcurse@cryptocrack.de>
Date: Sun, 24 Jul 2011 22:46:26 +0200
Subject: Fix sort order in app_arg()

We used to iterate over the list of regular appointments and the list of
recurrent appointments in separate loops, thus leading to recurrent
appointments being printed first and regular appointments being printed
afterwards, regardless of their start and end times. Merge these loops
to coerce precedence of the start time.

There's still a fair bit of hackery in here - we will fix that later.

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
---
 src/args.c | 130 ++++++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 78 insertions(+), 52 deletions(-)

(limited to 'src')

diff --git a/src/args.c b/src/args.c
index 652736a..e447997 100644
--- a/src/args.c
+++ b/src/args.c
@@ -351,7 +351,7 @@ static int
 app_arg (int add_line, struct date *day, long date, int print_note,
          struct conf *conf, regex_t *regex)
 {
-  llist_item_t *i;
+  llist_item_t *i, *j;
   long today;
   unsigned print_date = 1;
   int app_found = 0;
@@ -417,72 +417,98 @@ app_arg (int add_line, struct date *day, long date, int print_note,
     }
 
   /* Same process is performed but this time on the appointments. */
+  LLIST_TS_LOCK (&alist_p);
   LLIST_TS_LOCK (&recur_alist_p);
-  LLIST_TS_FIND_FOREACH (&recur_alist_p, today, recur_apoint_inday, i)
-    {
-      struct recur_apoint *ra = LLIST_TS_GET_DATA (i);
-      struct apoint *apt;
 
-      if (regex && regexec (regex, ra->mesg, 0, 0, 0) != 0)
-        continue;
+  /*
+   * Iterate over regular appointments and recurrent ones simultaneously (fixes
+   * http://lists.calcurse.org/bugs/msg00002.html).
+   */
+  i = LLIST_TS_FIND_FIRST (&alist_p, today, apoint_inday);
+  j = LLIST_TS_FIND_FIRST (&recur_alist_p, today, recur_apoint_inday);
+  while (i || j)
+    {
+      struct apoint *apt = LLIST_TS_GET_DATA (i);
+      struct recur_apoint *ra = LLIST_TS_GET_DATA (j);
 
-      app_found = 1;
-      if (add_line)
+      while (i && regex && regexec (regex, apt->mesg, 0, 0, 0) != 0)
         {
-          fputs ("\n", stdout);
-          add_line = 0;
+          i = LLIST_TS_FIND_NEXT (i, today, apoint_inday);
+          apt = LLIST_TS_GET_DATA (i);
         }
-      if (print_date)
+
+      while (j && regex && regexec (regex, ra->mesg, 0, 0, 0) != 0)
         {
-          arg_print_date (today, conf);
-          print_date = 0;
+          j = LLIST_TS_FIND_NEXT (j, today, recur_apoint_inday);
+          ra = LLIST_TS_GET_DATA (j);
         }
-      apt = apoint_recur_s2apoint_s (ra);
-      apoint_sec2str (apt, RECUR_APPT, today, apoint_start_time,
-                      apoint_end_time);
-      mem_free (apt->mesg);
-      mem_free (apt);
-      fputs (" - ", stdout);
-      fputs (apoint_start_time, stdout);
-      fputs (" -> ", stdout);
-      fputs (apoint_end_time, stdout);
-      fputs ("\n\t", stdout);
-      fputs (ra->mesg, stdout);
-      fputs ("\n", stdout);
-      if (print_note && ra->note)
-        print_notefile (stdout, ra->note, 2);
-    }
-  LLIST_TS_UNLOCK (&recur_alist_p);
 
-  LLIST_TS_LOCK (&alist_p);
-  LLIST_TS_FIND_FOREACH (&alist_p, today, apoint_inday, i)
-    {
-      struct apoint *apt = LLIST_TS_GET_DATA (i);
-      if (regex && regexec (regex, apt->mesg, 0, 0, 0) != 0)
-        continue;
+      if (apt && ra)
+        {
+          if (apt->start <= recur_apoint_inday (ra, today))
+            ra = NULL;
+          else
+            apt = NULL;
+        }
 
-      app_found = 1;
-      if (add_line)
+      if (apt)
         {
+          app_found = 1;
+          if (add_line)
+            {
+              fputs ("\n", stdout);
+              add_line = 0;
+            }
+          if (print_date)
+            {
+              arg_print_date (today, conf);
+              print_date = 0;
+            }
+          apoint_sec2str (apt, APPT, today, apoint_start_time, apoint_end_time);
+          fputs (" - ", stdout);
+          fputs (apoint_start_time, stdout);
+          fputs (" -> ", stdout);
+          fputs (apoint_end_time, stdout);
+          fputs ("\n\t", stdout);
+          fputs (apt->mesg, stdout);
           fputs ("\n", stdout);
-          add_line = 0;
+          if (print_note && apt->note)
+            print_notefile (stdout, apt->note, 2);
+          i = LLIST_TS_FIND_NEXT (i, today, apoint_inday);
         }
-      if (print_date)
+      else if (ra)
         {
-          arg_print_date (today, conf);
-          print_date = 0;
+          app_found = 1;
+          if (add_line)
+            {
+              fputs ("\n", stdout);
+              add_line = 0;
+            }
+          if (print_date)
+            {
+              arg_print_date (today, conf);
+              print_date = 0;
+            }
+          apt = apoint_recur_s2apoint_s (ra);
+          apoint_sec2str (apt, RECUR_APPT, today, apoint_start_time,
+                          apoint_end_time);
+          mem_free (apt->mesg);
+          mem_free (apt);
+          fputs (" - ", stdout);
+          fputs (apoint_start_time, stdout);
+          fputs (" -> ", stdout);
+          fputs (apoint_end_time, stdout);
+          fputs ("\n\t", stdout);
+          fputs (ra->mesg, stdout);
+          fputs ("\n", stdout);
+          if (print_note && ra->note)
+            print_notefile (stdout, ra->note, 2);
+          apt = NULL;
+          j = LLIST_TS_FIND_NEXT (j, today, recur_apoint_inday);
         }
-      apoint_sec2str (apt, APPT, today, apoint_start_time, apoint_end_time);
-      fputs (" - ", stdout);
-      fputs (apoint_start_time, stdout);
-      fputs (" -> ", stdout);
-      fputs (apoint_end_time, stdout);
-      fputs ("\n\t", stdout);
-      fputs (apt->mesg, stdout);
-      fputs ("\n", stdout);
-      if (print_note && apt->note)
-        print_notefile (stdout, apt->note, 2);
     }
+
+  LLIST_TS_UNLOCK (&recur_alist_p);
   LLIST_TS_UNLOCK (&alist_p);
 
   return (app_found);
-- 
cgit v1.2.3-70-g09d2