]> 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 c4e59b60d426db7e7a3d522059ce73607399bf16..3d38aa0fd51adfb9a6ad3bd12322b575f5a7d931 100644 (file)
--- a/bin/sup
+++ b/bin/sup
@@ -8,19 +8,6 @@ Thread.abort_on_exception = true # make debugging possible
 
 module Redwood
 
-$exception = nil
-def reporting_thread
-  ::Thread.new do
-    begin
-      yield
-    rescue Exception => e
-      $exception ||= e
-      raise
-    end
-  end
-end
-module_function :reporting_thread
-
 global_keymap = Keymap.new do |k|
   k.add :quit, "Quit Redwood", 'q'
   k.add :help, "Show help", 'H', '?'
@@ -34,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
@@ -53,9 +41,7 @@ end
 module_function :start_cursing, :stop_cursing
 
 Redwood::start
-
 Index.new.load
-log "loaded #{Index.size} messages from index"
 
 if(s = Index.source_for DraftManager.source_name)
   DraftManager.source = s
@@ -85,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
@@ -114,9 +102,18 @@ 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 } }
 
-  reporting_thread { sleep 3; PollManager.poll }
   PollManager.start_thread
 
   until $exception
@@ -131,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 }
@@ -140,11 +137,12 @@ begin
         when :roll_buffers_backwards
           bm.roll_buffers_backwards
         when :kill_buffer
-          bm.kill_buffer bm.focus_buf if bm.focus_buf.mode.killable?
+          bm.kill_buffer_safely bm.focus_buf
         when :list_buffers
           bm.spawn_unless_exists("Buffer List") { BufferListMode.new }
         when :list_contacts
-          bm.spawn_unless_exists("Contact List") { ContactListMode.new }
+          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*$/
@@ -155,11 +153,10 @@ begin
             log "built query from #{text.inspect}: #{qobj}"
             mode = SearchResultsMode.new qobj
             bm.spawn "search: \"#{short_text}\"", mode
-            mode.load_more_threads mode.buffer.content_height
+            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
@@ -170,6 +167,22 @@ begin
         when :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
@@ -179,40 +192,29 @@ begin
       end
     end
   end
-  bm.kill_all_buffers
 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
-mail 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
@@ -221,7 +223,6 @@ William
 The problem was: #{$exception.message} (error type #{$exception.class.name})
 A backtrace follows:
 EOS
-  end
   raise $exception
 end