Allow to set a default search engine.
authorDaniel Carl <danielcarl@gmx.de>
Mon, 25 Mar 2013 20:35:38 +0000 (21:35 +0100)
committerDaniel Carl <danielcarl@gmx.de>
Mon, 25 Mar 2013 20:35:38 +0000 (21:35 +0100)
Added command searchengine-default to set the handle of the default search
engine.

doc/vimb.1.txt
src/command.c
src/command.h
src/config.h
src/main.h
src/searchengine.c
src/searchengine.h

index 7aa0a49..85d1598 100644 (file)
@@ -255,6 +255,14 @@ Adds a searchengine with the \fISHORTCUT\fP and search \fIURI\fP. The uri must
 not contain more than one '%s' placeholder.
 
 Example: searchengine-add dl=https://duckduckgo.com/lite/?q=%s
+.TP
+.BI "searchengine-remove " "SHORTCUT"
+Remove the search engine to the given \fISHORTCUT\fP.
+.TP
+.BI "searchengine-default " "SHORTCUT"
+Set the search engine for given \fISHORTCUT\fP as the default search engine.
+It doesn't metter if the \fISHORTCUT\fP is already in use or not to be able to
+set the default search engine.
 .SS Configuration
 .TP
 .BI "set " VAR = VALUE
index 89b6b13..09c5766 100644 (file)
@@ -88,6 +88,7 @@ static CommandInfo cmd_list[] = {
     {"search-backward",     command_search,      {VB_SEARCH_BACKWARD}},
     {"searchengine-add",    command_searchengine,{1}},
     {"searchengine-remove", command_searchengine,{0}},
+    {"searchengine-default", command_searchengine_default,{0}},
     {"zoomin",              command_zoom,        {COMMAND_ZOOM_IN}},
     {"zoomout",             command_zoom,        {COMMAND_ZOOM_OUT}},
     {"zoominfull",          command_zoom,        {COMMAND_ZOOM_IN | COMMAND_ZOOM_FULL}},
@@ -474,6 +475,13 @@ gboolean command_searchengine(const Arg* arg)
     return result;
 }
 
+gboolean command_searchengine_default(const Arg* arg)
+{
+    vb_set_mode(VB_MODE_NORMAL, FALSE);
+
+    return searchengine_set_default(arg->s);
+}
+
 gboolean command_zoom(const Arg* arg)
 {
     float step, level;
index a2aeee2..15e8e1f 100644 (file)
@@ -67,6 +67,7 @@ gboolean command_yank(const Arg* arg);
 gboolean command_paste(const Arg* arg);
 gboolean command_search(const Arg* arg);
 gboolean command_searchengine(const Arg* arg);
+gboolean command_searchengine_default(const Arg* arg);
 gboolean command_zoom(const Arg* arg);
 gboolean command_history(const Arg* arg);
 
index 7341f9b..16853dd 100644 (file)
@@ -94,6 +94,7 @@ const struct {
     {"hmap <shift-tab>=hint-focus-prev"},
     {"searchengine-add dl=https://duckduckgo.com/lite/?q=%s"},
     {"searchengine-add dd=https://duckduckgo.com/?q=%s"},
+    {"searchengine-default dl"},
     {"set images=on"},
     {"set cursivfont=serif"},
     {"set defaultencondig=utf-8"},
index 76f3829..bb9fd9e 100644 (file)
@@ -258,6 +258,7 @@ typedef struct {
     GSList*     keys;
     GString*    modkeys;
     GSList*     searchengines;
+    char*       searchengine_default;   /* handle of the default search engine */
 } Behaviour;
 
 typedef struct {
index a19e076..9eb07cb 100644 (file)
 
 extern VbCore vb;
 
+typedef struct {
+    char* handle;
+    char* uri;
+} Searchengine;
+
 static GSList* searchengine_find(const char* handle);
 static gboolean searchengine_is_valid_uri(const char* uri);
 static void searchengine_free(Searchengine* se);
@@ -64,12 +69,24 @@ gboolean searchengine_remove(const char* handle)
     return FALSE;
 }
 
+gboolean searchengine_set_default(const char* handle)
+{
+    /* do not check if the search engin exists to be able to set the default
+     * before defining the search engines */
+    OVERWRITE_STRING(vb.behave.searchengine_default, handle);
+
+    return TRUE;
+}
+
 char* searchengine_get_uri(const char* handle)
 {
-    GSList* list = searchengine_find(handle);
+    char* def = vb.behave.searchengine_default;
+    GSList* l = searchengine_find(handle);
 
-    if (list) {
-        return ((Searchengine*)list->data)->uri;
+    if (l) {
+        return ((Searchengine*)l->data)->uri;
+    } else if (def && (l = searchengine_find(def))) {
+        return ((Searchengine*)l->data)->uri;
     }
 
     return NULL;
index d9079cc..4645c66 100644 (file)
 #ifndef _SEARCHENGINE_H
 #define _SEARCHENGINE_H
 
-typedef struct {
-    char* handle;
-    char* uri;
-} Searchengine;
-
 void searchengine_cleanup(void);
 gboolean searchengine_add(const char* handle, const char* uri);
 gboolean searchengine_remove(const char* handle);
+gboolean searchengine_set_default(const char* handle);
 char* searchengine_get_uri(const char* handle);
 
 #endif /* end of include guard: _SEARCHENGINE_H */