diff options
| author | nagai <nagai@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-07-05 05:55:58 +0000 |
|---|---|---|
| committer | nagai <nagai@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-07-05 05:55:58 +0000 |
| commit | 69100ebb4d8fc48c985061631d09a01a2c6b2b82 (patch) | |
| tree | 4b1223c94538d16ba68ef6f1ce74491e310a1011 /ext/tk | |
| parent | 648ac9b2416c00ab70b5a1c0f39950a55c91120b (diff) | |
| download | ruby-69100ebb4d8fc48c985061631d09a01a2c6b2b82.tar.gz ruby-69100ebb4d8fc48c985061631d09a01a2c6b2b82.tar.xz ruby-69100ebb4d8fc48c985061631d09a01a2c6b2b82.zip | |
* ext/tcltklib/tcltklib.c: bug fix on treating Unicode strings.
* ext/tcltklib/tcltklib.c: add methods to treat encoding mode.
* ext/tcltklib/MANUAL.eng: add description of TclTkLib#encoding,
encoding_system, and so on.
* ext/tcltklib/MANUAL.euc: ditto.
* ext/tk/tkutil.c: fail to create a Tcl's list string from an
array including multiple kind of encoded strings.
* ext/tk/lib/tk.rb: ditto.
* ext/tk/lib/multi-tk.rb: 2nd arg of _{to|from}UTF8 is omissible.
* ext/tk/lib/remote-tk.rb: ditto.
* ext/tk/lib/tk.rb: override TclTkLib#encoding and encoding= to
use TkCore::INTERP.encoding and encoding=.
* ext/tk/lib/tk.rb: when "require 'tk'" and $KCODE=='NONE', check
DEFAULT_TK_ENCODING to decide Ruby/Tk's system encoding mode.
* ext/tk/lib/tk/encodedstr.rb: check both of Tk.encoding and
Tk.encoding_system. Tk.encoding has higher priority.
* ext/tk/lib/tk/optiondb.rb: ditto.
* ext/tk/lib/tk/spinbox.rb: ditto.
* ext/tk/lib/tk/validation.rb: ditto.
* ext/tk/lib/tk/namespace.rb: arguemnts for TclTkIp#_merge_tklist
should be UTF-8 strings.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_8@8720 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/tk')
| -rw-r--r-- | ext/tk/lib/multi-tk.rb | 8 | ||||
| -rw-r--r-- | ext/tk/lib/remote-tk.rb | 4 | ||||
| -rw-r--r-- | ext/tk/lib/tk.rb | 51 | ||||
| -rw-r--r-- | ext/tk/lib/tk/encodedstr.rb | 8 | ||||
| -rw-r--r-- | ext/tk/lib/tk/namespace.rb | 12 | ||||
| -rw-r--r-- | ext/tk/lib/tk/optiondb.rb | 2 | ||||
| -rw-r--r-- | ext/tk/lib/tk/spinbox.rb | 3 | ||||
| -rw-r--r-- | ext/tk/lib/tk/validation.rb | 3 | ||||
| -rw-r--r-- | ext/tk/tkutil.c | 183 |
9 files changed, 226 insertions, 48 deletions
diff --git a/ext/tk/lib/multi-tk.rb b/ext/tk/lib/multi-tk.rb index 6d048e91b..e8bbf67de 100644 --- a/ext/tk/lib/multi-tk.rb +++ b/ext/tk/lib/multi-tk.rb @@ -1730,11 +1730,11 @@ class << MultiTkIp __getip._invoke_with_enc(*args) end - def _toUTF8(str, encoding) + def _toUTF8(str, encoding=nil) __getip._toUTF8(str, encoding) end - def _fromUTF8(str, encoding) + def _fromUTF8(str, encoding=nil) __getip._fromUTF8(str, encoding) end @@ -2119,11 +2119,11 @@ class MultiTkIp @interp._invoke_with_enc(*args) end - def _toUTF8(str, encoding) + def _toUTF8(str, encoding=nil) @interp._toUTF8(str, encoding) end - def _fromUTF8(str, encoding) + def _fromUTF8(str, encoding=nil) @interp._fromUTF8(str, encoding) end diff --git a/ext/tk/lib/remote-tk.rb b/ext/tk/lib/remote-tk.rb index bb2a2c114..ed7f75b33 100644 --- a/ext/tk/lib/remote-tk.rb +++ b/ext/tk/lib/remote-tk.rb @@ -321,11 +321,11 @@ class RemoteTkIp _appsend(true, false, *args) end - def _toUTF8(str, encoding) + def _toUTF8(str, encoding=nil) @interp._toUTF8(str, encoding) end - def _fromUTF8(str, encoding) + def _fromUTF8(str, encoding=nil) @interp._fromUTF8(str, encoding) end diff --git a/ext/tk/lib/tk.rb b/ext/tk/lib/tk.rb index 9daee2de5..4267b42a3 100644 --- a/ext/tk/lib/tk.rb +++ b/ext/tk/lib/tk.rb @@ -309,19 +309,48 @@ if USE_TCLs_LIST_FUNCTIONS def array2tk_list(ary, enc=nil) return "" if ary.size == 0 + sys_enc = TkCore::INTERP.encoding + sys_enc = TclTkLib.encoding_system unless sys_enc + + dst_enc = (enc == nil)? sys_enc: enc + dst = ary.collect{|e| if e.kind_of? Array - array2tk_list(e, enc) + s = array2tk_list(e, enc) elsif e.kind_of? Hash tmp_ary = [] #e.each{|k,v| tmp_ary << k << v } e.each{|k,v| tmp_ary << "-#{_get_eval_string(k)}" << v } - array2tk_list(tmp_ary, enc) + s = array2tk_list(tmp_ary, enc) else - _get_eval_string(e, enc) + s = _get_eval_string(e, enc) + end + + if dst_enc != true && dst_enc != false + if (s_enc = s.instance_variable_get(:@encoding)) + s_enc = s_enc.to_s + else + s_enc = sys_enc + end + dst_enc = true if s_enc != dst_enc end + + s } - TkCore::INTERP._merge_tklist(*dst) + + if sys_enc && dst_enc + dst.map!{|s| _toUTF8(s)} + ret = TkCore::INTERP._merge_tklist(*dst) + if dst_enc.kind_of?(String) + ret = _fromUTF8(ret, dst_enc) + ret.instance_variable_set(:@encoding, dst_enc) + else + ret.instance_variable_set(:@encoding, 'utf-8') + end + ret + else + TkCore::INTERP._merge_tklist(*dst) + end end else @@ -2163,6 +2192,15 @@ if (/^(8\.[1-9]|9\.|[1-9][0-9])/ =~ Tk::TCL_VERSION && !Tk::JAPANIZED_TK) =end end + module TclTkLib + def self.encoding=(name) + TkCore::INTERP.encoding = name + end + def self.encoding + TkCore::INTERP.encoding + end + end + module Tk module Encoding extend Encoding @@ -2231,6 +2269,9 @@ if (/^(8\.[1-9]|9\.|[1-9][0-9])/ =~ Tk::TCL_VERSION && !Tk::JAPANIZED_TK) Tk.encoding = 'utf-8' Tk.encoding_system = 'utf-8' else # NONE + if defined? DEFAULT_TK_ENCODING + Tk.encoding_system = DEFAULT_TK_ENCODING + end begin Tk.encoding = Tk.encoding_system rescue StandardError, NameError @@ -4153,7 +4194,7 @@ end #Tk.freeze module Tk - RELEASE_DATE = '2005-06-24'.freeze + RELEASE_DATE = '2005-07-05'.freeze autoload :AUTO_PATH, 'tk/variable' autoload :TCL_PACKAGE_PATH, 'tk/variable' diff --git a/ext/tk/lib/tk/encodedstr.rb b/ext/tk/lib/tk/encodedstr.rb index aed0e7524..797e514a4 100644 --- a/ext/tk/lib/tk/encodedstr.rb +++ b/ext/tk/lib/tk/encodedstr.rb @@ -67,9 +67,13 @@ module Tk def initialize(str, enc = nil) super(str) + # @encoding = ( enc || + # ((self.class::Encoding)? + # self.class::Encoding : Tk.encoding_system) ) @encoding = ( enc || - ((self.class::Encoding)? - self.class::Encoding : Tk.encoding_system) ) + ((self.class::Encoding)? + self.class::Encoding : + ((Tk.encoding)? Tk.encoding : Tk.encoding_system) ) ) end attr_reader :encoding diff --git a/ext/tk/lib/tk/namespace.rb b/ext/tk/lib/tk/namespace.rb index e0ef7c39d..85a94372b 100644 --- a/ext/tk/lib/tk/namespace.rb +++ b/ext/tk/lib/tk/namespace.rb @@ -115,19 +115,19 @@ class TkNamespace < TkObject # alias __tk_call_with_enc tk_call_with_enc def tk_call(*args) #super('namespace', 'eval', @namespace, *args) - args = args.collect{|arg| (s = _get_eval_string(arg))? s: ''} + args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''} super('namespace', 'eval', @namespace, TkCore::INTERP._merge_tklist(*args)) end def tk_call_without_enc(*args) #super('namespace', 'eval', @namespace, *args) - args = args.collect{|arg| (s = _get_eval_string(arg))? s: ''} + args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''} super('namespace', 'eval', @namespace, TkCore::INTERP._merge_tklist(*args)) end def tk_call_with_enc(*args) #super('namespace', 'eval', @namespace, *args) - args = args.collect{|arg| (s = _get_eval_string(arg))? s: ''} + args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''} super('namespace', 'eval', @namespace, TkCore::INTERP._merge_tklist(*args)) end @@ -159,19 +159,19 @@ class TkNamespace < TkObject alias __tk_call_with_enc tk_call_with_enc def tk_call(*args) #super('namespace', 'eval', @fullname, *args) - args = args.collect{|arg| (s = _get_eval_string(arg))? s: ''} + args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''} super('namespace', 'eval', @fullname, TkCore::INTERP._merge_tklist(*args)) end def tk_call_without_enc(*args) #super('namespace', 'eval', @fullname, *args) - args = args.collect{|arg| (s = _get_eval_string(arg))? s: ''} + args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''} super('namespace', 'eval', @fullname, TkCore::INTERP._merge_tklist(*args)) end def tk_call_with_enc(*args) #super('namespace', 'eval', @fullname, *args) - args = args.collect{|arg| (s = _get_eval_string(arg))? s: ''} + args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''} super('namespace', 'eval', @fullname, TkCore::INTERP._merge_tklist(*args)) end diff --git a/ext/tk/lib/tk/optiondb.rb b/ext/tk/lib/tk/optiondb.rb index fe79de123..a806f3971 100644 --- a/ext/tk/lib/tk/optiondb.rb +++ b/ext/tk/lib/tk/optiondb.rb @@ -44,7 +44,7 @@ module TkOptionDB "can't call 'TkOptionDB.read_entries' on a safe interpreter" end - i_enc = Tk.encoding() + i_enc = ((Tk.encoding)? Tk.encoding : Tk.encoding_system) unless f_enc f_enc = i_enc diff --git a/ext/tk/lib/tk/spinbox.rb b/ext/tk/lib/tk/spinbox.rb index fe50d85e4..ac84e06cb 100644 --- a/ext/tk/lib/tk/spinbox.rb +++ b/ext/tk/lib/tk/spinbox.rb @@ -25,7 +25,8 @@ class TkSpinbox<TkEntry [ ?w, TkComm.method(:window) ], [ ?e, proc{|val| - enc = Tk.encoding + #enc = Tk.encoding + enc = ((Tk.encoding)? Tk.encoding : Tk.encoding_system) if enc Tk.fromUTF8(TkComm::string(val), enc) else diff --git a/ext/tk/lib/tk/validation.rb b/ext/tk/lib/tk/validation.rb index eeb0163ee..6755eef05 100644 --- a/ext/tk/lib/tk/validation.rb +++ b/ext/tk/lib/tk/validation.rb @@ -226,7 +226,8 @@ class TkValidateCommand [ ?w, TkComm.method(:window) ], [ ?e, proc{|val| - enc = Tk.encoding + #enc = Tk.encoding + enc = ((Tk.encoding)? Tk.encoding : Tk.encoding_system) if enc Tk.fromUTF8(TkComm::string(val), enc) else diff --git a/ext/tk/tkutil.c b/ext/tk/tkutil.c index 09332a31d..ada17da52 100644 --- a/ext/tk/tkutil.c +++ b/ext/tk/tkutil.c @@ -8,7 +8,7 @@ ************************************************/ -#define TKUTIL_RELEASE_DATE "2005-06-14" +#define TKUTIL_RELEASE_DATE "2005-07-05" #include "ruby.h" #include "rubysig.h" @@ -31,12 +31,15 @@ static ID ID_toUTF8; static ID ID_fromUTF8; static ID ID_path; static ID ID_at_path; +static ID ID_at_enc; static ID ID_to_eval; static ID ID_to_s; static ID ID_source; static ID ID_downcase; static ID ID_install_cmd; static ID ID_merge_tklist; +static ID ID_encoding; +static ID ID_encoding_system; static ID ID_call; static ID ID_SUBST_INFO; @@ -237,9 +240,23 @@ ary2list(ary, enc_flag, self) VALUE enc_flag; VALUE self; { - int idx, idx2, size, size2; - volatile VALUE val, val2; + int idx, idx2, size, size2, req_chk_flag; + volatile VALUE val, val2, str_val; volatile VALUE dst; + volatile VALUE sys_enc, dst_enc, str_enc; + + sys_enc = rb_funcall(cTclTkLib, ID_encoding, 0, 0); + if NIL_P(sys_enc) { + sys_enc = rb_funcall(cTclTkLib, ID_encoding_system, 0, 0); + } + + if NIL_P(enc_flag) { + dst_enc = sys_enc; + req_chk_flag = 1; + } else { + dst_enc = enc_flag; + req_chk_flag = 0; + } /* size = RARRAY(ary)->len; */ size = 0; @@ -255,10 +272,25 @@ ary2list(ary, enc_flag, self) RARRAY(dst)->len = 0; for(idx = 0; idx < RARRAY(ary)->len; idx++) { val = RARRAY(ary)->ptr[idx]; + str_val = Qnil; switch(TYPE(val)) { case T_ARRAY: - RARRAY(dst)->ptr[RARRAY(dst)->len++] - = ary2list(val, enc_flag, self); + str_val = ary2list(val, enc_flag, self); + RARRAY(dst)->ptr[RARRAY(dst)->len++] = str_val; + + if (req_chk_flag) { + str_enc = rb_ivar_get(str_val, ID_at_enc); + if NIL_P(str_enc) { + str_enc = rb_funcall(str_enc, ID_to_s, 0, 0); + } else { + str_enc = sys_enc; + } + if (!rb_str_cmp(str_enc, dst_enc)) { + dst_enc = Qtrue; + req_chk_flag = 0; + } + } + break; case T_HASH: @@ -273,24 +305,36 @@ ary2list(ary, enc_flag, self) val2 = RARRAY(val)->ptr[idx2]; switch(TYPE(val2)) { case T_ARRAY: - RARRAY(dst)->ptr[RARRAY(dst)->len++] - = ary2list(val2, enc_flag, self); + str_val = ary2list(val2, enc_flag, self); + RARRAY(dst)->ptr[RARRAY(dst)->len++] = str_val; break; case T_HASH: if (RTEST(enc_flag)) { - RARRAY(dst)->ptr[RARRAY(dst)->len++] - = hash2list_enc(val2, self); + str_val = hash2list_enc(val2, self); } else { - RARRAY(dst)->ptr[RARRAY(dst)->len++] - = hash2list(val2, self); + str_val = hash2list(val2, self); } + RARRAY(dst)->ptr[RARRAY(dst)->len++] = str_val; break; default: if (val2 != TK_None) { - RARRAY(dst)->ptr[RARRAY(dst)->len++] - = get_eval_string_core(val2, enc_flag, self); + str_val = get_eval_string_core(val2, enc_flag, self); + RARRAY(dst)->ptr[RARRAY(dst)->len++] = str_val; + } + } + + if (req_chk_flag) { + str_enc = rb_ivar_get(str_val, ID_at_enc); + if NIL_P(str_enc) { + str_enc = rb_funcall(str_enc, ID_to_s, 0, 0); + } else { + str_enc = sys_enc; + } + if (!rb_str_cmp(str_enc, dst_enc)) { + dst_enc = Qtrue; + req_chk_flag = 0; } } } @@ -298,12 +342,46 @@ ary2list(ary, enc_flag, self) default: if (val != TK_None) { - RARRAY(dst)->ptr[RARRAY(dst)->len++] - = get_eval_string_core(val, enc_flag, self); + str_val = get_eval_string_core(val, enc_flag, self); + RARRAY(dst)->ptr[RARRAY(dst)->len++] = str_val; + + if (req_chk_flag) { + str_enc = rb_ivar_get(str_val, ID_at_enc); + if NIL_P(str_enc) { + str_enc = rb_funcall(str_enc, ID_to_s, 0, 0); + } else { + str_enc = sys_enc; + } + if (!rb_str_cmp(str_enc, dst_enc)) { + dst_enc = Qtrue; + req_chk_flag = 0; + } + } + } + } + } + + if (RTEST(dst_enc) && !NIL_P(sys_enc)) { + for(idx = 0; idx < RARRAY(dst)->len; idx++) { + str_val = RARRAY(dst)->ptr[idx]; + if (rb_respond_to(self, ID_toUTF8)) { + str_val = rb_funcall(self, ID_toUTF8, 1, str_val); + } else { + str_val = rb_funcall(cTclTkLib, ID_toUTF8, 1, str_val); } + RARRAY(dst)->ptr[idx] = str_val; } + val = rb_apply(cTclTkLib, ID_merge_tklist, dst); + if (TYPE(dst_enc) == T_STRING) { + val = rb_fun_call(cTclTkLib, ID_fromUTF8, 2, val, dst_enc); + rb_ivar_set(val, ID_at_enc, dst_enc); + } else { + rb_ivar_set(val, ID_at_enc, rb_str_new2("utf-8")); + } + return val; + } else { + return rb_apply(cTclTkLib, ID_merge_tklist, dst); } - return rb_apply(cTclTkLib, ID_merge_tklist, dst); } static VALUE @@ -312,38 +390,88 @@ ary2list2(ary, enc_flag, self) VALUE enc_flag; VALUE self; { - int idx, size; - volatile VALUE val; + int idx, size, req_chk_flag; + volatile VALUE val, str_val; volatile VALUE dst; + volatile VALUE sys_enc, dst_enc, str_enc; + + sys_enc = rb_funcall(cTclTkLib, ID_encoding, 0, 0); + if NIL_P(sys_enc) { + sys_enc = rb_funcall(cTclTkLib, ID_encoding_system, 0, 0); + } + + if NIL_P(enc_flag) { + dst_enc = sys_enc; + req_chk_flag = 1; + } else { + dst_enc = enc_flag; + req_chk_flag = 0; + } size = RARRAY(ary)->len; dst = rb_ary_new2(size); RARRAY(dst)->len = 0; for(idx = 0; idx < RARRAY(ary)->len; idx++) { val = RARRAY(ary)->ptr[idx]; + str_val = Qnil; switch(TYPE(val)) { case T_ARRAY: - RARRAY(dst)->ptr[RARRAY(dst)->len++] - = ary2list(val, enc_flag, self); + str_val = ary2list(val, enc_flag, self); break; case T_HASH: if (RTEST(enc_flag)) { - RARRAY(dst)->ptr[RARRAY(dst)->len++] = hash2list(val, self); + str_val = hash2list(val, self); } else { - RARRAY(dst)->ptr[RARRAY(dst)->len++] - = hash2list_enc(val, self); + str_val = hash2list_enc(val, self); } break; default: if (val != TK_None) { - RARRAY(dst)->ptr[RARRAY(dst)->len++] - = get_eval_string_core(val, enc_flag, self); + str_val = get_eval_string_core(val, enc_flag, self); + } + } + + if (!NIL_P(str_val)) { + RARRAY(dst)->ptr[RARRAY(dst)->len++] = str_val; + + if (req_chk_flag) { + str_enc = rb_ivar_get(str_val, ID_at_enc); + if NIL_P(str_enc) { + str_enc = rb_funcall(str_enc, ID_to_s, 0, 0); + } else { + str_enc = sys_enc; + } + if (!rb_str_cmp(str_enc, dst_enc)) { + dst_enc = Qtrue; + req_chk_flag = 0; + } } } } - return rb_apply(cTclTkLib, ID_merge_tklist, dst); + + if (RTEST(dst_enc) && !NIL_P(sys_enc)) { + for(idx = 0; idx < RARRAY(dst)->len; idx++) { + str_val = RARRAY(dst)->ptr[idx]; + if (rb_respond_to(self, ID_toUTF8)) { + str_val = rb_funcall(self, ID_toUTF8, 1, str_val); + } else { + str_val = rb_funcall(cTclTkLib, ID_toUTF8, 1, str_val); + } + RARRAY(dst)->ptr[idx] = str_val; + } + val = rb_apply(cTclTkLib, ID_merge_tklist, dst); + if (TYPE(dst_enc) == T_STRING) { + val = rb_fun_call(cTclTkLib, ID_fromUTF8, 2, val, dst_enc); + rb_ivar_set(val, ID_at_enc, dst_enc); + } else { + rb_ivar_set(val, ID_at_enc, rb_str_new2("utf-8")); + } + return val; + } else { + return rb_apply(cTclTkLib, ID_merge_tklist, dst); + } } static VALUE @@ -1395,12 +1523,15 @@ Init_tkutil() ID_path = rb_intern("path"); ID_at_path = rb_intern("@path"); + ID_at_enc = rb_intern("@encoding"); ID_to_eval = rb_intern("to_eval"); ID_to_s = rb_intern("to_s"); ID_source = rb_intern("source"); ID_downcase = rb_intern("downcase"); ID_install_cmd = rb_intern("install_cmd"); ID_merge_tklist = rb_intern("_merge_tklist"); + ID_encoding = rb_intern("encoding"); + ID_encoding_system = rb_intern("encoding_system"); ID_call = rb_intern("call"); /* --------------------- */ |
