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