Mercurial Hosting > editor
comparison src/luan_editor/window.luan @ 37:b7ff52d45b9a
copy from luan
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Mon, 21 Apr 2025 13:07:29 -0600 |
| parents | |
| children | 8c90e0077cfe |
comparison
equal
deleted
inserted
replaced
| 36:0a8865de3d53 | 37:b7ff52d45b9a |
|---|---|
| 1 local Luan = require "luan:Luan.luan" | |
| 2 local error = Luan.error | |
| 3 local stringify = Luan.stringify or error() | |
| 4 local Math = require "luan:Math.luan" | |
| 5 local min = Math.min or error() | |
| 6 local String = require "luan:String.luan" | |
| 7 local sub_string = String.sub or error() | |
| 8 local replace = String.replace or error() | |
| 9 local starts_with = String.starts_with or error() | |
| 10 local Io = require "luan:Io.luan" | |
| 11 local new_text_area = require("luan:swing/Text_area.luan").new or error() | |
| 12 local new_frame = require("luan:swing/Frame.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_scroll_pane = require("luan:swing/Scroll_pane.luan").new or error() | |
| 17 local new_text_area_line_numbers = require("luan:swing/Text_area_line_numbers.luan").new or error() | |
| 18 local int_to_color = require("luan:swing/Color.luan").int_to_color or error() | |
| 19 local Border = require "luan:swing/Border.luan" | |
| 20 local create_empty_border = Border.create_empty_border or error() | |
| 21 local new_label = require("luan:swing/Label.luan").new or error() | |
| 22 local make_find_panel = require "classpath:luan_editor/find.luan" | |
| 23 local add_menu_bar = require "classpath:luan_editor/menu.luan" | |
| 24 local Swing = require "luan:swing/Swing.luan" | |
| 25 local File_chooser = require "luan:swing/File_chooser.luan" | |
| 26 local choose_file = File_chooser.awt_choose_file or error() | |
| 27 local Logging = require "luan:logging/Logging.luan" | |
| 28 local logger = Logging.logger "editor/window" | |
| 29 | |
| 30 | |
| 31 local n_windows = 0 | |
| 32 local documents = {} | |
| 33 | |
| 34 local function bool(val,default) | |
| 35 if val ~= nil then | |
| 36 return val | |
| 37 else | |
| 38 return default | |
| 39 end | |
| 40 end | |
| 41 | |
| 42 local config_file = Io.uri("file:"..Swing.home_dir.."/.luan_editor") | |
| 43 local config = {} | |
| 44 if config_file.exists() then | |
| 45 try | |
| 46 config = Luan.parse(config_file.read_text()) | |
| 47 catch e | |
| 48 logger.error(e) | |
| 49 end | |
| 50 end | |
| 51 config.size = config.size or { width=700, height=700 } | |
| 52 config.line_wrap = bool( config.line_wrap, true ) | |
| 53 config.whitespace_visible = bool( config.whitespace_visible, false ) | |
| 54 config.tab_size = config.tab_size or 4 | |
| 55 | |
| 56 local function save_config() | |
| 57 config_file.write_text( stringify(config).."\n" ) | |
| 58 end | |
| 59 | |
| 60 local function new_window(file) | |
| 61 local window = {} | |
| 62 window.has_file = file~=nil and file.is_file() | |
| 63 local text_area = new_text_area{ | |
| 64 wrap_style_word = true | |
| 65 line_wrap = config.line_wrap | |
| 66 whitespace_visible = config.whitespace_visible | |
| 67 tab_size = config.tab_size | |
| 68 font = { family="Monospaced", size=13 } | |
| 69 } | |
| 70 window.text_area = text_area | |
| 71 local title = file and file.canonical().to_string() or "new" | |
| 72 if file ~= nil then | |
| 73 local document = documents[title] | |
| 74 if document == nil then | |
| 75 documents[title] = text_area.document | |
| 76 else | |
| 77 text_area.document = document | |
| 78 end | |
| 79 if file.is_file() then | |
| 80 text_area.text = file.read_text() | |
| 81 text_area.document.clear_unedited() | |
| 82 end | |
| 83 end | |
| 84 text_area.set_selection(0) | |
| 85 local status_bar = new_label{ | |
| 86 constraints = "span,growx" | |
| 87 text = " " | |
| 88 border = create_empty_border(2,16,4,16) | |
| 89 } | |
| 90 window.status_bar = status_bar | |
| 91 local find_panel = make_find_panel(window) | |
| 92 local frame = new_frame{ | |
| 93 preferred_size = config.size | |
| 94 content_pane = new_panel{ | |
| 95 layout = new_mig_layout("insets 0,wrap,fill,hidemode 3","","[][grow 0]") | |
| 96 children = { | |
| 97 new_scroll_pane{ | |
| 98 constraints = "grow" | |
| 99 view = text_area | |
| 100 row_header_view = new_text_area_line_numbers{ | |
| 101 text_area = text_area | |
| 102 foreground_color = int_to_color(0x888888) | |
| 103 border = create_empty_border(0,8,0,8) | |
| 104 } | |
| 105 } | |
| 106 find_panel | |
| 107 status_bar | |
| 108 } | |
| 109 } | |
| 110 } | |
| 111 window.frame = frame | |
| 112 frame.add_close_listener(function() | |
| 113 n_windows = n_windows - 1 | |
| 114 if n_windows == 0 then | |
| 115 Luan.exit() | |
| 116 end | |
| 117 end) | |
| 118 frame.add_resize_stopped_listener( 200, function() | |
| 119 --logger.info(stringify(frame.size)) | |
| 120 config.size = frame.size | |
| 121 save_config() | |
| 122 end) | |
| 123 frame.add_move_stopped_listener( 200, function() | |
| 124 --logger.info(stringify(frame.location)) | |
| 125 config.location = frame.location | |
| 126 save_config() | |
| 127 end) | |
| 128 local function set_title() | |
| 129 local s = title | |
| 130 if not text_area.document.is_unedited() then | |
| 131 s = s.." *" | |
| 132 end | |
| 133 frame.title = s | |
| 134 end | |
| 135 set_title() | |
| 136 window.set_title = set_title -- dont gc | |
| 137 text_area.document.add_undo_listener(set_title) | |
| 138 window.new = new_window | |
| 139 function window.open() | |
| 140 local new_file = choose_file{ | |
| 141 action = "load" | |
| 142 parent = frame | |
| 143 directory = file and file.parent() | |
| 144 } | |
| 145 if new_file ~= nil then | |
| 146 new_window(new_file) | |
| 147 end | |
| 148 end | |
| 149 function window.save() | |
| 150 if file == nil then | |
| 151 file = choose_file{ | |
| 152 action = "save" | |
| 153 parent = frame | |
| 154 } | |
| 155 if file == nil then | |
| 156 return false | |
| 157 end | |
| 158 title = file.canonical().to_string() | |
| 159 frame.title = title | |
| 160 documents[title] = text_area.document | |
| 161 end | |
| 162 file.write_text(text_area.text) | |
| 163 text_area.document.set_unedited() | |
| 164 return true | |
| 165 end | |
| 166 function window.revert() | |
| 167 local selection = text_area.get_selection() | |
| 168 local text = file.read_text() | |
| 169 text_area.text = text | |
| 170 text_area.set_selection(min(selection,#text+1)) | |
| 171 text_area.document.set_unedited() | |
| 172 status_bar.text = "Reverted" | |
| 173 end | |
| 174 local function selection_lines() | |
| 175 local start_seletion, end_selection = text_area.get_selection() | |
| 176 local end_ = end_selection == start_seletion and end_selection or end_selection - 1 | |
| 177 local start_line = text_area.get_line_from_position(start_seletion) | |
| 178 local end_line = text_area.get_line_from_position(end_) | |
| 179 local start_pos = text_area.get_line_start_position(start_line) | |
| 180 local end_pos = text_area.get_line_end_position(end_line) | |
| 181 local text = text_area.text | |
| 182 text = sub_string(text,start_pos,end_pos-2) | |
| 183 return { | |
| 184 text = text | |
| 185 start_pos = start_pos | |
| 186 length = #text | |
| 187 lines = end_line - start_line + 1 | |
| 188 start_seletion = start_seletion | |
| 189 end_selection = end_selection | |
| 190 } | |
| 191 end | |
| 192 function window.indent() | |
| 193 local r = selection_lines() | |
| 194 local text = r.text | |
| 195 local start_pos = r.start_pos | |
| 196 text = "\t"..replace(text,"\n","\n\t") | |
| 197 text_area.replace(start_pos,r.length,text) | |
| 198 --logger.info(stringify{text_area.get_selection()}) | |
| 199 text_area.set_selection( r.start_seletion+1, r.end_selection+r.lines ) | |
| 200 end | |
| 201 function window.unindent() | |
| 202 local r = selection_lines() | |
| 203 local text = r.text | |
| 204 text = "\n"..text | |
| 205 local start_seletion = r.start_seletion | |
| 206 if starts_with(text,"\n\t") then | |
| 207 start_seletion = start_seletion - 1 | |
| 208 end | |
| 209 local len1 = #text | |
| 210 text = replace(text,"\n\t","\n") | |
| 211 local len2 = #text | |
| 212 local end_selection = r.end_selection - (len1 - len2) | |
| 213 text = sub_string(text,2) | |
| 214 text_area.replace(r.start_pos,r.length,text) | |
| 215 text_area.set_selection(start_seletion,end_selection) | |
| 216 end | |
| 217 function window.cursor_column() | |
| 218 local cursor_pos = text_area.get_selection() | |
| 219 local line = text_area.get_line_from_position(cursor_pos) | |
| 220 local start_line_pos = text_area.get_line_start_position(line) | |
| 221 return cursor_pos - start_line_pos + 1 | |
| 222 end | |
| 223 function window.goto(line) | |
| 224 local pos = text_area.get_line_start_position(line) | |
| 225 text_area.set_selection(pos) | |
| 226 end | |
| 227 function window.set_line_wrap(line_wrap) | |
| 228 text_area.line_wrap = line_wrap | |
| 229 config.line_wrap = line_wrap | |
| 230 save_config() | |
| 231 end | |
| 232 function window.set_whitespace_visible(whitespace_visible) | |
| 233 text_area.whitespace_visible = whitespace_visible | |
| 234 config.whitespace_visible = whitespace_visible | |
| 235 save_config() | |
| 236 end | |
| 237 function window.set_tab_size(tab_size) | |
| 238 text_area.tab_size = tab_size | |
| 239 config.tab_size = tab_size | |
| 240 save_config() | |
| 241 end | |
| 242 add_menu_bar(window) | |
| 243 frame.pack() | |
| 244 if config.location ~= nil then | |
| 245 frame.location = config.location | |
| 246 end | |
| 247 frame.visible = true | |
| 248 text_area.request_focus_in_window() | |
| 249 n_windows = n_windows + 1 | |
| 250 end | |
| 251 | |
| 252 return new_window |
