aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormitchell <70453897+667e-11@users.noreply.github.com>2016-04-05 20:52:58 -0400
committermitchell <70453897+667e-11@users.noreply.github.com>2016-04-05 20:52:58 -0400
commita340aea2bd31b0f7360a597a7a6e06f37765122f (patch)
treec1858b96ce201f37144ea741e92d1429bffa2783
parent4b0e565061d8e0b292966cbc5935e9abb410ebab (diff)
downloadtextadept-a340aea2bd31b0f7360a597a7a6e06f37765122f.tar.gz
textadept-a340aea2bd31b0f7360a597a7a6e06f37765122f.zip
Support UTF-8 searches in files; modules/textadept/find.lua
-rw-r--r--modules/textadept/find.lua15
1 files changed, 13 insertions, 2 deletions
diff --git a/modules/textadept/find.lua b/modules/textadept/find.lua
index 1ba93536..b388161c 100644
--- a/modules/textadept/find.lua
+++ b/modules/textadept/find.lua
@@ -265,19 +265,30 @@ function M.find_in_files(dir)
ui._print(_L['[Files Found Buffer]'], _L['Find:']..' '..text)
buffer.indicator_current = M.INDIC_FIND
+ -- Note: I do not trust utf8.find completely, so only use it if there are
+ -- UTF-8 characters in the pattern. Otherwise default to string.find.
+ local lib_find = not text:find('[\xC2-\xF4]') and string.find or utf8.find
local found = false
lfs.dir_foreach(dir, function(filename)
local match_case = M.match_case
local line_num = 1
for line in io.lines(filename) do
- local s, e = (match_case and line or line:lower()):find(text)
+ local s, e = lib_find(match_case and line or line:lower(), text)
if s and e then
local utf8_filename = filename:iconv('UTF-8', _CHARSET)
buffer:append_text(string.format('%s:%d:%s\n', utf8_filename, line_num,
line))
local pos = buffer:position_from_line(buffer.line_count - 2) +
#utf8_filename + #tostring(line_num) + 2
- buffer:indicator_fill_range(pos + s - 1, e - s + 1)
+ if lib_find == string.find then
+ -- Positions are bytes.
+ buffer:indicator_fill_range(pos + s - 1, e - s + 1)
+ else
+ -- Positions are characters, which may be multiple bytes.
+ s = buffer:position_relative(pos, s - 1)
+ e = buffer:position_relative(pos, e)
+ buffer:indicator_fill_range(s, e - s)
+ end
found = true
end
line_num = line_num + 1