Most webjumps are used as a shorthand to a web site search page.  But
when no arguments are supplied to the webjump, the effect is either an
error from conkeror (when there are no spaces) or a search with a
blank search term.  Neither is particularly useful.

This commit alters the default webjump handler so that a webjump with
no arguments will visit a relevant page, usually the associated home
page.  In most cases this is just the base URL of the webjump.  For
instance, visiting the emacswiki webjump with no arguments will now
load the emacswiki.org front page.

Most webjumps gain this new functionality without needing
modification.  However in some cases the search url is unrelated to
the desired home page url.  In this case the webjump definition can
supply an array of two elements; the first is the search url and the
second is the home page url.  The provided clhs and xulplanet webjumps
are treated in this way.

A similar fall back facility could be provided for search engine
webjumps, but that is not done here.

The meaning of the webjump no_argument keyword is extended to support
these optional arguments.  As previously, a value of true means that
no arguments are allowed and a false value means that arguments are
required.  The extension is that any other value ("maybe" is used)
means that arguments are optional.

This commit also includes a small fix to match_webjump().  A webjump
keyword followed by spaces is now interpreted as meaning no arguments.

The function compute_url_pre_path() is also included to calculate a
base URL from a given URL.
---

The no_argument extension is a bit confusing.  Possibly a separate
parameter should be used instead.

Perhaps the check for whether handler is an array should be more
stringent.

If preferred I could split this into separate commits:
  - match_webjump() fix
  - compute_url_pre_path()
  - default handler and no_argument changes
  - changes to provided webjumps


 modules/utils.js   |    9 +++++++++
 modules/webjump.js |   32 +++++++++++++++++++++++---------
 2 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/modules/utils.js b/modules/utils.js
index 2ca17f4..994a3d1 100644
--- a/modules/utils.js
+++ b/modules/utils.js
@@ -1266,6 +1266,15 @@ function compute_url_up_path (url)
 }
 
 
+function compute_url_pre_path (url)
+{
+    var new_url = Cc["@mozilla.org/network/standard-url;1"]
+        .createInstance (Ci.nsIURL);
+    new_url.spec = url;
+    return new_url.prePath;
+}
+
+
 /* possibly_valid_url returns true if the string might be a valid
  * thing to pass to nsIWebNavigation.loadURI.  Currently just checks
  * that there's no whitespace in the middle and that it's not entirely
diff --git a/modules/webjump.js b/modules/webjump.js
index c2886dd..e276115 100644
--- a/modules/webjump.js
+++ b/modules/webjump.js
@@ -17,20 +17,31 @@ function define_webjump(key, handler) {
 
     var no_argument = arguments.$no_argument;
 
-    if (typeof(handler) == "string") {
-        let template = handler;
+    make_handler = function(template, alternative) {
         let b = template.indexOf('%s');
         if (b == -1)
             no_argument = true;
-        handler = function (arg) {
+        if (alternative == null)
+            alternative = compute_url_pre_path(template);
+        if (alternative && !no_argument)
+            no_argument = "maybe";
+        return function (arg) {
             var a = b + 2;
+            if (arg == null)
+                return alternative;
             // Just return the same string if it doesn't contain a %s
             if (b == -1)
                 return template;
-            else
-                return template.substr(0,b) + encodeURIComponent(arg) + 
template.substring(a);
+            return template.substr(0,b) + encodeURIComponent(arg) + 
template.substring(a);
         };
     }
+
+    if (typeof(handler) == "string")
+        handler = make_handler(handler);
+    if (typeof(handler) == "object")
+        // An array of a template and an alternative url (for no args)
+        handler = make_handler(handler[0], handler[1]);
+
     webjumps.put(key,
                  {key: key,
                   handler: handler, completer: arguments.$completer,
@@ -65,13 +76,14 @@ function define_default_webjumps()
     add_webjump("clusty",     "http://www.clusty.com/search?query=%s";);
     add_webjump("slang",      
"http://www.urbandictionary.com/define.php?term=%s";);
     add_webjump("dictionary", "http://dictionary.reference.com/search?q=%s";);
-    add_webjump("xulplanet",  "http://www.google.com/custom?q=%s&cof=S%3A"+
+    add_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");
+                
"181409%3B&domains=xulplanet.com&sitesearch=xulplanet.com&sa=Go",
+                "http://xulplanet.com";]);
     add_webjump("image",      "http://images.google.com/images?q=%s";);
-    add_webjump("clhs",       "http://www.xach.com/clhs?q=%s";);
+    add_webjump("clhs",       ["http://www.xach.com/clhs?q=%s";, 
"http://www.lispworks.com/documentation/HyperSpec/Front/index.htm";]);
     add_webjump("emacswiki",  
"http://www.emacswiki.org/cgi-bin/wiki?search=%s";);
     add_webjump("cliki",      "http://www.cliki.net/admin/search?words=%s";);
     add_webjump("ratpoisonwiki", 
"http://ratpoison.antidesktop.net/?search=%s";);
@@ -95,6 +107,8 @@ function match_webjump(str) {
     } else {
         key = str.substring(0, sp);
         arg = str.substring(sp + 1);
+        if (/^\s*$/.test(arg))
+            arg = null;
     }
 
     // Look for an exact match
@@ -149,7 +163,7 @@ function webjump_completer()
 {
     let base_completer = prefix_completer(
         $completions = [ v for ([k,v] in webjumps.iterator()) ],
-        $get_string = function (x) x.key + (x.no_argument ? "" : " "),
+        $get_string = function (x) x.key + (x.no_argument==true ? "" : " "),
         $get_description = function (x) x.description || "");
 
     return function(input, pos, conservative) {
-- 

-- 
IMPORTANT: This email remains the property of the Australian Defence
Organisation and is subject to the jurisdiction of section 70 of the
CRIMES ACT 1914. If you have received this email in error, you are
requested to contact the sender and delete the email.

_______________________________________________
Conkeror mailing list
[email protected]
https://www.mozdev.org/mailman/listinfo/conkeror

Reply via email to