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