commit - e49ce157fae23fdb5dac3e093f743b85af2d952f
commit + c7107cec96c1cb1a0bc0a0fcb5bc341e5464e57e
blob - a2bbcc59aef4fc9f0250f5c2f6635843250074fb
blob + ddba9d64c485f3f5ea1b1fcd4dc321eea2624c41
--- fs.c
+++ fs.c
static void handle_quit(struct imsg*, size_t);
static void handle_bookmark_page(struct imsg*, size_t);
static void handle_save_cert(struct imsg*, size_t);
+static void handle_session_start(struct imsg*, size_t);
+static void handle_session_tab(struct imsg*, size_t);
+static void handle_session_end(struct imsg*, size_t);
static void handle_dispatch_imsg(int, short, void*);
static struct event imsgev;
static struct imsgbuf *ibuf;
+static FILE *session;
+
static char bookmark_file[PATH_MAX];
static char known_hosts_file[PATH_MAX];
+static char session_file[PATH_MAX];
static imsg_handlerfn *handlers[] = {
[IMSG_GET] = handle_get,
[IMSG_QUIT] = handle_quit,
[IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
[IMSG_SAVE_CERT] = handle_save_cert,
+ [IMSG_SESSION_START] = handle_session_start,
+ [IMSG_SESSION_TAB] = handle_session_tab,
+ [IMSG_SESSION_END] = handle_session_end,
};
static void __attribute__((__noreturn__))
}
static void
+handle_session_start(struct imsg *imsg, size_t datalen)
+{
+ if (datalen != 0)
+ die();
+
+ if ((session = fopen(session_file, "w")) == NULL)
+ die();
+}
+
+static void
+handle_session_tab(struct imsg *imsg, size_t datalen)
+{
+ if (session == NULL)
+ die();
+
+ if (datalen == 0)
+ die();
+
+ /* skip the NUL-terminator */
+ fwrite(imsg->data, 1, datalen-1, session);
+
+ fprintf(session, "\n");
+}
+
+static void
+handle_session_end(struct imsg *imsg, size_t datalen)
+{
+ if (session == NULL)
+ die();
+ fclose(session);
+ session = NULL;
+}
+
+static void
handle_dispatch_imsg(int fd, short ev, void *d)
{
struct imsgbuf *ibuf = d;
strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
+ strlcpy(session_file, getenv("HOME"), sizeof(session_file));
+ strlcat(session_file, "/.telescope/session", sizeof(session_file));
+
return 1;
}
free(line);
return ferror(f);
}
+
+int
+load_last_session(void (*cb)(const char*))
+{
+ char *nl, *line = NULL;
+ int e;
+ size_t linesize = 0;
+ ssize_t linelen;
+ FILE *session;
+
+ if ((session = fopen(session_file, "r")) == NULL)
+ return 0;
+
+ while ((linelen = getline(&line, &linesize, session)) != -1) {
+ if ((nl = strchr(line, '\n')) != NULL)
+ *nl = '\0';
+ cb(line);
+ }
+
+ free(line);
+ e = ferror(session);
+ fclose(session);
+
+ return !e;
+}
blob - bdbb0df70436547074fb284a20ded7414f1f21bb
blob + 4df53b9b15288038037f8c6d5a78591df9712e09
--- telescope.c
+++ telescope.c
add_to_bookmarks(const char *str)
{
imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
+ imsg_flush(fsibuf);
+}
+
+void
+save_session(void)
+{
+ struct tab *tab;
+
+ imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
+ imsg_flush(fsibuf);
+
+ TAILQ_FOREACH(tab, &tabshead, tabs) {
+ imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
+ tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
+ imsg_flush(fsibuf);
+ }
+
+ imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
imsg_flush(fsibuf);
}
blob - 50a7cc2dee9be23766864d7cf244a65e06b7363f
blob + f655701a0218a729095cb36427b2c787a3dda7b6
--- telescope.h
+++ telescope.h
IMSG_BOOKMARK_OK,
IMSG_SAVE_CERT,
IMSG_SAVE_CERT_OK,
+
+ IMSG_SESSION_START,
+ IMSG_SESSION_TAB,
+ IMSG_SESSION_END,
};
enum line_type {
int fs_init(void);
int fs_main(struct imsgbuf*);
int load_certs(struct ohash*);
+int load_last_session(void(*)(const char*));
/* gemini.c */
int client_main(struct imsgbuf*);
int load_next_page(struct tab*);
void stop_tab(struct tab*);
void add_to_bookmarks(const char*);
+void save_session(void);
/* textplain.c */
void textplain_initparser(struct parser*);
blob - 05ffdd75e9e4b6cd6c5c3808ca18c7f721fcc6b4
blob + c0554c6ea45fbf875d2f309a526aec8be7868885
--- ui.c
+++ ui.c
static void exit_minibuffer(void);
static void switch_to_tab(struct tab*);
static struct tab *new_tab(const char*);
+static void session_new_tab_cb(const char*);
static void usage(void);
static struct { short meta; int key; uint32_t cp; } thiskey;
static void
cmd_kill_telescope(struct window *window)
{
+ save_session();
event_loopbreak();
}
load_url_in_tab(tab, url);
return tab;
+}
+
+static void
+session_new_tab_cb(const char *url)
+{
+ new_tab(url);
}
static void
signal_set(&winchev, SIGWINCH, handle_resize, NULL);
signal_add(&winchev, NULL);
- new_tab(url);
+ load_last_session(session_new_tab_cb);
+ if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
+ new_tab(url);
return 1;
}