Mercurial Hosting > editor
comparison editor.luan @ 15:93e46dadb694
add indent
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Sun, 06 Apr 2025 16:34:58 -0600 |
| parents | 357fdbf446cb |
| children | 90abee9e07d5 |
comparison
equal
deleted
inserted
replaced
| 14:357fdbf446cb | 15:93e46dadb694 |
|---|---|
| 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 stringify = Luan.stringify or error() | |
| 5 local String = require "luan:String.luan" | |
| 6 local sub_string = String.sub or error() | |
| 7 local replace = String.replace or error() | |
| 8 local starts_with = String.starts_with or error() | |
| 4 local Io = require "luan:Io.luan" | 9 local Io = require "luan:Io.luan" |
| 5 local print = Io.print or error() | 10 local print = Io.print or error() |
| 6 local new_file = Io.schemes.file or error() | 11 local new_file = Io.schemes.file or error() |
| 7 local Math = require "luan:Math.luan" | 12 local Math = require "luan:Math.luan" |
| 8 local min = Math.min or error() | 13 local min = Math.min or error() |
| 112 paste.add_action_listener(window.text_area.paste) | 117 paste.add_action_listener(window.text_area.paste) |
| 113 edit_menu.add(paste) | 118 edit_menu.add(paste) |
| 114 end | 119 end |
| 115 edit_menu.add_separator() | 120 edit_menu.add_separator() |
| 116 do | 121 do |
| 122 local indent = new_menu_item() | |
| 123 indent.text = "Indent" | |
| 124 indent.accelerator = "meta CLOSE_BRACKET" | |
| 125 indent.add_action_listener(window.indent) | |
| 126 edit_menu.add(indent) | |
| 127 end | |
| 128 do | |
| 129 local unindent = new_menu_item() | |
| 130 unindent.text = "Unindent" | |
| 131 unindent.accelerator = "meta OPEN_BRACKET" | |
| 132 unindent.add_action_listener(window.unindent) | |
| 133 edit_menu.add(unindent) | |
| 134 end | |
| 135 edit_menu.add_separator() | |
| 136 do | |
| 117 local select_all = new_menu_item() | 137 local select_all = new_menu_item() |
| 118 select_all.text = "Select All" | 138 select_all.text = "Select All" |
| 119 select_all.accelerator = "meta A" | 139 select_all.accelerator = "meta A" |
| 120 select_all.add_action_listener(window.text_area.select_all) | 140 select_all.add_action_listener(window.text_area.select_all) |
| 121 edit_menu.add(select_all) | 141 edit_menu.add(select_all) |
| 188 text_area.columns = 20 | 208 text_area.columns = 20 |
| 189 text_area.wrap_style_word = true | 209 text_area.wrap_style_word = true |
| 190 text_area.line_wrap = true | 210 text_area.line_wrap = true |
| 191 text_area.tab_size = 4 | 211 text_area.tab_size = 4 |
| 192 text_area.set_font{ family="Monospaced", size=13 } | 212 text_area.set_font{ family="Monospaced", size=13 } |
| 193 text_area.caret_position = 0 | 213 text_area.set_selection(0) |
| 194 --print(text_area.line_count) | 214 --print(text_area.line_count) |
| 195 local scroll_pane = new_scroll_pane(text_area) | 215 local scroll_pane = new_scroll_pane(text_area) |
| 196 local line_numbers = new_text_area_line_numbers(text_area) | 216 local line_numbers = new_text_area_line_numbers(text_area) |
| 197 line_numbers.foreground_color = int_to_color(0x888888) | 217 line_numbers.foreground_color = int_to_color(0x888888) |
| 198 line_numbers.border = create_empty_border(0,8,0,8) | 218 line_numbers.border = create_empty_border(0,8,0,8) |
| 224 file.write_text(text_area.text) | 244 file.write_text(text_area.text) |
| 225 text_area.document.set_unedited() | 245 text_area.document.set_unedited() |
| 226 return true | 246 return true |
| 227 end | 247 end |
| 228 function window.revert() | 248 function window.revert() |
| 229 local caret_position = text_area.caret_position | 249 local selection = text_area.get_selection() |
| 230 local text = file.read_text() | 250 local text = file.read_text() |
| 231 text_area.text = text | 251 text_area.text = text |
| 232 text_area.caret_position = min(caret_position,#text) | 252 text_area.set_selection(min(selection,#text+1)) |
| 233 text_area.document.set_unedited() | 253 text_area.document.set_unedited() |
| 254 end | |
| 255 local function selection_lines() | |
| 256 local start_seletion, end_selection = text_area.get_selection() | |
| 257 local end_ = end_selection == start_seletion and end_selection or end_selection - 1 | |
| 258 local start_line = text_area.get_line_from_position(start_seletion) | |
| 259 local end_line = text_area.get_line_from_position(end_) | |
| 260 local start_pos = text_area.get_line_start_position(start_line) | |
| 261 local end_pos = text_area.get_line_end_position(end_line) | |
| 262 local text = text_area.text | |
| 263 text = sub_string(text,start_pos,end_pos-2) | |
| 264 return { | |
| 265 text = text | |
| 266 start_pos = start_pos | |
| 267 length = #text | |
| 268 lines = end_line - start_line + 1 | |
| 269 start_seletion = start_seletion | |
| 270 end_selection = end_selection | |
| 271 } | |
| 272 end | |
| 273 function window.indent() | |
| 274 local r = selection_lines() | |
| 275 local text = r.text | |
| 276 local start_pos = r.start_pos | |
| 277 text = "\t"..replace(text,"\n","\n\t") | |
| 278 text_area.replace(start_pos,r.length,text) | |
| 279 --logger.info(stringify{text_area.get_selection()}) | |
| 280 text_area.set_selection( r.start_seletion+1, r.end_selection+r.lines ) | |
| 281 end | |
| 282 function window.unindent() | |
| 283 local r = selection_lines() | |
| 284 local text = r.text | |
| 285 text = "\n"..text | |
| 286 local start_seletion = r.start_seletion | |
| 287 if starts_with(text,"\n\t") then | |
| 288 start_seletion = start_seletion - 1 | |
| 289 end | |
| 290 local len1 = #text | |
| 291 text = replace(text,"\n\t","\n") | |
| 292 local len2 = #text | |
| 293 local end_selection = r.end_selection - (len1 - len2) | |
| 294 text = sub_string(text,2) | |
| 295 text_area.replace(r.start_pos,r.length,text) | |
| 296 text_area.set_selection(start_seletion,end_selection) | |
| 234 end | 297 end |
| 235 local menu_bar = make_menu_bar(window) | 298 local menu_bar = make_menu_bar(window) |
| 236 frame.set_menu_bar(menu_bar) | 299 frame.set_menu_bar(menu_bar) |
| 237 frame.pack() | 300 frame.pack() |
| 238 frame.visible = true | 301 frame.visible = true |
