shithub: riscv

Download patch

ref: 97f742c860b78599342caca96e43f608a7c34a5d
parent: f2c136f9039c1c9a45e01e0f9296f2971754ddd7
author: cinap_lenrek <cinap_lenrek@centraldogma>
date: Sun Sep 11 00:03:30 EDT 2011

npage: cleanup

--- a/sys/src/cmd/npage.c
+++ b/sys/src/cmd/npage.c
@@ -124,6 +124,19 @@
 	return p;
 }
 
+void
+resizewin(Point size)
+{
+	int wctl;
+
+	if((wctl = open("/dev/wctl", OWRITE)) < 0)
+		return;
+	/* add rio border */
+	size = addpt(size, Pt(Borderwidth*2, Borderwidth*2));
+	fprint(wctl, "resize -dx %d -dy %d\n", size.x, size.y);
+	close(wctl);
+}
+
 int
 createtmp(ulong id, char *pfx)
 {
@@ -555,6 +568,25 @@
 	return nil;
 }
 
+int
+openpage(Page *p)
+{
+	int fd;
+
+	fd = -1;
+	if(p->open == nil || (fd = p->open(p)) < 0)
+		p->open = nil;
+	else {
+		if(rotate)
+			pipeline(fd, "rotate -r %d", rotate);
+		if(resize.x)
+			pipeline(fd, "resize -x %d", resize.x);
+		else if(resize.y)
+			pipeline(fd, "resize -y %d", resize.y);
+	}
+	return fd;
+}
+
 void
 loadpage(Page *p)
 {
@@ -561,16 +593,8 @@
 	int fd;
 
 	if(p->open && p->image == nil && p->text == nil){
-		if((fd = p->open(p)) < 0)
-			p->open = nil;
-		else {
+		if((fd = openpage(p)) >= 0){
 			pagegen++;
-			if(rotate)
-				pipeline(fd, "rotate -r %d", rotate);
-			if(resize.x)
-				pipeline(fd, "resize -x %d", resize.x);
-			else if(resize.y)
-				pipeline(fd, "resize -y %d", resize.y);
 			p->image = readimage(display, fd, 1);
 			close(fd);
 		}
@@ -613,18 +637,14 @@
 }
 
 void
-resizewin(Point);
-
-void
 loadpages(Page *p, int ahead, int oviewgen)
 {
 	Point size;
 	int i;
 
-	unloadpages(NAHEAD*2);
-
 	ahead++;	/* load at least one */
-	for(i = 0; i < ahead && p; p = nextpage(p)){
+	unloadpages(ahead*2);
+	for(i = 0; i < ahead && p; p = nextpage(p), i++){
 		if(viewgen != oviewgen)
 			break;
 		if(canqlock(p)){
@@ -636,7 +656,6 @@
 			}
 			size = p->image ? subpt(p->image->r.max, p->image->r.min) : ZP;
 			qunlock(p);
-			i++;
 
 			if(p == current){
 				if(size.x && size.y && newwin){
@@ -712,19 +731,6 @@
 }
 
 void
-resizewin(Point size)
-{
-	int wctl;
-
-	if((wctl = open("/dev/wctl", OWRITE)) < 0)
-		return;
-	/* add rio border */
-	size = addpt(size, Pt(Borderwidth, Borderwidth));
-	fprint(wctl, "resize -dx %d -dy %d\n", size.x, size.y);
-	close(wctl);
-}
-
-void
 eresized(int new)
 {
 	Rectangle r;
@@ -737,7 +743,6 @@
 			sysfatal("getwindow: %r");
 		unlockdisplay(display);
 	}
-
 	if((p = current) == nil)
 		return;
 
@@ -745,22 +750,23 @@
 	lockdisplay(display);
 	if((i = p->image) == nil){
 		char *s;
+
 		if((s = p->text) == nil)
-			goto Out;
+			s = "...";
 		r.min = ZP;
 		r.max = stringsize(font, p->text);
-		if((i = allocimage(display, r, screen->chan, 0, DWhite)) == nil)
-			goto Out;
-		string(i, r.min, display->black, ZP, font, s);
-		p->image = i;
+		r = rectaddpt(r, addpt(subpt(divpt(subpt(screen->r.max, screen->r.min), 2), divpt(r.max, 2)),
+			screen->r.min));
+		draw(screen, r, display->white, nil, ZP);
+		string(screen, r.min, display->black, ZP, font, s);
+	} else {
+		r = rectaddpt(rectaddpt(Rpt(ZP, subpt(i->r.max, i->r.min)), screen->r.min), pos);
+		draw(screen, r, i, nil, i->r.min);
 	}
-	r = rectaddpt(rectaddpt(Rpt(ZP, subpt(i->r.max, i->r.min)), screen->r.min), pos);
-	draw(screen, r, i, nil, i->r.min);
 	gendrawdiff(screen, screen->r, r, display->white, ZP, nil, ZP, S);
 	border(screen, r, -Borderwidth, display->black, ZP);
 	flushimage(display, 1);
 	esetcursor(nil);
-Out:
 	unlockdisplay(display);
 	qunlock(p);
 }
@@ -855,29 +861,6 @@
 	}
 }
 
-void killcohort(void)
-{
-	int i;
-	for(i=0;i!=3;i++){	/* It's a long way to the kitchen */
-		postnote(PNGROUP, getpid(), "kill");
-		sleep(1);
-	}
-}
-void drawerr(Display *, char *msg)
-{
-	sysfatal("draw: %s", msg);
-}
-
-char*
-shortname(char *s)
-{
-	char *x;
-	if(x = strrchr(s, '/'))
-		if(x[1] != 0)
-			return x+1;
-	return s;
-}
-
 void
 zerox(Page *p)
 {
@@ -888,10 +871,8 @@
 		return;
 	esetcursor(&reading);
 	qlock(p);
-	if(p->open == nil || (fd = p->open(p)) < 0){
-		p->open = nil;
+	if((fd = openpage(p)) < 0)
 		goto Out;
-	}
 	if(rfork(RFREND|RFFDG|RFPROC|RFENVG|RFNOTEG|RFNOWAIT) == 0){
 		dup(fd, 0);
 		close(fd);
@@ -909,6 +890,29 @@
 	esetcursor(nil);
 }
 
+void killcohort(void)
+{
+	int i;
+	for(i=0;i!=3;i++){	/* It's a long way to the kitchen */
+		postnote(PNGROUP, getpid(), "kill");
+		sleep(1);
+	}
+}
+void drawerr(Display *, char *msg)
+{
+	sysfatal("draw: %s", msg);
+}
+
+char*
+shortname(char *s)
+{
+	char *x;
+	if(x = strrchr(s, '/'))
+		if(x[1] != 0)
+			return x+1;
+	return s;
+}
+
 void
 usage(void)
 {
@@ -1056,6 +1060,7 @@
 			switch(e.kbdc){
 			case 'q':
 			case Kdel:
+			case Keof:
 				exits(0);
 			case Kup:
 				lockdisplay(display);
@@ -1073,7 +1078,7 @@
 			case Kdown:
 				lockdisplay(display);
 				o = addpt(pos, pagesize(current));
-				if(o.y >= Dy(screen->r)){
+				if(o.y > Dy(screen->r)){
 					translate(current, Pt(0, -Dy(screen->r)/2));
 					unlockdisplay(display);
 					continue;
@@ -1094,6 +1099,8 @@
 
 				fd = -1;
 				s = plumblookup(pm->attr, "action");
+				if(s && strcmp(s, "quit")==0)
+					exits(0);
 				if(s && strcmp(s, "showdata")==0){
 					static ulong plumbid;