[Conkeror] [PATCH 1/2] webjump: reworked code for the "alternative" url

David Kettler kettler at internode.on.net
Fri Feb 6 20:49:35 PST 2009


The alternative url (which is used when no argument is supplied by the
user when invoking a webjump) should now be specified explicitly using
the $alternative keyword to define_webjump().  The previous method
(which will continue to be supported) of specifying the handler as an
array of two strings was a bit arbitrary.  Furthermore, it seems quite
peculiar when considering an expansion of alternative urls for function
handlers.

Prepared for allowing search engine webjumps to specify an alternative
url by allowing function handlers to have an alternative url.

Expanded the comment describing the parameters to define_webjump().

The supplied webjumps that have alternative urls are changed to use
the new style (although the old style will still work).
---
 modules/webjump.js |   76 ++++++++++++++++++++++++++++++---------------------
 1 files changed, 45 insertions(+), 31 deletions(-)

diff --git a/modules/webjump.js b/modules/webjump.js
index ce429d2..572329e 100644
--- a/modules/webjump.js
+++ b/modules/webjump.js
@@ -9,39 +9,54 @@
 
 var webjumps = {};
 
-define_keywords("$completer", "$description", "$argument");
+define_keywords("$completer", "$description", "$argument", "$alternative");
 function define_webjump(key, handler) {
-    keywords(arguments, $argument = true);
-    var argument;
-
-    // handler may be a function, a string, or an array
-    // of a string and a "no args" alternate string.
+    keywords(arguments);
+    var argument = arguments.$argument;
+    let alternative = arguments.$alternative;
+
+    // handler may be a function, a string, or (discouraged) an array
+    // of a string and an alternative string.  The alternative string
+    // may be passed explictly using the $alternative keyword; it is
+    // used in place of the handler when no arguments are supplied by
+    // the user when invoking the webjump.  For string webjumps that
+    // contain %s and for which no alternative is provided, an
+    // alternative is autogenerated by trimming the path from the url.
+    // A webjump can thus function both as a way to invoke a search
+    // and as a bookmark.
     //
-    // if the argument property was given as false, then completing on
-    // the name of the webjump in the minibuffer will not result in a
-    // space being appended.  for string webjumps that contain %s, the
-    // argument is set as optional and a no-arg alternative is
-    // autogenerated by trimming the path from the url.
+    // The argument property may be false (no arguments will be
+    // accepted for the webjump), true (arguments are required for the
+    // webjump) or 'optional' (arguments are accepted but not
+    // required).  If the property is not specified, a sensible default
+    // is chosen depending on the type of the handler and whether an
+    // alternative is specified.  If the property is false, then
+    // completing on the name of the webjump in the minibuffer will
+    // not result in a space being appended.
     //
+    if (typeof(handler) == "object" && handler.length == 2) {
+        // the webjump was given as a two element array, with
+        // the second element being the provided alternative.
+        alternative = handler[1];
+        handler = handler[0];
+    }
     if (typeof(handler) == "function") {
-        argument = arguments.$argument;
+        if (argument == null && alternative == null)
+            argument = true;
     } else if (typeof(handler) == "string") {
         if (handler.indexOf('%s') == -1)
             argument = false;
-        else
-            argument = 'optional';
-    } else if (typeof(handler) == "object")
-        // the webjump was given as a two element array, with
-        // the second element being the provided alternative.
+        else if (alternative == null)
+            alternative = url_path_trim(handler);
+    }
+    if (alternative && argument == null)
         argument = 'optional';
 
-    function make_handler (template, alternative) {
-        if (alternative == null)
-            alternative = url_path_trim(template);
+    function make_handler (template) {
         var b = template.indexOf('%s');
         return function (arg) {
             var a = b + 2;
-            if (arg == null && b > -1)
+            if (arg == null && alternative)
                 return alternative;
             // Just return the same string if it doesn't contain a %s
             if (b == -1)
@@ -52,9 +67,6 @@ function define_webjump(key, handler) {
 
     if (typeof(handler) == "string")
         handler = make_handler(handler);
-    else if (typeof(handler) == "object")
-        // An array of a template and an alternative url (for no args)
-        handler = make_handler(handler[0], handler[1]);
 
     webjumps[key] = { key: key,
                       handler: handler,
@@ -107,15 +119,17 @@ function define_default_webjumps()
     define_webjump("clusty",     "http://www.clusty.com/search?query=%s");
     define_webjump("slang",      "http://www.urbandictionary.com/define.php?term=%s");
     define_webjump("dictionary", "http://dictionary.reference.com/search?q=%s");
-    define_webjump("xulplanet",  ["http://www.google.com/custom?q=%s&cof=S%3A"+
-				  "http%3A%2F%2Fwww.xulplanet.com%3BAH%3Aleft%3BLH%3A65%3BLC"+
-				  "%3A4682B4%3BL%3Ahttp%3A%2F%2Fwww.xulplanet.com%2Fimages%2F"+
-				  "xulplanet.png%3BALC%3Ablue%3BLW%3A215%3BAWFID%3A0979f384d5"+
-				  "181409%3B&domains=xulplanet.com&sitesearch=xulplanet.com&sa=Go",
-				  "http://xulplanet.com"]);
+    define_webjump("xulplanet",
+                   "http://www.google.com/custom?q=%s&cof=S%3A"+
+		   "http%3A%2F%2Fwww.xulplanet.com%3BAH%3Aleft%3BLH%3A65%3BLC"+
+		   "%3A4682B4%3BL%3Ahttp%3A%2F%2Fwww.xulplanet.com%2Fimages%2F"+
+		   "xulplanet.png%3BALC%3Ablue%3BLW%3A215%3BAWFID%3A0979f384d5"+
+		   "181409%3B&domains=xulplanet.com&sitesearch=xulplanet.com&sa=Go",
+		   $alternative = "http://xulplanet.com");
     define_webjump("image",      "http://images.google.com/images?q=%s");
-    define_webjump("clhs",       ["http://www.xach.com/clhs?q=%s",
-                                  "http://www.lispworks.com/documentation/HyperSpec/Front/index.htm"]);
+    define_webjump("clhs",
+                   "http://www.xach.com/clhs?q=%s",
+                   $alternative = "http://www.lispworks.com/documentation/HyperSpec/Front/index.htm");
     define_webjump("emacswiki",  "http://www.emacswiki.org/cgi-bin/wiki?search=%s");
     define_webjump("cliki",      "http://www.cliki.net/admin/search?words=%s");
     define_webjump("ratpoisonwiki", "http://ratpoison.antidesktop.net/?search=%s");
-- 
1.5.6.5



More information about the Conkeror mailing list