# Scite Inplace Calculator

A simple calculator based on the user formula. It requires luawin.dll. The formula is a string that includes "<>" as a place holder for the number.

### Script to execute an existing formula

If there is a selection (line or rectangular selection), the operation will occur in the selection; otherwise, the next number to the right will be updated. It only supports very simple number format.

```function executeFormula()
editor:BeginUndoAction()
if not formula or formula =="" then
print('Please set formula first by ALT+SHIFT+F.')
return
end

local expr = editor:GetSelText()
local ss = editor.CurrentPos
local se = editor.Anchor
if not expr or expr == "" then
ss, se = editor:findtext("\-*[0-9]+[.0-9]*", SCFIND_REGEXP, ss)
end

if ss > se then ss,se = se, ss end
local cs, ce
if editor.SelectionIsRectangle then
cs = editor.Column[ss]
ce = editor.Column[se]
-- print (cs..' '..ce)
end
-- print (ss..' '..se)

for m in editor:match("\-*[0-9]+[.0-9]*", SCFIND_REGEXP, ss) do
local c = m.pos
if c > se then break end

local width = m.len
local col = editor.Column[c]
local chFlag
-- print(col .. ' ' ..width ..'|'..cs..' '..ce)
if not editor.SelectionIsRectangle or
(col >= cs and (col + width -1) <= ce) then
chFlag = true
else
chFlag = false
end

if chFlag then
expr = m.text
local pt = string.find(expr, '.', 1, true)
local acc, fmt
if pt then
acc = width - pt
if acc < 0 then acc = 0 end
fmt = "%"..width.."."..acc.."f"
else
fmt = "%"..width.."i"
end
-- print (expr .. "|" .. fmt.."|"..pt)
expr = string.gsub(formula, '<>', expr)
local f, msg = loadstring("return "..expr)
if f then
m:replace(string.format(fmt, f()))
else
print(">Execute Formula: cannot evaluate the formula")
end
end
end
editor:EndUndoAction()
end
```
--JRN

### Script to prompt for a formula and then execute the operation

It requires the luawin.dll to ask for a formula.

```local formula
local Init =loadlib('C:\\Program Files\\Scintilla Text Editor\\luawin.dll', 'Init')
Init()

function setFormula()
local flag, msg = win.InputBox('Formula: <> as the place holder. e.g. <>*10')
if flag and string.find(msg,'<>',1,true) then
formula = msg
end

if formula then msg = formula else msg = "NONE" end
print("FORMULA: "..msg)

executeFormula()
end
```
--JRN
RecentChanges · preferences
edit · history
Last edited August 31, 2006 6:32 pm GMT (diff)