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