From 69cadae851b48fc297cb5293c7899dd43dd499e5 Mon Sep 17 00:00:00 2001 From: Julien Blanchard Date: Thu, 31 Dec 2020 14:33:22 +0000 Subject: [PATCH] Bookmark a page, refact current_url stuff --- castor.c | 58 ++++++++++++++++++++++++++++++++++++++++++-------------- util.c | 13 ++++++++++++- 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/castor.c b/castor.c index 8a0c150..6dd26fc 100644 --- a/castor.c +++ b/castor.c @@ -43,6 +43,7 @@ int request(Url *u); 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 *urlp; Panel *textp; Panel *statusp; Panel *popup; -Url *current_base_url; Mouse *mouse; Hist *hist = nil; int preformatted = 0; @@ -63,6 +63,8 @@ enum Mback, Mforward, Msearch, + Mbookmarks, + Maddbookmark, Mexit, }; @@ -70,10 +72,30 @@ char *menu3[] = { "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) @@ -93,13 +115,6 @@ cleanup(char *line) return line; } -void -set_current_base_url(Url *url) -{ - freeurl(current_base_url); - current_base_url = url; -} - void show(Ctx *c) { @@ -369,7 +384,6 @@ gemini_get(Url *url) 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 @@ backhit(Panel *p, int b) return; hist->p->n = hist; hist = hist->p; - set_current_base_url(base_url(hist->c->url)); show(hist->c); } @@ -537,7 +550,6 @@ nexthit(Panel *p, int b) if(hist==nil || hist->n==nil) return; hist = hist->n; - set_current_base_url(base_url(hist->c->url)); show(hist->c); } @@ -556,12 +568,30 @@ menuhit(int button, int item) case Msearch: search(); break; + case Mbookmarks: + //showbookmarks(); + break; + case Maddbookmark: + addbookmark(); + break; case Mexit: exits(nil); break; } } +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) { @@ -603,13 +633,13 @@ texthit(Panel *p, int b, Rtext *rt) /* 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); } diff --git a/util.c b/util.c index dfdc646..476ed86 100644 --- a/util.c +++ b/util.c @@ -31,4 +31,15 @@ int 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; +} -- 2.45.2