shithub: riscv

Download patch

ref: b86a38ed13145b4a3961c1c60a3be80594579256
parent: 2036c493940eea8cd9d67fe792713d38b81547af
author: cinap_lenrek <[email protected]>
date: Sun Oct 2 10:27:03 EDT 2011

mothra: mothmode cleanup

--- a/sys/src/cmd/mothra/mothra.c
+++ b/sys/src/cmd/mothra/mothra.c
@@ -238,6 +238,16 @@
 		f=create(stem, OWRITE, mode);
 	return f;
 }
+
+void donecurs(void){
+	if(current == nil)
+		return;
+	if(mothmode)
+		esetcursor(&mothcurs);
+	else
+		esetcursor(current->alldone ? 0 : &readingcurs);
+}
+
 void main(int argc, char *argv[]){
 	Event e;
 	enum { Eplumb = 128 };
@@ -327,8 +337,7 @@
 				current->changed=0;
 				current->alldone=1;
 				message(mothra);
-				if(mothmode==0)
-					esetcursor(0);
+				donecurs();
 			}
 			else if(current->changed){
 				updtext(current);
@@ -369,11 +378,6 @@
 			break;
 		case Emouse:
 			mouse=e.mouse;
-			if(mouse.buttons > 1){
-				mothmode = 0;
-				esetcursor(current && current->alldone?0:&readingcurs);
-				message(mothra);
-			}
 			plmouse(root, e.mouse);
 			break;
 		case Eplumb:
@@ -457,13 +461,6 @@
 	return buf;
 }
 
-void donecurs(void){
-	if(mothmode)
-		esetcursor(current && current->alldone?0:&mothcurs);
-	else
-		esetcursor(current && current->alldone?0:&readingcurs);
-}
-
 /*
  * selected text should be a url.
  * get the document, scroll to the given tag
@@ -661,47 +658,42 @@
  * convert a url into a local file name.
  */
 char *urltofile(char *url){
-	static char file[128];
-	if(strrchr(url, '/')){
-		snprint(file, sizeof(file), "%s", strrchr(url, '/')+1);
-		if(file[0]==0)
-			strcpy(file, "index");
-	}else
-		snprint(file, sizeof(file), "%s", url);
-	return file;
+	char *slash;
+	if(slash = strrchr(url, '/'))
+		url = slash+1;
+	if(url[0] == 0)
+		return "index";
+	return url;
 }
+
 /*
  * Follow an html link
  */
 void dolink(Panel *p, int buttons, Rtext *word){
-	char file[128], mapurl[NNAME];
+	char *file, mapurl[NNAME];
+	Point coord;
+	int yoffs;
 	Action *a;
 	Url u;
-	Point coord;
-	int fd, yoffs;
-	USED(p);
+
 	a=word->user;
-	if(mothmode && a){
-		if(a->image){
-			seturl(&u, a->image, current->url->fullname);
-			snprint(file, sizeof(file), "%s", urltofile(a->image));
-		}else{
-			seturl(&u, a->link, current->url->fullname);
-			snprint(file, sizeof(file), "%s", urltofile(a->link));
-		}
-		save(&u, file);
+	if(a == nil || a->image == nil && a->link == nil)
+		return;
+	if(mothmode){
+		seturl(&u, a->image ? a->image : a->link, current->url->fullname);
+		save(&u, file=urltofile(u.reltext));
 		message("saved %s", file);
-		esetcursor(&mothcurs);
-	}else if(a && a->link){
-		if(a->ismap){
-			yoffs=plgetpostextview(p);
-			coord=subpt(subpt(mouse.xy, word->r.min), p->r.min);
-			snprint(mapurl, sizeof(mapurl), "%s?%d,%d", a->link, coord.x, coord.y+yoffs);
-			hiturl(buttons, mapurl, 1);
-		}else
-			hiturl(buttons, a->link, 0);
+		return;
 	}
+	if(a->ismap){
+		yoffs=plgetpostextview(p);
+		coord=subpt(subpt(mouse.xy, word->r.min), p->r.min);
+		snprint(mapurl, sizeof(mapurl), "%s?%d,%d", a->link, coord.x, coord.y+yoffs);
+		hiturl(buttons, mapurl, 1);
+	}else
+		hiturl(buttons, a->link ? a->link : a->image, 0);
 }
+
 void filter(char *cmd, int fd){
 	flushimage(display, 1);
 	switch(rfork(RFFDG|RFPROC|RFNOWAIT)){
@@ -1116,9 +1108,11 @@
 		pldraw(root, screen);
 		break;
 	case 1:
-		mothmode = 1;
-		message("moth mode");
-		esetcursor(&mothcurs);
+		if(mothmode = !mothmode)
+			message("moth mode!");
+		else
+			message(mothra);
+		donecurs();
 		break;
 	case 2:
 		snarf(cmd);
--- a/sys/src/cmd/mothra/rdhtml.c
+++ b/sys/src/cmd/mothra/rdhtml.c
@@ -147,7 +147,8 @@
 			space=1000000;
 		}
 	}
-	plrtstr(&g->dst->text, space, indent, f->font, strdup(s), g->state->link[0]!=0, ap);
+	plrtstr(&g->dst->text, space, indent, f->font, strdup(s),
+		g->state->link[0] || g->state->image[0], ap);
 	g->para=0;
 	g->linebrk=0;
 	g->dst->changed=1;
@@ -639,11 +640,8 @@
 		case Tag_meta:
 			break;
 		case Tag_img:
-			if(str=pl_getattr(g.attr, "src")){
+			if(str=pl_getattr(g.attr, "src"))
 				strncpy(g.state->image, str, sizeof(g.state->image));
-				if(g.state->link[0]==0)
-					strncpy(g.state->link, str, sizeof(g.state->link));
-			}
 			g.state->ismap=pl_hasattr(g.attr, "ismap");
 			if(str=pl_getattr(g.attr, "width"))
 				g.state->width = strtolength(&g, HORIZ, str);