# Copyright (c) 2010 ancestor <xmpp: ancestor@jabber.org>
# Extended Highlight plugin v0.7
# Special thanks to teo.

namespace eval highlightex {
    ::msgcat::mcload [file join [file dirname [info script]] msgs]

    if {![::plugins::is_registered highlightex]} {
        ::plugins::register highlightex \
                -namespace [namespace current] \
                -source [info script] \
                -description [::msgcat::mc "Whether the Highlightex plugin is loaded."] \
                -loadcommand [namespace code load] \
                -unloadcommand [namespace code unload]
        return
    }
    
    custom::defgroup Plugins [::msgcat::mc "Plugins options."] -group Tkabber

    custom::defvar options(use_bold_font) 0 \
	[::msgcat::mc "Use bold font for highlight."] \
	-type boolean -group Highlightex \
    -command [namespace current]::set_bold_font

    custom::defvar options(highlight_color) red3 \
    [::msgcat::mc "Highlight color."] \
    -type string -group Highlightex \
    -command [namespace current]::set_highlight_color
}

########################################################################################

proc highlightex::process_highlights_ex {atLevel accVar} {
    upvar #$atLevel $accVar chunks
    
    variable options

    set out {}
    set allstr ""
    set fnick 0
    set nick ""

    foreach {s type tags} $chunks {
        if {$allstr == ""} {
            set allstr $s
        } else {
            set allstr "$allstr $s"
        }
    }
    
    if {[::richtext::property_exists mynick]} {
        set nick [::richtext::property_get mynick]
        set re_esc_nick [::plugins::emoticons::re_escape $nick]
                
        if {[regexp $re_esc_nick $allstr] == 1} {
            set fnick 1
        }
    }
    
        foreach {s type tags} $chunks {
        if {$tags == "server" || $tags == "highlightex" || $s == " "} {
            # pass through
            lappend out $s $type $tags
            continue
        }
                
        if {$fnick == 1} {
            set tags highlightex
        }
        lappend out $s $type $tags
    }
    set chunks $out
}

proc highlightex::add_chat_win_popup_highlightex_menu {m chatwin X Y x y} {
    variable options
    
    set tags [$chatwin tag names "@$x,$y"]

    set hght_tag ""

    set hght_col $options(highlight_color)

    if {[set idx [lsearch -glob $tags highlight*]] >= 0} {
        set hght_tag [lindex $tags $idx]
    }
    
    if {$hght_tag == ""} return

    $m add command -label [::msgcat::mc "Edit highlight color..."] \
    -command [list [namespace current]::edit_highlight_color $chatwin $hght_col]
}

proc highlightex::edit_highlight_color {chatwin hght_col} {
    variable options

    set new [tk_chooseColor -initialcolor $hght_col \
        -title [::msgcat::mc "Edit highlight color"]]

    if {$new == ""} return

    set options(highlight_color) $new
}

proc highlightex::set_highlight_color {args} {
    variable options
    set hcolor $options(highlight_color)

    option add *Chat.highlightforeground $hcolor

    foreach tmpchatid [array names ::chat::chat_id] {
        set chatid $::chat::chat_id($tmpchatid)
        
        if {[chat::is_groupchat $chatid]} {
            set w [chat::chat_win $chatid]
            $w tag configure highlightex -foreground $hcolor
        }
    }
}

proc highlightex::set_bold_font {args} {
    variable options
    set hcolor $options(highlight_color)

    foreach tmpchatid [array names ::chat::chat_id] {
        set chatid $::chat::chat_id($tmpchatid)
        
        if {[chat::is_groupchat $chatid]} {
            set w [chat::chat_win $chatid]

            if {$options(use_bold_font)} {
                $w tag configure highlightex -foreground $hcolor -font $::ChatBoldFont
            } else {
                $w tag configure highlightex -foreground $hcolor -font $::ChatFont
            }
        }
    }
}

proc highlightex::configure_highlightex_tag {w} {
    variable options
    set hcolor $options(highlight_color)
    
    if {$options(use_bold_font)} {
        $w tag configure highlightex -foreground $hcolor -font $::ChatBoldFont
        return
    }
    
    $w tag configure highlightex -foreground $hcolor -font $::ChatFont
}

########################################################################################

proc highlightex::load {} {	
    custom::defgroup Highlightex [::msgcat::mc "Highlightex options."] -group Plugins -type visible

    if {[lsearch $::richtext::registered highlightex] == -1} {
        ::richtext::register_entity highlightex \
            -configurator [namespace current]::configure_highlightex_tag \
            -parser [namespace current]::process_highlights_ex \
            -parser-priority 60
    }

    hook::add chat_win_popup_menu_hook [namespace current]::add_chat_win_popup_highlightex_menu 10

    ::richtext::entity_state highlightex 1
}

proc highlightex::unload {} {
    ::richtext::entity_state highlightex 0

    option add *Chat.highlightforeground red3

    hook::remove chat_win_popup_menu_hook [namespace current]::add_chat_win_popup_highlightex_menu 10

    custom::defgroup Highlightex [::msgcat::mc "Highlightex options."] -group Plugins -type hidden
}