aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorNiklas Baumstark2012-01-21 01:31:18 +0100
committerNiklas Baumstark2012-04-10 23:54:35 +0200
commit253dc6f55885dd0185ab5b061ec2d9429c729ed9 (patch)
tree9c2161f911292dcf76e38335c528f9ba94197fdf /lib
parentc808c6a165b5fed117afe22ba0372e288bf6d5ab (diff)
downloadvimium-253dc6f55885dd0185ab5b061ec2d9429c729ed9.tar.bz2
add possibility to use custom search engines and use "wiki ", "cc ", and ";" (goto) as custom commands
Diffstat (limited to 'lib')
-rw-r--r--lib/completion.js51
1 files changed, 42 insertions, 9 deletions
diff --git a/lib/completion.js b/lib/completion.js
index 75785c34..f713eaa0 100644
--- a/lib/completion.js
+++ b/lib/completion.js
@@ -136,6 +136,11 @@ var completion = (function() {
}
}
+ /** Creates a function that returns a constant value */
+ function createConstantFunction(x) {
+ return function() { return x; }
+ }
+
/** Helper class to construct fuzzy completers for asynchronous data sources like history or bookmark
* matchers. */
var AsyncFuzzyUrlCompleter = function() {
@@ -190,30 +195,58 @@ var completion = (function() {
/** A simple completer that suggests to open the input string as an URL or to trigger a web search for the
* given term, depending on whether it thinks the input is an URL or not. */
- var SmartCompleter = function() {
+ var SmartCompleter = function(commands) {
+ commands = commands || {};
+ var commandKeys = Object.keys(commands);
+
this.refresh = function() { };
- this.filter = function(query, callback) {
- var url;
- var str;
+ /** Checks if the input is a special command and if yes, add according suggestions to the given array */
+ this.addCommandSuggestions = function(query, suggestions) {
+ // check if the input is a special command
+ for (var i = 0; i < commandKeys.length; ++i) {
+ var key = commandKeys[i];
+ if (query.indexOf(key) != 0)
+ continue;
+
+ var term = query.slice(key.length, query.length);
+ var command = commands[key];
+ var desc = command[0];
+ var urlPattern = command[1];
+ suggestions.push({
+ render: createConstantFunction('<em>' + desc + '</em> ' + term),
+ action: createActionOpenUrl(utils.createFullUrl(urlPattern.replace(/%s/g, term))),
+ });
+ }
+ }
+
+ /** Checks if the input is a URL. If yes, add the URL to the list of suggestions. If no, add a search
+ * query to the list of suggestions. */
+ this.addUrlOrSearchSuggestion = function(query, suggestions) {
+ var url, str;
// trim query
query = query.replace(/^\s+|\s+$/g, '');
if (utils.isUrl(query)) {
url = utils.createFullUrl(query);
- str = '<em>goto</em> ' + url;
+ str = '<em>goto</em> ' + query;
} else {
url = utils.createSearchUrl(query);
str = '<em>search</em> ' + query;
}
-
- // call back with exactly one suggestion
- callback([{
+ suggestions.push({
render: function() { return str; },
action: createActionOpenUrl(url),
// relevancy will always be the lowest one, so the suggestion is at the top
relevancy: -1,
- }]);
+ });
+ }
+
+ this.filter = function(query, callback) {
+ var suggestions = [];
+ this.addCommandSuggestions(query, suggestions);
+ this.addUrlOrSearchSuggestion(query, suggestions);
+ callback(suggestions);
};
}