shithub: Nail

Download patch

ref: 456ca6c5a7d0307ffe95e4d9ecf487fab92028d4
parent: 2fbe0aa0d0416035e94b39c5099877edb55754af
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Nov 4 23:40:14 EST 2020

reorder whole threads when new messages come in

--- a/comp.c
+++ b/comp.c
@@ -127,9 +127,11 @@
 		break;
 		}
 	}
-	for(pc = &mbox.opencomp; *pc != nil; *pc = (*pc)->qnext)
-		if(*pc == c)
+	for(pc = &mbox.opencomp; *pc != nil; pc = &(*pc)->qnext)
+		if(*pc == c){
 			*pc = c->qnext;
+			break;
+		}
 	mbox.nopen--;
 	winclose(c);
 	free(c->replyto);
--- a/mbox.c
+++ b/mbox.c
@@ -150,7 +150,7 @@
 	if(depth != nil)
 		*depth = d;
 	assert(n + o < mbox.nmesg);
-	return n + o;
+	return n + o + 1;
 }
 
 static int
@@ -380,7 +380,7 @@
 	int ln, depth;
 
 	ln = mesglineno(m, &depth);
-	fprint(mbox.addr, "%d%s", ln+1, add ? "-#0" : "");
+	fprint(mbox.addr, "%d%s", ln, add?"-#0":"");
 	bfd = bwindata(&mbox, OWRITE);
 	showmesg(bfd, m, depth, rec);
 	Bterm(bfd);
@@ -535,7 +535,7 @@
 			continue;
 		}
 		ln = mesglineno(m, nil);
-		fprint(mbox.addr, "%d,%d", ln+1, ln+1+m->nsub);
+		fprint(mbox.addr, "%d,%d", ln, ln+m->nsub);
 		write(mbox.data, "", 0);
 		if(m->flags & Ftodel)
 			fprint(fd, "delete %s %d", mailbox, atoi(m->name));
@@ -635,29 +635,53 @@
 	threadexitsall(nil);
 }
 
+/*
+ * shuffle a message to the right location
+ * in the list without doing a full sort.
+ */
 static void
+reinsert(Mesg *m)
+{
+	int i, idx;
+
+	idx = slotfor(m);
+	for(i = idx; i < mbox.nmesg; i++)
+		if(mbox.mesg[i] == m)
+			break;
+	memmove(&mbox.mesg[idx + 1], &mbox.mesg[idx], i - idx);
+	mbox.mesg[idx] = m;
+}
+
+static void
 changemesg(Plumbmsg *pm)
 {
 	char *digest, *action;
-	Mesg *m;
-	int add;
+	Mesg *m, *r;
+	int ln;
 
-	m = nil;
-	add = 0;
-
 	digest = plumblookup(pm->attr, "digest");
 	action = plumblookup(pm->attr, "mailtype");
 //	fprint(2, "changing message %s, %s %s\n", action, pm->data, digest);
 	if(strcmp(action, "new") == 0){
-		m = load(pm->data, digest, 1);
-		add = 1;
-	}else if(strcmp(action, "delete") == 0)
-		m = delete(pm->data, digest);
-	else if(strcmp(action, "modify") == 0)
-		m = change(pm->data, digest);
-	if(m == nil)
-		return;
-	mbredraw(m, add, 0);
+		if((m = load(pm->data, digest, 1)) == nil)
+			return;
+		for(r = m; r->parent != nil; r = r->parent)
+			/* nothing */;
+		/* Bump whole thread up in list */
+		if(r->nsub > 0){
+			ln = mesglineno(r, nil);
+			fprint(mbox.addr, "%d,%d", ln, ln+r->nsub-1);
+			write(mbox.data, "", 0);
+			reinsert(r);
+		}
+		mbredraw(r, 1, 1);
+	}else if(strcmp(action, "delete") == 0){
+		if((m = delete(pm->data, digest)) != nil)
+			mbredraw(m, 0, 0);
+	}else if(strcmp(action, "modify") == 0){
+		if((m = change(pm->data, digest)) != nil)
+			mbredraw(m, 0, 0);
+	}
 }
 
 static void
--- a/mesg.c
+++ b/mesg.c
@@ -387,9 +387,11 @@
 			break;
 		}
 	}
-	for(pm = &mbox.openmesg; *pm != nil; *pm = (*pm)->qnext)
-		if(*pm == m)
+	for(pm = &mbox.openmesg; *pm != nil; pm = &(*pm)->qnext)
+		if(*pm == m){
 			*pm = m->qnext;
+			break;
+		}
 	mbox.nopen--;
 	m->flags &= ~Fopen;
 	winclose(m);