sites

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

surf-0.4-searchengines.diff (1606B)


      1 diff -r dbb565b8d61c surf.c
      2 --- a/surf.c	Fri Jun 25 09:42:58 2010 +0200
      3 +++ b/surf.c	Fri Aug 13 16:28:31 2010 -0400
      4 @@ -55,6 +55,11 @@
      5  	const Arg arg;
      6  } Key;
      7  
      8 +typedef struct {
      9 +	char *token;
     10 +	char *uri;
     11 +} SearchEngine;
     12 +
     13  static Display *dpy;
     14  static Atom atoms[AtomLast];
     15  static Client *clients = NULL;
     16 @@ -90,6 +95,7 @@
     17  static Client *newclient(void);
     18  static void newwindow(Client *c, const Arg *arg);
     19  static void newrequest(SoupSession *s, SoupMessage *msg, gpointer v);
     20 +static gchar *parseuri(const gchar *uri);
     21  static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
     22  static void print(Client *c, const Arg *arg);
     23  static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer d);
     24 @@ -388,8 +394,7 @@
     25  
     26  	if(strcmp(uri, "") == 0)
     27  		return;
     28 -	u = g_strrstr(uri, "://") ? g_strdup(uri)
     29 -		: g_strdup_printf("http://%s", uri);
     30 +	u = parseuri(uri);
     31  	/* prevents endless loop */
     32  	if(c->uri && strcmp(u, c->uri) == 0) {
     33  		reload(c, &a);
     34 @@ -562,6 +567,19 @@
     35  	spawn(NULL, &a);
     36  }
     37  
     38 +
     39 +gchar *
     40 +parseuri(const gchar *uri) {
     41 +	guint i;
     42 +	for (i = 0; i < LENGTH(searchengines); i++) {
     43 +		if (searchengines[i].token == NULL || searchengines[i].uri == NULL || *(uri + strlen(searchengines[i].token)) != ' ')
     44 +			continue;
     45 +		if (g_str_has_prefix(uri, searchengines[i].token))
     46 +			return g_strdup_printf(searchengines[i].uri, uri + strlen(searchengines[i].token) + 1);
     47 +	}
     48 +	return g_strrstr(uri, "://") ? g_strdup(uri) : g_strdup_printf("http://%s", uri);
     49 +}
     50 +
     51  void
     52  pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
     53  	Arg arg = {.v = text };