From eaf8f96e065ef30fb78ead5cf1e4007aefd25fbf Mon Sep 17 00:00:00 2001
From: Lukas Fleischer <lfleischer@calcurse.org>
Date: Mon, 15 Feb 2016 08:35:21 +0100
Subject: Improve ordering of appointments/events

* Order by start time first.
* Order items with the same start time by priority.
* Order items with the same start and priority by description.

Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
---
 src/apoint.c | 17 +++++++++++++----
 src/day.c    | 22 +++++++++++++++++++---
 src/event.c  | 13 +++++++++----
 src/recur.c  | 31 ++++++++++++++++++++++---------
 4 files changed, 63 insertions(+), 20 deletions(-)

(limited to 'src')

diff --git a/src/apoint.c b/src/apoint.c
index 1c9aa23..cb23ead 100644
--- a/src/apoint.c
+++ b/src/apoint.c
@@ -84,9 +84,18 @@ void apoint_llist_free(void)
 	LLIST_TS_FREE(&alist_p);
 }
 
-static int apoint_cmp_start(struct apoint *a, struct apoint *b)
+static int apoint_cmp(struct apoint *a, struct apoint *b)
 {
-	return a->start < b->start ? -1 : (a->start == b->start ? 0 : 1);
+	if (a->start < b->start)
+		return -1;
+	if (a->start > b->start)
+		return 1;
+	if ((a->state & APOINT_NOTIFY) && !(b->state & APOINT_NOTIFY))
+		return -1;
+	if (!(a->state & APOINT_NOTIFY) && (b->state & APOINT_NOTIFY))
+		return 1;
+
+	return strcmp(a->mesg, b->mesg);
 }
 
 struct apoint *apoint_new(char *mesg, char *note, long start, long dur,
@@ -102,7 +111,7 @@ struct apoint *apoint_new(char *mesg, char *note, long start, long dur,
 	apt->dur = dur;
 
 	LLIST_TS_LOCK(&alist_p);
-	LLIST_TS_ADD_SORTED(&alist_p, apt, apoint_cmp_start);
+	LLIST_TS_ADD_SORTED(&alist_p, apt, apoint_cmp);
 	LLIST_TS_UNLOCK(&alist_p);
 
 	return apt;
@@ -319,7 +328,7 @@ void apoint_paste_item(struct apoint *apt, long date)
 	apt->start = date + get_item_time(apt->start);
 
 	LLIST_TS_LOCK(&alist_p);
-	LLIST_TS_ADD_SORTED(&alist_p, apt, apoint_cmp_start);
+	LLIST_TS_ADD_SORTED(&alist_p, apt, apoint_cmp);
 	LLIST_TS_UNLOCK(&alist_p);
 
 	if (notify_bar())
diff --git a/src/day.c b/src/day.c
index 3dc8349..2a98706 100644
--- a/src/day.c
+++ b/src/day.c
@@ -66,14 +66,30 @@ void day_free_vector(void)
 	VECTOR_FREE(&day_items);
 }
 
-static int day_cmp_start(struct day_item **pa, struct day_item **pb)
+static int day_cmp(struct day_item **pa, struct day_item **pb)
 {
 	struct day_item *a = *pa;
 	struct day_item *b = *pb;
+	int a_state, b_state;
 
 	if ((a->type == APPT || a->type == RECUR_APPT) &&
 	    (b->type == APPT || b->type == RECUR_APPT)) {
-		return a->start - b->start;
+		if (a->start < b->start)
+			return -1;
+		if (a->start > b->start)
+			return 1;
+
+		a_state = day_item_get_state(a);
+		b_state = day_item_get_state(b);
+		if ((a_state & APOINT_NOTIFY) && !(b_state & APOINT_NOTIFY))
+			return -1;
+		if (!(a_state & APOINT_NOTIFY) && (b_state & APOINT_NOTIFY))
+			return 1;
+
+		return strcmp(day_item_get_mesg(a), day_item_get_mesg(b));
+	} else if ((a->type == EVNT || a->type == RECUR_EVNT) &&
+		   (b->type == EVNT || b->type == RECUR_EVNT)) {
+		return strcmp(day_item_get_mesg(a), day_item_get_mesg(b));
 	}
 
 	return a->type - b->type;
@@ -347,7 +363,7 @@ day_store_items(long date, int include_captions)
 	if (include_captions && events > 0 && apts > 0)
 		day_add_item(DAY_SEPARATOR, 0, p);
 
-	VECTOR_SORT(&day_items, day_cmp_start);
+	VECTOR_SORT(&day_items, day_cmp);
 	day_items_nb = events + apts;
 }
 
diff --git a/src/event.c b/src/event.c
index d2526dc..ad26f19 100644
--- a/src/event.c
+++ b/src/event.c
@@ -78,9 +78,14 @@ void event_llist_free(void)
 	LLIST_FREE(&eventlist);
 }
 
-static int event_cmp_day(struct event *a, struct event *b)
+static int event_cmp(struct event *a, struct event *b)
 {
-	return a->day < b->day ? -1 : (a->day == b->day ? 0 : 1);
+	if (a->day < b->day)
+		return -1;
+	if (a->day > b->day)
+		return 1;
+
+	return strcmp(a->mesg, b->mesg);
 }
 
 /* Create a new event */
@@ -94,7 +99,7 @@ struct event *event_new(char *mesg, char *note, long day, int id)
 	ev->id = id;
 	ev->note = (note != NULL) ? mem_strdup(note) : NULL;
 
-	LLIST_ADD_SORTED(&eventlist, ev, event_cmp_day);
+	LLIST_ADD_SORTED(&eventlist, ev, event_cmp);
 
 	return ev;
 }
@@ -217,5 +222,5 @@ void event_delete(struct event *ev)
 void event_paste_item(struct event *ev, long date)
 {
 	ev->day = date;
-	LLIST_ADD_SORTED(&eventlist, ev, event_cmp_day);
+	LLIST_ADD_SORTED(&eventlist, ev, event_cmp);
 }
diff --git a/src/recur.c b/src/recur.c
index 55f9a12..186acbe 100644
--- a/src/recur.c
+++ b/src/recur.c
@@ -181,15 +181,28 @@ void recur_event_llist_free(void)
 }
 
 static int
-recur_apoint_cmp_start(struct recur_apoint *a, struct recur_apoint *b)
+recur_apoint_cmp(struct recur_apoint *a, struct recur_apoint *b)
 {
-	return a->start < b->start ? -1 : (a->start == b->start ? 0 : 1);
+	if (a->start < b->start)
+		return -1;
+	if (a->start > b->start)
+		return 1;
+	if ((a->state & APOINT_NOTIFY) && !(b->state & APOINT_NOTIFY))
+		return -1;
+	if (!(a->state & APOINT_NOTIFY) && (b->state & APOINT_NOTIFY))
+		return 1;
+
+	return strcmp(a->mesg, b->mesg);
 }
 
-static int recur_event_cmp_day(struct recur_event *a,
-			       struct recur_event *b)
+static int recur_event_cmp(struct recur_event *a, struct recur_event *b)
 {
-	return a->day < b->day ? -1 : (a->day == b->day ? 0 : 1);
+	if (a->day < b->day)
+		return -1;
+	if (a->day > b->day)
+		return 1;
+
+	return strcmp(a->mesg, b->mesg);
 }
 
 /* Insert a new recursive appointment in the general linked list */
@@ -218,7 +231,7 @@ struct recur_apoint *recur_apoint_new(char *mesg, char *note, long start,
 	}
 
 	LLIST_TS_LOCK(&recur_alist_p);
-	LLIST_TS_ADD_SORTED(&recur_alist_p, rapt, recur_apoint_cmp_start);
+	LLIST_TS_ADD_SORTED(&recur_alist_p, rapt, recur_apoint_cmp);
 	LLIST_TS_UNLOCK(&recur_alist_p);
 
 	return rapt;
@@ -246,7 +259,7 @@ struct recur_event *recur_event_new(char *mesg, char *note, long day,
 		LLIST_INIT(&rev->exc);
 	}
 
-	LLIST_ADD_SORTED(&recur_elist, rev, recur_event_cmp_day);
+	LLIST_ADD_SORTED(&recur_elist, rev, recur_event_cmp);
 
 	return rev;
 }
@@ -966,7 +979,7 @@ void recur_event_paste_item(struct recur_event *rev, long date)
 		exc->st += time_shift;
 	}
 
-	LLIST_ADD_SORTED(&recur_elist, rev, recur_event_cmp_day);
+	LLIST_ADD_SORTED(&recur_elist, rev, recur_event_cmp);
 }
 
 void recur_apoint_paste_item(struct recur_apoint *rapt, long date)
@@ -986,7 +999,7 @@ void recur_apoint_paste_item(struct recur_apoint *rapt, long date)
 	}
 
 	LLIST_TS_LOCK(&recur_alist_p);
-	LLIST_TS_ADD_SORTED(&recur_alist_p, rapt, recur_apoint_cmp_start);
+	LLIST_TS_ADD_SORTED(&recur_alist_p, rapt, recur_apoint_cmp);
 	LLIST_TS_UNLOCK(&recur_alist_p);
 
 	if (notify_bar())
-- 
cgit v1.2.3-70-g09d2