:theme system
:module
:help.m


eval addhelp tsave&tsave $(c4)[$(c3)name$(c4)]&saves the current format settings to a theme.
alias tsave {
	if (![$0]) {	
		chelp tsave
	}
	{
		if (right(3 $0) != [.th]) {
			@ thfile = [$0] ## [.th]
			@ thname = [$0]
		}
		{
			@ thfile = [$0]
			@ thname = before(. $0)
		}
		if (fexist($(themepath)$(thfile))) {
			@ rename($(themepath)$(thfile) $(themepath)$(thfile)~)
		}
		if ([$N]) {
			@ auth = [$N]
			_tsave
		}
		{
			input "Enter your nick for author of the theme: " {
				@ auth = [$0]
				_tsave
			}
		}
	}
        ^restub tsave theme.m
}
alias _tsave {
                @ :thsave = open($twiddle($(themepath)$(thfile)) W T)
                @ :author = [$auth]
                @ :date   = stime($time())
                @ :name   = [$thname]
                @ write($thsave :$author)
                @ write($thsave :$date)  
                @ write($thsave :$name)
                @ write($thsave @author=[N])  
                @ write($thsave @author=[$auth]) 
                @ write($thsave @date=[$date])
                @ write($thsave @name=[$name])
                @ write($thsave @actionform=[$actionform])
                @ write($thsave @abotform=[$abotform])    
                @ write($thsave @awayform=[$awayform])  
                @ write($thsave @boxesform=[$boxesform])
                @ write($thsave @cdccform=[$cdccform])
                @ write($thsave @colorsform=[$colorsform])
                @ write($thsave @ctcpform=[$ctcpform])    
                @ write($thsave @ctcprform=[$ctcprform])
                @ write($thsave @dccform=[$dccform])
                @ write($thsave @estringform=[$estringform])
                @ write($thsave @joinform=[$joinform])      
                @ write($thsave @killform=[$killform])
                @ write($thsave @kickform=[$kickform])      
                @ write($thsave @kstringform=[$kstringform])
                @ write($thsave @modeform=[$modeform])      
                @ write($thsave @msgform=[$msgform])    
                @ write($thsave @ncompform=[$ncompform])
                @ write($thsave @nickform=[$nickform])
                @ write($thsave @noticeform=[$noticeform])
                @ write($thsave @partform=[$partform])    
                @ write($thsave @pubform=[$pubform])  
                @ write($thsave @qstringform=[$qstringform])
                @ write($thsave @quitform=[$quitform])      
                @ write($thsave @sactionform=[$sactionform])
                @ write($thsave @sbarform=[$sbarform])      
                @ write($thsave @scform=[$scform])    
                @ write($thsave @sctcpform=[$sctcpform])
                @ write($thsave @sdccform=[$sdccform])
                @ write($thsave @smsgform=[$smsgform])
                @ write($thsave @snoticeform=[$snoticeform])
                @ write($thsave @spubform=[$spubform])
                @ write($thsave @svform=[$svform])
                @ write($thsave @wallform=[$wallform])
                @ write($thsave @whoisform=[$whoisform])
                @ write($thsave @c1=[$c1)               
                @ write($thsave @c2=[$c2) 
                @ write($thsave @c3=[$c3) 
                @ write($thsave @c4=[$c4)
                @ close($thsave)
                xecho -b saved theme $(c4)[$(c2)$name$(c4)]$(c3) to file $(c4)[$(c2)$(themepath)$(thfile)$(c4)] 
                ^load $(themepath)$(thfile)   
                ^loadformats
	        ^restub _tsave theme.m
}
eval addhelp theme&theme $(c4)[$(c3)name$(c4)|$(c3)file$(c4)]&loads the specified theme.
alias theme {
	if (![$0]) {
		echo $fparse(format_header core themes)
		echo $fparse(format_midbar $(c4)[$(c2)file           $(c4)][$(c2)author         $(c4)][$(c2)created                   $(c4)])
		fe ($glob($(corepath)themes\/*.th)) theme {
			@ :thfile = open($theme R T)
			@ :author = after(: $read($thfile))
			@ :date   = after(: $read($thfile))
			@ :name   = after(: $read($thfile))
			@ close($thfile)
			echo $fparse(format_midbar $(c4)[$(c3)$[15]name$(c4)][$(c3)$[15]author$(c4)][$(c3)$[26]date$(c4)])
		}
		echo $fparse(format_footer core themes)
	}
	{
                if (right(3 $0) != [.th]) {
                        @ thfile = [$0] ## [.th]
                        @ thname = [$0]
                }
                {
                        @ thfile = [$0]
                        @ thname = before(. $0)
                }
                if (fexist($(themepath)$(thfile))) {
                        ^load $(themepath)$(thfile)
			^loadformats
			xecho -b loaded theme $(c4)[$(c2)$name$(c4)]$(c3) by $(author).
                }
		{
			xecho -b error: invalid theme request
		}
	}
        ^restub theme theme.m
}
eval addhelp rtheme&rtheme $(c4)[$(c3)name$(c4)|$(c3)file$(c4)]&removes the selected theme.
alias rtheme {
	if ([$0]) {
	        if (right(3 $0) != [.th]) {
	                @ thfile = [$0] ## [.th]
	                @ thname = [$0]
	        }
	        {
	                @ thfile = [$0]
	                @ thname = before(. $0)
	        }
	        if (fexist($(themepath)$(thfile))) {
			input "Delete theme file $(thfile)? (y/n): " {
				if (left(1 $0) == [y]) {
					@ rename($(themepath)$(thfile) $(themepath)$(thfile)~)
			                xecho -b removed theme $(c4)[$(c2)$thname$(c4)]$(c3)
			        }
				{
					xecho -b error: remove theme aborted
				}
			}
		}
	        {
	                xecho -b error: invalid theme request
	        }
	}
	{
		chelp rtheme
	}
        ^restub rtheme theme.m
}