Mercurial Hosting > editor
comparison editor.luan @ 34:8535209b61ff
replace
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Mon, 14 Apr 2025 14:21:36 -0600 |
| parents | e19ac0784287 |
| children | c5b57654147b |
comparison
equal
deleted
inserted
replaced
| 33:e19ac0784287 | 34:8535209b61ff |
|---|---|
| 9 local starts_with = String.starts_with or error() | 9 local starts_with = String.starts_with or error() |
| 10 local to_number = String.to_number or error() | 10 local to_number = String.to_number or error() |
| 11 local find = String.find or error() | 11 local find = String.find or error() |
| 12 local regex = String.regex or error() | 12 local regex = String.regex or error() |
| 13 local regex_quote = String.regex_quote or error() | 13 local regex_quote = String.regex_quote or error() |
| 14 local regex_quote_replacement = String.regex_quote_replacement or error() | |
| 14 local Io = require "luan:Io.luan" | 15 local Io = require "luan:Io.luan" |
| 15 local print = Io.print or error() | 16 local print = Io.print or error() |
| 16 local new_file = Io.schemes.file or error() | 17 local new_file = Io.schemes.file or error() |
| 17 local Math = require "luan:Math.luan" | 18 local Math = require "luan:Math.luan" |
| 18 local min = Math.min or error() | 19 local min = Math.min or error() |
| 229 return matches | 230 return matches |
| 230 end | 231 end |
| 231 | 232 |
| 232 local function make_find_panel(window) | 233 local function make_find_panel(window) |
| 233 local text_area = window.text_area | 234 local text_area = window.text_area |
| 234 local find_field, regex_check_box, output | 235 local find_field, replace_field, regex_check_box, output |
| 235 local function find_match(event) | 236 local function find_match(event) |
| 236 --logger.info("action "..event.action) | 237 --logger.info("action "..event.action) |
| 237 local s = find_field.text | 238 local s = find_field.text |
| 238 if #s == 0 then | 239 if #s == 0 then |
| 239 output.text = "" | 240 output.text = "" |
| 282 output.text = n_matches.." of "..n_matches.." matches; wrapped past end" | 283 output.text = n_matches.." of "..n_matches.." matches; wrapped past end" |
| 283 else | 284 else |
| 284 error(action) | 285 error(action) |
| 285 end | 286 end |
| 286 end | 287 end |
| 288 local function replace_match(_) | |
| 289 local find = find_field.text | |
| 290 if #find == 0 then | |
| 291 output.text = "" | |
| 292 return | |
| 293 end | |
| 294 local replace = replace_field.text | |
| 295 if not regex_check_box.is_selected then | |
| 296 find = regex_quote(find) | |
| 297 replace = regex_quote_replacement(replace) | |
| 298 end | |
| 299 local r | |
| 300 try | |
| 301 r = regex(find) | |
| 302 catch e | |
| 303 output.text = "Regex error: "..e.get_message() | |
| 304 return | |
| 305 end | |
| 306 local selection = text_area.selected_text | |
| 307 local new, n = r.gsub(selection,replace) | |
| 308 if n > 0 then | |
| 309 text_area.selected_text = new | |
| 310 end | |
| 311 output.text = n.." replacements" | |
| 312 end | |
| 287 find_field = new_text_field{ | 313 find_field = new_text_field{ |
| 288 constraints = "growx" | 314 constraints = "growx" |
| 289 show_whitespace = true | 315 show_whitespace = true |
| 290 action = "next" | 316 action = "next" |
| 291 action_listener = find_match | 317 action_listener = find_match |
| 318 } | |
| 319 replace_field = new_text_field{ | |
| 320 constraints = "growx" | |
| 321 show_whitespace = true | |
| 322 action_listener = replace_match | |
| 292 } | 323 } |
| 293 regex_check_box = new_check_box{ | 324 regex_check_box = new_check_box{ |
| 294 text = "Use Regex" | 325 text = "Use Regex" |
| 295 } | 326 } |
| 296 output = new_label{ | 327 output = new_label{ |
| 320 } | 351 } |
| 321 new_label{ | 352 new_label{ |
| 322 constraints = "right" | 353 constraints = "right" |
| 323 text = "Replace:" | 354 text = "Replace:" |
| 324 } | 355 } |
| 325 new_text_field{ | 356 replace_field |
| 326 constraints = "growx" | |
| 327 show_whitespace = true | |
| 328 } | |
| 329 new_button{ | 357 new_button{ |
| 330 constraints = "grow" | 358 constraints = "grow" |
| 331 text = "Replace" | 359 text = "Replace" |
| 360 tool_tip_text = "Replace matches in selected text" | |
| 361 action_listener = replace_match | |
| 332 } | 362 } |
| 333 new_button{ | 363 new_button{ |
| 334 constraints = "grow,wrap" | 364 constraints = "grow,wrap" |
| 335 text = "Replace All" | 365 text = "Replace All" |
| 336 } | 366 } |
| 358 window.find_menu_item.is_selected = true | 388 window.find_menu_item.is_selected = true |
| 359 regex_check_box.is_selected = true | 389 regex_check_box.is_selected = true |
| 360 find_field.text = "(?i)\Q\E" | 390 find_field.text = "(?i)\Q\E" |
| 361 find_field.set_selection(7) | 391 find_field.set_selection(7) |
| 362 find_field.request_focus_in_window() | 392 find_field.request_focus_in_window() |
| 393 output.text = "" | |
| 363 end | 394 end |
| 364 return find_panel | 395 return find_panel |
| 365 end | 396 end |
| 366 | 397 |
| 367 local n_windows = 0 | 398 local n_windows = 0 |
