aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Blott2015-05-17 07:55:11 +0100
committerStephen Blott2015-05-17 07:55:11 +0100
commit378bb6cf68e410aa0b68f15796aa395de61d18e1 (patch)
tree0838bda7c0bbf196a30978885ed7e0ae90d5a590
parentc2eb404ad87689ac886e1ddbab2c62d7d7a1be9d (diff)
downloadvimium-378bb6cf68e410aa0b68f15796aa395de61d18e1.tar.bz2
TabToOpen: insert queries for custom search engines.
-rw-r--r--background_scripts/completion.coffee38
1 files changed, 25 insertions, 13 deletions
diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee
index 80b47055..69cb10de 100644
--- a/background_scripts/completion.coffee
+++ b/background_scripts/completion.coffee
@@ -239,14 +239,6 @@ class HistoryCompleter
else
[]
onComplete results.map (entry) =>
- # This entry's URL might match the default search engine, in which case we'll insert its query text
- # into the vomnibar input whenever this entry is selected.
- insertText = Utils.extractQuery searchUrl, entry.url
-
- # If this history item does not have a title and we successfully extracted query text above, then use
- # that text in lieu of a title.
- entry.title ||= insertText if insertText?
-
new Suggestion
queryTerms: queryTerms
type: "history"
@@ -254,7 +246,6 @@ class HistoryCompleter
title: entry.title
relevancyFunction: @computeRelevancy
relevancyData: entry
- insertText: insertText
computeRelevancy: (suggestion) ->
historyEntry = suggestion.relevancyData
@@ -418,6 +409,7 @@ class SearchEngineCompleter
triageRequest: (request) ->
@searchEngines.use (engines) =>
{ queryTerms, query } = request
+ request.searchEngines = engines
keyword = queryTerms[0]
# Note. For a keyword "w", we match "w search terms" and "w ", but not "w" on its own.
if keyword and engines[keyword] and (1 < queryTerms.length or /\s$/.test query)
@@ -444,6 +436,7 @@ class SearchEngineCompleter
searchUrl: url
description: description
searchUrlPrefix: url.split("%s")[0]
+ insertTextPrefix: "#{keyword} "
callback engines
@@ -495,7 +488,7 @@ class SearchEngineCompleter
# We only accept suggestions:
# - from this completer, or
# - from other completers, but then only if their URL matches this search engine and matches this
- # query (that is only if their URL could have been generated by this search engine).
+ # query (that is only if their URL could have been generated by this search engine).
suggestions.filter (suggestion) ->
suggestion.type == description or
# This is a suggestion for the same search engine.
@@ -511,6 +504,7 @@ class SearchEngineCompleter
relevancy: 1
autoSelect: custom
highlightTerms: not haveCompletionEngine
+ isSearchSuggestion: true
mkSuggestion = (suggestion) ->
new Suggestion
@@ -521,6 +515,7 @@ class SearchEngineCompleter
relevancy: relevancy *= 0.9
insertText: suggestion
highlightTerms: false
+ isSearchSuggestion: true
cachedSuggestions =
if haveCompletionEngine then CompletionSearch.complete searchUrl, queryTerms else null
@@ -551,6 +546,20 @@ class SearchEngineCompleter
console.log "fetched suggestions:", suggestions.length, query if SearchEngineCompleter.debug
onComplete suggestions.map mkSuggestion
+ postProcessSuggestions: (request, suggestions) ->
+ return unless request.searchEngines
+ engines = (engine for _, engine of request.searchEngines)
+ engines.sort (a,b) -> b.searchUrl.length - a.searchUrl.length
+ engines.push insertTextPrefix: "", searchUrl: Settings.get "searchUrl"
+ for suggestion in suggestions
+ unless suggestion.isSearchSuggestion or suggestion.insertText
+ for engine in engines
+ if suggestion.insertText = Utils.extractQuery engine.searchUrl, suggestion.url
+ suggestion.insertText = "#{engine.insertTextPrefix}#{suggestion.insertText}"
+ suggestion.title ||= suggestion.insertText
+ break
+ delete request.searchEngines
+
# A completer which calls filter() on many completers, aggregates the results, ranks them, and returns the top
# 10. All queries from the vomnibar come through a multi completer.
class MultiCompleter
@@ -594,7 +603,7 @@ class MultiCompleter
# Post results, unless there are none and we will be running a continuation. This avoids
# collapsing the vomnibar briefly before expanding it again, which looks ugly.
unless suggestions.length == 0 and shouldRunContinuations
- suggestions = @prepareSuggestions queryTerms, suggestions
+ suggestions = @prepareSuggestions request, queryTerms, suggestions
onComplete
results: suggestions
mayCacheResults: continuations.length == 0
@@ -611,7 +620,7 @@ class MultiCompleter
jobs.onReady =>
suggestions = filter suggestions for filter in filters
- suggestions = @prepareSuggestions queryTerms, suggestions
+ suggestions = @prepareSuggestions request, queryTerms, suggestions
# We post these results even if a new query has started. The vomnibar will not display them
# (because they're arriving too late), but it will cache them.
onComplete
@@ -623,7 +632,7 @@ class MultiCompleter
if @mostRecentQuery
@filter @mostRecentQuery...
- prepareSuggestions: (queryTerms, suggestions) ->
+ prepareSuggestions: (request, queryTerms, suggestions) ->
# Compute suggestion relevancies and sort.
suggestion.computeRelevancy queryTerms for suggestion in suggestions
suggestions.sort (a, b) -> b.relevancy - a.relevancy
@@ -638,6 +647,9 @@ class MultiCompleter
break if count++ == @maxResults
seenUrls[url] = suggestion
+ # Give each completer an opportunity to tweak the suggestions.
+ completer.postProcessSuggestions? request, suggestions for completer in @completers
+
# Generate HTML for the remaining suggestions and return them.
suggestion.generateHtml() for suggestion in suggestions
suggestions