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