aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormitchell <70453897+667e-11@users.noreply.github.com>2010-06-17 18:34:42 -0400
committermitchell <70453897+667e-11@users.noreply.github.com>2010-06-17 18:34:42 -0400
commit34e8a85baecb9ef822a3d482474216e1aa2fb881 (patch)
tree0ab94200f53a434616e50190e66239d8dda18564
parentaf284ee664ab3da7ce6324a46d4d9a59ef1e2362 (diff)
downloadtextadept-34e8a85baecb9ef822a3d482474216e1aa2fb881.tar.gz
textadept-34e8a85baecb9ef822a3d482474216e1aa2fb881.zip
Added abbreviations for Lua commands from Jay Gould.
-rw-r--r--core/.command_entry.lua8
-rw-r--r--modules/textadept/command_entry.lua68
2 files changed, 59 insertions, 17 deletions
diff --git a/core/.command_entry.lua b/core/.command_entry.lua
index 89e59809..93615f22 100644
--- a/core/.command_entry.lua
+++ b/core/.command_entry.lua
@@ -22,9 +22,11 @@ module('gui.command_entry')
-- `Tab` key to display a list of available completions. Use the arrow keys to
-- make a selection and press `Enter` to insert it.
--
--- Note: Use [`gui.print()`][gui_print] instead of the global `print()`
--- function. The former prints to a new buffer, the latter to standard out
--- (`STDOUT`).
+-- Abbreviated commands for the `buffer`, `view` and `gui` are available. So
+-- `buffer:append_text('textadept')` can be shortened to
+-- `append_text('textadept')`. Please note `print()` calls
+-- [`gui.print()`][gui_print] and not Lua's `print()`. The latter can be
+-- accessed with `_G.print()`.
--
-- [gui_print]: ../modules/gui.html#print
--
diff --git a/modules/textadept/command_entry.lua b/modules/textadept/command_entry.lua
index 296244f4..32f32f1b 100644
--- a/modules/textadept/command_entry.lua
+++ b/modules/textadept/command_entry.lua
@@ -1,12 +1,35 @@
-- Copyright 2007-2010 Mitchell mitchell<att>caladbolg.net. See LICENSE.
+-- Modified by Jay Gould.
local locale = _G.locale
+local env = setmetatable({}, {
+ __index = function(t, k)
+ local f = buffer[k]
+ if f and type(f) == 'function' then
+ f = function(...) buffer[k](buffer, ...) end
+ elseif f == nil then
+ f = view[k] or gui[k] or _G[k]
+ end
+ return f
+ end,
+ __newindex = function(t, k, v)
+ for _, t2 in ipairs{ buffer, view, gui } do
+ if t2[k] ~= nil then
+ t2[k] = v
+ return
+ end
+ end
+ rawset(t, k, v)
+ end,
+})
+
events.connect('command_entry_command',
function(command) -- execute a Lua command
local f, err = loadstring(command)
if err then error(err) end
gui.command_entry.focus() -- toggle focus to hide
+ setfenv(f, env)
f()
end)
@@ -20,23 +43,40 @@ events.connect('command_entry_keypress',
elseif KEYSYMS[code] == '\t' then
local substring = ce.entry_text:match('[%w_.:]+$') or ''
local path, o, prefix = substring:match('^([%w_.:]-)([.:]?)([%w_]*)$')
- local ret, tbl = pcall(loadstring('return ('..path..')'))
- if not ret then tbl = getfenv(0) end
- if type(tbl) ~= 'table' then return end
+ local f, err = loadstring('return ('..path..')')
+ if type(f) == "function" then setfenv(f, env) end
+ local ret, tbl = pcall(f)
local cmpls = {}
- for k in pairs(tbl) do
- if type(k) == 'string' and k:find('^'..prefix) then
- cmpls[#cmpls + 1] = k
+ if not ret then -- shorthand notation
+ for _, t in ipairs{ buffer, view, gui, _G } do
+ for k in pairs(t) do
+ if type(k) == 'string' and k:find('^'..prefix) then
+ cmpls[#cmpls + 1] = k
+ end
+ end
end
- end
- if path == 'buffer' then
- if o == ':' then
- for f in pairs(_SCINTILLA.functions) do
- if f:find('^'..prefix) then cmpls[#cmpls + 1] = f end
+ for f in pairs(_SCINTILLA.functions) do
+ if f:find('^'..prefix) then cmpls[#cmpls + 1] = f end
+ end
+ for p in pairs(_SCINTILLA.properties) do
+ if p:find('^'..prefix) then cmpls[#cmpls + 1] = p end
+ end
+ else
+ if type(tbl) ~= 'table' then return end
+ for k in pairs(tbl) do
+ if type(k) == 'string' and k:find('^'..prefix) then
+ cmpls[#cmpls + 1] = k
end
- else
- for p in pairs(_SCINTILLA.properties) do
- if p:find('^'..prefix) then cmpls[#cmpls + 1] = p end
+ end
+ if path == 'buffer' then
+ if o == ':' then
+ for f in pairs(_SCINTILLA.functions) do
+ if f:find('^'..prefix) then cmpls[#cmpls + 1] = f end
+ end
+ else
+ for p in pairs(_SCINTILLA.properties) do
+ if p:find('^'..prefix) then cmpls[#cmpls + 1] = p end
+ end
end
end
end