Commit Diff


commit - 58df4f477b392f0c092cb828339e2c53dfef3439
commit + 87e3e8018ef2858201ca3641eff06e130ea3ccd9
blob - fbf7657aea803786e1539de41bfaa02d56ba677c
blob + 8b1930f121dc679ff6bbf19e2e3cf77711b1ae65
--- ChangeLog
+++ ChangeLog
@@ -1,3 +1,7 @@
+2021-07-17  Omar Polo  <op@omarpolo.com>
+
+	* telescope.c (load_last_session): keep track of the current tab and re-focus it during next startup
+
 2021-07-16  Omar Polo  <op@omarpolo.com>
 
 	* ui.c (redraw_tabline): separate the tabs with a vertical bar
blob - dcb985784f24a6aab196eb8fcd9d8ee32d53e683
blob + e4f215ea98921e76d7926eb4a98183f0bcf936e4
--- fs.c
+++ fs.c
@@ -54,7 +54,8 @@ static FILE			*session;
 static char	lockfile_path[PATH_MAX];
 static char	bookmark_file[PATH_MAX];
 static char	known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
-static char	session_file[PATH_MAX];
+
+char	session_file[PATH_MAX];
 
 static imsg_handlerfn *handlers[] = {
 	[IMSG_GET] = handle_get,
@@ -286,15 +287,21 @@ handle_session_start(struct imsg *imsg, size_t datalen
 static void
 handle_session_tab(struct imsg *imsg, size_t datalen)
 {
-	char *url;
+	char		*url;
+	uint32_t	 flags;
 
 	if (session == NULL)
 		die();
 
+	flags = imsg->hdr.peerid;
 	url = imsg->data;
 	if (datalen == 0 || url[datalen-1] != '\0')
 		die();
 	fprintf(session, "%s", url);
+
+	if (flags & TAB_CURRENT)
+		fprintf(session, " current");
+
 	fprintf(session, "\n");
 }
 
@@ -452,31 +459,4 @@ load_certs(struct ohash *h)
 	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) {
-		/* first time? */
-		cb("about:help");
-		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 - 6be08a37cd8a4492839caeb49525d37245aa7745
blob + 2071a45e0dd3581a6b8a123d034abf4051e0a0fd
--- telescope.1
+++ telescope.1
@@ -760,8 +760,11 @@ Lock file used to prevent multiple instance of
 .Nm
 from running at the same time.
 .It Pa ~/.telescope/session
-Contains the list of opened tabs in the last session, one per line.
-Gets written on
+Contains the list of tabs from the last session.
+Every line identifies a tab.
+The syntax is the full URL, followed by a space, followed by an
+optional comma-separated list of attributes.
+Is written by
 .Ic kill-telescope
 and loaded on startup.
 .El
blob - 66c68f7af41101d93be01adda679992b77bec1af
blob + 45b3de8e43439b48ab6e38c7d4ae2962a91fdfce
--- telescope.c
+++ telescope.c
@@ -61,6 +61,8 @@ static void		 handle_imsg_update_cert_ok(struct imsg *
 static void		 handle_dispatch_imsg(int, short, void*);
 static void		 load_page_from_str(struct tab*, const char*);
 static int		 do_load_url(struct tab*, const char*);
+static void		 parse_session_line(char *, uint32_t *);
+static void		 load_last_session(void);
 static pid_t		 start_child(enum telescope_process, const char *, int);
 static int		 ui_send_net(int, uint32_t, const void *, uint16_t);
 static int		 ui_send_fs(int, uint32_t, const void *, uint16_t);
@@ -674,22 +676,83 @@ add_to_bookmarks(const char *str)
 void
 save_session(void)
 {
-	struct tab *tab;
+	struct tab	*tab;
+	int		 flags;
 
 	ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
 
 	TAILQ_FOREACH(tab, &tabshead, tabs) {
-		ui_send_fs(IMSG_SESSION_TAB, 0,
+		flags = tab->flags;
+		if (tab == current_tab)
+			flags |= TAB_CURRENT;
+		ui_send_fs(IMSG_SESSION_TAB, flags,
 		    tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
 	}
 
 	ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
 }
 
+/*
+ * Parse a line of the session file.  The format is:
+ *
+ *	URL [flags,...]\n
+ */
 static void
-session_new_tab_cb(const char *url)
+parse_session_line(char *line, uint32_t *flags)
 {
-	new_tab(url);
+	char *s, *ap;
+
+	*flags = 0;
+	if ((s = strchr(line, ' ')) == NULL)
+		return;
+
+	*s++ = '\0';
+	while ((ap = strsep(&s, ",")) != NULL) {
+		if (*ap == '\0')
+			;
+		else if (!strcmp(ap, "current"))
+			*flags |= TAB_CURRENT;
+		else
+			message("unknown tab flag: %s", ap);
+	}
+}
+
+static void
+load_last_session(void)
+{
+	char		*nl, *line = NULL;
+	uint32_t	 flags;
+	size_t		 linesize = 0;
+	ssize_t		 linelen;
+	FILE		*session;
+	struct tab	*tab, *curr;
+
+	if ((session = fopen(session_file, "r")) == NULL) {
+		/* first time? */
+		current_tab = new_tab("about:help");
+		return;
+	}
+
+	while ((linelen = getline(&line, &linesize, session)) != -1) {
+                if ((nl = strchr(line, '\n')) != NULL)
+			*nl = '\0';
+		parse_session_line(line, &flags);
+		if ((tab = new_tab(line)) == NULL)
+                        err(1, "new_tab");
+		if (flags & TAB_CURRENT)
+			curr = tab;
+	}
+
+	if (ferror(session))
+		message("error reading %s: %s",
+		    session_file, strerror(errno));
+	fclose(session);
+	free(line);
+
+	if (curr != NULL)
+		switch_to_tab(curr);
+
+	return;
 }
 
 static pid_t
@@ -883,11 +946,12 @@ main(int argc, char * const *argv)
 	event_add(&iev_net->ev, NULL);
 
 	if (ui_init()) {
-		load_last_session(session_new_tab_cb);
+		load_last_session();
 		if (has_url || TAILQ_EMPTY(&tabshead))
 			new_tab(url);
 
 		sandbox_ui_process();
+		ui_refresh();
 		event_dispatch();
 		ui_end();
 	}
blob - b4b1612f8f89dd857875b9ce9f5c3cbff6e58329
blob + db1e9f2418b4cad2132d76acede8ae8f7a818453
--- telescope.h
+++ telescope.h
@@ -22,6 +22,7 @@
 #include "phos/phos.h"
 
 #include <event.h>
+#include <limits.h>
 
 #define MIN(a, b) ((a) < (b) ? (a) : (b))
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
@@ -178,7 +179,8 @@ struct buffer {
 	TAILQ_HEAD(vhead, vline) head;
 };
 
-#define TAB_URGENT	0x1
+#define TAB_CURRENT	0x1	/* only for save_session */
+#define TAB_URGENT	0x2
 
 #define NEW_TAB_URL	"about:new"
 
@@ -279,11 +281,12 @@ int		 config_setattr(const char *, int, int, int);
 void		 config_apply_style(void);
 
 /* fs.c */
+extern char	session_file[PATH_MAX];
+
 int		 fs_init(void);
 int		 fs_main(void);
 int		 lock_session(void);
 int		 load_certs(struct ohash*);
-int		 load_last_session(void(*)(const char*));
 
 /* gemini.c */
 int		 client_main(void);
blob - c4ea0103e6abc4a94279880058fbe8be2bd691e7
blob + f8e75898e806d312863cb977af8340dc670751db
--- ui.c
+++ ui.c
@@ -1271,6 +1271,12 @@ ui_init()
 	signal_add(&winchev, NULL);
 
 	return 1;
+}
+
+void
+ui_refresh(void)
+{
+	redraw_tab(current_tab);
 }
 
 void
blob - fd2b1d1e28eb18527ee22fd67f4e3e457267215d
blob + 4842da52ee6c87e17611c8976f9de9f6ed722b6f
--- ui.h
+++ ui.h
@@ -113,6 +113,7 @@ struct tab	*new_tab(const char *);
 unsigned int	 tab_new_id(void);
 int		 ui_print_colors(void);
 int		 ui_init(void);
+void		 ui_refresh(void);
 void		 ui_on_tab_loaded(struct tab *);
 void		 ui_on_tab_refresh(struct tab *);
 const char	*ui_keyname(int);