37
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local ipairs = Luan.ipairs or error()
|
|
4 local range = Luan.range or error()
|
|
5 local String = require "luan:String.luan"
|
|
6 local regex = String.regex or error()
|
|
7 local regex_quote = String.regex_quote or error()
|
|
8 local regex_quote_replacement = String.regex_quote_replacement or error()
|
|
9 local repeated = String.repeated or error()
|
|
10 local new_text_field = require("luan:swing/Text_field.luan").new or error()
|
|
11 local new_check_box = require("luan:swing/Check_box.luan").new or error()
|
|
12 local new_label = require("luan:swing/Label.luan").new or error()
|
|
13 local new_panel = require("luan:swing/Component.luan").new_panel or error()
|
|
14 local Layout = require "luan:swing/Layout.luan"
|
|
15 local new_mig_layout = Layout.new_mig_layout or error()
|
|
16 local new_button = require("luan:swing/Button.luan").new or error()
|
|
17 local Swing = require "luan:swing/Swing.luan"
|
|
18 local browse = Swing.browse or error()
|
|
19
|
|
20
|
87
|
21 local find_text = ""
|
|
22
|
37
|
23 local function get_matches(text,s)
|
|
24 local r = regex(s)
|
|
25 local matches = {}
|
|
26 local i = 1
|
|
27 local n = #text
|
|
28 while i <= n do
|
|
29 local j1, j2 = r.find(text,i)
|
|
30 if j1 == nil then
|
|
31 break
|
|
32 end
|
|
33 j2 = j2 + 1
|
|
34 if j1 == j2 then
|
|
35 i = j2 + 1
|
|
36 else
|
|
37 matches[#matches+1] = { start=j1, end_=j2 }
|
|
38 i = j2
|
|
39 end
|
|
40 end
|
|
41 return matches
|
|
42 end
|
|
43
|
|
44 local function make_find_panel(window)
|
|
45 local text_area = window.text_area
|
|
46 local status_bar = window.status_bar
|
|
47 local find_field, replace_field, regex_check_box
|
|
48 local function find_match(event)
|
|
49 --logger.info("action "..event.action)
|
|
50 local s = find_field.text
|
|
51 if #s == 0 then
|
|
52 status_bar.text = " "
|
|
53 return
|
|
54 end
|
87
|
55 find_text = s
|
37
|
56 if not regex_check_box.is_selected then
|
|
57 s = regex_quote(s)
|
|
58 end
|
|
59 local matches
|
|
60 try
|
|
61 matches = get_matches( text_area.text, s )
|
|
62 catch e
|
|
63 status_bar.text = "Regex error: "..e.get_message()
|
|
64 return
|
|
65 end
|
|
66 text_area.set_hightlights(matches)
|
|
67 local n_matches = #matches
|
|
68 if n_matches == 0 then
|
|
69 status_bar.text = "0 matches"
|
|
70 return
|
|
71 end
|
|
72 local action = event.action
|
|
73 if action == "next" then
|
|
74 local _, pos = text_area.get_selection()
|
|
75 for i, match in ipairs(matches) do
|
|
76 if match.start >= pos then
|
|
77 text_area.set_selection( match.start, match.end_ )
|
|
78 status_bar.text = i.." of "..n_matches.." matches"
|
|
79 return
|
|
80 end
|
|
81 end
|
|
82 local match = matches[1]
|
|
83 text_area.set_selection( match.start, match.end_ )
|
|
84 status_bar.text = "1 of "..n_matches.." matches; wrapped past end"
|
|
85 elseif action == "previous" then
|
|
86 local pos = text_area.get_selection()
|
|
87 for i in range(n_matches,1,-1) do
|
|
88 local match = matches[i]
|
|
89 if match.end_ <= pos then
|
|
90 text_area.set_selection( match.start, match.end_ )
|
|
91 status_bar.text = i.." of "..n_matches.." matches"
|
|
92 return
|
|
93 end
|
|
94 end
|
|
95 local match = matches[n_matches]
|
|
96 text_area.set_selection( match.start, match.end_ )
|
|
97 status_bar.text = n_matches.." of "..n_matches.." matches; wrapped past end"
|
|
98 else
|
|
99 error(action)
|
|
100 end
|
|
101 end
|
|
102 local function replace_match(event)
|
|
103 local find = find_field.text
|
|
104 if #find == 0 then
|
|
105 status_bar.text = " "
|
|
106 return
|
|
107 end
|
87
|
108 find_text = find
|
37
|
109 local replace = replace_field.text
|
|
110 if not regex_check_box.is_selected then
|
|
111 find = regex_quote(find)
|
|
112 replace = regex_quote_replacement(replace)
|
|
113 end
|
|
114 local r
|
|
115 try
|
|
116 r = regex(find)
|
|
117 catch e
|
|
118 status_bar.text = "Regex error: "..e.get_message()
|
|
119 return
|
|
120 end
|
|
121 local new, n
|
|
122 local action = event.action
|
|
123 if action == "replace" then
|
|
124 local selection = text_area.selected_text
|
|
125 new, n = r.gsub(selection,replace)
|
|
126 if n > 0 then
|
|
127 text_area.selected_text = new
|
|
128 end
|
|
129 elseif action == "replace_all" then
|
|
130 local text = text_area.text
|
|
131 new, n = r.gsub(text,replace)
|
|
132 if n > 0 then
|
|
133 text_area.text = new
|
|
134 end
|
|
135 else
|
|
136 error(action)
|
|
137 end
|
|
138 status_bar.text = n.." replacements"
|
|
139 end
|
|
140 find_field = new_text_field{
|
|
141 constraints = "growx"
|
|
142 whitespace_visible = true
|
|
143 action = "next"
|
|
144 action_listener = find_match
|
|
145 }
|
|
146 replace_field = new_text_field{
|
|
147 constraints = "growx"
|
|
148 whitespace_visible = true
|
|
149 action = "replace"
|
|
150 action_listener = replace_match
|
|
151 }
|
|
152 regex_check_box = new_check_box{
|
|
153 text = "Use Regex"
|
|
154 }
|
|
155 local find_panel = new_panel{
|
|
156 constraints = "growy 0,growx"
|
|
157 layout = new_mig_layout("insets 8 16 0 16","[][grow][grow 0]")
|
69
|
158 is_visible = false
|
37
|
159 children = {
|
|
160 new_label{
|
|
161 constraints = "right"
|
|
162 text = "Find:"
|
|
163 }
|
|
164 find_field
|
|
165 new_button{
|
|
166 constraints = "grow"
|
|
167 text = "Find Next"
|
|
168 action = "next"
|
|
169 action_listener = find_match
|
|
170 }
|
|
171 new_button{
|
|
172 constraints = "grow,wrap"
|
|
173 text = "Find Previous"
|
|
174 action = "previous"
|
|
175 action_listener = find_match
|
|
176 }
|
|
177 new_label{
|
|
178 constraints = "right"
|
|
179 text = "Replace:"
|
|
180 }
|
|
181 replace_field
|
|
182 new_button{
|
|
183 constraints = "grow"
|
|
184 text = "Replace"
|
|
185 tool_tip_text = "Replace matches in selected text"
|
|
186 action = "replace"
|
|
187 action_listener = replace_match
|
|
188 }
|
|
189 new_button{
|
|
190 constraints = "grow,wrap"
|
|
191 text = "Replace All"
|
|
192 action = "replace_all"
|
|
193 action_listener = replace_match
|
|
194 }
|
|
195 new_panel{
|
70
|
196 constraints = "span,grow,wrap"
|
37
|
197 layout = new_mig_layout("insets 0,gap 16px")
|
|
198 children = {
|
|
199 regex_check_box
|
|
200 new_button{
|
|
201 text = "Learn About Regular Expressions"
|
|
202 action_listener = function(_)
|
|
203 browse("https://www.reactionary.software/learn.html#regex")
|
|
204 end
|
|
205 }
|
70
|
206 new_button{
|
|
207 constraints = "pushx, align right"
|
|
208 text = "Close"
|
|
209 action_listener = function(_)
|
|
210 window.hide_find_panel()
|
|
211 end
|
|
212 }
|
37
|
213 }
|
|
214 }
|
|
215 }
|
|
216 }
|
70
|
217 function window.show_find_panel()
|
|
218 find_panel.is_visible = true
|
87
|
219 find_field.text = find_text
|
70
|
220 find_field.request_focus_in_window()
|
|
221 find_field.select_all()
|
|
222 end
|
|
223 function window.hide_find_panel()
|
|
224 find_panel.is_visible = false
|
|
225 text_area.clear_hightlights()
|
37
|
226 end
|
|
227 function window.find_case_insensitive(_)
|
70
|
228 find_panel.is_visible = true
|
37
|
229 regex_check_box.is_selected = true
|
|
230 find_field.text = "(?i)\Q\E"
|
|
231 find_field.set_selection(7)
|
|
232 find_field.request_focus_in_window()
|
|
233 status_bar.text = [[Put search text between "\Q" and "\E"]]
|
|
234 end
|
|
235 function window.tabs_to_spaces(_)
|
70
|
236 find_panel.is_visible = true
|
37
|
237 regex_check_box.is_selected = true
|
|
238 find_field.text = [[(?m)^(\t*)\t]]
|
|
239 local spaces = repeated( " ", text_area.tab_size )
|
|
240 replace_field.text = "$1"..spaces
|
|
241 status_bar.text = [[Do "Replace All" until 0 replacements]]
|
|
242 end
|
|
243 function window.spaces_to_tabs(_)
|
70
|
244 find_panel.is_visible = true
|
37
|
245 regex_check_box.is_selected = true
|
|
246 local tab_size = text_area.tab_size
|
|
247 find_field.text = `%>(?m)^(( {<%=tab_size%>})*) {<%=tab_size%>}<%`
|
|
248 replace_field.text = "$1\t"
|
|
249 status_bar.text = [[Do "Replace All" until 0 replacements]]
|
|
250 end
|
|
251 return find_panel
|
|
252 end
|
|
253
|
|
254 return make_find_panel
|