From: Daniel Carl <danielcarl@gmx.de>
Date: Sun, 5 May 2013 11:58:31 +0000 (+0200)
Subject: Simplified the shortcut system.
X-Git-Url: https://git.owens.tech/projects.html/projects.html/git?a=commitdiff_plain;h=ee19f17849f6526556e7d717f16b48e6066c55cd;p=vimb.git

Simplified the shortcut system.

Don't treat the $0 with a special meaning to be filled by the whole query
string. Instead split the query string into as many parts like the highest
available placeholder + 1. That means if only $0 is used in a shortcut this
will also be replaced by the whole query string, but it could also be only the
first parameter in the case when the shortcut contains also other
placeholders.
---

diff --git a/doc/vimb.1.txt b/doc/vimb.1.txt
index c9962a3..5d61736 100644
--- a/doc/vimb.1.txt
+++ b/doc/vimb.1.txt
@@ -257,16 +257,16 @@ Shortcuts are a good to use with search engines where the URL is nearly the
 same but a single parameter is user defined.
 .TP
 .BI "shortcut-add " "SHORTCUT" "=" "URI"
-Adds a shortcut with the \fISHORTCUT\fP and search \fIURI\fP. The \fIURI\fP
+Adds a shortcut with the \fISHORTCUT\fP and \fIURI\fP template. The \fIURI\fP
 can contain multiple placeholders $0-$9 that will be filled by the parameters
-given when the shortcut is called. The $0 placeholder will be filled with all
-given parameters (this is useful for search engines), all other placeholders $1
-to $9 will be replaced by the numbered argument.
+given when the shortcut is called. The parameters given when the shortcut is
+called will be split into as many parameters like the highest used
+placeholder.
 
 Example 1: shortcut-add dl=https://duckduckgo.com/lite/?q=$0 to setup a
 search engine. Can be called by `:open dl my search phrase'.
 
-Example 2: shortcut-add gh=https://github.com/$1/$2 to build urls from given
+Example 2: shortcut-add gh=https://github.com/$0/$1 to build urls from given
 parameters. Can be called `:open gh fanglingsu vimb'.
 .TP
 .BI "shortcut-remove " "SHORTCUT"
diff --git a/src/shortcut.c b/src/shortcut.c
index 7d53b1a..02d194b 100644
--- a/src/shortcut.c
+++ b/src/shortcut.c
@@ -70,21 +70,17 @@ gboolean shortcut_set_default(const char *key)
 char *shortcut_get_uri(const char *string)
 {
     const char *tmpl, *query = NULL;
+    char *uri, **parts, ph[3] = "$0";
+    unsigned int len;
+    int max;
 
     tmpl = shortcut_lookup(string, &query);
     if (!tmpl) {
         return NULL;
     }
 
-    char *qs, *uri, **parts, ph[3] = "$0";
-    unsigned int len;
-
-    /* replace $0 with all parameters */
-    qs = soup_uri_encode(query, "&");
-    uri = util_str_replace(ph, qs, tmpl);
-    g_free(qs);
-
-    int max = get_max_placeholder(tmpl);
+    uri = g_strdup(tmpl);
+    max = get_max_placeholder(tmpl);
     /* skip if no placeholders found */
     if (max < 0) {
         return uri;
@@ -95,8 +91,8 @@ char *shortcut_get_uri(const char *string)
     len   = g_strv_length(parts);
 
     for (unsigned int n = 0; n < len; n++) {
-        char *new;
-        ph[1] = n + '1';
+        char *new, *qs;
+        ph[1] = n + '0';
         qs  = soup_uri_encode(parts[n], "&");
         new = util_str_replace(ph, qs, uri);
         g_free(qs);