@@ -176,17 +176,9 @@ M.keys = {
176176}
177177-- LuaFormatter on
178178
179- -- Add REPL to Tools menu.
180- table.insert (textadept .menu .menubar [_L [' Tools' ]], {' ' })
181- -- LuaFormatter off
182- table.insert (textadept .menu .menubar [_L [' Tools' ]], {_L [' Lua REPL' ], function ()
183- buffer .new ()._type = ' [Lua REPL]'
184- buffer :set_lexer (' lua' )
185- buffer :add_text (' -- ' .. _L [' Lua REPL' ]:gsub (' _' , ' ' ))
186- buffer :new_line ()
187- buffer :set_save_point ()
188- -- Cannot initially define keys in `keys.lua` because that table does not exist yet and will
189- -- be overwritten by the Lua language module. Instead, define keys here.
179+ -- Cannot initially define keys in `keys.lua` because that table does not exist yet and will
180+ -- be overwritten by the Lua language module. Instead, define keys here.
181+ local function register_keys ()
190182 if not keys .lua [next (M .keys )] then
191183 for key , f in pairs (M .keys ) do
192184 keys .lua [key ] = function ()
@@ -195,7 +187,46 @@ table.insert(textadept.menu.menubar[_L['Tools']], {_L['Lua REPL'], function()
195187 end
196188 end
197189 end
198- end })
199- -- LuaFormatter on
190+ end
191+ events .connect (events .RESET_AFTER , register_keys )
192+
193+ ---
194+ -- Creates or switches to a Lua REPL.
195+ -- If *new* is `true`, creates a new REPL even if one already exists.
196+ -- @param new Flag that indicates whether or not to create a new REPL even if one already exists.
197+ function M .open (new )
198+ local repl_view , repl_buf = nil , nil
199+ for i = 1 , # _VIEWS do
200+ if _VIEWS [i ].buffer ._type == ' [Lua REPL]' then
201+ repl_view = _VIEWS [i ]
202+ break
203+ end
204+ end
205+ for i = 1 , # _BUFFERS do
206+ if _BUFFERS [i ]._type == ' [Lua REPL]' then
207+ repl_buf = _BUFFERS [i ]
208+ break
209+ end
210+ end
211+ if new or not (repl_view or repl_buf ) then
212+ buffer .new ()._type = ' [Lua REPL]'
213+ buffer :set_lexer (' lua' )
214+ buffer :add_text (' -- ' .. _L [' Lua REPL' ]:gsub (' _' , ' ' ))
215+ buffer :new_line ()
216+ buffer :set_save_point ()
217+ register_keys ()
218+ else
219+ if repl_view then
220+ ui .goto_view (repl_view )
221+ else
222+ view :goto_buffer (repl_buf )
223+ end
224+ buffer :document_end () -- in case it's been scrolled in the meantime
225+ end
226+ end
227+
228+ -- Add REPL to Tools menu.
229+ table.insert (textadept .menu .menubar [_L [' Tools' ]], {' ' })
230+ table.insert (textadept .menu .menubar [_L [' Tools' ]], {_L [' Lua REPL' ], M .open })
200231
201232return M
0 commit comments