forked from rgieseke/ta-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathack.lua
More file actions
90 lines (82 loc) · 2.89 KB
/
ack.lua
File metadata and controls
90 lines (82 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
-- The __common.ack__ module provides an interface to the
-- [ack](http://betterthangrep.com/) command line utility for searching
-- in a directory of source files.<br>
-- As an alternative Textadept's find in files can be used.
-- Ack runs on Windows as well, but since it is not a package
-- repository away, Lua search is the default on Windows.<br>
-- Using `Alt/⌘`+`L` and`Alt/⌘`+`A` you can toggle between the two search
-- modes.
module('_m.common.ack', package.seeall)
-- To start a search in the project's root directory we use the
-- [common.project](project.html) module.
require 'common.project'
-- ### Fields
-- Ack settings.
-- They can be overwritten in your `init.lua` after loading the
-- _common_ module, for example:
-- _m.common.ack.OPTIONS = '--nocolor --nogroup --ignore-case '
OPTIONS = '--nocolor --nogroup '
-- ## Setup
local L = _G.locale.localize
local buffer_type = L('[Files Found Buffer]')
-- ## Functions
-- If the command entry is open and a search with ack or Lua
-- is active run the search after pressing enter.
events.connect('command_entry_command',
function(text)
if ack_search then
local search_dir = _m.common.project.root()
gui.command_entry.focus()
local command = 'ack '..OPTIONS..text
local p = io.popen(command..' '..search_dir..' 2>&1')
local out = p:read('*all')
p:close()
gui._print(buffer_type, 'ack: '..search_dir..'\n\n'..out)
ack_search = false
buffer:goto_pos(0)
buffer.read_only = 1
return true
end
if textadept_find_in_files then
local search_dir = _m.common.project.root()
gui.command_entry.focus()
gui.find.find_entry_text = text
gui.find.find_in_files(search_dir)
textadept_find_in_files = false
return true
end
end, 1)
-- Switch the search mode in the command entry.
events.connect('command_entry_keypress',
function(code, shift, control, alt)
local K = _G.keys.KEYSYMS
if ack_search or textadept_find_in_files then
if K[code] == 'esc' then
ack_search = nil
textadept_find_in_files = nil
gui.command_entry.focus()
gui.statusbar_text = ''
return true
elseif alt and string.char(code) == 'l' then
ack_search = nil
textadept_find_in_files = true
gui.statusbar_text = "Lua find in files: ".._m.common.project.root()
elseif alt and string.char(code) == 'a' then
ack_search = true
textadept_find_in_files = false
gui.statusbar_text = "ack: ".._m.common.project.root()
end
end
end, 1)
-- Open command entry to enter search term.
function search_entry()
if buffer.filename then
if WIN32 then
textadept_find_in_files = true
else
ack_search = true
end
gui.command_entry.entry_text = ''
gui.command_entry.focus()
end
end