]> git.cworth.org Git - sup/blob - lib/sup/modes/search-results-mode.rb
Merge branch 'dispatch-and-prev-redo' into next
[sup] / lib / sup / modes / search-results-mode.rb
1 module Redwood
2
3 class SearchResultsMode < ThreadIndexMode
4   def initialize qobj, qopts = nil
5     @qobj = qobj
6     @qopts = qopts
7
8     super [], { :qobj => @qobj }.merge(@qopts)
9   end
10
11   register_keymap do |k|
12     k.add :refine_search, "Refine search", '|'
13   end
14
15   def refine_search
16     query = BufferManager.ask :search, "refine query: ", (@qobj.to_s + " ")
17     return unless query && query !~ /^\s*$/
18     SearchResultsMode.spawn_from_query query
19   end
20
21   ## a proper is_relevant? method requires some way of asking ferret
22   ## if an in-memory object satisfies a query. i'm not sure how to do
23   ## that yet. in the worst case i can make an in-memory index, add
24   ## the message, and search against it to see if i have > 0 results,
25   ## but that seems pretty insane.
26
27   def self.spawn_from_query text
28     begin
29       qobj, extraopts = Index.parse_user_query_string(text)
30       return unless qobj
31       short_text = text.length < 20 ? text : text[0 ... 20] + "..."
32       mode = SearchResultsMode.new qobj, extraopts
33       BufferManager.spawn "search: \"#{short_text}\"", mode
34       mode.load_threads :num => mode.buffer.content_height
35     rescue Ferret::QueryParser::QueryParseException => e
36       BufferManager.flash "Couldn't parse query."
37     end
38   end
39 end
40
41 end