^on ^set "names_* *" {
  ${[$1]?[^assign $0 $1-]:[^assign -$0]}}

alias lth {
  ^local aa $glob(~/argon/themes\/*.t)
  ^local dd
  fe ($aa) ii {
    @ dd = strip(. $before(-1 .t $after(-1 / $ii)))
    lthm $dd
    push themetypes $dd}}

alias lthm {
  ^local l.
  @ tcol[$0] = 1
  @ l.h = open(~/argon/themes/$0.t R)
  @ tname[$0] = read($l.h)
  @ thex[$0] = read($l.h)
  while (!eof($l.h)) {
    ^hook lthm $0 $read($l.h)}
  @ close($l.h)
  @ tnum[$0] = 1}

^on ^hook "lthm % *" {
  ^assign theme[$1][$themes[$1]][$2] $3-
  ^assign themetypes[$1][$themes[$1]] $themetypes[$1][$themes[$1]] $2}

^on ^hook "lthm % !cd %" {
  ^assign tcol[$1] $3}

^on ^hook "lthm % !cs %" {
  ^assign tspc[$1] $3}

^on ^hook "lthm % ++*" {
  @ themes[$1]++}

alias uthm {
  fe ($themetypes[$0][$tnum[$0]]) ii {
    ^hook uset $0 $ii $theme[$0][$tnum[$0]][$ii]}}

^on ^hook "uset % % *" {
  ^set $2-}

^on ^hook "uset % % -empty-" {
  ^set -$2}

^on ^hook "uset b *" {
  ^set $2-
  ^wset * $2-}

^on ^hook "uset b % -empty-" {
  ^set -$2
  ^wset * -$2}

alias format sthm
alias sthm if (![$0]) {
  ^local aa,jj,kk,ii.
  echo $box.head()
  echo $box.echo_head($(c.hk)\($(cl)formats$(c.hk)\)$(cl).$(c.hw) $(cl)usage$(c.hw): $(cl)/format $(c.lm)\($(cl)type$(c.lm)\) \($(cl)value$(c.lm)\)$(cl))
  foreach theme ii {@ push(aa $ii)}
  fe ($aa) ii.1 ii.2 {
    echo $box.echo($(c.hw)$[2]tolower($ii.1) $(c.hk)[$(c.lc)$[30]tname[$tolower($ii.1)]$(c.hk)]$(cl) ${ii.2?[$(c.hw)$[2]tolower($ii.2) $(c.hk)\[$(c.lc)$[30]tname[$tolower($ii.2)]$(c.hk)\]$(cl)]:})}
  echo $box.footer()} elsif (![$1]) {
    if (themes[$0]) {
      if (thex[$0]) {
        echo $box.head()
        echo $box.echo_head(valid formats for $0.. $(cl)usage$(c.hw): $(cl)/format $(c.lm)\($(cl)type$(c.lm)\) \($(cl)value$(c.lm)\)$(c.hk) - $(c.lm)\($(cl)currently $tnum[$0]$(c.lm)\)$(cl))
          for (@ii=1,ii<=themes[$0],@ii=ii+tcol[$0]) {
            if (tcol[$0] > 1) {
              for (@jj=0,jj<tcol[$0],@jj++) {
                ${theme[$0][${ii+jj}][$leftw(1 $thex[$0])] ? [push kk $anspad($tspc[$0] $(c.hk)[$(c.hw)$[-2]rs(${ii+jj})$(c.hk)]$(cl) $sar(gÿ%awÿ$theme[$0][${ii+jj}][aw_logpage]ÿ$cparse("$theme[$0][${ii+jj}][$leftw(1 $thex[$0])]" $rightw(${strcnt($thex[$0]) - 1} $thex[$0]))))]:}}
              echo $box.echo($kk)
              @ kk = []} else {
            echo $box.echo($(c.hk)[$(c.hw)$[-2]ii$(c.hk)]$(cl) $sar(gÿ%awÿ$theme[$0][$ii][aw_logpage]ÿ$sar(gÿ%namÿ$cparse("$names_delimiter")$cparse("$theme[$0][$ii][format_names_opcolor]" @ psykotyk)$cparse("$theme[$0][$ii][names_delimiter]")$cparse("$theme[$0][$ii][format_names_voicecolor]" + otherguy)$cparse("$theme[$0][$ii][names_delimiter]")$cparse("$theme[$0][$ii][format_names_nickcolor]" . shade)$cparse("$names_delimiter")ÿ$cparse("$theme[$0][$ii][$leftw(1 $thex[$0])]" $rightw(${strcnt($thex[$0]) - 1} $thex[$0])))))}}
          echo $box.footer()} else {
        echo $box.head()
        echo $box.echo($(cl)usage$(c.hw): $(cl)/format $(c.lm)\($(cl)$0$(c.lm)\) \($(cl)1-$themes[$0]$(c.lm)\) $(c.hk)-$(cl) current is $tnum[$0])
        echo $box.footer()}} else {
          ge invalid format type.  use /format to list valid formats.}} else {
  if ([$1] <= themes[$0]) {
    @ tnum[$0] = [$1]
    uthm $0
    if (!match(-q $*)) {ge set $(c.lm)\($(cl)$tname[$0]$(c.lm)\)$(cl) format to $1.};^hook uthm $0} else {
    ge format is out of range.  use /format $0 to list format numbers.}}

alias tsave if ([$0]) {
  ^local ts.
  @ ts.f = [$0] =~ [%\/%.th] ? [$0] : ([$0] =~ [%\/%] ? [$0.th] : [~/argon/tlib/$0.th])
  @ ts.n = a.tname = [$1] ? [$1-] : []
  @ fexist($ts.f) ? unlink($ts.f) :
  @ ts.h = open($ts.f W T)
  if (ts.n) {
    @ write($ts.h :setname $1-)} else {
    @ write($ts.h :unsetname)}
  @ write($ts.h :setauth $N)
  @ write($ts.h :setecho $echo)
  foreach tnum ii {
    @ write($ts.h :settheme $ii $tnum[$ii])}
  @ close($ts.h)
  ge wrote theme file $(c.lm)\($(cl)$ts.f$(c.lm)\)${ts.n ? [\:$(cl) $ts.n] :}} else {
  ausage tsave}

alias tload if ([$0]) {
  ^local tl.
  @ tl.f = [$0] =~ [%\/%.th] ? [$0] : ([$0] =~ [%\/%] ? [$0.th] : [~/argon/tlib/$0.th])
  if (fexist($tl.f) != -1) {
    @ tl.h = open($tl.f R)
    while (!eof($tl.h)) {
      unless (eof($tl.h)) {
        @ tl.r = read($tl.h)
        switch ($leftw(1 $tl.r)) {
          (:setname) {@ a.tname = rightw(${#tl.r - 1} $tl.r)}
          (:unsetname) {@ a.tname = []}
          (:setecho) {@ echo = rightw(${#tl.r - 1} $tl.r);fixecho}
          (:setauth) {@ tl.auth = rightw(${#tl.r - 1} $tl.r)}
          (:settheme) {@ tnum[$midw(1 1 $tl.r)] = rightw(${#tl.r - 2} $tl.r)
                       uthm $midw(1 1 $tl.r)}}}}
    @ close($tl.h)
    ge read theme file $(c.lm)\($(cl)$tl.f$(c.lm)\)$(cl) by $tl.auth${a.tname?[$(c.hk)\:$(cl) $a.tname]:}} else {
    ge theme file $(c.lm)\($(cl)$tl.f$(c.lm)\)$(cl) does not exist!}} else {
  ausage tload}

alias tlist {
  ^local tl.
  @ tl.l = glob(~/argon/tlib/\*.th)
  fe ($tl.l) tl.ii {
    @ setitem(tl.n $numitems(tl.n) $after(-1 / $before(-1 . $tl.ii)) $_getdesc($tl.ii))}
  echo $box.head()
  echo $box.echo_head(themes.. use /theme $(c.lm)\($(cl)filename$(c.lm)\)$(cl) to load.)
  for (@tl.c=0,tl.c<numitems(tl.n),@tl.c++) {
    echo $box.echo($(c.hk)[$(c.hw)$numdots(15 $leftw(1 $getitem(tl.n $tl.c)))$(c.hk)] [$(cl)$numdots(10 $midw(1 1 $getitem(tl.n $tl.c)))$(c.hk)] $(cl)$rightw(${strcnt($getitem(tl.n $tl.c)) - 2} $getitem(tl.n $tl.c)))}
  echo $box.footer()
  @ delarray(tl.n)}

alias theme if ([$0]) {tload $*}{tlist}

alias svt {
  ^local st.
  @ fexist(~/argon/etc/theme.sav) != -1 ? unlink(~/argon/etc/theme.sav) :
  @ st.h = open(~/argon/etc/theme.sav W)
  foreach tnum ii {
    @ write($st.h $ii $tnum[$ii])}
  @ close($st.h)}

alias ldt {
  if (fexist(~/argon/etc/theme.sav) != -1) {
    ^local lt.
    @ lt.h = open(~/argon/etc/theme.sav R)
    @ lt.r = read($lt.h)
    while (!eof($lt.h)) {
      @ tnum[$leftw(1 $lt.r)] = rightw(1 $lt.r)
      @ lt.r = read($lt.h)}
    @ close($lt.h)}
  fe ($themetypes) ii {
    uthm $ii}}

lth
ldt
reinitstat
