1# -*- tcl -*-
2# ### ### ### ######### ######### #########
3## Terminal packages - ANSI - Control codes
4
5# ### ### ### ######### ######### #########
6## Requirements
7
8package require  term::send
9package require  term::ansi::code::ctrl
10
11namespace eval ::term::ansi::send {}
12
13# ### ### ### ######### ######### #########
14## Make command easily available
15
16proc ::term::ansi::send::import {{ns send} args} {
17    if {![llength $args]} {set args *}
18    set args ::term::ansi::send::[join $args " ::term::ansi::send::"]
19    uplevel 1 [list namespace eval ${ns} [linsert $args 0 namespace import]]
20    return
21}
22
23# ### ### ### ######### ######### #########
24## Internal - Setup.
25
26proc ::term::ansi::send::ChName {n} {
27    if {![string match *-* $n]} {
28	return ${n}ch
29    }
30    set nl   [split $n -]
31    set stem [lindex       $nl 0]
32    set sfx  [join [lrange $nl 1 end] -]
33    return ${stem}ch-$sfx
34}
35
36proc ::term::ansi::send::Args {n -> arv achv avv} {
37    upvar 1 $arv a $achv ach $avv av
38    set code ::term::ansi::code::ctrl::$n
39    set a   [info args $code]
40    set ach [linsert $a 0 ch]
41    set av  [expr {
42	[llength $a]
43	? " \$[join $a " \$"]"
44	: $a
45    }]
46    return $code
47}
48
49proc ::term::ansi::send::INIT {} {
50    foreach n [::term::ansi::code::ctrl::names] {
51	set nch  [ChName $n]
52	set code [Args $n -> a ach av]
53
54	if {[string equal [lindex $a end] args]} {
55	    # An args argument requires more care, and an eval
56	    set av [lrange $av 0 end-1]
57	    if {![string equal $av ""]} {set av " $av"}
58	    set gen "eval \[linsert \$args 0 $code$av\]"
59
60	    #8.5: set gen "$code$av \{expand\}\$args"
61	} else {
62	    set gen $code$av
63	}
64
65	proc $n   $a   "wr        \[$gen\]" ; namespace export $n
66	proc $nch $ach "wrch \$ch \[$gen\]" ; namespace export $nch
67    }
68    return
69}
70
71namespace eval ::term::ansi::send {
72    namespace import ::term::send::wr
73    namespace import ::term::send::wrch
74    namespace export wr wrch
75}
76
77::term::ansi::send::INIT
78
79# ### ### ### ######### ######### #########
80## Ready
81
82package provide term::ansi::send 0.1
83
84##
85# ### ### ### ######### ######### #########
86