/************************************************************************* * This macro works as a calculator. It uses eval() method to evaluate selected * text as an expression. The result is displayed and copied to the clipboard. * Functions: pow(), sqrt(), exp(), log(), abs(), * sin(), cos(), tan(), asin(), acos(), atan(), atan2(), * ceil(), floor(), round(), random(), min(), max() * Constants: PI, E, LN2, LN10, LOG2E, LOG10E, SQRT1_2, SQRT2 * See JScript or JavaScript reference (Math object) for details. * Examples of expressions: * (4*3.1-3.4)/1.5+6 * 7 % sqrt( pow (5,2) - 9*sin(PI/2) ) * 0xff * 1e-3 * 111111111*111111111==12345678987654321 * a=1; b=3; a/b * *originally posted 6/16/2004 at http://www.emeditor.com/forum/viewtopic.php?t=161 **************************************************************************/ str = document.selection.Text; if (str=="") { alert("selection is empty"); Quit(); } // optionally massage text before evaluation //str=str.replace( /,/g, ""); //str=str.replace( /\bln\s*\(/g, "log(" ); //str=str.replace( /\blg\s*\(/g, "1/LN10*log(" ); with (Math) result = eval(str); alert(result); clipboardData.clearData(); clipboardData.setData( "Text", result ); // custom functions can be added: //function fact(n) { // x=1; n=Math.round(n); // for (i=n; i>1; --i) // x=x*i; // return x; //}