shithub: fm

Download patch

ref: fd11b89767dc2586e0a79cd5b276df5e1c2e9ae0
parent: 6caad0e0a9bf45e8a86219b574599622d021e683
author: phil9 <[email protected]>
date: Sun Nov 14 17:39:30 EST 2021

add mouse selection and activation

	it is now possible to select a match from the list
	using mouse1 and activate it using mouse3

--- a/main.c
+++ b/main.c
@@ -92,6 +92,27 @@
 	Bterm(bp);
 }
 
+void
+activate(void)
+{
+	Match m;
+
+	m = matches[lsel + loff];
+	if(pmode){
+		print("%s", m.n);
+		threadexitsall(nil);
+	}
+	plumbsendtext(plumbfd, argv0, nil, pwd, m.n);
+}
+
+int
+lineat(Point p)
+{
+	if(ptinrect(p, lr) == 0)
+		return -1;
+	return (p.y - lr.min.y) / lh;	
+}
+
 Rectangle
 linerect(int i)
 {
@@ -136,23 +157,6 @@
 	flushimage(display, 1);
 }
 
-void
-eresize(void)
-{
-	ir = Rect(Padding, Padding, screen->r.max.x - Padding, Padding + font->height + Padding);
-	ir = rectaddpt(ir, screen->r.min);
-	lr = Rect(screen->r.min.x + Padding, ir.max.y, ir.max.x, screen->r.max.y - Padding);
-	lh = font->height + Padding;
-	lcount = Dy(lr) / lh;
-	redraw();
-}
-
-void
-emouse(Mouse *m)
-{
-	USED(m);
-}
-
 int
 scroll(int lines, int setsel)
 {
@@ -207,9 +211,34 @@
 }
 
 void
+eresize(void)
+{
+	ir = Rect(Padding, Padding, screen->r.max.x - Padding, Padding + font->height + Padding);
+	ir = rectaddpt(ir, screen->r.min);
+	lr = Rect(screen->r.min.x + Padding, ir.max.y, ir.max.x, screen->r.max.y - Padding);
+	lh = font->height + Padding;
+	lcount = Dy(lr) / lh;
+	redraw();
+}
+
+void
+emouse(Mouse *m)
+{
+	int n;
+
+	if(m->buttons == 1){
+		if((n = lineat(m->xy)) != -1){
+			changesel(lsel, n);
+			lsel = n;
+		}
+	}else if(m->buttons == 4){
+		activate();
+	}
+}
+
+void
 ekeyboard(Rune k)
 {
-	Match m;
 	int osel;
 
 	switch(k){
@@ -250,14 +279,8 @@
 		}
 		break;
 	case '\n':
-		if(lsel >= 0){
-			m = matches[lsel + loff];
-			if(pmode){
-				print("%s", m.n);
-				threadexitsall(nil);
-			}else
-				plumbsendtext(plumbfd, argv0, nil, pwd, m.n);
-		}
+		if(lsel >= 0)
+			activate();
 		break;
 	case Kesc:
 		if(ninput > 0){