Skip to content

Instantly share code, notes, and snippets.

@BGMcoder
Created April 23, 2018 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BGMcoder/f29b7014be1d20ee9eb487a2d569ed53 to your computer and use it in GitHub Desktop.
Save BGMcoder/f29b7014be1d20ee9eb487a2d569ed53 to your computer and use it in GitHub Desktop.
my calculator script
;when a user types a command, we want to wait until they push ENTER before processing
;Remember that you don't need the enter key to calculate
Action_Enter:
;we don't need to fetch equation or answer here - it's global and fetched from the last action_calculate
gui, mainui:default ;if you don't place this here, you can't set the answer
;thiscommand and thisparam are fetched into global memory in Action_Calculate
;When the user pushes Enter, we are supposed to move the current answer to the equation side so that we end up with the same thing in both boxes.
if(!thiscommand){
;reset the equation and answer if the user hits enter - we don't want to send ready or queue to the equation
if(answer = "Ready" || answer = queue){
gosub, action_clear
;Store the last equation in the recent list
}else{
if(recentmode = 0){
StoreRecent(equation)
}
;Now set the Answer text to the Equation side
hequation.SetText(answer)
hequation.SelEnd()
}
return
}else if(thiscommand = "run"){
try{
run, %thisparam%
}catch e{
alert("Sorry, having trouble trying to RUN that!")
}
}else if(thiscommand = "ini"){
gosub, Action_Ini
gosub, action_clear
}else if(thiscommand = "hide"){
gosub, TOGGLEMAINUI
gosub, action_clear
}else if(thiscommand = "dec"){
gosub, TOGGLESCIENTIFIC
gosub, action_clear
}else if(thiscommand = "about"){
gosub, about
gosub, action_clear
}else if(thiscommand = "help"){
gosub, HELP
gosub, action_clear
}else if(thiscommand = "fun"){
gosub, Action_mathisfun
gosub, action_clear
}else if(thiscommand = "hotkey"){
gosub, action_hotkey
}else if(thiscommand = "options"){
gosub, action_optionsui
}
return
;--------------------------------------------------------------------------------------------------------------------------------------
;Renew the answer every time the equation is changed
Action_Calculate:
;-------------maestrith junk------------------
/*
info:=A_EventInfo
code:=NumGet(info+8)
if(code=2008){
ch:=NumGet(info+(A_PtrSize*4))
if(ch=40)
hequation.2003(hequation.2008,")")
}
*/
;-------------/maestrith junk------------------
if(!recalc){ ;use recalc to force recalculation without the user typing in the equation box
recalc := false ;we have to set it false in the same place we call Action_Calculate or it will keep firing; but do it here just in case
if(a_guievent != "Normal") ;don't proceed for notifications - else it fires every second
return
if(a_eventinfo != 0x0300) ;if the event is not a text-change, don't proceed
return
}
gui, mainui:default
;if the user is using the mouse or the arrow buttons to select text, then don't calculate
If GetKeyState("lbutton", "P")
return
if GetKeyState("Shift", "P") && ( getkeystate("left","P") || getkeystate("right","P") )
return
equation := hequation.GetText()
equation := ltrim(equation) ;don't trim both sides - we want to allow spaces in the expression
if(equation = ""){
answer := ready
hanswer.settext(answer)
return
}
;if(left(equation)=":")
; hequation.2100(0,regexreplace(commandlist,"|"," "))
;colourize for enter action commands
;but they won't be executed until the user presses return - that's why we return out of this section
;THISCOMMAND AND THISPARAM are global and are used by Action_Enter too. We set the values here though.
RegExMatch(equation, "(" . commandlist . ")(.*)", thisaction)
thiscommand := thisaction1
thisparam := trimtrim(thisaction2)
;don't use min(x,x) as a command
if(thiscommand = "min"){
if( regexmatch(thisparam, "\(") ){
thiscommand := ""
}
}
if(thiscommand){
answer := "command"
hanswer.settext(answer)
return
}else{ ;LAST CHANCE BEFORE EVAL
;since is not a command phrase, then check to see if we want to proceed
;if(equation = lastequation && !recalc) ---can't remember if we need this; if we *do* need it, make sure to set it to true in action_clear so recalc is done on e
;if(equation = lastequation)
; return
;THIS OUGHT TO WORK, BUT FOR SOME REASON IT DOES NOT - NEVER FIGURED OUT WHY
;remove characters in the equation we cannot process
; cursel := hequation.getsel()
; equation := RegExReplace(equation,"(?<!:)=") ;allow := but not =
; equation := RegExReplace(equation,"(?=\d)\,") ;don't allow commas
; hequation.settext(equation)
;hequation.setsel(cursel)
}
;just do regular evaluation now
answer := eval(equation,thiscommand)
;format the answer so the decimal points are right
if(usedecimals){
}
if(!usescientific){
}
if(answer || answer = "0"){ ;if an answer is returned (that is, it's not blank or empty)
}else{ ;if we have typed something besides a number, then display a "waiting" symbol to show the user they need to finish the equation
answer := queue
}
hanswer.settext(answer)
if(equation != lastequation){
lastequation := equation
SaveForUndoing(equation)
if(recentmode = 1 && answer){
StoreRecent(equation)
}
}
;AUTOCLIP
if(autoclipanswer){
if(answer != queue && answer != ready){
clipboard := answer
}
}
return
;--------------------------------------------------------------------------------------------------------------------------------------
;allow the user to select part of the expression and evaluate just that part and replace it in the main equation
;this is useful for converting fractions to decimals, too
Action_EvalSelection:
curtext := hequation.GetSelText()
selAnswer := eval(curtext) ;evaluate the equation and fetch the answer
hequation.ReplaceSel(selAnswer)
return
;--------------------------------------------------------------------------------------------------------------------------------------
Action_Grow:
thiswin := % "ahk_id " . %apphwnd%
WinGetPos,,,uiw,,%thiswin%
uiw := uiw + keysizing
winmove, %thiswin%,,,,%uiw%
;---------------maestrith junk---------------
;width:=hequation.2276(5,hequation.gettext() "asdf")+80
;WinMove,%thiswin%,,,,%width%
;pos:=hequation.2008(),end:=hequation.2009
;hequation.2025(0),hequation.2160(pos,end)
;---------------maestrith junk---------------
return
Action_Shrink:
thiswin := % "ahk_id " . %apphwnd%
WinGetPos,,,uiw,,%thiswin%
uiw := uiw - keysizing
winmove, %thiswin%,,,,%uiw%
return
Action_GrowAnswer:
keyflag := true
gosub, action_grow
keyflag := false
return
Action_ShrinkAnswer:
keyflag := true
gosub, action_shrink
keyflag := false
return
Action_GrowEquation:
gosub, action_grow
return
Action_ShrinkEquation:
gosub, action_shrink
return
Action_SaveSize:
GuiControlGet, equation, pos
GuiControlGet, answer, pos
ini.write(appini, sec_settings, key_startanswerw, answerw)
ini.write(appini, sec_settings, key_startequationw, equationw)
return
Action_Ini:
run %appini%
return
Action_Clear:
gui, mainui:default
answer := hanswer.GetText()
if(answer && recentmode = 0){
equation := hequation.GetText()
StoreRecent(equation)
}
hequation.settext("")
return
Action_CopyAnswer:
answer := hanswer.GetText()
clipboard := answer
return
Action_CopyEquation:
equation := hequation.GetText()
clipboard := equation
return
Action_CopyFormula:
equation := hequation.GetText()
answer := hanswer.GetText()
clipboard := equation . " = " . answer
return
Action_ClickCopyAnswer:
gui, mainui:default
clipboard := answer
return
Action_Answer2Fraction:
gui, mainui:default
answer := hanswer.gettext()
fractionalanswer := Decimal2Fraction(answer,fractionoptions)
sleep, 100 ;if you don't sleep, then it might not update the answer control if called from the menu command
hanswer.settext(fractionalanswer)
;guicontrol,,answer, %fractionalanswer%
return
Action_mathisfun:
run, %mathisfun%
return
;--------------------------------------------------------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------------------------------------------------------
;AHK Functions Abs|Ceil|Exp|Floor|Log|Ln|Round|Sqrt|Sin|Cos|Tan|ASin|ACos|ATan
;Predefined functions: SGN|Fib|Fac (sign, Fibonacci numbers, Factorials)
; +,-,*,/,\(or % = mod); **(or @ = power); var (pi, e); abs(),sqrt(),floor()
InsertInSelection(whatcommand){
global hequation
curtext := hequation.GetSelText()
hequation.replaceSel(whatcommand . "(" . curtext . ")")
if(!curtext){
cursel := hequation.getsel()
hequation.setsel(cursel-1,cursel-1)
}
}
Insert_sqrt:
InsertInSelection("sqrt")
return
Insert_abs:
InsertInSelection("abs")
return
Insert_floor:
InsertInSelection("floor")
return
Insert_exp:
InsertInSelection("exp")
return
Insert_exp2:
hequation.ReplaceSel("**")
return
Insert_ceil:
InsertInSelection("ceil")
return
Insert_log:
InsertInSelection("log")
return
Insert_ln:
InsertInSelection("Ln")
return
Insert_round:
InsertInSelection("round")
return
Insert_sin:
InsertInSelection("sin")
return
Insert_cos:
InsertInSelection("cos")
return
Insert_tan:
InsertInSelection("tan")
return
Insert_asin:
InsertInSelection("asin")
return
Insert_acos:
InsertInSelection("acos")
return
Insert_atan:
InsertInSelection("atan")
return
Insert_fac:
InsertInSelection("fac")
return
Insert_fib:
InsertInSelection("fib")
return
Insert_rad:
InsertInSelection("rad")
return
Insert_deg:
InsertInSelection("deg")
return
Insert_gcd:
InsertInSelection("gcd")
return
Insert_choose:
InsertInSelection("choose")
return
Insert_min:
InsertInSelection("min")
return
Insert_max:
InsertInSelection("max")
return
;------------does not surround - just inserts before selection-------------------------------------------------------
InsertBeforeSelection(whatcommand){
global hequation
curtext := hequation.GetSelText()
hequation.replaceSel(whatcommand . curtext)
if(!curtext){
cursel := hequation.getsel()
hequation.setsel(cursel-1,cursel-1)
}
}
Insert_ub2d:
InsertBeforeSelection("'")
return
Insert_sb2d:
InsertBeforeSelection("''")
return
Insert_0x: ;hex prefix for hex to decimal
InsertBeforeSelection("0x")
return
;------------constants-------------------------------------------------------
Insert_pi:
hequation.ReplaceSel("pi")
return
Insert_euler: ;2.718281828459045 ;http://www.mathsisfun.com/numbers/e-eulers-number.html
hequation.ReplaceSel("e")
return
Insert_phi:
hequation.ReplaceSel("phi")
return
;-------------------Commands that need the user to push enter-------------------------------------------------------------------------------------------------------------------
Insert_run:
hequation.SetText("run ")
hequation.Selend()
return
Insert_ini:
hequation.SetText("ini")
hequation.Selend()
return
Insert_hide:
hequation.SetText("hide")
hequation.Selend()
return
Insert_hotkey:
hequation.SetText("hotkey")
hequation.Selend()
return
;-------------------Auto-Calculating Commands-------------------------------------------------------------------------------------------------------------------
InsertAutoCalc(whatcommand){
global hequation
hequation.SetText(whatcommand)
hequation.Selend()
}
Insert_h: ;decimal to hex
InsertAutoCalc("$h")
return
Insert_b: ;decimal to binary
InsertAutoCalc("$b")
return
Insert_c: ;character to code
InsertAutoCalc("$c")
return
Insert_u: ;unicode to character
InsertAutoCalc("$u")
return
Insert_a: ;ascii code to character
InsertAutoCalc("$a")
return
Insert_n: ;char to unicode
InsertAutoCalc("$n")
return
Insert_rf:
InsertInSelection("$rf")
return
Insert_rfs:
InsertInSelection("$rfs")
return
;--------------------------------------------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment