2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include "minibuffer.h"
33 struct history history;
35 static struct event autosaveev;
38 switch_to_tab(struct tab *tab)
41 tab->flags &= ~TAB_URGENT;
43 if (operating && tab->flags & TAB_LAZY)
44 load_url_in_tab(tab, tab->hist_cur->h, NULL, LU_MODE_NOHIST);
50 static uint32_t tab_counter;
56 new_tab(const char *url, const char *base, struct tab *after)
63 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
68 TAILQ_INIT(&tab->hist.head);
69 TAILQ_INIT(&tab->buffer.head);
70 TAILQ_INIT(&tab->buffer.page.head);
71 evtimer_set(&tab->loadingev, NULL, NULL);
73 tab->id = tab_new_id();
76 TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
78 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
81 tab->flags |= TAB_LAZY;
82 load_url_in_tab(tab, url, base, 0);
88 * Move a tab from the tablist to the killed tab list and erase its
89 * contents. Append should always be 0 to prepend tabs so unkill_tab
90 * can work correctly; appending is only useful during startup when
91 * receiving the list of killed tabs to keep the correct order.
92 * NB: doesn't update the current_tab.
95 kill_tab(struct tab *tab, int append)
100 erase_buffer(&tab->buffer);
101 TAILQ_REMOVE(&tabshead, tab, tabs);
102 ui_schedule_redraw();
105 if (evtimer_pending(&tab->loadingev, NULL))
106 evtimer_del(&tab->loadingev);
109 TAILQ_INSERT_TAIL(&ktabshead, tab, tabs);
111 TAILQ_INSERT_HEAD(&ktabshead, tab, tabs);
115 TAILQ_FOREACH(tab, &ktabshead, tabs)
117 while (count > max_killed_tabs) {
119 free_tab(TAILQ_LAST(&ktabshead, tabshead));
124 * Resurrects the lastest killed tab and returns it. The tab is already
125 * added to the tab list with the TAB_LAZY flag set. NB: this doesn't
126 * update current_tab.
133 if (TAILQ_EMPTY(&ktabshead))
136 ui_schedule_redraw();
139 t = TAILQ_FIRST(&ktabshead);
140 TAILQ_REMOVE(&ktabshead, t, tabs);
141 TAILQ_INSERT_TAIL(&tabshead, t, tabs);
142 t->flags |= TAB_LAZY;
147 * Free every resource linked to the tab, including the tab itself, and
148 * removes it from the *killed* tablist.
151 free_tab(struct tab *tab)
153 TAILQ_REMOVE(&ktabshead, tab, tabs);
154 hist_clear(&tab->hist);
159 stop_tab(struct tab *tab)
161 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
165 savetab(FILE *fp, struct tab *tab, int killed)
168 size_t top_line, current_line;
171 get_scroll_position(tab, &top_line, ¤t_line);
173 fprintf(fp, "%s ", tab->hist_cur->h);
174 if (tab == current_tab)
175 fprintf(fp, "current,");
177 fprintf(fp, "killed,");
179 fprintf(fp, "top=%zu,cur=%zu %s\n", top_line, current_line,
180 tab->buffer.page.title);
183 TAILQ_FOREACH(h, &tab->hist.head, entries) {
184 if (h == tab->hist_cur) {
189 fprintf(fp, "%s %s\n", future ? ">" : "<", h->h);
196 FILE *session, *hist;
203 if ((session = fopen(session_file, "w")) == NULL)
206 TAILQ_FOREACH(tab, &tabshead, tabs)
207 savetab(session, tab, 0);
208 TAILQ_FOREACH(tab, &ktabshead, tabs)
209 savetab(session, tab, 1);
213 if ((hist = fopen(history_file, "a")) == NULL)
217 for (i = 0; i < history.len && history.dirty > 0; ++i) {
218 if (!history.items[i].dirty)
221 history.items[i].dirty = 0;
223 fprintf(hist, "%lld %s\n",
224 (long long)history.items[i].ts,
225 history.items[i].uri);
234 history_push(struct histitem *hi)
236 size_t i, oldest = 0;
239 for (i = 0; i < history.len; ++i) {
240 if (history.items[i].ts < history.items[oldest].ts)
243 /* remove duplicates */
244 if (!strcmp(history.items[i].uri, hi->uri))
248 if ((uri = strdup(hi->uri)) == NULL)
251 /* don't grow too much; replace the oldest */
252 if (history.len == HISTORY_CAP) {
253 history.items[oldest].ts = hi->ts;
254 free(history.items[oldest].uri);
255 history.items[oldest].uri = uri;
259 history.items[history.len].ts = hi->ts;
260 history.items[history.len].uri = uri;
265 history_cmp(const void *a, const void *b)
267 const struct history_item *i = a, *j = b;
268 return strcmp(i->uri, j->uri);
274 qsort(history.items, history.len, sizeof(history.items[0]),
279 history_add(const char *uri)
281 size_t i, j, insert = 0, oldest = 0;
285 for (i = 0; i < history.len; ++i) {
286 if (history.items[i].ts < history.items[oldest].ts)
289 if (insert != 0 && insert < i)
292 c = strcmp(uri, history.items[i].uri);
294 history.items[i].ts = time(NULL);
295 history.items[i].dirty = 1;
305 if ((u = strdup(uri)) == NULL)
308 /* if history is full, replace the oldest one */
309 if (history.len == HISTORY_CAP) {
310 free(history.items[oldest].uri);
311 history.items[oldest].uri = u;
312 history.items[oldest].ts = time(NULL);
313 history.items[oldest].dirty = 1;
320 /* otherwise just insert in the right spot */
322 for (j = history.len; j > insert; --j)
323 memcpy(&history.items[j], &history.items[j-1],
324 sizeof(history.items[j]));
326 history.items[insert].ts = time(NULL);
327 history.items[insert].uri = u;
328 history.items[insert].dirty = 1;
337 evtimer_set(&autosaveev, autosave_timer, NULL);
341 autosave_timer(int fd, short event, void *data)
347 * Function to be called in "interesting" places where we may want to
348 * schedule an autosave (like on new tab or before loading an url.)
358 if (!evtimer_pending(&autosaveev, NULL)) {
359 tv.tv_sec = autosave;
362 evtimer_add(&autosaveev, &tv);