+require 'set'
+
module Redwood
## subclasses should implement:
k.add :save, "Save changes now", '$'
k.add :jump_to_next_new, "Jump to next new thread", :tab
k.add :reply, "Reply to latest message in a thread", 'r'
+ k.add :reply_all, "Reply to all participants of the latest message in a thread", 'G'
k.add :forward, "Forward latest message in a thread", 'f'
k.add :toggle_tagged, "Tag/untag selected thread", 't'
k.add :toggle_tagged_all, "Tag/untag all threads", 'T'
k.add :tag_matching, "Tag matching threads", 'g'
- k.add :apply_to_tagged, "Apply next command to all tagged threads", ';'
+ k.add :apply_to_tagged, "Apply next command to all tagged threads", '+', '='
k.add :join_threads, "Force tagged threads to be joined into the same thread", '#'
k.add :undo, "Undo the previous action", 'u'
end
UpdateManager.register self
+ @save_thread_mutex = Mutex.new
+
@last_load_more_size = nil
to_load_more do |size|
next if @last_load_more_size == 0
end
end
+ def unsaved?; dirty? end
def lines; @text.length; end
def [] i; @text[i]; end
def contains_thread? t; @threads.include?(t) end
## let's see you do THIS in python
@threads = @ts.threads.select { |t| !@hidden_threads[t] }.sort_by { |t| [t.date, t.first.id] }.reverse
@size_widgets = @threads.map { |t| size_widget_for_thread t }
- @size_widget_width = @size_widgets.max_of { |w| w.length }
+ @size_widget_width = @size_widgets.max_of { |w| w.display_length }
end
regen_text
BufferManager.flash "#{threads.size.pluralize 'thread'} killed."
end
- def save
- dirty_threads = @mutex.synchronize { (@threads + @hidden_threads.keys).select { |t| t.dirty? } }
- return if dirty_threads.empty?
+ def save background=true
+ if background
+ Redwood::reporting_thread("saving thread") { actually_save }
+ else
+ actually_save
+ end
+ end
+
+ def actually_save
+ @save_thread_mutex.synchronize do
+ BufferManager.say("Saving contacts...") { ContactManager.instance.save }
+ dirty_threads = @mutex.synchronize { (@threads + @hidden_threads.keys).select { |t| t.dirty? } }
+ next if dirty_threads.empty?
- BufferManager.say("Saving threads...") do |say_id|
- dirty_threads.each_with_index do |t, i|
- BufferManager.say "Saving modified thread #{i + 1} of #{dirty_threads.length}...", say_id
- t.save Index
+ BufferManager.say("Saving threads...") do |say_id|
+ dirty_threads.each_with_index do |t, i|
+ BufferManager.say "Saving modified thread #{i + 1} of #{dirty_threads.length}...", say_id
+ t.save_state Index
+ end
end
end
end
sleep 0.1 # TODO: necessary?
BufferManager.erase_flash
end
- save
+ save false
super
end
end
def tag_matching
- query = BufferManager.ask :search, "tag threads matching: "
+ query = BufferManager.ask :search, "tag threads matching (regex): "
return if query.nil? || query.empty?
- query = /#{query}/i
+ query = begin
+ /#{query}/i
+ rescue RegexpError => e
+ BufferManager.flash "error interpreting '#{query}': #{e.message}"
+ return
+ end
@mutex.synchronize { @threads.each { |t| @tags.tag t if thread_matches?(t, query) } }
regen_text
end
keepl, modifyl = thread.labels.partition { |t| speciall.member? t }
user_labels = BufferManager.ask_for_labels :label, "Labels for thread: ", modifyl, @hidden_labels
-
return unless user_labels
- thread.labels = keepl + user_labels
+
+ thread.labels = Set.new(keepl) + user_labels
user_labels.each { |l| LabelManager << l }
update_text_for_line curpos
- UndoManager.register "labeling thread #{thread.first.id}" do
+ UndoManager.register "labeling thread" do
thread.labels = old_labels
update_text_for_line pos
UpdateManager.relay self, :labeled, thread.first
end
def multi_edit_labels threads
- user_labels = BufferManager.ask_for_labels :add_labels, "Add labels: ", [], @hidden_labels
+ user_labels = BufferManager.ask_for_labels :labels, "Add/remove labels (use -label to remove): ", [], @hidden_labels
return unless user_labels
-
- hl = user_labels.select { |l| @hidden_labels.member? l }
+
+ user_labels.map! { |l| (l.to_s =~ /^-/)? [l.to_s.gsub(/^-?/, '').to_sym, true] : [l, false] }
+ hl = user_labels.select { |(l,_)| @hidden_labels.member? l }
unless hl.empty?
BufferManager.flash "'#{hl}' is a reserved label!"
return
old_labels = threads.map { |t| t.labels.dup }
threads.each do |t|
- user_labels.each do |l|
- t.apply_label l
- LabelManager << l
- UpdateManager.relay self, :labeled, t.first
+ user_labels.each do |(l, to_remove)|
+ if to_remove
+ t.remove_label l
+ else
+ t.apply_label l
+ LabelManager << l
+ end
end
+ UpdateManager.relay self, :labeled, t.first
end
regen_text
end
end
- def reply
+ def reply type_arg=nil
t = cursor_thread or return
m = t.latest_message
return if m.nil? # probably won't happen
m.load_from_source!
- mode = ReplyMode.new m
+ mode = ReplyMode.new m, type_arg
BufferManager.spawn "Reply to #{m.subj}", mode
end
+ def reply_all; reply :all; end
+
def forward
t = cursor_thread or return
m = t.latest_message
def add_or_unhide m
@ts_mutex.synchronize do
if (is_relevant?(m) || @ts.is_relevant?(m)) && !@ts.contains?(m)
- @ts.load_thread_for_message m
+ @ts.load_thread_for_message m, @load_thread_opts
end
@hidden_threads.delete @ts.thread_for(m)
def authors; map { |m, *o| m.from if m }.compact.uniq; end
- def author_names_and_newness_for_thread t
+ def author_names_and_newness_for_thread t, limit=nil
new = {}
- authors = t.map do |m, *o|
+ authors = Set.new
+ t.each do |m, *o|
next unless m
+ break if limit and authors.size >= limit
name =
if AccountManager.is_account?(m.from)
end
new[name] ||= m.has_label?(:unread)
- name
+ authors << name
end
- authors.compact.uniq.map { |a| [a, new[a]] }
+ authors.to_a.map { |a| [a, new[a]] }
end
+ AUTHOR_LIMIT = 5
def text_for_thread_at line
t, size_widget = @mutex.synchronize { [@threads[line], @size_widgets[line]] }
## format the from column
cur_width = 0
- ann = author_names_and_newness_for_thread t
+ ann = author_names_and_newness_for_thread t, AUTHOR_LIMIT
from = []
ann.each_with_index do |(name, newness), i|
break if cur_width >= from_width
last = i == ann.length - 1
abbrev =
- if cur_width + name.length > from_width
+ if cur_width + name.display_length > from_width
name[0 ... (from_width - cur_width - 1)] + "."
- elsif cur_width + name.length == from_width
+ elsif cur_width + name.display_length == from_width
name[0 ... (from_width - cur_width)]
else
if last
end
end
- cur_width += abbrev.length
+ cur_width += abbrev.display_length
if last && from_width > cur_width
abbrev += " " * (from_width - cur_width)
[subj_color, size_widget_text],
[:to_me_color, t.labels.member?(:attachment) ? "@" : " "],
[:to_me_color, dp ? ">" : (p ? '+' : " ")],
- [subj_color, t.subj + (t.subj.empty? ? "" : " ")],
] +
- (t.labels - @hidden_labels).map { |label| [:label_color, "+#{label} "] } +
- [[:snippet_color, snippet]
+ (t.labels - @hidden_labels).map { |label| [:label_color, "#{label} "] } +
+ [
+ [subj_color, t.subj + (t.subj.empty? ? "" : " ")],
+ [:snippet_color, snippet],
]
-
end
def dirty?; @mutex.synchronize { (@hidden_threads.keys + @threads).any? { |t| t.dirty? } } end