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