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