#pragma TextEncoding = "UTF-8" #pragma rtGlobals=3 #pragma version=1.02 // by tony.withers@uwo.ca // use up and down arrows to insert previously executed lines // use shift-up/down to replace selection with previous answers // execution environment is current data folder // global variables v_CalcPad and s_CalcPad are created temporarily in // the current data folder menu "Macros" "CalcPad", /Q end function CalcPad() NewDataFolder /O root:Packages NewDataFolder /O root:Packages:CalcPad DFREF dfr = root:Packages:CalcPad if(waveexists(dfr:history)==0) make /o/T/N=0 dfr:w_history, dfr:w_answers variable /G dfr:v_ans=0, dfr:v_hist=0 endif if (strlen(WinList("CalcPadNB", ";", "WIN:16"))) dowindow /F CalcPadNB else NewNotebook /K=1 /F=1 /W=(20, 50, 400, 300) /N=CalcPadNB as "CalcPad" notebook CalcPadNB showRuler=0, fSize=14 Notebook CalcPadNB text="\t\t\t* CalcPad *\r" endif SetWindow CalcPadNB, hook(hCalcPad)=CalcPadHook return 1 end function CalcPadHook(H_Struct) STRUCT WMWinHookStruct &H_Struct DFREF dfr = root:Packages:CalcPad wave /T w_hist=dfr:w_history, w_ans=dfr:w_answers NVAR v_ans=dfr:v_ans, v_hist=dfr:v_hist if(H_Struct.eventcode==11) // keyboard event if(H_Struct.keycode==28 || H_Struct.keycode==29) // left or right arrow return 0 // allow these for selecting text endif // make sure that we're typing on the last line GetSelection notebook, CalcPadNB, 1 // get current position in notebook // store position make /free w={V_startParagraph, V_startPos, V_endParagraph, V_endPos} // find last paragraph notebook CalcPadNB selection={endOfFile,endofFile} GetSelection notebook, CalcPadNB, 1 if (V_startParagraph==w[0]) // we're writing on the last line // restore current position notebook CalcPadNB selection={(w[0],w[1]), (w[2],w[3])} else notebook CalcPadNB findText={"",1} return 1 endif // this allows select, copy within notebook // typing allowed on the last line only // trying to type elsewhere sends cursor to end of file string s_insert if (H_Struct.keycode==30 || H_Struct.keycode==31) // up or down arrow if (H_Struct.eventMod & 2) // shift key // replace currently highlighted text with a previous answer GetSelection notebook, CalcPadNB, 1 w={V_startParagraph, V_startPos, V_endParagraph, V_endPos} v_ans+=1-2*(H_Struct.keycode==30) v_ans=max(0, v_ans) if (v_ansnumpnts(w_hist)-1) s_insert="" else s_insert=w_hist(v_hist) endif v_hist=min(v_hist, numpnts(w_hist)) Notebook CalcPadNB text=s_insert notebook CalcPadNB selection={endOfFile,endofFile} endif return 1 endif if (H_Struct.keycode==8) // delete GetSelection notebook, CalcPadNB, 1 // get current position in notebook if(V_startPos==0 && V_endPos==0) // Don't delete the start of the last line return 1 endif endif if (H_Struct.keycode==13) // return // select the last paragraph notebook CalcPadNB selection={startOfParagraph, endOfChars} GetSelection notebook, CalcPadNB, 2 // sets S_selection if (strlen(s_selection)==0) return 1 endif notebook CalcPadNB selection={endOfParagraph, endOfParagraph} w_hist[numpnts(w_hist)]={s_selection} v_hist=numpnts(w_hist) variable /G v_CalcPad=nan // work with a global variable in the current data folder so that // we can be folder aware execute /z/q "v_CalcPad = "+S_selection NVAR v_result=v_CalcPad string s_result if(numtype(v_result)!=0) // not a normal number, so try to evaluate as text string /G s_CalcPad="" execute /Z/Q "s_CalcPad = "+S_selection if (strlen(s_CalcPad)) sprintf s_result, "\"%s\"", s_CalcPad else sprintf s_result, "%g", v_result endif else sprintf s_result, "%g", v_result endif w_ans[numpnts(w_ans)]={s_result} v_ans=numpnts(w_ans) Notebook CalcPadNB text="\r\t" notebook CalcPadNB textRGB=(0,0,65280) Notebook CalcPadNB text=s_result +"\r" notebook CalcPadNB textRGB=(0,0,0) KillVariables /Z v_result KillStrings /Z s_CalcPad return 1 endif endif if(H_Struct.eventcode==10) // menu if (stringmatch(H_Struct.menuItem, "Paste")) GetSelection notebook, CalcPadNB, 1 // get current position in notebook // store position make /free w={V_startParagraph, V_startPos, V_endParagraph, V_endPos} // find last paragraph notebook CalcPadNB selection={endOfFile,endofFile} GetSelection notebook, CalcPadNB, 1 if (V_startParagraph==w[0]) // we're writing on the last line // restore current position notebook CalcPadNB selection={(w[0],w[1]), (w[2],w[3])} else // trying to paste somewhere else return 1 endif endif endif if(H_Struct.eventcode==2) // kill window killdatafolder /Z dfr endif return 0 end