]> git.armaanb.net Git - chorizo.git/blobdiff - browser.c
draft 8
[chorizo.git] / browser.c
index 84a2a4a3a1dad2c0a8a78dadf086c3e2be90ee82..93df003240ee8855f62cad9dbc56c951ae324381 100644 (file)
--- a/browser.c
+++ b/browser.c
@@ -6,80 +6,61 @@
 #include <webkit2/webkit2.h>
 
 #include "config.h"
-#include "downloads.h"
-
-WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
-                                 gpointer);
-gboolean crashed_web_view(WebKitWebView *, gpointer);
-gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
-                      WebKitPolicyDecisionType, gpointer);
-gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
-gboolean key_tablabel(GtkWidget *, GdkEvent *, gpointer);
-gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
-gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
-gchar *ensure_uri_scheme(const gchar *);
-void changed_favicon(GObject *, GParamSpec *, gpointer);
-void changed_load_progress(GObject *, GParamSpec *, gpointer);
-void changed_title(GObject *, GParamSpec *, gpointer);
-void changed_uri(GObject *, GParamSpec *, gpointer);
-void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
-void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
-void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
-void mainwindow_title(gint);
-void notebook_switch_page(GtkNotebook *, GtkWidget *, guint, gpointer);
-void show_web_view(WebKitWebView *, gpointer);
-void trust_user_certs(WebKitWebContext *);
-
-struct Client {
-       GtkWidget *jsbutton;
-       GtkWidget *location;
-       GtkWidget *tabicon;
-       GtkWidget *tablabel;
-       GtkWidget *vbox;
-       GtkWidget *web_view;
-       WebKitSettings *settings;
-       gboolean focus_new_tab;
-       gchar *external_handler_uri;
-       gchar *feed_html;
-       gchar *hover_uri;
-};
-
-struct MainWindow {
-       GtkWidget *notebook;
-       GtkWidget *win;
-} mw;
-
-struct Configuration {
-       gboolean cooperative_alone;
-       gboolean noncooperative_instances;
-       gboolean private;
-       gboolean verbose;
-} cfg;
+#include "chorizo.h"
+
+struct MainWindow mw;
+struct Configuration cfg;
+gboolean switch_tab;
 
 gint clients = 0;
-struct Client **client_arr;
 
 int cooperative_pipe_fp = 0;
-gchar *search_text;
 gchar *fifopath;
 char **closed_tabs;
 size_t num_closed = 0;
 
-void
-allocfail(void)
+gboolean
+isearch_counted_matches(GtkWidget *widget, guint matches, gpointer data)
 {
-       fprintf(stderr, "chorizo: fatal: alloc failed\n");
-       exit(EXIT_FAILURE);
+       struct Client *c = (struct Client *)data;
+       char *text = malloc(12);
+       sprintf(text, "%d matches", matches);
+       gtk_label_set_text(GTK_LABEL(c->isearch_matches), text);
+       free(text);
+       return TRUE;
+}
+
+gboolean
+quit_if_nothing_active(void)
+{
+       if (clients == 0) {
+               if (downloads == 0) {
+                       gtk_main_quit();
+                       return TRUE;
+               } else {
+                       downloadmanager_show();
+               }
+       }
+       return FALSE;
+}
+gboolean
+remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
+{
+       gchar *uri = NULL;
+       g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
+       if (uri) {
+               g_strstrip(uri);
+               client_new(uri, NULL);
+               g_free(uri);
+       }
+       return TRUE;
 }
 
 void
-togglejs(GtkButton *jsbutton, gpointer data)
+allocfail(void)
 {
-       struct Client *c = (struct Client *)data;
-       webkit_settings_set_enable_javascript(
-               c->settings,
-               !webkit_settings_get_enable_javascript(c->settings));
-       webkit_web_view_set_settings(WEBKIT_WEB_VIEW(c->web_view), c->settings);
+       fprintf(stderr, "chorizo: fatal: alloc failed\n");
+       exit(EXIT_FAILURE);
 }
 
 void
@@ -101,22 +82,24 @@ client_destroy(GtkWidget *widget, gpointer data)
                        webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
 
                // TODO: Shift everything left if over certain amount
-               num_closed++;
-               if (num_closed > cfg_max_tabs_closed) {
-                       memmove(closed_tabs, closed_tabs,
-                               cfg_max_tabs_closed - 1);
-                       num_closed = cfg_max_tabs_closed;
-               } else {
-                       closed_tabs =
-                               realloc(closed_tabs,
+               if (uri != NULL) {
+                       num_closed++;
+                       if (num_closed > cfg_max_tabs_closed) {
+                               memmove(closed_tabs, closed_tabs,
+                                       cfg_max_tabs_closed - 1);
+                               num_closed = cfg_max_tabs_closed;
+                       } else {
+                               closed_tabs = realloc(
+                                       closed_tabs,
                                        num_closed * sizeof(closed_tabs[0]));
-                       if (!closed_tabs) allocfail();
+                               if (!closed_tabs) allocfail();
+                       }
+                       closed_tabs[num_closed - 1] = strdup(uri);
                }
-               closed_tabs[num_closed - 1] = strdup(uri);
        }
 
-       free(c);
        clients--;
+       free(c);
 
        quit_if_nothing_active();
 }
@@ -130,8 +113,7 @@ set_uri(const char *uri, struct Client *c)
 }
 
 WebKitWebView *
-client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
-          gboolean focus_tab)
+client_new(const gchar *uri, WebKitWebView *related_wv)
 {
        struct Client *c;
        gchar *f;
@@ -146,7 +128,6 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
        }
        c = calloc(1, sizeof(struct Client));
        if (!c) allocfail();
-       c->focus_new_tab = focus_tab;
 
        if (related_wv == NULL) {
                WebKitUserContentManager *ucm =
@@ -165,11 +146,13 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
                                if (g_str_has_suffix(path, ".js")) {
                                        g_file_get_contents(path, &source, NULL,
                                                            NULL);
+                                       // clang-format off
                                        wkscript = webkit_user_script_new(
                                                source,
-                                               WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
-                                               WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START,
+                                         WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
+                                   WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START,
                                                NULL, NULL);
+                                       // clang-format on
                                        webkit_user_content_manager_add_script(
                                                ucm, wkscript);
                                        webkit_user_script_unref(wkscript);
@@ -188,13 +171,15 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
                                if (g_str_has_suffix(path, ".css")) {
                                        g_file_get_contents(path, &source, NULL,
                                                            NULL);
+                                       // clang-format off
                                        wkstyle = webkit_user_style_sheet_new(
                                                source,
-                                               WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
-                                               WEBKIT_USER_STYLE_LEVEL_USER,
+                                         WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
+                                                  WEBKIT_USER_STYLE_LEVEL_USER,
                                                NULL, NULL);
-                                       webkit_user_content_manager_add_style_sheet(
+                                   webkit_user_content_manager_add_style_sheet(
                                                ucm, wkstyle);
+                                       // clang-format on
                                        webkit_user_style_sheet_unref(wkstyle);
                                }
                                g_free(path);
@@ -206,18 +191,20 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
 
                c->web_view =
                        webkit_web_view_new_with_user_content_manager(ucm);
+
+               c->settings = webkit_web_view_get_settings(
+                       WEBKIT_WEB_VIEW(c->web_view));
+               if (cfg.verbose) {
+                       // clang-format off
+               webkit_settings_set_enable_write_console_messages_to_stdout(
+                               c->settings, true);
+                       // clang-format on
+               }
+               webkit_settings_set_enable_developer_extras(c->settings, TRUE);
        } else {
                c->web_view = webkit_web_view_new_with_related_view(related_wv);
        }
 
-       c->settings =
-               webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view));
-       webkit_settings_set_enable_javascript(c->settings, cfg_js_default);
-       if (cfg.verbose)
-               webkit_settings_set_enable_write_console_messages_to_stdout(
-                       c->settings, true);
-       webkit_settings_set_enable_developer_extras(c->settings, TRUE);
-
        g_signal_connect(G_OBJECT(c->web_view), "notify::favicon",
                         G_CALLBACK(changed_favicon), c);
        g_signal_connect(G_OBJECT(c->web_view), "notify::title",
@@ -228,15 +215,13 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
                         "notify::estimated-load-progress",
                         G_CALLBACK(changed_load_progress), c);
        g_signal_connect(G_OBJECT(c->web_view), "create",
-                        G_CALLBACK(client_new_request), NULL);
+                        G_CALLBACK(client_new_request), c);
        g_signal_connect(G_OBJECT(c->web_view), "close",
                         G_CALLBACK(client_destroy), c);
        g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
                         G_CALLBACK(decide_policy), NULL);
        g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
                         G_CALLBACK(key_web_view), c);
-       g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
-                        G_CALLBACK(key_web_view), c);
        g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
                         G_CALLBACK(key_web_view), c);
        g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
@@ -245,45 +230,68 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
                         G_CALLBACK(crashed_web_view), c);
 
        GtkWidget *locbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
-       c->jsbutton = gtk_toggle_button_new_with_label("JS");
-       gtk_widget_set_tooltip_text(c->jsbutton, "Toggle JavaScript execution");
-       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(c->jsbutton),
-                                    cfg_js_default);
-       g_signal_connect(G_OBJECT(c->jsbutton), "toggled", G_CALLBACK(togglejs),
-                        c);
 
        c->location = gtk_entry_new();
-       gtk_box_pack_start(GTK_BOX(locbox), c->location, TRUE, TRUE, 0);
-
+       gtk_entry_set_placeholder_text(GTK_ENTRY(c->location), "URL");
+       char *location_symbol;
        if (cfg.private) {
-               GtkWidget *privindicator = gtk_label_new("Private mode");
-               gtk_widget_set_tooltip_text(
-                       privindicator,
+               location_symbol = "security-high-symbolic";
+               gtk_entry_set_icon_tooltip_markup(
+                       GTK_ENTRY(c->location), GTK_ENTRY_ICON_PRIMARY,
                        "You are in private mode. No history, caches, or "
                        "cookies will be saved beyond this session.");
-               gtk_box_pack_end(GTK_BOX(locbox), privindicator, FALSE, FALSE,
-                                5);
+       } else {
+               location_symbol = "text-x-generic-symbolic";
        }
-       gtk_box_pack_start(GTK_BOX(locbox), c->jsbutton, FALSE, FALSE, 0);
+
+       gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
+                                         GTK_ENTRY_ICON_PRIMARY,
+                                         location_symbol);
+       gtk_box_pack_start(GTK_BOX(locbox), c->location, TRUE, TRUE, 0);
+
+       c->wsearch = gtk_entry_new();
+       gtk_entry_set_placeholder_text(GTK_ENTRY(c->wsearch), "Search the web");
+       gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->wsearch),
+                                         GTK_ENTRY_ICON_PRIMARY,
+                                         "system-search-symbolic");
+       gtk_box_pack_start(GTK_BOX(locbox), c->wsearch, FALSE, TRUE, 0);
 
        g_signal_connect(G_OBJECT(c->location), "key-press-event",
                         G_CALLBACK(key_location), c);
        g_signal_connect(G_OBJECT(c->location), "icon-release",
                         G_CALLBACK(icon_location), c);
+       g_signal_connect(G_OBJECT(c->wsearch), "key-press-event",
+                        G_CALLBACK(key_wsearch), c);
        /*
-       * XXX This is a workaround. Setting this to NULL (which is done in
-       * grab_feeds_finished() if no feed has been detected) adds a little
-       * padding left of the text. Not sure why. The point of this call
-       * right here is to have that padding right from the start. This
-       * avoids a graphical artifact.
-       */
+        * XXX This is a workaround. Setting this to NULL (which is done in
+        * grab_feeds_finished() if no feed has been detected) adds a little
+        * padding left of the text. Not sure why. The point of this call
+        * right here is to have that padding right from the start. This
+        * avoids a graphical artifact.
+        */
 
        gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
                                          GTK_ENTRY_ICON_SECONDARY, NULL);
 
+       c->isearch_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
+       c->isearch = gtk_entry_new();
+       gtk_entry_set_placeholder_text(GTK_ENTRY(c->isearch), "Search page");
+       gtk_box_pack_start(GTK_BOX(c->isearch_box), c->isearch, FALSE, TRUE, 0);
+       gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->isearch),
+                                         GTK_ENTRY_ICON_PRIMARY,
+                                         "system-search-symbolic");
+       g_signal_connect(G_OBJECT(c->isearch), "key-press-event",
+                        G_CALLBACK(key_isearch), c);
+
+       c->isearch_matches = gtk_label_new("0 matches");
+       gtk_box_pack_start(GTK_BOX(c->isearch_box), c->isearch_matches, FALSE,
+                          TRUE, 5);
+
        c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
-       gtk_box_pack_start(GTK_BOX(c->vbox), locbox, FALSE, FALSE, 0);
        gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
+       gtk_box_pack_start(GTK_BOX(c->vbox), c->isearch_box, FALSE, TRUE, 0);
+       gtk_box_pack_start(GTK_BOX(c->vbox), locbox, FALSE, FALSE, 0);
+
        gtk_container_set_focus_child(GTK_CONTAINER(c->vbox), c->web_view);
 
        c->tabicon = gtk_image_new_from_icon_name("text-html",
@@ -291,13 +299,13 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
 
        c->tablabel = gtk_label_new("chorizo");
        gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
-       gtk_label_set_width_chars(GTK_LABEL(c->tablabel), 20);
+       gtk_label_set_width_chars(GTK_LABEL(c->tablabel), cfg_tab_width);
        gtk_widget_set_has_tooltip(c->tablabel, TRUE);
 
        /*
-       * XXX I don't own a HiDPI screen, so I don't know if scale_factor
-       * does the right thing.
-       */
+        * XXX I don't own a HiDPI screen, so I don't know if scale_factor
+        * does the right thing.
+        */
        tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
                             5 * gtk_widget_get_scale_factor(mw.win));
        gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0);
@@ -312,14 +320,14 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
        g_signal_connect(G_OBJECT(evbox), "scroll-event",
                         G_CALLBACK(key_tablabel), c);
 
-       //For easy access, store a reference to our label.
+       // For easy access, store a reference to our label.
        g_object_set_data(G_OBJECT(evbox), "chorizo-tab-label", c->tablabel);
 
        /*
-       * This only shows the event box and the label inside, nothing else.
-       * Needed because the evbox/label is "internal" to the notebook and
-       * not part of the normal "widget tree" (IIUC).
-       */
+        * This only shows the event box and the label inside, nothing else.
+        * Needed because the evbox/label is "internal" to the notebook and
+        * not part of the normal "widget tree" (IIUC).
+        */
        gtk_widget_show_all(evbox);
 
        int page = gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)) + 1;
@@ -328,11 +336,7 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
        gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox,
                                         TRUE);
 
-       if (show)
-               show_web_view(NULL, c);
-       else
-               g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
-                                G_CALLBACK(show_web_view), c);
+       show_web_view(NULL, c);
 
        if (uri != NULL) {
                f = ensure_uri_scheme(uri);
@@ -342,12 +346,8 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
        set_uri(uri, c);
 
        clients++;
-       client_arr = realloc(client_arr, (clients + 1) * sizeof(client_arr[0]));
-       if (!client_arr) allocfail();
 
-       client_arr[clients] = c;
-
-       if (clients == 1 || uri == NULL) gtk_widget_grab_focus(c->location);
+       if (uri == NULL) gtk_widget_grab_focus(c->location);
 
        return WEBKIT_WEB_VIEW(c->web_view);
 }
@@ -356,7 +356,10 @@ WebKitWebView *
 client_new_request(WebKitWebView *web_view,
                   WebKitNavigationAction *navigation_action, gpointer data)
 {
-       return client_new(NULL, web_view, FALSE, FALSE);
+       switch_tab = FALSE;
+       WebKitWebView *new_web_view = client_new(NULL, web_view);
+       gtk_widget_grab_focus(GTK_WIDGET(new_web_view));
+       return new_web_view;
 }
 
 void
@@ -401,9 +404,9 @@ cooperation_setup(void)
        } else {
                if (write(cooperative_pipe_fp, "", 0) == -1) {
                        /*
-                       * Could not do an empty write to the FIFO which
-                       * means there's no one listening.
-                       */
+                        * Could not do an empty write to the FIFO which
+                        * means there's no one listening.
+                        */
                        close(cooperative_pipe_fp);
                        towatch = g_io_channel_new_file(fifopath, "r+", NULL);
                        g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg,
@@ -447,11 +450,11 @@ changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
                p = 0;
 
                /*
-               * The page has loaded fully. We now run the short JavaScript
-               * snippet above that operates on the DOM. It tries to grab
-               * all occurences of <link rel="alternate" ...>, i.e.
-               * RSS/Atom feed references.
-               */
+                * The page has loaded fully. We now run the short JavaScript
+                * snippet above that operates on the DOM. It tries to grab
+                * all occurences of <link rel="alternate" ...>, i.e.
+                * RSS/Atom feed references.
+                */
                webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
                                               grab_feeds, NULL,
                                               grab_feeds_finished, c);
@@ -496,18 +499,13 @@ changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
        u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
        t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
 
-       u = u == NULL ? "chorizo" : u;
-       u = u[0] == 0 ? "chorizo" : u;
-
-       t = t == NULL ? u : t;
-       t = t[0] == 0 ? u : t;
+       if (t == NULL) t = (u == NULL) ? "chorizo" : u;
 
        gchar *name = malloc(strlen(t) + 4);
        if (!name) allocfail();
-
        gboolean mute =
                webkit_web_view_get_is_muted(WEBKIT_WEB_VIEW(c->web_view));
-       gchar *muted = (mute) ? "[m] " : "";
+       gchar *muted = (mute) ? "[M] " : "";
        sprintf(name, "%s%s", muted, t);
        gtk_label_set_text(GTK_LABEL(c->tablabel), name);
        g_free(name);
@@ -528,13 +526,15 @@ changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
        if (t != NULL && strlen(t) > 0) {
                set_uri(t, c);
 
-               //No g_get_user_state_dir unfortunately
+               // No g_get_user_state_dir unfortunately
                gchar *state_env = getenv("XDG_STATE_DIR");
-               gchar *state_dir = (state_env) ?
-                                                state_env :
-                                                g_build_filename(g_get_home_dir(),
+               // clang-format off
+               gchar *state_dir = (state_env)
+                                       ? state_env
+                                       : g_build_filename(g_get_home_dir(),
                                                            ".local", "state",
                                                            "chorizo", NULL);
+               // clang-format on
 
                gchar *history_file =
                        g_build_filename(state_dir, "history", NULL);
@@ -545,7 +545,8 @@ changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
                                fprintf(fp, "%s\n", t);
                                fclose(fp);
                        } else {
-                               perror("chorizo: error: could not open history file");
+                               perror("chorizo: error: could not open history"
+                                      "file");
                        }
                }
                g_free(history_file);
@@ -556,11 +557,10 @@ changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
 gboolean
 crashed_web_view(WebKitWebView *web_view, gpointer data)
 {
-       struct Client *c = (struct Client *)data;
        GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT;
        GtkWidget *dialog = gtk_message_dialog_new(
-               GTK_WINDOW(c->vbox), flags, GTK_MESSAGE_ERROR,
-               GTK_BUTTONS_CLOSE, "ERROR: Web process %s crashed.",
+               GTK_WINDOW(mw.win), flags, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
+               "ERROR: Web process %s crashed.\n%s",
                webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)),
                g_strerror(errno));
        gtk_dialog_run(GTK_DIALOG(dialog));
@@ -568,6 +568,7 @@ crashed_web_view(WebKitWebView *web_view, gpointer data)
 
        return TRUE;
 }
+
 gboolean
 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
              WebKitPolicyDecisionType type, gpointer data)
@@ -582,7 +583,7 @@ decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
                        webkit_policy_decision_use(decision);
                break;
        default:
-               //Use whatever default there is.
+               // Use whatever default there is.
                return FALSE;
        }
        return TRUE;
@@ -622,11 +623,11 @@ grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
        c->feed_html = NULL;
 
        /*
-       * This was taken almost verbatim from the example in WebKit's
-       * documentation:
-       *
-       * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html
-       */
+        * This was taken almost verbatim from the example in WebKit's
+        * documentation:
+        *
+        * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html
+        */
 
        js_result = webkit_web_view_run_javascript_finish(
                WEBKIT_WEB_VIEW(object), result, &err);
@@ -644,7 +645,8 @@ grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
                        jsc_context_get_exception(jsc_value_get_context(value));
                if (exception != NULL) {
                        fprintf(stderr,
-                               "chorizo: warning: error running javascript: %s\n",
+                               "chorizo: warning: error running javascript:"
+                               "%s\n",
                                jsc_exception_get_message(exception));
                } else {
                        c->feed_html = str_value;
@@ -704,20 +706,20 @@ icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
                               "</html>";
        if (c->feed_html != NULL) {
                /*
-               * What we're actually trying to do is show a simple HTML
-               * page that lists all the feeds on the current page. The
-               * function webkit_web_view_load_html() looks like the proper
-               * way to do that. Sad thing is, it doesn't create a history
-               * entry, but instead simply replaces the content of the
-               * current page. This is not what we want.
-               *
-               * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
-               * mechanism to show my custom HTML snippet* and*create a
-               * history entry.
-               *
-               * [0]: https://tools.ietf.org/html/rfc2397 [1]:
-               * https://en.wikipedia.org/wiki/Data_URI_scheme
-               */
+                * What we're actually trying to do is show a simple HTML
+                * page that lists all the feeds on the current page. The
+                * function webkit_web_view_load_html() looks like the proper
+                * way to do that. Sad thing is, it doesn't create a history
+                * entry, but instead simply replaces the content of the
+                * current page. This is not what we want.
+                *
+                * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
+                * mechanism to show my custom HTML snippet* and*create a
+                * history entry.
+                *
+                * [0]: https://tools.ietf.org/html/rfc2397 [1]:
+                * https://en.wikipedia.org/wiki/Data_URI_scheme
+                */
 
                d = g_strdup_printf(data_template, c->feed_html);
                webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
@@ -740,9 +742,8 @@ init_default_web_context(void)
        webkit_web_context_add_path_to_sandbox(wc, p, TRUE);
        g_free(p);
 
-       WebKitProcessModel model =
-               WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES;
-       webkit_web_context_set_process_model(wc, model);
+       webkit_web_context_set_process_model(
+               wc, WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
 
        p = g_build_filename(g_get_user_data_dir(), "chorizo", "web-extensions",
                             NULL);
@@ -774,21 +775,23 @@ init_default_web_context(void)
 }
 
 void
-search(gpointer data, gint direction)
+isearch(gpointer data, gint direction)
 {
        struct Client *c = (struct Client *)data;
        WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
        WebKitFindController *fc =
                webkit_web_view_get_find_controller(web_view);
-       if (search_text == NULL) return;
+       const gchar *isearch_text = gtk_entry_get_text(GTK_ENTRY(c->isearch));
+       if (isearch_text == NULL) return;
 
        switch (direction) {
        case 0:
-               webkit_find_controller_search(
-                       fc, search_text,
-                       WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
-                               WEBKIT_FIND_OPTIONS_WRAP_AROUND,
-                       G_MAXUINT);
+               g_signal_connect(G_OBJECT(fc), "counted-matches",
+                                G_CALLBACK(isearch_counted_matches), c);
+               webkit_find_controller_count_matches(
+                       fc, isearch_text, cfg_isearch_options, G_MAXUINT);
+               webkit_find_controller_search(fc, isearch_text,
+                                             cfg_isearch_options, G_MAXUINT);
                break;
        case 1:
                webkit_find_controller_search_next(fc);
@@ -803,330 +806,29 @@ search(gpointer data, gint direction)
 }
 
 void
-search_init(struct Client *c, int direction)
+isearch_init(struct Client *c, int direction)
 {
-       gtk_widget_grab_focus(c->location);
-       const gchar *contents = gtk_entry_get_text(GTK_ENTRY(c->location));
-       if (strcspn(contents, "s/")) {
-               gtk_entry_set_text(GTK_ENTRY(c->location), "s/");
-               gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
-       } else {
-               search(c, 0);
-               search(c, -1);
-               search(c, direction);
+       if (webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view))) {
+               gtk_widget_show(c->isearch_box);
+               gtk_widget_grab_focus(c->isearch);
        }
 }
-gboolean
-key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
-{
-       struct Client *c = (struct Client *)data;
-       gdouble now;
-       if (event->type == GDK_KEY_PRESS) {
-               if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK) {
-                       const char *uri = webkit_web_view_get_uri(
-                               WEBKIT_WEB_VIEW(c->web_view));
-                       int key = ((GdkEventKey *)event)->keyval;
-                       if (GDK_KEY_y == key) {
-                               downloadmanager_show();
-                               return TRUE;
-                       } else if (GDK_KEY_h == key) {
-                               webkit_web_view_go_back(
-                                       WEBKIT_WEB_VIEW(c->web_view));
-                               return TRUE;
-                       } else if (GDK_KEY_l == key) {
-                               webkit_web_view_go_forward(
-                                       WEBKIT_WEB_VIEW(c->web_view));
-                               return TRUE;
-                       } else if (GDK_KEY_o == key) {
-                               gtk_widget_grab_focus(c->location);
-                               return TRUE;
-                       } else if (GDK_KEY_Print == key) {
-                               WebKitPrintOperation *operation =
-                                       webkit_print_operation_new(
-                                               WEBKIT_WEB_VIEW(c->web_view));
-                               GtkWidget *toplevel =
-                                       gtk_widget_get_toplevel(mw.win);
-                               webkit_print_operation_run_dialog(
-                                       operation, GTK_WINDOW(toplevel));
-                               return TRUE;
-                       } else if (GDK_KEY_g == key) {
-                               search(c, 2);
-                               gtk_widget_grab_focus(c->web_view);
-                               gtk_editable_set_position(
-                                       GTK_EDITABLE(c->location), -1);
-                               if (uri)
-                                       gtk_entry_set_text(
-                                               GTK_ENTRY(c->location), uri);
-                               webkit_web_view_run_javascript(
-                                       WEBKIT_WEB_VIEW(c->web_view),
-                                       "window.getSelection().removeAllRanges();"
-                                       "document.activeElement.blur();",
-                                       NULL, NULL, c);
-                               return TRUE;
-                       } else if (GDK_KEY_semicolon == key) {
-                               webkit_web_view_reload_bypass_cache(
-                                       WEBKIT_WEB_VIEW(c->web_view));
-                               return TRUE;
-                       } else if (GDK_KEY_j == key) {
-                               for (int i = 0; i <= cfg_scroll_lines - 1;
-                                    i++) {
-                                       event->key.keyval = GDK_KEY_Down;
-                                       gdk_event_put(event);
-                               }
-                               return TRUE;
-                       } else if (GDK_KEY_k == key) {
-                               for (int i = 0; i <= cfg_scroll_lines - 1;
-                                    i++) {
-                                       event->key.keyval = GDK_KEY_Up;
-                                       gdk_event_put(event);
-                               }
-                               return TRUE;
-                       } else if (GDK_KEY_f == key) {
-                               event->key.keyval = GDK_KEY_Page_Down;
-                               gdk_event_put(event);
-                               return TRUE;
-                       } else if (GDK_KEY_b == key) {
-                               event->key.keyval = GDK_KEY_Page_Up;
-                               gdk_event_put(event);
-                               return TRUE;
-                       } else if (GDK_KEY_s == key) {
-                               search_init(c, 1);
-                               return TRUE;
-                       } else if (GDK_KEY_r == key) {
-                               search_init(c, -1);
-                               return TRUE;
-                       } else if (GDK_KEY_q == key) {
-                               client_destroy(NULL, c);
-                               return TRUE;
-                       } else if (GDK_KEY_1 == key) {
-                               gtk_notebook_set_current_page(
-                                       GTK_NOTEBOOK(mw.notebook), 0);
-                               return TRUE;
-                       } else if (GDK_KEY_2 == key) {
-                               gtk_notebook_set_current_page(
-                                       GTK_NOTEBOOK(mw.notebook), 1);
-                               return TRUE;
-                       } else if (GDK_KEY_3 == key) {
-                               gtk_notebook_set_current_page(
-                                       GTK_NOTEBOOK(mw.notebook), 2);
-                               return TRUE;
-                       } else if (GDK_KEY_4 == key) {
-                               gtk_notebook_set_current_page(
-                                       GTK_NOTEBOOK(mw.notebook), 3);
-                               return TRUE;
-                       } else if (GDK_KEY_5 == key) {
-                               gtk_notebook_set_current_page(
-                                       GTK_NOTEBOOK(mw.notebook), 4);
-                               return TRUE;
-                       } else if (GDK_KEY_6 == key) {
-                               gtk_notebook_set_current_page(
-                                       GTK_NOTEBOOK(mw.notebook), 5);
-                               return TRUE;
-                       } else if (GDK_KEY_7 == key) {
-                               gtk_notebook_set_current_page(
-                                       GTK_NOTEBOOK(mw.notebook), 6);
-                               return TRUE;
-                       } else if (GDK_KEY_8 == key) {
-                               gtk_notebook_set_current_page(
-                                       GTK_NOTEBOOK(mw.notebook), 7);
-                               return TRUE;
-                       } else if (GDK_KEY_9 == key) {
-                               gtk_notebook_set_current_page(
-                                       GTK_NOTEBOOK(mw.notebook), 8);
-                               return TRUE;
-                       } else if (GDK_KEY_u == key) {
-                               gtk_notebook_prev_page(
-                                       GTK_NOTEBOOK(mw.notebook));
-                               return TRUE;
-                       } else if (GDK_KEY_m == key) {
-                               gboolean muted = webkit_web_view_get_is_muted(
-                                       WEBKIT_WEB_VIEW(c->web_view));
-                               webkit_web_view_set_is_muted(
-                                       WEBKIT_WEB_VIEW(c->web_view), !muted);
-                               changed_title(G_OBJECT(c->web_view), NULL, c);
-                               return TRUE;
-                       } else if (GDK_KEY_t == key) {
-                               client_new(cfg_home_uri, NULL, TRUE, TRUE);
-                               return TRUE;
-                       } else if (GDK_KEY_bracketleft == key) {
-                               if (num_closed == 0) return TRUE;
-                               client_new(closed_tabs[num_closed - 1], NULL,
-                                          TRUE, TRUE);
-                               num_closed--;
-                               closed_tabs = realloc(
-                                       closed_tabs,
-                                       num_closed * sizeof(closed_tabs[0]));
-                               if (!closed_tabs) allocfail();
-                               return TRUE;
-                       } else if (GDK_KEY_i == key) {
-                               gtk_notebook_next_page(
-                                       GTK_NOTEBOOK(mw.notebook));
-                               return TRUE;
-                       } else if (GDK_KEY_p == key) {
-                               gboolean on =
-                                       webkit_settings_get_enable_javascript(
-                                               c->settings);
-                               webkit_settings_set_enable_javascript(
-                                       c->settings, !on);
-                               webkit_web_view_set_settings(
-                                       WEBKIT_WEB_VIEW(c->web_view),
-                                       c->settings);
-                               gtk_toggle_button_set_active(
-                                       GTK_TOGGLE_BUTTON(c->jsbutton), !on);
-                       } else if (GDK_KEY_d == key) {
-                               gtk_widget_grab_focus(c->location);
-                               gtk_entry_set_text(GTK_ENTRY(c->location),
-                                                  "w/");
-                               gtk_editable_set_position(
-                                       GTK_EDITABLE(c->location), -1);
-                               return TRUE;
-                       } else if (GDK_KEY_equal == key) {
-                               now = webkit_web_view_get_zoom_level(
-                                       WEBKIT_WEB_VIEW(c->web_view));
-                               webkit_web_view_set_zoom_level(
-                                       WEBKIT_WEB_VIEW(c->web_view),
-                                       now + 0.1);
-                               return TRUE;
-                       } else if (GDK_KEY_minus == key) {
-                               now = webkit_web_view_get_zoom_level(
-                                       WEBKIT_WEB_VIEW(c->web_view));
-                               webkit_web_view_set_zoom_level(
-                                       WEBKIT_WEB_VIEW(c->web_view),
-                                       now - 0.1);
-                               return TRUE;
-                       } else if (GDK_KEY_0 == key) {
-                               webkit_web_view_set_zoom_level(
-                                       WEBKIT_WEB_VIEW(c->web_view), 1);
-                               return TRUE;
-                       }
-               }
-       }
-       return FALSE;
-}
-gboolean
-key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
-{
-       struct Client *c = (struct Client *)data;
-       const gchar *t;
-       if (key_common(widget, event, data)) return TRUE;
-
-       if (event->type == GDK_KEY_PRESS) {
-               int key = ((GdkEventKey *)event)->keyval;
-               if ((GDK_KEY_KP_Enter == key) || (GDK_KEY_Return == key)) {
-                       gtk_widget_grab_focus(c->web_view);
-                       t = gtk_entry_get_text(GTK_ENTRY(c->location));
-                       if (t != NULL && t[0] == 's' && t[1] == '/') {
-                               if (search_text != NULL) g_free(search_text);
-                               search_text = g_strdup(t + 2);
-                               search(c, 0);
-                       } else if (t != NULL && t[0] == 'w' && t[1] == '/') {
-                               int len = strlen(cfg_search_engine) +
-                                         strlen(t) - 2;
-                               gchar *f = malloc(len + 1);
-                               if (!f) allocfail();
-                               snprintf(f, len + 1, "%s%s", cfg_search_engine,
-                                        t + 2);
-                               webkit_web_view_load_uri(
-                                       WEBKIT_WEB_VIEW(c->web_view), f);
-                               g_free(f);
-                       } else {
-                               webkit_web_view_load_uri(
-                                       WEBKIT_WEB_VIEW(c->web_view),
-                                       ensure_uri_scheme(t));
-                       }
-                       return TRUE;
-               } else if (GDK_KEY_Escape == key) {
-                       t = webkit_web_view_get_uri(
-                               WEBKIT_WEB_VIEW(c->web_view));
-                       gtk_entry_set_text(GTK_ENTRY(c->location),
-                                          (t == NULL) ? "" : t);
-                       return TRUE;
-               }
-       }
-       return FALSE;
-}
-gboolean
-key_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data)
-{
-       GdkScrollDirection direction;
-       if (event->type == GDK_BUTTON_RELEASE) {
-               switch (((GdkEventButton *)event)->button) {
-               case 2:
-                       client_destroy(NULL, data);
-                       return TRUE;
-               }
-       } else if (event->type == GDK_SCROLL) {
-               gdk_event_get_scroll_direction(event, &direction);
-               switch (direction) {
-               case GDK_SCROLL_UP:
-                       gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
-                       break;
-               case GDK_SCROLL_DOWN:
-                       gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
-                       break;
-               default:
-                       break;
-               }
-               return TRUE;
-       }
-       return FALSE;
-}
-gboolean
-key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
+
+void
+reopen_tab(void)
 {
-       struct Client *c = (struct Client *)data;
-       gdouble dx, dy;
-       gfloat z;
-       if (key_common(widget, event, data)) return TRUE;
-
-       if (event->type == GDK_KEY_PRESS) {
-               if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape) {
-                       webkit_web_view_stop_loading(
-                               WEBKIT_WEB_VIEW(c->web_view));
-                       gtk_entry_set_progress_fraction(GTK_ENTRY(c->location),
-                                                       0);
-               }
-       } else if (event->type == GDK_BUTTON_RELEASE) {
-               GdkModifierType modifiers =
-                       gtk_accelerator_get_default_mod_mask();
-               switch (((GdkEventButton *)event)->button) {
-               case 1:
-                       if ((((GdkEventButton *)event)->state & modifiers) ==
-                                   GDK_CONTROL_MASK &&
-                           c->hover_uri != NULL) {
-                               client_new(c->hover_uri, NULL, TRUE, FALSE);
-                               return TRUE;
-                       }
-                       break;
-               case 8:
-                       webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
-                       return TRUE;
-               case 9:
-                       webkit_web_view_go_forward(
-                               WEBKIT_WEB_VIEW(c->web_view));
-                       return TRUE;
-               }
-       } else if (event->type == GDK_SCROLL) {
-               event->scroll.delta_y *= cfg_scroll_lines;
-               if (((GdkEventScroll *)event)->state & GDK_CONTROL_MASK) {
-                       gdk_event_get_scroll_deltas(event, &dx, &dy);
-                       z = webkit_web_view_get_zoom_level(
-                               WEBKIT_WEB_VIEW(c->web_view));
-                       z += -dy * 0.1;
-                       z = dx != 0 ? 1 : z;
-                       webkit_web_view_set_zoom_level(
-                               WEBKIT_WEB_VIEW(c->web_view), z);
-                       return TRUE;
-               }
-       }
-       return FALSE;
+       if (num_closed == 0) return;
+       client_new(closed_tabs[num_closed - 1], NULL);
+       num_closed--;
+       closed_tabs = realloc(closed_tabs, num_closed * sizeof(closed_tabs[0]));
+       if (!closed_tabs) allocfail();
 }
 
 void
 mainwindow_setup(void)
 {
        mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
-       gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
+       gtk_window_set_default_size(GTK_WINDOW(mw.win), 1200, 900);
        g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
 
        gchar *priv = (cfg.private) ? "-private" : "";
@@ -1138,9 +840,23 @@ mainwindow_setup(void)
 
        mw.notebook = gtk_notebook_new();
        gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
+       gtk_notebook_set_tab_pos(GTK_NOTEBOOK(mw.notebook), GTK_POS_LEFT);
        gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
        g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
                         G_CALLBACK(notebook_switch_page), NULL);
+
+       GtkCssProvider *css = gtk_css_provider_new();
+       const char *css_data = "notebook header.left * { \
+                                       margin: 0; \
+                                       padding-top: 0; \
+                                       padding-bottom: 0; \
+                               }";
+       gtk_css_provider_load_from_data(css, css_data, strlen(css_data), NULL);
+       gtk_style_context_add_provider_for_screen(
+               gdk_screen_get_default(), GTK_STYLE_PROVIDER(css),
+               GTK_STYLE_PROVIDER_PRIORITY_USER);
+
+       gtk_widget_show_all(mw.win);
 }
 
 void
@@ -1163,49 +879,21 @@ notebook_switch_page(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data)
 {
        mainwindow_title(idx);
 }
-gboolean
-quit_if_nothing_active(void)
-{
-       if (clients == 0) {
-               if (downloads == 0) {
-                       gtk_main_quit();
-                       return TRUE;
-               } else {
-                       downloadmanager_show();
-               }
-       }
-       return FALSE;
-}
-gboolean
-remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
-{
-       gchar *uri = NULL;
-       g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
-       if (uri) {
-               g_strstrip(uri);
-               client_new(uri, NULL, TRUE, TRUE);
-               g_free(uri);
-       }
-       return TRUE;
-}
 
 void
 show_web_view(WebKitWebView *web_view, gpointer data)
 {
        struct Client *c = (struct Client *)data;
-       gint idx;
        (void)web_view;
+       gint idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
 
-       gtk_widget_show_all(mw.win);
-
-       if (c->focus_new_tab) {
-               idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
-               if (idx != -1)
-                       gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook),
-                                                     idx);
+       gtk_widget_show_all(GTK_WIDGET(
+               gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx)));
+       gtk_widget_hide(c->isearch_box);
 
-               gtk_widget_grab_focus(c->web_view);
-       }
+       if (idx != -1 && switch_tab)
+               gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
+       gtk_widget_grab_focus(c->web_view);
 }
 
 void
@@ -1229,11 +917,14 @@ trust_user_certs(WebKitWebContext *wc)
                        g_free(absfile);
                        if (cert == NULL)
                                fprintf(stderr,
-                                       "chorizo: warning: could not load trusted cert: %s\n",
+                                       "chorizo: warning: could not load"
+                                       "trusted cert: %s\n",
                                        file);
                        else
-                               webkit_web_context_allow_tls_certificate_for_host(
+                               // clang-format off
+                             webkit_web_context_allow_tls_certificate_for_host(
                                        wc, cert, file);
+                       // clang-format on
                        file = g_dir_read_name(dir);
                }
                g_dir_close(dir);
@@ -1251,7 +942,7 @@ main(int argc, char **argv)
 {
        int opt, i;
 
-       //TODO:pretty this
+       // TODO:pretty this
        cfg.noncooperative_instances = FALSE;
        cfg.cooperative_alone = TRUE;
        closed_tabs = malloc(0);
@@ -1294,21 +985,16 @@ main(int argc, char **argv)
        downloadmanager_setup();
        mainwindow_setup();
 
-       client_arr = malloc(sizeof(struct Client *));
-       if (!client_arr) allocfail();
-
        if (optind >= argc) {
-               client_new(cfg_home_uri, NULL, TRUE, TRUE);
+               client_new(cfg_home_uri, NULL);
        } else {
-               for (i = optind; i < argc; i++)
-                       client_new(argv[i], NULL, TRUE, TRUE);
+               for (i = optind; i < argc; i++) client_new(argv[i], NULL);
        }
 
        if (cfg.noncooperative_instances || cfg.cooperative_alone) {
                gtk_main();
                remove(fifopath);
        }
-       for (int i = 0; i < clients; i++) { free(&(client_arr[i])); }
 
        exit(EXIT_SUCCESS);
 }