sites

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

surf-0.6-searchengines.diff (2237B)


      1 diff --git a/config.def.h b/config.def.h
      2 index 93a3d49..4ac7f15 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 fdfaab1..b6ac237 100644
     21 --- a/surf.c
     22 +++ b/surf.c
     23 @@ -91,6 +91,12 @@ 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 +
     32 +typedef struct {
     33  	char *regex;
     34  	char *style;
     35  	regex_t re;
     36 @@ -178,6 +184,7 @@ static void loaduri(Client *c, const Arg *arg);
     37  static void navigate(Client *c, const Arg *arg);
     38  static Client *newclient(void);
     39  static void newwindow(Client *c, const Arg *arg, gboolean noembed);
     40 +static gchar *parseuri(const gchar *uri);
     41  static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
     42  static gboolean contextmenu(WebKitWebView *view, GtkWidget *menu,
     43                              WebKitHitTestResult *target, gboolean keyboard,
     44 @@ -838,8 +845,7 @@ loaduri(Client *c, const Arg *arg)
     45  		u = g_strdup_printf("file://%s", rp);
     46  		free(rp);
     47  	} else {
     48 -		u = g_strrstr(uri, "://") ? g_strdup(uri)
     49 -		    : g_strdup_printf("http://%s", uri);
     50 +		u = parseuri(uri);
     51  	}
     52  
     53  	setatom(c, AtomUri, uri);
     54 @@ -1172,7 +1178,22 @@ menuactivate(GtkMenuItem *item, Client *c)
     55  	}
     56  }
     57  
     58 -void
     59 +static gchar *
     60 +parseuri(const gchar *uri) {
     61 +	guint i;
     62 +
     63 +	for (i = 0; i < LENGTH(searchengines); i++) {
     64 +		if (searchengines[i].token == NULL || searchengines[i].uri == NULL || \
     65 +		    *(uri + strlen(searchengines[i].token)) != ' ')
     66 +			continue;
     67 +		if (g_str_has_prefix(uri, searchengines[i].token))
     68 +			return g_strdup_printf(searchengines[i].uri, uri + strlen(searchengines[i].token) + 1);
     69 +	}
     70 +
     71 +	return g_strrstr(uri, "://") ? g_strdup(uri) : g_strdup_printf("http://%s", uri);
     72 +}
     73 +
     74 +static void
     75  pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
     76  {
     77  	Arg arg = {.v = text };