~julienxx/castor9

69cadae851b48fc297cb5293c7899dd43dd499e5 — Julien Blanchard 3 years ago 6227d00
Bookmark a page, refact current_url stuff
2 files changed, 56 insertions(+), 15 deletions(-)

M castor.c
M util.c
M castor.c => castor.c +44 -14
@@ 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)


@@ 94,13 116,6 @@ cleanup(char *line)
}

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 @@ 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,6 568,12 @@ menuhit(int button, int item)
	case Msearch:
		search();
		break;
	case Mbookmarks:
		//showbookmarks();
		break;
	case Maddbookmark:
		addbookmark();
		break;
	case Mexit:
		exits(nil);
		break;


@@ 563,6 581,18 @@ menuhit(int button, int item)
}

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 @@ 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);
	}

M util.c => util.c +12 -1
@@ 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;
}