Version 5 of encoview as eTcl plugin

Updated 2006-03-22 17:51:27 by suchenwi

Richard Suchenwirth 2006-01-20 - As eTcl comes with all the encodings that regular Tcl offers, I felt tempted to write a little plugin for inspecting such codepages (currently only for 8-bit encodings, with codes 0..255). Also added was a generic listbox to select a name. When a codepage is displayed, you can click on a character to see it and its hex value in the title bar. The page has no menu of its own, so the previous one shows through - if it's the console with menu extended, it's easy to switch to another encoding then.

 proc encoview {{name ""} {page 0}} {
   if {$name eq ""} {
      set name [lb'select [lsort -dic [encoding names]] "Select encoding"]
   if {$name ne ""} {
      catch {wce siphide}
      set w [toplevel .[clock click]]
      wm title $w $name
      pack [canvas $w.c -bg lightyellow]
      set 0_15 [iota 16]
      if {$page} {$w.c create text 11 6 -text [format %02X.. $page]}
      set x 26
      set y 6
      foreach col $0_15 {
         $w.c create text $x $y -text [format %X $col] -fill blue
         incr x 13
      incr y 12
      foreach row $0_15 {
         set x 10
         $w.c create text $x $y -text [format %X0 $row] -fill blue
         incr x 16
         foreach col $0_15 {
            set i  [expr {$row*16+$col}]
            if {$page==0} {
                set c [encoding convertfrom $name [format %c $i]]
            } else {
                set c [format %c [expr {$page*256+$i}]]
            $w.c create text $x $y -text  $c \
                 -tag [list $c = [format 0x%02x [expr {$page*256+$i}]]] \
                 -font {{Bitstream Cyberbit} 10}
             incr x 13
         incr y 16
   $w.c bind = <1> {encoview'title %W}
   raise $w; focus -force $w
   bind $w <FocusOut> {destroy %W}

#-- Display the tags of the current item (which happen to look like "X = 0x58"

 proc encoview'title w {
   set c [lrange [$w gettags current] 0 2]
   wm title [winfo parent $w] $c

#-- Generic listbox selector, will go in a different file later

 proc lb'select {list {title "Select one:"}} {
   set w [toplevel .[clock click]]
   wm title $w $title
   bind $w <FocusOut> {set _ ""}
   listbox $w.l -yscr "$w.y set" -font {Tahoma 9}
   scrollbar $w.y -comm "$w.l yview"
   pack $w.y $w.l -side right -fill y -padx 3 -pady 3
   eval $w.l insert end $list
   bind $w.l <ButtonRelease-1> {set ::_ [%W get @%x,%y]}
   raise $w; focus -force $w
   vwait ::_
   catch {destroy $w}
   return $::_

You can call encoview by typing the command at the console, but for real "plugging" I added the line (see console for menu+) to my startup script:

 menu+ help Encoding encoview

2006-03-22 RS: Updated the encoview proc - now it can also display a Unicode page: specify its number as second argument (the first is ignored then), decimal or hex. For example, this displays the first page of CJK characters:

 encoview . 0x4E

Category Development