new-tabs: fix bug in tab_bar_kill_buffer
[conkeror.git] / modules / webjump.js
blob7411910d7637e0e1985c63f5065fd05f8c21f80d
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 function define_webjump (key, handler) {
16     keywords(arguments);
17     var argument = arguments.$argument;
18     let alternative = arguments.$alternative;
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     // The argument property may be false (no arguments will be
30     // accepted for the webjump), true (arguments are required for the
31     // webjump) or 'optional' (arguments are accepted but not
32     // required).  If the property is not specified, a sensible default
33     // is chosen depending on the type of the handler and whether an
34     // alternative is specified.  If the property is false, then
35     // completing on the name of the webjump in the minibuffer will
36     // not result in a space being appended.
37     //
38     if (typeof(handler) == "function") {
39         if (argument == null && alternative == null)
40             argument = true;
41     } else if (typeof(handler) == "string") {
42         if (handler.indexOf('%s') == -1)
43             argument = false;
44         else if (alternative == null)
45             alternative = url_path_trim(handler);
46     }
47     if (alternative && argument == null)
48         argument = 'optional';
50     function make_handler (template) {
51         var b = template.indexOf('%s');
52         return function (arg) {
53             var a = b + 2;
54             // Just return the same string if it doesn't contain a %s
55             if (b == -1)
56                 return template;
57             return template.substr(0,b) + encodeURIComponent(arg) + template.substring(a);
58         };
59     }
61     if (typeof(handler) == "string")
62         handler = make_handler(handler);
64     webjumps[key] = { key: key,
65                       handler: handler,
66                       completer: arguments.$completer,
67                       description: arguments.$description,
68                       argument: argument,
69                       alternative: alternative};
72 function define_delicious_webjumps (username) {
73     define_webjump("delicious", "http://www.delicious.com/" + username + "/%s",
74                    $alternative = "http://www.delicious.com/" + username);
75     define_webjump("adelicious", "javascript:location.href='http://www.delicious.com/"+username+
76                    "?v=2&url='+encodeURIComponent(location.href)+'&title='+"+
77                    "encodeURIComponent(document.title);");
78     define_webjump("sdelicious", "http://www.delicious.com/search?p=%s&u="+username+
79                    "&chk=&context=userposts&fr=del_icio_us&lc=1");
80     define_webjump("sadelicious", "http://www.delicious.com/search/all?search=%s");
83 add_delicious_webjumps = define_delicious_webjumps;
85 function define_lastfm_webjumps (username) {
86     if (! username) username = "";
87     define_webjump("lastfm", "http://www.last.fm/user/"+username);
88     define_webjump("lastfm-user", "http://www.last.fm/user/%s");
89     define_webjump("lastfm-music", "http://www.last.fm/search?m=all&q=%s");
90     define_webjump("lastfm-group", "http://www.last.fm/users/groups?s_bio=%s");
91     define_webjump("lastfm-tag", "http://www.last.fm/search?m=tag&q=%s");
92     define_webjump("lastfm-label", "http://www.last.fm/search?m=label&q=%s");
93     define_webjump("lastfm-event", "http://www.last.fm/events?by=artists&q=%s");
96 add_lastfm_webjumps = define_lastfm_webjumps;
98 function clear_webjumps () {
99     webjumps = {};
102 // Some built in web jumps
103 function define_default_webjumps () {
104     define_webjump("conkerorwiki",
105                    "http://conkeror.org/?action=fullsearch&context=60&value=%s&fullsearch=Text");
106     define_webjump("lucky",      "http://www.google.com/search?q=%s&btnI=I'm Feeling Lucky");
107     define_webjump("maps",       "http://maps.google.com/?q=%s");
108     define_webjump("scholar",    "http://scholar.google.com/scholar?q=%s");
109     define_webjump("clusty",     "http://www.clusty.com/search?query=%s");
110     define_webjump("slang",      "http://www.urbandictionary.com/define.php?term=%s");
111     define_webjump("dictionary", "http://dictionary.reference.com/search?q=%s");
112     define_webjump("image",      "http://images.google.com/images?q=%s");
113     define_webjump("clhs",
114                    "http://www.xach.com/clhs?q=%s",
115                    $alternative = "http://www.lispworks.com/documentation/HyperSpec/Front/index.htm");
116     define_webjump("cliki",      "http://www.cliki.net/admin/search?words=%s");
117     define_webjump("ratpoisonwiki", "http://ratpoison.antidesktop.net/?search=%s");
118     define_webjump("stumpwmwiki", "http://stumpwm.antidesktop.net/wiki?search=%s");
119     define_webjump("savannah",
120                    "http://savannah.gnu.org/search/?words=%s&type_of_search=soft&Search=Search&exact=1");
121     define_webjump("sourceforge", "http://sourceforge.net/search/?words=%s");
122     define_webjump("freshmeat", "http://freshmeat.net/search/?q=%s");
123     define_webjump("slashdot", "http://slashdot.org/search.pl?query=%s");
124     define_webjump("kuro5hin", "http://www.kuro5hin.org/?op=search&string=%s");
127 define_variable("webjump_partial_match", true,
128                 "When entering a url, if the input is not a webjump, " +
129                 "but would uniquely complete to a webjump, then accept " +
130                 "that webjump only if this is true.");
132 function match_webjump (str) {
133     var sp = str.indexOf(' ');
135     var key, arg;
136     if (sp == -1) {
137         key = str;
138         arg = null;
139     } else {
140         key = str.substring(0, sp);
141         arg = str.substring(sp + 1);
142         if (/^\s*$/.test(arg))
143             arg = null;
144     }
146     // Look for an exact match
147     var match = webjumps[key];
149     // Look for a partial match
150     if (!match && webjump_partial_match) {
151         for (let [k,v] in Iterator(webjumps)) {
152             if (k.substring(0, key.length) == key) {
153                 if (match) {
154                     // key is not a unique prefix, as there are multiple partial matches
155                     return null;
156                 }
157                 match = v;
158             }
159         }
160     }
162     if (match) {
163         if (arg == null && match.argument == true)
164             throw interactive_error('Webjump '+key+' requires an argument.');
165         return [match, key, arg];
166     }
167     return null;
171 function get_webjump (value) {
172     var res = match_webjump(value);
173     if (!res)
174         return null;
175     let [match,key,arg] = res;
176     if (arg == null && match.alternative)
177         return match.alternative;
178     return match.handler(arg);
181 function get_url_or_webjump (input) {
182     var url = get_webjump(input);
183     if (url != null)
184         return url;
185     else
186         return input;
189 define_default_webjumps();
191 function webjump_completer () {
192     let base_completer = prefix_completer(
193         $completions = [ v for ([k,v] in Iterator(webjumps)) ],
194         $get_string = function (x) { return x.key + (x.argument ? " " : ""); },
195         $get_description = function (x) { return x.description || ""; });
196     return function (input, pos, conservative) {
197         let str = input.substring(0,pos);
198         let res;
199         try { res = match_webjump(str); }
200         catch (e) { res = null; }
201         if (res) {
202             let [match, key, arg] = res;
203             if (arg != null) { // If there is no argument yet, we use the base completer
204                 if (match.completer) {
205                     let c = yield match.completer.call(null, arg, pos - key.length - 1, conservative);
206                     yield co_return(nest_completions(c, match.key + " "));
207                 }
208                 yield co_return(null);
209             }
210         }
211         yield co_return(base_completer(input, pos, conservative));
212     };
215 provide("webjump");