From 40be99af5add5ff149933558c75c6aa63a9b2205 Mon Sep 17 00:00:00 2001
From: Damien Leone <damien.leone@fensalir.fr>
Date: Wed, 9 Jun 2010 12:57:18 +0200
Subject: [PATCH 3/3] edit-mode: change the way signatures are handled

The current signatures handling was not suitable for account changing
in edit-mode. It was working fine when the edit_signature option was
false, but it could have a better behaviour if this option was
enabled.

This commit tries to do this by appending the signature to the body
text if the edit_signature is true, then after editing the message it
checks if the signature has been modified by comparing the end of the
file to the current account's signature. If it has been edited then we
stick to it by setting @sig_edited to true, otherwise the signature
will still be automatically changed if another account is selected.
---
 lib/sup/modes/edit-message-mode.rb |   19 +++++++++++++++++--
 1 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/lib/sup/modes/edit-message-mode.rb b/lib/sup/modes/edit-message-mode.rb
index f9e37b0..00d6ba9 100644
--- a/lib/sup/modes/edit-message-mode.rb
+++ b/lib/sup/modes/edit-message-mode.rb
@@ -81,7 +81,7 @@ EOS
     @header_lines = []
 
     @body = opts.delete(:body) || []
-    @body += sig_lines if $config[:edit_signature] && !opts.delete(:have_signature)
+    @sig_edited = false
 
     if opts[:attachments]
       @attachments = opts[:attachments].values
@@ -167,12 +167,14 @@ EOS
   def edit_subject; edit_field "Subject" end
 
   def edit_message
+    sig = sig_lines.join("\n")
     old_from = @header["From"] if @account_selector
 
     @file = Tempfile.new "sup.#{self.class.name.gsub(/.*::/, '').camel_to_hyphy}"
     @file.puts format_headers(@header - NON_EDITABLE_HEADERS).first
     @file.puts
     @file.puts @body.join("\n")
+    @file.puts sig if ($config[:edit_signature] and !@sig_edited)
     @file.close
 
     editor = $config[:editor] || ENV['EDITOR'] || "/usr/bin/vi"
@@ -186,6 +188,19 @@ EOS
     header, @body = parse_file @file.path
     @header = header - NON_EDITABLE_HEADERS
 
+    if $config[:edit_signature]
+      pbody = @body.join("\n")
+      blen = pbody.length
+      slen = sig.length
+
+      if blen > slen and pbody[blen-slen..blen] == sig
+        @sig_edited = false
+        @body = pbody[0..blen-slen].split("\n")
+      else
+        @sig_edited = true
+      end
+    end
+
     if @account_selector and @header["From"] != old_from
       @account_user = @header["From"]
       @account_selector.set_to nil
@@ -289,7 +304,7 @@ protected
   def regen_text
     header, @header_lines = format_headers(@header - NON_EDITABLE_HEADERS) + [""]
     @text = header + [""] + @body
-    @text += sig_lines unless $config[:edit_signature]
+    @text += sig_lines unless @sig_edited
     
     @attachment_lines_offset = 0
 
-- 
1.7.1

