browser_object_up_url: support trimming #ref (fragment)
[conkeror.git] / modules / webjump.js
blob8e2fa1e8d57e1726d44094dd10dccd60bb4b1b2f
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 in_module(null);
12 var webjumps = {};
14 define_keywords("$completer", "$description", "$argument", "$alternative",
15                 "$post_data");
16 function define_webjump (key, handler) {
17     keywords(arguments);
18     var argument = arguments.$argument;
19     let alternative = arguments.$alternative;
20     var post_data = arguments.$post_data;
22     // handler may be a function or a string.  An alternative url may
23     // be passed using the $alternative keyword; it is used in place
24     // of the handler when no arguments are supplied by the user when
25     // invoking the webjump (see get_webjump).  For string webjumps
26     // that contain %s and for which no alternative is provided, an
27     // alternative is autogenerated by trimming the path from the url.
28     // A webjump can thus function both as a way to invoke a search
29     // and as a bookmark.
30     //
31     // When the handler is a string and post_data is given the webjump
32     // will result in a POST.  A '%s' element in post_data will be
33     // replaced by the webjump argument and the alternative is the
34     // same as the handler (but as a GET).
35     //
36     // The argument property may be false (no arguments will be
37     // accepted for the webjump), true (arguments are required for the
38     // webjump) or 'optional' (arguments are accepted but not
39     // required).  If the property is not specified, a sensible default
40     // is chosen depending on the type of the handler and whether an
41     // alternative is specified.  If the property is false, then
42     // completing on the name of the webjump in the minibuffer will
43     // not result in a space being appended.
44     //
45     if (typeof(handler) == "function") {
46         if (argument == null && alternative == null)
47             argument = true;
48     } else if (typeof(handler) == "string") {
49         if (post_data && alternative == null)
50             alternative = handler;
51         else if (handler.indexOf('%s') == -1)
52             argument = false;
53         else if (alternative == null)
54             alternative = url_path_trim(handler);
55     }
56     if (alternative && argument == null)
57         argument = 'optional';
59     function make_string_handler (template) {
60         var b = template.indexOf('%s');
61         return function (arg) {
62             var a = b + 2;
63             // Just return the same string if it doesn't contain a %s
64             if (b == -1)
65                 return template;
66             return template.substr(0,b) + encodeURIComponent(arg) + template.substring(a);
67         };
68     }
70     function make_post_handler (uri, post_data) {
71         return function (arg) {
72             return load_spec({
73                 uri: uri,
74                 post_data: make_post_data(post_data.map(function (pair) {
75                     if (pair[1] == '%s')
76                         return [pair[0], arg];
77                     else
78                         return pair;
79                 }))
80             });
81         };
82     }
84     if (typeof(handler) == "string") {
85         if (post_data)
86             handler = make_post_handler(handler, post_data);
87         else
88             handler = make_string_handler(handler);
89     }
91     webjumps[key] = { key: key,
92                       handler: handler,
93                       completer: arguments.$completer,
94                       description: arguments.$description,
95                       argument: argument,
96                       alternative: alternative};
99 function define_delicious_webjumps (username) {
100     define_webjump("delicious", "http://www.delicious.com/" + username + "/%s",
101                    $alternative = "http://www.delicious.com/" + username);
102     define_webjump("adelicious", "javascript:location.href='http://www.delicious.com/save"+
103                    "?v=2&url='+encodeURIComponent(location.href)+'&title='+"+
104                    "encodeURIComponent(document.title);");
105     define_webjump("sdelicious", "http://www.delicious.com/search?p=%s&u="+username+
106                    "&chk=&context=userposts&fr=del_icio_us&lc=1");
107     define_webjump("sadelicious", "http://www.delicious.com/search/all?search=%s");
110 add_delicious_webjumps = define_delicious_webjumps;
112 function define_lastfm_webjumps (username) {
113     if (! username) username = "";
114     define_webjump("lastfm", "http://www.last.fm/user/"+username);
115     define_webjump("lastfm-user", "http://www.last.fm/user/%s");
116     define_webjump("lastfm-music", "http://www.last.fm/search?m=all&q=%s");
117     define_webjump("lastfm-group", "http://www.last.fm/users/groups?s_bio=%s");
118     define_webjump("lastfm-tag", "http://www.last.fm/search?m=tag&q=%s");
119     define_webjump("lastfm-label", "http://www.last.fm/search?m=label&q=%s");
120     define_webjump("lastfm-event", "http://www.last.fm/events?by=artists&q=%s");
123 add_lastfm_webjumps = define_lastfm_webjumps;
125 function clear_webjumps () {
126     webjumps = {};
129 // Some built in web jumps
130 function define_default_webjumps () {
131     define_webjump("conkerorwiki",
132                    "http://conkeror.org/?action=fullsearch&context=60&value=%s&fullsearch=Text");
133     define_webjump("lucky",      "http://www.google.com/search?q=%s&btnI=I'm Feeling Lucky");
134     define_webjump("maps",       "http://maps.google.com/?q=%s");
135     define_webjump("scholar",    "http://scholar.google.com/scholar?q=%s");
136     define_webjump("slang",      "http://www.urbandictionary.com/define.php?term=%s");
137     define_webjump("dictionary", "http://dictionary.reference.com/search?q=%s");
138     define_webjump("image",      "http://images.google.com/images?q=%s");
139     define_webjump("clhs",
140                    "http://www.xach.com/clhs?q=%s",
141                    $alternative = "http://www.lispworks.com/documentation/HyperSpec/Front/index.htm");
142     define_webjump("cliki",      "http://www.cliki.net/admin/search?words=%s");
143     define_webjump("ratpoisonwiki", "http://ratpoison.antidesktop.net/?search=%s");
144     define_webjump("stumpwmwiki", "http://stumpwm.antidesktop.net/wiki?search=%s");
145     define_webjump("savannah",
146                    "http://savannah.gnu.org/search/?words=%s&type_of_search=soft&Search=Search&exact=1");
147     define_webjump("sourceforge", "http://sourceforge.net/search/?words=%s");
148     define_webjump("freshmeat", "http://freshmeat.net/search/?q=%s");
149     define_webjump("slashdot", "http://slashdot.org/search.pl?query=%s");
150     define_webjump("kuro5hin", "http://www.kuro5hin.org/?op=search&string=%s");
153 define_variable("webjump_partial_match", true,
154                 "When entering a url, if the input is not a webjump, " +
155                 "but would uniquely complete to a webjump, then accept " +
156                 "that webjump only if this is true.");
158 function match_webjump (str) {
159     var sp = str.indexOf(' ');
161     var key, arg;
162     if (sp == -1) {
163         key = str;
164         arg = null;
165     } else {
166         key = str.substring(0, sp);
167         arg = str.substring(sp + 1);
168         if (/^\s*$/.test(arg))
169             arg = null;
170     }
172     // Look for an exact match
173     var match = webjumps[key];
175     // Look for a partial match
176     if (!match && webjump_partial_match) {
177         for (let [k,v] in Iterator(webjumps)) {
178             if (String(k).substring(0, key.length) == key) {
179                 if (match) {
180                     // key is not a unique prefix, as there are multiple partial matches
181                     return null;
182                 }
183                 match = v;
184             }
185         }
186     }
188     if (match) {
189         if (arg == null && match.argument == true)
190             throw interactive_error('Webjump '+key+' requires an argument.');
191         return [match, key, arg];
192     }
193     return null;
197 function get_webjump (value) {
198     var res = match_webjump(value);
199     if (!res)
200         return null;
201     let [match,key,arg] = res;
202     if (arg == null && match.alternative)
203         return match.alternative;
204     return match.handler(arg);
207 function get_url_or_webjump (input) {
208     var url = get_webjump(input);
209     if (url != null)
210         return url;
211     else
212         return input;
215 define_default_webjumps();
217 function webjump_completer () {
218     let base_completer = prefix_completer(
219         $completions = [ v for ([k,v] in Iterator(webjumps)) ],
220         $get_string = function (x) { return x.key + (x.argument ? " " : ""); },
221         $get_description = function (x) { return x.description || ""; });
222     return function (input, pos, conservative) {
223         let str = input.substring(0,pos);
224         let res;
225         try { res = match_webjump(str); }
226         catch (e) { res = null; }
227         if (res) {
228             let [match, key, arg] = res;
229             if (arg != null) { // If there is no argument yet, we use the base completer
230                 if (match.completer) {
231                     let c = yield match.completer.call(null, arg, pos - key.length - 1, conservative);
232                     yield co_return(nest_completions(c, match.key + " "));
233                 }
234                 yield co_return(null);
235             }
236         }
237         yield co_return(base_completer(input, pos, conservative));
238     };
241 provide("webjump");