# Description: adds support for mirc colors

package require roxirc 2.0

AddToPrefs colors "bool cmd [namespace current]::colors" 1

proc parsecolors {win} {
    global info
    set line [split [$win get end-1l end-1c] "\x03"]
    if {[llength $line] == 1} {return}
    set pos 0
    foreach x $line {
        if [string match "\[0-9\]*" $x] {
            if [string match "\[0-9\]" [string index $x 1]] {
                set fg [string range $x 0 1]
                $win delete end-1l+${pos}c end-1l+[expr $pos + 2]c
                set x [string range $x 2 end]
            } else {
                set fg [string index $x 0]
                $win delete end-1l+${pos}c
                set x [string range $x 1 end]
            }
            if {$fg < 16} {
                $win tag add ${fg}f end-1l+${pos}c end-1l+[expr $pos + [string length $x]]c
            }
            if [string match ",\[0-9\]*" $x] {
                if [string match "\[0-9\]" [string index $x 2]] {
                    set bg [string range $x 1 2]
                    $win delete end-1l+${pos}c end-1l+[expr $pos + 3]c
                    set x [string range $x 3 end]
                } else {
                    set bg [string index $x 1]
                    $win delete end-1l+${pos}c end-1l+[expr $pos + 2]c
                    set x [string range $x 2 end]
                }
                if {$bg < 16} {
                    $win tag add ${bg}b end-1l+${pos}c end-1l+[expr $pos + [string length $x]]c
                }
            }
        }
        incr pos [string length $x]
        $win delete end-1l+${pos}c
    }
}

proc colors {window line} {
    global info prefs
    if $line {
        set tmp $prefs(colors)
        set prefs(colors) 1
        foreach x [textwindows] {configurecolortags $x}
        set prefs(colors) $tmp
    } else {
        foreach x [textwindows] {
            for {set a 0} {$a <= 15} {incr a} {
                $info(text,$x) tag configure ${a}f -foreground ""
                $info(text,$x) tag configure ${a}b -background ""
            }
        }
    }
}

proc configurecolortags {window} {
    global info prefs
    set win $info(text,$window)
    if {!$prefs(colors)} {
        for {set a 0} {$a <= 15} {incr a} {
            $win tag configure ${a}f -foreground ""
            $win tag configure ${a}b -background ""
        }
    } else {
        set n 0
        foreach color {#ffffff #000000 #00007f #008f00 #ff0000 #7f0000 #9f009f #ff7f00 #ffff00 #00ff00 #008f8f #00ffff #0000ff #ff00ff #7f7f7f #cfcfcf} {
            $win tag configure ${n}f -foreground $color
            $win tag configure ${n}b -background $color
            incr n
        }
    }
    foreach tag {<> @<> +<> me my<> mynick mytext hilight ts search sel} {
        catch {$win tag raise $tag}
    }
}

proc unload { } {
    global info
    RemoveFromPrefs colors
    unbind all <<channelcreate>> "[namespace current]::configurecolortags %W"
    unbind all <<querycreate>> "[namespace current]::configurecolortags %W"
    unbind all <<dccchatcreate>> "[namespace current]::configurecolortags %W"
    unbind Text <<echo>> "[namespace current]::parsecolors %W"
    foreach x [textwindows] {
        $info(text,$x) tag delete 0f 1f 2f 3f 4f 5f 6f 7f 8f 9f 10f 11f 12f 13f 14f 15f 0b 1b 2b 3b 4b 5b 6b 7b 8b 9b 10b 11b 12b 13b 14b 15b
    }
}

proc help {window line} {
    Echo $window {[ help ] Variables added by this script: COLORS}
    Echo $window {[ help ] the COLORS variable turns the display of mirc colors on or off. When off the color characters are still parsed but the colors are not displayed}
}

foreach x [textwindows] {
    configurecolortags $x
}

bind Text <<echo>> "+[namespace current]::parsecolors %W"
bind all <<channelcreate>> "+[namespace current]::configurecolortags %W"
bind all <<querycreate>> "+[namespace current]::configurecolortags %W"
bind all <<dccchatcreate>> "+[namespace current]::configurecolortags %W"
