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