commit e473648de1a8fad4e66906f1b487a753686570c5
parent 29985bd2d93a9bdc3a28209363a11739f78266e6
Author: Gaute Hope <eg@gaute.vetsj.com>
Date: Sat, 3 Aug 2013 13:05:34 +0200
handle utf-8 completions and r/w encodings for labels and contacts
Diffstat:
5 files changed, 24 insertions(+), 6 deletions(-)
diff --git 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
@@ -1,3 +1,5 @@
+# encoding: utf-8
+
require 'etc'
require 'thread'
@@ -466,7 +468,7 @@ EOS
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 @@ EOS
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 @@ EOS
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 @@ EOS
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
@@ -1,3 +1,5 @@
+# encoding: utf-8
+
module Redwood
class ContactManager
@@ -54,7 +56,7 @@ class ContactManager
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
@@ -1,3 +1,5 @@
+# encoding: utf-8
+
module Redwood
class LabelManager
@@ -77,7 +79,7 @@ class LabelManager
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
@@ -367,6 +367,15 @@ class String
#
# 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