encoview as eTcl plugin

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, and Unicode pages). 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.

WikiDbImage encoview.jpg WikiDbImage encoview2.jpg


 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 (see screenshot above):

 encoview . 0x4E

For more convenience, I borrowed from iFile 1.1 a proc that annotates at least part of the Unicode page numbers, so one can choose a page from the listbox:

 proc upages {} {
    for {set i 0} {$i<256} {incr i} {set a($i) ""}
    array set a {0 iso8859-1 1 "Extended Latin"
       3 Greek 4 Cyrillic 5 Hebrew 6 Arabic 14 Thai 30 Vietnamese
       32 Currency 33 Symbols,arrows 34 Math 36 "Circled char.s"
       37 "Block graphics" 38 Dingbats
       48 Hiragana,Katakana 49 Bopomofo,Jamo
       50 "Circled symbols" 51 Units
       52 "1.0 Hangul..." 61 "...1.0 Hangul"
       78 "CJK ideographs..." 159 ...CJK
       172 Hangul... 248 ...Hangul 249 Hanja
       251 "Hebrew dotted" 252 "Arab ligatures"
       254 "Arab glyphs" 255 Double-width
    }
    set res ""
    for {set i 0} {$i<256} {incr i} {
      lappend res [format "%.2X %s" $i $a($i)]
    }
    set res
 }
# This proc can then be plugged into one of the [eTcl] menus:
 proc unicodeview {} {
    set page [lindex [lb'select [upages]] 0]
    if {$page ne ""} {encoview U+$page.. 0x$page}

 }