sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

surf-git-20160127-searchengines.diff (2280B)


      1 diff --git a/config.def.h b/config.def.h
      2 index 93a3d49..df96d15 100644
      3 --- a/config.def.h
      4 +++ b/config.def.h
      5 @@ -75,6 +75,13 @@ static SiteStyle styles[] = {
      6  	{ ".*",                 "default.css" },
      7  };
      8  
      9 +/* search engines */
     10 +static SearchEngine searchengines[] = {
     11 +       { "g",   "http://www.google.de/search?q=%s"   },
     12 +       { "leo", "http://dict.leo.org/ende?search=%s" },
     13 +       { "ddg", "https://duckduckgo.com/?q=%s"       },
     14 +};
     15 +
     16  #define MODKEY GDK_CONTROL_MASK
     17  
     18  /* hotkeys */
     19 diff --git a/surf.c b/surf.c
     20 index 23c49bd..579848d 100644
     21 --- a/surf.c
     22 +++ b/surf.c
     23 @@ -92,6 +92,11 @@ typedef struct {
     24  G_DEFINE_TYPE(CookieJar, cookiejar, SOUP_TYPE_COOKIE_JAR_TEXT)
     25  
     26  typedef struct {
     27 +	char *token;
     28 +	char *uri;
     29 +} SearchEngine;
     30 +
     31 +typedef struct {
     32  	char *regex;
     33  	char *style;
     34  	regex_t re;
     35 @@ -179,6 +184,7 @@ static void loaduri(Client *c, const Arg *arg);
     36  static void navigate(Client *c, const Arg *arg);
     37  static Client *newclient(void);
     38  static void newwindow(Client *c, const Arg *arg, gboolean noembed);
     39 +static gchar *parseuri(const gchar *uri);
     40  static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
     41  static gboolean contextmenu(WebKitWebView *view, GtkWidget *menu,
     42                              WebKitHitTestResult *target, gboolean keyboard,
     43 @@ -840,8 +846,7 @@ loaduri(Client *c, const Arg *arg)
     44  		u = g_strdup_printf("file://%s", rp);
     45  		free(rp);
     46  	} else {
     47 -		u = g_strrstr(uri, "://") || g_str_has_prefix(uri, "about:") ? g_strdup(uri)
     48 -		    : g_strdup_printf("http://%s", uri);
     49 +		u = parseuri(uri);
     50  	}
     51  
     52  	setatom(c, AtomUri, uri);
     53 @@ -1173,6 +1178,21 @@ menuactivate(GtkMenuItem *item, Client *c)
     54  	}
     55  }
     56  
     57 +gchar *
     58 +parseuri(const gchar *uri) {
     59 +	guint i;
     60 +
     61 +	for (i = 0; i < LENGTH(searchengines); i++) {
     62 +		if (searchengines[i].token == NULL || searchengines[i].uri == NULL
     63 +		    || *(uri + strlen(searchengines[i].token)) != ' ')
     64 +			continue;
     65 +		if (g_str_has_prefix(uri, searchengines[i].token))
     66 +			return g_strdup_printf(searchengines[i].uri, uri + strlen(searchengines[i].token) + 1);
     67 +	}
     68 +	return g_strrstr(uri, "://") || g_str_has_prefix(uri, "about:") ? g_strdup(uri)
     69 +	                 : g_strdup_printf("http://%s", uri);
     70 +}
     71 +
     72  void
     73  pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
     74  {