shithub: mongrel

Download patch

ref: 04e7a77895b3f00e513f70d5bfd956391bfc04e2
parent: f32e1f6267b2d95699483430af9afb9097b16b03
author: phil9 <telephil9@gmail.com>
date: Sat Feb 12 01:19:10 EST 2022

implement mailbox switching

	it is now possible to switch between loaded mailboxes by right-clicking
	in the header bar.
	In addition, mailboxes are lazy-loaded meaning that the mails will only
	be loaded the first time we try to display the mailbox.

--- a/README.md
+++ b/README.md
@@ -11,6 +11,7 @@
 % mk install
 % mongrel -m mbox
 ```
+**NOTE:** you can pass multiple -m parameters to load multiple mailboxes.
 
 ## Usage
 mongrel has two components:
@@ -22,6 +23,8 @@
 - delete deletes the message
 
 In the pager, navigation is done using either the scrollbar or the mouse wheel. It is also possible to use keyboard navigation by pressing `Alt` in addition to the arrow keys, page up, page down, home and end.
+
+Right-clicking in the header bar (the one displaying the current mailbox name) will show a menu allowing to switch between mailboxes.
 
 Other shortcuts:
 - `q` will hide the pager if it is open or quit mongrel if in the index.  
--- a/a.h
+++ b/a.h
@@ -8,6 +8,7 @@
 	Lock;
 	char	*name;
 	char	*path;
+	int		loaded;
 	int		count;
 	int		unseen;
 	Mlist*	list;
@@ -63,7 +64,8 @@
 	Emodify,
 };
 
-Mailbox* loadmbox(char *name);
+Mailbox* mboxinit(char *name);
+void mboxload(Mailbox*);
 void mesgloadbody(Message*);
 int mesgmarkseen(Mailbox*, Message*);
 int mboxadd(Mailbox *mbox, char *path);
--- a/main.c
+++ b/main.c
@@ -40,6 +40,8 @@
 Channel *eventc;
 Mailbox *mboxes[16];
 int nmboxes;
+char *mbmenustr[16] = {0};
+Menu mbmenu = { mbmenustr };
 Mailbox *mbox;
 static Image *cols[NCOLS];
 Rectangle headr;
@@ -95,8 +97,29 @@
 }
 
 void
+switchmbox(int n)
+{
+	if(mbox==mboxes[n])
+		return;
+	mbox = mboxes[n];
+	if(!mbox->loaded)
+		mboxload(mbox);
+	indexswitch(mbox);
+	collapsed = 0;
+	resize();
+}
+
+void
 mouse(Mouse m)
 {
+	int n;
+
+	if(ptinrect(m.xy, headr) && m.buttons==4){
+		n = menuhit(3, mctl, &mbmenu, nil);
+		if(n >= 0)
+			switchmbox(n);
+		return;
+	}
 	indexmouse(m);
 	if(collapsed)
 		pagermouse(m);
@@ -187,15 +210,6 @@
 }
 
 void
-switchmbox(int n)
-{
-	if(mbox==mboxes[n])
-		return;
-	mbox = mboxes[n];
-	indexswitch(mbox);
-}
-
-void
 plumbmsg(Message *m)
 {
 	int fd;
@@ -258,7 +272,9 @@
 	ARGBEGIN{
 	case 'm':
 		s = EARGF(usage());
-		mboxes[nmboxes++] = loadmbox(s);
+		mboxes[nmboxes] = mboxinit(s);
+		mbmenustr[nmboxes] = mboxes[nmboxes]->name;
+		nmboxes++;
 		break;
 	default:
 		fprint(2, "unknown flag '%c'\n", ARGC());
--- a/mbox.c
+++ b/mbox.c
@@ -186,13 +186,9 @@
 }
 
 Mailbox*
-loadmbox(char *name)
+mboxinit(char *name)
 {
 	Mailbox *mb;
-	Dir *d;
-	int n, fd, i;
-	char buf[256];
-	Message *m;
 
 	mb = mallocz(sizeof(Mailbox), 1);
 	if(mb==nil)
@@ -199,6 +195,18 @@
 		sysfatal("malloc: %r");
 	mb->name = strdup(name);
 	mb->path = smprint("/mail/fs/%s", name);
+	mb->loaded = 0;
+	return mb;
+}
+
+void
+mboxload(Mailbox *mb)
+{
+	Dir *d;
+	int n, fd, i;
+	char buf[256];
+	Message *m;
+
 	fd = open(mb->path, OREAD);
 	if(fd<0)
 		sysfatal("open: %r");
@@ -218,7 +226,7 @@
 		++mb->count;
 	}
 	free(d);
-	return mb;
+	mb->loaded = 1;
 }
 
 int