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