Mercurial Hosting > editor
comparison editor.luan @ 28:bdb8754f1211
find work
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Fri, 11 Apr 2025 21:41:26 -0600 |
| parents | 32232df78246 |
| children | 01b8a25b38aa |
comparison
equal
deleted
inserted
replaced
| 27:32232df78246 | 28:bdb8754f1211 |
|---|---|
| 1 local Luan = require "luan:Luan.luan" | 1 local Luan = require "luan:Luan.luan" |
| 2 local error = Luan.error | 2 local error = Luan.error |
| 3 local ipairs = Luan.ipairs or error() | 3 local ipairs = Luan.ipairs or error() |
| 4 local range = Luan.range or error() | |
| 4 local stringify = Luan.stringify or error() | 5 local stringify = Luan.stringify or error() |
| 5 local String = require "luan:String.luan" | 6 local String = require "luan:String.luan" |
| 6 local sub_string = String.sub or error() | 7 local sub_string = String.sub or error() |
| 7 local replace = String.replace or error() | 8 local replace = String.replace or error() |
| 8 local starts_with = String.starts_with or error() | 9 local starts_with = String.starts_with or error() |
| 9 local to_number = String.to_number or error() | 10 local to_number = String.to_number or error() |
| 11 local find = String.find or error() | |
| 10 local Io = require "luan:Io.luan" | 12 local Io = require "luan:Io.luan" |
| 11 local print = Io.print or error() | 13 local print = Io.print or error() |
| 12 local new_file = Io.schemes.file or error() | 14 local new_file = Io.schemes.file or error() |
| 13 local Math = require "luan:Math.luan" | 15 local Math = require "luan:Math.luan" |
| 14 local min = Math.min or error() | 16 local min = Math.min or error() |
| 175 new_menu_item{ | 177 new_menu_item{ |
| 176 text = "Goto Line" | 178 text = "Goto Line" |
| 177 accelerator = "meta G" | 179 accelerator = "meta G" |
| 178 action_listener = function(_) | 180 action_listener = function(_) |
| 179 local input = show_input_dialog( window.frame, "Goto line" ) | 181 local input = show_input_dialog( window.frame, "Goto line" ) |
| 180 --logger.info("input "..input) | |
| 181 local line = input and to_number(input) | 182 local line = input and to_number(input) |
| 182 if line ~= nil then | 183 if line ~= nil then |
| 183 window.goto(line) | 184 window.goto(line) |
| 184 end | 185 end |
| 185 end | 186 end |
| 188 } | 189 } |
| 189 } | 190 } |
| 190 } | 191 } |
| 191 end | 192 end |
| 192 | 193 |
| 194 local function get_matches(text,s) | |
| 195 local n = #s | |
| 196 if n == 0 then | |
| 197 return nil | |
| 198 end | |
| 199 local matches = {} | |
| 200 local i = 0 | |
| 201 while(true) do | |
| 202 local j = find(text,s,i) | |
| 203 if j == nil then | |
| 204 break | |
| 205 end | |
| 206 matches[#matches+1] = { start=j, end_=j+n } | |
| 207 i = j + n | |
| 208 end | |
| 209 return matches | |
| 210 end | |
| 211 | |
| 193 local function make_find_dialog(window) | 212 local function make_find_dialog(window) |
| 213 local text_area = window.text_area | |
| 214 local find_field, output | |
| 215 local function find_match(event) | |
| 216 --logger.info("action "..event.action) | |
| 217 local s = find_field.text | |
| 218 local matches = get_matches( text_area.text, s ) | |
| 219 if matches == nil then | |
| 220 output.text = "" | |
| 221 return | |
| 222 end | |
| 223 local n_matches = #matches | |
| 224 if n_matches == 0 then | |
| 225 output.text = "0 matches" | |
| 226 return | |
| 227 end | |
| 228 local action = event.action | |
| 229 if action == "next" then | |
| 230 local _, pos = text_area.get_selection() | |
| 231 for i, match in ipairs(matches) do | |
| 232 if match.start >= pos then | |
| 233 text_area.set_selection( match.start, match.end_ ) | |
| 234 output.text = i.." of "..n_matches.." matches" | |
| 235 return | |
| 236 end | |
| 237 end | |
| 238 local match = matches[1] | |
| 239 text_area.set_selection( match.start, match.end_ ) | |
| 240 output.text = "1 of "..n_matches.." matches; wrapped past end" | |
| 241 elseif action == "previous" then | |
| 242 local pos = text_area.get_selection() | |
| 243 for i in range(n_matches,1,-1) do | |
| 244 local match = matches[i] | |
| 245 if match.end_ <= pos then | |
| 246 text_area.set_selection( match.start, match.end_ ) | |
| 247 output.text = i.." of "..n_matches.." matches" | |
| 248 return | |
| 249 end | |
| 250 end | |
| 251 local match = matches[n_matches] | |
| 252 text_area.set_selection( match.start, match.end_ ) | |
| 253 output.text = n_matches.." of "..n_matches.." matches; wrapped past end" | |
| 254 else | |
| 255 error(action) | |
| 256 end | |
| 257 end | |
| 258 find_field = new_text_field{ | |
| 259 constraints = "wrap,growx" | |
| 260 columns = 20 | |
| 261 action = "next" | |
| 262 action_listener = find_match | |
| 263 } | |
| 264 output = new_label{ | |
| 265 constraints = "span,grow" | |
| 266 text = "testing" | |
| 267 } | |
| 194 local dialog = new_dialog{ | 268 local dialog = new_dialog{ |
| 195 owner_frame = window.frame | 269 owner_frame = window.frame |
| 196 content_pane = new_panel{ | 270 content_pane = new_panel{ |
| 197 layout = new_mig_layout("debug","[][grow]","[][grow,top]") | 271 layout = new_mig_layout("","[][grow]") |
| 198 children = { | 272 children = { |
| 199 new_label{ | 273 new_label{ |
| 200 text = "Find Next:" | 274 constraints = "right" |
| 275 text = "Find:" | |
| 276 } | |
| 277 find_field | |
| 278 new_label{ | |
| 279 constraints = "right" | |
| 280 text = "Replace:" | |
| 201 } | 281 } |
| 202 new_text_field{ | 282 new_text_field{ |
| 203 constraints = "wrap,growx" | 283 constraints = "wrap,growx" |
| 204 columns = 20 | 284 columns = 20 |
| 205 } | 285 } |
| 206 new_panel{ | 286 new_panel{ |
| 207 constraints = "span,wrap" | 287 constraints = "span,wrap" |
| 208 layout = new_mig_layout("debug,insets 0") | 288 layout = new_mig_layout("insets 0") |
| 209 --border = create_empty_border(8,8,8,8) | |
| 210 --border = create_line_border(int_to_color(0)) | |
| 211 children = { | 289 children = { |
| 212 new_button{ | 290 new_button{ |
| 213 text = "Find Next" | 291 text = "Find Next" |
| 292 action = "next" | |
| 293 action_listener = find_match | |
| 214 } | 294 } |
| 215 new_button{ | 295 new_button{ |
| 216 text = "Find Previous" | 296 text = "Find Previous" |
| 297 action = "previous" | |
| 298 action_listener = find_match | |
| 217 } | 299 } |
| 218 } | 300 } |
| 219 } | 301 } |
| 302 output | |
| 220 } | 303 } |
| 221 } | 304 } |
| 222 } | 305 } |
| 223 dialog.pack() | 306 dialog.pack() |
| 224 local was_shown = false | 307 local was_shown = false |
