// filter.mac ver.1.1 modified by Hideaki Shirata 1998/10/22
// Modified for Opereting on Windows NT4

if (basename=="") {
	$temp = getenv("TMP");
	saveas $temp + "\\filter\\temp.txt";
}

execmacro "atpartsv.mac";

$prompt = "Filter:\n";

if (rectselecting) {
	message $prompt + "Don't use BOX selection!";
	endmacro;
}

save;
if (result == no) endmacro;

call get_suffix basename;
$suffix = $$return;

if ($suffix == "$$$") {
	message $prompt + "Don't use $$$ extension!";
	endmacro;
}
if ($suffix == "org") {
	question $prompt + "This is ORIGINAL, are you sure?";
	if (result == no) endmacro;
}

$filename = filename;

call get_hidemarudir;
$hidemarudir = $$return;
$inifile = $hidemarudir + "filter.ini";

$cmnd = getinistr($inifile, "SUFFIXES", $suffix);

if ($cmnd == "") {
	#i = 1;
	while (#i < hidemarucount) {
		call get_suffix basename[#i];
		if ($$return != "") {
			$cmnd = getinistr($inifile, "SCRIPT", $$return);
			if ($cmnd != "") {
				$cmnd = $cmnd + " " + filename[#i];
				break;
			}
		}
		#i = #i + 1;
	}
}

if ($cmnd == "") $cmnd = getinistr($inifile, "HISTORY", "1");

	#imestate = imestate;
if (#imestate) imeswitch;
$cmnd = input($prompt + "Input filter commands", $cmnd);
if (#imestate) imeswitch;
if (result == no || $cmnd == "") endmacro;

call get_tempdir;
$tempdir = $$return;

if (selecting) {
	#blockmode = true;
	#topx = seltopx; #topy = seltopy;
	#endx = selendx; #endy = selendy;
	$input = $tempdir + "_input.$$$";
	disabledraw;
	copy;
	openfile "/h " + $input;
	setactivehidemaru findhidemaru($input);
	config "m-";
	selectall;
	cut;
	getcliphist 1;
	paste;
	save;
	setactivehidemaru findhidemaru($filename);
	closehidemaruforced findhidemaru($input);
} else {
	#blockmode = false;
	$input = $filename;
	if (updated) save;
}

$output = $tempdir + "_output.$$$";

if (findhidemaru($output) != -1)
	closehidemaruforced findhidemaru($output);

#i = 0;
#len = strlen($cmnd);
while (#i < #len) {
	if (midstr($cmnd, #i, 1) == "|") {
		$cmnd = leftstr($cmnd, #i) + "<" + $input
					+ " |" + rightstr($cmnd, #len - 1 - #i);
		break;
	}
	#i = #i + 1;
}

writeinistr $inifile, "HISTORY", "1", $cmnd;

if (strlen($cmnd) == #len) $cmnd = $cmnd + " < " + $input;
	if (getenv("OS") == "Windows_NT") {
	runsync2 "filternt.pif /c " + $cmnd + " > " + $output;
		} else {
	runsync2 "filter.pif /c " + $cmnd + " > " + $output;
	}

openfile $output;
gofiletop;
if (code == eof) {
	message $prompt + "Nothing returns.";
	setactivehidemaru findhidemaru($filename);
	closehidemaruforced 1;
	if (#blockmode) {
		moveto #topx, #topy;
		beginsel;
		moveto #endx, #endy;
	}
	endmacro;
}

if (#blockmode) {
	question $prompt + "Insert to original, OK?";
	#result = result;
		setactivehidemaru findhidemaru($filename);
		if (#result == yes) closehidemaruforced 1;
		moveto #topx, #topy;
		beginsel;
		moveto #endx, #endy;
		if (#result == yes) {
			cut;
			insertfile $output;
		}
} else {
	question $prompt + "Exchange, OK?";
	if (result == yes) {
		closehidemaruforced findhidemaru($filename);
		setactivehidemaru findhidemaru($output);
		changename $filename;
	}
}
title 0;
endmacro;

add_pathdelim:
	$$dir = $$1;
	if (rightstr($$dir, 1) != "\\") $$dir = $$dir + "\\";
	return $$dir;

get_hidemarudir:
	call add_pathdelim hidemarudir;
	return $$return;

get_suffix:	//(filename)
	##p = strstr($$1, ".");
	if (##p == -1)
		return;
	else
		return rightstr($$1, strlen($$1) - 1 - ##p);

get_tempdir:
	$$dir = getenv("TEMP");
	if ($$dir == "") $$dir = getenv("TMP");
	if ($$dir == "") $$dir = directory;
	call add_pathdelim $$dir;
	return $$return;