diff options
| -rw-r--r-- | fuzzyMode.js | 6 | ||||
| -rw-r--r-- | lib/completion.js | 51 |
2 files changed, 47 insertions, 10 deletions
diff --git a/fuzzyMode.js b/fuzzyMode.js index ec2e351b..0387e97b 100644 --- a/fuzzyMode.js +++ b/fuzzyMode.js @@ -4,7 +4,11 @@ var fuzzyMode = (function() { function start(newTab) { if (!fuzzyBox) { var completer = new completion.MergingCompleter([ - new completion.SmartCompleter(), + new completion.SmartCompleter({ + 'wiki ': [ 'Wikipedia (en)', 'http://en.wikipedia.org/wiki/%s' ], + 'cc ' : [ 'dict.cc', 'http://www.dict.cc/?s=%s' ], + ';' : [ 'goto', '%s' ] + }), new completion.FuzzyHistoryCompleter(1000), new completion.FuzzyBookmarkCompleter(), ]); 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); }; } |
