All pastes #417315 Raw Edit

Untitled

public text v1 · immutable
#417315 ·published 2007-03-30 20:19 UTC
rendered paste body
on *:LOAD:{
  if ($version < 6.1) || ($version > 6.14) {
    var %i = $$input(Sorry $+ $chr(44) TEMX was meant for mIRC versions 6.1 to 6.14 $+ $chr(44) you are using version $version $+ . $crlf $crlf $+ If you are using an older version $+ $chr(44) please upgrade $+ $chr(44) preferably to version 6.14. $crlf $crlf $+ If you are using a newer version $+ $chr(44) please see if an update of this engine is available. $crlf $crlf $+ When you click ok the engine will be unloaded...,wod,Theme Engine MX)
    unload -rs $script
  }
  else {
    if (!$hget(theme.settings)) { hmake theme.settings 20 }
    theme.opt.restoreevents
    theme.opt.restoreraws
    ha theme.load.back 1 | ha theme.load.color 1 | ha theme.load.events 1 | ha theme.load.everything 1 | ha theme.load.font 1 | ha theme.load images 1 | ha theme.load.nick 1 | ha theme.load.rgb 1 | ha theme.load.schemes 0 | ha theme.load.sound 1 | ha theme.load.timestamp 1
    ha theme.add.clear 0 | ha theme.add.everything 0 | ha theme.add.minimize 0 | ha theme.add.preview 0 | ha theme.add.unload 0 | ha theme.add.progress 1 | ha theme.add.cache 1
    ha theme.ontop 0 | ha theme.desktop 0 | ha theme.startrefresh 0 | ha theme.list.name 1 | ha theme.list.format 1
    ha mdxfolder $scriptdirdll\ | ha xmlfolder $scriptdirdll\ | ha szipfolder $scriptdirdll\ | ha fontfolder $scriptdirdll\
    ha theme.frep.GwdTE_437 IBMPC | ha theme.frep.MS\~Sans\~Serif Microsoft Sans Serif | ha theme.frep.PC8X16 IBMPC

    if (!$isfile($mdxdir $+ mdx.dll)) || ($findfile($mdxdir,*.mdx,0) != 4) {
      var %m = $input(MDX.DLL or one or more of it's needed plugins were not found $+ $chr(44) make sure all are present. $crlf $crlf $+ If the files can be found in a different location $+ $chr(44) you can set it there. Would you like to do that? $crlf $crlf $+ Note that the script will load regardless of what you do.,wyd,Theme Engine MX)
      if (%m) { ha mdxfolder $$sdir($scriptdir,Choose a new folder for MDX.DLL and it's plugins) }
    }
    if (!$isfile($xmldir $+ xml.dll)) {
      var %x = $input(XML.DLL was not found $+ $chr(44) make sure it is in the right location. $crlf $crlf $+ If it can be found somewhere else on your harddrive you can set it to that location. Would you like to do that? $crlf $crlf $+ Note that the script will load regardless of what you do $+ $chr(44) and the dll is only needed for XTS themes.,wyd,Theme Engine MX)
      if (%x) { ha xmlfolder $$sdir($scriptdir,Choose a new folder for XML.DLL) }
    }
    if (!$isfile($szipdir $+ xml.dll)) {
      var %s = $input(SZIP.DLL was not found $+ $chr(44) make sure it is in the right location. $crlf $crlf $+ If it can be found somewhere else on your harddrive you can set it to that location. Would you like to do that? $crlf $crlf $+ Note that the script will load regardless of what you do $+ $chr(44) and the dll is only needed for zipped themes.,wyd,Theme Engine MX)
      if (%s) { ha szipfolder $$sdir($scriptdir,Choose a new folder for SZIP.DLL) }
    }
    if (!$isfile($fontdir $+ xml.dll)) {
      var %f = $input(FONT.DLL was not found $+ $chr(44) make sure it is in the right location. $crlf $crlf $+ If it can be found somewhere else on your harddrive you can set it to that location. Would you like to do that? $crlf $crlf $+ Note that the script will load regardless of what you do $+ $chr(44) and the dll is only needed for font selection.,wyd,Theme Engine MX)
      if (%f) { ha fontfolder $$sdir($scriptdir,Choose a new folder for FONT.DLL) }
    }
    if (!$themesdir) {
      var %t = $input(There is currently no theme folder set $+ $chr(44) would you like to do that now?,qyd,Theme Engine MX)
      if (%t) { dialog -m theme.folders theme.folders }
    }

    hsave -o theme.settings " $+ $scriptdirsettings.src $+ "
    var %i = $input(Successfully loaded the TEMX $+ $chr(44) written by $crlf $+ Voice of Power (marckuiper@hotmail.com). $crlf $crlf $+ The script is all set to go $+ $chr(44) simply type /themes to open the loading dialog or access it via the menu popups. $crlf $crlf $+ $+ To access the engine's help file type /themes help. $crlf $crlf $+ Enjoy!,iod,Theme Engine MX)
  }
}
on *:UNLOAD:{
  theme.unload
  if ($dialog(themes)) { dialog -x themes }
  if ($hget(theme.settings)) { hfree theme.settings }
  if ($hget(theme.current)) { hfree theme.current }
  if ($isfile($shortfn($scriptdir) $+ settings.src)) { .remove $shortfn($scriptdir) $+ settings.src }
  if ($isfile($shortfn($scriptdir) $+ current.src)) { .remove $shortfn($scriptdir) $+ current.src }
}

on *:START:{ theme.start }
alias -l theme.start {
  if (!$hget(theme.current)) { hmake theme.current 15 }
  if ($exists($scriptdircurrent.src)) { hload theme.current " $+ $scriptdircurrent.src $+ " }
  if (!$hget(theme.settings)) { hmake theme.settings 20 }
  if ($exists($scriptdirsettings.src)) { hload theme.settings " $+ $scriptdirsettings.src $+ " }

  $eval($hgt(init),2)
  if ($isfile($hgt(script))) && (!$script($hgt(script))) { .load -rs " $+ $hgt(script) $+ " }
  if ($hgt(timestampformat)) { .timestamp -f $theme.parse($iif($theme.current == mts,m,x),$hgt(timestampformat)) }

  if ($hg(theme.startrefresh)) { theme.refresh }
  theme.sound start
}
on *:EXIT:{
  hsave -o theme.settings " $+ $scriptdirsettings.src $+ "
  hsave -o theme.current " $+  $scriptdircurrent.src $+ "
}

menu menubar {
  -
  Theme System
  .Load/Setup:themes
  .$iif($hgt(schemenames),Change Scheme)
  ..$iif(!$hgt(scheme),$style(1)) $iif($theme.current == mts,Default,$gettok($hgt(schemenames),1,59) (default)):theme.scheme
  ..-
  ..$submenu($theme.menu.schemes($1))
  .-
  .Refresh colors:theme.refresh
  .-
  .Links
  ..Support forum:url -n http://www.voiceofpower.us/forum/
  ..-
  ..mIRCscripts.org:url -n http://www.mircscripts.org/archive/themes
  ..ScriptsDB.org:url -n http://www.scriptsdb.org/files.php?type=theme
  .Help:themes help
  -
}
alias -l theme.menu.schemes {
  if ($1 == begin) || ($1 == end) { return - }
  var %s = $gettok($hgt(schemenames),$1,59)
  if (!%s) || (($theme.current == xts) && ($1 == 1)) { return - }
  return $iif($hgt(scheme) == %s,$style(1)) $iif($theme.current == mts,$1,$calc($1 - 1)) $+ . %s $+ :theme.scheme $iif($theme.current == mts,$1,$calc($1 - 1))
}

alias mtsversion { return 1.3 }
alias xtsversion { return 0.1 }
alias themesversion { return 1.12 }
alias themesengine { return Theme Engine MX by Voice of Power version $themesversion for MTS 1.3 and XTS 0.1 }
alias theme.current {
  if ($hgt($1)) { return $ifmatch }
  else {
    if ($right($hgt(file),4) == .mts) { return mts }
    elseif ($right($hgt(file),4) == .xml) { return xts }
  }
}
alias theme.setting { if ($hgt($1)) { return $ifmatch } }
alias themesdir {
  var %t = $hg(theme.folder)
  if (!$1) { return $numtok(%t,59) }
  elseif ($1 isnum) { return $gettok(%t,$1,59) }
}

alias lt { return < }
alias gt { return > }
alias amp { return & }
alias lp { return ( }
alias rp { return ) }
alias lb { return [ }
alias rb { return ] }
alias la { return $chr(123) }
alias ra { return $chr(125) }
alias nsym { return $left($remove($nick($chan,$iif($nick,$nick,$me)).pnick,$iif($nick,$nick,$me)),1) }
alias tstamp { return $eval($timestamp,2) }
alias pre {
  var %pre = $theme.parse($iif($theme.current == mts,m,x),$hgt(prefix))
  return $eval(%pre,2)
}
alias -l cmdchar { return $ri(text,commandchar) }
alias -l cdef { return 0,6,4,5,2,3,3,3,3,3,3,1,5,7,6,1,3,2,3,5,1,0,1,0,1,15,6,0 }
alias -l rgbdef { return 255,255,255 0,0,0 0,0,128 0,144,0 255,0,0 128,0,0 160,0,160 255,128,0 255,255,0 0,255,0 0,144,144 0,255,255 0,0,255 255,0,255 128,128,128 208,208,208 }

alias -l ha { hadd theme.settings $1- }
alias -l hg { return $hget(theme.settings,$1) }
alias -l hat { hadd theme.current $1- }
alias -l hgt { return $hget(theme.current,$1) }
alias -l hap { hadd theme.preview $1- }
alias -l hgp { return $hget(theme.preview,$1) }
alias -l wi { if ($3) { writeini " $+ $mircini $+ " $1- } }
alias -l remi { if ($1) { remini " $+ $mircini $+ " $1- } }
alias -l ri { return $readini($mircini,n,$1,$2) }
alias -l mdx { return $dll($mdxdir $+ mdx.dll,$1,$2-) }
alias -l mdxdir { return $shortfn($hg(mdxfolder)) }
alias -l xml { return $dll($xmldir $+ xml.dll,$1,$2-) }
alias -l xmldir { return $shortfn($hg(xmlfolder)) }
alias -l szip { return $dll($szipdir $+ szip.dll,$1,$2-) }
alias -l szipdir { return $shortfn($hg(szipfolder)) }
alias -l fontdll { return $dll($fontdir $+ font.dll,Font,Select a font) }
alias -l fontdir { return $shortfn($hg(fontfolder)) }
alias -l cell { return $gettok($gettok($gettok($did($1,$2,$3),$4,9),$iif($4 = 1,6,5) $+ -,32),1,4) }
alias -l shorten {
  if ($prop == cutpath) && ($len($2-) > $1) {
    var %i = 2,%n = $1,%p = $2-
    while (%i <= $numtok($2-,92)) {
      %p = $replace($remove(%p,...\),$gettok($2-,%i,92),...)
      if ($len(%p) <= $1) { return %p }
      inc %i
    }
  }
  elseif ($1 > 3) && ($len($2-) > $1) { return $left($2-,$calc($1 - 3)) $+ ... }
  return $2-
}

alias -l isfont {
  var %x = i oW
  if ($width(%x,$$1,10) == $width(%x,MS Shell Dlg,10)) && ($height(%x,$$1,10) == $height(%x,MS Shell Dlg,10)) || ($$1 == $hg(theme.defaultfont)) { return $true }
  elseif ($width(%x,$$1,10) == $width(%x,x,10)) && ($height(%x,$$1,10) == $height(%x,x,10)) { return $false }
  else { return $true }
}

alias -l wins {
  var %s = 1
  while ($scon(%s)) {
    scon %s
    if ($left($1,1) == $chr(45)) {
      var %i = 1
      while ($window(*,%i)) {
        var %t = $window(*,%i)
        window $1 $iif($left(%t,1) == $chr(61),%t,$+(",%t,"))
        inc %i
      }
    }
    inc %s
  }
}

alias -l theme.colornum {
  var %i = 1
  while ($ini($shortfn($nofile($mircini)) $+ $nopath($mircini),colors,%i)) {
    var %m = $ifmatch
    if ($gettok($ri(colors,%m),1,44) == $1-) { var %p = %m }
    inc %i
  }
  if (!%p) { return n $+ $calc($right(%m,-1) + 1) }
  return %p
}

alias -l theme.options {
  if ($1 == load) {
    var %l
    if ($hg(theme.load.everything)) { %l = a }
    if ($hg(theme.load.font)) { %l = %l $+ f }
    if ($hg(theme.load.color)) { %l = %l $+ c }
    if ($hg(theme.load.nick)) { %l = %l $+ n }
    if ($hg(theme.load.sound)) { %l = %l $+ s }
    if ($hg(theme.load.back)) { %l = %l $+ b }
    if ($hg(theme.load.images)) { %l = %l $+ i }
    if ($hg(theme.load.timestamp)) { %l = %l $+ t }
    if ($hg(theme.load.events)) { %l = %l $+ e }
    if ($hg(theme.load.schemes)) { %l = %l $+ h }
    return %l
  }
  if ($1 == add) {
    var %a
    if ($hg(theme.add.everything)) { %a = a }
    if ($hg(theme.add.minimize)) { %a = %a $+ w }
    if ($hg(theme.add.clear)) { %a = %a $+ l }
    if ($hg(theme.add.unload)) { %a = %a $+ u }
    if ($hg(theme.add.preview)) { %a = %a $+ p }
    if ($hg(theme.add.cache)) { %a = %a $+ c }
    if ($hg(theme.add.progress)) { %a = %a $+ g }
    return %a
  }
}

alias theme.echo {
  if (!$theme.current) { echo $1- }
  else {
    var %i = $iif($hgt(indent),i $+ $ifmatch)
    if ($left($1,1) == -) {
      if ($window($2)) { theme.vars echo $color(info) $1 $+ %i $2 | set -u %::text $3- | set -u %::target $2 }
      else { theme.vars echo $color(info) $1 $+ %i | set -u %::text $2- }
    }
    else {
      if ($window($1)) { theme.vars echo $color(info) $iif(%i,- $+ %i) $1 | set -u %::text $2- | set -u %::target $1 }
      else { theme.vars echo $color(info) -a $+ %i | set -u %::text $1- }
    }
    if ($theme.current == mts) { theme.text echo $+ $iif($numtok(%:echo,32) == 4,target) }
    elseif ($theme.current == xts) { %:echo %::pre %::text }
  }
}
alias theme.error {
  if (!$theme.current) { echo $1- }
  else {
    var %i = $iif($hgt(indent),i $+ $ifmatch)
    if ($left($1,1) == -) {
      if ($window($2)) { theme.vars echo $color(info) $1 $+ %i $2 | set -u %::text $3- }
      else { theme.vars echo $color(info) $1 $+ %i | set -u %::text $2- }
    }
    else {
      if ($window($1)) { theme.vars echo $color(info) $iif(%i,- $+ %i) $1 | set -u %::text $2- }
      else { theme.vars echo $color(info) -a $+ %i | set -u %::text $1- }
    }
    if ($theme.current == mts) { theme.text error }
    elseif ($theme.current == xts) { theme.text error %::text }
  }
}
alias ptext {
  if ($1) {
    set -nu %::text $1-
    if ($hgt(parentext)) { return $theme.parse(m,$hgt(parentext)) }
    else { return ( $+ $1- $+ ) }
  }
}


;################# PARSING ################

alias theme.info {
  if ($0 == 4) { var %s = $1,%c = $2,%e = $3,%f = $4 }
  elseif ($0 == 3) {
    if ($1 isnum) { var %s = $1,%e = $2,%f = $3 }
    else { var %s = 0,%c = $1,%e = $2,%f = $3 }
  }
  elseif ($0 == 2) { var %e = $1,%f = $2 }
  if (!$isfile(%f)) { return }
  var %t = $iif($right(%f,4) == .mts,m,x)

  if (%t == m) {
    var %s = $iif(%s,%s,0)
    if ($fopen(parse)) { .fclose parse }
    .fopen parse " $+ %f $+ "
    if ($ferr) { .fclose parse | return }

    if (%e == schemenames) {
      .fseek -w parse scheme*
      while (!$feof) {
        var %r = $fread(parse)
        if (scheme* iswm %r) { var %n = $addtok(%n,$gettok(%r,2-,32),59) }
        elseif ($+($chr(91),scheme*,$chr(93)) iswm $1) { .fclose parse | break }
      }
      .fclose parse | return %n
    }
    else {
      if (%s) { .fseek -w parse $+($chr(91),scheme $+ %s,$chr(93)) | var %l = $fread(parse) }
      while (!$feof) {
        if ($fread(parse)) {
          tokenize 32 $ifmatch
          if ($1 == [mts]) || (!$1) || ($left($1,1) == $chr(59)) { continue }
          elseif ($+($chr(91),scheme*,$chr(93)) iswm $1) { .fclose parse | return }
          elseif ($1 == %e) { .fclose parse | return $2- }
        }
      }
    }
    if ($fopen(parse)) { .fclose parse }
  }
  elseif (%t == x) {
    if ($0 == 2) { var %s = info }
    var %s = $iif((%s) && (%s != info),scheme $+ %s,$iif(%s == info,info,default))
    xml CreateParser xts.parse
    xml StartElement xts.parse theme.xtsparse -s $+ $iif(%c,c) $+ $iif(%s,h) %e %c %s
    xml EndElement xts.parse theme.xtsparse -e %e
    xml Data xts.parse theme.xtsparse -d
    if (%e == schemenames) { xml Attribute xts.parse theme.xtsparse -a }
    xml File xts.parse " $+ %f $+ "
    xml ParseFile xts.parse
    xml FreeParser xts.parse

    var %r = $iif(%schemes,$ifmatch,%event)
    unset %scheme %section %item %event %schemes
    return %r
  }
}

alias theme.xtsparse {
  if (-s isin $1) {
    if (h isin $1) && (c !isin $1) && ($3 == $5) { %scheme = $3 }
    elseif (h isin $1) && (c isin $1) && ($4 == $6) { %scheme = $4 }
    if (%scheme) {
      if (c isin $1) && ($3 == $iif(h isin $1,$6,$5)) { %section = $3 }
      elseif (%scheme == info) || (c !isin $1) { %section = 1 }
    }
    if (%section) {
      if (ch isin $1) && ($2 == $6) { %item = $2 }
      elseif ((c isin $1) || (h isin $1)) && ($2 == $5) { %item = $2 }
      elseif ($2 == $4) { %item = $2 }
    }
  }
  elseif ($1 == -e) {
    if ($3 == %scheme) { xml FreeParser xts }
  }
  elseif ($1 == -d) && (%item) { xml FreeParser xts.parse | %event = $3- }
  elseif ($1 == -a) && ($4 == name) { %schemes = $addtok(%schemes,$5-,59) }
}

alias theme.parse {
  if ($1 == m) {
    var %t = $replace($2,<pre>,%::pre,<timestamp>,%::timestamp,<c1>,$bc(1),<c2>,$bc(2),<c3>,$bc(3),<c4>,$bc(4))
    var %t = $replace(%t,<me>,%::me,<server>,%::server,<port>,%::port,<nick>,%::nick,<chan>,%::chan,<address>,%::address,<me>,%::nick)
    var %t = $replace(%t,<cmode>,%::cmode,<cnick>,%::cnick,<target>,%::target,<knick>,%::knick,<kaddress>,%::kaddress,<newnick>,%::newnick)
    var %t = $replace(%t,<modes>,%::modes,<ctcp>,%::ctcp,<numeric>,%::numeric,<value>,%::value,<fromserver>,%::fromserver,<users>,%::users)
    var %t = $replace(%t,<away>,%::away,<realname>,%::realname,<isoper>,%::isoper,<operline>,%::operline,<isregd>,%::isregd)
    var %t = $replace(%t,<wserver>,%::wserver,<serverinfo>,%::serverinfo,<idletime>,%::idletime,<signontime>,%::signontime,<iaddress>,%::iaddress,<naddress>,%::naddress,<raddress>,%::raddress)
    var %t = $replace(%t,<ts>,,<lt>,<,<gt>,>,<parentext>,%::parentext,<text>,%::text)
    return %t
  }
  elseif ($1 == x) {
    var %t = $replace($2,$!pre,%::pre,$!timestamp,%::timestamp)
    var %t = $replace(%t,$!me,%::me,$!server,%::server,$!port,%::port,$!nick,%::nick,$!chan,%::chan,$!address,%::address,$!me,%::nick)
    var %t = $replace(%t,$!nsym,%::cmode,$!cnick,%::cnick,$!target,%::target,$!knick,%::knick,$!kaddress,%::kaddress,$!newnick,%::newnick)
    var %t = $replace(%t,$!modes,%::modes,$!ctcp,%::ctcp,$!numeric,%::numeric,$!value,%::value,$!fromserver,%::fromserver,$!users,%::users)
    var %t = $replace(%t,$!away,%::away,$!realname,%::realname,$!isoper,%::isoper,$!operline,%::operline,$!isregd,%::isregd)
    var %t = $replace(%t,$!wserver,%::wserver,$!serverinfo,%::serverinfo,$!idletime,%::idletime,$!signontime,%::signontime,$!iaddress,%::iaddress,$!naddress,%::naddress,$!raddress,%::raddress)
    var %t = $replace(%t,$!text,%::text)
    return %t
  }
  return $2
}

alias -l bc { if ($1 isnum) && ($hgt(basecolors)) { return  $+ $gettok($hgt(basecolors),$1,44) } }


alias theme.vars {
  set -nu %:echo $1-
  set -nu %::me $me
  set -nu %::server $server
  set -nu %::port $port
  set -nu %::pre $pre
  set -nu %::target $target
  set -nu %::c1 $bc(1)
  set -nu %::c2 $bc(2)
  set -nu %::c3 $bc(3)
  set -nu %::c4 $bc(4)
  set -nu %::timestamp $time($theme.parse($iif($theme.current == mts,m,x),$hgt(timestampformat)))
  if ($theme.current == xts) { set -nu %::timestamp $eval(%::timestamp,2) }
}

alias theme.text {
  if (%::text) { set -nu %::parentext $ptext(%::text) }
  if ($1) { set -nu %:text $hgt($1) }
  if (!%:text) && ($theme.current == xts) && ($1 == RAW.other) { set -nu %:text RAW.Other }
  if (!%:text) { halt }

  if (RAW. isin $1) { var %e = $calc($2 + 1) }
  else { var %e = $gettok($hg(theme.event. $+ $2),1,32) }
  if (!%e) { %e = 3 }
  if (%e == 1) { halt }
  elseif (%e == 2) || (!$theme.current) { goto end }
  elseif (%e == 3) {
    if ($theme.current == mts) {
      set -nu %:text $theme.parse(m,%:text)
      if ($gettok(%:text,1,32) != !script) { $eval(%:echo,2) %:text %:comments }
      else {
        if (!$gettok(%:text,2,32)) {
          if (raw. isin $1) { halt }
        }
        else { $eval($gettok(%:text,2-,32),2) }
      }
    }
    elseif ($theme.current == xts) {
      tokenize 32 $3-
      if (%:text == RAW.Other) { $eval(%:echo,2) $2- }
      else {
        set -nu %:text $theme.parse(x,%:text)
        if ($gettok(%:text,1,32) != !xts) { $eval(%:echo,2) $eval(%:text,2) }
        else { $eval($gettok(%:text,2,32),2) $1- }
      }
    }
    haltdef
  }
  elseif (%e == 4) {
    if (RAW. isin $1) { var %h = $gettok($hg($hfind(theme.settings,theme.raw.*. $+ $gettok($1,2,46),1,w)),3-,32) }
    else { var %h = $gettok($hg(theme.event. $+ $2),2-,32) }
    if ($theme.current == mts) { set -nu %:text $theme.parse(m,%:text) }
    elseif ($theme.current == xts) {
      tokenize 32 $3-
      set -nu %:text $eval($theme.parse(x,%:text),2)
    }
    $eval(%h,2)
    haltdef
  }
  :end
}

alias xecho {
  if (-r* iswm $1) { %:echo $2- }
  elseif ($1 == -w) { %:echo $3- }
}

alias cmode { return $left($remove($nick($2,$1).pnick,$1),1) }

alias theme.sound {
  var %f = $shortfn($nofile($hgt(file))) $+ $hgt(snd $+ $1)
  if ($isfile(%f)) {
    if ($right(%f,4) == .wav) { var %p = -w }
    elseif ($right(%f,4) == .mid) { var %p = -m }
    elseif ($right(%f,4) == .mp3) { var %p = -p }
    .splay %p %f
  }
}

;################## MAIN DIALOG #####################

alias themes {
  if ($1 == help) { run $shortfn($scriptdir) $+ Readme.html }
  elseif (!$dialog(themes)) { dialog -m $+ $iif($hg(theme.desktop),d) $+ $iif($hg(theme.ontop),o) themes themes } | else { dialog -v themes themes }
}

dialog -l themes {
  title "Theme Engine MX"
  size -1 -1 230 160
  option dbu

  list 1, 2 2 65 100, size extsel
  list 2, 2 104 65 40, size
  button "&Load", 3, 133 148 30 10, default
  button "&Unload", 4, 165 148 30 10
  button "&Close", 5, 198 148 30 10, cancel
  ;text "For more, goto:", 6, 3 149 38 8
  ;link "mircscripts.org", 7, 43 149 35 8
  ;text "or", 8, 80 149 5 8
  ;link "scriptsdb.org", 9, 87 149 35 8

  tab "Preview", 10, -20 -20 0 0
  tab "Information", 30
  tab "Apply", 50
  tab "RAW Events", 100

  list 21, 71 1 160 10, size
  text "", 17, 0 0 230 1
  text "", 18, 70 12 160 1
  text "", 19, 0 145 230 1
  text "", 20, 70 0 1 146

  text "Preview: none", 11, 77 17 80 8, tab 10
  button "&Preview", 12, 164 15 30 10, tab 10
  button "L&arger", 13, 196 15 30 10, tab 10
  box "", 14, 75 25 151 116, tab 10
  text "No preview generated at the moment.", 15, 105 77 120 8, tab 10
  icon 16, 72 29 156 112, $mircexe, 0, border hide tab 10

  box "", 31, 75 14 151 14, tab 30
  text "No theme file selected...", 32, 78 19 144 8, tab 30
  text "Name:", 33, 78 34 30 8, tab 30
  text "", 34, 115mu 34 80 8, tab 30
  text "Version:", 35, 78 45 30 8, tab 30
  text "", 36, 115 45 80 8, tab 30
  text "Author:", 37, 78 56 30 8, tab 30
  text "", 38, 115 56 80 8, tab 30
  text "E-mail:", 39, 78 67 30 8, tab 30
  link "", 40, 115 67 80 8, tab 30
  text "Website:", 41, 78 77 30 8, tab 30
  link "", 42, 115 77 80 8, tab 30
  text "Description:", 44, 78 92 40 8, tab 30
  edit "", 43, 75 100 151 38, read multi vsbar tab 30

  text "When loading a theme, apply:", 51, 77 16 120 8, tab 50
  check "E&verything", 52, 192 16 35 8, tab 50
  check "&Fonts", 53, 80 25 60 8, tab 50
  check "&Colors", 54, 80 33 60 8, tab 50
  check "&Sounds", 55, 80 49 60 8, tab 50
  check "&Nicklist colors", 56, 80 41 60 8, tab 50
  check "&Background images", 57, 150 25 70 8, tab 50
  check "Toolbar/switchbar &images", 58, 150 33 70 8, tab 50
  check "&Timestamp", 59, 150 41 70 8, tab 50
  check "Scripted &events", 60, 150 49 70 8, tab 50
  text "", 61, 73 60 155 1, tab 50

  text "Event options:", 62, 77 63 100 8, tab 50
  list 63, 77 71 50 70, tab 50 size vsbar
  text "Description:", 64, 131 71 30 8, tab 50
  box "", 65, 130 76 96 20, tab 50
  text "Description of event here.", 66, 133 81 86 14, tab 50
  text "Action:", 67, 131 98 30 8, tab 50
  combo 68, 130 106 96 10, drop tab 50
  text "Custom format command:", 69, 131 120 70 8, tab 50
  edit "", 70, 130 128 96 10, autohs tab 50

  text "RAW event settings:", 101, 77 17 129 8, tab 100
  combo 102, 165 15 60 10, tab 100 drop
  text "", 103, 73 28 155 1, tab 100
  text "Included RAWs:", 104, 77 32 50 8, tab 100
  list 105, 77 40 40 101, tab 100 size vsbar
  text "Description:", 106, 121 40 50 8, tab 100
  box "", 107, 120 45 106 34, tab 100
  text "Description of raw here.", 108, 123 50 96 28, tab 100
  text "Display in:", 109, 121 81 30 8, tab 100
  combo 110, 120 89 106 10, drop tab 100
  text "Action:", 111, 121 102 30 8, tab 100
  combo 112, 120 110 106 10, drop tab 100
  text "Custom format command:", 113, 121 121 70 8, tab 100
  edit "", 114, 120 129 106 10, autohs tab 100

  menu "&File", 151
  item "Open file", 152, 151
  item "Open folder", 153, 151
  item break, 154, 151
  item "Refresh list", 155, 151
  item "Clear list", 156, 151
  item break, 159, 151
  item "Exit", 160, 151

  menu "&Options", 171
  menu "On load", 172, 171
  item "Load all schemes", 173, 172
  item break, 174, 172
  item "Show progress", 175, 172
  item "Clear windows", 176, 172
  item "Minimize windows", 177, 172
  item "Unload old theme", 178, 172
  menu "Previews", 179, 171
  item "Preview on click", 180, 179
  item "Cache previews", 181, 179
  item break, 182, 179
  item "Destroy previews", 183, 179
  item break, 184, 171
  item "Refresh on start", 185, 171
  item break, 186, 171
  item "Show on desktop", 187, 171
  item "Always on top", 188, 171

  menu "&Edit", 201
  menu "Folders", 202, 201
  item "Theme folders", 203, 202
  item break, 204, 202
  item "Set MDX.dll folder...", 205, 202
  item "Set XML.dll folder...", 206, 202
  item "Set SZIP.dll folder...", 221, 202
  item "Set FONT.dll folder...", 224, 202
  menu "Listing", 207, 201
  item "List zipped themes", 222, 207
  item break, 223, 207
  item "Theme name", 208, 207
  item "File name", 209, 207
  item break, 210, 207
  item "Format", 211, 207
  item "Version", 212, 207
  item "Nothing", 213, 207
  item break, 214, 201
  item "Edit timestamp", 215, 201
  item "Change fonts", 216, 201
  item break, 218, 201
  item "Restore events to default", 219, 201
  item "Restore raws to default", 220, 201

  menu "&Help", 231
  item "Help", 232, 231
  item break, 233, 231
  item "About", 234, 231
}

on *:dialog:themes:*:*:{
  if ($devent == init) {
    mdx SetMircVersion $version
    mdx MarkDialog $dname
    mdx SetControlMDX $dname 1 ListView rowselect report single showsel sortascending noheader labeltip > $mdxdir $+ views.mdx
    mdx SetControlMDX $dname 21 ToolBar nodivider flat arrows list > $mdxdir $+ bars.mdx
    did -i $dname 1 1 headerdims 74:1 35:2 0:3
    did -i $dname 1 1 headertext Theme 	Format 	File
    mdx SetColor $dname 43 background $rgb(hilight)
    mdx SetColor $dname 43 textbg $rgb(hilight)
    mdx SetColor $dname 43 text $rgb(text)
    mdx SetBorderStyle $dname 17,18,19,20,61,103 staticedge
    mdx SetBorderStyle $dname 21

    did -i $dname 21 1 bmpsize 16 16
    did -i $dname 21 1 setimage 0 icon small 0, $+ $shortfn($scriptdir) $+ icons.icl
    did -i $dname 21 1 setimage 0 icon small 1, $+ $shortfn($scriptdir) $+ icons.icl
    did -i $dname 21 1 setimage 0 icon small 2, $+ $shortfn($scriptdir) $+ icons.icl
    didtok -a $dname 21 59 +acgx 1 Preview;+acg 2 Information;+acg 3 Apply;+acg 3 RAW Events

    theme.list

    var %l = $theme.options(load)
    if (a isin %l) { did -c $dname 52 | did -b $dname 53,54,55,56,57,58,59,60 }
    if (f isin %l) { did -c $dname 53 }
    if (c isin %l) { did -c $dname 54 }
    if (s isin %l) { did -c $dname 55 }
    if (n isin %l) { did -c $dname 56 }
    if (b isin %l) { did -c $dname 57 }
    if (i isin %l) { did -c $dname 58 }
    if (t isin %l) { did -c $dname 59 }
    if (e isin %l) { did -c $dname 60 }
    if (h isin %l) { did -c $dname 173 }

    didtok -a $dname 63 59 on INPUT;on TEXT;on ACTION;on NOTICE;on CHAT;on JOIN;on PART;on NICK;on QUIT;on NOTIFY;on UNOTIFY;on DNS;on USERMODE;on ERROR;on SNOTICE;on INVITE;on TOPIC;on KICK;on RAWMODE;on WALLOPS;on CTCPREPLY;CTCP
    didtok -a $dname 68 59 Hide;Use mIRC default;Apply theme format;Apply custom format
    did -c $dname 63,68 1
    theme.opt.dispevent $did(63).seltext

    var %a = $theme.options(add)
    if (g isin %a) { did -c $dname 175 }
    if (l isin %a) { did -c $dname 176 }
    if (w isin %a) { did -c $dname 177 }
    if (u isin %a) { did -c $dname 178 }
    if (p isin %a) { did -c $dname 180 }
    if (c isin %a) { did -c $dname 181 }
    if ($hg(theme.startrefresh)) { did -c $dname 185 }
    if ($hg(theme.desktop)) { did -c $dname 187 }
    if ($hg(theme.ontop)) { did -c $dname 188 }
    if ($hg(theme.list.zip)) { did -c $dname 222 }
    if ($hg(theme.list.name)) { did -c $dname 208 }
    else { did -c $dname 209 }
    if ($hg(theme.list.format) == 1) { did -c $dname 211 }
    elseif ($hg(theme.list.format) == 2) { did -c $dname 212 }
    else { did -c $dname 213 }

    didtok -a $dname 102 59 Connection;Join;Whois;Who;Mode;Error;Stats;Admin;Misc;All
    didtok -a $dname 110 59 Target window, else in active;Target window, else in status;Always in active window;Always in status window;None
    didtok -a $dname 112 59 Use mIRC default;Apply theme format;Apply custom format
    did -c $dname 102,110,112 1
    theme.opt.listraw 1

    if (!$theme.current) { did -b $dname 4 }
  }
  if ($devent == sclick) {
    if ($did == 21) {
      var %b = $did(21).sel
      if (%b == 2) { did -f $dname 10 }
      elseif (%b == 3) { did -f $dname 30 }
      elseif (%b == 4) { did -f $dname 50 }
      elseif (%b == 5) { did -f $dname 100 }
    }
    if ($did == 1) && ($gettok($did(1,1),1,32) == sclick) { theme.dispinfo $cell(themes,1,$did(1).sel,3) }
    if ($did == 2) { if (p isin $theme.options(add)) || (a isin $theme.options(add)) { theme.preview $iif($did(2).sel > 1,$calc($did(2).sel - 1)) $cell(themes,1,$did(1).sel,3) } }
    if ($did == 3) { $iif(!$hg(theme.add.progress),.) $+ theme.load - $+ $iif(a isin $theme.options(load),fcnsbite,$theme.options(load)) $+ $iif(h isin $theme.options(load),h) $+ $iif(a isin $theme.options(add),wlu,$theme.options(add)) $iif($did(2).sel != 1,$calc($did(2).sel - 1)) $cell(themes,1,$did(1).sel,3) }
    if ($did == 4) { $iif(!$hg(theme.add.progress),.) $+ theme.unload - $+ $iif(a isin $theme.options(add),wl,$theme.options(add)) }
    if ($did == 12) && ($did(1).sel) { theme.preview $iif($did(2).sel > 1,$calc($did(2).sel - 1)) $cell(themes,1,$did(1).sel,3) }
    if ($did == 13) && ($did(1).sel) { theme.preview -l $iif($did(2).sel > 1,$calc($did(2).sel - 1)) $cell(themes,1,$did(1).sel,3) }
    if ($did == 40) && ($did(40)) { url -n $did(40) }
    if ($did == 42) && ($did(42)) { url -n $did(42) }

    if ($did == 52) {
      if ($did(52).state) { did -b $dname 53,54,55,56,57,58,59,60 }
      else { did -e $dname 53,54,55,56,57,58,59,60 }
      ha theme.load.everything $did(52).state
    }
    if ($did == 53) { ha theme.load.font $did(53).state }
    if ($did == 54) { ha theme.load.color $did(54).state }
    if ($did == 55) { ha theme.load.sound $did(55).state }
    if ($did == 56) { ha theme.load.nick $did(56).state }
    if ($did == 57) { ha theme.load.back $did(57).state }
    if ($did == 58) { ha theme.load.images $did(58).state }
    if ($did == 59) { ha theme.load.timestamp $did(59).state }
    if ($did == 60) { ha theme.load.events $did(60).state }

    if ($did == 63) { theme.opt.dispevent $did(63).seltext }
    if ($did == 68) {
      if ($did(68).sel == 4) { did -e $dname 69,70 }
      else { did -b $dname 69,70 }
      theme.opt.editevent -a $iif($did(63).seltext == CTCP,$ifmatch,$right($did(63).seltext,-3)) $did(68).sel
    }

    if ($did == 102) { theme.opt.listraw $did(102).sel }
    if ($did == 105) { var %r = $iif($did(102).seltext == Connection,con,$iif($lower($did(102).seltext) != all,$ifmatch)) | theme.opt.dispraw $iif(%r,%r $+ .) $+ $did(105).seltext }
    if ($did == 110) {
      if ($did(110).sel == 5) { did -b $dname 111,112 }
      else { did -e $dname 111,112 }
      var %r = $iif($did(102).seltext == Connection,con,$lower($did(102).seltext))
      theme.opt.editraw -d $iif(%r,%r $+ .) $+ $did(105).seltext $theme.opt.inforaw($did(110).sel)
    }
    if ($did == 112) {
      if ($did(112).sel == 3) { did -e $dname 113,114 | did -b $dname 109,110 }
      else { did -b $dname 113,114 | did -e $dname 109,110 }
      var %r = $iif($did(102).seltext == Connection,con,$lower($did(102).seltext))
      theme.opt.editraw -a $iif(%r,%r $+ .) $+ $did(105).seltext $did(112).sel
    }
  }
  if ($devent == dclick) {
    if ($did == 1) || ($did == 2) { $iif(!$hg(theme.add.progress),.) $+ theme.load - $+ $iif(a isin $theme.options(load),fcnsbite,$theme.options(load)) $+ $iif(h isin $theme.options(load),h) $+ $iif(a isin $theme.options(add),wlu,$theme.options(add)) $iif($did(2).sel != 1,$calc($did(2).sel - 1)) $cell(themes,1,$did(1).sel,3) }
  }
  if ($devent == edit) {
    if ($did == 70) { theme.opt.editevent -f $iif($did(63).seltext == CTCP,$ifmatch,$right($did(63).seltext,-3)) $did(70) }
    if ($did == 114) {
      var %r = $iif($did(102).seltext == Connection,con,$lower($did(102).seltext))
      theme.opt.editraw -f $iif(%r,%r $+ .) $+ $did(105).seltext $did(114)
    }
  }
  if ($devent == menu) {
    if ($did == 152) {
      var %f = $$sfile($scriptdir*.mts;*.xml,Choose a theme file,Choose)
      theme.list -a %f
    }
    if ($did == 153) {
      var %f = $$sdir($scriptdir,Choose a theme folder)
      var %m = $findfile(%f,*.mts,0,theme.list -a $1-)
      var %x = $findfile(%f,*.xml,0,theme.list -a $1-)
    }
    if ($did == 155) { theme.list }
    if ($did == 156) { did -r $dname 1,2 }
    if ($did == 160) { dialog -x themes }

    if ($did == 173) { did $iif($did(173).state,-u,-c) $dname 173 | ha theme.load.schemes $did(173).state }
    if ($did == 175) { did $iif($did(175).state,-u,-c) $dname 175 | ha theme.add.progress $did(175).state }
    if ($did == 176) { did $iif($did(176).state,-u,-c) $dname 176 | ha theme.add.clear $did(176).state }
    if ($did == 177) { did $iif($did(177).state,-u,-c) $dname 177 | ha theme.add.minimize $did(177).state }
    if ($did == 178) { did $iif($did(178).state,-u,-c) $dname 178 | ha theme.add.unload $did(178).state }
    if ($did == 180) { did $iif($did(180).state,-u,-c) $dname 180 | ha theme.add.preview $did(180).state }
    if ($did == 181) { did $iif($did(181).state,-u,-c) $dname 181 | ha theme.add.cache $did(181).state }
    if ($did == 183) { theme.clearcache -p }
    if ($did == 185) { did $iif($did(185).state,-u,-c) $dname 185 | ha theme.startrefresh $did(185).state }
    if ($did == 187) { did $iif($did(187).state,-u,-c) $dname 187 | ha theme.desktop $did(187).state }
    if ($did == 188) { did $iif($did(188).state,-u,-c) $dname 188 | ha theme.ontop $did(188).state }

    if ($did == 203) { $dialog(theme.folders,theme.folders,-4) }
    if ($did == 205) { ha mdxfolder $$sdir($iif($mdxdir,$ifmatch,$scriptdir),Choose the folder for MDX.DLL and it's plugins) }
    if ($did == 206) { ha xmlfolder $$sdir($iif($xmldir,$ifmatch,$scriptdir),Choose the folder for XML.DLL) }
    if ($did == 221) { ha szipfolder $$sdir($iif($szipdir,$ifmatch,$scriptdir),Choose the folder for SZIP.DLL) }
    if ($did == 224) { ha fontfolder $$sdir($iif($fontdir,$ifmatch,$scriptdir),Choose the folder for FONT.DLL) }
    if ($did == 222) { did $iif($did(222).state,-u,-c) $dname 222 | ha theme.list.zip $did(222).state }
    if ($did == 208) { did -c $dname 208 | did -u $dname 209 | ha theme.list.name 1 }
    if ($did == 209) { did -c $dname 209 | did -u $dname 208 | ha theme.list.name 0 }
    if ($did == 211) { did -c $dname 211 | did -u $dname 212,213 | ha theme.list.format 1 }
    if ($did == 212) { did -c $dname 212 | did -u $dname 211,213 | ha theme.list.format 2 }
    if ($did == 213) { did -c $dname 213 | did -u $dname 211,212 | ha theme.list.format 0 }
    if ($did == 215) { $dialog(theme.tstamp,theme.tstamp,-4) }
    if ($did == 216) { $dialog(theme.font,theme.font,-4) }
    if ($did == 219) { if ($input(Are you sure you wish to restore all event settings to default? $crlf $crlf $+ All old settings will be lost.,yw,Theme Engine MX)) { theme.opt.restoreevents } }
    if ($did == 220) { if ($input(Are you sure you wish to restore all raw event settings to default? $crlf $crlf $+ All old settings will be lost.,yw,Theme Engine MX)) { theme.opt.restoreraws } }

    if ($did == 232) { themes help }
    if ($did == 234) { var %a = $input(Theme Engine MX version $themesversion $+ . $crlf $crlf $+ Written by Voice of Power. $crlf $chr(32) $chr(32) $chr(32) Contact: marckuiper@hotmail.com $crlf $crlf $+ Support forum: $crlf $chr(32) $chr(32) $chr(32) http://www.voiceofpower.us/forum $crlf $crlf $+ Supported theme formats: $crlf $chr(32) $chr(32) $chr(32) MTS (mIRC Theme Standard) 1.3 $crlf $chr(32) $chr(32) $chr(32) XTS (XML Theme Standard) 0.1 $crlf $crlf $+ Credits and other info in the help file.,io,Theme Engine MX) }
  }
  if ($devent == close) {
    window -c @theme.desc
    window -c @theme.filter
    theme.clearcache -z
  }
}

alias -l theme.opt.dispevent {
  if (!$window(@theme.desc)) { window -h @theme.desc }
  if (!$window(@theme.filter)) { window -h @theme.filter }
  clear @theme.desc
  loadbuf -tEvents @theme.desc $shortfn($scriptdir) $+ desc.src
  var %e = $iif($1 == CTCP,$1,$2)

  filter -ww @theme.desc @theme.filter %e *
  if ($filtered) {
    tokenize 9 $line(@theme.filter,$line(@theme.filter,0))
    did -ra themes 66 $gettok($1,2-,32)
    did -c themes 68 $gettok($hg(theme.event. $+ %e),1,32)
    did $iif($gettok($hg(theme.event. $+ %e),1,32) == 4,-e,-b) themes 69,70
    did -ra themes 70 $gettok($hg(theme.event. $+ %e),2-,32)
  }
}
alias -l theme.opt.editevent {
  var %o = $hg(theme.event. $+ $2)
  if (a isin $1) { ha theme.event. $+ $2 $3 $gettok(%o,2-,32) }
  elseif (f isin $1) { ha theme.event. $+ $2 $gettok(%o,1,32) $3- }
}
alias -l theme.opt.restoreevents {
  ha theme.event.ACTION 3 | ha theme.event.CHAT 3 | ha theme.event.CTCP 3 | ha theme.event.CTCPREPLY 3 | ha theme.event.DNS 3 | ha theme.event.ERROR 3 | ha theme.event.INPUT 3 | ha theme.event.INVITE 3 | ha theme.event.SNOTICE 3 | ha theme.event.TEXT 3 | ha theme.event.TOPIC 3 | ha theme.event.WALLOPS 3
  ha theme.event.JOIN 3 | ha theme.event.JOIN 3 | ha theme.event.KICK 3 | ha theme.event.NICK 3 | ha theme.event.NOTICE 3 | ha theme.event.NOTIFY 3 | ha theme.event.PART 3 | ha theme.event.QUIT 3 | ha theme.event.RAWMODE 3 | ha theme.event.UNOTIFY 3 | ha theme.event.USERMODE 3
}

alias -l theme.opt.listraw {
  did -ra themes 105 All
  if ($1 == 1) { didtok -a themes 105 59 001;002;003;004;005;221;250;251;252;253;254;255;265;266;372;375;376;391 | var %r = con }
  elseif ($1 == 2) { didtok -a themes 105 59 331;332;333;353;366;471;473;474;475 | var %r = join }
  elseif ($1 == 3) { didtok -a themes 105 59 301;307;311;312;313;314;317;318;319;330;338;369;406;431 | var %r = whois }
  elseif ($1 == 4) { didtok -a themes 105 59 315;352;416 | var %r = who }
  elseif ($1 == 5) { didtok -a themes 105 59 324;329;367;368;467;472;478;484;501;502 | var %r = mode }
  elseif ($1 == 6) { didtok -a themes 105 59 263;401;403;404;405;411;421;432;433;436;442;451;481;482 | var %r = error }
  elseif ($1 == 7) { didtok -a themes 105 59 211;212;213;214;215;216;217;218;219;223;243;244;247;248;249 | var %r = stats }
  elseif ($1 == 8) { didtok -a themes 105 59 256;257;258;259;423 | var %r = admin }
  elseif ($1 == 9) { didtok -a themes 105 59 006;007;271;272;302;303;305;306;323;381;382;438;443;603;604;606 | var %r = misc }
  elseif ($1 == 10) { didtok -a themes 105 59 001;002;003;004;005;006;007;212;213;214;215;216;217;218;219;221;223;243;244;247;248;249;250;251;252;253;254;255;256;257;258;259;263;265;266;271;272;301;302;303;305;306;307;311;312;313;314;315;317;318;319;323;330;331;332;333;338;352;353;366;369;372;375;381;382;391;401;403;404;405;406;411;416;421;423;431;432;433;436;438;442;443;451;471;473;474;475;481;482;483;603;604;606 }

  if (!$window(@theme.desc)) { window -h @theme.desc }
  if (!$window(@theme.filter)) { window -h @theme.filter }
  clear @theme.desc
  loadbuf -tRaws @theme.desc $shortfn($scriptdir) $+ desc.src
  did -c themes 105 1
  theme.opt.dispraw $iif(%r,%r $+ .) $+ $did(105).seltext
}
alias -l theme.opt.dispraw {
  filter -ww @theme.desc @theme.filter $iif(($numtok($1,46) == 2) || ($gettok($1,1,46) == All),$1,*. $+ $1) *
  if ($filtered) {
    tokenize 32 $line(@theme.filter,$line(@theme.filter,0))
    did -ra themes 108 $3-
    did -c themes 110 $theme.opt.inforaw($gettok($hg(theme.raw. $+ $1),1,32))
    did -c themes 112 $gettok($hg(theme.raw. $+ $1),2,32)

    if ($did(themes,110).sel == 5) { did -b themes 111,112,113,114 | did -e themes 109,110 }
    else {
      did -e themes 110,111,112
      if ($did(themes,112).sel == 3) { did -b themes 109,110 | did -e themes 113,114 }
      else { did -e themes 109,110 | did -b themes 113,114 }
    }

    did -ra themes 114 $gettok($hg(theme.raw. $+ $1),3-,32)
  }
}
alias -l theme.opt.editraw {
  if ($gettok($2,1,46) != all) { var %r = $2 }
  elseif ($gettok($2,2,46) == All) { var %r = all }
  else {
    var %h = $hfind(theme.settings,theme.raw.*. $+ $gettok($2,2,46),1,w)
    var %r = $gettok(%h,3-,46)
  }

  if (all isin %r) {
    var %h = $hfind(theme.settings,theme.raw. $+ $iif(%r == all,*,$gettok(%r,1,46) $+ .*),0,w),%i = 1
    while (%i <= %h) {
      var %t = $hfind(theme.settings,theme.raw. $+ $iif(%r == all,*,$gettok(%r,1,46) $+ .*),%i,w),%o = $hg(%t)
      if (d isin $1) { ha %t $3 $gettok(%o,2-,32) }
      elseif (a isin $1) { ha %t $gettok(%o,1,32) $3 $gettok(%o,3-,32) }
      elseif (f isin $1) { ha %t $gettok(%o,1-2,32) $3- }
      inc %i
    }
  }
  else {
    var %o = $hg(theme.raw. $+ %r)
    if (d isin $1) { ha theme.raw. $+ %r $3 $gettok(%o,2-,32) }
    elseif (a isin $1) { ha theme.raw. $+ %r $gettok(%o,1,32) $3 $gettok(%o,3-,32) }
    elseif (f isin $1) { ha theme.raw. $+ %r $gettok(%o,1-2,32) $3- }
  }
}
alias -l theme.opt.inforaw {
  if ($1 == 5) { return 0 }
  if ($1 == 4) { return 1 }
  if ($1 == 3) { return 2 }
  if ($1 == 2) { return 3 }
  if ($1 == 1) { return 4 }
  return 5
}
alias -l theme.opt.restoreraws {
  window -h @theme.restore
  loadbuf -tRaws @theme.restore $shortfn($scriptdir) $+ desc.src
  var %l = $line(@theme.restore,0),%i = 1
  while (%i <= %l) {
    if ($line(@theme.restore,%i)) { ha theme.raw. $+ $gettok($ifmatch,1-2,32) 2 }
    inc %i
  }
  window -c @theme.restore
}

alias -l theme.list {
  if ($dialog(themes)) {
    if ($1 == -a) && ($isfile($2-)) {
      if ($right($2-,4) == .mts) && ($theme.info(mtsversion,$2-)) && ($theme.info(name,$2-)) { var %n = $ifmatch,%t = MTS }
      elseif ($right($2-,4) == .xml) && ($theme.info(xtsversion,$2-)) { var %n = $left($nopath($2-),-4),%t = XTS }
      else { var %n = $left($nopath($2-),-4),%t = ZIP }

      if (!$hg(theme.list.name)) { var %n = $nopath($2-)) }
      if ($hg(theme.list.format) == 2) { var %t = $theme.info(version,$2-) }

      did -a themes 1 0 0 0 0 %n 	0 0 0 %t 	0 0 0 $2-
    }
    else {
      did -r themes 1
      if ($hg(theme.list.format) == 1) { did -i $dname 1 1 headerdims 74:1 35:2 0:3 }
      elseif ($hg(theme.list.format) == 2) { did -i $dname 1 1 headerdims 72:1 37:2 0:3 }
      else { did -i $dname 1 1 headerdims 109:1 0:2 0:3 }

      var %i = 1
      while ($themesdir(%i)) {
        var %f = $ifmatch
        var %m = $findfile(%f,*.mts,0,theme.list -a $1-)
        var %x = $findfile(%f,*.xml,0,theme.list -a $1-)
        if ($hg(theme.list.zip)) { var %z = $findfile(%f,*.zip,0,theme.list -a $1-) }

        if (!$did(themes,1).sel) && ($hgt(file)) {
          if (%f isin $hgt(file)) { did -c themes 1 $didwm(themes,1,* $hgt(file)) }
          elseif ($scriptdirextracted\ isin $hgt(file)) { did -c themes 1 $didwm(themes,1,* $+ $left($nopath($hgt(file)),-4) $+ .zip) }
        }
        inc %i
      }
      if (!$did(themes,1).sel) { did -c themes 1 2 }
      theme.dispinfo $cell(themes,1,$did(themes,1).sel,3)
    }
  }
}

alias -l theme.dispinfo {
  if ($1 == -p) { var %f = $2- }
  else { var %f = $1- }
  if ($right(%f,4) == .mts) { var %t = m }
  elseif ($right(%f,4) == .xml) { var %t = x }
  elseif ($right(%f,4) == .zip) { var %t = z }

  if ($dialog(themes)) && ($isfile(%f)) {
    if (%t == z) {
      var %z = $scriptdirextracted\ $+ $left($nopath(%f),-4) $+ \
      ha theme.extract $+(disp,	,%f,	,%z)
      if (!$isdir(%z)) { szip SUnZipFile %f > %z }
      else { .signal SZIP Z_OK %f }
      return
    }
    elseif (%t == m) && (($theme.info(mtsversion,%f) > $mtsversion) || ($theme.info(mtsversion,%f) < 1.0) || (!$theme.info(mtsversion,%f)) || (!$theme.info(name,%f))) { var %e = 1 }
    elseif (%t == x) && (($theme.info(xtsversion,%f) > $xtsversion) || (!$theme.info(xtsversion,%f))) { var %e = 1 }

    did -ra themes 32 $shorten(55,%f).cutpath
    if (%e) {
      did -b themes 2,3,12,13,33,34,35,36,37,38,39,40,41,42,43,44
      did -r themes 36,38,40,42,43
      did -ra themes 34 (invalid theme file)
      did -ra themes 2 Default
      did -c themes 2 1
    }
    else {
      did -e themes 2,3,12,13,33,34,35,36,37,38,39,40,41,42,43,44

      did -ra themes 34 $theme.info($iif(%t == x,th) $+ name,%f)
      did -ra themes 36 $theme.info(version,%f) ( $+ $upper(%t) $+ TS standard v $+ $theme.info(%t $+ tsversion,%f) $+ )
      did -ra themes 38 $theme.info(author,%f)
      did -ra themes 40 $theme.info(email,%f)
      did -ra themes 42 $theme.info(website,%f)

      if (%t == m) {
        did -ra themes 43 $theme.info(description,%f)
        did -ra themes 2 Default
        var %i = 1,%s = $theme.info(schemenames,%f)
        while ($gettok(%s,%i,59)) { did -a themes 2 $ifmatch | inc %i }
      }
      elseif (%t == x) {
        did -ra themes 43 Date of creation: $theme.info(cdate,%f) $crlf $+ Last update: $theme.info(udate,%f) $crlf $crlf $+ Desc: $theme.info(desc,%f)
        did -r themes 2
        var %i = 1,%s = $theme.info(schemenames,%f)
        while ($gettok(%s,%i,59)) { did -a themes 2 $ifmatch $iif(%i == 1,(default)) | inc %i }
      }
      did -c themes 2 1
      if ($did(themes,2).lines > 1) { did -e themes 2 }
      else { did -b themes 2 }
      if ($hgt(scheme)) && ($hg(theme.folder) isin $hgt(file)) { did -c themes 2 $didwm(themes,2,$hgt(scheme)) }
      else { did -c themes 2 1 }
      if ($1 == -p) || (p isin $theme.options(add)) || (a isin $theme.options(add)) { theme.preview $iif($did(themes,2).sel > 1,$calc($did(themes,2).sel - 1)) %f }
    }
  }
}

alias -l theme.clearcache {
  if ((p isin $1) || (!$1)) && ($findfile($scriptdirpreview\,*.bmp,0)) {
    if ($input(Are you sure you wish to delete all cached previews? $crlf $crlf $+ There are currently $ifmatch previews cached.,yw,Theme Engine MX)) {
      did -ra themes 11 Preview: none | did -h themes 16 | did -v themes 14,15
      var %p = $findfile($scriptdirpreview\,*.bmp,0,.remove " $+ $1- $+ ")
    }
  }
  if ((z isin $1) || (!$1)) && ($finddir($scriptdirextracted\,*,0)) {
    var %i = $ifmatch
    if ($dll(szip.dll)) { dll -u $ifmatch }

    while ($finddir($scriptdirextracted\,*,%i)) {
      var %f = $ifmatch $+ \
      if ($nofile($hgt(file)) isin %f) { dec %i | if (%i == 0) { break } | else { continue } }
      var %d = $findfile(%f,*,0,.remove " $+ $1- $+ ")
      .rmdir " $+ %f $+ "
      dec %i
      if (%i == 0) { break }
    }
  }
}


on *:SIGNAL:SZIP:{
  if ($1 == Z_OK) && ($hg(theme.extract)) {
    var %a = $gettok($ifmatch,1,9),%z = $gettok($ifmatch,2,9),%f = $gettok($ifmatch,3,9),%o = $gettok($ifmatch,4,9)
    if ($2- == %z) && (($findfile(%f,*.mts,1,var %t = $1-)) || ($findfile(%f,*.xml,1,var %t = $1-))) {
      if (%a == disp) { theme.dispinfo %t }
      elseif (%a == load) { theme.load %o %t }
      elseif (%a == preview) { theme.preview %o %t }
    }
    hdel theme.settings theme.extract
  }
}


dialog -l theme.folders {
  title "Theme folders"
  size -1 -1 150 99
  option dbu

  list 1, 2 2 146 85, size extsel
  button "&Add", 2, 2 88 25 10
  button "&Remove", 3, 29 88 30 10
  button "&Ok", 4, 86 88 30 10, ok default
  button "&Cancel", 5, 118 88 30 10, cancel
}
on *:dialog:theme.folders:*:*:{
  if ($devent == init) {
    mdx SetMircVersion $version
    mdx MarkDialog $dname
    mdx SetControlMDX $dname 1 ListView rowselect report single showsel labeltip > $mdxdir $+ views.mdx
    did -i $dname 1 1 headerdims 211:1 60:2
    did -i $dname 1 1 headertext Folder 	Themes

    var %i = 1
    while ($themesdir(%i)) {
      var %f = $ifmatch,%m = $findfile(%f,*.mts,0),%x = $findfile(%f,*.xml,0),%z = $findfile(%f,*.zip,0)
      did -a $dname 1 0 0 0 0 %f 	0 0 0 $calc(%m + %x + %z)
      inc %i
    }
  }
  if ($devent == sclick) {
    if ($did == 2) {
      var %f = $$sdir($mircdir,Choose a theme folder)
      var %m = $findfile(%f,*.mts,0),%x = $findfile(%f,*.xml,0)
      did -a $dname 1 0 0 0 0 %f 	0 0 0 $calc(%m + %x + %z)
    }
    if ($did == 3) {
      var %d = $$input(Are you sure you wish to delete the folder: $crlf $crlf $+ $cell($dname,1,$did(1).sel,1),wy,Theme folders)
      did -d $dname 1 $did(1).sel
    }
    if ($did == 4) {
      ha theme.folder
      var %f = 2
      while ($did(1,%f)) {
        var %a = $gettok($gettok($ifmatch,6-,32),1,9)
        ha theme.folder $addtok($hg(theme.folder),%a,59)
        inc %f
      }
    }
  }
}

dialog -l theme.tstamp {
  title "Edit timestamp"
  size -1 -1 120 38
  option dbu

  edit "", 1, 3 2 114 10, autohs
  button "", 2, 3 14 114 10
  text "", 3, 2 25 116 1
  button "&Reset", 4, 3 27 25 10
  button "&Ok", 5, 60 27 25 10, ok
  button "&Cancel", 6, 87 27 30 10, cancel
}
on *:dialog:theme.tstamp:*:*:{
  var %t = $iif($theme.current == mts,m,x)
  if ($devent == init) {
    mdx SetMircVersion $version
    mdx MarkDialog $dname
    mdx SetControlMDX $dname 2 Window > $mdxdir $+ dialog.mdx
    mdx SetBorderStyle $dname 3 staticedge

    if ($window(@theme.tstamp)) { window -c @theme.tstamp }
    window -hl @theme.tstamp
    did -a $dname 2 grab $window(@theme.tstamp).hwnd @theme.tstamp
    if (!$hfind(theme.current,timestampformatold,1)) { hat timestampformatold $hgt(timestampformat) }

    did -a $dname 1 $eval($hgt(timestampformat))
    aline @theme.tstamp $eval($time($theme.parse(%t,$hgt(timestampformat))))
  }
  if ($devent == sclick) {
    if ($did == 4) {
      did -ra $dname 1 $eval($hgt(timestampformatold))
      clear @theme.tstamp
      aline @theme.tstamp $eval($time($theme.parse(%t,$hgt(timestampformatold))))
    }
    if ($did == 5) {
      hat timestampformat $did(1)
      .timestamp -f $theme.parse(%t,$did(1))
    }
  }
  if ($devent == edit) && ($did == 1) {
    clear @theme.tstamp
    if ($did(1)) { aline @theme.tstamp $eval($time($theme.parse(%t,$ifmatch))) }
  }
}

dialog theme.font {
  title "Change fonts"
  size -1 -1 130 95
  option dbu

  tab "Set fonts", 10, 2 0 126 80
  tab "Font replacement", 30

  button "&Ok", 1, 66 83 30 10, ok
  button "&Cancel", 2, 98 83 30 10, cancel

  text "Change font for:", 11, 6 16 50 8, tab 10
  text "&Default:", 12, 6 28 50 8, tab 10
  combo 13, 60 27 64 50, drop size tab 10
  text "(status, @windows, finger, serv, etc..)", 18, 6 35 50 16, disable tab 10
  text "C&hannels:", 14, 6 50 50 8, tab 10
  combo 15, 60 49 64 50, drop size tab 10
  text "&Queries:", 16, 6 64 50 8, tab 10
  combo 17, 60 63 64 50, drop size tab 10
  text "", 19, 0 0 0 0, tab 10

  check "Enable &font replacement", 31, 6 16 70 8, tab 30
  list 32, 6 26 118 42, size extsel tab 30
  button "&Add", 33, 6 69 25 10, tab 30
  button "&Edit", 34, 33 69 25 10, tab 30
  button "&Remove", 35, 94 69 30 10, tab 30
}
on *:dialog:theme.font:*:*:{
  if ($devent == init) {
    mdx SetMircVersion $version
    mdx MarkDialog $dname
    mdx SetControlMDX $dname 32 ListView rowselect report single showsel labeltip sortascending > $mdxdir $+ views.mdx
    did -i $dname 32 1 headerdims 108:1 107:2
    did -i $dname 32 1 headertext Font 	Replacement

    var %l = 1,%n,%c
    while (%l <= 3) {
      if (%l == 1) { %n = default | %c = 13 }
      elseif (%l == 2) { %n = chan | %c = 15 }
      else { %n = query | %c = 17 }
      if ($hgt(font $+ %n)) {
        var %f = $ifmatch,%i = 1
        while ($gettok(%f,%i,59)) {
          var %font = $gettok($ifmatch,1,44),%size = $gettok(%f,2,44),%bold = $gettok(%f,3,44)
          if ($isfont(%font)) { did -a $dname %c $+(%font,$chr(44),$chr(32),%size,$iif(%bold,$chr(44) b)) }
          inc %i
        }
      }
      inc %l
    }
    did -a $dname 13,15,17 Other...
    did -c $dname 13,15,17 1

    if ($hg(theme.fontreplace)) { did -c $dname 31 }

    var %i = 1,%h
    while ($hfind(theme.settings,theme.frep.*,%i,w)) {
      did -a $dname 32 0 0 0 0 $replace($mid($ifmatch,12),\~,$chr(32)) 	0 0 0 $hg($ifmatch)
      inc %i
    }
  }
  if ($devent == sclick) {
    if ($did == 1) {
      if ($istok($did(19),13,59)) { tokenize 44 $did(13).seltext | theme.load.font default $1- }
      if ($istok($did(19),15,59)) { tokenize 44 $did(15).seltext | theme.load.font chan $1- }
      if ($istok($did(19),17,59)) { tokenize 44 $did(17).seltext | theme.load.font query $1- }

      ha theme.fontreplace $did(31).state
      var %f = 2
      while ($did(32,%f)) {
        ha theme.frep. $+ $replace($cell($dname,32,%f,1),$chr(32),\~) $cell($dname,32,%f,2)
        inc %f
      }
    }
    if ($did == 13) || ($did == 15) || ($did == 17) {
      var %d = $did
      if ($did(%d).sel == $did(%d).lines) && ($fontdll) {
        tokenize 44 $ifmatch
        did -i $dname %d 1 $+($4,$chr(44),$chr(32),$1,$iif(b isin $3,$chr(44) b))
      }
      did -c $dname %d 1
      did -ra $dname 19 $addtok($did(19),%d,59)
    }
    if ($did == 33) { $dialog(theme.font.add,theme.font.frep,-4) }
    if ($did == 34) && ($did(32).sel) { $dialog(theme.font.edit,theme.font.frep,-4) }
    if ($did == 35) && ($did(32).sel) {
      hdel theme.settings theme.frep. $+ $replace($cell($dname,32,$did(32).sel,1),$chr(32),\~)
      did -d $dname 32 $did(32).sel
    }
  }
}
dialog -l theme.font.frep {
  size -1 -1 145 44
  option dbu

  text "Original font:", 1 , 3 4 32 7
  edit "", 2, 39 2 72 10, autohs
  text "Replacement:", 4, 3 18 33 7
  edit "", 5, 39 16 72 10, autohs
  button "&Choose", 3, 113 2 30 10
  button "C&hoose", 6, 113 16 30 10
  text "", 7, 1 29 143 1
  button "&Ok", 8, 81 32 30 10, default
  button "&Cancel", 9, 113 32 30 10, cancel
}
on *:dialog:theme.font.*:*:*:{
  if ($devent == init) {
    mdx SetMircVersion $version
    mdx MarkDialog $dname
    mdx SetBorderStyle $dname 7 staticedge

    if ($dname == theme.font.add) { dialog -t $dname Add replacement }
    else {
      dialog -t $dname Edit replacement
      did -a $dname 2 $cell(theme.font,32,$did(theme.font,32).sel,1)
      did -a $dname 5 $cell(theme.font,32,$did(theme.font,32).sel,2)
    }
  }
  if ($devent == sclick) {
    if ($did == 3) { if ($fontdll) { did -ra $dname 2 $gettok($ifmatch,4,44) } }
    if ($did == 6) {
      if ($fontdll) {
        tokenize 44 $ifmatch
        did -ra $dname 5 $+($4,$chr(44),$chr(32),$1,$iif(b isin $3,$+($chr(44),$chr(32),b)))
      }
    }
    if ($did == 8) {
      if (!$isfont($iif($gettok($did(5),1,44),$ifmatch,Font does not exist))) { var %f = $input(The font $+(',$gettok($did(5),1,44),') is not installed on your system!,ow,Font replacements) }
      else {
        var %a = $iif($dname == theme.font.add,-a theme.font 32,-o theme.font 32 $did(theme.font,32).sel)
        did %a 0 0 0 0 $gettok($did(2),1,44) 	0 0 0 $did(5)
        dialog -x $dname
      }
    }
  }
}


;############## LOADING AND UNLOADING ##################

alias theme.load {
  var %fl,%s,%f
  if ($left($1,1) == -) { if ($2 isnum) { %s = $2 | %f = $3- } | else { %f = $2- } }
  elseif ($1 isnum) { %s = $1 | %f = $2- }
  else { %f = $1- }

  if ($right(%f,4) == .mts) { %fl = m }
  elseif ($right(%f,4) == .xml) { %fl = x }
  elseif ($right(%f,4) == .zip) { %fl = z }
  %fl = %fl $+ $iif(a isin $1,frcnbteuwl,$right($1,-1))
  if (%s) { %s = scheme $+ %s }
  else { %s = $iif(m isin %fl,mts,default) }

  if ($isfile(%f)) {

    ;; STEP ONE: VERSION CHECKING / UNZIPPING
    if (z isin %fl) {
      var %z = $scriptdirextracted\ $+ $left($nopath(%f),-4) $+ \
      ha theme.extract $+(load,	,%f,	,%z,	,$iif($remove(%fl,m,x,z),- $+ $ifmatch) $iif($2 isnum,$2))
      if (!$isdir(%z)) { szip SUnZipFile %f > %z }
      else { .signal SZIP Z_OK %f }
      return
    }
    elseif (m isin %fl) && ($theme.info(mtsversion,%f) > $mtsversion) {
      var %v = $$input(This theme file is invalid $+ $chr(44) it is made for MTS format $ifmatch $+ . This theme engine supports up to format $mtsversion $+ $chr(44) errors might occur. $crlf $crlf $+ Proceed?,yvwd,Invalid theme file)
      if (%v == $no) { return }
    }
    elseif (x isin %fl) && ($theme.info(xtsversion,%f) > $xtsversion) {
      var %v = $$input(This theme file is invalid $+ $chr(44) it is made for XTS format $ifmatch $+ . This theme engine supports up to format $xtsversion $+ $chr(44) errors might occur. $crlf $crlf $+ Proceed?,yvwd,Invalid theme file)
      if (%v == $no) { return }
    }
    if ($dialog(themes)) { did -b themes 3 }
    if ($dialog(theme.load)) { dialog -x theme.load }
    if ($show) { dialog -m theme.load theme.load }
    theme.load.show 3 1 0 2
    if (w isin %fl) { wins -n }
    if (l isin %fl) { scid -a clearall }
    theme.load.show 2 1 0 11
    theme.load.show 3 2 0 2

    ;; STEP TWO: UNLOADING OLD THEME
    theme.load.show 1 Unloading current theme
    if (u isin %fl) && ($theme.current) { .theme.unload }
    elseif ($script($hgt(script))) { .unload -rsn " $+ $hgt(script) $+ " }
    if ($dialog(theme.load)) { dialog -t theme.load Loading Theme... }
    theme.load.show 2 2 0 11

    ;; STEP THREE: LOADING HASH TABLE
    theme.load.show 3 0 0 2
    theme.load.show 1 Generating source
    if ($hget(theme.current)) { hfree theme.current }
    hmake theme.current 15
    theme.load.show 3 1 0 2
    hat file %f
    if (m isin %fl) { hat scheme $theme.info(%s,%f) }
    elseif (x isin %fl) { hat scheme $gettok($theme.info(schemenames,%f),$iif(%s != default,$calc($right(%s,-6) + 1)),59) }
    theme.load.hash $iif(h isin %fl,-h) %f
    theme.load.show 2 3 0 11
    theme.load.show 3 2 0 2

    ;; STEP FOUR: SCHEME OVERLAY
    theme.load.show 3 0 0 1
    theme.load.show 1 Overlaying scheme
    if ($hgt(scheme)) { theme.load.hash $right(%s,-6) %f }
    if (!$hgt(colors)) { hat colors 0,6,4,5,2,3,3,3,3,3,3,1,5,7,6,1,3,2,3,5,1,0,1,0,1,15,6,0  }
    if (!$hgt(rgbcolors)) { hat rgbcolors $rgbdef }
    hsave -o theme.current $shortfn($scriptdir) $+ current.src
    theme.load.show 2 4 0 11

    ;; STEP FIVE: LOADING THEME FILE
    theme.load.show 3 0 0 3
    if (e isin %fl) {
      theme.load.show 1 Loading scripted events
      if (m isin %fl) { hat script $nofile(%f) $+ $hgt(script) }
      elseif (x isin %fl) { hat script %f }
      theme.load.show 3 1 0 3
      if ($isfile($hgt(script))) { .load -rs " $+ $hgt(script) $+ " }
      theme.load.show 3 2 0 3
    }
    theme.load.show 2 5 0 11
    theme.load.show 3 3 0 3

    ;; STEP SIX: TIMESTAMP APPLYING
    theme.load.show 3 0 0 2
    if (t isin %fl) {
      theme.load.show 1 Applying timestamp settings
      .timestamp on
      theme.load.show 3 1 0 2
      if (m isin %fl) { .timestamp -f $theme.parse(m,$hgt(timestampformat)) }
      elseif (x isin %fl) { .timestamp -f $theme.parse(x,$hgt(timestampformat)) }
    }
    else { .timestamp off | .timestamp -f [HH:nn] }
    theme.load.show 2 6 0 11
    theme.load.show 3 2 0 2

    ;; STEP SEVEN: LOAD EVENT
    theme.load.show 1 Applying load event
    theme.load.show 3 0 0 1

    if (m isin %fl) && ($hgt(load)) {
      theme.vars echo $color(info) $+(-at,$iif($hgt(indent),i $+ $ifmatch))
      if ($gettok($hgt(load),1,32) == !script) { $eval($gettok( [ $hgt(load) ] ,2-,32),2) }
      else { %:echo $theme.parse(m,$hgt(load)) }
    }
    elseif (x isin %fl) {
      if ($hgt(init)) { $eval($hgt(init),2)) }
      if ($hgt(load)) { echo -a $hgt(load) }
    }
    theme.load.show 2 7 0 11
    theme.load.show 3 1 0 1

    ;; STEP EIGHT: COLOR LOADING
    theme.load.show 3 0 0 15
    if (c isin %fl) {
      theme.load.show 1 Loading colors
      .theme.load.resetcolors

      if (h isin %fl) {
        var %c = 1
        while ($gettok($hgt(rgbcolors.theme),%c,32)) {
          var %r = %r $+ , $+ $rgb( [ $theme.load.color($ifmatch) ] )
          inc %c
        }
        wi palettes $theme.colornum($hgt(name)) $right(%r,-1)
        hat rgbpalette.theme $right(%r,-1)
        var %c = $hgt(colors.theme)
        if ($numtok(%c,44) == 26) { var %c = %c $+ , $+ $gettok(%c,-1,44) }
        if ($numtok(%c,44) == 27) { var %c = %c $+ , $+ $gettok(%c,22,44) }
        wi colors $theme.colornum($hgt(name)) $hgt(name) $+ , $+ %c
        hat colorspalette.theme $hgt(name) $+ , $+ %c

        var %i = 1,%s = $hgt(schemenames)
        while ($gettok(%s,$iif(m isin %fl,%i,$calc(%i + 1)),59)) {
          var %n = $ifmatch

          var %c = 1,%r,%g = $iif($hgt(rgbcolors.scheme $+ %i),$ifmatch,$hgt(rgbcolors.theme))
          while ($gettok(%g,%c,32)) {
            var %r = %r $+ , $+ $rgb( [ $theme.load.color($ifmatch) ] )
            inc %c
          }
          wi palettes $theme.colornum($hgt(name) - %n) $right(%r,-1)
          hat rgbpalette.scheme $+ %i $right(%r,-1)

          var %c = $iif($hgt(colors.scheme $+ %i),$ifmatch,$hgt(colors.theme))
          if ($numtok(%c,44) == 26) { var %c = %c $+ , $+ $gettok(%c,-1,44) }
          if ($numtok(%c,44) == 27) { var %c = %c $+ , $+ $gettok(%c,22,44) }
          wi colors $theme.colornum($hgt(name) - %n) $hgt(name) - %n $+ , $+ %c
          hat colorspalette.scheme $+ %i $hgt(name) - %n $+ , $+ %c

          theme.load.show 3 %i 0 $numtok(%s,59)
          inc %i
        }
        .color -ls $hgt(name) $iif($hgt(scheme),- $hgt(scheme))
      }
      else {
        var %c = 1,%g = $hgt(rgbcolors)
        while ($gettok(%g,%c,32)) {
          var %r = %r $+ , $+ $rgb( [ $theme.load.color($ifmatch) ] )
          inc %c
        }
        wi palettes $theme.colornum($hgt(name)) $right(%r,-1)
        hat rgbpalette.theme $right(%r,-1)
        var %c = $hgt(colors)
        if ($numtok(%c,44) == 26) { var %c = %c $+ , $+ $gettok(%c,-1,44) }
        if ($numtok(%c,44) == 27) { var %c = %c $+ , $+ $gettok(%c,22,44) }
        wi colors $theme.colornum($hgt(name)) $hgt(name) $+ , $+ %c
        hat colorspalette.theme $hgt(name) $+ , $+ %c

        .color -ls $hgt(name)
      }
    }

    theme.load.show 2 8 0 11

    ;; STEP NINE: NICKLIST COLOR APPLYING
    theme.load.show 3 0 0 1
    if (n isin %fl) {
      theme.load.show 1 Applying nicklist colors
      .cnick -r $!me
      theme.load.show 3 1 0 1
      while ($cnick(0)) { .cnick -r * }
      theme.load.show 3 2 0 2
      theme.load.nicks m0 clineme
      theme.load.show 3 8 0 3
      theme.load.nicks m0 clineowner .
      theme.load.show 3 3 0 4
      theme.load.nicks m0 clineop @
      theme.load.show 3 4 0 5
      theme.load.nicks m0 clinehop %
      theme.load.show 3 5 0 6
      theme.load.nicks m0 clinevoice +
      theme.load.show 3 6 0 7
      theme.load.nicks m0 clineregular
      theme.load.show 3 7 0 8
      theme.load.nicks ovpym0 clinefriend
      theme.load.show 3 9 0 9
      theme.load.nicks im0 clineenemy
      theme.load.show 3 10 0 10
      theme.load.nicks m0 clineircop
      theme.load.show 3 11 0 11
    }
    theme.load.show 2 9 0 11

    ;; STEP TEN: FONT APPLYING
    theme.load.show 3 0 0 1
    if (f isin %fl) {
      theme.load.show 1 Applying font settings
      remi fonts
      theme.load.show 3 1 0 3
      if (!$hgt(fontchan)) { hat fontchan $hgt(fontdefault) }
      if (!$hgt(fontquery)) { hat fontquery $hgt(fontdefault) }

      var %l = 1,%n
      while (%l <= 3) {
        if (%l == 1) { %n = default }
        elseif (%l == 2) { %n = chan }
        else { %n = query }

        if ($hgt(font $+ %n)) {
          var %font = $ifmatch,%i = 1
          while ($gettok(%font,%i,59)) {
            var %fnt = $gettok($ifmatch,1,44),%size = $gettok(%font,2,44),%bold = $gettok(%font,3,44)
            if ($isfont(%fnt)) { theme.load.font %n %fnt %size %bold }
            inc %i
          }
        }
        inc %l
      }
    }
    else { theme.load.resetfont }
    theme.load.show 2 10 0 11
    theme.load.show 3 3 0 3

    ;; STEP ELEVEN: BACKGROUND IMAGES APPLYING
    theme.load.show 3 0 0 1
    theme.load.resetback
    if (b isin %fl) {
      theme.load.show 1 Applying background images
      remi background

      if ($hgt(imagestatus)) { theme.load.background status $ifmatch }
      if ($hgt(imagechan)) { theme.load.background chan $ifmatch }
      if ($hgt(imagequery)) { theme.load.background query $ifmatch }
      if ($hgt(imagemirc)) { theme.load.background mirc $ifmatch }
      if ($hgt(imagetoolbar)) { theme.load.background toolbar $ifmatch }
      if ($hgt(imagebuttons)) { theme.load.background buttons $ifmatch }
      if ($hgt(imageswitchbar)) { theme.load.background switchbar $ifmatch }
    }

    theme.load.show 2 11 0 11
    theme.load.show 3 4 0 4
    if (w isin %fl) { wins -r }
    if ($dialog(theme.load)) { dialog -x theme.load }
    if ($dialog(themes)) { did -e themes 3,4 }
  }
  else { themes }
}

alias -l theme.load.hash {
  if ($left($1,1) == -) && ($2 isnum) { var %fl = $1,%s = $2,%f = $3- }
  elseif ($left($1,1) == -) && ($2 !isnum) { var %fl = $1,%f = $2- }
  elseif ($1 isnum) { var %s = $1,%f = $2- }
  else { var %f = $1- }
  var %t = $iif($right(%f,4) == .mts,m,x),%ha = $iif(p isin %fl,hap,hat)

  if (%t == m) {
    if ($fopen(pre)) { .fclose pre }
    .fopen pre " $+ %f $+ "
    if ($ferr) { .fclose pre | return }
    if (%s) { .fseek -w pre $+($chr(91),scheme $+ %s,$chr(93)) | var %l = $fread(pre) }
    var %r1 = 1,%r2 = $lines(%f)
    while (!$feof) {
      if ($fread(pre)) {
        tokenize 32 $ifmatch
        if ($1 == [mts]) || (!$1) || ($left($1,1) == $chr(59)) { continue }
        elseif ($+($chr(91),scheme*,$chr(93)) iswm $1) { .fclose pre | break }
        else { %ha $lower($1) $2- }
      }
      theme.load.show 3 %r1 0 %r2
      inc %r1
    }
    if ($fopen(pre)) { .fclose pre }

    did -ra theme.load 3 0 0 1
    hat schemenames $theme.info(schemenames,%f)
    if (h isin %fl) {
      var %i = 1,%sn = $hgt(schemenames)
      while ($gettok(%sn,%i,59)) {
        hat colors.scheme $+ %i $theme.info(%i,colors,%f)
        hat rgbcolors.scheme $+ %i $theme.info(%i,rgbcolors,%f)
        theme.load.show 3 %i 0 $numtok(%sn,59)
        inc %i
      }
      hat allschemesloaded 1
    }
    hat colors.theme $hgt(colors)
    hat rgbcolors.theme $hgt(rgbcolors)
    theme.load.show 3 1 0 1
  }

  elseif (%t == x) {
    xml CreateParser xts
    xml StartElement xts theme.xts.parse -s $+ %s
    xml EndElement xts theme.xts.parse -e
    xml Data xts theme.xts.parse -dt
    xml File xts " $+ %f $+ "
    xml ParseFile xts
    xml FreeParser xts
    unset %section %item %scheme
    hat name $left($nopath(%f),-4)

    theme.load.show 3 0 0 1
    hat schemenames $theme.info(schemenames,%f)
    if (h isin %fl) {
      var %i = 1,%sn = $hgt(schemenames)
      while ($gettok(%sn,$calc(%i + 1),59)) {
        hat colors.scheme $+ %i $theme.info(%i,colors,%f)
        hat rgbcolors.scheme $+ %i $theme.info(%i,rgbcolors,%f)
        theme.load.show 3 %i 0 $numtok(%sn,59)
        inc %i
      }
      hat allschemesloaded 1
    }
    hat colors.theme $hgt(colors)
    hat rgbcolors.theme $hgt(rgbcolors)
    theme.load.show 3 1 0 1
  }
}

alias theme.xts.parse {
  if (-s isin $1) {
    if ($3 == info) && ($right($1,-2) !isnum) { %section = info | unset %item }
    elseif ($3 == default) || ((scheme* iswm $3) && ($3 != schemes)) && (!%scheme) {
      if ($right($1,-2) isnum) && ($3 == scheme $+ $right($1,-2)) { %scheme = $3 }
      elseif ($1 == -s) { %scheme = default }
      unset %section %item
    }
    elseif (%section == info) { %item = $iif($3 == desc,description,$3) }

    elseif (%scheme) {
      if (($3 == font) || ($3 == image) || ($3 == cnick) || ($3 == text) || ($3 == notice) || ($3 == action) || ($3 == event) || ($3 == ctcp) || ($3 == raw)) && (%scheme) { %section = $3 | unset %item }
      elseif (!%section) {
        if ($3 == pre) { %item = prefix }
        elseif ($3 == timestamp) { %item = timestampformat }
        else { %item = $3 }
      }
      elseif (%section == font) { %item = font $+ $iif($3 == base,default,$3) }
      elseif (%section == image) { %item = image $+ $3 }
      elseif (%section == cnick) { %item = cline $+ $iif($3 == reg,regular,$3) }
      elseif (%section == text) { %item = text $+ $iif($3 == targetself,msgself,$3) }
      elseif (%section == notice) {
        if ($3 == reg) { %item = notice }
        elseif ($3 == regself) { %item = noticeself }
        elseif ($3 == chanself) { %item = noticeselfchan }
        else { %item = notice $+ $3 }
      }
      elseif (%section == action) { %item = action $+ $3 }
      elseif (%section == event) {
        if ($3 == modechan) { %item = mode }
        elseif ($3 == unnotify) { %item = unotify }
        elseif ($3 == wallops) { %item = wallop }
        elseif ($3 == snotice) { %item = noticeserver }
        elseif ($3 == dnsstart) { %item = dns }
        elseif ($3 == dnsfind) { %item = dnsresolve }
        else { %item = $3 }
      }
      elseif (%section == ctcp) {
        if ($3 == reg) { %item = ctcp }
        elseif ($3 == regself) { %item = ctcpself }
        else { %item = ctcp $+ $3 }
      }
      elseif (%section == raw) { %item = raw. $+ $right($3,-1) }
      else { unset %item }
    }
  }
  elseif ($1 == -e) {
    if ($3 == info) { unset %section %item }
    elseif ($3 == %scheme) { xml FreeParser xts }
  }
  elseif (-d isin $1) && (%item) {
    if (image* iswm %item) { $iif(t isin $1,hat,hap) $lower(%item) $gettok($3-,2,44) $gettok($3-,1,44) }
    else { $iif(t isin $1,hat,hap) $lower(%item) $3- }
  }
}


alias -l theme.load.resetcolors {
  remini $shortfn($nofile($mircini)) $+ $nopath($mircini) colors
  remini $shortfn($nofile($mircini)) $+ $nopath($mircini) palettes
  wi colors n0 mIRC Classic,0,6,4,5,2,3,3,3,3,3,3,1,5,7,6,1,3,2,3,5,1,0,1,0,1,15,7,0
  wi colors n1 mIRC Modern,0,6,4,7,2,3,4,3,3,3,3,1,5,2,6,1,14,2,3,5,1,0,1,0,1,14,5,0
  wi colors n2 Monochrome State,1,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,1,15,1,15,15,15,14
  wi colors n3 Placid Hues,0,2,4,7,2,3,3,3,3,15,3,1,5,7,6,1,3,2,3,5,1,0,1,0,1,15,6,0
  wi colors n4 Rainbow Sky,0,7,4,5,1,1,3,3,8,13,3,14,2,7,13,5,3,8,3,4,14,0,5,0,3,14,10,0
  wi palettes n0 16777215,0,8323072,37632,255,127,10223772,32764,65535,64512,9671424,16776960,16515072,16711935,8355711,13816530
  wi palettes n1 16777215,0,11010048,3299627,240,4737160,8388720,26832,1632504,57344,94740,16776960,16515072,16711935,8355711,13816530
  wi palettes n2 16777215,0,8323072,37632,255,127,10223772,32764,65535,64512,9671424,16776960,16515072,16711935,8355711,13816530
  wi palettes n3 15658734,0,12140,1508038,255,10964547,6579262,33023,65535,4227072,9474048,9920537,16711680,16711935,6579300,8553090
  wi palettes n4 16777215,3618615,12087408,16744448,255,32764,65535,43008,9671424,16776960,16515072,16711935,8355711,16711808,8355711,13816530
  if ($show) { color -ls mIRC Classic }
}
alias -l theme.load.color {
  if ($remove($1,$chr(44)) isnum) { return $1- }
  else {
    if ($1 == Aliceblue) { return 240,248,255 }
    elseif ($1 == Antiquewhite) { return 250,235,215 }
    elseif ($1 == Aqua) { return 0,255,255 }
    elseif ($1 == Aquamarine) { return 127,255,212 }
    elseif ($1 == Azure) { return 240,255,255 }
    elseif ($1 == Beige) { return 245,245,220 }
    elseif ($1 == Bisque) { return 255,228,196 }
    elseif ($1 == Black) { return 0,0,0 }
    elseif ($1 == Blanchedalmond) { return 255,235,205 }
    elseif ($1 == Blue) { return 0,0,255 }
    elseif ($1 == Blueviolet) { return 138,43,226 }
    elseif ($1 == Brown) { return 165,42,42 }
    elseif ($1 == Burlywood) { return 222,184,135 }
    elseif ($1 == Cadetblue) { return 95,158,160 }
    elseif ($1 == Chartreuse) { return 127,255,0 }
    elseif ($1 == Chocolate) { return 210,105,30 }
    elseif ($1 == Coral) { return 255,127,80 }
    elseif ($1 == Cornflowerblue) { return 100,149,237 }
    elseif ($1 == Cornsilk) { return 255,248,220 }
    elseif ($1 == Crimson) { return 220,20,60 }
    elseif ($1 == Cyan) { return 0,255,255 }
    elseif ($1 == Darkblue) { return 0,0,139 }
    elseif ($1 == Darkcyan) { return 0,139,139 }
    elseif ($1 == Darkgoldenrod) { return 184,134,11 }
    elseif ($1 == Darkgray) { return 169,169,169 }
    elseif ($1 == Darkgreen) { return 0,100,0 }
    elseif ($1 == Darkkhaki) { return 189,183,107 }
    elseif ($1 == Darkmagenta) { return 139,0,139 }
    elseif ($1 == Darkolivegreen) { return 85,107,47 }
    elseif ($1 == Darkorange) { return 255,140,0 }
    elseif ($1 == Darkorchid) { return 153,50,204 }
    elseif ($1 == Darkred) { return 139,0,0 }
    elseif ($1 == Darksalmon) { return 233,150,122 }
    elseif ($1 == Darkseagreen) { return 143,188,143 }
    elseif ($1 == Darkslateblue) { return 72,61,139 }
    elseif ($1 == Darkslategray) { return 47,79,79 }
    elseif ($1 == Darkturquoise) { return 0,206,209 }
    elseif ($1 == Darkviolet) { return 148,0,211 }
    elseif ($1 == Deeppink) { return 255,20,147 }
    elseif ($1 == Deepskyblue) { return 0,191,255 }
    elseif ($1 == Dimgray) { return 105,105,105 }
    elseif ($1 == Dodgerblue) { return 30,144,255 }
    elseif ($1 == Firebrick) { return 178,34,34 }
    elseif ($1 == Floralwhite) { return 255,250,240 }
    elseif ($1 == Forestgreen) { return 34,139,34 }
    elseif ($1 == Fuchsia) { return 255,0,255 }
    elseif ($1 == Gainsboro) { return 220,220,220 }
    elseif ($1 == Ghostwhite) { return 248,248,255 }
    elseif ($1 == Gold) { return 255,215,0 }
    elseif ($1 == Goldenrod) { return 218,165,32 }
    elseif ($1 == Gray) { return 128,128,128 }
    elseif ($1 == Green) { return 0,128,0 }
    elseif ($1 == Greenyellow) { return 173,255,47 }
    elseif ($1 == Honeydew) { return 240,255,240 }
    elseif ($1 == Hotpink) { return 255,105,180 }
    elseif ($1 == Indianred) { return 205,92,92 }
    elseif ($1 == Indigo) { return 75,0,130 }
    elseif ($1 == Ivory) { return 255,255,240 }
    elseif ($1 == Khaki) { return 240,230,140 }
    elseif ($1 == Lavender) { return 230,230,250 }
    elseif ($1 == Lavenderblush) { return 255,240,245 }
    elseif ($1 == Lawngreen) { return 124,252,0 }
    elseif ($1 == Lemonchiffon) { return 255,250,205 }
    elseif ($1 == Lightblue) { return 173,216,230 }
    elseif ($1 == Lightcoral) { return 240,128,128 }
    elseif ($1 == Lightcyan) { return 224,255,255 }
    elseif ($1 == Lightgoldenrodyellow) { return 250,250,210 }
    elseif ($1 == Lightgreen) { return 144,238,144 }
    elseif ($1 == Lightgrey) { return 211,211,211 }
    elseif ($1 == Lightpink) { return 255,182,193 }
    elseif ($1 == Lightsalmon) { return 255,160,122 }
    elseif ($1 == Lightseagreen) { return 32,178,170 }
    elseif ($1 == Lightskyblue) { return 135,206,250 }
    elseif ($1 == Lightslategray) { return 119,136,153 }
    elseif ($1 == Lightsteelblue) { return 176,196,222 }
    elseif ($1 == Lightyellow) { return 255,255,224 }
    elseif ($1 == Lime) { return 0,255,0 }
    elseif ($1 == Limegreen) { return 50,205,50 }
    elseif ($1 == Linen) { return 250,240,230 }
    elseif ($1 == Magenta) { return 255,0,255 }
    elseif ($1 == Maroon) { return 128,0,0 }
    elseif ($1 == Mediumauqamarine) { return 102,205,170 }
    elseif ($1 == Mediumblue) { return 0,0,205 }
    elseif ($1 == Mediumorchid) { return 186,85,211 }
    elseif ($1 == Mediumpurple) { return 147,112,216 }
    elseif ($1 == Mediumseagreen) { return 60,179,113 }
    elseif ($1 == Mediumslateblue) { return 123,104,238 }
    elseif ($1 == Mediumspringgreen) { return 0,250,154 }
    elseif ($1 == Mediumturquoise) { return 72,209,204 }
    elseif ($1 == Mediumvioletred) { return 199,21,133 }
    elseif ($1 == Midnightblue) { return 25,25,112 }
    elseif ($1 == Mintcream) { return 245,255,250 }
    elseif ($1 == Mistyrose) { return 255,228,225 }
    elseif ($1 == Moccasin) { return 255,228,181 }
    elseif ($1 == Navajowhite) { return 255,222,173 }
    elseif ($1 == Navy) { return 0,0,128 }
    elseif ($1 == Oldlace) { return 253,245,230 }
    elseif ($1 == Olive) { return 128,128,0 }
    elseif ($1 == Olivedrab) { return 104,142,35 }
    elseif ($1 == Orange) { return 255,165,0 }
    elseif ($1 == Orangered) { return 255,69,0 }
    elseif ($1 == Orchid) { return 218,112,214 }
    elseif ($1 == Palegoldenrod) { return 238,232,170 }
    elseif ($1 == Palegreen) { return 152,251,152 }
    elseif ($1 == Paleturquoise) { return 175,238,238 }
    elseif ($1 == Palevioletred) { return 216,112,147 }
    elseif ($1 == Papayawhip) { return 255,239,213 }
    elseif ($1 == Peachpuff) { return 255,218,185 }
    elseif ($1 == Peru) { return 205,133,63 }
    elseif ($1 == Pink) { return 255,192,203 }
    elseif ($1 == Plum) { return 221,160,221 }
    elseif ($1 == Powderblue) { return 176,224,230 }
    elseif ($1 == Purple) { return 128,0,128 }
    elseif ($1 == Red) { return 255,0,0 }
    elseif ($1 == Rosybrown) { return 188,143,143 }
    elseif ($1 == Royalblue) { return 65,105,225 }
    elseif ($1 == Saddlebrown) { return 139,69,19 }
    elseif ($1 == Salmon) { return 250,128,114 }
    elseif ($1 == Sandybrown) { return 244,164,96 }
    elseif ($1 == Seagreen) { return  46,139,87 }
    elseif ($1 == Seashell) { return  255,245,238 }
    elseif ($1 == Sienna) { return  160,82,45 }
    elseif ($1 == Silver) { return  192,192,192 }
    elseif ($1 == Skyblue) { return  135,206,235 }
    elseif ($1 == Slateblue) { return  106,90,205 }
    elseif ($1 == Slategray) { return  112,128,144 }
    elseif ($1 == Snow) { return  255,250,250 }
    elseif ($1 == Springgreen) { return  0,255,127 }
    elseif ($1 == Steelblue) { return  70,130,180 }
    elseif ($1 == Tan) { return  210,180,140 }
    elseif ($1 == Teal) { return  0,128,128 }
    elseif ($1 == Thistle) { return  216,191,216 }
    elseif ($1 == Tomato) { return  255,99,71 }
    elseif ($1 == Turquoise) { return  64,224,208 }
    elseif ($1 == Violet) { return  238,130,238 }
    elseif ($1 == Wheat) { return  245,222,179 }
    elseif ($1 == White) { return  255,255,255 }
    elseif ($1 == Whitesmoke) { return 245,245,245 }
    elseif ($1 == Yellow) { return 255,255,0 }
    elseif ($1 == YellowGreen) { return 154,205,50 }
  }
}
alias -l theme.load.nicks {
  if ($hgt($2)) { .cnick - $+ $1 $iif($2 == clineme,$!me,*) $iif($hgt($2),$ifmatch) $3 }
}
alias -l theme.load.font {
  if ($gettok($2-,-1,32) == b) { var %f = $gettok($2-,1- $+ $calc($0 - 3),32),%s = $gettok($2-,-2,32),%b = b }
  else { var %f = $gettok($2-,1- $+ $calc($0 - 2),32),%s = $gettok($2-,-1,32) }
  var %ini = $+(%f,$chr(44),$calc(%s + $iif(%b,700,400)),$chr(44),1)
  if ($hg(theme.fontreplace)) && ($hg(theme.frep. $+ $replace(%f,$chr(32),\~))) {
    tokenize 44 default, $+ $ifmatch
    %f = $2
    if ($remove($3,$chr(32)) isnum) { %s = $3 }
    if ($4 == b) { var %b = b }
  }

  if ($isfont(%f)) && (%s) {
    if ($1 == default) {
      var %serv = $scon(0),%win = $window(0)
      while (%serv) { scon %serv | font -ds $+ %b %s %f | dec %serv }
      while (%win) { font -d $window(%win) %s %f | dec %win }
      if ($window(Finger Window)) { font -dg $+ %b %s %f }
      else { wi fonts ffinger %ini }
      wi fonts flinks %ini
      wi fonts flist %ini
      wi fonts fwwwlist %ini
      wi fonts fnotify %ini
      wi fonts fdccg %ini
      wi fonts fdccs %ini
    }
    elseif ($1 == chan) {
      var %serv = $scon(0),%a,%p = -d $+ %b
      while (%serv) {
        scon %serv
        var %i = $chan(0)
        while (%i) { font %p $chan(%i) %s %f | dec %i | inc %a }
        dec %serv
      }
      if (!%a) { wi fonts fchannel %ini }
    }
    elseif ($1 == query) {
      var %serv = $scon(0),%a,%a2,%a3,%p = -d $+ %b
      while (%serv) {
        scon %serv
        var %i = $query(0),%c = $chat(0),%j = $fserv(0)
        while (%i) { font %p $query(%i) %s %f | dec %i | inc %a }
        while (%c) { font %p $chat(%c) %s %f | dec %c | inc %a2 }
        while (%j) { font %p $fserv(%j) %s %f | dec %j | inc %a3 }
        dec %serv
      }
      if (!%a) { wi fonts fquery %ini }
      if (!%a2) { wi fonts fchat %ini }
      if (!%a3) { wi fonts fserv %ini }
      wi fonts fmessage %ini
    }
  }
}
alias -l theme.load.resetfont {
  remi fonts
  theme.load.font default fixedsys 9
  theme.load.font chan fixedsys 9
  theme.load.font query fixedsys 9
}
alias -l theme.load.background {
  var %s = $iif($1 != buttons,$2),%f = $nofile($hgt(file)) $+ $iif($1 != buttons,$3-,$2-),%ini = $+(",%f,",$chr(44),$calc($findtok(center fill normal stretch tile photo,%s,1,32) -1))
  if ($isfile(%f)) {
    %s = $iif(%s == stretch,r,$left(%s,1))
    if ($1 == mirc) { background -me $+ %s " $+ %f $+ " }
    elseif ($1 == toolbar) { background -l $+ %s " $+ %f $+ " }
    elseif ($1 == buttons) { background -u " $+ %f $+ " }
    elseif ($1 == switchbar) { background -h $+ %s " $+ %f $+ " }
    else {
      var %serv = $scon(0)
      while (%serv) {
        scon %serv
        if ($1 == status) { background -se $+ %s " $+ %f $+ " }
        elseif ($1 == chan) {
          var %c = $chan(0),%a
          while (%c) { background -e $+ %s $chan(%s) " $+ %f $+ " | dec %c | inc %a }
          if (!%a) { wi background wchannel %ini }
        }
        elseif ($1 == query) {
          var %q = $query(0),%c = $chat(0),%a,%a2
          while (%q) { background -e $+ %s $query(%q) " $+ %f $+ " | dec %q | inc %a }
          while (%c) { background -e $+ %s $chat(%s) " $+ %f $+ " | dec %c | inc %a2 }
          if (!%a) { wi background wquery %ini }
          if (!%a2) { wi background wchat %ini }
        }
        dec %serv
      }
    }
  }
}
alias -l theme.load.resetback {
  remi background
  var %s = $scon(0)
  while (%s) {
    scon %s
    background -xs
    background -xm
    background -xl
    background -xu
    background -xh
    background -xg
    var %c = $chan(0),%q = $query(0),%d = $chat(0),%w = $window(0)
    while (%c) { background -x $chan(%c) | dec %c }
    while (%q) { background -x $query(%q) | dec %q }
    while (%d) { background -x $+(=,$chat(%d)) | dec %d }
    while (%w) { if ($window(%w).type != picture) background -x $window(%w) | dec %w }
    dec %s
  }
}


alias theme.unload {
  if (!$theme.current) { return }
  if (w isin $1) || (a isin $1) { wins -n }
  if (l isin $1) || (a isin $1) { scid -a clearall }
  if ($dialog(themes)) { did -b themes 4 }
  if ($dialog(theme.load)) { dialog -x theme.load }
  if ($show) { dialog -m theme.load theme.load | dialog -t theme.load Unloading theme... }
  if ($theme.current == mts) && ($hgt(unload)) {
    theme.vars echo $color(info) $+(-at,$iif($hgt(indent),i $+ $ifmatch))
    if ($gettok($hgt(unload),1,32) == !script) { $eval($gettok( [ $hgt(unload) ] ,2-,32),2) }
    else { %:echo $theme.parse(m,$hgt(unload)) }
  }

  ;; STEP ONE
  theme.load.show 1 Restoring colors
  theme.load.resetcolors
  theme.load.show 2 1 0 7

  ;; STEP TWO
  theme.load.show 1 Removing nickcolors
  .cnick -r $!me
  while ($cnick(0)) { .cnick -r * }
  theme.load.show 2 2 0 7

  ;; STEP THREE
  theme.load.show 1 Restoring rgb colors
  theme.load.show 2 3 0 7

  ;; STEP FOUR
  theme.load.show 1 Removing background images
  theme.load.resetback
  theme.load.show 2 4 0 7

  ;; STEP FIVE
  theme.load.show 1 Restoring fonts
  theme.load.resetfont
  theme.load.show 2 5 0 7

  ;; STEP SIX
  theme.load.show 1 Restoring timestamp
  .timestamp on
  .timestamp -f [HH:nn]
  theme.load.show 2 6 0 7

  ;; STEP SEVEN
  theme.load.show 1 Unloading files
  if ($script($hgt(script))) { .unload -rsn $shortfn($nofile($hgt(script))) $+ $nopath($hgt(script)) }
  if ($hget(theme.current)) { hfree theme.current }
  if ($isfile($shortfn($scriptdir) $+ current.src)) { .remove $shortfn($scriptdir) $+ current.src }

  theme.load.show 2 7 0 7
  if ($dialog(theme.load)) { dialog -x theme.load }
  if (w isin $1) { wins -r }
}

alias theme.scheme {
  ha theme.edit.curscheme $iif($1,scheme $+ $1,theme)
  if ($1) { hat scheme $gettok($hgt(schemenames),$iif($theme.current == mts,$1,$calc($1 + 1)),59) }
  else { hat scheme }

  if ($hgt(allschemesloaded)) {
    if ($iif($hgt(scheme),$hgt(name) - $hgt(scheme),$hgt(name)) $+ ,* iswm $ri(colors,$theme.colornum($hgt(name) $iif($hgt(scheme),- $hgt(scheme))))) { .color -ls $hgt(name) $iif($hgt(scheme),- $hgt(scheme)) }
    else { .theme.refresh | .color -ls $hgt(name) $iif($hgt(scheme),- $hgt(scheme)) }
  }
  else {
    $iif(!$show,.) $+ theme.load - $+ $iif(a isin $theme.options(load),fcnsbite,$theme.options(load)) $+ $iif(h isin $theme.options(load),h) $+ $iif(a isin $theme.options(add),wlu,$theme.options(add)) $iif($1,$1) $hgt(file)
  }
}


alias theme.refresh {
  .theme.load.resetcolors
  wi text theme $hgt(name) $iif($hgt(scheme),- $hgt(scheme))
  wi palettes $theme.colornum($hgt(name)) $hgt(rgbpalette.theme)
  wi colors $theme.colornum($hgt(name)) $hgt(colorspalette.theme)
  var %i = 1,%s = $hgt(schemenames)
  while ($gettok(%s,$iif($theme.current == mts,%i,$calc(%i + 1)),59)) {
    wi palettes $theme.colornum($hgt(name) - $ifmatch) $hgt(rgbpalette.scheme $+ %i)
    wi colors $theme.colornum($hgt(name) - $ifmatch) $hgt(colorspalette.scheme $+ %i)
    inc %i
  }
  if ($show) && ($hgt(allschemesloaded)) { .color -ls $hgt(name) $iif($hgt(scheme),- $hgt(scheme)) }
}

;###################### ALIASES #########################

alias say {
  if (!$theme.current) || (!$server) { say $1- }
  elseif ($1 != $null) && (($active ischan) || ($query($active)) || ($left($active,1) == $chr(61))) { msg $active $1- }
}
alias say {
  if (!$theme.current) || (!$server) {
    if ($active == Status Window) { echo -a * You can't do this in the status window }
    else { say $1- }
  }
  elseif ($1 != $null) && (($active ischan) || ($query($active)) || ($left($active,1) == $chr(61))) { msg $active $1- }
}

alias msg {
  if (!$theme.current) || (!$server) { msg $1- }
  elseif ($2 != $null) {
    if ($show) {
      if (!$window($1)) || ($1 != $active) { var %t = textmsgself }
      elseif ($1 ischan) { var %t = textchanself }
      else { var %t = textqueryself }
      if ($hgt(%t)) && ($gettok($hg(theme.event.INPUT),1,32) != 2) {
        var %i =  $iif($hgt(indent),i $+ $ifmatch)
        theme.vars echo $color(own) $iif(%t == textmsgself,-ta $+ %i,-tm $+ %i $1)
        set -nu %::nick $me
        set -nu %::chan $1
        set -nu %::target $1
        set -nu %::address $address($me,5)
        if ($1 ischan) { set -u %::cmode $cmode($me,$1) }
        set -u %::cnick $hgt(clineme)
        set -nu %::me $me
        set -nu %::text $2-
        var %m = .msg
        set -nu %:raw $1-
        theme.text %t INPUT $iif($target,$ifmatch,$active) %::text
        if ($window($1)) && ($1 != $active) {
          theme.vars echo $color(own) -tm $+ %i $1
          theme.text textqueryself $targe  %::text
        }
      }
      else { var %m = msg }
    }
    else { var %m = .msg }
    %m $1-
  }
}
alias dns {
  if (!$theme.current) || (!$server) { dns $1- }
  elseif ($hgt(dns)) {
    if ($left($1,1) == -) { var %t = $2 }
    else { var %t = $1 }
    theme.vars echo $color(other) -a $+ $iif($hgt(indent),i $+ $ifmatch)
    set -u %::nick %t
    set -u %::address %t
    set -u %:raw $1-
    .dns $1-
    if ($show) { theme.text dns DNS %t }
  }
  else { $iif($show,dns,.dns) $1- }
}
alias describe {
  if (!$theme.current) || (!$server) { describe $1- }
  else {
    if ($1 ischan) || ($1 == Status) { var %t = actionchanself }
    else { var %t = actionqueryself }
    if ($1 != $null) && ($1 ischan) || ($query($1)) || ($left($1,1) == $chr(61)) {
      if ($show) {
        if ($hgt($iif($active ischan,actionchanself,actionqueryself))) && ($gettok($hg(theme.event.ACTION),1,32) != 2) {
          theme.vars echo $color(action) -tm $+ $iif($hgt(indent),i $+ $ifmatch) $1
          set -nu %::nick $active
          set -nu %::chan $active
          set -nu %::address $address($me,5)
          if ($active ischan) { set -u %::cmode $cmode($me,$active) }
          set -u %::cnick $cnick($me).color
          set -nu %::me $me
          set -nu %::text $2-
          set -u %:raw $1-
          var %m = .describe
          theme.text %t ACTION %::text
        }
        else { var %m = describe }
      }
      else { var %m = .describe }
      %m $1-
    }
  }
}
alias me {
  if (!$theme.current) || (!$server) { me $1- }
  elseif ($1 != $null) && (($active ischan) || ($query($active)) || ($left($active,1) == $chr(61))) { describe $active $1- }
}
alias amsg {
  if (!$theme.current) || (!$server) { amsg $1- }
  elseif ($1 != $null) {
    set -u %:raw $1-
    theme.msg amsg textchanself $1-
    %m $1-
  }
}
alias ame {
  if (!$theme.current) || (!$server) { ame $1- }
  elseif ($1 != $null) {
    set -u %:raw $1-
    theme.msg ame actionchanself $1-
    %m $1-
  }
}
alias qmsg {
  if (!$theme.current) || (!$server) { qmsg $1- }
  elseif ($1 != $null) {
    set -u %:raw $1-
    theme.msg qmsg textqueryself
    %m $1-
  }
}
alias qme {
  if (!$theme.current) || (!$server) { qme $1- }
  elseif ($1 != $null) {
    set -u %:raw $1-
    theme.msg qme textqueryself
    %m $1-
  }
}
alias notice {
  if (!$theme.current) || (!$server) { notice $1- }
  elseif ($2 != $null) {
    if ($show) {
      if ($hgt($iif($left($1,1) == $chr(35),noticechanself,noticeself))) {
        theme.vars echo $color(notice) -ta $+ $iif($hgt(indent),i $+ $ifmatch)
        set -nu %::nick $1
        set -nu %::target $1
        set -nu %::chan $1
        set -nu %::address $address($me,5)
        if ($1 ischan) { set -u %::cmode $cmode($me,$1) }
        set -u %::cnick $cnick($me).color
        set -nu %::me $me
        set -nu %::text $2-
        var %m = .notice
        set -u %:raw $1-
        theme.text $iif($left($1,1) == $chr(35),noticechanself,noticeself) NOTICE
      }
      else { var %m = notice }
    }
    else { var %m = .notice }
    %m $1-
  }
}

alias ctcp {
  if (!$theme.current) || (!$server) { ctcp $1- }
  elseif ($2 != $null) {
    set -u %:raw $1-
    theme.ctcp ctcp CTCP $1-
    %m $iif($2 == PING,$1 $2 $ctime,$1-)
    theme.sound ctcpself
  }
}
alias ctcpreply {
  if (!$theme.current) || (!$server) { ctcpreply $1- }
  elseif ($2 != $null) {
    set -u %:raw $1-
    theme.ctcp ctcpreply CTCPREPLY $1-
    %m $1-
    theme.sound ctcpreplyself
  }
}


alias -l theme.msg {
  if ($show) {
    set -u %::address $address($me,5)
    set -u %::cnick $cnick($me).color
    set -u %::text $3-
    set -u %m . $+ $1
    var %i = 1
    while ($chan(%i)) {
      theme.vars echo $color($iif(($1 == amsg) || ($1 == qmsg),own,action)) -t $+ $iif($hgt(indent),i $+ $ifmatch) $chan(%i)
      set -u %::me $me
      set -u %::cmode $cmode($me,$chan(%i))
      set -u %::nick $nick
      set -u %::chan $chan(%i)
      theme.text $2 $iif(($1 == amsg) || ($1 == qmsg),INPUT,ACTION) $iif(($2 == textchanself) || ($2 == textqueryself),$me) %::text
      inc %i
    }
  }
  else { set -u %m . $+ $1 }
}
alias -l theme.ctcp {
  if ($show) {
    if ($hgt($+($1,$iif($left($3,1) == $chr(35),chanself,self)))) {
      theme.vars echo $color(own) -at $+ $iif($hgt(indent),i $+ $ifmatch)
      set -u %::nick $3
      set -u %::target $3
      set -u %::chan $3
      set -u %::ctcp $upper($4)
      set -u %::text $5-
      set -nu %m . $+ $1
      var %t = $iif($1 == ctcpreply,ctcpreplyself,ctcp)
      theme.text %t $2-
    }
    else { set -nu %m $1 }
  }
  else { set -nu %m . $+ $1 }
}
alias -l theme.textevent {
  var %i = $iif($hgt(indent),i $+ $ifmatch)
  if ($chan) {
    var %t = -tlm $+ %i $chan
    if ($event == text) { var %e = textchan }
    elseif ($event == action) { var %e = actionchan }
    else { var %e = noticechan }
    set -nu %::chan $chan
    set -u %::cmode $cmode($nick,$chan)
    set -u %::cnick $nick($chan,$nick).color
  }
  else {
    var %t = $iif($event != notice,$iif($query($nick),-tlm $+ %i $nick,-tldm $+ %i),-tlam $+ %i)
    if ($event == text) { var %e = $iif($query($nick),textquery,textmsg) }
    elseif ($event == action) { var %e = actionquery }
    else { var %e = notice }
    set -u %::cnick $cnick($nick).color
  }
  if ($hgt(%e)) {

    if ($highlight($nick).nicks) && ($highlight) {
      if ($highlight($nick).sound != none) {
        var %s = $ifmatch
        $iif(%s == beep,%s,.splay -wmp $+(",%s,"))
      }
      if ($highlight($nick).color) { var %c = $ifmatch - 1 }
      if ($highlight($nick).flash) { flash $+(-r,$ifmatch) $highlight($nick).message }
    }
    theme.vars echo $iif(%c != $null,%c,$color($iif($event == text,normal,$event))) %t
    set -nu %::nick $nick
    set -nu %::address $iif($address,$ifmatch,N/A)
    set -nu %::text $1-
    theme.text %e $upper($event) %::text
  }
}
alias -l theme.joinpart {
  if ($hgt($1)) {
    theme.vars echo $color($event) -t $+ $iif($hgt(indent),i $+ $ifmatch) $chan
    set -nu %::nick $nick
    set -nu %::address $iif($address,$ifmatch,N/A)
    set -nu %::chan $chan
    set -nu %::text $2-
    theme.text $1 $upper($event) %::text
  }
}
alias -l theme.nickquit {
  var %i = 1
  while ($comchan($2,%i)) {
    theme.vars echo $color($event) -t $+ $iif($hgt(indent),i $+ $ifmatch) $comchan($2,%i)
    theme.text $1 $upper($event) %::text
    inc %i
  }
}
alias -l theme.notify {
  if ($hgt($event)) {
    theme.vars echo $color(notify) -at $+ $iif($hgt(indent),i $+ $ifmatch)
    set -nu %::nick $nick
    set -nu %::address $notify($nick).addr
    set -nu %::text $notify($nick).note
    theme.text $event $upper($event) %::text
  }
}

;####################### EVENTS #######################

on &*:INPUT:*:{
  if ($hgt(file)) && ($left($1,1) != $cmdchar) && ($left($1,1) != /) && ($theme.current) {
    say $1-
    halt
  }
}
on ^&*:TEXT:*:*:{
  if ($theme.current) {
    set -u %:raw $1-
    theme.textevent $1-
  }
}
on ^&*:ACTION:*:*:{
  if ($theme.current) {
    set -u %:raw $1-
    theme.textevent $1-
  }
}
on ^&*:NOTICE:*:*:{
  if ($theme.current) {
    set -u %:raw $1-
    theme.textevent $1-
    theme.sound notice $+ $iif($nick == $me,self)
  }
}
on ^&*:CHAT:*:{
  if ($hgt(textquery)) && ($theme.current) {
    theme.vars echo $color(normal) -mlt $+ $iif($hgt(indent),i $+ $ifmatch) = $+ $nick
    set -nu %::nick $nick
    set -nu %::address $iif($address,$ifmatch,N/A)
    set -nu %::text $1-
    set -u %::cnick $cnick($nick).color
    set -nu %::nick $nick
    set -u %:raw $1-
    theme.text textquery CHAT %::text
  }
  theme.sound chat
}

on ^&*:JOIN:#:{
  if ($theme.current) {
    set -u %:raw $1-
    theme.joinpart $event $+ $iif($nick == $me,self)
    theme.sound join $+ $iif($nick == $me,self)
  }
}
on ^&*:PART:#:{
  if ($theme.current) {
    if ($nick != $me) { set -u %:raw $1- | theme.joinpart $event $1- }
    theme.sound part $+ $iif($nick == $me,self)
  }
}

on ^&*:NICK:{
  if ($hgt($iif($nick == $me,nickself,nick))) && ($theme.current) {
    set -nu %::nick $nick
    set -nu %::newnick $newnick
    set -nu %::address $iif($address,$ifmatch,N/A)
    set -u %:raw $1-
    theme.nickquit $iif($nick == $me,nickself,nick) $newnick
  }
  theme.sound nick $+ $iif($nick == $me,self)
}
on ^&*:QUIT:{
  if ($hgt(quit)) && ($theme.current) {
    set -nu %::nick $nick
    set -nu %::address $iif($address != $null,$ifmatch,N/A)
    set -nu %::text $1-
    set -u %:raw $1-
    theme.nickquit quit $nick
  }
  theme.sound quit $+ $iif($nick == $me,self)
}

on ^*:NOTIFY:{
  if ($theme.current) {
    set -u %:raw $1-
    theme.notify
    theme.sound notify
  }
}
on ^*:UNOTIFY:{
  if ($theme.current) {
    set -u %:raw $1-
    theme.notify
    theme.sound unotify
  }
}

on &*:DNS:{
  var %t = $iif($dns(0) > 0,dnsresolve,dnserror)
  if ($hgt(%t)) && ($theme.current) {
    theme.vars echo $color(other) -a $+ $iif($hgt(indent),i $+ $ifmatch)
    set -nu %::nick $nick
    set -nu %::address $address
    set -nu %::iaddress $iaddress
    set -nu %::naddress $naddress
    if (%t == dnsresolve) {
      var %i = 1
      while ($dns(%i)) {
        set -nu %::address $dns(%i).ip
        set -nu %::raddress $dns(%i)
        set -u %:raw $1-
        theme.text %t DNS $address $iaddress $naddress $nick
        inc %i
      }
    }
    else { theme.text %t DNS $address $iaddress $naddress $nick }
  }
}

on ^&*:USERMODE:{
  if ($hgt(modeuser)) && ($theme.current) {
    theme.vars echo $color(mode) -st $+ $iif($hgt(indent),i $+ $ifmatch)
    set -nu %::nick $nick
    set -nu %::modes $1-
    set -u %:raw $1-
    theme.text modeuser USERMODE %::modes
  }
  theme.sound usermode
}
on ^&*:ERROR:*:{
  if ($hgt(servererror)) && ($theme.current) {
    theme.vars echo $color(info) -st $+ $iif($hgt(indent),i $+ $ifmatch)
    set -nu %::text $1-
    set -u %:raw $1-
    theme.text servererror ERROR %::text
    halt
  }
  theme.sound error
}
on ^&*:SNOTICE:*:{
  if ($hgt(noticeserver)) && ($theme.current) {
    theme.vars echo $color(info) -st $+ $iif($hgt(indent),i $+ $ifmatch)
    set -nu %::nick $nick
    set -nu %::address $iif($address != $null,$ifmatch,N/A)
    set -nu %::text $1-
    set -u %:raw $1-
    theme.text noticeserver SNOTICE %::text
  }
  theme.sound snotice
}
on ^&*:INVITE:#:{
  if ($hgt(invite)) && ($theme.current) {
    theme.vars echo $color(invite) -at $+ $iif($hgt(indent),i $+ $ifmatch)
    set -nu %::nick $nick
    set -nu %::address $iif($address != $null,$ifmatch,N/A)
    set -nu %::chan $chan
    set -nu %::text $1-
    set -u %:raw $1-
    theme.text invite INVITE %::text
  }
  theme.sound invite $+ $iif($nick == $me,self)
}
on ^&*:TOPIC:#:{
  if ($hgt(topic)) && ($theme.current) {
    theme.vars echo $color(topic) -t $+ $iif($hgt(indent),i $+ $ifmatch) $chan
    set -u %::nick $nick
    set -u %::address $iif($address,$ifmatch,N/A)
    set -u %::chan $chan
    set -u %::text $1-
    set -u %:raw $1-
    theme.text topic TOPIC %::text
  }
  theme.sound topic $+ $iif($nick == $me,self)
}
on ^&*:KICK:#:{
  var %t = $iif($knick == $me,kickself,$event),%i = $iif($hgt(indent),i $+  $ifmatch)
  if ($hgt(%t)) && ($theme.current) {
    theme.vars echo $color(kick) -t $+ %i $chan
    set -u %::nick $nick
    set -u %::address $address
    set -u %::chan $chan
    set -u %::knick $knick
    set -u %::kaddress $address($knick,5)
    set -u %::text $1-
    set -u %:raw $1-
    theme.text %t KICK %::text
    if %t == kickself {
      theme.vars echo $color(kick) -st $+ %i
      theme.text %t KICK %::text
    }
  }
  theme.sound kick $+ $iif($nick == $me,self)
}



on ^&*:RAWMODE:#:{
  if ($hgt(mode)) && ($theme.current) {
    theme.vars echo $color(mode) -t $+ $iif($hgt(indent),i $+ $ifmatch) $chan
    set -u %::nick $nick
    set -u %::address $iif($address,$ifmatch,N/A)
    set -u %::chan $chan
    set -u %::modes $1-
    set -u %:raw $1-
    theme.text mode RAWMODE %::modes
  }
}
on ^&*:WALLOPS:*:{
  if ($hgt(wallops)) && ($theme.current) {
    theme.vars echo  $color(wallops) -st $+ $iif($hgt(indent),i $+ $ifmatch)
    set -nu %::nick $nick
    set -nu %::address $iif($address != $null,$ifmatch,N/A)
    set -nu %::text $1-
    set -u %:raw $1-
    theme.text wallops WALLOPS %::text
  }
}

ctcp &*:*:*:{
  if ($hgt($iif($chan,ctcpchan,ctcp))) && ($theme.current) {
    if ($1 == VERSION) || ($1 == DCC) { return }
    theme.vars echo $color(ctcp) -at $+ $iif($hgt(indent),i $+ $ifmatch)
    set -nu %::nick $nick
    set -nu %::address $iif($address,$ifmatch,N/A)
    set -nu %::chan $chan
    set -nu %::ctcp $1
    set -nu %::text $2-
    if ($1 == PING) { set -un %ctcpreply $2- }
    elseif ($1- == TIME) { set -un %ctcpreply $fulldate }
    elseif ($1- == FINGER) { set -un %ctcpreply $fullname ( $+ $emailaddr $+ ) Idle $idle second(s) $readini(mirc.ini,text,finger) }
    if (%ctcpreply) { .ctcpreply $nick $1 %ctcpreply }
    set -u %:raw $1-
    theme.text $iif($chan,ctcpchan,ctcp) CTCP
  }
  theme.sound ctcp
}
on &*:CTCPREPLY:*:{
  if ($hgt(ctcpreply)) && ($theme.current) {
    theme.vars echo $color(ctcp) -at $+ $iif($hgt(indent),i $+ $ifmatch)
    set -nu %::nick $nick
    set -nu %::chan $chan
    set -nu %::ctcp $1
    set -nu %::text $2-
    set -u %:raw $1-
    theme.text ctcpreply CTCPREPLY $1-
  }
  theme.sound ctcpreply
}

on *:CONNECT:{ theme.sound connect }
on *:DISCONNECT:{ theme.sound disconnect }
on *:OP:#:{ theme.sound op $+ $iif($nick == $me,self) }
on *:DEOP:#:{ theme.sound deop $+ $iif($nick == $me,self) }
on *:HELP:#:{ theme.sound hop $+ $iif($nick == $me,self) }
on *:DEHELP:#:{ theme.sound dehop $+ $iif($nick == $me,self) }
on *:VOICE:#:{ theme.sound voice $+ $iif($nick == $me,self) }
on *:DEVOICE:#:{ theme.sound devoice $+ $iif($nick == $me,self) }
on *:BAN:#:{ theme.sound ban $+ $iif($nick == $me,self) }
on *:MODE:#:{ theme.sound mode $+ $iif($nick == $me,self) }
on *:OPEN:?:{ theme.sound query }
on *:OPEN:@:{ theme.sound open }
on *:OPEN:=:{ theme.sound open }
on *:OPEN:!:{ theme.sound open }
on *:SERV:*:{ theme.sound serv }
on *:FILESENT:*:{ theme.sound filesent }
on *:FILERCVD:*:{ theme.sound filercvd }
on *:SENDFAIL:*:{ theme.sound sendfail }
on *:GETFAIL:*:{ theme.sound getfail }


;################# RAW EVENT HANDLING ##################

RAW *:*:{
  var %n = $theme.rawdigit($numeric)
  if ($halted) { return }
  if ($hgt(raw. $+ %n)) { var %r = RAW. $+ %n }
  if (!%r) { var %r = RAW.Other }
  if (%r) && ($theme.current) {
    if (%n == 001) { set -nu %::text $2- | set -nu %::nick $8 }
    elseif (%n == 002) { set -nu %::value $8 }
    elseif (%n == 003) { set -nu %::value $5- }
    elseif (%n == 004) { set -nu %::value $3- }
    elseif (%n == 005) { set -nu %::text $2-15 }

    elseif (%n == 219) { set -nu %::value $2 | set -nu %::text $3- }
    elseif (%n == 221) { set -nu %::nick $1 | set -nu %::modes $2 }

    elseif (%n == 250) { set -nu %::value $4- }
    elseif (%n == 251) { set -nu %::users $4 | set -nu %::text $7 | set -nu %::value $10 }
    elseif (%n == 252) { set -nu %::value $2 }
    elseif (%n == 253) { set -nu %::value $2 }
    elseif (%n == 254) { set -nu %::value $2 }
    elseif (%n == 255) { set -nu %::users $4 | set -nu %::value $7 }
    elseif (%n == 256) { set -nu %::value $5 }

    elseif (%n == 265) { set -nu %::users $5 | set -nu %::value $7 }
    elseif (%n == 266) { set -nu %::users $5 | set -nu %::value $7 }

    elseif (%n == 271) { set -nu %::nick $2 | set -nu %::text $3- }

    elseif (%n == 301) { set -nu %::nick $2 | set -nu %::text $3- }
    elseif (%n == 302) { set -nu %::nick $gettok($2,1,61) | set -nu %::address $gettok($2,2,64) | set -nu %::value $2 }

    elseif (%n == 321) || (%n == 322) { return }
    elseif (%n == 324) { set -nu %::chan $2 | set -nu %::modes $3 }
    elseif (%n == 329) { set -nu %::chan $2 }

    elseif (%n == 331) { set -nu %::chan $2 }
    elseif (%n == 332) { set -nu %::chan $2 }
    elseif (%n == 333) { set -nu %::chan $2 | set -nu %::nick $3 | set -nu %::text $4- }
    elseif (%n == 338) { set -nu %::nick $2 | set -nu %::value $3 | set -nu %::value $4 | set -nu %::text $5- }

    elseif (%n == 352) { set -nu %::nick $6 | set -nu %::address $4 | set -nu %::cmode $right($7,1) | set -nu %::away $iif($left($7,1) == G,yes,no) | set -nu %::chan $2 | set -nu %::wserver $5 | set -nu %::realname $3 | set -nu %::value $8 | set -nu %::text $1- | set -nu %::isoper $iif(* isin $7,is,is not) }
    elseif (%n == 353) { set -nu %::chan $3 | set -nu %::text $4- }

    elseif (%n == 364) || (%n == 365) { return }
    elseif (%n == 366) { set -nu %::chan $2 }
    elseif (%n == 382) { set -nu %::value $2 }
    elseif (%n == 391) { set -nu %::text $3- }

    elseif (%n == 401) || (%n == 403) || (%n == 406) || (%n == 421) || (%n == 433) || (%n == 471) || (%n == 473) || (%n == 474) || (%n == 475) || (%n == 482) { set -nu %::chan $2 | set -nu %::nick $2 | set -nu %::text $3- }
    elseif (%n == 404) { set -nu %::chan $7- | set -nu %::nick $2 | set -nu %::text $3- }
    elseif (%n == 405) { set -nu %::chan $2 }
    elseif (%n == 416) { set -nu %::value $2 }
    elseif (%n == 432) { set -nu %::nick $2 }
    elseif (%n == 436) { set -nu %::nick $2 }
    elseif (%n == 438) { set -nu %::nick $2 }
    elseif (%n == 442) { set -nu %::chan $2 }
    elseif (%n == 443) { set -nu %::nick $2 | set -nu %::chan $3 }
    elseif (%n == 451) { set -nu %::value $2 }

    elseif (%n == 603) { set -nu %::value $3 | set -nu %::text $7 }
    elseif (%n == 604) { set -nu %::nick $2 | set -nu %::value $3 | set -nu %::text $4- }
    elseif (%n == 606) { set -nu %::nick $2 }

    elseif (%n == 311) || (%n == 314) { set -nu %::nick $2 | set -nu %::address $3 $+ @ $+ $4 | set -nu %::realname $6- | if ($hgt(whois)) { set %whois.nick $2 | set %whois.address $3 $+ @ $+ $4 | set %whois.realname $6- | halt } }
    elseif (%n == 319) { set -nu %::nick $2 | set -nu %::chan $3- | if ($hgt(whois)) { set %whois.chan %whois.chan $3- | halt } }
    elseif (%n == 312) { set -nu %::nick $2 | set -nu %::wserver $3 | set -nu %::serverinfo $4- | if ($hgt(whois)) { set %whois.wserver $3 | set %whois.serverinfo $4- | halt } }
    elseif (%n == 301) { set -nu %::nick $2 | set -nu %::away $3- | if ($hgt(whois)) { set %whois.away $3- | halt } }
    elseif (%n == 307) { set -nu %::nick $2 | set -nu %::isregd is | if $hgt(whois)) { set %whois.isregd is | halt } }
    elseif (%n == 313) { set -nu %::nick $2 | set -nu %::isoper is | set -nu %::operline $3- | if $hgt(whois)) { set %whois.isoper is | set %whois.operline $3- | halt } }
    elseif (%n == 317) { set -nu %::nick $2 | set -nu %::idletime $3 | set -nu %::signontime $asctime($4) | if ($hgt(whois)) { set %whois.idletime $3 | set %whois.signontime $asctime($4) | halt } }
    elseif (%n == 330) { set -nu %::nick $2 }
    elseif (%n == 318) {
      set -nu %::nick $2
      if ($hgt(whois)) && ($theme.current == mts) && ($gettok($theme.raw.echo(318),1,9)) && (%whois.nick) {
        set -nu %::nick %whois.nick
        set -nu %::address %whois.address 
        set -nu %::realname %whois.realname
        set -nu %::chan %whois.chan
        set -nu %::wserver %whois.wserver
        set -nu %::serverinfo %whois.serverinfo
        set -nu %::away %whois.away
        set -nu %::isregd %whois.isregd
        set -nu %::isoper %whois.isoper
        set -nu %::operline %whois.operline
        set -nu %::idletime %whois.idletime
        set -nu %::signontime %whois.signontime
        theme.vars echo $color(whois) $iif($hgt(indent),-i $+ $ifmatch) $gettok($theme.raw.echo(318),1,9)
        theme.text whois $gettok($theme.raw.echo(318),2,9) $1-
        unset %whois.*
        halt
      }
    }
    elseif (%n == 369) {
      set -nu %::nick $2
      if ($hgt(whowas)) && ($theme.current == mts) && ($gettok($theme.raw.echo(369),1,9)) && (%whois.nick) {
        set -nu %::nick %whois.nick
        set -nu %::address %whois.address
        set -nu %::realname %whois.realname
        set -nu %::wserver %whois.wserver
        set -nu %::serverinfo %whois.serverinfo
        theme.vars echo $color(whois) $iif($hg(indent),-i $+ $ifmatch) $gettok($theme.raw.echo(369),1,9)
        theme.text whowas $gettok($theme.raw.echo(369),2,9) $1-
        unset %whois.*
        halt
      }
    }

    if (%::chan == $2) || (%::nick == $2) || (%::value == $2) { set -nu %::text $3- }
    if (!%::value) { set -nu %::value $2 }
    if (!%::text) { set -nu %::text $2- }
    if (!%:target) { set -nu %:target $2 }
    set -nu %::fromserver $nick
    set -nu %::numeric %n

    var %e = $theme.raw.echo(%n,%:target)
    if (%e) {
      if ($gettok(%e,2,9) == 2) || ($gettok(%e,2,9) == 3) {
        theme.vars echo $color(info) $gettok(%e,1,9)
        set -u %:raw $1-
        theme.text %r $gettok(%e,2,9) $1-
      }
    }
    else { halt }
  }
}

alias -l theme.rawdigit {
  if ($len($1) == 1) { return 00 $+ $1 }
  elseif ($len($1) == 2) { return 0 $+ $1 }
  return $1
}
alias -l theme.raw.echo {
  var %i = $iif($hgt(indent),i $+ $ifmatch)
  var %h = $hg($hfind(theme.settings,theme.raw.*. $+ $1,1,w))
  if (%h == $null) { var %h = $hg(theme.raw.all) }
  var %d = $gettok(%h,1,32),%a = $gettok(%h,2,32)

  if (%d == 4) { return $iif($window($2),-t $+ %i $2,-ta $+ %i) 	 $+ %a }
  elseif (%d == 3) { return $iif($window($2),-t $+ %i $2,-ts $+ %i) 	 $+ %a }
  elseif (%d == 2) { return -ta $+ %i 	 $+ %a }
  elseif (%d == 1) { return -ts $+ %i 	 $+ %a }
  return
}

;################### PREVIEWING ######################

alias theme.preview {
  if ($1 == -l) {
    var %z = l
    if ($2 isnum) { var %s = $2,%f = $3- }
    else { var %f = $2- }
  }
  elseif ($1 isnum) { var %z = s,%s = $1,%f = $2- }
  else { var %z = s,%f = $1- }
  if ($right(%f,4) == .mts) { var %t = m }
  elseif ($right(%f,4) == .xml) { var %t = x }
  elseif ($right(%f,4) == .zip) { var %t = z }

  if ($isfile(%f)) {
    if (%t == z) {
      var %z = $scriptdirextracted\ $+ $left($nopath(%f),-4) $+ \
      ha theme.extract $+(preview,	,%f,	,%z,	,$iif($1 == -l,-l) %s)
      if (!$isdir(%z)) { szip SUnZipFile %f > %z }
      else { .signal SZIP Z_OK %f }
      return
    }

    if ($dialog(themes)) { did -b themes $iif(%z == s,12,13) }

    set -u %:theme $iif(%t == m,$theme.info(name,%f),$left($nopath(%f),-4))
    set -u %:scheme $iif(%t == m,$theme.info(%s,%f),$gettok($theme.info(schemenames,%f),$calc(%s + 1),59))

    var %p = $+($shortfn($scriptdirpreview\),$nopath(%f),$iif(%s,-),%s,-,%z,.bmp)
    if ($isfile(%p)) { theme.preview.show %p | if ($dialog(themes)) { did -e themes $iif(%z == s,12,13) } | return }
    if (!$hget(theme.preview)) { hmake theme.preview 15 }
    if ($window(@theme.preview.text)) { window -c @theme.preview.text }
    window -h @theme.preview.text

    theme.preview.hash %f
    if (%s) { theme.preview.hash %s %f }
    if (!$hgp(colors)) { hap colors 0,6,4,5,2,3,3,3,3,3,3,1,5,7,6,1,3,2,3,5,1,0,1,0,1,15,6,0 }
    if (!$hgp(rgbcolors)) { hap colors $rgbdef }


    if (%t == m) {
      if (%s) { hap scheme $theme.info(%s,%f) }
      if ($hgp(basecolors)) {
        set -u %::c1  $+ $gettok($ifmatch,1,44)
        set -u %::c2  $+ $gettok($ifmatch,2,44)
        set -u %::c3  $+ $gettok($ifmatch,3,44)
        set -u %::c4  $+ $gettok($ifmatch,4,44)
      }
      set -u %::pre $theme.parse(m,$hgp(prefix))

      set -u %::timestampformat $hgp(timestampformat)
      if (!%::timestampformat) { set -u %::timestampformat [HH:nn] }
      set -u %::timestamp $iif($hgp(timestamp) == on,$theme.parse(m,%::timestampformat))

      var %:script = $nofile(%f) $+ $hgp(script)
      if ($isfile(%:script)) && (!$script(%:script)) {
        .load -rs $+(",%:script,")
        set -u %:unload 1
      }
    }
    elseif (%t == x) {
      if (%s) { hap scheme $gettok($theme.info(schemenames,%f),$calc(%s + 1),59) }
      if ($hgp(timestamp)) { set -u %::timestamp $theme.parse(x,$hgp(timestamp)) }
      var %:script = %f
      if (!$script(%f)) {
        .load -rs $+(",%f,")
        set -u %:unload 1
      }
      if ($hgp(init)) { $eval($ifmatch,2) }
    }

    set -u %::me $me
    set -u %::chan #preview
    set -u %::address me@voiceofpower.us
    theme.preview.add %t 8 joinself
    if (%z == l) {
      set -u %::text Download TEMX on mircscripts.org! || Latest version: $themesversion || For mIRC $version
      set -u %::numeric 332
      theme.preview.add %t 21 raw.332 %::me %::chan %::text
      set -u %::nick Voice_of_Power
      set -u %::text $asctime($calc($ctime - $r(5,864000)))
      set -u %::numeric 333
      theme.preview.add %t 21 raw.333 %::me %::chan %::nick %::text
      set -u %::text $calc($ctime - $r(864000,8640000))
      set -u %::numeric 329
      theme.preview.add %t 21 raw.329 %::me %::chan %::text
    }
    set -u %::nick TheXenocide
    set -u %::cmode @
    set -u %::text $gettok(Greetings Welcome Hola Hey Aye Ahoy,$r(1,6),32) %::me :)
    theme.preview.add %t 12 textchan %::text
    unset %::cmode
    set -u %::text $gettok(hi howdy hello,$r(1,3),32) Xeno
    theme.preview.add %t 16 textchanself %::me %::text
    set -u %::nick XhaKeR
    set -u %::address xhaker@xpro.tk
    theme.preview.add %t 8 join
    if (%z == l) {
      set -u %::fromserver XhaKeR
      set -u %::isoper is
      set -u %::isregd is
      set -u %::realname Weird Dude
      set -u %::chan #thescript +#whatever % $+ #preview @#mircscripts
      set -u %::idletime 42
      set -u %::signontime $asctime($calc(1063090795 - $r(360,7200)))
      set -u %::wserver irc.voiceofpower.us
      set -u %::serverinfo Preview server
      set -u %::isoper is
      if ($hgp(whois)) { var %whois1 = $ifmatch }
      if (%whois1) {
        set -u %::numeric 318
        var %whois = $theme.parse(m,$ifmatch)
        set -u %:echo aline @theme.preview.text 21 $time(%::timestamp)
        if ($gettok(%whois,1,32) == !script) { $gettok(%whois,2-,32) }
        else { %:echo %whois }
      }
      else {
        set -u %::numeric 311
        theme.preview.add %t 21 raw.311 %:me %::nick %::fromserver %::wserver * %::realname
        set -u %::numeric 319
        theme.preview.add %t 21 raw.319 %::me %::nick %::chan
        set -u %::numeric 312
        theme.preview.add %t 21 raw.312 %::me %::nick %::wserver %::serverinfo
        set -u %::numeric 317
        theme.preview.add %t 21 raw.317 %::me %::nick %::idletime %::signontime
        set -u %::numeric 318
        theme.preview.add %t 21 raw.318 %::me %::nick End of /WHOIS list
      }
    }
    set -u %::nick Chanserv
    set -u %::modes +h XhaKeR
    theme.preview.add %t 10 mode %::modes
    set -u %::nick XhaKeR
    set -u %::cmode %
    set -u %::text wassup $gettok(dudes? guys? girls? pigs?,$r(1,4),32)
    theme.preview.add %t 12 textchan %::text
    set -u %::nick VoP|away
    set -u %::newnick Voice_of_Power
    set -u %::chan #thescript
    theme.preview.add %t 11 nick
    set -u %::nick Voice_of_Power
    set -u %::cmode +
    set -u %::text desires to be opped.... NOW!
    theme.preview.add %t 2 actionchan %::text
    set -u %::nick TheXenocide
    set -u %::knick Voice_of_Power
    set -u %::chan #thechan
    set -u %::text this is what you can get! :D
    theme.preview.add %t 8 kick %::text
    set -u %::nick XhaKeR
    set -u %::modes +v %::me
    theme.preview.add %t 10 mode %::modes
    set -u %::nick Voice_of_Power
    set -u %::address vop@voiceofpower.us
    theme.preview.add %t 8 join
    set -u %::nick Chanserv
    set -u %::modes +o Voice_of_Power
    theme.preview.add %t 10 mode %::modes
    set -u %::nick $me
    set -u %::cmode +
    set -u %::text lol
    theme.preview.add %t 16 textchanself %::me %::text
    set -u %::nick Voice_of_Power
    set -u %::text Kick me and fear ban!
    theme.preview.add %t 19 topic %::text
    set -u %::nick TheXenocide
    set -u %::text ban me and fear g-line ;)
    theme.preview.add %t 13 notice %::text
    set -u %::target TheXenocide
    set -u %::address xeno@blaat.org
    set -u %::text oops, wrong guy =)
    theme.preview.add %t 13 textmsg %::target %::text
    set -u %::nick Syko
    set -u %::address syko@galactic-quest.com
    set -u %::text Quit: That load button's tempting.. isn't it? ;)
    theme.preview.add %t 18 quit %::text
    if (%z == l) { theme.preview.add %t 10 unotify }
    set -u %::nick Peon
    set -u %::ctcp PING
    set -u %::text $ctime
    theme.preview.add %t 3 ctcp %::ctcp %::text
    if (%z == l) {
      set -u %::target Peon
      set -u %::text 1sec
      theme.preview.add %t 3 ctcpreplyself %::text
      set -u %::chan #omgthischannelnameislongisthatevenpossible?
      theme.preview.add %t 7 invite
      set -u %::modes +w
      theme.preview.add %t 10 modeuser %::modes
      set -u %::nick Syko
      theme.preview.add %t 10 notify
      set -u %::nick Voice_of_Power
      set -u %::text Blaat! ;)
      theme.preview.add %t 4 wallop %::text
      set -u %::nick TheXenocide
      set -u %::text shut up :P
      theme.preview.add %t 4 wallop %::text
      set -u %::nick Voice_of_Power
      set -u %::cmode @
      set -u %::text So %::me $+ , how do you like $hgp(name) $iif(theme !isin $hgp(name),theme) $+ ? :)
      theme.preview.add %t 12 textchan %::text
      set -u %::cmode +
      set -u %::text I like it, $iif($hgp(scheme),$ifmatch,default) scheme.. wasn't it?
      theme.preview.add %t 16 textchanself %::me %::text
      set -u %::cmode @
      set -u %::text yup
      theme.preview.add %t 12 textchan %::text
      theme.preview.add %t 12 load
      set -u %::cmode +
      set -u %::text :)
      theme.preview.add %t 16 textchanself %::me %::text
    }

    var %:colors = $hgp(colors),%:rgbcolors = $hgp(rgbcolors),%:fontchan = $hgp(fontchan)
    if ($window(@theme.preview)) { window -c @theme.preview }
    window -Bph +d @theme.preview -1 -1 $iif(%z == s,300 222,499 540)
    drawfill @theme.preview $gettok(%:colors,1,44) $gettok(%:colors,1,44) 1 1
    var %back = $gettok(%:colors,1,44),%font = " $+ $gettok(%:fontchan,1,44) $+ ",%size = $gettok(%:fontchan,2,44),%i = 1
    while ($line(@theme.preview.text,%i)) {
      var %color = $gettok($ifmatch,1,32),%line = $gettok($ifmatch,2-,32)
      drawtext -pb @theme.preview $gettok(%:colors,%color,44) %back %font %size 3 $iif(%i != 1,$calc(1 + ((%i - 1) * $height(%line,%font,%size))),1) %line
      inc %i
    }


    var %i = 1
    while (%i <= 16) {
      var %r = $rgb( [ $gettok($theme.preview.falsecol,%i,32) ] )
      if ($gettok(%r,2,44)) { var %r = $rgb( [ %r ] ) }
      if (%r) { drawreplace -nr @theme.preview $color($calc(%i -1)).rgb %r }
      inc %i
    }
    %i = 1
    while (%i <= 16) {
      var %r = $rgb( [ $gettok(%:rgbcolors,%i,32) ] )
      if ($gettok(%r,2,44)) { var %r = $rgb( [ %r ] ) }
      if (%r) { drawreplace -nr @theme.preview $rgb( [ $gettok($theme.preview.falsecol,%i,32) ] ) %r }
      inc %i
    }

    drawsave @theme.preview %p
    theme.preview.show %p
    window -c @theme.preview
    window -c @theme.preview.text
    hfree theme.preview

    if (%:unload) { .unload -rs " $+ %:script $+ " }
    if ($dialog(themes)) { did -e themes $iif(%z == s,12,13) }
  }
}
alias theme.preview.falsecol { return 1,1,1 2,2,2 3,3,3 4,4,4 5,5,5 6,6,6 7,7,7 8,8,8 9,9,9 10,10,10 11,11,11 12,12,12 13,13,13 14,14,14 15,15,15 16,16,16 }


alias theme.preview.hash {
  if ($1 isnum) { var %s = $1,%f = $2- }
  else { var %f = $1- }
  var %t = $iif($right(%f,4) == .mts,m,x)

  if ($isfile(%f)) {
    if (%t == m) {
      if ($fopen(pre)) { .fclose pre }
      .fopen pre " $+ %f $+ "
      if ($ferr) { .fclose pre | return }
      if (%s) { .fseek -w pre $+($chr(91),scheme $+ %s,$chr(93)) | var %l = $fread(pre) }
      while (!$feof) {
        if ($fread(pre)) {
          tokenize 32 $ifmatch
          if ($1 == [mts]) || (!$1) || ($left($1,1) == $chr(59)) { continue }
          elseif ($+($chr(91),scheme*,$chr(93)) iswm $1) { .fclose pre | return }
          else { hap $lower($1) $2- }
        }
      }
      .fclose pre
    }

    elseif (%t == x) {
      xml CreateParser xts
      xml StartElement xts theme.xts.parse -s $+ %s
      xml EndElement xts theme.xts.parse -e
      xml Data xts theme.xts.parse -dp
      xml Attribute xts theme.xts.parse -a
      xml File xts " $+ %f $+ "
      xml ParseFile xts
      xml FreeParser xts

      unset %section %item %scheme
      hap name $left($nopath(%f),-4)
    }
  }
}

alias -l theme.preview.add {
  if ($hgp($3)) {
    var %t = $ifmatch
    set -nu %:echo aline @theme.preview.text $2 $time(%::timestamp)
    set -nu %::parentext $ptext(%::text)

    if (m isin $1) {
      %t = $theme.parse(m,%t)
      if ($gettok(%t,1,32) == !script) {
        if ($gettok(%t,2-,32)) {
          var %ifmatch = [ [ $ifmatch ] ]
          %ifmatch
        }
      }
      else { %:echo %t }
    }
    elseif (x isin $1) {
      %t = $theme.parse(x,%t)
      if ($4-) { tokenize 32 $ifmatch }
      if ($gettok(%t,1,32) == !xts) { $eval($gettok(%t,2,32),2) $1- }
      else { %:echo $eval(%t,2) }
    }
  }
}

alias -l theme.preview.show {
  var %s = $mid($1-,-5,1)
  if (%s == s) {
    if (!$dialog(themes)) { themes }
    did -ra themes 11 $shorten(32,Preview: %:theme $iif(%:scheme,- $ifmatch))
    did -g themes 16 $1-
    did -h themes 14,15
    did -v themes 16
  }
  elseif (%s == l) {
    if ($dialog(theme.large)) { dialog -x theme.large theme.large }
    dialog -hm theme.large theme.large
    did -g theme.large 4 $1-
    did -ra theme.large 2 Preview: %:theme $iif(%:scheme,- $ifmatch)
  }
  if (c !isin $theme.options(add)) { .remove $1- }
}



dialog -l theme.large {
  title " Large Theme Preview"
  size -1 -1 517 565
  text "Preview:", 2, 10 4 250 16
  icon 4, 9 18 499 550, $mircexe, 0, border
}
on *:dialog:theme.large:init:*:{
  mdx SetMircVersion $version
  mdx MarkDialog $dname
  mdx SetDialog $dname style title sysmenu tool
}

alias -l theme.load.show { if ($1 isnum) && ($dialog(theme.load)) { did -a theme.load $1 $2- } }

dialog -l theme.load {
  title "Loading Theme..."
  size -1 -1 87 38
  option dbu
  text "", 1, 5 4 76 8, center
  text "", 3, 5 16 76 8
  text "", 2, 5 26 76 8
}
on *:dialog:theme.load:init:*:{
  mdx SetMircVersion $version
  mdx MarkDialog $dname
  mdx SetControlMDX $dname 2,3 progressbar smooth > $mdxdir $+ ctl_gen.mdx
}