Add webjump integration with OpenSearch search engines
[conkeror.git] / modules / webjump.js
blobc60ab673aaeeb1c07d3f4d1e6d7bbb8c636bd5da
2 //// web jump stuff
4 var webjumps = new string_hashmap();
6 define_keywords("$completer", "$description", "$no_argument");
7 function define_webjump(key, handler) {
8     keywords(arguments);
10     if (typeof(handler) == "string") {
11         let template = handler;
12         handler = function (arg) {
13             var b = template.indexOf('%s');
14             var a = b + 2;
15             // Just return the same string if it doesn't contain a %s
16             if (b == -1)
17                 return template;
18             else
19                 return template.substr(0,b) + encodeURIComponent(arg) + template.substring(a);
20         };
21     }
22     webjumps.put(key,
23                  {key: key,
24                   handler: handler, completer: arguments.$completer,
25                   description: arguments.$description,
26                   no_argument: arguments.$no_argument});
29 // Compatibility
30 var add_webjump = define_webjump;
32 function add_delicious_webjumps (username)
34     add_webjump("delicious", " http://del.icio.us/" + username);
35     add_webjump("adelicious", "javascript:location.href='http://del.icio.us/" + username + "?v=2&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title);");
36     add_webjump("sdelicious", " http://del.icio.us/search/?search=%s");
37     add_webjump("sadelicious", " http://del.icio.us/search/all?search=%s");
40 function clear_webjumps()
42     webjumps = {};
45 // Some built in web jumps
46 function define_default_webjumps()
48     add_webjump("conkerorwiki",
49                 "http://conkeror.org/?action=fullsearch&context=60&value=%s&fullsearch=Text");
50     add_webjump("lucky",      "http://www.google.com/search?q=%s&btnI=I'm Feeling Lucky");
51     add_webjump("maps",       "http://maps.google.com/?q=%s");
52     add_webjump("scholar",    "http://scholar.google.com/scholar?q=%s");
53     add_webjump("clusty",     "http://www.clusty.com/search?query=%s");
54     add_webjump("slang",      "http://www.urbandictionary.com/define.php?term=%s");
55     add_webjump("dictionary", "http://dictionary.reference.com/search?q=%s");
56     add_webjump("xulplanet",  "http://www.google.com/custom?q=%s&cof=S%3A"+
57                 "http%3A%2F%2Fwww.xulplanet.com%3BAH%3Aleft%3BLH%3A65%3BLC"+
58                 "%3A4682B4%3BL%3Ahttp%3A%2F%2Fwww.xulplanet.com%2Fimages%2F"+
59                 "xulplanet.png%3BALC%3Ablue%3BLW%3A215%3BAWFID%3A0979f384d5"+
60                 "181409%3B&domains=xulplanet.com&sitesearch=xulplanet.com&sa=Go");
61     add_webjump("image",      "http://images.google.com/images?q=%s");
62     add_webjump("clhs",       "http://www.xach.com/clhs?q=%s");
63     add_webjump("emacswiki",  "http://www.emacswiki.org/cgi-bin/wiki?search=%s");
64     add_webjump("cliki",      "http://www.cliki.net/admin/search?words=%s");
65     add_webjump("ratpoisonwiki", "http://ratpoison.elektrubadur.se/?search=%s");
66     add_webjump("stumpwmwiki", "http://stumpwm.elektrubadur.se/?search=%s");
67     add_webjump("savannah", "http://savannah.gnu.org/search/?words=%s&type_of_search=soft&Search=Search&exact=1");
68     add_webjump("sourceforge", "http://sourceforge.net/search/?words=%s");
69     add_webjump("freshmeat", "http://freshmeat.net/search/?q=%s");
70     add_webjump("slashdot", "http://slashdot.org/search.pl?query=%s");
71     add_webjump("kuro5hin", "http://www.kuro5hin.org/?op=search&string=%s");
72     add_webjump("sheldonbrown",     "http://www.google.com/search?q=site:sheldonbrown.com %s");
75 function match_webjump(str) {
76     var sp = str.indexOf(' ');
78     var key, arg;
79     if (sp == -1) {
80         key = str;
81         arg = null;
82     } else {
83         key = str.substring(0, sp);
84         arg = str.substring(sp + 1);
85     }
87     // Look for an exact match
88     var match = webjumps.get(key);
90     // Look for a partial match
91     if (!match) {
92         for (let [k,v] in webjumps.iterator()) {
93             if (k.substring(0, key.length) == key) {
94                 if (match) {
95                     // key is not a unique prefix, as there are multiple partial matches
96                     return null;
97                 }
98                 match = v;
99             }
100         }
101     }
103     if (match) {
104         if (!arg && !match.no_argument) {
105             // This webjump requires an argument, but none was given
106             return null;
107         }
108         return [match, key, arg];
109     }
110     return null;
114 function getWebJump(value)
116     var res = match_webjump(value);
117     if (!res)
118         return null;
119     let [match,key,arg] = res;
120     return match.handler(arg);
123 function get_url_or_webjump(input)
125     var url = getWebJump(input);
127     if (url != null) {
128         return url;
129     } else {
130         return input;
131     }
134 define_default_webjumps();
136 function webjump_completer()
138     let base_completer = prefix_completer(
139         $completions = [ v for ([k,v] in webjumps.iterator()) ],
140         $get_string = function (x) x.key + (x.no_argument ? "" : " "),
141         $get_description = function (x) x.description || "");
143     return function(input, pos, conservative) {
144         let str = input.substring(0,pos);
145         let res = match_webjump(str);
146         if (res) {
147             let [match, key, arg] = res;
148             if (arg != null) { // If there is no argument yet, we use the base completer
149                 if (match.completer) {
150                     let c = yield match.completer.call(null, arg, pos - key.length - 1, conservative);
151                     yield co_return(nest_completions(c, match.key + " "));
152                 }
153                 yield co_return(null);
154             }
155         }
156         yield co_return(base_completer(input, pos, conservative));
157     }