ctrl.tcl

Go to the documentation of this file.
00001 /*  -*- tcl -*-*/
00002 /*  ### ### ### ######### ######### #########*/
00003 /*  Terminal packages - ANSI - Control codes*/
00004 
00005 /*  References*/
00006 /*  [0] Google: ansi terminal control*/
00007 /*  [1] http://vt100.net/docs/vt100-ug/chapter3.html*/
00008 /*  [2] http://www.termsys.demon.co.uk/vtansi.htm*/
00009 /*  [3] http://rrbrandt.dyndns.org:60000/docs/tut/redes/ansi.php*/
00010 /*  [4] http://www.dee.ufcg.edu.br/~rrbrandt/tools/ansi.html*/
00011 /*  [5] http://www.ecma-international.org/publications/standards/Ecma-048.htm*/
00012 
00013 /*  ### ### ### ######### ######### #########*/
00014 /*  Requirements*/
00015 
00016 package require  term::ansi::code
00017 package require  term::ansi::code::attr
00018 
00019 namespace ::term::ansi::code::ctrl {}
00020 
00021 /*  ### ### ### ######### ######### #########*/
00022 /*  API. Symbolic names.*/
00023 
00024 ret  ::term::ansi::code::ctrl::names () {
00025     variable ctrl
00026     return  $ctrl
00027 }
00028 
00029 ret  ::term::ansi::code::ctrl::import (optional ns =ctrl , type args) {
00030     if {![llength $args]} {set args *}
00031     set args ::term::ansi::code::ctrl::[join $args " ::term::ansi::code::ctrl::"]
00032     uplevel 1 [list namespace eval ${ns} [linsert $args 0 namespace import]]
00033     return
00034 }
00035 
00036 /*  ### ### ### ######### ######### #########*/
00037 
00038 /*  TODO = symbolic key codes for skd.*/
00039 
00040 /*  ### ### ### ######### ######### #########*/
00041 /*  Internal - Setup*/
00042 
00043 ret  ::term::ansi::code::ctrl::DEF (type name , type esc , type value) {
00044     variable  ctrl
00045     define           $name $esc $value
00046     lappend   ctrl   $name
00047     namespace export $name
00048     return
00049 }
00050 
00051 ret  ::term::ansi::code::ctrl::DEFC (type name , type args , type script) {
00052     variable  ctrl
00053     proc             $name $args $script
00054     lappend   ctrl   $name
00055     namespace export $name
00056     return
00057 }
00058 
00059 ret  ::term::ansi::code::ctrl::INIT () {
00060     # ### ### ### ######### ######### #########
00061     ##
00062 
00063     # Erasing
00064 
00065     DEF eeol    escb K  ; # Erase (to) End Of Line
00066     DEF esol    escb 1K ; # Erase (to) Start Of Line
00067     DEF el      escb 2K ; # Erase (current) Line
00068     DEF ed      escb J  ; # Erase Down (to bottom)
00069     DEF eu      escb 1J ; # Erase Up (to top)
00070     DEF es      escb 2J ; # Erase Screen
00071 
00072     # Scrolling
00073 
00074     DEF sd      esc D    ; # Scroll Down
00075     DEF su      esc M    ; # Scroll Up
00076 
00077     # Cursor Handling
00078 
00079     DEF ch      escb H  ; # Cursor Home
00080     DEF sc      escb s  ; # Save Cursor
00081     DEF rc      escb u  ; # Restore Cursor (Unsave)
00082     DEF sca     esc  7  ; # Save Cursor + Attributes
00083     DEF rca     esc  8  ; # Restore Cursor + Attributes
00084 
00085     # Tabbing
00086 
00087     DEF st      esc  H  ; # Set Tab (@ current position)
00088     DEF ct      escb g  ; # Clear Tab (@ current position)
00089     DEF cat     escb 3g ; # Clear All Tabs
00090 
00091     # Device Introspection
00092 
00093     DEF qdc     escb c  ; # Query Device Code
00094     DEF qds     escb 5n ; # Query Device Status
00095     DEF qcp     escb 6n ; # Query Cursor Position
00096     DEF rd      esc  c  ; # Reset Device
00097 
00098     # Linewrap on/off
00099 
00100     DEF elw     escb 7h ; # Enable Line Wrap
00101     DEF dlw     escb 7l ; # Disable Line Wrap
00102 
00103     # Graphics Mode (aka use alternate font on/off)
00104 
00105     DEF eg      esc F   ; # Enter Graphics Mode
00106     DEF lg      esc G   ; # Exit Graphics Mode
00107 
00108     ##
00109     # ### ### ### ######### ######### #########
00110 
00111     # ### ### ### ######### ######### #########
00112     ## Complex, parameterized codes
00113 
00114     # Select Character Set
00115     # Choose which char set is used for default and
00116     # alternate font. This does not change whether
00117     # default or alternate font are used
00118 
00119     DEFC scs0 {tag} {esc  ($tag}  ; # Set default character set
00120     DEFC scs1 {tag} {esc  )$tag}  ; # Set alternate character set
00121 
00122     # tags in A : United Kingdom Set
00123     #         B : ASCII Set
00124     #         0 : Special Graphics
00125     #         1 : Alternate Character ROM Standard Character Set
00126     #         2 : Alternate Character ROM Special Graphics
00127 
00128     # Set Display Attributes
00129 
00130     DEFC sda {args} {escb [join $args ";"]m}
00131 
00132     # Force Cursor Position (aka Go To)
00133 
00134     DEFC fcp {r c}  {escb ${r}\;${c}f}
00135 
00136     # Cursor Up, Down, Forward, Backward
00137 
00138     DEFC cu {{n 1}} {escb [expr {$n == 1 ? "A" : "${n}A"}]}
00139     DEFC cd {{n 1}} {escb [expr {$n == 1 ? "B" : "${n}B"}]}
00140     DEFC cf {{n 1}} {escb [expr {$n == 1 ? "C" : "${n}C"}]}
00141     DEFC cb {{n 1}} {escb [expr {$n == 1 ? "D" : "${n}D"}]}
00142 
00143     # Scroll Screen (entire display, or between rows start end, inclusive).
00144 
00145     DEFC ss {args} {
00146     if {[llength $args] == 0} {return [escb r]}
00147     if {[llength $args] == 2} {foreach {s e} $args break ; return [escb ${s};${e}r]}
00148     return -code error "wrong\#args"
00149     }
00150 
00151     # Set Key Definition
00152 
00153     DEFC skd {code str} {escb "${code};\"${str}\"p"}
00154 
00155     # Terminal title
00156 
00157     DEFC title {str} {esc "\]0;${str}\007"}
00158 
00159     # Switch to and from character/box graphics.
00160 
00161     DEFC gron  {} {return \016}
00162     DEFC groff {} {return \017}
00163 
00164     # Character graphics, box symbols
00165     # - 4 corners, 4 t-junctions,
00166     #   one 4-way junction, 2 lines
00167 
00168     DEFC tlc   {} {return [gron]l[groff]} ; # Top    Left  Corner
00169     DEFC trc   {} {return [gron]k[groff]} ; # Top    Right Corner
00170     DEFC brc   {} {return [gron]j[groff]} ; # Bottom Right Corner
00171     DEFC blc   {} {return [gron]m[groff]} ; # Bottom Left  Corner
00172 
00173     DEFC ltj   {} {return [gron]t[groff]} ; # Left   T Junction
00174     DEFC ttj   {} {return [gron]w[groff]} ; # Top    T Junction
00175     DEFC rtj   {} {return [gron]u[groff]} ; # Right  T Junction
00176     DEFC btj   {} {return [gron]v[groff]} ; # Bottom T Junction
00177 
00178     DEFC fwj   {} {return [gron]n[groff]} ; # Four-Way Junction
00179 
00180     DEFC hl    {} {return [gron]q[groff]} ; # Horizontal Line
00181     DEFC vl    {} {return [gron]x[groff]} ; # Vertical   Line
00182 
00183     # Optimize character graphics. The generator commands above create
00184     # way to many superfluous commands shifting into and out of the
00185     # graphics mode. The command below removes all shifts which are
00186     # not needed. To this end it also knows which characters will look
00187     # the same in both modes, to handle strings created outside this
00188     # package.
00189 
00190     DEFC groptim {string} {
00191     variable grforw
00192     variable grback
00193     while {![string equal $string [set new [string map \
00194         "\017\016 {} \016\017 {}" [string map \
00195         $grback [string map \
00196         $grforw $string]]]]]} {
00197         set string $new
00198     }
00199     return $string
00200     }
00201 
00202     ##
00203     # ### ### ### ######### ######### #########
00204 
00205     # ### ### ### ######### ######### #########
00206     ## Higher level operations
00207 
00208     # Clear screen <=> CursorHome + EraseDown
00209     # Init (Fonts): Default ASCII, Alternate Graphics
00210     # Show a block of text at a specific location.
00211 
00212     DEFC clear {} {return [ch][ed]}
00213     DEFC init  {} {return [scs0 B][scs1 0]}
00214 
00215     DEFC showat {r c text} {
00216     if {![string length $text]} {return {}}
00217     return [fcp $r $c][sca][join \
00218         [split $text \n] \
00219         [rca][cd][sca]][rca][cd]
00220     }
00221 
00222     ##
00223     # ### ### ### ######### ######### #########
00224 
00225     # ### ### ### ######### ######### #########
00226     ## Attribute control (single attributes)
00227 
00228     foreach a [::term::ansi::code::attr::names] {
00229     DEF sda_$a escb "[::term::ansi::code::attr::$a]m"
00230     }
00231 
00232     ##
00233     # ### ### ### ######### ######### #########
00234     return
00235 }
00236 
00237 /*  ### ### ### ######### ######### #########*/
00238 /*  Data structures.*/
00239 
00240 namespace ::term::ansi::code::ctrl {
00241     namespace import ::term::ansi::code::define
00242     namespace import ::term::ansi::code::esc
00243     namespace import ::term::ansi::code::escb
00244 
00245     variable grforw
00246     variable grback
00247     variable _
00248     foreach _ {
00249     ! \" /*  $ % & ' ( ) * + , - . /*/
00250     0 1 2 3 4 5 6 7 8 9 : ; < = >
00251     ? @ A B C D E F G H I J K L M
00252     N O P Q R S T U V W X Y Z [ \\ 
00253     ] ^
00254     } {
00255     lappend grforw \016$_ $_\016
00256     lappend grback $_\017 \017$_
00257     }
00258     un _
00259 }
00260 
00261 ::term = ::ansi::code::ctrl::INIT
00262 
00263 /*  ### ### ### ######### ######### #########*/
00264 /*  Ready*/
00265 
00266 package provide term::ansi::code::ctrl 0.1
00267 
00268 /** 
00269  * ### ### ### ######### ######### #########
00270 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1