diff options
author | 2017-06-22 23:34:25 -0400 | |
---|---|---|
committer | 2017-06-22 23:34:25 -0400 | |
commit | 823d2770fe7ea7e8269ea43119d98f3f9130611e (patch) | |
tree | 528a040e77e30cd39914364aa1c7212ed7e3a0b9 /modules | |
parent | 838a4812cda87badd9b8b8425ab1cc0ac6053821 (diff) | |
download | textadept-823d2770fe7ea7e8269ea43119d98f3f9130611e.tar.gz textadept-823d2770fe7ea7e8269ea43119d98f3f9130611e.zip |
Lua command entry improvements.
Mimic Lua 5.3 interpreter by auto-printing results and making '=' prefix
optional, call returned functions implicitly (no calling parentheses required),
and pretty print tables.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/textadept/command_entry.lua | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/modules/textadept/command_entry.lua b/modules/textadept/command_entry.lua index 066c4213..a549dfbb 100644 --- a/modules/textadept/command_entry.lua +++ b/modules/textadept/command_entry.lua @@ -131,11 +131,27 @@ local env = setmetatable({}, { -- environment. -- In this environment, the contents of the `buffer`, `view`, and `ui` tables -- are also considered as global functions and fields. --- Prints the results of '=' expressions like in the Lua prompt. +-- Prints the results of expressions like in the Lua prompt. Also invokes bare +-- functions as commands. -- @param code The Lua code to execute. local function run_lua(code) - if code:find('^=') then code = 'return '..code:sub(2) end - local result = assert(load(code, nil, 'bt', env))() + if code:find('^=') then code = code:sub(2) end -- for compatibility + local f, errmsg = load('return '..code, nil, 't', env) + if not f then f, errmsg = load(code, nil, 't', env) end + local result = assert(f, errmsg)() + if type(result) == 'function' then result = result() end + if type(result) == 'table' then + local items = {} + for k, v in pairs(result) do + items[#items + 1] = tostring(k)..' = '..tostring(v) + end + table.sort(items) + result = '{'..table.concat(items, ', ')..'}' + if buffer.edge_column > 0 and #result > buffer.edge_column then + local indent = string.rep(' ', buffer.tab_width) + result = '{\n'..indent..table.concat(items, ',\n'..indent)..'\n}' + end + end if result ~= nil or code:find('^return ') then ui.print(result) end events.emit(events.UPDATE_UI) end |