aboutsummaryrefslogtreecommitdiff
path: root/core/ext/find.lua
diff options
context:
space:
mode:
authormitchell <70453897+667e-11@users.noreply.github.com>2007-08-11 17:59:37 -0400
committermitchell <70453897+667e-11@users.noreply.github.com>2007-08-11 17:59:37 -0400
commit1dc88b9de221b380cd525513e9e5a3c7c4d45698 (patch)
tree2494c9bb49444dbf85afa44095439ef4a61ec688 /core/ext/find.lua
parentb178dd7961297818664091047c89b05329f33dc9 (diff)
downloadtextadept-1dc88b9de221b380cd525513e9e5a3c7c4d45698.tar.gz
textadept-1dc88b9de221b380cd525513e9e5a3c7c4d45698.zip
Fixed escapes in replace, added %() sequence; core/ext/find.lua
'%%' is now properly escaped. %() sequence executes Lua code, showing an error dialog if one occured.
Diffstat (limited to 'core/ext/find.lua')
-rw-r--r--core/ext/find.lua27
1 files changed, 23 insertions, 4 deletions
diff --git a/core/ext/find.lua b/core/ext/find.lua
index 8ff5236d..640317fd 100644
--- a/core/ext/find.lua
+++ b/core/ext/find.lua
@@ -81,18 +81,37 @@ end
-- via scripts.
-- textadept.find.find is called first, to select any found text. The selected
-- text is then replaced by the specified replacement text.
--- @param rtext The text to replace found text with. It can contain Lua escape
--- sequences to use text captured by a Lua pattern. (%n where 1 <= n <= 9.)
+-- @param rtext The text to replace found text with. It can contain both Lua
+-- capture items (%n where 1 <= n <= 9) for Lua pattern searches and %()
+-- sequences for embedding Lua code for any search.
function find.replace(rtext)
if #buffer:get_sel_text() == 0 then return end
local buffer = buffer
buffer:target_from_selection()
+ rtext = rtext:gsub('%%%%', '\\037') -- escape '%%'
if find.captures then
for i, v in ipairs(find.captures) do
- rtext = rtext:gsub('[^%%]?[^%%]?%%'..i, v) -- not entirely correct
+ rtext = rtext:gsub('%%'..i, v)
end
end
- buffer:replace_target( rtext:gsub('\\[abfnrtv\\]', escapes) )
+ local ret, rtext = pcall( rtext.gsub, rtext, '%%(%b())',
+ function(code)
+ local ret, val = pcall( loadstring('return '..code) )
+ if not ret then
+ os.execute('zenity --error --text "'..val:gsub('"', '\\"')..'"')
+ error()
+ end
+ return val
+ end )
+ if ret then
+ rtext = rtext:gsub('\\037', '%%') -- unescape '%'
+ buffer:replace_target( rtext:gsub('\\[abfnrtv\\]', escapes) )
+ buffer:goto_pos(buffer.target_end + 1) -- 'find' text after this replacement
+ else
+ -- Since find is called after replace returns, have it 'find' the current
+ -- text again, rather than the next occurance so the user can fix the error.
+ buffer:goto_pos(buffer.current_pos)
+ end
end
---