129129-- ---------------------------------------------------------------------------
130130-- init_theme(THEME)
131131--
132- -- Initialize THEME. Uses the theme search path.
132+ -- Initialize THEME. Uses the theme search path. Returns the theme.
133+ --
134+ -- If the theme has already been initialized by an earlier call to this
135+ -- function, the existing theme is returned.
133136-- ---------------------------------------------------------------------------
134137function themepark :init_theme (theme )
138+ if themepark .themes [theme ] then
139+ return themepark .themes [theme ]
140+ end
141+
135142 if theme == ' ' then
136143 local dir = script_path (2 )
137144 themepark .themes [' ' ] = { dir = dir }
@@ -178,31 +185,31 @@ function themepark:init_theme(theme)
178185 if themepark .debug then
179186 print (" Themepark: Loading theme '" .. theme .. " ' done." )
180187 end
188+
189+ return themepark .themes [theme ]
181190end
182191
183192-- ---------------------------------------------------------------------------
184193-- ---------------------------------------------------------------------------
185194function themepark :add_topic (topic , cfg )
186- local theme = ' '
195+ local theme_name = ' '
187196 local slash = string.find (topic , ' /' )
188197 if slash then
189- theme = string.sub (topic , 1 , slash - 1 )
198+ theme_name = string.sub (topic , 1 , slash - 1 )
190199 topic = string.sub (topic , slash + 1 )
191200 end
192201
193- if not themepark .themes [theme ] then
194- themepark :init_theme (theme )
195- end
202+ local theme = themepark :init_theme (theme_name )
196203
197204 if themepark .debug then
198- print (" Themepark: Adding topic '" .. topic .. " ' from theme '" .. theme .. " ' ..." )
205+ print (" Themepark: Adding topic '" .. topic .. " ' from theme '" .. theme_name .. " ' ..." )
199206 end
200207
201- local filename = themepark . themes [ theme ] .dir .. ' /topics/' .. topic .. ' .lua'
208+ local filename = theme .dir .. ' /topics/' .. topic .. ' .lua'
202209
203210 local file , errmsg = io.open (filename , ' r' )
204211 if not file then
205- error (" No topic '" .. topic .. " ' in theme '" .. theme .. " '" )
212+ error (" No topic '" .. topic .. " ' in theme '" .. theme_name .. " '" )
206213 end
207214
208215 local script = file :read (' a*' )
@@ -213,10 +220,10 @@ function themepark:add_topic(topic, cfg)
213220 error (' Load failed: ' .. msg )
214221 end
215222
216- local result = func (self , self . themes [ theme ] , cfg or {})
223+ local result = func (self , theme , cfg or {})
217224
218225 if themepark .debug then
219- print (" Themepark: Adding topic '" .. topic .. " ' from theme '" .. theme .. " ' done." )
226+ print (" Themepark: Adding topic '" .. topic .. " ' from theme '" .. theme_name .. " ' done." )
220227 end
221228
222229 return result
0 commit comments