]> git.cworth.org Git - sup/blobdiff - bin/sup
ask when quitting with unsaved buffers
[sup] / bin / sup
diff --git a/bin/sup b/bin/sup
index f8e50b7a591697d6f22e03350ea5e73891e76857..3d38aa0fd51adfb9a6ad3bd12322b575f5a7d931 100644 (file)
--- a/bin/sup
+++ b/bin/sup
@@ -4,9 +4,9 @@ require 'rubygems'
 require 'ncurses'
 require "sup"
 
-module Redwood
+Thread.abort_on_exception = true # make debugging possible
 
-$exception = nil
+module Redwood
 
 global_keymap = Keymap.new do |k|
   k.add :quit, "Quit Redwood", 'q'
@@ -21,6 +21,7 @@ global_keymap = Keymap.new do |k|
   k.add :list_labels, "List labels", 'L'
   k.add :poll, "Poll for new messages", 'P'
   k.add :compose, "Compose new message", 'm'
+  k.add :recall_draft, "Edit most recent draft message", 'R'
 end
 
 def start_cursing
@@ -39,16 +40,8 @@ def stop_cursing
 end
 module_function :start_cursing, :stop_cursing
 
-Redwood::SentManager.new Redwood::SENT_FN
-Redwood::ContactManager.new Redwood::CONTACT_FN
-Redwood::LabelManager.new Redwood::LABEL_FN
-Redwood::AccountManager.new $config[:accounts]
-Redwood::DraftManager.new Redwood::DRAFT_DIR
-Redwood::UpdateManager.new
-Redwood::PollManager.new
-
+Redwood::start
 Index.new.load
-log "loaded #{Index.size} messages from index"
 
 if(s = Index.source_for DraftManager.source_name)
   DraftManager.source = s
@@ -61,7 +54,7 @@ if(s = Index.source_for SentManager.source_name)
 else
   Index.add_source SentManager.new_source
 end
-  
+
 begin
   log "starting curses"
   start_cursing
@@ -78,6 +71,8 @@ begin
     c.add :twiddle_color, Ncurses::COLOR_BLUE, Ncurses::COLOR_BLACK
     c.add :label_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
     c.add :message_patina_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_GREEN
+    c.add :alternate_patina_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_BLUE
+    c.add :missing_message_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_RED
     c.add :mime_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
     c.add :quote_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
     c.add :sig_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
@@ -107,9 +102,19 @@ begin
   Logger.make_buf
 
   bm.draw_screen
-  imode.load_more_threads ibuf.content_height
+  Index.usual_sources.each do |s|
+    reporting_thread do
+      begin
+        s.connect
+      rescue SourceError => e
+        Redwood::log "Fatal error loading from #{s}: #{e.message}"
+      end
+    end if s.respond_to? :connect
+  end
+
+  imode.load_threads :num => ibuf.content_height, :when_done => lambda { reporting_thread { sleep 1; PollManager.poll } }
 
-  ::Thread.new { sleep 3; PollManager.poll }
+  PollManager.start_thread
 
   until $exception
     bm.draw_screen
@@ -123,7 +128,7 @@ begin
         x = global_keymap.action_for c
         case x
         when :quit
-          break
+          break if bm.kill_all_buffers_safely
         when :help
           curmode = bm.focus_buf.mode
           bm.spawn_unless_exists("<help for #{curmode.name}>") { HelpMode.new curmode, global_keymap }
@@ -132,79 +137,84 @@ begin
         when :roll_buffers_backwards
           bm.roll_buffers_backwards
         when :kill_buffer
-          bm.kill_buffer bm.focus_buf unless bm.focus_buf.mode.is_a? InboxMode
+          bm.kill_buffer_safely bm.focus_buf
         when :list_buffers
           bm.spawn_unless_exists("Buffer List") { BufferListMode.new }
         when :list_contacts
-          mode = ContactListMode.new 
-          bm.spawn "compose to contacts", mode
+          b = bm.spawn_unless_exists("Contact List") { ContactListMode.new }
+          b.mode.load_in_background
         when :search
           text = bm.ask :search, "query: "
           next unless text && text !~ /^\s*$/
-          mode = SearchResultsMode.new text
-          short_text = 
-            if text.length < 20
-              text
-            else
-              text[0 ... 20] + "..."
-            end
-          bm.spawn "search: \"#{short_text}\"", mode
-          bm.draw_screen
-          mode.load_more_threads mode.buffer.content_height
-        when :list_labels
-          b = BufferManager.spawn_unless_exists("all labels") do
-            LabelListMode.new
+
+          begin
+            qobj = Index.parse_user_query_string text
+            short_text = text.length < 20 ? text : text[0 ... 20] + "..."
+            log "built query from #{text.inspect}: #{qobj}"
+            mode = SearchResultsMode.new qobj
+            bm.spawn "search: \"#{short_text}\"", mode
+            mode.load_threads :num => mode.buffer.content_height
+          rescue Ferret::QueryParser::QueryParseException => e
+            bm.flash "Couldn't parse query."
           end
+        when :list_labels
+          b = bm.spawn_unless_exists("Label List") { LabelListMode.new }
           b.mode.load_in_background
         when :compose
           mode = ComposeMode.new
-          bm.spawn "new message", mode
+          bm.spawn "New Message", mode
           mode.edit
         when :poll
-          BufferManager.raise_to_front PollManager.buffer
-          PollManager.poll
+          bm.raise_to_front PollManager.buffer
+          reporting_thread { PollManager.poll }
+        when :recall_draft
+          case Index.num_results_for :label => :draft
+          when 0
+            bm.flash "No draft messages."
+          when 1
+            m = nil
+            Index.each_id_by_date(:label => :draft) { |mid, builder| m = builder.call }
+            r = ResumeMode.new(m)
+            BufferManager.spawn "Edit message", r
+            r.edit
+          else
+            b = BufferManager.spawn_unless_exists(:draft) do
+              mode = LabelSearchResultsMode.new [:draft]
+            end
+            b.mode.load_threads :num => b.content_height
+          end
         when :nothing
         when :redraw
           bm.completely_redraw_screen
         else
-          BufferManager.flash "Unknown key press '#{c.to_character}' for #{bm.focus_buf.mode.name}."
+          bm.flash "Unknown key press '#{c.to_character}' for #{bm.focus_buf.mode.name}."
         end
       end
     end
   end
-  bm.kill_all_buffers
-  Redwood::LabelManager.save
-  Redwood::ContactManager.save
 rescue Exception => e
   $exception ||= e
 ensure
+  Redwood::finish
   stop_cursing
+  
+  # don't ask me why, but sometimes it's necessary to print something
+  # to stderr at this point or the exception doesn't get printed.
+  # doesn't get printed. WHY?
+
+  $stderr.puts " " 
 end
 
 Index.save unless $exception # TODO: think about this
 
 if $exception 
-  case $exception
-  when IndexError
-    $stderr.puts <<EOS
-An error occurred while parsing a message from source "#{$exception.source}".
-Typically, this means that the source has been modified in some
-way which has rendered the messages invalid. For example, if it's an mbox
-file, you may have read or deleted messages using another client.
-
-You must rebuild the index for this source. Please run:
-  sup-import --rebuild #{$exception.source}
-to correct this error.
-EOS
-#' stupid ruby-mode
-  else
-    $stderr.puts <<EOS
+  $stderr.puts <<EOS
 ----------------------------------------------------------------
 I'm very sorry, but it seems that an error occurred in Sup. 
 Please accept my sincere apologies. If you don't mind, please
 send the backtrace below and a brief report of the circumstances
-to user wmorgan-sup at site masanjin dot net so that I might
-address this problem. Thank you!
+to wmorgan-sup at masanjin dot nets so that I might address this
+problem. Thank you!
 
 Sincerely,
 William
@@ -213,7 +223,6 @@ William
 The problem was: #{$exception.message} (error type #{$exception.class.name})
 A backtrace follows:
 EOS
-  end
   raise $exception
 end