2
# tk/namespace.rb : methods to manipulate Tcl/Tk namespace
3
# by Hidetoshi Nagai <nagai@ai.kyutech.ac.jp>
7
class TkNamespace < TkObject
14
Tk_Namespace_ID_TBL = TkCore::INTERP.create_table
15
Tk_Namespace_ID = ["ns".freeze, "00000".taint].freeze
17
Tk_NsCode_RetObjID_TBL = TkCore::INTERP.create_table
19
TkCore::INTERP.init_ip_env{
20
Tk_Namespace_ID_TBL.clear
21
Tk_NsCode_RetObjID_TBL.clear
24
def TkNamespace.id2obj(id)
25
Tk_Namespace_ID_TBL[id]? Tk_Namespace_ID_TBL[id]: id
28
#####################################
30
class Ensemble < TkObject
32
['namespace', 'ensemble', 'configure', self.path]
37
['namespace', 'ensemble', 'configure', self.path]
41
def __configinfo_struct
42
{:key=>0, :alias=>nil, :db_name=>nil, :db_class=>nil,
43
:default_value=>nil, :current_value=>2}
45
private :__configinfo_struct
50
private :__boolval_optkeys
53
['map', 'subcommands', 'unknown']
55
private :__listval_optkeys
57
def self.exist?(ensemble)
58
bool(tk_call('namespace', 'ensemble', 'exists', ensemble))
61
def initialize(keys = {})
62
@ensemble = @path = tk_call('namespace', 'ensemble', 'create', keys)
66
if slot == :namespace || slot == 'namespace'
68
if TkNamespace::Tk_Namespace_ID_TBL.key?(ns)
69
TkNamespace::Tk_Namespace_ID_TBL[ns]
78
def configinfo(slot = nil)
80
if slot == :namespace || slot == 'namespace'
82
if TkNamespace::Tk_Namespace_ID_TBL.key?(val)
83
val = TkNamespace::Tk_Namespace_ID_TBL[val]
89
if TkComm::GET_CONFIGINFO_AS_ARRAY
91
else # ! TkComm::GET_CONFIGINFO_AS_ARRAY
98
if TkComm::GET_CONFIGINFO_AS_ARRAY
100
if inf[0] == 'namespace' &&
101
TkNamespace::Tk_Namespace_ID_TBL.key?(inf[-1])
102
[inf[0], TkNamespace::Tk_Namespace_ID_TBL[inf[-1]]]
107
else # ! TkComm::GET_CONFIGINFO_AS_ARRAY
108
val = info['namespace']
109
if TkNamespace::Tk_Namespace_ID_TBL.key?(val)
110
info['namespace'] = TkNamespace::Tk_Namespace_ID_TBL[val]
119
bool(tk_call('namespace', 'ensemble', 'exists', @path))
123
#####################################
125
class ScopeArgs < Array
128
# alias __tk_call tk_call
129
# alias __tk_call_without_enc tk_call_without_enc
130
# alias __tk_call_with_enc tk_call_with_enc
132
#super('namespace', 'eval', @namespace, *args)
133
args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
134
super('namespace', 'eval', @namespace,
135
TkCore::INTERP._merge_tklist(*args))
137
def tk_call_without_enc(*args)
138
#super('namespace', 'eval', @namespace, *args)
139
args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
140
super('namespace', 'eval', @namespace,
141
TkCore::INTERP._merge_tklist(*args))
143
def tk_call_with_enc(*args)
144
#super('namespace', 'eval', @namespace, *args)
145
args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
146
super('namespace', 'eval', @namespace,
147
TkCore::INTERP._merge_tklist(*args))
150
def initialize(namespace, *args)
151
@namespace = namespace
157
#####################################
159
class NsCode < TkObject
160
def initialize(scope, use_obj_id = false)
162
@use_obj_id = use_obj_id
171
ret = TkCore::INTERP._eval_without_enc(@scope + array2tk_list(args))
173
ret = TkNamespace::Tk_NsCode_RetObjID_TBL.delete(ret.to_i)
179
#####################################
182
lst = tk_split_simplelist(super(cmd), false, false)
186
lst.insert(1, @fullname)
188
TkCore::INTERP._merge_tklist(*lst)
191
alias __tk_call tk_call
192
alias __tk_call_without_enc tk_call_without_enc
193
alias __tk_call_with_enc tk_call_with_enc
195
#super('namespace', 'eval', @fullname, *args)
196
args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
197
super('namespace', 'eval', @fullname,
198
TkCore::INTERP._merge_tklist(*args))
200
def tk_call_without_enc(*args)
201
#super('namespace', 'eval', @fullname, *args)
202
args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
203
super('namespace', 'eval', @fullname,
204
TkCore::INTERP._merge_tklist(*args))
206
def tk_call_with_enc(*args)
207
#super('namespace', 'eval', @fullname, *args)
208
args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
209
super('namespace', 'eval', @fullname,
210
TkCore::INTERP._merge_tklist(*args))
212
alias ns_tk_call tk_call
213
alias ns_tk_call_without_enc tk_call_without_enc
214
alias ns_tk_call_with_enc tk_call_with_enc
216
def initialize(name = nil, parent = nil)
218
# name = Tk_Namespace_ID.join('')
219
name = Tk_Namespace_ID.join(TkCore::INTERP._ip_id_)
220
Tk_Namespace_ID[1].succ!
222
name = __tk_call('namespace', 'current') if name == ''
226
@fullname = parent + name
228
@fullname = parent +'::'+ name
231
ancestor = __tk_call('namespace', 'current')
232
ancestor = '' if ancestor == '::'
234
@fullname = ancestor + '::' + parent + name
236
@fullname = ancestor + '::'+ parent +'::'+ name
240
ancestor = __tk_call('namespace', 'current')
241
ancestor = '' if ancestor == '::'
245
@fullname = ancestor + '::' + name
249
@parent = __tk_call('namespace', 'qualifiers', @fullname)
250
@name = __tk_call('namespace', 'tail', @fullname)
253
__tk_call('namespace', 'eval', @fullname, '')
255
Tk_Namespace_ID_TBL[@fullname] = self
258
def self.children(*args)
259
# args ::= [<namespace>] [<pattern>]
260
# <pattern> must be glob-style pattern
261
tk_split_simplelist(tk_call('namespace', 'children', *args)).collect{|ns|
263
if Tk_Namespace_ID_TBL.key?(ns)
264
Tk_Namespace_ID_TBL[ns]
270
def children(pattern=None)
271
TkNamespace.children(@fullname, pattern)
274
def self.code(script = Proc.new)
275
TkNamespace.new('').code(script)
278
def code(script = Proc.new)
279
if script.kind_of?(String)
280
cmd = proc{|*args| ScopeArgs.new(@fullname,*args).instance_eval(script)}
281
elsif script.kind_of?(Proc)
282
cmd = proc{|*args| ScopeArgs.new(@fullname,*args).instance_eval(&script)}
284
fail ArgumentError, "String or Proc is expected"
286
TkNamespace::NsCode.new(tk_call_without_enc('namespace', 'code',
287
_get_eval_string(cmd, false)))
290
def code(script = Proc.new)
291
if script.kind_of?(String)
293
ret = ScopeArgs.new(@fullname,*args).instance_eval(script)
295
TkNamespace::Tk_NsCode_RetObjID_TBL[id] = ret
298
elsif script.kind_of?(Proc)
300
ret = ScopeArgs.new(@fullname,*args).instance_eval(&script)
302
TkNamespace::Tk_NsCode_RetObjID_TBL[id] = ret
306
fail ArgumentError, "String or Proc is expected"
308
TkNamespace::NsCode.new(tk_call_without_enc('namespace', 'code',
309
_get_eval_string(cmd, false)),
313
def self.current_path
314
tk_call('namespace', 'current')
321
ns = self.current_path
322
if Tk_Namespace_ID_TBL.key?(ns)
323
Tk_Namespace_ID_TBL[ns]
328
def current_namespace
329
# ns_tk_call('namespace', 'current')
333
alias current current_namespace
335
def self.delete(*ns_list)
336
tk_call('namespace', 'delete', *ns_list)
338
if ns.kind_of?(TkNamespace)
339
Tk_Namespace_ID_TBL.delete(ns.path)
341
Tk_Namespace_ID_TBL.delete(ns.to_s)
346
TkNamespece.delete(@fullname)
349
def self.ensemble_create(*keys)
350
tk_call('namespace', 'ensemble', 'create', *hash_kv(keys))
352
def self.ensemble_configure(cmd, slot, value=None)
353
if slot.kind_of?(Hash)
354
tk_call('namespace', 'ensemble', 'configure', cmd, *hash_kv(slot))
356
tk_call('namespace', 'ensemble', 'configure', cmd, '-'+slot.to_s, value)
359
def self.ensemble_configinfo(cmd, slot = nil)
361
tk_call('namespace', 'ensemble', 'configure', cmd, '-' + slot.to_s)
364
Hash(*tk_split_simplelist(tk_call('namespace', 'ensemble', 'configure', cmd))).each{|k, v| inf[k[1..-1]] = v}
368
def self.ensemble_exist?(cmd)
369
bool(tk_call('namespace', 'ensemble', 'exists', cmd))
372
def self.eval(namespace, cmd = Proc.new, *args)
373
#tk_call('namespace', 'eval', namespace, cmd, *args)
374
TkNamespace.new(namespece).eval(cmd, *args)
377
def eval(cmd = Proc.new, *args)
378
#TkNamespace.eval(@fullname, cmd, *args)
379
#ns_tk_call(cmd, *args)
381
ret = code_obj.call(*args)
382
# uninstall_cmd(TkCore::INTERP._split_tklist(code_obj.path)[-1])
383
uninstall_cmd(_fromUTF8(TkCore::INTERP._split_tklist(_toUTF8(code_obj.path))[-1]))
387
def eval(cmd = Proc.new, *args)
389
ret = code_obj.call(*args)
390
uninstall_cmd(_fromUTF8(TkCore::INTERP._split_tklist(_toUTF8(code_obj.path))[-1]))
395
bool(tk_call('namespace', 'exists', ns))
398
TkNamespece.exist?(@fullname)
401
def self.export(*patterns)
402
tk_call('namespace', 'export', *patterns)
404
def self.export_with_clear(*patterns)
405
tk_call('namespace', 'export', '-clear', *patterns)
408
TkNamespace.export(@fullname)
410
def export_with_clear
411
TkNamespace.export_with_clear(@fullname)
414
def self.forget(*patterns)
415
tk_call('namespace', 'forget', *patterns)
418
TkNamespace.forget(@fullname)
421
def self.import(*patterns)
422
tk_call('namespace', 'import', *patterns)
424
def self.force_import(*patterns)
425
tk_call('namespace', 'import', '-force', *patterns)
428
TkNamespace.import(@fullname)
431
TkNamespace.force_import(@fullname)
434
def self.inscope(namespace, script, *args)
435
tk_call('namespace', 'inscope', namespace, script, *args)
437
def inscope(script, *args)
438
TkNamespace.inscope(@fullname, script, *args)
442
tk_call('namespace', 'origin', cmd)
445
def self.parent(namespace=None)
446
ns = tk_call('namespace', 'parent', namespace)
447
if Tk_Namespace_ID_TBL.key?(ns)
448
Tk_Namespace_ID_TBL[ns]
454
tk_call('namespace', 'parent', @fullname)
458
tk_call('namespace', 'path')
460
def self.set_path(*namespace_list)
461
tk_call('namespace', 'path', array2tk_list(namespace_list))
464
tk_call('namespace', 'path', @fullname)
467
def self.qualifiers(str)
468
tk_call('namespace', 'qualifiers', str)
472
tk_call('namespace', 'tail', str)
475
def self.upvar(namespace, *var_pairs)
476
tk_call('namespace', 'upvar', namespace, *(var_pairs.flatten))
478
def upvar(*var_pairs)
479
TkNamespace.inscope(@fullname, *(var_pairs.flatten))
482
def self.get_unknown_handler
483
tk_tcl2ruby(tk_call('namespace', 'unknown'))
485
def self.set_unknown_handler(cmd = Proc.new)
486
tk_call('namespace', 'unknown', cmd)
490
tk_call('namespace', 'which', name)
492
def self.which_command(name)
493
tk_call('namespace', 'which', '-command', name)
495
def self.which_variable(name)
496
tk_call('namespace', 'which', '-variable', name)
500
TkNamespace::Global = TkNamespace.new('::')