diff options
author | 2007-08-09 15:47:47 -0400 | |
---|---|---|
committer | 2007-08-09 15:47:47 -0400 | |
commit | 473c7c0973a3f0760efac0e54c68b420695baff9 (patch) | |
tree | e3a88f9bb47a415712ff70625ac42b91ff7e6985 /modules/textadept/snippets.lua | |
parent | 712ddca9a7d5d2188af6c9f9a6598ccc6090fb47 (diff) | |
download | textadept-473c7c0973a3f0760efac0e54c68b420695baff9.tar.gz textadept-473c7c0973a3f0760efac0e54c68b420695baff9.zip |
Can use selected_text inside a snippet; modules/textadept/snippets.lua
Originally just the global environment was available for executing Lua code in
snippets, but now a 'selected_text' variable is available for (key) commands
that insert snippets. This is typically useful for wrapping selected text in a
snippet.
Diffstat (limited to 'modules/textadept/snippets.lua')
-rw-r--r-- | modules/textadept/snippets.lua | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/modules/textadept/snippets.lua b/modules/textadept/snippets.lua index 418e9bc9..45b8cb39 100644 --- a/modules/textadept/snippets.lua +++ b/modules/textadept/snippets.lua @@ -56,7 +56,7 @@ local escape, unescape, remove_escapes, _DEBUG function insert(snippet_arg) local buffer = buffer local orig_pos, new_pos, s_name - local selected_text = buffer:get_sel_text() + local sel_text = buffer:get_sel_text() if not snippet_arg then orig_pos = buffer.current_pos buffer:word_left_extend() new_pos = buffer.current_pos @@ -96,9 +96,12 @@ function insert(snippet_arg) _DEBUG('s_text escaped:\n'..s_text) -- Replace Lua code return. + local env = setmetatable( { selected_text = sel_text }, { __index = _G } ) s_text = s_text:gsub('$(%b())', function(s) - local ret, val = pcall( loadstring( 'return '..s:sub(2, -2) ) ) + local f = loadstring( 'return '..s:sub(2, -2) ) + setfenv(f, env) + local ret, val = pcall(f) if ret then return val or '' end buffer:goto_pos(orig_pos) error(val) @@ -118,7 +121,7 @@ function insert(snippet_arg) snippet.index = 0 snippet.start_pos = buffer.current_pos snippet.cursor = nil - snippet.sel_text = selected_text + snippet.sel_text = sel_text -- Make a table of placeholders and tab stops. local patt, patt2 = '($%b{})', '^%${(%d+):.*}$' |