From: Daniel Carl Date: Sat, 27 Apr 2013 16:06:56 +0000 (+0200) Subject: Renamed searchengine to shortcut. X-Git-Url: https://git.owens.tech/assets/lich_lifts_title_slice.png/assets/lich_lifts_title_slice.png/git?a=commitdiff_plain;h=2bf1f59c07642af9689b8b4ffa0debefd6b07a0e;p=vimb.git Renamed searchengine to shortcut. The feature isn't only useful for searchengines but also in general for structured urls. An if the placeholder will be optional, we can use this to store also url quickmarks. --- diff --git a/README.md b/README.md index 1c6a2ea..e58cc8b 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ browsing-experience with low memory and cpu usage. - custom configuration files - tagged bookmarks - open input or teaxteaeas with configurable external editor +- user defined URL-shortcuts with placeholders ## Dependencies - libwebkit-1.0 diff --git a/doc/vimb.1.txt b/doc/vimb.1.txt index 18b1111..2ceffd6 100644 --- a/doc/vimb.1.txt +++ b/doc/vimb.1.txt @@ -1,3 +1,4 @@ +.\" vim: ft=groff .\" Process this file with .\" groff -man -Tascii vimb.1 .TH PROJECT 1 "06/04/2013" "PROJECT/VERSION" "Vimb Manual" @@ -249,21 +250,24 @@ Yank the current url to the primary and secondary clipboard. .TP .B yank-selection Yank the selected text into the primary and secondary clipboard. -.SS Searchengines -Searchengines allows to query a searchengine instead of typing a full url. If -a searchengine with shortcut 'dd' is defined you can use 'dd search terms' to -perform a search query. +.SS Shortcuts +Shortcuts allows to open URL build up from a named tamplate with additional +parameters. If a sortcut named 'dd' is defined, you can use it with `:open dd +list of parameters' to open the generated URL. + +Shortcuts are a good to use searchengines where the URL is nearly the same but +a single parameter is user diefined. .TP -.BI "searchengine-add " "SHORTCUT" "=" "URI" -Adds a searchengine with the \fISHORTCUT\fP and search \fIURI\fP. The uri must +.BI "shortcut-add " "SHORTCUT" "=" "URI" +Adds a shortcut 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 +Example: shortcut-add dl=https://duckduckgo.com/lite/?q=%s .TP -.BI "searchengine-remove " "SHORTCUT" +.BI "shortcut-remove " "SHORTCUT" Remove the search engine to the given \fISHORTCUT\fP. .TP -.BI "searchengine-default " "SHORTCUT" +.BI "shortcut-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. diff --git a/src/command.c b/src/command.c index 08bb8dd..0a62612 100644 --- a/src/command.c +++ b/src/command.c @@ -24,7 +24,7 @@ #include "completion.h" #include "hints.h" #include "util.h" -#include "searchengine.h" +#include "shortcut.h" #include "history.h" #include "bookmark.h" #include "dom.h" @@ -93,9 +93,9 @@ static CommandInfo cmd_list[] = { {"tabopen-clipboard", command_paste, {VB_CLIPBOARD_PRIMARY | VB_CLIPBOARD_SECONDARY | VB_TARGET_NEW}}, {"search-forward", command_search, {VB_SEARCH_FORWARD}}, {"search-backward", command_search, {VB_SEARCH_BACKWARD}}, - {"searchengine-add", command_searchengine, {1}}, - {"searchengine-remove", command_searchengine, {0}}, - {"searchengine-default", command_searchengine_default, {0}}, + {"shortcut-add", command_shortcut, {1}}, + {"shortcut-remove", command_shortcut, {0}}, + {"shortcut-default", command_shortcut_default, {0}}, {"zoomin", command_zoom, {COMMAND_ZOOM_IN}}, {"zoomout", command_zoom, {COMMAND_ZOOM_OUT}}, {"zoominfull", command_zoom, {COMMAND_ZOOM_IN | COMMAND_ZOOM_FULL}}, @@ -529,7 +529,7 @@ gboolean command_search(const Arg *arg) return true; } -gboolean command_searchengine(const Arg *arg) +gboolean command_shortcut(const Arg *arg) { gboolean result; if (arg->i) { @@ -538,13 +538,13 @@ gboolean command_searchengine(const Arg *arg) if ((handle = strchr(arg->s, '='))) { *handle = '\0'; handle++; - result = searchengine_add(arg->s, handle); + result = shortcut_add(arg->s, handle); } else { return false; } } else { - /* remove the search engine */ - result = searchengine_remove(arg->s); + /* remove the shortcut */ + result = shortcut_remove(arg->s); } vb_set_mode(VB_MODE_NORMAL, false); @@ -552,11 +552,11 @@ gboolean command_searchengine(const Arg *arg) return result; } -gboolean command_searchengine_default(const Arg *arg) +gboolean command_shortcut_default(const Arg *arg) { vb_set_mode(VB_MODE_NORMAL, false); - return searchengine_set_default(arg->s); + return shortcut_set_default(arg->s); } gboolean command_zoom(const Arg *arg) diff --git a/src/command.h b/src/command.h index a526e03..fb50ba7 100644 --- a/src/command.h +++ b/src/command.h @@ -69,8 +69,8 @@ gboolean command_hints_focus(const Arg *arg); 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_shortcut(const Arg *arg); +gboolean command_shortcut_default(const Arg *arg); gboolean command_zoom(const Arg *arg); gboolean command_history(const Arg *arg); gboolean command_bookmark(const Arg *arg); diff --git a/src/config.h b/src/config.h index 68d850c..838ffff 100644 --- a/src/config.h +++ b/src/config.h @@ -92,9 +92,9 @@ const char *default_config[] = { "cmap =hist-prev", "cmap =hist-next", "imap =editor", - "searchengine-add dl=https://duckduckgo.com/lite/?q=%s", - "searchengine-add dd=https://duckduckgo.com/?q=%s", - "searchengine-default dl", + "shortcut-add dl=https://duckduckgo.com/lite/?q=%s", + "shortcut-add dd=https://duckduckgo.com/?q=%s", + "shortcut-default dl", "set images=on", "set cursivfont=serif", "set defaultencondig=utf-8", diff --git a/src/main.c b/src/main.c index 2146103..862e29f 100644 --- a/src/main.c +++ b/src/main.c @@ -28,7 +28,7 @@ #include "completion.h" #include "dom.h" #include "hints.h" -#include "searchengine.h" +#include "shortcut.h" #include "history.h" #include "session.h" @@ -144,8 +144,8 @@ gboolean vb_load_uri(const Arg *arg) uri = g_strdup_printf("file://%s", rp); free(rp); } else if (!strchr(path, '.')) { - /* use a searchengine */ - uri = searchengine_get_uri(path); + /* use a shortcut */ + uri = shortcut_get_uri(path); } if (!uri) { @@ -1000,7 +1000,7 @@ static void destroy_client() command_cleanup(); setting_cleanup(); keybind_cleanup(); - searchengine_cleanup(); + shortcut_cleanup(); history_cleanup(); for (int i = 0; i < FILES_LAST; i++) { diff --git a/src/searchengine.c b/src/searchengine.c deleted file mode 100644 index 0724dd6..0000000 --- a/src/searchengine.c +++ /dev/null @@ -1,136 +0,0 @@ -/** - * vimb - a webkit based vim like browser. - * - * Copyright (C) 2012-2013 Daniel Carl - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - */ - -#include "main.h" -#include "searchengine.h" -#include "util.h" - -extern VbCore vb; - -typedef struct { - char *handle; - char *uri; -} Searchengine; - -static GSList *searchengines; -static char *default_handle = NULL; - -static GSList *find(const char *handle); -static void free_searchengine(Searchengine *se); - - -void searchengine_cleanup(void) -{ - if (searchengines) { - g_slist_free_full(searchengines, (GDestroyNotify)free_searchengine); - } -} - -gboolean searchengine_add(const char *handle, const char *uri) -{ - /* validate if the uri contains only one %s sequence */ - if (!util_valid_format_string(uri, 's', 1)) { - return false; - } - Searchengine *s = g_new0(Searchengine, 1); - - s->handle = g_strdup(handle); - s->uri = g_strdup(uri); - - searchengines = g_slist_prepend(searchengines, s); - - return true; -} - -gboolean searchengine_remove(const char *handle) -{ - GSList *list = find(handle); - - if (list) { - free_searchengine((Searchengine*)list->data); - searchengines = g_slist_delete_link(searchengines, list); - - return true; - } - - 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(default_handle, handle); - - return true; -} - -/** - * Retrieves the search uri for given query string. - * Not that the memory of the returned uri must be freed. - */ -char *searchengine_get_uri(const char *string) -{ - GSList *l = NULL; - char *p = NULL, *tmpl = NULL, *query = NULL, *uri = NULL; - - if ((p = strchr(string, ' '))) { - *p = '\0'; - /* is the first word the handle? */ - if ((l = find(string))) { - tmpl = ((Searchengine*)l->data)->uri; - query = soup_uri_encode(p + 1, "&"); - } else { - *p = ' '; - } - } - - if (!tmpl && (l = find(default_handle))) { - tmpl = ((Searchengine*)l->data)->uri; - query = soup_uri_encode(string, "&"); - } - - if (tmpl) { - uri = g_strdup_printf(tmpl, query); - g_free(query); - - return uri; - } - - return NULL; -} - -static GSList *find(const char *handle) -{ - GSList *s; - for (s = searchengines; s != NULL; s = s->next) { - if (!strcmp(((Searchengine*)s->data)->handle, handle)) { - return s; - } - } - - return NULL; -} - -static void free_searchengine(Searchengine *se) -{ - g_free(se->uri); - g_free(se->handle); - g_free(se); -} diff --git a/src/searchengine.h b/src/searchengine.h deleted file mode 100644 index a0b876b..0000000 --- a/src/searchengine.h +++ /dev/null @@ -1,29 +0,0 @@ -/** - * vimb - a webkit based vim like browser. - * - * Copyright (C) 2012-2013 Daniel Carl - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - */ - -#ifndef _SEARCHENGINE_H -#define _SEARCHENGINE_H - -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 */ diff --git a/src/shortcut.c b/src/shortcut.c new file mode 100644 index 0000000..1b22b03 --- /dev/null +++ b/src/shortcut.c @@ -0,0 +1,136 @@ +/** + * vimb - a webkit based vim like browser. + * + * Copyright (C) 2012-2013 Daniel Carl + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + */ + +#include "main.h" +#include "shortcut.h" +#include "util.h" + +extern VbCore vb; + +typedef struct { + char *handle; + char *uri; +} Shortcut; + +static GSList *shortcuts; +static char *default_handle = NULL; + +static GSList *find(const char *handle); +static void free_shortcut(Shortcut *se); + + +void shortcut_cleanup(void) +{ + if (shortcuts) { + g_slist_free_full(shortcuts, (GDestroyNotify)free_shortcut); + } +} + +gboolean shortcut_add(const char *handle, const char *uri) +{ + /* validate if the uri contains only one %s sequence */ + if (!util_valid_format_string(uri, 's', 1)) { + return false; + } + Shortcut *s = g_new0(Shortcut, 1); + + s->handle = g_strdup(handle); + s->uri = g_strdup(uri); + + shortcuts = g_slist_prepend(shortcuts, s); + + return true; +} + +gboolean shortcut_remove(const char *handle) +{ + GSList *list = find(handle); + + if (list) { + free_shortcut((Shortcut*)list->data); + shortcuts = g_slist_delete_link(shortcuts, list); + + return true; + } + + return false; +} + +gboolean shortcut_set_default(const char *handle) +{ + /* do not check if the shotcut exists to be able to set the default + * before defining the shotcut */ + OVERWRITE_STRING(default_handle, handle); + + return true; +} + +/** + * Retrieves the uri for given query string. Not that the memory of the + * returned uri must be freed. + */ +char *shortcut_get_uri(const char *string) +{ + GSList *l = NULL; + char *p = NULL, *tmpl = NULL, *query = NULL, *uri = NULL; + + if ((p = strchr(string, ' '))) { + *p = '\0'; + /* is the first word the handle? */ + if ((l = find(string))) { + tmpl = ((Shortcut*)l->data)->uri; + query = soup_uri_encode(p + 1, "&"); + } else { + *p = ' '; + } + } + + if (!tmpl && (l = find(default_handle))) { + tmpl = ((Shortcut*)l->data)->uri; + query = soup_uri_encode(string, "&"); + } + + if (tmpl) { + uri = g_strdup_printf(tmpl, query); + g_free(query); + + return uri; + } + + return NULL; +} + +static GSList *find(const char *handle) +{ + GSList *s; + for (s = shortcuts; s != NULL; s = s->next) { + if (!strcmp(((Shortcut*)s->data)->handle, handle)) { + return s; + } + } + + return NULL; +} + +static void free_shortcut(Shortcut *se) +{ + g_free(se->uri); + g_free(se->handle); + g_free(se); +} diff --git a/src/shortcut.h b/src/shortcut.h new file mode 100644 index 0000000..f55de7f --- /dev/null +++ b/src/shortcut.h @@ -0,0 +1,29 @@ +/** + * vimb - a webkit based vim like browser. + * + * Copyright (C) 2012-2013 Daniel Carl + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + */ + +#ifndef _SHORTCUT_H +#define _SHORTCUT_H + +void shortcut_cleanup(void); +gboolean shortcut_add(const char *handle, const char *uri); +gboolean shortcut_remove(const char *handle); +gboolean shortcut_set_default(const char *handle); +char *shortcut_get_uri(const char *handle); + +#endif /* end of include guard: _SHORTCUT_H */