- Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathSessionManager.lua
More file actions
Latest commit
187 lines (165 loc) · 6.16 KB
/
Copy pathSessionManager.lua
File metadata and controls
187 lines (165 loc) · 6.16 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
-- A very simplistic session manager. It opens previously open files and restores
-- cursor position and scroll position. It is not perfect, for example it does
-- not keep track of the order or in which view the files are in. This code must
-- be ran during startup. This is just a demonstration of how to use the API
-- and what can be done in a small amount of Lua.
localopened_files= {}
localis_shutting_down=false
-- If a session is found, load it. Note this cannot be done directly during
-- startup since N++ hasn't completely initialized yet.
npp.AddEventHandler("OnReady", function()
localfiles=table.load(npp:GetPluginsConfigDir() .."\\session.lua")
iffiles~=nilthen
fori,vinpairs(files) do
npp:DoOpen(v.name)
ifv.langthennpp.BufferLangType[npp.CurrentBufferID] =v.langend
editor.FirstVisibleLine=v.topor0
editor.CurrentPos=v.posor0
editor.Anchor=v.anchoror0
end
end
returnfalse
end)
-- Remove any files that are closed
npp.AddEventHandler("OnClose", function(f,i)
-- If N++ is in the middle of shutting down, just ignore it
ifis_shutting_downthenreturnfalseend
opened_files[i] =nil
returnfalse
end)
-- This won't be called unless the user manually changes it, else it just
-- defaults to whatever value N++ decides is best
npp.AddEventHandler("OnLangChange", function()
locali=npp.CurrentBufferID
opened_files[i].lang=npp.BufferLangType[i]
end)
-- Keep track of some of the variables we want to restore in the file
-- This event will also catch any files being opened for the first time
npp.AddEventHandler("OnUpdateUI", function()
locali=npp.CurrentBufferID
ifopened_files[i] ==nilthen
opened_files[i] = {}
opened_files[i].name=npp:GetFullCurrentPath()
end
opened_files[i].pos=editor.CurrentPos
opened_files[i].anchor=editor.Anchor
opened_files[i].top=editor.FirstVisibleLine
returnfalse
end)
npp.AddEventHandler("OnFileRenamed", function(f,i)
opened_files[i].name=f-- update the name
returnfalse
end)
-- Set a flag so we know N++ is shutting down
npp.AddEventHandler("OnBeforeShutdown", function()
is_shutting_down=true
returnfalse
end)
-- The user could cancel the shutdown
npp.AddEventHandler("OnCancelShutdown", function()
is_shutting_down=false
returnfalse
end)
-- If it is actually shutting down, save the session file
npp.AddEventHandler("OnShutdown", function()
-- Remove any dummy files, e.g. "new 1"
forid, tabinpairs(opened_files) do
iftab.name:find("^new %d+$") ~=nilthen
opened_files[id] =nil
end
end
table.save(opened_files, npp:GetPluginsConfigDir() .."\\session.lua")
returnfalse
end)
-- These below are merely helper fuctions to serialize tables. They were found
-- laying around on the Internet somewhere and worked for this case.
functiontable.save(tbl,filename )
localfunctionexportstring( s )
returnstring.format("%q", s)
end
localcharS,charE="\t","\n"
localfile,err=io.open( filename, "wb" )
iferrthenreturnerrend
-- initiate variables for save procedure
localtables,lookup= { tbl },{ [tbl] =1 }
file:write( "return {"..charE )
foridx,tinipairs( tables ) do
file:write( "-- Table: {"..idx.."}"..charE )
file:write( "{"..charE )
localthandled= {}
fori,vinipairs( t ) do
thandled[i] =true
localstype=type( v )
-- only handle value
ifstype=="table" then
ifnotlookup[v] then
table.insert( tables, v )
lookup[v] =#tables
end
file:write( charS.."{"..lookup[v].."},"..charE )
elseifstype=="string" then
file:write( charS..exportstring( v )..","..charE )
elseifstype=="number" then
file:write( charS..tostring( v )..","..charE )
end
end
fori,vinpairs( t ) do
-- escape handled values
if (notthandled[i]) then
localstr=""
localstype=type( i )
-- handle index
ifstype=="table" then
ifnotlookup[i] then
table.insert( tables,i )
lookup[i] =#tables
end
str=charS.."[{"..lookup[i].."}]="
elseifstype=="string" then
str=charS.."["..exportstring( i ).."]="
elseifstype=="number" then
str=charS.."["..tostring( i ).."]="
end
ifstr~="" then
stype=type( v )
-- handle value
ifstype=="table" then
ifnotlookup[v] then
table.insert( tables,v )
lookup[v] =#tables
end
file:write( str.."{"..lookup[v].."},"..charE )
elseifstype=="string" then
file:write( str..exportstring( v )..","..charE )
elseifstype=="number" then
file:write( str..tostring( v )..","..charE )
end
end
end
end
file:write( "},"..charE )
end
file:write( "}" )
file:close()
end
functiontable.load( sfile )
localftables,err=loadfile( sfile )
iferrthenreturn_,errend
localtables=ftables()
foridx=1,#tablesdo
localtolinki= {}
fori,vinpairs( tables[idx] ) do
iftype( v ) =="table" then
tables[idx][i] =tables[v[1]]
end
iftype( i ) =="table" andtables[i[1]] then
table.insert( tolinki,{ i,tables[i[1]] } )
end
end
-- link indices
for_,vinipairs( tolinki ) do
tables[idx][v[2]],tables[idx][v[1]] =tables[idx][v[1]],nil
end
end
returntables[1]
end