* ext/tk/tkutil/tkutil.c: improve handling callback-subst-keys.

Now, support longnam-keys (e.g. '%CTT' on tkdnd-2.0; however, still
  not support tkdnd-2.0 on tkextlib), and symbols of parameters (e.g. 
  :widget=>'%W', :keycode=>'%k', '%x'=>:x, '%X'=>:root_x, and so on; 
  those are attributes of event object). It means that Ruby/Tk accepts
  not only "widget.bind(ev, '%W', '%k', ...){|w, k, ...| ... }", but 
  also "widget.bind(ev, :widget, :keycode, ...){|w, k, ...| ... }". 
  It is potentially incompatible, when user passes symbols to the
  arguments of the callback block (the block receives the symbols as
  strings). I think that is very rare case (probably, used by Ruby/Tk
  experts only). When causes such trouble, please give strings instead
  of such symbol parameters (e.g. call Symbol#to_s method).

* ext/tk/lib/tk/event.rb, ext/tk/lib/tk/validation.rb, 
  ext/tk/lib/tkextlib/blt/treeview.rb, 
  ext/tk/lib/tkextlib/winico/winico.rb: ditto.

* ext/tk/tkutil/tkutil.c: strings are available on subst_tables on 
  TkUtil::CallbackSubst class (it is useful on Ruby 1.9). 

* ext/tk/lib/tk/spinbox.rb, ext/tk/lib/tkextlib/iwidgets/hierarchy.rb, 
  ext/tk/lib/tkextlib/iwidgets/spinner.rb, 
  ext/tk/lib/tkextlib/iwidgets/entryfield.rb, 
  ext/tk/lib/tkextlib/iwidgets/calendar.rb, 
  ext/tk/lib/tkextlib/blt/dragdrop.rb, 
  ext/tk/lib/tkextlib/tkDND/tkdnd.rb, 
  ext/tk/lib/tkextlib/treectrl/tktreectrl.rb, 
  ext/tk/lib/tkextlib/tktable/tktable.rb: disable code piece became 
  unnecessary by reason of the changes of ext/tk/tkutil/tkutil.c.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@16410 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nagai 2008-05-14 00:54:31 +00:00
parent 8cf1995901
commit d37d14c0a5
17 changed files with 410 additions and 166 deletions

View file

@ -1,3 +1,35 @@
Wed May 14 09:52:02 2008 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp>
* ext/tk/tkutil/tkutil.c: improve handling callback-subst-keys.
Now, support longnam-keys (e.g. '%CTT' on tkdnd-2.0; however, still
not support tkdnd-2.0 on tkextlib), and symbols of parameters (e.g.
:widget=>'%W', :keycode=>'%k', '%x'=>:x, '%X'=>:root_x, and so on;
those are attributes of event object). It means that Ruby/Tk accepts
not only "widget.bind(ev, '%W', '%k', ...){|w, k, ...| ... }", but
also "widget.bind(ev, :widget, :keycode, ...){|w, k, ...| ... }".
It is potentially incompatible, when user passes symbols to the
arguments of the callback block (the block receives the symbols as
strings). I think that is very rare case (probably, used by Ruby/Tk
experts only). When causes such trouble, please give strings instead
of such symbol parameters (e.g. call Symbol#to_s method).
* ext/tk/lib/tk/event.rb, ext/tk/lib/tk/validation.rb,
ext/tk/lib/tkextlib/blt/treeview.rb,
ext/tk/lib/tkextlib/winico/winico.rb: ditto.
* ext/tk/tkutil/tkutil.c: strings are available on subst_tables on
TkUtil::CallbackSubst class (it is useful on Ruby 1.9).
* ext/tk/lib/tk/spinbox.rb, ext/tk/lib/tkextlib/iwidgets/hierarchy.rb,
ext/tk/lib/tkextlib/iwidgets/spinner.rb,
ext/tk/lib/tkextlib/iwidgets/entryfield.rb,
ext/tk/lib/tkextlib/iwidgets/calendar.rb,
ext/tk/lib/tkextlib/blt/dragdrop.rb,
ext/tk/lib/tkextlib/tkDND/tkdnd.rb,
ext/tk/lib/tkextlib/treectrl/tktreectrl.rb,
ext/tk/lib/tkextlib/tktable/tktable.rb: disable code piece became
unnecessary by reason of the changes of ext/tk/tkutil/tkutil.c.
Tue May 13 15:10:50 2008 Akinori MUSHA <knu@iDaemons.org>
* enumerator.c: Update rdoc.

View file

@ -5359,7 +5359,7 @@ TkWidget = TkWindow
#Tk.freeze
module Tk
RELEASE_DATE = '2008-05-12'.freeze
RELEASE_DATE = '2008-05-14'.freeze
autoload :AUTO_PATH, 'tk/variable'
autoload :TCL_PACKAGE_PATH, 'tk/variable'

View file

@ -352,6 +352,14 @@ module TkEvent
nil
]
# [ <'%' subst-key str>, <proc type char>, <instance var (accessor) name>]
# the subst-key string will be converted to a bytecode (128+idx).
LONGKEY_TBL = [
# for example, for %CTT and %CST subst-key on tkdnd-2.0
# ['CTT', ?l, :drop_target_type],
# ['CST', ?l, :drop_source_type],
]
# [ <proc type char>, <proc/method to convert tcl-str to ruby-obj>]
PROC_TBL = [
[ ?n, TkComm.method(:num_or_str) ],
@ -371,6 +379,7 @@ module TkEvent
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -386,6 +395,7 @@ module TkEvent
end
inf
}
=end
# setup tables to be used by scan_args, _get_subst_key, _get_all_subst_keys
#
@ -399,7 +409,8 @@ module TkEvent
# ( which are Tcl strings ) to ruby objects based on the key string
# that is generated by _get_subst_key() or _get_all_subst_keys().
#
_setup_subst_table(KEY_TBL, PROC_TBL);
_setup_subst_table(KEY_TBL, PROC_TBL)
# _setup_subst_table(KEY_TBL, LONGKEY_TBL, PROC_TBL) # if use longname-keys
#
# NOTE: The order of parameters which passed to callback procedure is
@ -447,6 +458,7 @@ module TkEvent
extra_args_tbl = klass._get_extra_args_tbl
if args.compact.size > 0
args.map!{|arg| klass._sym2subst(arg)}
args = args.join(' ')
keys = klass._get_subst_key(args)

View file

@ -37,6 +37,7 @@ class Tk::Spinbox<Tk::Entry
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -52,6 +53,7 @@ class Tk::Spinbox<Tk::Entry
end
inf
}
=end
_setup_subst_table(KEY_TBL, PROC_TBL);

View file

@ -50,7 +50,8 @@ module Tk
key2class.each{|key, klass|
if keys[key].kind_of?(Array)
cmd, *args = keys[key]
keys[key] = klass.new(cmd, args.join(' '))
#keys[key] = klass.new(cmd, args.join(' '))
keys[key] = klass.new(cmd, *args)
# elsif keys[key].kind_of?(Proc) || keys[key].kind_of?(Method)
elsif TkComm._callback_entry?(keys[key])
keys[key] = klass.new(keys[key])
@ -151,7 +152,8 @@ module Tk
key2class.each{|key, klass|
if keys[key].kind_of?(Array)
cmd, *args = keys[key]
keys[key] = klass.new(cmd, args.join(' '))
#keys[key] = klass.new(cmd, args.join(' '))
keys[key] = klass.new(cmd, *args)
# elsif keys[key].kind_of?(Proc) || keys[key].kind_of?(Method)
elsif TkComm._callback_entry?(keys[key])
keys[key] = klass.new(keys[key])
@ -249,6 +251,7 @@ class TkValidateCommand
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -264,6 +267,7 @@ class TkValidateCommand
end
inf
}
=end
_setup_subst_table(KEY_TBL, PROC_TBL);
@ -293,6 +297,7 @@ class TkValidateCommand
extra_args_tbl = klass._get_extra_args_tbl
if args.compact.size > 0
args.map!{|arg| klass._sym2subst(arg)}
args = args.join(' ')
keys = klass._get_subst_key(args)
if cmd.kind_of?(String)

View file

@ -81,6 +81,7 @@ module Tk::BLT
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -96,6 +97,7 @@ module Tk::BLT
end
inf
}
=end
_setup_subst_table(KEY_TBL, PROC_TBL)
@ -123,6 +125,7 @@ module Tk::BLT
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -138,6 +141,7 @@ module Tk::BLT
end
inf
}
=end
_setup_subst_table(KEY_TBL, PROC_TBL)
@ -177,6 +181,7 @@ module Tk::BLT
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -192,6 +197,7 @@ module Tk::BLT
end
inf
}
=end
_setup_subst_table(KEY_TBL, PROC_TBL)
end

View file

@ -239,6 +239,7 @@ class Tk::BLT::Treeview
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -254,6 +255,7 @@ class Tk::BLT::Treeview
end
inf
}
=end
_setup_subst_table(KEY_TBL, PROC_TBL);
@ -492,6 +494,7 @@ class Tk::BLT::Treeview
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -507,6 +510,7 @@ class Tk::BLT::Treeview
end
inf
}
=end
_setup_subst_table(KEY_TBL, PROC_TBL);
@ -523,7 +527,8 @@ class Tk::BLT::Treeview
def _find_exec_flag_value(val)
if val.kind_of?(Array)
cmd, *args = val
FindExecFlagValue.new(cmd, args.join(' '))
#FindExecFlagValue.new(cmd, args.join(' '))
FindExecFlagValue.new(cmd, *args)
elsif TkComm._callback_entry?(val)
FindExecFlagValue.new(val)
else

View file

@ -46,6 +46,7 @@ class Tk::Iwidgets::Calendar
KEY_TBL = [ [?d, ?s, :date], nil ]
PROC_TBL = [ [?s, TkComm.method(:string) ], nil ]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -61,6 +62,7 @@ class Tk::Iwidgets::Calendar
end
inf
}
=end
_setup_subst_table(KEY_TBL, PROC_TBL);

View file

@ -43,6 +43,7 @@ class Tk::Iwidgets::Entryfield
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -58,6 +59,7 @@ class Tk::Iwidgets::Entryfield
end
inf
}
=end
_setup_subst_table(KEY_TBL, PROC_TBL);
end

View file

@ -31,6 +31,7 @@ class Tk::Iwidgets::Hierarchy
KEY_TBL = [ [?n, ?s, :node], nil ]
PROC_TBL = [ [?s, TkComm.method(:string) ], nil ]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -46,6 +47,7 @@ class Tk::Iwidgets::Hierarchy
end
inf
}
=end
_setup_subst_table(KEY_TBL, PROC_TBL);
@ -74,6 +76,7 @@ class Tk::Iwidgets::Hierarchy
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -89,6 +92,7 @@ class Tk::Iwidgets::Hierarchy
end
inf
}
=end
_setup_subst_table(KEY_TBL, PROC_TBL);
@ -112,6 +116,7 @@ class Tk::Iwidgets::Hierarchy
]
PROC_TBL = [ [ ?s, TkComm.method(:string) ], nil ]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -127,6 +132,7 @@ class Tk::Iwidgets::Hierarchy
end
inf
}
=end
_setup_subst_table(KEY_TBL, PROC_TBL);

View file

@ -38,6 +38,7 @@ class Tk::Iwidgets::Spinner
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -53,6 +54,7 @@ class Tk::Iwidgets::Spinner
end
inf
}
=end
_setup_subst_table(KEY_TBL, PROC_TBL);
end

View file

@ -57,6 +57,7 @@ module Tk
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -72,6 +73,7 @@ module Tk
end
inf
}
=end
# setup tables
_setup_subst_table(KEY_TBL, PROC_TBL);

View file

@ -291,6 +291,7 @@ class Tk::TkTable
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -306,6 +307,7 @@ class Tk::TkTable
end
inf
}
=end
_setup_subst_table(KEY_TBL, PROC_TBL);
@ -340,6 +342,7 @@ class Tk::TkTable
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -355,6 +358,7 @@ class Tk::TkTable
end
inf
}
=end
_setup_subst_table(KEY_TBL, PROC_TBL);
@ -387,6 +391,7 @@ class Tk::TkTable
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -402,6 +407,7 @@ class Tk::TkTable
end
inf
}
=end
_setup_subst_table(KEY_TBL, PROC_TBL);
@ -437,6 +443,7 @@ class Tk::TkTable
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -452,6 +459,7 @@ class Tk::TkTable
end
inf
}
=end
_setup_subst_table(KEY_TBL, PROC_TBL);
end

View file

@ -137,6 +137,7 @@ class Tk::TreeCtrl::NotifyEvent
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -152,6 +153,7 @@ class Tk::TreeCtrl::NotifyEvent
end
inf
}
=end
# setup tables to be used by scan_args, _get_subst_key, _get_all_subst_keys
#

View file

@ -2,5 +2,5 @@
# release date of tkextlib
#
module Tk
Tkextlib_RELEASE_DATE = '2008-05-12'.freeze
Tkextlib_RELEASE_DATE = '2008-05-14'.freeze
end

View file

@ -150,6 +150,7 @@ class Tk::Winico
nil
]
=begin
# for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
KEY_TBL.map!{|inf|
if inf.kind_of?(Array)
@ -165,6 +166,7 @@ class Tk::Winico
end
inf
}
=end
_setup_subst_table(KEY_TBL, PROC_TBL);
@ -185,7 +187,8 @@ class Tk::Winico
Winico_callback._config_keys.each{|k|
if keys[k].kind_of?(Array)
cmd, *args = keys[k]
keys[k] = Winico_callback.new(cmd, args.join(' '))
#keys[k] = Winico_callback.new(cmd, args.join(' '))
keys[k] = Winico_callback.new(cmd, *args)
# elsif keys[k].kind_of?(Proc)
elsif TkComm._callback_entry?(keys[k])
keys[k] = Winico_callback.new(keys[k])
@ -201,7 +204,8 @@ class Tk::Winico
Winico_callback._config_keys.each{|k|
if keys[k].kind_of?(Array)
cmd, *args = keys[k]
keys[k] = Winico_callback.new(cmd, args.join(' '))
#keys[k] = Winico_callback.new(cmd, args.join(' '))
keys[k] = Winico_callback.new(cmd, *args)
# elsif keys[k].kind_of?(Proc)
elsif TkComm._callback_entry?(keys[k])
keys[k] = Winico_callback.new(keys[k])

View file

@ -7,7 +7,7 @@
************************************************/
#define TKUTIL_RELEASE_DATE "2008-03-29"
#define TKUTIL_RELEASE_DATE "2008-05-14"
#include "ruby.h"
@ -1073,11 +1073,13 @@ tcl2rb_num_or_str(self, value)
/*************************************/
#define CBSUBST_TBL_MAX (256)
struct cbsubst_info {
int size;
char *key;
char *type;
ID *ivar;
int full_subst_length;
int keylen[CBSUBST_TBL_MAX];
unsigned char *key[CBSUBST_TBL_MAX];
unsigned char type[CBSUBST_TBL_MAX];
ID ivar[CBSUBST_TBL_MAX];
VALUE proc;
VALUE aliases;
};
@ -1094,33 +1096,33 @@ static void
subst_free(ptr)
struct cbsubst_info *ptr;
{
int i;
if (ptr) {
if (ptr->key != (char*)NULL) free(ptr->key);
if (ptr->type != (char*)NULL) free(ptr->type);
if (ptr->ivar != (ID*)NULL) free(ptr->ivar);
for(i = 0; i < CBSUBST_TBL_MAX; i++) {
if (ptr->key[i] != (unsigned char *)NULL) free(ptr->key[i]);
}
free(ptr);
}
}
static void
cbsubst_init()
static struct cbsubst_info *
allocate_cbsubst_info()
{
struct cbsubst_info *inf;
ID *ivar;
volatile VALUE proc, aliases;
int idx;
inf = ALLOC(struct cbsubst_info);
inf->size = 0;
inf->full_subst_length = 0;
inf->key = ALLOC_N(char, 1);
inf->key[0] = '\0';
inf->type = ALLOC_N(char, 1);
inf->type[0] = '\0';
ivar = ALLOC_N(ID, 1);
inf->ivar = ivar;
for(idx = 0; idx < CBSUBST_TBL_MAX; idx++) {
inf->keylen[idx] = 0;
inf->key[idx] = (unsigned char *) NULL;
inf->type[idx] = '\0';
inf->ivar[idx] = (ID) 0;
}
proc = rb_hash_new();
inf->proc = proc;
@ -1128,8 +1130,15 @@ cbsubst_init()
aliases = rb_hash_new();
inf->aliases = aliases;
return inf;
}
static void
cbsubst_init()
{
rb_const_set(cCB_SUBST, ID_SUBST_INFO,
Data_Wrap_Struct(cSUBST_INFO, subst_mark, subst_free, inf));
Data_Wrap_Struct(cSUBST_INFO, subst_mark, subst_free,
allocate_cbsubst_info()));
}
static VALUE
@ -1139,24 +1148,29 @@ cbsubst_initialize(argc, argv, self)
VALUE self;
{
struct cbsubst_info *inf;
int idx;
int idx, iv_idx;
Data_Get_Struct(rb_const_get(rb_obj_class(self), ID_SUBST_INFO),
struct cbsubst_info, inf);
for(idx = 0; idx < argc; idx++) {
rb_ivar_set(self, inf->ivar[idx], argv[idx]);
idx = 0;
for(iv_idx = 0; iv_idx < CBSUBST_TBL_MAX; iv_idx++) {
if ( inf->ivar[iv_idx] == (ID) 0 ) continue;
rb_ivar_set(self, inf->ivar[iv_idx], argv[idx++]);
if (idx >= argc) break;
}
return self;
}
static VALUE
cbsubst_ret_val(self, val)
VALUE self;
VALUE val;
{
/* This method may be overwritten on some sub-classes. */
/* This method is used for converting from ruby's callback-return-value */
/* to tcl's value (e.g. validation procedure of entry widget). */
return val;
}
@ -1216,6 +1230,59 @@ cbsubst_def_attr_aliases(self, tbl)
return rb_funcall(inf->aliases, rb_intern("update"), 1, tbl);
}
static VALUE
cbsubst_sym_to_subst(self, sym)
VALUE self;
VALUE sym;
{
struct cbsubst_info *inf;
const char *str;
unsigned char *buf, *ptr;
int idx, len;
ID id;
volatile VALUE ret;
if (TYPE(sym) != T_SYMBOL) return sym;
Data_Get_Struct(rb_const_get(self, ID_SUBST_INFO),
struct cbsubst_info, inf);
if (!NIL_P(ret = rb_hash_aref(inf->aliases, sym))) {
str = rb_id2name(SYM2ID(ret));
} else {
str = rb_id2name(SYM2ID(sym));
}
id = rb_intern(RSTRING_PTR(rb_str_cat2(rb_str_new2("@"), str)));
for(idx = 0; idx < CBSUBST_TBL_MAX; idx++) {
if (inf->ivar[idx] == id) break;
}
if (idx >= CBSUBST_TBL_MAX) return sym;
ptr = buf = ALLOC_N(char, inf->full_subst_length + 1);
*(ptr++) = '%';
if (len = inf->keylen[idx]) {
/* longname */
strncpy(ptr, inf->key[idx], len);
ptr += len;
} else {
/* single char */
*(ptr++) = idx;
}
*(ptr++) = ' ';
*(ptr++) = '\0';
ret = rb_str_new2(buf);
free(buf);
return ret;
}
static VALUE
cbsubst_get_subst_arg(argc, argv, self)
int argc;
@ -1224,17 +1291,15 @@ cbsubst_get_subst_arg(argc, argv, self)
{
struct cbsubst_info *inf;
const char *str;
char *buf, *ptr;
int i, j, len;
unsigned char *buf, *ptr;
int i, idx, len;
ID id;
volatile VALUE arg_sym, ret;
Data_Get_Struct(rb_const_get(self, ID_SUBST_INFO),
struct cbsubst_info, inf);
buf = ALLOC_N(char, 3*argc + 1);
ptr = buf;
len = strlen(inf->key);
ptr = buf = ALLOC_N(char, inf->full_subst_length + 1);
for(i = 0; i < argc; i++) {
switch(TYPE(argv[i])) {
@ -1256,16 +1321,24 @@ cbsubst_get_subst_arg(argc, argv, self)
id = rb_intern(RSTRING_PTR(rb_str_cat2(rb_str_new2("@"), str)));
for(j = 0; j < len; j++) {
if (inf->ivar[j] == id) break;
for(idx = 0; idx < CBSUBST_TBL_MAX; idx++) {
if (inf->ivar[idx] == id) break;
}
if (j >= len) {
if (idx >= CBSUBST_TBL_MAX) {
rb_raise(rb_eArgError, "cannot find attribute :%s", str);
}
*(ptr++) = '%';
*(ptr++) = *(inf->key + j);
if (len = inf->keylen[idx]) {
/* longname */
strncpy(ptr, inf->key[idx], len);
ptr += len;
} else {
/* single char */
*(ptr++) = idx;
}
*(ptr++) = ' ';
}
@ -1283,27 +1356,50 @@ cbsubst_get_subst_key(self, str)
VALUE self;
VALUE str;
{
struct cbsubst_info *inf;
volatile VALUE list;
volatile VALUE ret;
int i, len;
char *buf, *ptr;
VALUE keyval;
int i, len, keylen, idx;
unsigned char *buf, *ptr, *key;
list = rb_funcall(cTclTkLib, ID_split_tklist, 1, str);
len = RARRAY_LEN(list);
buf = ALLOC_N(char, len + 1);
Data_Get_Struct(rb_const_get(self, ID_SUBST_INFO),
struct cbsubst_info, inf);
ptr = buf = ALLOC_N(unsigned char, inf->full_subst_length + len + 1);
for(i = 0; i < len; i++) {
ptr = RSTRING_PTR(RARRAY_PTR(list)[i]);
if (*ptr == '%' && *(ptr + 2) == '\0') {
*(buf + i) = *(ptr + 1);
keyval = RARRAY_PTR(list)[i];
key = (unsigned char*)RSTRING_PTR(keyval);
if (*key == '%') {
if (*(key + 2) == '\0') {
/* single char */
*(ptr++) = *(key + 1);
} else {
*(buf + i) = ' ';
/* search longname-key */
keylen = RSTRING_LEN(keyval) - 1;
for(idx = 0; idx < CBSUBST_TBL_MAX; idx++) {
if (inf->keylen[idx] != keylen) continue;
if (inf->key[idx][0] != *(key + 1)) continue;
if (strncmp(inf->key[idx], key + 1, keylen)) continue;
break;
}
if (idx < CBSUBST_TBL_MAX) {
*(ptr++) = (unsigned char)idx;
} else {
*(ptr++) = ' ';
}
}
*(buf + len) = '\0';
} else {
*(ptr++) = ' ';
}
}
*ptr = '\0';
ret = rb_str_new2(buf);
ret = rb_str_new2((const char*)buf);
free(buf);
return ret;
}
@ -1313,24 +1409,40 @@ cbsubst_get_all_subst_keys(self)
VALUE self;
{
struct cbsubst_info *inf;
char *buf, *ptr;
int i, len;
unsigned char *buf, *ptr;
unsigned char *keys_buf, *keys_ptr;
int idx, len;
volatile VALUE ret;
Data_Get_Struct(rb_const_get(self, ID_SUBST_INFO),
struct cbsubst_info, inf);
len = strlen(inf->key);
buf = ALLOC_N(char, 3*len + 1);
ptr = buf;
for(i = 0; i < len; i++) {
ptr = buf = ALLOC_N(unsigned char, inf->full_subst_length + 1);
keys_ptr = keys_buf = ALLOC_N(unsigned char, CBSUBST_TBL_MAX + 1);
for(idx = 0; idx < CBSUBST_TBL_MAX; idx++) {
if (inf->ivar[idx] == (ID) 0) continue;
*(keys_ptr++) = (unsigned char)idx;
*(ptr++) = '%';
*(ptr++) = *(inf->key + i);
if (len = inf->keylen[idx]) {
/* longname */
strncpy(ptr, inf->key[idx], len);
ptr += len;
} else {
/* single char */
*(ptr++) = (unsigned char)idx;
}
*(ptr++) = ' ';
}
*(buf + 3*len) = '\0';
ret = rb_ary_new3(2, rb_str_new2(inf->key), rb_str_new2(buf));
*ptr = '\0';
*keys_ptr = '\0';
ret = rb_ary_new3(2, rb_str_new2(keys_buf), rb_str_new2((const char*)buf));
free(buf);
@ -1338,68 +1450,108 @@ cbsubst_get_all_subst_keys(self)
}
static VALUE
cbsubst_table_setup(self, key_inf, proc_inf)
cbsubst_table_setup(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
VALUE key_inf;
VALUE proc_inf;
{
volatile VALUE key_inf;
volatile VALUE longkey_inf;
volatile VALUE proc_inf;
VALUE inf;
ID id;
struct cbsubst_info *subst_inf;
int idx;
int len = RARRAY_LEN(key_inf);
int real_len = 0;
char *key = ALLOC_N(char, len + 1);
char *type = ALLOC_N(char, len + 1);
ID *ivar = ALLOC_N(ID, len + 1);
volatile VALUE proc = rb_hash_new();
volatile VALUE aliases = rb_hash_new();
volatile VALUE inf;
int idx, len;
unsigned char chr;
/* accept (key_inf, proc_inf) or (key_inf, longkey_inf, procinf) */
if (rb_scan_args(argc, argv, "21", &key_inf, &longkey_inf, &proc_inf) == 2) {
proc_inf = longkey_inf;
longkey_inf = rb_ary_new();
}
/* check the number of longkeys */
if (RARRAY_LEN(longkey_inf) > 125 /* from 0x80 to 0xFD */) {
rb_raise(rb_eArgError, "too many longname-key definitions");
}
/* init */
subst_inf = ALLOC(struct cbsubst_info);
/* subst_inf->size = len; */
subst_inf->key = key;
subst_inf->type = type;
subst_inf->ivar = ivar;
subst_inf->proc = proc;
subst_inf->aliases = aliases;
subst_inf = allocate_cbsubst_info();
/*
* keys : array of [subst, type, ivar]
* subst ==> char code
* type ==> char code
* subst ==> char code or string
* type ==> char code or string
* ivar ==> symbol
*/
len = RARRAY_LEN(key_inf);
for(idx = 0; idx < len; idx++) {
inf = RARRAY_PTR(key_inf)[idx];
if (TYPE(inf) != T_ARRAY) continue;
*(key + real_len) = NUM2CHR(RARRAY_PTR(inf)[0]);
*(type + real_len) = NUM2CHR(RARRAY_PTR(inf)[1]);
*(ivar + real_len)
= rb_intern(
RSTRING_PTR(
rb_str_cat2(rb_str_new2("@"),
rb_id2name(SYM2ID(RARRAY_PTR(inf)[2])))
)
);
rb_attr(self, SYM2ID(RARRAY_PTR(inf)[2]), 1, 0, Qtrue);
real_len++;
if (TYPE(RARRAY_PTR(inf)[0]) == T_STRING) {
chr = *(RSTRING_PTR(RARRAY_PTR(inf)[0]));
} else {
chr = NUM2CHR(RARRAY_PTR(inf)[0]);
}
if (TYPE(RARRAY_PTR(inf)[1]) == T_STRING) {
subst_inf->type[chr] = *(RSTRING_PTR(RARRAY_PTR(inf)[1]));
} else {
subst_inf->type[chr] = NUM2CHR(RARRAY_PTR(inf)[1]);
}
subst_inf->full_subst_length += 3;
id = SYM2ID(RARRAY_PTR(inf)[2]);
subst_inf->ivar[chr] = rb_intern(RSTRING_PTR(rb_str_cat2(rb_str_new2("@"), rb_id2name(id))));
rb_attr(self, id, 1, 0, Qtrue);
}
/*
* longkeys : array of [name, type, ivar]
* name ==> longname key string
* type ==> char code or string
* ivar ==> symbol
*/
len = RARRAY_LEN(longkey_inf);
for(idx = 0; idx < len; idx++) {
inf = RARRAY_PTR(longkey_inf)[idx];
if (TYPE(inf) != T_ARRAY) continue;
chr = (unsigned char)(0x80 + idx);
subst_inf->keylen[chr] = RSTRING_LEN(RARRAY_PTR(inf)[0]);
subst_inf->key[chr] = strndup(RSTRING_PTR(RARRAY_PTR(inf)[0]),
RSTRING_LEN(RARRAY_PTR(inf)[0]));
if (TYPE(RARRAY_PTR(inf)[1]) == T_STRING) {
subst_inf->type[chr] = *(RSTRING_PTR(RARRAY_PTR(inf)[1]));
} else {
subst_inf->type[chr] = NUM2CHR(RARRAY_PTR(inf)[1]);
}
subst_inf->full_subst_length += (subst_inf->keylen[chr] + 2);
id = SYM2ID(RARRAY_PTR(inf)[2]);
subst_inf->ivar[chr] = rb_intern(RSTRING_PTR(rb_str_cat2(rb_str_new2("@"), rb_id2name(id))));
rb_attr(self, id, 1, 0, Qtrue);
}
*(key + real_len) = '\0';
*(type + real_len) = '\0';
subst_inf->size = real_len;
/*
* procs : array of [type, proc]
* type ==> char code
* type ==> char code or string
* proc ==> proc/method/obj (must respond to 'call')
*/
len = RARRAY_LEN(proc_inf);
for(idx = 0; idx < len; idx++) {
inf = RARRAY_PTR(proc_inf)[idx];
if (TYPE(inf) != T_ARRAY) continue;
rb_hash_aset(proc, RARRAY_PTR(inf)[0], RARRAY_PTR(inf)[1]);
rb_hash_aset(subst_inf->proc,
((TYPE(RARRAY_PTR(inf)[0]) == T_STRING)?
INT2FIX(*(RSTRING_PTR(RARRAY_PTR(inf)[0]))) :
RARRAY_PTR(inf)[0]),
RARRAY_PTR(inf)[1]);
}
rb_const_set(self, ID_SUBST_INFO,
@ -1424,10 +1576,11 @@ cbsubst_scan_args(self, arg_key, val_ary)
{
struct cbsubst_info *inf;
int idx;
int len = RARRAY_LEN(val_ary);
char c;
char *ptr;
volatile VALUE dst = rb_ary_new2(len);
unsigned char *keyptr = (unsigned char*)RSTRING_PTR(arg_key);
int keylen = RSTRING_LEN(arg_key);
int vallen = RARRAY_LEN(val_ary);
unsigned char type_chr;
volatile VALUE dst = rb_ary_new2(vallen);
volatile VALUE proc;
int thr_crit_bup;
VALUE old_gc;
@ -1440,25 +1593,24 @@ cbsubst_scan_args(self, arg_key, val_ary)
Data_Get_Struct(rb_const_get(self, ID_SUBST_INFO),
struct cbsubst_info, inf);
for(idx = 0; idx < len; idx++) {
if (idx >= RSTRING_LEN(arg_key)) {
for(idx = 0; idx < vallen; idx++) {
if (idx >= keylen) {
proc = Qnil;
} else if (*(RSTRING_PTR(arg_key) + idx) == ' ') {
} else if (*(keyptr + idx) == ' ') {
proc = Qnil;
} else {
ptr = strchr(inf->key, *(RSTRING_PTR(arg_key) + idx));
if (ptr == (char*)NULL) {
proc = Qnil;
if (type_chr = inf->type[*(keyptr + idx)]) {
proc = rb_hash_aref(inf->proc, INT2FIX((int)type_chr));
} else {
c = *(inf->type + (ptr - inf->key));
proc = rb_hash_aref(inf->proc, INT2FIX(c));
proc = Qnil;
}
}
if (NIL_P(proc)) {
rb_ary_push(dst, RARRAY_PTR(val_ary)[idx]);
} else {
rb_ary_push(dst, rb_funcall(proc, ID_call, 1, RARRAY_PTR(val_ary)[idx]));
rb_ary_push(dst, rb_funcall(proc, ID_call, 1,
RARRAY_PTR(val_ary)[idx]));
}
}
@ -1543,6 +1695,8 @@ Init_tkutil()
ID_SUBST_INFO = rb_intern("SUBST_INFO");
rb_define_singleton_method(cCB_SUBST, "ret_val", cbsubst_ret_val, 1);
rb_define_singleton_method(cCB_SUBST, "scan_args", cbsubst_scan_args, 2);
rb_define_singleton_method(cCB_SUBST, "_sym2subst",
cbsubst_sym_to_subst, 1);
rb_define_singleton_method(cCB_SUBST, "subst_arg",
cbsubst_get_subst_arg, -1);
rb_define_singleton_method(cCB_SUBST, "_get_subst_key",
@ -1550,7 +1704,7 @@ Init_tkutil()
rb_define_singleton_method(cCB_SUBST, "_get_all_subst_keys",
cbsubst_get_all_subst_keys, 0);
rb_define_singleton_method(cCB_SUBST, "_setup_subst_table",
cbsubst_table_setup, 2);
cbsubst_table_setup, -1);
rb_define_singleton_method(cCB_SUBST, "_get_extra_args_tbl",
cbsubst_get_extra_args_tbl, 0);
rb_define_singleton_method(cCB_SUBST, "_define_attribute_aliases",