# unit convertor for DCT, by Shiar

# 1.09.1 2004-10-02 23:05 - moved %unit specs from 1.9 main
# 1.09.2 2004-10-11 22:05 - all code moved here as well

use strict;
use utf8;

my %unit;

{
my $i = 0;
$unit{$_->[0]} = { name=>$_->[0], type=>$i, val=>$_->[1] }
for map {$i++; @$_} (
	[
		['m', 1],
		['cm', .01],
		['mm', .001],
		['km', 1000],
		['ft', .3048],
		['in', .0254],
		['yd', .9144],
		['mile', 1609.344],
		['nmile', 1852],
		['lyr', 9.46052840488e+15],
		['mil', 2.54e-5],
	#		_m _cm _mm _yd _ft _in _Mpc _pc _lyr _au _km _mi
	#		_nmi _miUS _chain _rd _fath _ftUS _Mil _μ _Å _fermi
	], # lengths
	[
		['m^3', 1],
		['cm^3', 1e-6],
		['ft^3', .028316846592],
		['in^3', 1.6387064e-5],
	], # volume
);
} # create unit table

$action{_m} = [0, sub {print "test\n"}];

push @{ $hook{precmd} }, sub {
	if ($_ =~ /^_/) {{
		$_ = $unit{substr $_, 1} or next;
		if (exists $val{unit} and $val{unit}{type}==$_->{type}) {
			unshift @stack, $val{i} if defined $val{i};
			$stack[0] *= delete($val{unit})->{val} / $_->{val};
			showstack();
			%val = (i=>undef, frac=>0);
		} # convert
		else {
			$val{unit} = $_;
		} # set source unit
		return 1;
	}} # conversion
}; # precmd

push @{ $hook{showentry} }, sub {
	exists $val{unit} && '_'.$val{unit}{name};
}; # showentry

1;

