diff options
-rw-r--r-- | doc/manual.md | 15 | ||||
-rw-r--r-- | modules/textadept/command_entry.lua | 22 |
2 files changed, 28 insertions, 9 deletions
diff --git a/doc/manual.md b/doc/manual.md index 4aed5d5f..ad882e50 100644 --- a/doc/manual.md +++ b/doc/manual.md @@ -1343,12 +1343,15 @@ For now, the [wiki][] hosts third-party, user-created themes. The command entry grants access to Textadept's Lua state. Press `Ctrl+E` (`⌘E` on Mac OSX | `M-C` in curses) to display the entry. It is useful for debugging, -inspecting, and entering `buffer` or `view` commands. If you try to cause -instability in Textadept's Lua state, you will probably succeed so be careful. -The [Lua API][] lists available commands. The command entry provides abbreviated -commands for [`buffer`][], [`view`][] and [`ui`][]: for example you may reduce -the `buffer:append_text('foo')` command to `append_text('foo')`. These commands -are runnable on startup using the `-e` and `--execute` command line switches. +inspecting, and entering commands (e.g. `buffer` or `view` commands). If you try +to cause instability in Textadept's Lua state, you will probably succeed, so be +careful. The [Lua API][] lists available commands. In addition to behaving like +Lua's interactive prompt, the command entry provides some shortcuts for common +[`buffer`][], [`view`][] and [`ui`][] commands. For example, instead of entering +`buffer:append_text('foo')`, you can use `append_text('foo')`. Also, function +call parentheses can be omitted. For example, instead of `view:split()`, you can +simply use `split`. Finally, these commands are runnable on startup using the +`-e` and `--execute` command line switches. Pressing `Ctrl+H` (`^H` | `M-H` or `M-S-H`) shows help for the current command. 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 |