]> git.cworth.org Git - sup/blob - lib/sup/modes/inbox-mode.rb
Add a refine_search command to InboxMode
[sup] / lib / sup / modes / inbox-mode.rb
1 require 'sup'
2
3 module Redwood
4
5 class InboxMode < ThreadIndexMode
6   register_keymap do |k|
7     ## overwrite toggle_archived with archive
8     k.add :archive, "Archive thread (remove from inbox)", 'a'
9     k.add :read_and_archive, "Archive thread (remove from inbox) and mark read", 'A'
10     k.add :refine_search, "Refine search", '|'
11   end
12
13   def initialize
14     super [:inbox, :sent, :draft], { :label => :inbox, :skip_killed => true }
15     raise "can't have more than one!" if defined? @@instance
16     @@instance = self
17   end
18
19   def is_relevant? m; (m.labels & [:spam, :deleted, :killed, :inbox]) == Set.new([:inbox]) end
20
21   def refine_search
22     text = BufferManager.ask :search, "refine query: ", "label:inbox AND "
23     return unless text && text !~ /^\s*$/
24     SearchResultsMode.spawn_from_query text
25   end
26
27   ## label-list-mode wants to be able to raise us if the user selects
28   ## the "inbox" label, so we need to keep our singletonness around
29   def self.instance; @@instance; end
30   def killable?; false; end
31
32   def archive
33     return unless cursor_thread
34     thread = cursor_thread # to make sure lambda only knows about 'old' cursor_thread
35
36     UndoManager.register "archiving thread" do
37       thread.apply_label :inbox
38       add_or_unhide thread.first
39     end
40
41     cursor_thread.remove_label :inbox
42     hide_thread cursor_thread
43     regen_text
44   end
45
46   def multi_archive threads
47     UndoManager.register "archiving #{threads.size.pluralize 'thread'}" do
48       threads.map do |t|
49         t.apply_label :inbox
50         add_or_unhide t.first
51       end
52       regen_text
53     end
54
55     threads.each do |t|
56       t.remove_label :inbox
57       hide_thread t
58     end
59     regen_text
60   end
61
62   def read_and_archive
63     return unless cursor_thread
64     thread = cursor_thread # to make sure lambda only knows about 'old' cursor_thread
65
66     UndoManager.register "reading and archiving thread" do
67       thread.apply_label :inbox
68       thread.apply_label :unread
69       add_or_unhide thread.first
70     end
71
72     cursor_thread.remove_label :unread
73     cursor_thread.remove_label :inbox
74     hide_thread cursor_thread
75     regen_text
76   end
77
78   def multi_read_and_archive threads
79     old_labels = threads.map { |t| t.labels.dup }
80
81     threads.each do |t|
82       t.remove_label :unread
83       t.remove_label :inbox
84       hide_thread t
85     end
86     regen_text
87
88     UndoManager.register "reading and archiving #{threads.size.pluralize 'thread'}" do
89       threads.zip(old_labels).each do |t, l|
90         t.labels = l
91         add_or_unhide t.first
92       end
93       regen_text
94     end
95
96   end
97
98   def handle_unarchived_update sender, m
99     add_or_unhide m
100   end
101
102   def handle_archived_update sender, m
103     t = thread_containing(m) or return
104     hide_thread t
105     regen_text
106   end
107
108   def status
109     super + "    #{Index.size} messages in index"
110   end
111 end
112
113 end