/usr/lib/ruby/2.0.0/tk/variable.rb is in ruby2.0-tcltk 2.0.0.484-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 | #
# 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, TkUtil.untrust("00000")]).instance_eval{
@mutex = Mutex.new
def mutex; @mutex; end
freeze
}
TkCore::INTERP.init_ip_env{
TkVar_CB_TBL.mutex.synchronize{ TkVar_CB_TBL.clear }
TkVar_ID_TBL.mutex.synchronize{ TkVar_ID_TBL.clear }
}
major, minor, type, patchlevel = TclTkLib.get_version
USE_OLD_TRACE_OPTION_STYLE = (major < 8) || (major == 8 && minor < 4)
#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(id, name1, name2, op)
#name1,name2,op = tk_split_list(args)
#name1,name2,op = tk_split_simplelist(args)
if cb_obj = TkVar_CB_TBL[id]
#_get_eval_string(TkVar_CB_TBL[name1].trace_callback(name2,op))
begin
_get_eval_string(cb_obj.trace_callback(name2, op))
rescue SystemExit
exit(0)
rescue Interrupt
exit!(1)
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 >-------"
if TkCore::WITH_ENCODING
msg.force_encoding('utf-8')
else
msg.instance_variable_set(:@encoding, 'utf-8')
end
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 self.new_hash(val = {})
if val.kind_of?(Hash)
self.new(val)
else
fail ArgumentError, 'Hash is expected'
end
end
#
# default_value is available only when the variable is an assoc array.
#
def default_value(val=nil, &b)
if b
@def_default = :proc
@default_val = proc(&b)
else
@def_default = :val
@default_val = val
end
self
end
def set_default_value(val)
@def_default = :val
@default_val = val
self
end
alias default_value= set_default_value
def default_proc(cmd = Proc.new)
@def_default = :proc
@default_val = cmd
self
end
def undef_default
@default_val = nil
@def_default = false
self
end
def default_value_type
@type
end
def default_element_value_type(idxs)
if idxs.kind_of?(Array)
index = idxs.collect{|idx| _get_eval_string(idx, true)}.join(',')
else
index = _get_eval_string(idxs, true)
end
@element_type[index]
end
def _set_default_value_type_core(type, idxs)
if type.kind_of?(Class)
if type == NilClass
type = nil
elsif type == Numeric
type = :numeric
elsif type == TrueClass || type == FalseClass
type = :bool
elsif type == String
type = :string
elsif type == Symbol
type = :symbol
elsif type == Array
type = :list
elsif type <= TkVariable
type = :variable
elsif type <= TkWindow
type = :window
elsif TkComm._callback_entry_class?(type)
type = :procedure
else
type = nil
end
else
case(type)
when nil
type = nil
when :numeric, 'numeric'
type = :numeric
when true, false, :bool, 'bool'
type = :bool
when :string, 'string'
type = :string
when :symbol, 'symbol'
type = :symbol
when :list, 'list'
type = :list
when :numlist, 'numlist'
type = :numlist
when :variable, 'variable'
type = :variable
when :window, 'window'
type = :window
when :procedure, 'procedure'
type = :procedure
else
return _set_default_value_type_core(type.class, idxs)
end
end
if idxs
if idxs.kind_of?(Array)
index = idxs.collect{|idx| _get_eval_string(idx, true)}.join(',')
else
index = _get_eval_string(idxs, true)
end
@element_type[index] = type
else
@type = type
end
type
end
private :_set_default_value_type_core
def set_default_value_type(type)
_set_default_value_type_core(type, nil)
self
end
alias default_value_type= set_default_value_type
def set_default_element_value_type(idxs, type)
_set_default_value_type_core(type, idxs)
self
end
def _to_default_type(val, idxs = nil)
if idxs
if idxs.kind_of?(Array)
index = idxs.collect{|idx| _get_eval_string(idx, true)}.join(',')
else
index = _get_eval_string(idxs, true)
end
type = @element_type[index]
else
type = @type
end
return val unless type
if val.kind_of?(Hash)
val.keys.each{|k| val[k] = _to_default_type(val[k], idxs) }
val
else
begin
case(type)
when :numeric
number(val)
when :bool
TkComm.bool(val)
when :string
val
when :symbol
val.intern
when :list
tk_split_simplelist(val)
when :numlist
tk_split_simplelist(val).collect!{|v| number(v)}
when :variable
TkVarAccess.new(val)
when :window
TkComm.window(val)
when :procedure
TkComm.procedure(val)
else
val
end
rescue
val
end
end
end
private :_to_default_type
def _to_default_element_type(idxs, val)
_to_default_type(val, idxs)
end
private :_to_default_element_type
def initialize(val="", type=nil)
# @id = Tk_VARIABLE_ID.join('')
begin
Tk_VARIABLE_ID.mutex.synchronize{
@id = Tk_VARIABLE_ID.join(TkCore::INTERP._ip_id_)
Tk_VARIABLE_ID[1].succ!
}
end until INTERP._invoke_without_enc('info', 'globals', @id).empty?
TkVar_ID_TBL.mutex.synchronize{
TkVar_ID_TBL[@id] = self
}
@var = @id
@elem = nil
@def_default = false
@default_val = nil
@trace_var = nil
@trace_elem = nil
@trace_opts = nil
@type = nil
var = self
@element_type = Hash.new{|k,v| var.default_value_type }
self.default_value_type = type
# teach Tk-ip that @id is global var
INTERP._invoke_without_enc('global', @id)
#INTERP._invoke('global', @id)
# create and init
if val.kind_of?(Hash)
# assoc-array variable
self[''] = 0
self.clear
end
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 ref(*idxs)
# "#{@id}(#{idxs.collect{|idx| _get_eval_string(idx)}.join(',')})"
TkVarAccess.new("#{@id}(#{idxs.collect{|idx| _get_eval_string(idx)}.join(',')})")
end
def is_hash?
#ITNERP._eval("global #{@id}; array exist #{@id}") == '1'
INTERP._invoke_without_enc('global', @id)
# INTERP._invoke_without_enc('array', 'exist', @id) == '1'
TkComm.bool(INTERP._invoke_without_enc('array', 'exist', @id))
end
def is_scalar?
! is_hash?
end
def exist?(*elems)
INTERP._invoke_without_enc('global', @id)
if elems.empty?
TkComm.bool(tk_call('info', 'exist', @id))
else
# array
index = elems.collect{|idx| _get_eval_string(idx, true)}.join(',')
TkComm.bool(tk_call('info', 'exist', "#{@id}")) &&
TkComm.bool(tk_call('info', 'exist', "#{@id}(#{index})"))
end
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}"))
INTERP._invoke_without_enc('global', @id)
#tk_split_simplelist(INTERP._fromUTF8(INTERP._invoke_without_enc('array', 'names', @id)))
tk_split_simplelist(INTERP._invoke_without_enc('array', 'names', @id),
false, true)
end
def size
INTERP._invoke_without_enc('global', @id)
TkComm.number(INTERP._invoke_without_enc('array', 'size', @id))
end
def clear
if (is_scalar?)
fail RuntimeError, 'cannot clear a scalar variable'
end
keys.each{|k| unset(k)}
self
end
def update(hash)
if (is_scalar?)
fail RuntimeError, 'cannot update a scalar variable'
end
hash.each{|k,v| self[k] = v}
self
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'
INTERP._invoke_without_enc('global', @id)
# if INTERP._invoke('array', 'exist', @id) == '1'
if TkComm.bool(INTERP._invoke('array', 'exist', @id))
#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)
val = val._value if !@type && @type != :variable && val.kind_of?(TkVariable)
if val.kind_of?(Hash)
self.clear
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)
=begin
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
=end
# _fromUTF8(INTERP._set_global_var(@id, array2tk_list(val, true)))
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 _element_value(*idxs)
index = idxs.collect{|idx| _get_eval_string(idx, true)}.join(',')
begin
_fromUTF8(INTERP._get_global_var2(@id, index))
rescue => e
case @def_default
when :proc
@default_val.call(self, *idxs)
when :val
@default_val
else
fail e
end
end
#_fromUTF8(INTERP._get_global_var2(@id, 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 []=(*args)
val = args.pop
type = default_element_value_type(args)
val = val._value if !type && type != :variable && val.kind_of?(TkVariable)
index = args.collect{|idx| _get_eval_string(idx, true)}.join(',')
_fromUTF8(INTERP._set_global_var2(@id, index, _get_eval_string(val, true)))
#_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(*elems)
if elems.empty?
INTERP._unset_global_var(@id)
else
index = elems.collect{|idx| _get_eval_string(idx, true)}.join(',')
INTERP._unset_global_var2(@id, index)
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)
val = val._value if !@type && @type != :variable && val.kind_of?(TkVariable)
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, true))}
#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, true)}.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 _element_value(*idxs)
index = idxs.collect{|idx| _get_eval_string(idx)}.join(',')
begin
INTERP._eval(Kernel.format('global %s; set %s(%s)', @id, @id, index))
rescue => e
case @def_default
when :proc
@default_val.call(self, *idxs)
when :val
@default_val
else
fail e
end
end
#INTERP._eval(Kernel.format('global %s; set %s(%s)', @id, @id, 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 []=(*args)
val = args.pop
type = default_element_value_type(args)
val = val._value if !type && type != :variable && val.kind_of?(TkVariable)
index = args.collect{|idx| _get_eval_string(idx)}.join(',')
INTERP._eval(Kernel.format('global %s; set %s(%s) %s', @id, @id,
index, _get_eval_string(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(*elems)
if elems.empty?
INTERP._eval(Kernel.format('global %s; unset %s', @id, @id))
#INTERP._eval(Kernel.format('unset %s', @id))
#INTERP._eval('unset ' + @id)
else
index = elems.collect{|idx| _get_eval_string(idx, true)}.join(',')
INTERP._eval(Kernel.format('global %s; unset %s(%s)', @id, @id, index))
#INTERP._eval(Kernel.format('global %s; unset %s(%s)',
# @id, @id, _get_eval_string(elem)))
#INTERP._eval(Kernel.format('unset %s(%s)', @id, tk_tcl2ruby(elem)))
#INTERP._eval('unset ' + @id + '(' + _get_eval_string(elem) + ')')
end
end
alias remove unset
end
protected :_value, :_element_value
def value
_to_default_type(_value)
end
def [](*idxs)
_to_default_element_type(idxs, _element_value(*idxs))
end
def set_value(val)
self.value = val
self
end
def to_hash
hash = {}
self.keys.each{|k|
hash[k] = self[k]
}
hash
end
def set_element_value(idxs, val)
if idxs.kind_of?(Array)
self[*idxs]=val
else
self[idxs]=val
end
self
end
def set_value_type(val)
self.default_value_type = val.class
self.value = val
self
end
alias value_type= set_value_type
def set_element_value_type(idxs, val)
self.set_default_element_value_type(idxs, val.class)
if idxs.kind_of?(Array)
self[*idxs]=val
else
self[idxs]=val
end
self
end
def numeric
number(_value)
end
def numeric_element(*idxs)
number(_element_value(*idxs))
end
def set_numeric(val)
case val
when Numeric
self.value=(val)
when TkVariable
self.value=(val.numeric)
else
raise ArgumentError, "Numeric is expected"
end
self
end
alias numeric= set_numeric
def set_numeric_element(idxs, val)
case val
when Numeric
val
when TkVariable
val = val.numeric
else
raise ArgumentError, "Numeric is expected"
end
if idxs.kind_of?(Array)
self[*idxs]=val
else
self[idxs]=val
end
self
end
def set_numeric_type(val)
@type = :numeric
self.numeric=(val)
self
end
alias numeric_type= set_numeric_type
def set_numeric_element_type(idxs, val)
self.set_default_element_value_type(idxs, :numeric)
self.set_numeric_element(idxs, val)
end
def bool
TkComm.bool(_value)
=begin
# see Tcl_GetBoolean man-page
case _value.downcase
when '0', 'false', 'no', 'off'
false
else
true
end
=end
end
def bool_element(*idxs)
TkComm.bool(_element_value(*idxs))
end
def set_bool(val)
if ! val
self.value = '0'
else
case val.to_s.downcase
when 'false', '0', 'no', 'off'
self.value = '0'
else
self.value = '1'
end
end
self
end
alias bool= set_bool
def set_bool_element(idxs, val)
if ! val
val = '0'
else
case val.to_s.downcase
when 'false', '0', 'no', 'off'
val = '0'
else
val = '1'
end
end
if idxs.kind_of?(Array)
self[*idxs]=val
else
self[idxs]=val
end
self
end
def set_bool_type(val)
@type = :bool
self.bool=(val)
self
end
alias bool_type= set_bool_type
def set_bool_element_type(idxs, val)
self.set_default_element_value_type(idxs, :bool)
self.set_bool_element(idxs, val)
end
def variable
# keeps a Tcl's variable name
TkVarAccess.new(self._value)
end
def variable_element(*idxs)
TkVarAccess.new(_element_value(*idxs))
end
def set_variable(var)
var = var.id if var.kind_of?(TkVariable)
self.value = var
self
end
alias variable= set_variable
def set_variable_element(idxs, var)
var = var.id if var.kind_of?(TkVariable)
if idxs.kind_of?(Array)
self[*idxs]=var
else
self[idxs]=var
end
self
end
def set_variable_type(var)
@type = :variable
var = var.id if var.kind_of?(TkVariable)
self.value = var
self
end
alias variable_type= set_variable_type
def set_variable_element_type(idxs, var)
self.set_default_element_value_type(idxs, :variable)
self.set_variable_element(idxs, var)
end
def window
TkComm.window(self._value)
end
def window_element(*idxs)
TkComm.window(_element_value(*idxs))
end
def set_window(win)
win = win._value if win.kind_of?(TkVariable)
self.value = win
self
end
alias window= set_window
def set_window_element(idxs, win)
win = win._value if win.kind_of?(TkVariable)
if idxs.kind_of?(Array)
self[*idxs]=win
else
self[idxs]=win
end
self
end
def set_window_type(win)
@type = :window
self.window=(win)
self
end
alias window_type= set_window_type
def set_window_element_type(idxs, win)
self.set_default_element_value_type(idxs, :window)
self.set_window_element(idxs, win)
end
def procedure
TkComm.procedure(self._value)
end
def procedure_element(*idxs)
TkComm.procedure(_element_value(*idxs))
end
def set_procedure(cmd)
self.value = cmd
self
end
alias procedure= set_procedure
def set_procedure_element(idxs, cmd)
cmd = cmd._value if cmd.kind_of?(TkVariable)
if idxs.kind_of?(Array)
self[*idxs]=cmd
else
self[idxs]=cmd
end
self
end
def set_procedure_type(cmd)
@type = :procedure
self.procedure=(cmd)
self
end
alias procedure_type= set_procedure_type
def set_procedure_element_type(idxs, cmd)
self.set_default_element_value_type(idxs, :procedure)
self.set_proceure_element(idxs, cmd)
end
def to_proc
cmd = self.procedure
if cmd.respond_to?(:call)
cmd
else
# cmd is a String
cmd.to_sym.to_proc
end
end
def to_i
number(_value).to_i
end
alias to_int to_i
def element_to_i(*idxs)
number(_element_value(*idxs)).to_i
end
def to_f
number(_value).to_f
end
def element_to_f(*idxs)
number(_element_value(*idxs)).to_f
end
def to_s
#string(value).to_s
_value
end
alias string to_s
alias to_str to_s
def element_to_s(*idxs)
_element_value(*idxs)
end
def string_element(*idxs)
_element_value(*idxs)
end
def set_string(val)
val = val._value if val.kind_of?(TkVariable)
self.value=val
self
end
alias string= set_string
def set_string_element(idxs, val)
val = val._value if val.kind_of?(TkVariable)
if idxs.kind_of?(Array)
self[*idxs]=val
else
self[idxs]=val
end
self
end
def set_string_type(val)
@type = :string
self.string=(val)
self
end
alias string_type= set_string_type
def set_string_element_type(idxs, val)
self.set_default_element_value_type(idxs, :string)
self.set_string_element(idxs, val)
end
def to_sym
_value.intern
end
alias symbol to_sym
def element_to_sym(*idxs)
_element_value(*idxs).intern
end
alias symbol_element element_to_sym
def set_symbol(val)
val = val._value if val.kind_of?(TkVariable)
self.value=val
self
end
alias symbol= set_symbol
def set_symbol_element(idxs, val)
val = val._value if val.kind_of?(TkVariable)
if idxs.kind_of?(Array)
self[*idxs]=val
else
self[idxs]=val
end
self
end
def set_symbol_type(val)
@type = :symbol
self.value=(val)
self
end
alias symbol_type= set_symbol_type
def set_symbol_element_type(idxs, val)
self.set_default_element_value_type(idxs, :symbol)
self.set_symbol_element(idxs, val)
end
def list
#tk_split_list(value)
tk_split_simplelist(_value)
end
alias to_a list
alias to_ary list
def list_element(*idxs)
tk_split_simplelist(_element_value(*idxs))
end
alias element_to_a list_element
def numlist
list.collect!{|val| number(val)}
end
def numlist_element(*idxs)
list_element(*idxs).collect!{|val| number(val)}
end
def set_list(val)
case val
when Array
self.value=(val)
when TkVariable
self.value=(val.list)
else
raise ArgumentError, "Array is expected"
end
self
end
alias list= set_list
alias set_numlist set_list
alias numlist= set_numlist
def set_list_element(idxs, val)
case val
when Array
val
when TkVariable
val = val.list
else
raise ArgumentError, "Array is expected"
end
if idxs.kind_of?(Array)
self[*idxs]=val
else
self[idxs]=val
end
self
end
alias set_numlist_element set_list_element
def set_list_type(val)
@type = :list
self.list=(val)
self
end
alias list_type= set_list_type
def set_list_element_type(idxs, val)
self.set_default_element_value_type(idxs, :list)
self.set_list_element(idxs, val)
end
def set_numlist_type(val)
@type = :numlist
self.numlist=(val)
self
end
alias numlist_type= set_numlist_type
def set_numlist_element_type(idxs, val)
self.set_default_element_value_type(idxs, :numlist)
self.set_numlist_element(idxs, val)
end
def lappend(*elems)
tk_call('lappend', @id, *elems)
self
end
def element_lappend(idxs, *elems)
if idxs.kind_of?(Array)
idxs = idxs.collect{|idx| _get_eval_string(idx, true)}.join(',')
end
tk_call('lappend', "#{@id}(#{idxs})", *elems)
self
end
def lindex(idx)
tk_call('lindex', self._value, idx)
end
alias lget lindex
def element_lindex(elem_idxs, idx)
if elem_idxs.kind_of?(Array)
val = _element_value(*elem_idxs)
else
val = _element_value(elem_idxs)
end
tk_call('lindex', val, idx)
end
alias element_lget element_lindex
def lget_i(idx)
number(lget(idx)).to_i
end
def element_lget_i(elem_idxs, idx)
number(element_lget(elem_idxs, idx)).to_i
end
def lget_f(idx)
number(lget(idx)).to_f
end
def element_lget_f(elem_idxs, idx)
number(element_lget(elem_idxs, idx)).to_f
end
def lset(idx, val)
tk_call('lset', @id, idx, val)
self
end
def element_lset(elem_idxs, idx, val)
if elem_idxs.kind_of?(Array)
idxs = elem_idxs.collect{|i| _get_eval_string(i, true)}.join(',')
end
tk_call('lset', "#{@id}(#{idxs})", idx, val)
self
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 Numeric
[other, self.numeric]
when Array
[other, self.to_a]
else
[other, self._value]
end
end
def +@
self.numeric
end
def -@
-(self.numeric)
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)
num_or_str(self._value) * other
#begin
# number(self._value) * other
#rescue
# self._value * other
#end
end
def /(other)
number(self._value) / other
end
def %(other)
num_or_str(self._value) % 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)
self._value == other._value
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
# false if self is not an assoc array
self._value == other
else
# false
self._value == _get_eval_string(other)
end
end
def ===(other)
if other.kind_of?(TkVariable)
self.id == other.id
else
super
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
elsif other.kind_of?(Numeric)
begin
return self.numeric <=> other
rescue
return self._value <=> other.to_s
end
elsif other.kind_of?(Array)
return self.list <=> other
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 _check_trace_opt(opts)
if opts.kind_of?(Array)
opt_str = opts.map{|s| s.to_s}.join(' ')
else
opt_str = opts.to_s
end
fail ArgumentError, 'null trace option' if opt_str.empty?
if opt_str =~ /[^arwu\s]/
# new format (Tcl/Tk8.4+?)
if opts.kind_of?(Array)
opt_ary = opts.map{|opt| opt.to_s.strip}
else
opt_ary = opt_str.split(/\s+|\|/)
opt_ary.delete('')
end
if USE_OLD_TRACE_OPTION_STYLE
opt_ary.uniq.map{|opt|
case opt
when 'array'
'a'
when 'read'
'r'
when 'write'
'w'
when 'unset'
'u'
else
fail ArgumentError, "unsupported trace option '#{opt}' on Tcl/Tk#{Tk::TCL_PATCHLEVEL}"
end
}.join
else
opt_ary
end
else
# old format
opt_ary = opt_str.delete('^arwu').split(//).uniq
if USE_OLD_TRACE_OPTION_STYLE
opt_ary.join
else
opt_ary.map{|c|
case c
when 'a'
'array'
when 'r'
'read'
when 'w'
'write'
when 'u'
'unset'
end
}
end
end
end
private :_check_trace_opt
def trace(opts, cmd = Proc.new)
opts = _check_trace_opt(opts)
(@trace_var ||= []).unshift([opts,cmd])
if @trace_opts == nil
TkVar_CB_TBL[@id] = self
@trace_opts = opts.dup
if USE_OLD_TRACE_OPTION_STYLE
Tk.tk_call_without_enc('trace', 'variable',
@id, @trace_opts, 'rb_var ' << @id)
else
Tk.tk_call_without_enc('trace', 'add', 'variable',
@id, @trace_opts, 'rb_var ' << @id)
end
else
newopts = @trace_opts.dup
if USE_OLD_TRACE_OPTION_STYLE
opts.each_byte{|c| newopts.concat(c.chr) unless newopts.index(c.chr)}
if newopts != @trace_opts
Tk.tk_call_without_enc('trace', 'vdelete',
@id, @trace_opts, 'rb_var ' << @id)
@trace_opts.replace(newopts)
Tk.tk_call_without_enc('trace', 'variable',
@id, @trace_opts, 'rb_var ' << @id)
end
else
newopts |= opts
unless (newopts - @trace_opts).empty?
Tk.tk_call_without_enc('trace', 'remove', 'variable',
@id, @trace_opts, 'rb_var ' << @id)
@trace_opts.replace(newopts)
Tk.tk_call_without_enc('trace', 'add', 'variable',
@id, @trace_opts, 'rb_var ' << @id)
end
end
end
self
end
def trace_element(elem, opts, cmd = Proc.new)
if @elem
fail(RuntimeError,
"invalid for a TkVariable which denotes an element of Tcl's array")
end
opts = _check_trace_opt(opts)
((@trace_elem ||= {})[elem] ||= []).unshift([opts,cmd])
if @trace_opts == nil
TkVar_CB_TBL[@id] = self
@trace_opts = opts.dup
if USE_OLD_TRACE_OPTION_STYLE
Tk.tk_call_without_enc('trace', 'add', 'variable',
@id, @trace_opts, 'rb_var ' << @id)
else
Tk.tk_call_without_enc('trace', 'variable',
@id, @trace_opts, 'rb_var ' << @id)
end
else
newopts = @trace_opts.dup
if USE_OLD_TRACE_OPTION_STYLE
opts.each_byte{|c| newopts.concat(c.chr) unless newopts.index(c.chr)}
if newopts != @trace_opts
Tk.tk_call_without_enc('trace', 'vdelete',
@id, @trace_opts, 'rb_var ' << @id)
@trace_opts.replace(newopts)
Tk.tk_call_without_enc('trace', 'variable',
@id, @trace_opts, 'rb_var ' << @id)
end
else
newopts |= opts
unless (newopts - @trace_opts).empty?
Tk.tk_call_without_enc('trace', 'remove', 'variable',
@id, @trace_opts, 'rb_var ' << @id)
@trace_opts.replace(newopts)
Tk.tk_call_without_enc('trace', 'add', 'variable',
@id, @trace_opts, 'rb_var ' << @id)
end
end
end
self
end
def trace_info
return [] unless @trace_var
@trace_var.dup
end
alias trace_vinfo trace_info
def trace_info_for_element(elem)
if @elem
fail(RuntimeError,
"invalid for a TkVariable which denotes an element of Tcl's array")
end
return [] unless @trace_elem
return [] unless @trace_elem[elem]
@trace_elem[elem].dup
end
alias trace_vinfo_for_element trace_info_for_element
def trace_remove(opts,cmd)
return self unless @trace_var.kind_of? Array
opts = _check_trace_opt(opts)
idx = -1
if USE_OLD_TRACE_OPTION_STYLE
newopts = ''
@trace_var.each_with_index{|e, i|
if idx < 0 && e[1] == cmd
diff = false
['a', 'r', 'w', 'u'].each{|c|
break if (diff = e[0].index(c) ^ opts.index(c))
}
unless diff
#find
idx = i
next
end
end
e[0].each_byte{|c| newopts.concat(c.chr) unless newopts.index(c.chr)}
}
else
newopts = []
@trace_var.each_with_index{|e, i|
if idx < 0 && e[1] == cmd &&
e[0].size == opts.size && (e[0] - opts).empty?
# find
idx = i
next
end
newopts |= e[0]
}
end
if idx >= 0
@trace_var.delete_at(idx)
else
return self
end
(@trace_elem ||= {}).each{|elem|
@trace_elem[elem].each{|e|
if USE_OLD_TRACE_OPTION_STYLE
e[0].each_byte{|c| newopts.concat(c.chr) unless newopts.index(c.chr)}
else
newopts |= e[0]
end
}
}
if USE_OLD_TRACE_OPTION_STYLE
diff = false
@trace_opts.each_byte{|c| break if (diff = ! newopts.index(c))}
if diff
Tk.tk_call_without_enc('trace', 'vdelete',
@id, @trace_opts, 'rb_var ' << @id)
@trace_opts.replace(newopts)
unless @trace_opts.empty?
Tk.tk_call_without_enc('trace', 'variable',
@id, @trace_opts, 'rb_var ' << @id)
end
end
else
unless (@trace_opts - newopts).empty?
Tk.tk_call_without_enc('trace', 'remove', 'variable',
@id, @trace_opts, 'rb_var ' << @id)
@trace_opts.replace(newopts)
unless @trace_opts.empty?
Tk.tk_call_without_enc('trace', 'add', 'variable',
@id, @trace_opts, 'rb_var ' << @id)
end
end
end
self
end
alias trace_delete trace_remove
alias trace_vdelete trace_remove
def trace_remove_for_element(elem,opts,cmd)
if @elem
fail(RuntimeError,
"invalid for a TkVariable which denotes an element of Tcl's array")
end
return self unless @trace_elem.kind_of? Hash
return self unless @trace_elem[elem].kind_of? Array
opts = _check_trace_opt(opts)
idx = -1
if USE_OLD_TRACE_OPTION_STYLE
@trace_elem[elem].each_with_index{|e, i|
if idx < 0 && e[1] == cmd
diff = false
['a', 'r', 'w', 'u'].each{|c|
break if (diff = e[0].index(c) ^ opts.index(c))
}
unless diff
#find
idx = i
next
end
end
}
else
@trace_elem[elem].each_with_index{|e, i|
if idx < 0 && e[1] == cmd &&
e[0].size == opts.size && (e[0] - opts).empty?
# find
idx = i
next
end
}
end
if idx >= 0
@trace_elem[elem].delete_at(idx)
else
return self
end
if USE_OLD_TRACE_OPTION_STYLE
newopts = ''
@trace_var.each{|e|
e[0].each_byte{|c| newopts.concat(c.chr) unless newopts.index(c.chr)}
}
@trace_elem.each{|elem|
@trace_elem[elem].each{|e|
e[0].each_byte{|c| newopts.concat(c.chr) unless newopts.index(c.chr)}
}
}
else
newopts = []
@trace_var.each{|e|
newopts |= e[0]
}
@trace_elem.each{|elem|
@trace_elem[elem].each{|e|
e[0].each_byte{|c| newopts.concat(c.chr) unless newopts.index(c.chr)}
}
}
end
if USE_OLD_TRACE_OPTION_STYLE
diff = false
@trace_opts.each_byte{|c| break if (diff = ! newopts.index(c))}
if diff
Tk.tk_call_without_enc('trace', 'vdelete',
@id, @trace_opts, 'rb_var ' << @id)
@trace_opts.replace(newopts)
unless @trace_opts.empty?
Tk.tk_call_without_enc('trace', 'variable',
@id, @trace_opts, 'rb_var ' << @id)
end
end
else
unless (@trace_opts - newopts).empty?
Tk.tk_call_without_enc('trace', 'remove', 'variable',
@id, @trace_opts, 'rb_var ' << @id)
@trace_opts.replace(newopts)
unless @trace_opts.empty?
Tk.tk_call_without_enc('trace', 'add', 'variable',
@id, @trace_opts, 'rb_var ' << @id)
end
end
end
self
end
alias trace_delete_for_element trace_remove_for_element
alias trace_vdelete_for_element trace_remove_for_element
end
class TkVarAccess<TkVariable
def self.new(name, *args)
if name.kind_of?(TkVariable)
name.value = args[0] unless args.empty?
return name
end
name = name.to_s
v = nil
TkVar_ID_TBL.mutex.synchronize{
if v = TkVar_ID_TBL[name]
v.value = args[0] unless args.empty?
return v
else
(v = self.allocate).instance_eval{
@id = name
TkVar_ID_TBL[@id] = self
@var = @id
}
end
}
v.instance_eval{ initialize(name, *args) }
v
end
def self.new_hash(name, *args)
if name.kind_of?(TkVariable)
unless name.is_hash?
fail ArgumentError, "already exist as a scalar variable"
end
name.value = args[0] unless args.empty?
return name
end
name = name.to_s
v = nil
TkVar_ID_TBL.mutex.synchronize{
if v = TkVar_ID_TBL[name]
unless v.is_hash?
fail ArgumentError, "already exist as a scalar variable"
end
v.value = args[0] unless args.empty?
return v
else
(v = self.allocate).instance_eval{
@id = name
TkVar_ID_TBL[@id] = self
@var = @id
}
end
}
INTERP._invoke_without_enc('global', name)
if args.empty? && INTERP._invoke_without_enc('array', 'exist', name) == '0'
v.instance_eval{ initialize(name, {}) } # force creating
else
v.instance_eval{ initialize(name, *args) }
end
v
end
def initialize(varname, val=nil)
# @id = varname
# TkVar_ID_TBL[@id] = self
# @var = @id
@elem = nil
@def_default = false
@default_val = nil
@trace_var = nil
@trace_elem = nil
@trace_opts = nil
@type = nil
var = self
@element_type = Hash.new{|k,v| var.default_value_type }
# is an element?
if @id =~ /^([^(]+)\((.+)\)$/
# is an element --> var == $1, elem == $2
@var = $1
@elem = $2
end
# teach Tk-ip that @id is global var
INTERP._invoke_without_enc('global', @var)
=begin
begin
INTERP._invoke_without_enc('global', @id)
rescue => e
if @id =~ /^(.+)\([^()]+\)$/
# is an element --> varname == $1
INTERP._invoke_without_enc('global', $1)
else
fail e
end
end
=end
if val
if val.kind_of?(Hash)
# assoc-array variable
self[''] = 0
self.clear
end
#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
INTERP._invoke_without_enc('global', 'auto_path')
auto_path = INTERP._invoke('set', 'auto_path')
rescue => e
begin
INTERP._invoke_without_enc('global', 'env')
auto_path = INTERP._invoke('set', 'env(TCLLIBPATH)')
rescue => e
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
|