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