From e473648de1a8fad4e66906f1b487a753686570c5 Mon Sep 17 00:00:00 2001 From: Gaute Hope Date: Sat, 3 Aug 2013 13:05:34 +0200 Subject: [PATCH 1/3] handle utf-8 completions and r/w encodings for labels and contacts --- bin/sup | 4 +++- lib/sup/buffer.rb | 9 ++++++--- lib/sup/contact.rb | 4 +++- lib/sup/label.rb | 4 +++- lib/sup/util.rb | 9 +++++++++ 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/bin/sup b/bin/sup index 9bbf85dc2..73b570d5d 100755 --- a/bin/sup +++ b/bin/sup @@ -1,3 +1,5 @@ +# encoding: utf-8 + #!/usr/bin/env ruby $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib]) @@ -416,7 +418,7 @@ unless Redwood::exceptions.empty? We are very sorry. It seems that an error occurred in Sup. Please accept our sincere apologies. Please submit the contents of #{BASE_DIR}/exception-log.txt and a brief report of the -circumstances to https://github.com/sup-heliotrope/sup/issues so that +circumstances to https://github.com/sup-heliotrope/sup/issues so that we might address this problem. Thank you! Sincerely, diff --git a/lib/sup/buffer.rb b/lib/sup/buffer.rb index 50e295c00..8e0b04890 100644 --- a/lib/sup/buffer.rb +++ b/lib/sup/buffer.rb @@ -1,3 +1,5 @@ +# encoding: utf-8 + require 'etc' require 'thread' @@ -466,7 +468,7 @@ def ask_many_with_completions domain, question, completions, default=nil prefix.fix_encoding target.fix_encoding - completions.select { |x| x =~ /^#{Regexp::escape target}/i }.map { |x| [prefix + x, x] } + completions.select { |x| x =~ /^#{Regexp::escape target}/iu }.map { |x| [prefix + x, x] } end end @@ -479,7 +481,7 @@ def ask_many_emails_with_completions domain, question, completions, default=nil prefix = prefix.join(", ") + (prefix.empty? ? "" : ", ") prefix.fix_encoding - completions.select { |x| x =~ /^#{Regexp::escape target}/i }.sort_by { |c| [ContactManager.contact_for(c) ? 0 : 1, c] }.map { |x| [prefix + x, x] } + completions.select { |x| x =~ /^#{Regexp::escape target}/iu }.sort_by { |c| [ContactManager.contact_for(c) ? 0 : 1, c] }.map { |x| [prefix + x, x] } end end @@ -492,7 +494,7 @@ def ask_for_filename domain, question, default=nil, allow_directory=false if dir [[s.sub(full, dir), "~#{name}"]] else - users.select { |u| u =~ /^#{Regexp::escape name}/ }.map do |u| + users.select { |u| u =~ /^#{Regexp::escape name}/u }.map do |u| [s.sub("~#{name}", "~#{u}"), "~#{u}"] end end @@ -551,6 +553,7 @@ def ask_for_contacts domain, question, default_contacts=[] completions = (recent + contacts).flatten.uniq completions += HookManager.run("extra-contact-addresses") || [] + answer = BufferManager.ask_many_emails_with_completions domain, question, completions, default if answer diff --git a/lib/sup/contact.rb b/lib/sup/contact.rb index 1f61a1e73..82c059edb 100644 --- a/lib/sup/contact.rb +++ b/lib/sup/contact.rb @@ -1,3 +1,5 @@ +# encoding: utf-8 + module Redwood class ContactManager @@ -54,7 +56,7 @@ def person_for email; @e2p[email] end def is_aliased_contact? person; !@p2a[person].nil? end def save - File.open(@fn, "w") do |f| + File.open(@fn, "w:UTF-8") do |f| @p2a.sort_by { |(p, a)| [p.full_address, a] }.each do |(p, a)| f.puts "#{a || ''}: #{p.full_address}" end diff --git a/lib/sup/label.rb b/lib/sup/label.rb index 1699896d7..24b7dc45b 100644 --- a/lib/sup/label.rb +++ b/lib/sup/label.rb @@ -1,3 +1,5 @@ +# encoding: utf-8 + module Redwood class LabelManager @@ -77,7 +79,7 @@ def delete t def save return unless @modified - File.open(@fn, "w") { |f| f.puts @labels.keys.sort_by { |l| l.to_s } } + File.open(@fn, "w:UTF-8") { |f| f.puts @labels.keys.sort_by { |l| l.to_s } } @new_labels = {} end end diff --git a/lib/sup/util.rb b/lib/sup/util.rb index a06c572ae..bf33dcff2 100644 --- a/lib/sup/util.rb +++ b/lib/sup/util.rb @@ -367,6 +367,15 @@ def wrap len # # Not Ruby 1.8 compatible def fix_encoding + # first try to set the string to utf-8 and check if it is valid + # in case ruby just thinks it is something else + orig_encoding = encoding + force_encoding 'UTF-8' + return self if valid_encoding? + + # that didn't work, go back to original and try to convert + force_encoding orig_encoding + encode!('UTF-8', :invalid => :replace, :undef => :replace) # do this anyway in case string is set to be UTF-8, encoding to From c6bd0a6f9defffc1eda9c2e58131efd1be50a45b Mon Sep 17 00:00:00 2001 From: Gaute Hope Date: Sat, 3 Aug 2013 13:12:07 +0200 Subject: [PATCH 2/3] encoding header for lib/sup.rb --- lib/sup.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/sup.rb b/lib/sup.rb index 4e340d8bc..ad6d405b1 100644 --- a/lib/sup.rb +++ b/lib/sup.rb @@ -1,3 +1,5 @@ +# encoding: utf-8 + require 'rubygems' require 'yaml' YAML::ENGINE.yamler = 'psych' From 9b36d1cb255f9e13516f13e2f42800b67cc75bd7 Mon Sep 17 00:00:00 2001 From: Gaute Hope Date: Sat, 3 Aug 2013 13:13:38 +0200 Subject: [PATCH 3/3] add encoding header to compose_mode.rb --- lib/sup/modes/compose_mode.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/sup/modes/compose_mode.rb b/lib/sup/modes/compose_mode.rb index cf7a9de67..7d08ac299 100644 --- a/lib/sup/modes/compose_mode.rb +++ b/lib/sup/modes/compose_mode.rb @@ -1,3 +1,5 @@ +# encoding: utf-8 + module Redwood class ComposeMode < EditMessageMode