surf-searchengines-20220804-609ea1c.diff - sites - public wiki contents of suckless.org
 (HTM) git clone git://git.suckless.org/sites
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       surf-searchengines-20220804-609ea1c.diff (2833B)
       ---
            1 From 2f64431f15777d93d146707dccdb6ad063c7a316 Mon Sep 17 00:00:00 2001
            2 From: Justinas Grigas <jstn_as@protonmail.com>
            3 Date: Thu, 4 Aug 2022 23:18:40 +0300
            4 Subject: [PATCH] searchengines: allows simple use of search engines
            5 
            6 The previous patches had some issues:
            7 * don't apply cleanly to the latest version.
            8 * a space between the token and query is implied, so having " " as a
            9   token means you actually have to use "  ". Or if your token is "e",
           10   searching for "example.com" would trigger it. Now you specify the exact
           11   token to look for.
           12 * has checks to skip badly configured search engines. The correct
           13   solution is to configure them right.
           14 
           15 Now it works like a better version of the spacesearch patch, as it
           16 allows you to specify " " as a token
           17 ---
           18  config.def.h |  5 +++++
           19  surf.c       | 22 +++++++++++++++++++++-
           20  2 files changed, 26 insertions(+), 1 deletion(-)
           21 
           22 diff --git a/config.def.h b/config.def.h
           23 index 075f7d0..7bb9c46 100644
           24 --- a/config.def.h
           25 +++ b/config.def.h
           26 @@ -8,6 +8,11 @@ static char *cachedir       = "~/.local/share/surf/cache/";
           27  static char *cookiefile     = "~/.local/share/surf/cookies.txt";
           28  static char *historyfile    = "~/.local/share/surf/history.txt";
           29  
           30 +static SearchEngine searchengines[] = {
           31 +        { " ", "https://duckduckgo.com/?q=%s" },
           32 +        { "osrs ", "https://oldschool.runescape.wiki/?search=%s" },
           33 +};
           34 +
           35  /* Webkit default features */
           36  /* Highest priority value will be used.
           37   * Default parameters are priority 0
           38 diff --git a/surf.c b/surf.c
           39 index a2b507c..7e85952 100644
           40 --- a/surf.c
           41 +++ b/surf.c
           42 @@ -133,6 +133,11 @@ typedef struct {
           43          unsigned int stopevent;
           44  } Button;
           45  
           46 +typedef struct {
           47 +        char *token;
           48 +        char *uri;
           49 +} SearchEngine;
           50 +
           51  typedef struct {
           52          const char *uri;
           53          Parameter config[ParameterLast];
           54 @@ -220,6 +225,7 @@ static void webprocessterminated(WebKitWebView *v,
           55                                   Client *c);
           56  static void closeview(WebKitWebView *v, Client *c);
           57  static void destroywin(GtkWidget* w, Client *c);
           58 +static gchar *parseuri(const gchar *uri);
           59  
           60  /* Hotkeys */
           61  static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
           62 @@ -584,7 +590,7 @@ loaduri(Client *c, const Arg *a)
           63                          url = g_strdup_printf("file://%s", path);
           64                          free(path);
           65                  } else {
           66 -                        url = g_strdup_printf("http://%s", uri);
           67 +                        url = parseuri(uri);
           68                  }
           69                  if (apath != uri)
           70                          free(apath);
           71 @@ -1811,6 +1817,20 @@ destroywin(GtkWidget* w, Client *c)
           72                  gtk_main_quit();
           73  }
           74  
           75 +gchar *
           76 +parseuri(const gchar *uri)
           77 +{
           78 +        guint i;
           79 +
           80 +        for (i = 0; i < LENGTH(searchengines); i++) {
           81 +                if (g_str_has_prefix(uri, searchengines[i].token))
           82 +                        return g_strdup_printf(searchengines[i].uri,
           83 +                                               uri + strlen(searchengines[i].token));
           84 +        }
           85 +
           86 +        return g_strdup_printf("http://%s", uri);
           87 +}
           88 +
           89  void
           90  pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
           91  {
           92 -- 
           93 2.37.1
           94