shithub: castor9

Download patch

ref: 69cadae851b48fc297cb5293c7899dd43dd499e5
parent: 6227d00cc133e573e42aaf95c43c8a1fdbadbb3a
author: Julien Blanchard <[email protected]>
date: Thu Dec 31 09:33:22 EST 2020

Bookmark a page, refact current_url stuff

--- a/castor.c
+++ b/castor.c
@@ -43,6 +43,7 @@
 void gemini_get(Url *u);
 void gemini_put(Response *r);
 void texthit(Panel *p, int b, Rtext *t);
+void addbookmark(void);
 void message(char *s, ...);
 
 Panel *root;
@@ -53,7 +54,6 @@
 Panel *textp;
 Panel *statusp;
 Panel *popup;
-Url *current_base_url;
 Mouse *mouse;
 Hist *hist = nil;
 int preformatted = 0;
@@ -63,6 +63,8 @@
 	Mback,
 	Mforward,
 	Msearch,
+	Mbookmarks,
+	Maddbookmark,
 	Mexit,
 };
 
@@ -70,11 +72,31 @@
 	"back",
 	"forward",
 	"search",
+	"bookmarks",
+	"add bookmark",
 	"exit",
 	0
 };
 
+Url *
+current_url(void)
+{
+	return hist->c->url;
+}
 
+Url *
+current_base_url(void)
+{
+	return base_url(current_url());
+}
+
+char *
+current_host(void)
+{
+	Url *base = current_base_url();
+	return base->host;
+}
+
 char*
 cleanup(char *line)
 {
@@ -94,13 +116,6 @@
 }
 
 void
-set_current_base_url(Url *url)
-{
-	freeurl(current_base_url);
-	current_base_url = url;
-}
-
-void
 show(Ctx *c)
 {
 	plinittextview(textp, PACKE|EXPAND, ZP, c->text, texthit);
@@ -369,7 +384,6 @@
 		break;
 	case 20:
 		c->url = url;
-		set_current_base_url(base_url(url));
 
 		if(r->meta != NULL && strbeg(r->meta, "text/") != 0){
 			Bflush(&body);
@@ -524,7 +538,6 @@
 		return;
 	hist->p->n = hist;
 	hist = hist->p;
-	set_current_base_url(base_url(hist->c->url));
 	show(hist->c);
 }
 
@@ -537,7 +550,6 @@
 	if(hist==nil || hist->n==nil)
 		return;
 	hist = hist->n;
-	set_current_base_url(base_url(hist->c->url));
 	show(hist->c);
 }
 
@@ -556,6 +568,12 @@
 	case Msearch:
 		search();
 		break;
+	case Mbookmarks:
+		//showbookmarks();
+		break;
+	case Maddbookmark:
+		addbookmark();
+		break;
 	case Mexit:
 		exits(nil);
 		break;
@@ -563,6 +581,18 @@
 }
 
 void
+addbookmark(void)
+{
+	int fd;
+	if((fd = create("bookmarks", OWRITE, 0600 | DMAPPEND)) < 0)
+		sysfatal("create(bookmarks): %r");
+
+	fprint(fd, "=> %s\n", hist->c->url->raw);
+	close(fd);
+	message("Bookmark added!");
+}
+
+void
 entryhit(Panel *p, char *t)
 {
 	USED(p);
@@ -603,13 +633,13 @@
 		/* assuming relative URL */
 		if(strcmp(link, "/") == 0){
 			/* no slash, must be a hostname */
-			n = smprint("gemini://%s", current_base_url->host);
+			n = smprint("gemini://%s", current_host());
 		}else if(*link == '/'){
 			/* start with a slash so use the base host */
-			n = smprint("gemini://%s%s", current_base_url->host, estrdup(link));
+			n = smprint("gemini://%s%s", current_host(), estrdup(link));
 		}else{
 			/* make an absolute URL of the link */
-			n = smprint("%s%s", urlparse(current_base_url, link)->raw, estrdup(link));
+			n = smprint("%s%s", urlparse(current_base_url(), link)->raw, estrdup(link));
 		}
 		next_url = urlparse(nil, n);
 	}
--- a/util.c
+++ b/util.c
@@ -31,4 +31,15 @@
 strbeg(char *str, char *prefix)
 {
 	return strncmp(str, prefix, strlen(prefix)) == 0 ? 0 : -1;
-}
\ No newline at end of file
+}
+
+char* 
+replace_char(char* str, char find, char replace)
+{
+	char *current_pos = strchr(str,find);
+	while(current_pos){
+		*current_pos = replace;
+		current_pos = strchr(current_pos,find);
+	}
+	return str;
+}