From 55940e07d46729b95c41e035e92691e630a843e3 Mon Sep 17 00:00:00 2001 From: David Kettler Date: Thu, 5 Feb 2009 11:17:23 +1030 Subject: [PATCH] webjump: use $alternative keyword; array form no longer accepted 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 of specifying the handler as an array of two strings was peculiar and will no longer be supported. 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. --- modules/webjump.js | 71 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/modules/webjump.js b/modules/webjump.js index ce429d2..e4b8456 100644 --- a/modules/webjump.js +++ b/modules/webjump.js @@ -9,39 +9,47 @@ 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 or a string. An alternative url may + // be passed 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) == "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 +60,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 +112,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"); -- 2.11.4.GIT