]> git.notmuchmail.org Git - sup/blob - bin/sup
tab completion for label editing
[sup] / bin / sup
1 #!/usr/bin/env ruby
2
3 require 'rubygems'
4 require 'ncurses'
5 require 'fileutils'
6 require 'trollop'
7 require "sup"
8
9 $opts = Trollop::options do
10   version "sup v#{Redwood::VERSION}"
11   banner <<EOS
12 Sup is a curses-based email client.
13
14 Usage:
15   sup [options]
16
17 Options are:
18 EOS
19   opt :no_threads, "Turn of threading. Helps with debugging. (Necessarily disables background polling for new messages.)"
20 end
21
22 Thread.abort_on_exception = true # make debugging possible
23
24 module Redwood
25
26 global_keymap = Keymap.new do |k|
27   k.add :quit, "Quit Redwood", 'q'
28   k.add :help, "Show help", 'H', '?'
29   k.add :roll_buffers, "Switch to next buffer", 'b'
30 #  k.add :roll_buffers_backwards, "Switch to previous buffer", 'B'
31   k.add :kill_buffer, "Kill the current buffer", 'x'
32   k.add :list_buffers, "List all buffers", 'B'
33   k.add :list_contacts, "List contacts", 'C'
34   k.add :redraw, "Redraw screen", :ctrl_l
35   k.add :search, "Search messages", '/'
36   k.add :list_labels, "List labels", 'L'
37   k.add :poll, "Poll for new messages", 'P'
38   k.add :compose, "Compose new message", 'm'
39   k.add :recall_draft, "Edit most recent draft message", 'R'
40 end
41
42 def start_cursing
43   Ncurses.initscr
44   Ncurses.noecho
45   Ncurses.cbreak
46   Ncurses.stdscr.keypad 1
47   Ncurses.curs_set 0
48   Ncurses.start_color
49   $cursing = true
50 end
51
52 def stop_cursing
53   return unless $cursing
54   Ncurses.curs_set 1
55   Ncurses.echo
56   Ncurses.endwin
57 end
58 module_function :start_cursing, :stop_cursing
59
60 Index.new
61 begin
62   Index.lock
63 rescue Index::LockError => e
64   require 'highline'
65
66   h = HighLine.new
67   h.wrap_at = :auto
68   h.say Index.fancy_lock_error_message_for(e)
69
70   case h.ask("Should I ask that process to kill itself? ")
71   when /^\s*y\s*$/i
72     h.say "Ok, suggesting sepuku..."
73     FileUtils.touch Redwood::SUICIDE_FN
74     sleep SuicideManager::DELAY * 2
75     FileUtils.rm_f Redwood::SUICIDE_FN
76     h.say "Let's try that again."
77     retry
78   else
79     h.say <<EOS
80 Ok, giving up. If the process crashed and left a stale lockfile, you
81 can fix this by manually deleting #{Index.lockfile}.
82 EOS
83     exit
84   end
85 end
86
87 begin
88   Redwood::start
89   Index.load
90
91   if(s = Index.source_for DraftManager.source_name)
92     DraftManager.source = s
93   else
94     Index.add_source DraftManager.new_source
95   end
96
97   if(s = Index.source_for SentManager.source_name)
98     SentManager.source = s
99   else
100     Index.add_source SentManager.new_source
101   end
102
103   log "starting curses"
104   start_cursing
105
106   log "initializing colormap"
107   Colormap.new do |c|
108     c.add :status_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLUE, Ncurses::A_BOLD
109     c.add :index_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
110     c.add :index_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK, 
111            Ncurses::A_BOLD
112     c.add :index_starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK, 
113            Ncurses::A_BOLD
114     c.add :labellist_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
115     c.add :labellist_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK, 
116            Ncurses::A_BOLD
117     c.add :twiddle_color, Ncurses::COLOR_BLUE, Ncurses::COLOR_BLACK
118     c.add :label_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
119     c.add :message_patina_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_GREEN
120     c.add :alternate_patina_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_BLUE
121     c.add :missing_message_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_RED
122     c.add :attachment_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
123     c.add :quote_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
124     c.add :sig_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
125     c.add :quote_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
126     c.add :sig_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
127     c.add :to_me_color, Ncurses::COLOR_GREEN, Ncurses::COLOR_BLACK
128     c.add :starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
129           Ncurses::A_BOLD
130     c.add :starred_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_GREEN,
131           Ncurses::A_BOLD
132     c.add :alternate_starred_patina_color, Ncurses::COLOR_YELLOW,
133           Ncurses::COLOR_BLUE, Ncurses::A_BOLD
134     c.add :snippet_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
135     c.add :option_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
136     c.add :tagged_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
137           Ncurses::A_BOLD
138     c.add :draft_notification_color, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK,
139           Ncurses::A_BOLD
140     c.add :completion_character_color, Ncurses::COLOR_WHITE,
141           Ncurses::COLOR_BLACK, Ncurses::A_BOLD
142   end
143
144   log "initializing buffer manager"
145   bm = BufferManager.new
146
147   log "initializing mail index buffer"
148   imode = InboxMode.new
149   ibuf = bm.spawn "Inbox", imode
150
151   log "ready for interaction!"
152   Logger.make_buf
153
154   bm.draw_screen
155
156   begin
157     Index.usual_sources.each { |s| s.check }
158   rescue SourceError
159     # do nothing! we'll report it at the next step
160   end
161   Redwood::report_broken_sources
162   
163   Index.usual_sources.each do |s|
164     reporting_thread do
165       begin
166         s.connect
167       rescue SourceError => e
168         Redwood::log "fatal error loading from #{s}: #{e.message}"
169       end
170     end if s.respond_to? :connect
171   end
172
173   imode.load_threads :num => ibuf.content_height, :when_done => lambda { reporting_thread { sleep 1; PollManager.poll } unless $opts[:no_threads] }
174
175   unless $opts[:no_threads]
176     PollManager.start
177     SuicideManager.start
178     Index.start_lock_update_thread
179   end
180
181   until $exception || SuicideManager.die?
182     c = Ncurses.nonblocking_getch
183     next unless c
184     bm.erase_flash
185
186     unless bm.handle_input(c)
187       x = global_keymap.action_for c
188       case x
189       when :quit
190         break if bm.kill_all_buffers_safely
191       when :help
192         curmode = bm.focus_buf.mode
193         bm.spawn_unless_exists("<help for #{curmode.name}>") { HelpMode.new curmode, global_keymap }
194       when :roll_buffers
195         bm.roll_buffers
196       when :roll_buffers_backwards
197         bm.roll_buffers_backwards
198       when :kill_buffer
199         bm.kill_buffer_safely bm.focus_buf
200       when :list_buffers
201         bm.spawn_unless_exists("Buffer List") { BufferListMode.new }
202       when :list_contacts
203         b = bm.spawn_unless_exists("Contact List") { ContactListMode.new }
204         b.mode.load_in_background
205       when :search
206         text = bm.ask :search, "query: "
207         next unless text && text !~ /^\s*$/
208
209         begin
210           qobj = Index.parse_user_query_string text
211           short_text = text.length < 20 ? text : text[0 ... 20] + "..."
212           log "built query from #{text.inspect}: #{qobj}"
213           mode = SearchResultsMode.new qobj
214           bm.spawn "search: \"#{short_text}\"", mode
215           mode.load_threads :num => mode.buffer.content_height
216         rescue Ferret::QueryParser::QueryParseException => e
217           bm.flash "Couldn't parse query."
218         end
219       when :list_labels
220         labels = LabelManager.listable_labels.map { |l| LabelManager.string_for l }
221         user_label = bm.ask_with_completions :label, "Show threads with label (enter for listing): ", labels
222         user_label = bm.spawn_modal("Label list", LabelListMode.new) if user_label && user_label.empty?
223         
224         label = LabelManager.label_for user_label if user_label
225         case label
226         when nil
227         when :inbox
228           BufferManager.raise_to_front InboxMode.instance.buffer
229         else
230           b = BufferManager.spawn_unless_exists("All threads with label '#{user_label}'") do
231             mode = LabelSearchResultsMode.new([label])
232           end
233           b.mode.load_threads :num => b.content_height
234         end
235
236       when :compose
237         mode = ComposeMode.new
238         bm.spawn "New Message", mode
239         mode.edit
240       when :poll
241         #          bm.raise_to_front PollManager.buffer
242         reporting_thread { PollManager.poll }
243       when :recall_draft
244         case Index.num_results_for :label => :draft
245         when 0
246           bm.flash "No draft messages."
247         when 1
248           m = nil
249           Index.each_id_by_date(:label => :draft) { |mid, builder| m = builder.call }
250           r = ResumeMode.new(m)
251           BufferManager.spawn "Edit message", r
252           r.edit
253         else
254           b = BufferManager.spawn_unless_exists("All drafts") do
255             mode = LabelSearchResultsMode.new [:draft]
256           end
257           b.mode.load_threads :num => b.content_height
258         end
259       when :nothing
260       when :redraw
261         bm.completely_redraw_screen
262       else
263         bm.flash "Unknown key press '#{c.to_character}' for #{bm.focus_buf.mode.name}."
264       end
265     end
266
267     bm.draw_screen
268   end
269 rescue Exception => e
270   $exception ||= e
271 ensure
272   unless $opts[:no_threads]
273     PollManager.stop if PollManager.instantiated?
274     SuicideManager.stop if PollManager.instantiated?
275     Index.stop_lock_update_thread
276   end
277
278   Redwood::finish
279   stop_cursing
280   Redwood::log "stopped cursing"
281
282   if SuicideManager.instantiated? && SuicideManager.die?
283     Redwood::log "I've been ordered to commit sepuku. I obey!"
284   end
285
286   case $exception
287   when nil
288     Redwood::log "no fatal errors. good job, william."
289     Index.save
290   else
291     Redwood::log "oh crap, an exception"
292   end
293
294   Index.unlock
295 end
296
297 if $exception 
298   $stderr.puts <<EOS
299 ----------------------------------------------------------------
300 I'm very sorry, but it seems that an error occurred in Sup. 
301 Please accept my sincere apologies. If you don't mind, please
302 send the backtrace below and a brief report of the circumstances
303 to sup-talk at rubyforge dot orgs so that I might address this
304 problem. Thank you!
305
306 Sincerely,
307 William
308 ----------------------------------------------------------------
309
310 The problem was: '#{$exception.message}' (error type #{$exception.class.name})
311 A backtrace follows:
312 EOS
313   raise $exception
314 end
315
316 end