2 * (C) Copyright 2004-2007 Shawn Betts
3 * (C) Copyright 2007-2008 John J. Foerch
4 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
6 * Use, modification, and distribution are subject to the terms specified in the
12 var webjumps = new string_hashmap();
14 define_keywords("$completer", "$description", "$no_argument");
15 function define_webjump(key, handler) {
18 if (typeof(handler) == "string") {
19 let template = handler;
20 handler = function (arg) {
21 var b = template.indexOf('%s');
23 // Just return the same string if it doesn't contain a %s
27 return template.substr(0,b) + encodeURIComponent(arg) + template.substring(a);
32 handler: handler, completer: arguments.$completer,
33 description: arguments.$description,
34 no_argument: arguments.$no_argument});
38 var add_webjump = define_webjump;
40 function add_delicious_webjumps (username)
42 add_webjump("delicious", " http://del.icio.us/" + username);
43 add_webjump("adelicious", "javascript:location.href='http://del.icio.us/" + username + "?v=2&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title);");
44 add_webjump("sdelicious", " http://del.icio.us/search/?search=%s");
45 add_webjump("sadelicious", " http://del.icio.us/search/all?search=%s");
48 function clear_webjumps()
53 // Some built in web jumps
54 function define_default_webjumps()
56 add_webjump("conkerorwiki",
57 "http://conkeror.org/?action=fullsearch&context=60&value=%s&fullsearch=Text");
58 add_webjump("lucky", "http://www.google.com/search?q=%s&btnI=I'm Feeling Lucky");
59 add_webjump("maps", "http://maps.google.com/?q=%s");
60 add_webjump("scholar", "http://scholar.google.com/scholar?q=%s");
61 add_webjump("clusty", "http://www.clusty.com/search?query=%s");
62 add_webjump("slang", "http://www.urbandictionary.com/define.php?term=%s");
63 add_webjump("dictionary", "http://dictionary.reference.com/search?q=%s");
64 add_webjump("xulplanet", "http://www.google.com/custom?q=%s&cof=S%3A"+
65 "http%3A%2F%2Fwww.xulplanet.com%3BAH%3Aleft%3BLH%3A65%3BLC"+
66 "%3A4682B4%3BL%3Ahttp%3A%2F%2Fwww.xulplanet.com%2Fimages%2F"+
67 "xulplanet.png%3BALC%3Ablue%3BLW%3A215%3BAWFID%3A0979f384d5"+
68 "181409%3B&domains=xulplanet.com&sitesearch=xulplanet.com&sa=Go");
69 add_webjump("image", "http://images.google.com/images?q=%s");
70 add_webjump("clhs", "http://www.xach.com/clhs?q=%s");
71 add_webjump("emacswiki", "http://www.emacswiki.org/cgi-bin/wiki?search=%s");
72 add_webjump("cliki", "http://www.cliki.net/admin/search?words=%s");
73 add_webjump("ratpoisonwiki", "http://ratpoison.elektrubadur.se/?search=%s");
74 add_webjump("stumpwmwiki", "http://stumpwm.elektrubadur.se/?search=%s");
75 add_webjump("savannah", "http://savannah.gnu.org/search/?words=%s&type_of_search=soft&Search=Search&exact=1");
76 add_webjump("sourceforge", "http://sourceforge.net/search/?words=%s");
77 add_webjump("freshmeat", "http://freshmeat.net/search/?q=%s");
78 add_webjump("slashdot", "http://slashdot.org/search.pl?query=%s");
79 add_webjump("kuro5hin", "http://www.kuro5hin.org/?op=search&string=%s");
80 add_webjump("sheldonbrown", "http://www.google.com/search?q=site:sheldonbrown.com %s");
83 function match_webjump(str) {
84 var sp = str.indexOf(' ');
91 key = str.substring(0, sp);
92 arg = str.substring(sp + 1);
95 // Look for an exact match
96 var match = webjumps.get(key);
98 // Look for a partial match
100 for (let [k,v] in webjumps.iterator()) {
101 if (k.substring(0, key.length) == key) {
103 // key is not a unique prefix, as there are multiple partial matches
112 if (arg == null && !match.no_argument) {
113 // This webjump requires an argument, but none was given
116 return [match, key, arg];
122 function getWebJump(value)
124 var res = match_webjump(value);
127 let [match,key,arg] = res;
128 return match.handler(arg);
131 function get_url_or_webjump(input)
133 var url = getWebJump(input);
142 define_default_webjumps();
144 function webjump_completer()
146 let base_completer = prefix_completer(
147 $completions = [ v for ([k,v] in webjumps.iterator()) ],
148 $get_string = function (x) x.key + (x.no_argument ? "" : " "),
149 $get_description = function (x) x.description || "");
151 return function(input, pos, conservative) {
152 let str = input.substring(0,pos);
153 let res = match_webjump(str);
155 let [match, key, arg] = res;
156 if (arg != null) { // If there is no argument yet, we use the base completer
157 if (match.completer) {
158 let c = yield match.completer.call(null, arg, pos - key.length - 1, conservative);
159 yield co_return(nest_completions(c, match.key + " "));
161 yield co_return(null);
164 yield co_return(base_completer(input, pos, conservative));