]> git.cworth.org Git - sup/blob - lib/sup/modes/search-results-mode.rb
121e8176a5e1060aa3aa96057f9cf526b758f5e4
[sup] / lib / sup / modes / search-results-mode.rb
1 module Redwood
2
3 class SearchResultsMode < ThreadIndexMode
4   def initialize query
5     @query = query
6     super [], query
7   end
8
9   register_keymap do |k|
10     k.add :refine_search, "Refine search", '|'
11   end
12
13   def refine_search
14     text = BufferManager.ask :search, "refine query: ", (@query[:text] + " ")
15     return unless text && text !~ /^\s*$/
16     SearchResultsMode.spawn_from_query text
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       query = Index.parse_query(text)
28       return unless query
29       short_text = text.length < 20 ? text : text[0 ... 20] + "..."
30       mode = SearchResultsMode.new query
31       BufferManager.spawn "search: \"#{short_text}\"", mode
32       mode.load_threads :num => mode.buffer.content_height
33     rescue Index::ParseError => e
34       BufferManager.flash "Problem: #{e.message}!"
35     end
36   end
37 end
38
39 end