aboutsummaryrefslogtreecommitdiff
path: root/modules/textadept/find.lua
diff options
context:
space:
mode:
authormitchell <70453897+667e-11@users.noreply.github.com>2016-04-16 13:38:55 -0400
committermitchell <70453897+667e-11@users.noreply.github.com>2016-04-16 13:38:55 -0400
commit367d89d08c30ca174b213607684572d2d68a3ab9 (patch)
tree98f88897d06085288a211a38525b6a662727f0c4 /modules/textadept/find.lua
parent66eb8256f829ddc5e0a3961428f9e986187b9f7a (diff)
downloadtextadept-367d89d08c30ca174b213607684572d2d68a3ab9.tar.gz
textadept-367d89d08c30ca174b213607684572d2d68a3ab9.zip
Find in files should not print the contents of binary files.
Use placeholder text instead. This is common in Unix tools.
Diffstat (limited to 'modules/textadept/find.lua')
-rw-r--r--modules/textadept/find.lua42
1 files changed, 28 insertions, 14 deletions
diff --git a/modules/textadept/find.lua b/modules/textadept/find.lua
index 6e234dbd..a7ca40a0 100644
--- a/modules/textadept/find.lua
+++ b/modules/textadept/find.lua
@@ -274,28 +274,42 @@ function M.find_in_files(dir)
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 f = io.open(filename, 'rb')
+ local binary, line_num = nil, 1
+ for line in f:lines() do
local s, e = lib_find(match_case and line or line:lower(), text)
if s and e then
+ found = true
+ if binary == nil then
+ local pos = f:seek()
+ f:seek('set') -- rewind
+ binary = f:read(65536):find('\0')
+ f:seek('set', pos) -- restore
+ end
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
- if lib_find == string.find then
- -- Positions are bytes.
- buffer:indicator_fill_range(pos + s - 1, e - s + 1)
+ if not binary then
+ 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
+ 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
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)
+ buffer:append_text(string.format('%s:1:%s\n', utf8_filename,
+ _L['Binary file matches.']))
+ break
end
- found = true
end
line_num = line_num + 1
end
+ f:close()
end, M.FILTER, true)
if not found then buffer:append_text(_L['No results found']) end
ui._print(_L['[Files Found Buffer]'], '') -- goto end, set save pos, etc.