Merge branch 'searchengines'
authorDaniel Liu <mr.picklepinosaur@gmail.com>
Tue, 4 Aug 2020 02:50:28 +0000 (22:50 -0400)
committerDaniel Liu <mr.picklepinosaur@gmail.com>
Tue, 4 Aug 2020 02:50:28 +0000 (22:50 -0400)
1  2 
config.def.h
surf.c

diff --combined config.def.h
@@@ -7,6 -7,12 +7,12 @@@ static char *certdir        = "~/.surf/
  static char *cachedir       = "~/.surf/cache/";
  static char *cookiefile     = "~/.surf/cookies.txt";
  
+ static SearchEngine searchengines[] = {
+     { "g",    "https://www.google.com/search?q=%s"             },
+     { "ji",   "https://jisho.org/search/%s"                    },
+     { "aw",   "https://wiki.archlinux.org/index.php?search=%s" }
+ };
  /* Webkit default features */
  /* Highest priority value will be used.
   * Default parameters are priority 0
@@@ -70,9 -76,8 +76,9 @@@ static WebKitFindOptions findopts = WEB
  #define SETPROP(r, s, p) { \
          .v = (const char *[]){ "/bin/sh", "-c", \
               "prop=\"$(printf '%b' \"$(xprop -id $1 $2 " \
 -             "| sed \"s/^$2(STRING) = //;s/^\\\"\\(.*\\)\\\"$/\\1/\")\" " \
 -             "| dmenu -p \"$4\" -w $1)\" && xprop -id $1 -f $3 8s -set $3 \"$prop\"", \
 +             "| sed \"s/^$2(STRING) = //;s/^\\\"\\(.*\\)\\\"$/\\1/\" && cat ~/.surf/bookmarks)\" " \
 +             "| dmenu -l 10 -p \"$4\" -w $1)\" && " \
 +             "xprop -id $1 -f $3 8s -set $3 \"$prop\"", \
               "surf-setprop", winid, r, s, p, NULL \
          } \
  }
          } \
  }
  
 +/* BM_ADD(readprop) */
 +#define BM_ADD(r) {\
 +        .v = (const char *[]){ "/bin/sh", "-c", \
 +             "(echo $(xprop -id $0 $1) | cut -d '\"' -f2 " \
 +             "| sed 's/.*https*:\\/\\/\\(www\\.\\)\\?//' && cat ~/.surf/bookmarks) " \
 +             "| awk '!seen[$0]++' > ~/.surf/bookmarks.tmp && " \
 +             "mv ~/.surf/bookmarks.tmp ~/.surf/bookmarks", \
 +             winid, r, NULL \
 +        } \
 +}
 +
  /* styles */
  /*
   * The iteration will stop at the first match, beginning at the beginning of
@@@ -145,7 -139,6 +151,7 @@@ static Key keys[] = 
        { MODKEY,                GDK_KEY_g,      spawn,      SETPROP("_SURF_URI", "_SURF_GO", PROMPT_GO) },
        { MODKEY,                GDK_KEY_f,      spawn,      SETPROP("_SURF_FIND", "_SURF_FIND", PROMPT_FIND) },
        { MODKEY,                GDK_KEY_slash,  spawn,      SETPROP("_SURF_FIND", "_SURF_FIND", PROMPT_FIND) },
 +      { MODKEY,                GDK_KEY_m,      spawn,      BM_ADD("_SURF_URI") },
  
        { 0,                     GDK_KEY_Escape, stop,       { 0 } },
        { MODKEY,                GDK_KEY_c,      stop,       { 0 } },
@@@ -206,5 -199,3 +212,5 @@@ static Button buttons[] = 
        { OnAny,        0,              9,      clicknavigate,  { .i = +1 },    1 },
        { OnMedia,      MODKEY,         1,      clickexternplayer, { 0 },       1 },
  };
 +
 +#define HOMEPAGE "https://duckduckgo.com/"
diff --combined surf.c
--- 1/surf.c
--- 2/surf.c
+++ b/surf.c
@@@ -128,6 -128,12 +128,12 @@@ typedef struct 
        unsigned int stopevent;
  } Button;
  
+ typedef struct {
+    char *token;
+    char *uri;
+ } SearchEngine;
  typedef struct {
        const char *uri;
        Parameter config[ParameterLast];
@@@ -214,6 -220,7 +220,7 @@@ static void webprocessterminated(WebKit
                                   Client *c);
  static void closeview(WebKitWebView *v, Client *c);
  static void destroywin(GtkWidget* w, Client *c);
+ static gchar *parseuri(const gchar *uri);
  
  /* Hotkeys */
  static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
@@@ -559,7 -566,7 +566,7 @@@ loaduri(Client *c, const Arg *a
                        url = g_strdup_printf("file://%s", path);
                        free(path);
                } else {
-                       url = g_strdup_printf("http://%s", uri);
+                       url = parseuri(uri);
                }
                if (apath != uri)
                        free(apath);
@@@ -1765,6 -1772,22 +1772,22 @@@ destroywin(GtkWidget* w, Client *c
                gtk_main_quit();
  }
  
+ gchar *
+ parseuri(const gchar *uri) {
+    guint i;
+    for (i = 0; i < LENGTH(searchengines); i++) {
+        if (searchengines[i].token == NULL || searchengines[i].uri == NULL ||
+            *(uri + strlen(searchengines[i].token)) != ' ')
+            continue;
+        if (g_str_has_prefix(uri, searchengines[i].token))
+            return g_strdup_printf(searchengines[i].uri,
+                           uri + strlen(searchengines[i].token) + 1);
+    }
+    return g_strdup_printf("http://%s", uri);
+ }
  void
  pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
  {
@@@ -2111,11 -2134,7 +2134,11 @@@ main(int argc, char *argv[]
        if (argc > 0)
                arg.v = argv[0];
        else
 +#ifdef HOMEPAGE
 +              arg.v = HOMEPAGE;
 +#else
                arg.v = "about:blank";
 +#endif
  
        setup();
        c = newclient(NULL);