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