Scite 内置计算器 |
|
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
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