// C A L C O P T . S R 
// shur, 8/24/76

// Calculator Options Command:
//	Subcommands:
//		0-9: Precision
//		F,S,R: Mode (Fixed, Scientific, Engineering)
//		R: Radix
//		+ = - * ← / % \    operate on stack

get "bravo1.df"
get "calc.df"
get "char.df"
get "st.df"
get "doc.df"
get "display.df"
get "rn1.df"
get "com.df"

// incoming procedures

external	[
	RealRadixConvert
	SetRegionSys
	SetRegionW
	updatedisplay
	uc
	stsize
	FChInSb
	StFromReal
	RealFromInt
	ConvertRealRadix
	PushCalc
	MakeCurrentBuf
	InsertBuf
	insertc
	deleted
	bravochar
	move
	FCalc
	]

// incoming statics

external	[
	vrlwsys
	vintradix
	vrealx
	vrealy
	vrealz
	vrealw
	frealmode
	cdigitpoint
	]

// outgoing procedures

external	[
	CalcOpt
	FixDispReal
	]


//-=-=-=-=-=-=--=-=-=-=-=
let CalcOpt(cf) = valof
//-=-=-=-=-=-=-=-=-=-=-=-

[
SetRegionSys(risyspast,rinil)
SetRegionSys(risysstate,190)
updatedisplay()

let ch= uc(bravochar())

if ch eq chdel then
	[
	SetRegionSys(risyspast, 188, 50)
	resultis abmsg
	]
if FChInSb(ch,"+=-**←/%") then
	[ cf>>CF.w0= ch; resultis FCalc(cf,fhpoptor) ]
if ch eq $\ then
	[ PushCalc(); FixDispReal();  resultis abnil ]
if (ch ge $0) & (ch le $9) then [ setprecision(ch- #60); resultis abmsg ]  
if ch eq $R then [  setradix(); resultis abmsg ]
if FChInSb(ch,"FSE") then [ setmode(ch); resultis abmsg ]

SetRegionSys(risyspast, 157, 50)		//illegal option
resultis abmsg
]

//-=-=-=-=-=-=--=-=-=-=-=
and setprecision(int) be
//-=-=-=-=-=-=-=-=-=-=-=-

[
cdigitpoint = int
FixDispReal()

let tsb= vec 1
tsb>>SB.cch=1
tsb>>SB.ch↑0= int + #60
SetRegionW(vrlwsys,0,tsb)
let trid=0
trid<<RID.nrl=1
trid<<RID.ri=0
SetRegionSys(risyspast,191,trid,202)

] 

//-=-=-=-=-=-=-=-=-=-=-=-=-
and setmode(ch) be 
//-=-=-=-=-=-=-=-=-=-=-=-=

[
frealmode = selecton ch into
	[
	case $F: frealfixed;
	case $S: frealsci;
	case $E: frealeng
	]

FixDispReal()

let tmsg= selecton frealmode into
	[
	case frealfixed: 193;
	case frealsci: 194;
	case frealeng: 195
	]

SetRegionSys(risyspast, tmsg)
]

//-=-=-=-=-=-=-=-=-=-=-=-=-=-
and setradix() be
//-=-=-=-=-=-=-=-=-=-=-=-=

[
let ch=nil
let oldradix=nil
let fbadch=nil
let tmsg=nil
let trid=vec 1
let tsb= vec 1

SetRegionSys(risysstate, 192)
updatedisplay()
ch= uc(bravochar())
if ch eq chdel then
	[
	SetRegionSys(risyspast, 188, 50)
	return
	]
oldradix= vintradix
fbadch= false

vintradix = selecton ch into
	[
	case $?: vintradix;
	case $B: 2;
	case $O: 8;
	case $D: 10;
	case $E: 11;
	case $T: 12;
	case $H: 16;
	default: valof [ 
		if (ch ge $2) & (ch le $9) then resultis (ch-#60)
		SetRegionSys(risyspast, 200, 50)
		fbadch= true
		resultis vintradix
		]
	]

if fbadch then return

convertradix(oldradix)

tmsg= selecton vintradix into
	[
	case 10: 196;
	case 11: 197;
	case 12: 198;
	case 16: 199;
	default: valof [
		tsb>>SB.cch=1
		tsb>>SB.ch↑0= vintradix + $0
		SetRegionW(vrlwsys,0,tsb)
		trid<<RID.nrl=1
		trid<<RID.ri=0
		resultis trid
		]
	]

FixDispReal()
SetRegionSys(risyspast, 201,tmsg)

]

//-=-=-=-=-=-=-=-=-
and FixDispReal() be
//-=-=-=-=-=-=-=-=-

[
let sbx=vec cwmaxreal
StFromReal(sbx,vrealx,20)

deleted(doctx3)
insertc(doctx3,0,sbx)
InsertBuf(buf3,doctx3,0,stsize(sbx))
MakeCurrentBuf(buf3)
]

//-=-=-=-=-=-=-=-==-==
and convertradix(oldradix) be
//--=-=-=-=-=-=-=-=-=-

[
let rradix= vec cwmaxreal
let treal= vec cwmaxreal

RealFromInt(rradix, oldradix)

RealRadixConvert(treal,vrealx,rradix)
move(treal,vrealx,cwmaxreal)
RealRadixConvert(treal,vrealy,rradix)
move(treal,vrealy,cwmaxreal)
RealRadixConvert(treal,vrealz,rradix)
move(treal,vrealz,cwmaxreal)
RealRadixConvert(treal,vrealw,rradix)
move(treal,vrealw,cwmaxreal)

]