rename browser-follow-next/previous to follow-next/previous
[conkeror.git] / modules / webjump.js
blob85eb0042a182e7f2d4d0c058fcef0031b9be1d3c
1 /**
2  * (C) Copyright 2004-2007 Shawn Betts
3  * (C) Copyright 2007-2008 John J. Foerch
4  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
5  *
6  * Use, modification, and distribution are subject to the terms specified in the
7  * COPYING file.
8 **/
10 //// web jump stuff
12 var webjumps = new string_hashmap();
14 define_keywords("$completer", "$description", "$no_argument");
15 function define_webjump(key, handler) {
16     keywords(arguments);
18     if (typeof(handler) == "string") {
19         let template = handler;
20         handler = function (arg) {
21             var b = template.indexOf('%s');
22             var a = b + 2;
23             // Just return the same string if it doesn't contain a %s
24             if (b == -1)
25                 return template;
26             else
27                 return template.substr(0,b) + encodeURIComponent(arg) + template.substring(a);
28         };
29     }
30     webjumps.put(key,
31                  {key: key,
32                   handler: handler, completer: arguments.$completer,
33                   description: arguments.$description,
34                   no_argument: arguments.$no_argument});
37 // Compatibility
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()
50     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(' ');
86     var key, arg;
87     if (sp == -1) {
88         key = str;
89         arg = null;
90     } else {
91         key = str.substring(0, sp);
92         arg = str.substring(sp + 1);
93     }
95     // Look for an exact match
96     var match = webjumps.get(key);
98     // Look for a partial match
99     if (!match) {
100         for (let [k,v] in webjumps.iterator()) {
101             if (k.substring(0, key.length) == key) {
102                 if (match) {
103                     // key is not a unique prefix, as there are multiple partial matches
104                     return null;
105                 }
106                 match = v;
107             }
108         }
109     }
111     if (match) {
112         if (arg == null && !match.no_argument) {
113             // This webjump requires an argument, but none was given
114             return null;
115         }
116         return [match, key, arg];
117     }
118     return null;
122 function getWebJump(value)
124     var res = match_webjump(value);
125     if (!res)
126         return null;
127     let [match,key,arg] = res;
128     return match.handler(arg);
131 function get_url_or_webjump(input)
133     var url = getWebJump(input);
135     if (url != null) {
136         return url;
137     } else {
138         return input;
139     }
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);
154         if (res) {
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 + " "));
160                 }
161                 yield co_return(null);
162             }
163         }
164         yield co_return(base_completer(input, pos, conservative));
165     }