[go: up one dir, main page]

Menu

[r80]: / lcstack.lua  Maximize  Restore  History

Download this file

70 lines (59 with data), 1.5 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
--
-- stack to store the commands so we can undo/redo
-- has a fixed maximum size, and will go back to the beginning
-- and over write values once full.
--
-- set max to a much larger number, once undo/redo is more stable.
commandstack = {max=50, top=1, depth=0}
-- put a command on the stack
-- @param s - the stack to push the value on to.
-- @param value - the value to push on to the stack.
-- @returns nothing
function commandstack.push (s, value)
s.depth=0
s[s.top] = value
s.top = (s.top+1)%s.max
end
-- different undo message depending on failure, useful for
-- debugging.
function commandstack.undo(s)
if s.depth < s.max-1 then
s.depth = s.depth+1
local f = s[(s.top-s.depth)%s.max]
if f then
f.undo()
return "undo!", f
else
return " no more to undo!"
end
else
return " no more to UNDO!"
end
end
function commandstack.redo(s)
if s.depth > 0 then
local f = s[(s.top-s.depth)%s.max]
if f then
f.redo()
s.depth = s.depth-1
return "redo!"
else
return "no more to redo"
end
else
return " no more to REDO!"
end
end
-- macro stack class.
macrostack = {}
macrostack.__index = macrostack
function macrostack.__call()
return setmetatable({stack={}}, macrostack)
end
--append
function macrostack:append(val)
self.stack[#self.stack +1]=val
end
function macrostack:ipairs()
return ipairs(self.stack)
end