1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
|
#
# tk/variable.rb : treat Tk variable object
#
require 'tk'
class TkVariable
include Tk
extend TkCore
include Comparable
#TkCommandNames = ['tkwait'.freeze].freeze
TkCommandNames = ['vwait'.freeze].freeze
#TkVar_CB_TBL = {}
#TkVar_ID_TBL = {}
TkVar_CB_TBL = TkCore::INTERP.create_table
TkVar_ID_TBL = TkCore::INTERP.create_table
Tk_VARIABLE_ID = ["v".freeze, "00000".taint].freeze
#TkCore::INTERP.add_tk_procs('rb_var', 'args',
# "ruby [format \"TkVariable.callback %%Q!%s!\" $args]")
TkCore::INTERP.add_tk_procs('rb_var', 'args', <<-'EOL')
if {[set st [catch {eval {ruby_cmd TkVariable callback} $args} ret]] != 0} {
set idx [string first "\n\n" $ret]
if {$idx > 0} {
global errorInfo
set tcl_backtrace $errorInfo
set errorInfo [string range $ret [expr $idx + 2] \
[string length $ret]]
append errorInfo "\n" $tcl_backtrace
bgerror [string range $ret 0 [expr $idx - 1]]
} else {
bgerror $ret
}
return ""
#return -code $st $ret
} else {
return $ret
}
EOL
#def TkVariable.callback(args)
def TkVariable.callback(name1, name2, op)
#name1,name2,op = tk_split_list(args)
#name1,name2,op = tk_split_simplelist(args)
if TkVar_CB_TBL[name1]
#_get_eval_string(TkVar_CB_TBL[name1].trace_callback(name2,op))
begin
_get_eval_string(TkVar_CB_TBL[name1].trace_callback(name2, op))
rescue Exception => e
begin
msg = _toUTF8(e.class.inspect) + ': ' +
_toUTF8(e.message) + "\n" +
"\n---< backtrace of Ruby side >-----\n" +
_toUTF8(e.backtrace.join("\n")) +
"\n---< backtrace of Tk side >-------"
msg.instance_variable_set(:@encoding, 'utf-8')
rescue Exception
msg = e.class.inspect + ': ' + e.message + "\n" +
"\n---< backtrace of Ruby side >-----\n" +
e.backtrace.join("\n") +
"\n---< backtrace of Tk side >-------"
end
fail(e, msg)
end
=begin
begin
raise 'check backtrace'
rescue
# ignore backtrace before 'callback'
pos = -($!.backtrace.size)
end
begin
_get_eval_string(TkVar_CB_TBL[name1].trace_callback(name2,op))
rescue
trace = $!.backtrace
raise $!, "\n#{trace[0]}: #{$!.message} (#{$!.class})\n" +
"\tfrom #{trace[1..pos].join("\n\tfrom ")}"
end
=end
else
''
end
end
def initialize(val="")
@id = Tk_VARIABLE_ID.join('')
Tk_VARIABLE_ID[1].succ!
TkVar_ID_TBL[@id] = self
@trace_var = nil
@trace_elem = nil
@trace_opts = nil
begin
INTERP._unset_global_var(@id)
rescue
end
# teach Tk-ip that @id is global var
INTERP._invoke_without_enc('global', @id)
#INTERP._invoke('global', @id)
self.value = val
=begin
if val == []
# INTERP._eval(format('global %s; set %s(0) 0; unset %s(0)',
# @id, @id, @id))
elsif val.kind_of?(Array)
a = []
# val.each_with_index{|e,i| a.push(i); a.push(array2tk_list(e))}
# s = '"' + a.join(" ").gsub(/[\[\]$"]/, '\\\\\&') + '"'
val.each_with_index{|e,i| a.push(i); a.push(e)}
#s = '"' + array2tk_list(a).gsub(/[\[\]$"]/, '\\\\\&') + '"'
s = '"' + array2tk_list(a).gsub(/[\[\]$"\\]/, '\\\\\&') + '"'
INTERP._eval(format('global %s; array set %s %s', @id, @id, s))
elsif val.kind_of?(Hash)
#s = '"' + val.to_a.collect{|e| array2tk_list(e)}.join(" ")\
# .gsub(/[\[\]$"]/, '\\\\\&') + '"'
s = '"' + val.to_a.collect{|e| array2tk_list(e)}.join(" ")\
.gsub(/[\[\]$"\\]/, '\\\\\&') + '"'
INTERP._eval(format('global %s; array set %s %s', @id, @id, s))
else
#s = '"' + _get_eval_string(val).gsub(/[\[\]$"]/, '\\\\\&') + '"'
s = '"' + _get_eval_string(val).gsub(/[\[\]$"\\]/, '\\\\\&') + '"'
INTERP._eval(format('global %s; set %s %s', @id, @id, s))
end
=end
=begin
if val.kind_of?(Hash)
#s = '"' + val.to_a.collect{|e| array2tk_list(e)}.join(" ")\
# .gsub(/[\[\]$"]/, '\\\\\&') + '"'
s = '"' + val.to_a.collect{|e| array2tk_list(e)}.join(" ")\
.gsub(/[\[\]$"\\]/, '\\\\\&') + '"'
INTERP._eval(Kernel.format('global %s; array set %s %s', @id, @id, s))
else
#s = '"' + _get_eval_string(val).gsub(/[\[\]$"]/, '\\\\\&') + '"'
s = '"' + _get_eval_string(val).gsub(/[\[\]$"\\]/, '\\\\\&') + '"'
INTERP._eval(Kernel.format('global %s; set %s %s', @id, @id, s))
end
=end
end
def wait(on_thread = false, check_root = false)
if $SAFE >= 4
fail SecurityError, "can't wait variable at $SAFE >= 4"
end
on_thread &= (Thread.list.size != 1)
if on_thread
if check_root
INTERP._thread_tkwait('variable', @id)
else
INTERP._thread_vwait(@id)
end
else
if check_root
INTERP._invoke_without_enc('tkwait', 'variable', @id)
else
INTERP._invoke_without_enc('vwait', @id)
end
end
end
def eventloop_wait(check_root = false)
wait(false, check_root)
end
def thread_wait(check_root = false)
wait(true, check_root)
end
def tkwait(on_thread = true)
wait(on_thread, true)
end
def eventloop_tkwait
wait(false, true)
end
def thread_tkwait
wait(true, true)
end
def id
@id
end
def is_hash?
#ITNERP._eval("global #{@id}; array exist #{@id}") == '1'
ITNERP._invoke_without_enc('array', 'exist', @id) == '1'
end
def is_scalar?
! is_hash?
end
def keys
if (is_scalar?)
fail RuntimeError, 'cannot get keys from a scalar variable'
end
#tk_split_simplelist(INTERP._eval("global #{@id}; array get #{@id}"))
tk_split_simplelist(INTERP._fromUTF8(INTERP._invoke_without_enc('array', 'get', @id)))
end
unless const_defined?(:USE_TCLs_SET_VARIABLE_FUNCTIONS)
USE_TCLs_SET_VARIABLE_FUNCTIONS = true
end
if USE_TCLs_SET_VARIABLE_FUNCTIONS
###########################################################################
# use Tcl function version of set tkvariable
###########################################################################
def value
#if INTERP._eval("global #{@id}; array exist #{@id}") == '1'
if INTERP._invoke('array', 'exist', @id) == '1'
#Hash[*tk_split_simplelist(INTERP._eval("global #{@id}; array get #{@id}"))]
Hash[*tk_split_simplelist(INTERP._invoke('array', 'get', @id))]
else
_fromUTF8(INTERP._get_global_var(@id))
end
end
def value=(val)
if val.kind_of?(Hash)
val.each{|k, v|
#INTERP._set_global_var2(@id, _toUTF8(_get_eval_string(k)),
# _toUTF8(_get_eval_string(v)))
INTERP._set_global_var2(@id, __get_eval_string(k, true),
_get_eval_string(v, true))
}
self.value
elsif val.kind_of?(Array)
INTERP._set_global_var(@id, '')
val.each{|v|
#INTERP._set_variable(@id, _toUTF8(_get_eval_string(v)),
INTERP._set_variable(@id, _get_eval_string(v, true),
TclTkLib::VarAccessFlag::GLOBAL_ONLY |
TclTkLib::VarAccessFlag::LEAVE_ERR_MSG |
TclTkLib::VarAccessFlag::APPEND_VALUE |
TclTkLib::VarAccessFlag::LIST_ELEMENT)
}
self.value
else
#_fromUTF8(INTERP._set_global_var(@id, _toUTF8(_get_eval_string(val))))
_fromUTF8(INTERP._set_global_var(@id, _get_eval_string(val, true)))
end
end
def [](index)
#_fromUTF8(INTERP._get_global_var2(@id, _toUTF8(_get_eval_string(index))))
_fromUTF8(INTERP._get_global_var2(@id, _get_eval_string(index, true)))
end
def []=(index,val)
#_fromUTF8(INTERP._set_global_var2(@id, _toUTF8(_get_eval_string(index)),
# _toUTF8(_get_eval_string(val))))
_fromUTF8(INTERP._set_global_var2(@id, _get_eval_string(index, true),
_get_eval_string(val, true)))
end
def unset(elem=nil)
if elem
INTERP._unset_global_var2(@id, tk_tcl2ruby(elem))
else
INTERP._unset_global_var(@id)
end
end
alias remove unset
else
###########################################################################
# use Ruby script version of set tkvariable (traditional methods)
###########################################################################
def value
begin
INTERP._eval(Kernel.format('global %s; set %s', @id, @id))
#INTERP._eval(Kernel.format('set %s', @id))
#INTERP._invoke_without_enc('set', @id)
rescue
if INTERP._eval(Kernel.format('global %s; array exists %s',
@id, @id)) != "1"
#if INTERP._eval(Kernel.format('array exists %s', @id)) != "1"
#if INTERP._invoke_without_enc('array', 'exists', @id) != "1"
fail
else
Hash[*tk_split_simplelist(INTERP._eval(Kernel.format('global %s; array get %s', @id, @id)))]
#Hash[*tk_split_simplelist(_fromUTF8(INTERP._invoke_without_enc('array', 'get', @id)))]
end
end
end
def value=(val)
begin
#s = '"' + _get_eval_string(val).gsub(/[\[\]$"]/, '\\\\\&') + '"'
s = '"' + _get_eval_string(val).gsub(/[\[\]$"\\]/, '\\\\\&') + '"'
INTERP._eval(Kernel.format('global %s; set %s %s', @id, @id, s))
#INTERP._eval(Kernel.format('set %s %s', @id, s))
#_fromUTF8(INTERP._invoke_without_enc('set', @id, _toUTF8(s)))
rescue
if INTERP._eval(Kernel.format('global %s; array exists %s',
@id, @id)) != "1"
#if INTERP._eval(Kernel.format('array exists %s', @id)) != "1"
#if INTERP._invoke_without_enc('array', 'exists', @id) != "1"
fail
else
if val == []
INTERP._eval(Kernel.format('global %s; unset %s; set %s(0) 0; unset %s(0)', @id, @id, @id, @id))
#INTERP._eval(Kernel.format('unset %s; set %s(0) 0; unset %s(0)',
# @id, @id, @id))
#INTERP._invoke_without_enc('unset', @id)
#INTERP._invoke_without_enc('set', @id+'(0)', 0)
#INTERP._invoke_without_enc('unset', @id+'(0)')
elsif val.kind_of?(Array)
a = []
val.each_with_index{|e,i| a.push(i); a.push(array2tk_list(e))}
#s = '"' + a.join(" ").gsub(/[\[\]$"]/, '\\\\\&') + '"'
s = '"' + a.join(" ").gsub(/[\[\]$"\\]/, '\\\\\&') + '"'
INTERP._eval(Kernel.format('global %s; unset %s; array set %s %s',
@id, @id, @id, s))
#INTERP._eval(Kernel.format('unset %s; array set %s %s',
# @id, @id, s))
#INTERP._invoke_without_enc('unset', @id)
#_fromUTF8(INTERP._invoke_without_enc('array','set', @id, _toUTF8(s)))
elsif val.kind_of?(Hash)
#s = '"' + val.to_a.collect{|e| array2tk_list(e)}.join(" ")\
# .gsub(/[\[\]$"]/, '\\\\\&') + '"'
s = '"' + val.to_a.collect{|e| array2tk_list(e)}.join(" ")\
.gsub(/[\[\]$\\"]/, '\\\\\&') + '"'
INTERP._eval(Kernel.format('global %s; unset %s; array set %s %s',
@id, @id, @id, s))
#INTERP._eval(Kernel.format('unset %s; array set %s %s',
# @id, @id, s))
#INTERP._invoke_without_enc('unset', @id)
#_fromUTF8(INTERP._invoke_without_enc('array','set', @id, _toUTF8(s)))
else
fail
end
end
end
end
def [](index)
INTERP._eval(Kernel.format('global %s; set %s(%s)',
@id, @id, _get_eval_string(index)))
#INTERP._eval(Kernel.format('set %s(%s)', @id, _get_eval_string(index)))
#INTERP._eval('set ' + @id + '(' + _get_eval_string(index) + ')')
end
def []=(index,val)
INTERP._eval(Kernel.format('global %s; set %s(%s) %s', @id, @id,
_get_eval_string(index), _get_eval_string(val)))
#INTERP._eval(Kernel.format('set %s(%s) %s', @id,
# _get_eval_string(index), _get_eval_string(val)))
#INTERP._eval('set ' + @id + '(' + _get_eval_string(index) + ') ' +
# _get_eval_string(val))
end
def unset(elem=nil)
if elem
INTERP._eval(Kernel.format('global %s; unset %s(%s)',
@id, @id, tk_tcl2ruby(elem)))
#INTERP._eval(Kernel.format('unset %s(%s)', @id, tk_tcl2ruby(elem)))
#INTERP._eval('unset ' + @id + '(' + _get_eval_string(elem) + ')')
else
INTERP._eval(Kernel.format('global %s; unset %s', @id, @id))
#INTERP._eval(Kernel.format('unset %s', @id))
#INTERP._eval('unset ' + @id)
end
end
alias remove unset
end
def numeric
number(value)
end
def numeric=(val)
case val
when Numeric
self.value=(val)
when TkVariable
self.value=(val.numeric)
else
raise ArgumentError, "Numeric is expected"
end
val
end
def to_i
number(value).to_i
end
def to_f
number(value).to_f
end
def to_s
#string(value).to_s
value
end
def to_sym
value.intern
end
def list
#tk_split_list(value)
tk_split_simplelist(value)
end
alias to_a list
def list=(val)
case val
when Array
self.value=(val)
when TkVariable
self.value=(val.list)
else
raise ArgumentError, "Array is expected"
end
val
end
def inspect
#Kernel.format "#<TkVariable: %s>", @id
'#<TkVariable: ' + @id + '>'
end
def coerce(other)
case other
when TkVariable
[other.value, self.value]
when String
[other, self.to_s]
when Symbol
[other, self.to_sym]
when Integer
[other, self.to_i]
when Float
[other, self.to_f]
when Array
[other, self.to_a]
else
[other, self.value]
end
end
def &(other)
if other.kind_of?(Array)
self.to_a & other.to_a
else
self.to_i & other.to_i
end
end
def |(other)
if other.kind_of?(Array)
self.to_a | other.to_a
else
self.to_i | other.to_i
end
end
def +(other)
case other
when Array
self.to_a + other
when String
self.value + other
else
begin
number(self.value) + other
rescue
self.value + other.to_s
end
end
end
def -(other)
if other.kind_of?(Array)
self.to_a - other
else
number(self.value) - other
end
end
def *(other)
begin
number(self.value) * other
rescue
self.value * other
end
end
def /(other)
number(self.value) / other
end
def %(other)
begin
number(self.value) % other
rescue
self.value % other
end
end
def **(other)
number(self.value) ** other
end
def =~(other)
self.value =~ other
end
def ==(other)
case other
when TkVariable
self.equal?(other)
when String
self.to_s == other
when Symbol
self.to_sym == other
when Integer
self.to_i == other
when Float
self.to_f == other
when Array
self.to_a == other
when Hash
self.value == other
else
false
end
end
def zero?
numeric.zero?
end
def nonzero?
!(numeric.zero?)
end
def <=>(other)
if other.kind_of?(TkVariable)
begin
val = other.numeric
other = val
rescue
other = other.value
end
end
if other.kind_of?(Numeric)
begin
return self.numeric <=> other
rescue
return self.value <=> other.to_s
end
else
return self.value <=> other
end
end
def to_eval
@id
end
def trace_callback(elem, op)
if @trace_var.kind_of? Array
@trace_var.each{|m,e| e.call(self,elem,op) if m.index(op)}
end
if elem.kind_of?(String) && elem != ''
if @trace_elem.kind_of?(Hash) && @trace_elem[elem].kind_of?(Array)
@trace_elem[elem].each{|m,e| e.call(self,elem,op) if m.index(op)}
end
end
end
def trace(opts, cmd = Proc.new)
@trace_var = [] if @trace_var == nil
opts = ['r','w','u'].find_all{|c| opts.index(c)}.join('')
@trace_var.unshift([opts,cmd])
if @trace_opts == nil
TkVar_CB_TBL[@id] = self
@trace_opts = opts.dup
Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, 'rb_var')
=begin
if /^(8\.([4-9]|[1-9][0-9])|9\.|[1-9][0-9])/ =~ Tk::TCL_VERSION
# TCL_VERSION >= 8.4
Tk.tk_call_without_enc('trace', 'add', 'variable',
@id, @trace_opts, 'rb_var')
else
# TCL_VERSION <= 8.3
Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, 'rb_var')
end
=end
else
newopts = @trace_opts.dup
opts.each_byte{|c| newopts += c.chr unless newopts.index(c)}
if newopts != @trace_opts
Tk.tk_call_without_enc('trace', 'vdelete', @id, @trace_opts, 'rb_var')
@trace_opts.replace(newopts)
Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, 'rb_var')
=begin
if /^(8\.([4-9]|[1-9][0-9])|9\.|[1-9][0-9])/ =~ Tk::TCL_VERSION
# TCL_VERSION >= 8.4
Tk.tk_call_without_enc('trace', 'remove', 'variable',
@id, @trace_opts, 'rb_var')
@trace_opts.replace(newopts)
Tk.tk_call_without_enc('trace', 'add', 'variable',
@id, @trace_opts, 'rb_var')
else
# TCL_VERSION <= 8.3
Tk.tk_call_without_enc('trace', 'vdelete',
@id, @trace_opts, 'rb_var')
@trace_opts.replace(newopts)
Tk.tk_call_without_enc('trace', 'variable',
@id, @trace_opts, 'rb_var')
end
=end
end
end
self
end
def trace_element(elem, opts, cmd = Proc.new)
@trace_elem = {} if @trace_elem == nil
@trace_elem[elem] = [] if @trace_elem[elem] == nil
opts = ['r','w','u'].find_all{|c| opts.index(c)}.join('')
@trace_elem[elem].unshift([opts,cmd])
if @trace_opts == nil
TkVar_CB_TBL[@id] = self
@trace_opts = opts.dup
Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, 'rb_var')
=begin
if /^(8\.([4-9]|[1-9][0-9])|9\.|[1-9][0-9])/ =~ Tk::TCL_VERSION
# TCL_VERSION >= 8.4
Tk.tk_call_without_enc('trace', 'add', 'variable',
@id, @trace_opts, 'rb_var')
else
# TCL_VERSION <= 8.3
Tk.tk_call_without_enc('trace', 'variable',
@id, @trace_opts, 'rb_var')
end
=end
else
newopts = @trace_opts.dup
opts.each_byte{|c| newopts += c.chr unless newopts.index(c)}
if newopts != @trace_opts
Tk.tk_call_without_enc('trace', 'vdelete', @id, @trace_opts, 'rb_var')
@trace_opts.replace(newopts)
Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, 'rb_var')
=begin
if /^(8\.([4-9]|[1-9][0-9])|9\.|[1-9][0-9])/ =~ Tk::TCL_VERSION
# TCL_VERSION >= 8.4
Tk.tk_call_without_enc('trace', 'remove', 'variable',
@id, @trace_opts, 'rb_var')
@trace_opts.replace(newopts)
Tk.tk_call_without_enc('trace', 'add', 'variable',
@id, @trace_opts, 'rb_var')
else
# TCL_VERSION <= 8.3
Tk.tk_call_without_enc('trace', 'vdelete',
@id, @trace_opts, 'rb_var')
@trace_opts.replace(newopts)
Tk.tk_call_without_enc('trace', 'variable',
@id, @trace_opts, 'rb_var')
end
=end
end
end
self
end
def trace_vinfo
return [] unless @trace_var
@trace_var.dup
end
def trace_vinfo_for_element(elem)
return [] unless @trace_elem
return [] unless @trace_elem[elem]
@trace_elem[elem].dup
end
def trace_vdelete(opts,cmd)
return self unless @trace_var.kind_of? Array
opts = ['r','w','u'].find_all{|c| opts.index(c)}.join('')
idx = -1
newopts = ''
@trace_var.each_with_index{|e,i|
if idx < 0 && e[0] == opts && e[1] == cmd
idx = i
next
end
e[0].each_byte{|c| newopts += c.chr unless newopts.index(c)}
}
if idx >= 0
@trace_var.delete_at(idx)
else
return self
end
@trace_elem.each{|elem|
@trace_elem[elem].each{|e|
e[0].each_byte{|c| newopts += c.chr unless newopts.index(c)}
}
}
newopts = ['r','w','u'].find_all{|c| newopts.index(c)}.join('')
if newopts != @trace_opts
Tk.tk_call_without_enc('trace', 'vdelete', @id, @trace_opts, 'rb_var')
=begin
if /^(8\.([4-9]|[1-9][0-9])|9\.|[1-9][0-9])/ =~ Tk::TCL_VERSION
# TCL_VERSION >= 8.4
Tk.tk_call_without_enc('trace', 'remove', 'variable',
@id, @trace_opts, 'rb_var')
else
# TCL_VERSION <= 8.3
Tk.tk_call_without_enc('trace', 'vdelete',
@id, @trace_opts, 'rb_var')
end
=end
@trace_opts.replace(newopts)
if @trace_opts != ''
Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, 'rb_var')
=begin
if /^(8\.([4-9]|[1-9][0-9])|9\.|[1-9][0-9])/ =~ Tk::TCL_VERSION
# TCL_VERSION >= 8.4
Tk.tk_call_without_enc('trace', 'add', 'variable',
@id, @trace_opts, 'rb_var')
else
# TCL_VERSION <= 8.3
Tk.tk_call_without_enc('trace', 'variable',
@id, @trace_opts, 'rb_var')
end
=end
end
end
self
end
def trace_vdelete_for_element(elem,opts,cmd)
return self unless @trace_elem.kind_of? Hash
return self unless @trace_elem[elem].kind_of? Array
opts = ['r','w','u'].find_all{|c| opts.index(c)}.join('')
idx = -1
@trace_elem[elem].each_with_index{|e,i|
if idx < 0 && e[0] == opts && e[1] == cmd
idx = i
next
end
}
if idx >= 0
@trace_elem[elem].delete_at(idx)
else
return self
end
newopts = ''
@trace_var.each{|e|
e[0].each_byte{|c| newopts += c.chr unless newopts.index(c)}
}
@trace_elem.each{|elem|
@trace_elem[elem].each{|e|
e[0].each_byte{|c| newopts += c.chr unless newopts.index(c)}
}
}
newopts = ['r','w','u'].find_all{|c| newopts.index(c)}.join('')
if newopts != @trace_opts
Tk.tk_call_without_enc('trace', 'vdelete', @id, @trace_opts, 'rb_var')
=begin
if /^(8\.([4-9]|[1-9][0-9])|9\.|[1-9][0-9])/ =~ Tk::TCL_VERSION
# TCL_VERSION >= 8.4
Tk.tk_call_without_enc('trace', 'remove', 'variable',
@id, @trace_opts, 'rb_var')
else
# TCL_VERSION <= 8.3
Tk.tk_call_without_enc('trace', 'vdelete',
@id, @trace_opts, 'rb_var')
end
=end
@trace_opts.replace(newopts)
if @trace_opts != ''
Tk.tk_call_without_enc('trace', 'variable', @id, @trace_opts, 'rb_var')
=begin
if /^(8\.([4-9]|[1-9][0-9])|9\.|[1-9][0-9])/ =~ Tk::TCL_VERSION
# TCL_VERSION >= 8.4
Tk.tk_call_without_enc('trace', 'add', 'variable',
@id, @trace_opts, 'rb_var')
else
# TCL_VERSION <= 8.3
Tk.tk_call_without_enc('trace', 'variable', @id,
@trace_opts, 'rb_var')
end
=end
end
end
self
end
end
class TkVarAccess<TkVariable
def self.new(name, *args)
return TkVar_ID_TBL[name] if TkVar_ID_TBL[name]
super(name, *args)
end
def initialize(varname, val=nil)
@id = varname
TkVar_ID_TBL[@id] = self
# teach Tk-ip that @id is global var
INTERP._invoke_without_enc('global', @id)
if val
#s = '"' + _get_eval_string(val).gsub(/[\[\]$"]/, '\\\\\&') + '"' #"
#s = '"' + _get_eval_string(val).gsub(/[\[\]$"\\]/, '\\\\\&') + '"' #"
#INTERP._eval(Kernel.format('global %s; set %s %s', @id, @id, s))
#INTERP._set_global_var(@id, _toUTF8(_get_eval_string(val)))
self.value = val
end
end
end
module Tk
begin
auto_path = INTERP._invoke('set', 'auto_path')
rescue
begin
auto_path = INTERP._invoke('set', 'env(TCLLIBPATH)')
rescue
auto_path = Tk::LIBRARY
end
end
AUTO_PATH = TkVarAccess.new('auto_path', auto_path)
=begin
AUTO_OLDPATH = tk_split_simplelist(INTERP._invoke('set', 'auto_oldpath'))
AUTO_OLDPATH.each{|s| s.freeze}
AUTO_OLDPATH.freeze
=end
TCL_PACKAGE_PATH = TkVarAccess.new('tcl_pkgPath')
PACKAGE_PATH = TCL_PACKAGE_PATH
TCL_LIBRARY_PATH = TkVarAccess.new('tcl_libPath')
LIBRARY_PATH = TCL_LIBRARY_PATH
TCL_PRECISION = TkVarAccess.new('tcl_precision')
end
|