summary refs log tree commit diff
diff options
context:
space:
mode:
authorwrmr2024-11-06 00:04:05 -0500
committerwrmr2024-11-06 00:04:05 -0500
commit5fe796490718f84d20ff9c836ce75d5318d44c69 (patch)
tree87abe322bee07ff6d4f4231bb7e79f81be63094f
parent837a9067c0ef2e0b3affbf7035788cd0e80ce7ba (diff)
add link number navigation
-rw-r--r--doc.c10
-rw-r--r--doc.h2
-rw-r--r--nav.c16
3 files changed, 28 insertions, 0 deletions
diff --git a/doc.c b/doc.c
index 89ff756..d41db7c 100644
--- a/doc.c
+++ b/doc.c
@@ -17,6 +17,7 @@ void doc_init(struct doc *d) {
 		.len = 0,
 	};
 	d->latest = 0;
+	d->linkc = 0;
 }
 
 void doc_fini(struct doc *d) {
@@ -89,3 +90,12 @@ int doc_line_next(struct doc *d, size_t *ofs) {
 		return -1;
 	}
 }
+
+const char *doc_get_link(struct doc *d, unsigned short lnk) {
+	size_t l = 1;
+	for (size_t i = 0; i < d->lnk.sz; i++) {
+		if (l == lnk) return &d->lnk.buf[i];
+		if (d->lnk.buf[i] == 0) l++;
+	}
+	return NULL;
+}
diff --git a/doc.h b/doc.h
index 08af700..ab66272 100644
--- a/doc.h
+++ b/doc.h
@@ -35,6 +35,8 @@ struct doc_line *doc_line_at(struct doc *d, size_t ofs);
 int doc_line_prev(struct doc *d, size_t *ofs);
 int doc_line_next(struct doc *d, size_t *ofs);
 
+const char *doc_get_link(struct doc *d, unsigned short lnk);
+
 void doc_set_link(struct doc *d, unsigned short lnk);
 unsigned short doc_add_link(struct doc *d, const char *url);
 
diff --git a/nav.c b/nav.c
index 83826dc..0c31bed 100644
--- a/nav.c
+++ b/nav.c
@@ -3,6 +3,7 @@
 
 #include "nav.h"
 #include "net.h"
+#include "err.h"
 #include "parse.h"
 
 /* history */
@@ -70,10 +71,17 @@ void nav_pg_up(struct nav_state *ns) {
 	nav_pg_down(ns);
 }
 
+#define MARGIN 8
+
 void nav_pg_down(struct nav_state *ns) {
 	size_t lines = pg_lines();
 	while (lines--) {
 		struct doc_line *l = nav_cur_line(ns);
+		int n = 0;
+		if (l->link != DOC_LINK_NONE) {
+			n = printf("[%hu]", l->link + 1);
+		}
+		while (n++ < MARGIN) putchar(' ');
 		fwrite(l->txt, 1, l->len, stdout);
 		putchar('\n');
 		if (!nav_line_down(ns)) break;
@@ -111,5 +119,13 @@ int nav_to(struct nav_state *ns, const char *url) {
 }
 
 int nav_link_nr(struct nav_state *ns, unsigned long link_nr) {
+	struct doc *d = &ns->histv[ns->cur_doc];
+	const char *url = doc_get_link(d, link_nr);
+	if (url) {
+		puts(url);
+		return nav_to(ns, url);
+	} else {
+		perr("invalid link number");
+	}
 	return 0;
 }