changeset 2146:aac29835ca8c

Added an optional label to dillorc's search_url. Format: "[<label> ]<url>"
author Jorge Arellano Cid <jcid@dillo.org>
date So, 24 Jul 2011 13:47:25 -0400
parents 09d5a78e4244
children 4ebc05e21b67
files dillorc src/dialog.cc src/misc.c src/misc.h src/uicmd.cc
diffstat 5 files changed, 53 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/dillorc	So Jul 24 13:47:24 2011 -0400
+++ b/dillorc	So Jul 24 13:47:25 2011 -0400
@@ -109,11 +109,14 @@
 # home="file:/home/jcid/HomePage/Home.html"
 #home="http://www.dillo.org/"
 
-# Set the URL used by the web search dialog.
+# Set the URLs used by the web search dialog.
 # "%s" is replaced with the search keywords separated by '+'.
+# Format: search_url="[<label> ]<url>"
+# You can have several search engines, with the first being the default.
+# e.g.
+# search_url="http://duckduckgo.com/html?q=%s"
 # search_url="http://www.wikipedia.org/wiki/Special:Search?search=%s"
 # search_url="http://search.lycos.com/?query=%s"
-# search_url="http://duckduckgo.com/html?q=%s"
 #search_url="http://www.google.com/search?ie=UTF-8&oe=UTF-8&q=%s"
 
 # If set, dillo will ask web servers to send pages in this language.
--- a/src/dialog.cc	So Jul 24 13:47:24 2011 -0400
+++ b/src/dialog.cc	So Jul 24 13:47:25 2011 -0400
@@ -86,7 +86,8 @@
    CustChoice (int x, int y, int w, int h, const char* l=0) :
       Fl_Choice(x,y,w,h,l) {};
    int handle(int e) {
-      if (e == FL_KEYBOARD && Fl::event_key() == FL_Enter &&
+      if (e == FL_KEYBOARD &&
+          (Fl::event_key() == FL_Enter || Fl::event_key() == FL_Down) &&
           (Fl::event_state() & (FL_SHIFT|FL_CTRL|FL_ALT|FL_META)) == 0) {
          MSG("CustChoice: ENTER\n");
          return Fl_Choice::handle(FL_PUSH);
@@ -160,14 +161,12 @@
        int n_it = dList_length(prefs.search_urls);
        pm = new Fl_Menu_Item[n_it+1];
        memset(pm, '\0', sizeof(Fl_Menu_Item[n_it+1]));
-       for (int i = 0; i < n_it; i++) {
-          char *p, *q, label[32];
-          char *url = (char *)dList_nth_data(prefs.search_urls, i);
-          if ((p = strstr(url, "//")) && (q = strstr(p+2,"/"))) {
-             strncpy(label,p+2,MIN(q-p-2,31));
-             label[MIN(q-p-2,31)] = 0;
-          }
-          pm[i].label(FL_NORMAL_LABEL, strdup(label));
+       for (int i = 0, j = 0; i < n_it; i++) {
+          char *label, *url, *source;
+          source = (char *)dList_nth_data(prefs.search_urls, i);
+          if (a_Misc_parse_search_url(source, &label, &url) < 0)
+             continue;
+          pm[j++].label(FL_NORMAL_LABEL, strdup(label));
        }
     }
     ch->tooltip("Select search engine");
--- a/src/misc.c	So Jul 24 13:47:24 2011 -0400
+++ b/src/misc.c	So Jul 24 13:47:25 2011 -0400
@@ -383,6 +383,38 @@
 }
 
 /*
+ * Parse dillorc's search_url string ("[<label> ]<url>")
+ * Return value: -1 on error, 0 on success (and label and urlstr pointers)
+ */
+int a_Misc_parse_search_url(char *source, char **label, char **urlstr)
+{
+   static char buf[32];
+   char *p, *q;
+   int ret = -1;
+
+   if ((p = strrchr(source, ' '))) {
+      /* label and url pair */
+      strncpy(buf,source,MIN(p-source,31));
+      buf[MIN(p-source,31)] = 0;
+      source = p+1;
+      if ((p = strchr(source, '/')) && p[1] && (q = strchr(p+2,'/'))) {
+         *urlstr = source;
+         ret = 0;
+      }
+   } else {
+      /* url only, make a custom label */
+      if ((p = strchr(source, '/')) && p[1] && (q = strchr(p+2,'/'))) {
+         strncpy(buf,p+2,MIN(q-p-2,31));
+         buf[MIN(q-p-2,31)] = 0;
+         *urlstr = source;
+         ret = 0;
+      }
+   }
+   *label = buf;
+   return ret;
+}
+
+/*
  * Encodes string using base64 encoding.
  * Return value: new string or NULL if input string is empty.
  */
--- a/src/misc.h	So Jul 24 13:47:24 2011 -0400
+++ b/src/misc.h	So Jul 24 13:47:25 2011 -0400
@@ -17,6 +17,7 @@
                                char **charset);
 int a_Misc_content_type_cmp(const char* ct1, const char *ct2);
 int a_Misc_parse_geometry(char *geom, int *x, int *y, int *w, int *h);
+int a_Misc_parse_search_url(char *source, char **label, char **urlstr);
 char *a_Misc_encode_base64(const char *in);
 Dstr *a_Misc_file2dstr(const char *filename);
 
--- a/src/uicmd.cc	So Jul 24 13:47:24 2011 -0400
+++ b/src/uicmd.cc	So Jul 24 13:47:25 2011 -0400
@@ -36,6 +36,7 @@
 #include "history.h"
 #include "msg.h"
 #include "prefs.h"
+#include "misc.h"
 
 #include "dw/fltkviewport.hh"
 
@@ -779,12 +780,15 @@
  */
 static char *UIcmd_make_search_str(const char *str)
 {
-   char *search_url;
+   char *search_url, *l, *u, *c;
    char *keys = a_Url_encode_hex_str(str),
-        *c = (char*)dList_nth_data(prefs.search_urls, prefs.search_url_idx);
+        *src = (char*)dList_nth_data(prefs.search_urls, prefs.search_url_idx);
    Dstr *ds = dStr_sized_new(128);
 
-   for (; *c; c++) {
+   /* parse search_url into label and url */
+   a_Misc_parse_search_url(src, &l, &u);
+
+   for (c = u; *c; c++) {
       if (*c == '%')
          switch(*++c) {
          case 's':