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
16 (Tk_Namespace_ID = ["ns".freeze, "00000".taint]).instance_eval{
18 def mutex; @mutex; end
22 Tk_NsCode_RetObjID_TBL = TkCore::INTERP.create_table
24 TkCore::INTERP.init_ip_env{
25 Tk_Namespace_ID_TBL.mutex.synchronize{ Tk_Namespace_ID_TBL.clear }
26 Tk_NsCode_RetObjID_TBL.mutex.synchronize{ Tk_NsCode_RetObjID_TBL.clear }
29 def TkNamespace.id2obj(id)
30 Tk_Namespace_ID_TBL.mutex.synchronize{
31 Tk_Namespace_ID_TBL[id]? Tk_Namespace_ID_TBL[id]: id
35 #####################################
37 class Ensemble < TkObject
39 ['namespace', 'ensemble', 'configure', self.path]
44 ['namespace', 'ensemble', 'configure', self.path]
48 def __configinfo_struct
49 {:key=>0, :alias=>nil, :db_name=>nil, :db_class=>nil,
50 :default_value=>nil, :current_value=>2}
52 private :__configinfo_struct
57 private :__boolval_optkeys
60 ['map', 'subcommands', 'unknown']
62 private :__listval_optkeys
64 def self.exist?(ensemble)
65 bool(tk_call('namespace', 'ensemble', 'exists', ensemble))
68 def initialize(keys = {})
69 @ensemble = @path = tk_call('namespace', 'ensemble', 'create', keys)
73 if slot == :namespace || slot == 'namespace'
75 Tk_Namespace_ID_TBL.mutex.synchronize{
76 if TkNamespace::Tk_Namespace_ID_TBL.key?(ns)
77 TkNamespace::Tk_Namespace_ID_TBL[ns]
87 if slot == :namespace || slot == 'namespace'
89 Tk_Namespace_ID_TBL.mutex.synchronize{
90 if TkNamespace::Tk_Namespace_ID_TBL.key?(ns)
91 TkNamespace::Tk_Namespace_ID_TBL[ns]
101 def configinfo(slot = nil)
103 if slot == :namespace || slot == 'namespace'
105 Tk_Namespace_ID_TBL.mutex.synchronize{
106 if TkNamespace::Tk_Namespace_ID_TBL.key?(val)
107 val = TkNamespace::Tk_Namespace_ID_TBL[val]
114 if TkComm::GET_CONFIGINFO_AS_ARRAY
116 else # ! TkComm::GET_CONFIGINFO_AS_ARRAY
123 if TkComm::GET_CONFIGINFO_AS_ARRAY
124 Tk_Namespace_ID_TBL.mutex.synchronize{
126 if inf[0] == 'namespace' &&
127 TkNamespace::Tk_Namespace_ID_TBL.key?(inf[-1])
128 [inf[0], TkNamespace::Tk_Namespace_ID_TBL[inf[-1]]]
134 else # ! TkComm::GET_CONFIGINFO_AS_ARRAY
135 val = info['namespace']
136 Tk_Namespace_ID_TBL.mutex.synchronize{
137 if TkNamespace::Tk_Namespace_ID_TBL.key?(val)
138 info['namespace'] = TkNamespace::Tk_Namespace_ID_TBL[val]
148 bool(tk_call('namespace', 'ensemble', 'exists', @path))
152 #####################################
154 class ScopeArgs < Array
157 # alias __tk_call tk_call
158 # alias __tk_call_without_enc tk_call_without_enc
159 # alias __tk_call_with_enc tk_call_with_enc
161 #super('namespace', 'eval', @namespace, *args)
162 args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
163 super('namespace', 'eval', @namespace,
164 TkCore::INTERP._merge_tklist(*args))
166 def tk_call_without_enc(*args)
167 #super('namespace', 'eval', @namespace, *args)
168 args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
169 super('namespace', 'eval', @namespace,
170 TkCore::INTERP._merge_tklist(*args))
172 def tk_call_with_enc(*args)
173 #super('namespace', 'eval', @namespace, *args)
174 args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
175 super('namespace', 'eval', @namespace,
176 TkCore::INTERP._merge_tklist(*args))
179 def initialize(namespace, *args)
180 @namespace = namespace
186 #####################################
188 class NsCode < TkObject
189 def initialize(scope, use_obj_id = false)
191 @use_obj_id = use_obj_id
200 ret = TkCore::INTERP._eval_without_enc(@scope + array2tk_list(args))
202 ret = TkNamespace::Tk_NsCode_RetObjID_TBL.delete(ret.to_i)
208 #####################################
211 lst = tk_split_simplelist(super(cmd), false, false)
215 lst.insert(1, @fullname)
217 TkCore::INTERP._merge_tklist(*lst)
220 alias __tk_call tk_call
221 alias __tk_call_without_enc tk_call_without_enc
222 alias __tk_call_with_enc tk_call_with_enc
224 #super('namespace', 'eval', @fullname, *args)
225 args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
226 super('namespace', 'eval', @fullname,
227 TkCore::INTERP._merge_tklist(*args))
229 def tk_call_without_enc(*args)
230 #super('namespace', 'eval', @fullname, *args)
231 args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
232 super('namespace', 'eval', @fullname,
233 TkCore::INTERP._merge_tklist(*args))
235 def tk_call_with_enc(*args)
236 #super('namespace', 'eval', @fullname, *args)
237 args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
238 super('namespace', 'eval', @fullname,
239 TkCore::INTERP._merge_tklist(*args))
241 alias ns_tk_call tk_call
242 alias ns_tk_call_without_enc tk_call_without_enc
243 alias ns_tk_call_with_enc tk_call_with_enc
245 def initialize(name = nil, parent = nil)
247 Tk_Namespace_ID.mutex.synchronize{
248 # name = Tk_Namespace_ID.join('')
249 name = Tk_Namespace_ID.join(TkCore::INTERP._ip_id_)
250 Tk_Namespace_ID[1].succ!
253 name = __tk_call('namespace', 'current') if name == ''
257 @fullname = parent + name
259 @fullname = parent +'::'+ name
262 ancestor = __tk_call('namespace', 'current')
263 ancestor = '' if ancestor == '::'
265 @fullname = ancestor + '::' + parent + name
267 @fullname = ancestor + '::'+ parent +'::'+ name
271 ancestor = __tk_call('namespace', 'current')
272 ancestor = '' if ancestor == '::'
276 @fullname = ancestor + '::' + name
280 @parent = __tk_call('namespace', 'qualifiers', @fullname)
281 @name = __tk_call('namespace', 'tail', @fullname)
284 __tk_call('namespace', 'eval', @fullname, '')
286 Tk_Namespace_ID_TBL.mutex.synchronize{
287 Tk_Namespace_ID_TBL[@fullname] = self
291 def self.children(*args)
292 # args ::= [<namespace>] [<pattern>]
293 # <pattern> must be glob-style pattern
294 tk_split_simplelist(tk_call('namespace', 'children', *args)).collect{|ns|
296 Tk_Namespace_ID_TBL.mutex.synchronize{
297 if Tk_Namespace_ID_TBL.key?(ns)
298 Tk_Namespace_ID_TBL[ns]
305 def children(pattern=None)
306 TkNamespace.children(@fullname, pattern)
309 def self.code(script = Proc.new)
310 TkNamespace.new('').code(script)
313 def code(script = Proc.new)
314 if script.kind_of?(String)
315 cmd = proc{|*args| ScopeArgs.new(@fullname,*args).instance_eval(script)}
316 elsif script.kind_of?(Proc)
317 cmd = proc{|*args| ScopeArgs.new(@fullname,*args).instance_eval(&script)}
319 fail ArgumentError, "String or Proc is expected"
321 TkNamespace::NsCode.new(tk_call_without_enc('namespace', 'code',
322 _get_eval_string(cmd, false)))
325 def code(script = Proc.new)
326 if script.kind_of?(String)
328 if TkCore::WITH_RUBY_VM ### Ruby 1.9 !!!!
329 obj = ScopeArgs.new(@fullname,*args)
330 ret = obj.instance_exec(obj, script)
332 ret = ScopeArgs.new(@fullname,*args).instance_eval(script)
335 TkNamespace::Tk_NsCode_RetObjID_TBL[id] = ret
338 elsif script.kind_of?(Proc)
340 if TkCore::WITH_RUBY_VM ### Ruby 1.9 !!!!
341 obj = ScopeArgs.new(@fullname,*args)
342 ret = obj.instance_exec(obj, &script)
344 ret = ScopeArgs.new(@fullname,*args).instance_eval(&script)
347 TkNamespace::Tk_NsCode_RetObjID_TBL[id] = ret
351 fail ArgumentError, "String or Proc is expected"
353 TkNamespace::NsCode.new(tk_call_without_enc('namespace', 'code',
354 _get_eval_string(cmd, false)),
358 def self.current_path
359 tk_call('namespace', 'current')
366 ns = self.current_path
367 Tk_Namespace_ID_TBL.mutex.synchronize{
368 if Tk_Namespace_ID_TBL.key?(ns)
369 Tk_Namespace_ID_TBL[ns]
375 def current_namespace
376 # ns_tk_call('namespace', 'current')
380 alias current current_namespace
382 def self.delete(*ns_list)
383 tk_call('namespace', 'delete', *ns_list)
385 Tk_Namespace_ID_TBL.mutex.synchronize{
386 if ns.kind_of?(TkNamespace)
387 Tk_Namespace_ID_TBL.delete(ns.path)
389 Tk_Namespace_ID_TBL.delete(ns.to_s)
395 TkNamespece.delete(@fullname)
398 def self.ensemble_create(*keys)
399 tk_call('namespace', 'ensemble', 'create', *hash_kv(keys))
401 def self.ensemble_configure(cmd, slot, value=None)
402 if slot.kind_of?(Hash)
403 tk_call('namespace', 'ensemble', 'configure', cmd, *hash_kv(slot))
405 tk_call('namespace', 'ensemble', 'configure', cmd, '-'+slot.to_s, value)
408 def self.ensemble_configinfo(cmd, slot = nil)
410 tk_call('namespace', 'ensemble', 'configure', cmd, '-' + slot.to_s)
413 Hash(*tk_split_simplelist(tk_call('namespace', 'ensemble', 'configure', cmd))).each{|k, v| inf[k[1..-1]] = v}
417 def self.ensemble_exist?(cmd)
418 bool(tk_call('namespace', 'ensemble', 'exists', cmd))
421 def self.eval(namespace, cmd = Proc.new, *args)
422 #tk_call('namespace', 'eval', namespace, cmd, *args)
423 TkNamespace.new(namespace).eval(cmd, *args)
426 def eval(cmd = Proc.new, *args)
427 #TkNamespace.eval(@fullname, cmd, *args)
428 #ns_tk_call(cmd, *args)
430 ret = code_obj.call(*args)
431 # uninstall_cmd(TkCore::INTERP._split_tklist(code_obj.path)[-1])
432 uninstall_cmd(_fromUTF8(TkCore::INTERP._split_tklist(_toUTF8(code_obj.path))[-1]))
436 def eval(cmd = Proc.new, *args)
438 ret = code_obj.call(*args)
439 uninstall_cmd(_fromUTF8(TkCore::INTERP._split_tklist(_toUTF8(code_obj.path))[-1]))
444 bool(tk_call('namespace', 'exists', ns))
447 TkNamespece.exist?(@fullname)
450 def self.export(*patterns)
451 tk_call('namespace', 'export', *patterns)
453 def self.export_with_clear(*patterns)
454 tk_call('namespace', 'export', '-clear', *patterns)
457 TkNamespace.export(@fullname)
459 def export_with_clear
460 TkNamespace.export_with_clear(@fullname)
463 def self.forget(*patterns)
464 tk_call('namespace', 'forget', *patterns)
467 TkNamespace.forget(@fullname)
470 def self.import(*patterns)
471 tk_call('namespace', 'import', *patterns)
473 def self.force_import(*patterns)
474 tk_call('namespace', 'import', '-force', *patterns)
477 TkNamespace.import(@fullname)
480 TkNamespace.force_import(@fullname)
483 def self.inscope(namespace, script, *args)
484 tk_call('namespace', 'inscope', namespace, script, *args)
486 def inscope(script, *args)
487 TkNamespace.inscope(@fullname, script, *args)
491 tk_call('namespace', 'origin', cmd)
494 def self.parent(namespace=None)
495 ns = tk_call('namespace', 'parent', namespace)
496 Tk_Namespace_ID_TBL.mutex.synchronize{
497 if Tk_Namespace_ID_TBL.key?(ns)
498 Tk_Namespace_ID_TBL[ns]
505 tk_call('namespace', 'parent', @fullname)
509 tk_call('namespace', 'path')
511 def self.set_path(*namespace_list)
512 tk_call('namespace', 'path', array2tk_list(namespace_list))
515 tk_call('namespace', 'path', @fullname)
518 def self.qualifiers(str)
519 tk_call('namespace', 'qualifiers', str)
523 tk_call('namespace', 'tail', str)
526 def self.upvar(namespace, *var_pairs)
527 tk_call('namespace', 'upvar', namespace, *(var_pairs.flatten))
529 def upvar(*var_pairs)
530 TkNamespace.inscope(@fullname, *(var_pairs.flatten))
533 def self.get_unknown_handler
534 tk_tcl2ruby(tk_call('namespace', 'unknown'))
536 def self.set_unknown_handler(cmd = Proc.new)
537 tk_call('namespace', 'unknown', cmd)
541 tk_call('namespace', 'which', name)
543 def self.which_command(name)
544 tk_call('namespace', 'which', '-command', name)
546 def self.which_variable(name)
547 tk_call('namespace', 'which', '-variable', name)
551 TkNamespace::Global = TkNamespace.new('::')