Mercurial Hosting > editor
annotate src/luan_editor/window.luan @ 52:3f4c3722b78b
.luan_editor dir
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 24 May 2025 16:25:55 -0600 |
parents | c17c5312e8de |
children | d5681da8ece8 |
rev | line source |
---|---|
37 | 1 local Luan = require "luan:Luan.luan" |
2 local error = Luan.error | |
3 local stringify = Luan.stringify or error() | |
52 | 4 local Parsers = require "luan:Parsers.luan" |
5 local json_string = Parsers.json_string or error() | |
6 local json_parse = Parsers.json_parse or error() | |
37 | 7 local Math = require "luan:Math.luan" |
8 local min = Math.min or error() | |
9 local String = require "luan:String.luan" | |
10 local sub_string = String.sub or error() | |
11 local replace = String.replace or error() | |
12 local starts_with = String.starts_with or error() | |
13 local Io = require "luan:Io.luan" | |
14 local new_text_area = require("luan:swing/Text_area.luan").new or error() | |
15 local new_frame = require("luan:swing/Frame.luan").new or error() | |
41 | 16 local new_dialog = require("luan:swing/Dialog.luan").new or error() |
37 | 17 local new_panel = require("luan:swing/Component.luan").new_panel or error() |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
18 local new_list = require("luan:swing/List.luan").new or error() |
37 | 19 local Layout = require "luan:swing/Layout.luan" |
20 local new_mig_layout = Layout.new_mig_layout or error() | |
21 local new_scroll_pane = require("luan:swing/Scroll_pane.luan").new or error() | |
22 local new_text_area_line_numbers = require("luan:swing/Text_area_line_numbers.luan").new or error() | |
23 local int_to_color = require("luan:swing/Color.luan").int_to_color or error() | |
24 local Border = require "luan:swing/Border.luan" | |
25 local create_empty_border = Border.create_empty_border or error() | |
26 local new_label = require("luan:swing/Label.luan").new or error() | |
27 local make_find_panel = require "classpath:luan_editor/find.luan" | |
28 local add_menu_bar = require "classpath:luan_editor/menu.luan" | |
29 local Swing = require "luan:swing/Swing.luan" | |
44 | 30 local run_later = Swing.run_later or error() |
37 | 31 local File_chooser = require "luan:swing/File_chooser.luan" |
32 local choose_file = File_chooser.awt_choose_file or error() | |
39 | 33 local Option_pane = require "luan:swing/Option_pane.luan" |
34 local show_message_dialog = Option_pane.show_message_dialog or error() | |
37 | 35 local Logging = require "luan:logging/Logging.luan" |
36 local logger = Logging.logger "editor/window" | |
37 | |
38 | |
39 local n_windows = 0 | |
40 local documents = {} | |
41 | |
42 local function bool(val,default) | |
43 if val ~= nil then | |
44 return val | |
45 else | |
46 return default | |
47 end | |
48 end | |
49 | |
52 | 50 local config_file = Io.uri("file:"..Swing.home_dir.."/.luan_editor/config.json") |
37 | 51 local config = {} |
52 if config_file.exists() then | |
53 try | |
52 | 54 config = json_parse(config_file.read_text()) |
37 | 55 catch e |
56 logger.error(e) | |
57 end | |
58 end | |
59 config.size = config.size or { width=700, height=700 } | |
60 config.line_wrap = bool( config.line_wrap, true ) | |
61 config.whitespace_visible = bool( config.whitespace_visible, false ) | |
62 config.tab_size = config.tab_size or 4 | |
46 | 63 config.list_window = config.list_window or {} |
64 config.list_window.size = config.list_window.size or { width=200, height=400 } | |
37 | 65 |
66 local function save_config() | |
52 | 67 config_file.write_text( json_string(config).."\n" ) |
37 | 68 end |
69 | |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
70 local list_view = new_list{ |
41 | 71 --layout = new_mig_layout("insets 0,wrap,fill") |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
72 --layout = new_mig_layout("wrap","[grow]") |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
73 horizontal_alignment = "right" |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
74 hover_background = int_to_color(0xEEEEEE) |
41 | 75 } |
76 local list_scroll_pane = new_scroll_pane{ | |
77 view = list_view | |
78 } | |
79 local list_window = new_dialog{ | |
46 | 80 size = config.list_window.size |
41 | 81 content_pane = list_scroll_pane |
43 | 82 focusable_window_state = false |
41 | 83 } |
46 | 84 list_window.add_resize_stopped_listener( 200, function() |
85 --logger.info(stringify(list_window.size)) | |
86 config.list_window.size = list_window.size | |
87 save_config() | |
88 end) | |
89 list_window.add_move_stopped_listener( 200, function() | |
90 --logger.info(stringify(list_window.location)) | |
91 config.list_window.location = list_window.location | |
92 save_config() | |
93 end) | |
44 | 94 local function show_list_window() |
46 | 95 local location = config.list_window.location |
96 if location ~= nil then | |
97 list_window.location = location | |
98 end | |
44 | 99 list_window.visible = true |
100 list_scroll_pane.scroll_to_right() | |
101 end | |
102 | |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
103 local function add_list_window_item(item) |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
104 list_view.add_element(item) |
41 | 105 list_scroll_pane.scroll_to_right() |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
106 end |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
107 local function remove_list_window_item(item) |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
108 list_view.remove_element(item) --or error() |
41 | 109 end |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
110 list_view.add_list_selection_listener( function(item) |
49 | 111 if item ~= nil then |
112 item.window.text_area.request_focus() | |
113 end | |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
114 end ) |
41 | 115 |
44 | 116 local black = int_to_color(0x000000) |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
117 local dark_blue = int_to_color(0x0000C0) |
44 | 118 local grey = int_to_color(0x888888) |
119 | |
38 | 120 local function new_window(file,document) |
37 | 121 local window = {} |
39 | 122 if file == nil or not file.exists() then |
123 window.has_file = false | |
124 elseif file.is_file() then | |
125 window.has_file = true | |
126 else | |
127 show_message_dialog(nil,"Not a file") | |
128 if n_windows == 0 then | |
129 Luan.exit() | |
130 else | |
131 return | |
132 end | |
133 end | |
134 window.has_file = file~=nil and file.exists() | |
37 | 135 local text_area = new_text_area{ |
136 wrap_style_word = true | |
137 line_wrap = config.line_wrap | |
138 whitespace_visible = config.whitespace_visible | |
139 tab_size = config.tab_size | |
140 font = { family="Monospaced", size=13 } | |
141 } | |
142 window.text_area = text_area | |
143 local title = file and file.canonical().to_string() or "new" | |
38 | 144 if document ~= nil then |
145 text_area.document = document | |
146 elseif file ~= nil then | |
37 | 147 local document = documents[title] |
39 | 148 if document ~= nil then |
37 | 149 text_area.document = document |
39 | 150 else |
151 documents[title] = text_area.document | |
152 if file.exists() then | |
153 text_area.text = file.read_text() | |
154 text_area.document.clear_unedited() | |
155 end | |
37 | 156 end |
157 end | |
158 text_area.set_selection(0) | |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
159 local list_window_item = { |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
160 window = window |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
161 } |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
162 add_list_window_item(list_window_item) |
37 | 163 local status_bar = new_label{ |
164 constraints = "span,growx" | |
165 text = " " | |
166 border = create_empty_border(2,16,4,16) | |
167 } | |
168 window.status_bar = status_bar | |
169 local find_panel = make_find_panel(window) | |
170 local frame = new_frame{ | |
171 preferred_size = config.size | |
172 content_pane = new_panel{ | |
173 layout = new_mig_layout("insets 0,wrap,fill,hidemode 3","","[][grow 0]") | |
174 children = { | |
175 new_scroll_pane{ | |
176 constraints = "grow" | |
177 view = text_area | |
178 row_header_view = new_text_area_line_numbers{ | |
179 text_area = text_area | |
44 | 180 foreground_color = grey |
37 | 181 border = create_empty_border(0,8,0,8) |
182 } | |
183 } | |
184 find_panel | |
185 status_bar | |
186 } | |
187 } | |
188 } | |
189 window.frame = frame | |
190 frame.add_close_listener(function() | |
191 n_windows = n_windows - 1 | |
192 if n_windows == 0 then | |
193 Luan.exit() | |
194 end | |
44 | 195 remove_list_window_item(list_window_item) |
37 | 196 end) |
43 | 197 frame.add_window_focus_listener(function() |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
198 list_view.set_selected_value(list_window_item) |
43 | 199 end) |
37 | 200 frame.add_resize_stopped_listener( 200, function() |
201 --logger.info(stringify(frame.size)) | |
202 config.size = frame.size | |
203 save_config() | |
204 end) | |
205 frame.add_move_stopped_listener( 200, function() | |
206 --logger.info(stringify(frame.location)) | |
207 config.location = frame.location | |
208 save_config() | |
209 end) | |
44 | 210 local function undo_listener() |
211 local is_unedited = text_area.document.is_unedited() | |
212 if window.is_unedited == is_unedited then | |
213 return | |
214 end | |
215 window.is_unedited = is_unedited | |
37 | 216 local s = title |
44 | 217 if not is_unedited then |
37 | 218 s = s.." *" |
219 end | |
220 frame.title = s | |
48 | 221 list_window_item.text = title |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
222 list_window_item.foreground_color = is_unedited and black or dark_blue |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
223 list_view.repaint(list_window_item) |
37 | 224 end |
44 | 225 undo_listener() |
226 --window.undo_listener = undo_listener -- dont gc | |
227 text_area.document.add_undo_listener(undo_listener) | |
37 | 228 window.new = new_window |
48 | 229 function window.title() |
230 return title | |
231 end | |
37 | 232 function window.open() |
233 local new_file = choose_file{ | |
234 action = "load" | |
235 parent = frame | |
236 directory = file and file.parent() | |
237 } | |
238 if new_file ~= nil then | |
239 new_window(new_file) | |
240 end | |
241 end | |
242 function window.save() | |
243 if file == nil then | |
244 file = choose_file{ | |
245 action = "save" | |
246 parent = frame | |
247 } | |
248 if file == nil then | |
249 return false | |
250 end | |
251 title = file.canonical().to_string() | |
252 documents[title] = text_area.document | |
48 | 253 window.is_unedited = nil |
254 undo_listener() | |
37 | 255 end |
39 | 256 try |
257 file.write_text(text_area.text) | |
258 catch e | |
259 show_message_dialog( frame, e.get_message() ) | |
260 return false | |
261 end | |
37 | 262 text_area.document.set_unedited() |
263 return true | |
264 end | |
265 function window.revert() | |
266 local selection = text_area.get_selection() | |
267 local text = file.read_text() | |
268 text_area.text = text | |
269 text_area.set_selection(min(selection,#text+1)) | |
270 text_area.document.set_unedited() | |
271 status_bar.text = "Reverted" | |
272 end | |
273 local function selection_lines() | |
274 local start_seletion, end_selection = text_area.get_selection() | |
275 local end_ = end_selection == start_seletion and end_selection or end_selection - 1 | |
276 local start_line = text_area.get_line_from_position(start_seletion) | |
277 local end_line = text_area.get_line_from_position(end_) | |
278 local start_pos = text_area.get_line_start_position(start_line) | |
279 local end_pos = text_area.get_line_end_position(end_line) | |
280 local text = text_area.text | |
281 text = sub_string(text,start_pos,end_pos-2) | |
282 return { | |
283 text = text | |
284 start_pos = start_pos | |
285 length = #text | |
286 lines = end_line - start_line + 1 | |
287 start_seletion = start_seletion | |
288 end_selection = end_selection | |
289 } | |
290 end | |
291 function window.indent() | |
292 local r = selection_lines() | |
293 local text = r.text | |
294 local start_pos = r.start_pos | |
295 text = "\t"..replace(text,"\n","\n\t") | |
296 text_area.replace(start_pos,r.length,text) | |
297 --logger.info(stringify{text_area.get_selection()}) | |
298 text_area.set_selection( r.start_seletion+1, r.end_selection+r.lines ) | |
299 end | |
300 function window.unindent() | |
301 local r = selection_lines() | |
302 local text = r.text | |
303 text = "\n"..text | |
304 local start_seletion = r.start_seletion | |
305 if starts_with(text,"\n\t") then | |
306 start_seletion = start_seletion - 1 | |
307 end | |
308 local len1 = #text | |
309 text = replace(text,"\n\t","\n") | |
310 local len2 = #text | |
311 local end_selection = r.end_selection - (len1 - len2) | |
312 text = sub_string(text,2) | |
313 text_area.replace(r.start_pos,r.length,text) | |
314 text_area.set_selection(start_seletion,end_selection) | |
315 end | |
316 function window.cursor_column() | |
317 local cursor_pos = text_area.get_selection() | |
318 local line = text_area.get_line_from_position(cursor_pos) | |
319 local start_line_pos = text_area.get_line_start_position(line) | |
320 return cursor_pos - start_line_pos + 1 | |
321 end | |
322 function window.goto(line) | |
323 local pos = text_area.get_line_start_position(line) | |
324 text_area.set_selection(pos) | |
325 end | |
326 function window.set_line_wrap(line_wrap) | |
327 text_area.line_wrap = line_wrap | |
328 config.line_wrap = line_wrap | |
329 save_config() | |
330 end | |
331 function window.set_whitespace_visible(whitespace_visible) | |
332 text_area.whitespace_visible = whitespace_visible | |
333 config.whitespace_visible = whitespace_visible | |
334 save_config() | |
335 end | |
336 function window.set_tab_size(tab_size) | |
337 text_area.tab_size = tab_size | |
338 config.tab_size = tab_size | |
339 save_config() | |
340 end | |
38 | 341 function window.duplicate() |
342 local new = new_window(file,text_area.document) | |
343 new.text_area.set_selection( text_area.get_selection() ) | |
344 end | |
44 | 345 window.show_list_window = show_list_window |
37 | 346 add_menu_bar(window) |
347 frame.pack() | |
46 | 348 local location = config.location |
349 if location ~= nil then | |
350 frame.location = location | |
37 | 351 end |
352 frame.visible = true | |
353 text_area.request_focus_in_window() | |
354 n_windows = n_windows + 1 | |
38 | 355 return window |
37 | 356 end |
357 | |
358 return new_window |