0
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
3
|
3 local ipairs = Luan.ipairs or error()
|
15
|
4 local stringify = Luan.stringify or error()
|
|
5 local String = require "luan:String.luan"
|
|
6 local sub_string = String.sub or error()
|
|
7 local replace = String.replace or error()
|
|
8 local starts_with = String.starts_with or error()
|
17
|
9 local to_number = String.to_number or error()
|
0
|
10 local Io = require "luan:Io.luan"
|
|
11 local print = Io.print or error()
|
3
|
12 local new_file = Io.schemes.file or error()
|
7
|
13 local Math = require "luan:Math.luan"
|
|
14 local min = Math.min or error()
|
0
|
15 local Swing = require "luan:swing/Swing.luan"
|
|
16 local new_frame = require("luan:swing/Frame.luan").new or error()
|
|
17 local new_label = require("luan:swing/Label.luan").new or error()
|
|
18 local new_text_area = require("luan:swing/Text_area.luan").new or error()
|
|
19 local new_scroll_pane = require("luan:swing/Scroll_pane.luan").new or error()
|
18
|
20 local new_text_area_line_numbers = require("luan:swing/Text_area_line_numbers.luan").new or error()
|
0
|
21 local new_menu_bar = require("luan:swing/Menu_bar.luan").new or error()
|
21
|
22 local Menu = require "luan:swing/Menu.luan"
|
|
23 local new_menu = Menu.new or error()
|
|
24 local separator = Menu.separator or error()
|
0
|
25 local new_menu_item = require("luan:swing/Menu_item.luan").new or error()
|
2
|
26 local new_check_box_menu_item = require("luan:swing/Check_box_menu_item.luan").new or error()
|
12
|
27 local int_to_color = require("luan:swing/Color.luan").int_to_color or error()
|
18
|
28 local Border = require "luan:swing/Border.luan"
|
|
29 local create_empty_border = Border.create_empty_border or error()
|
|
30 local create_line_border = Border.create_line_border or error()
|
|
31 local Layout = require "luan:swing/Layout.luan"
|
|
32 local new_flow_layout = Layout.new_flow_layout or error()
|
|
33 local new_box_layout = Layout.new_box_layout or error()
|
25
|
34 local new_mig_layout = Layout.new_mig_layout or error()
|
16
|
35 local Option_pane = require "luan:swing/Option_pane.luan"
|
|
36 local show_message_dialog = Option_pane.show_message_dialog or error()
|
17
|
37 local show_input_dialog = Option_pane.show_input_dialog or error()
|
18
|
38 local new_dialog = require("luan:swing/Dialog.luan").new or error()
|
|
39 local new_panel = require("luan:swing/Component.luan").new_panel or error()
|
|
40 local new_button = require("luan:swing/Button.luan").new or error()
|
25
|
41 local new_text_field = require("luan:swing/Text_field.luan").new or error()
|
0
|
42 local Logging = require "luan:logging/Logging.luan"
|
|
43 local logger = Logging.logger "editor"
|
|
44
|
|
45
|
|
46 local new_window
|
|
47
|
20
|
48 local function action_listener(fn)
|
|
49 return function(_)
|
|
50 fn()
|
|
51 end
|
|
52 end
|
|
53
|
1
|
54 local function make_menu_bar(window)
|
21
|
55 local document = window.text_area.document
|
|
56 local revert = new_menu_item{
|
|
57 text = "Revert"
|
|
58 enabled = window.has_file
|
|
59 action_listener = action_listener(window.revert)
|
|
60 }
|
|
61 local undo = new_menu_item{
|
|
62 text = "Undo"
|
|
63 accelerator = "meta Z"
|
|
64 action_listener = action_listener(document.undo)
|
|
65 }
|
|
66 local redo = new_menu_item{
|
|
67 text = "Redo"
|
|
68 accelerator = "meta shift Z"
|
|
69 action_listener = action_listener(document.redo)
|
|
70 }
|
|
71 local function update_undo_redo()
|
|
72 undo.set_enabled(document.can_undo())
|
|
73 redo.set_enabled(document.can_redo())
|
4
|
74 end
|
21
|
75 window.update_undo_redo = update_undo_redo -- dont gc
|
|
76 update_undo_redo()
|
|
77 document.add_undo_listener(update_undo_redo)
|
|
78
|
|
79 return new_menu_bar{
|
|
80 menus = {
|
|
81 new_menu{
|
|
82 text = "File"
|
|
83 menu_items = {
|
|
84 new_menu_item{
|
|
85 text = "New File"
|
|
86 accelerator = "meta N"
|
|
87 action_listener = function(_)
|
|
88 new_window()
|
|
89 end
|
|
90 }
|
|
91 new_menu_item{
|
|
92 text = "Open..."
|
|
93 accelerator = "meta O"
|
|
94 action_listener = action_listener(window.open)
|
|
95 }
|
|
96 new_menu_item{
|
|
97 text = "Save"
|
|
98 accelerator = "meta S"
|
|
99 action_listener = function(_)
|
|
100 if window.save() then
|
|
101 revert.set_enabled(true)
|
|
102 end
|
|
103 end
|
|
104 }
|
|
105 revert
|
|
106 }
|
19
|
107 }
|
21
|
108 new_menu{
|
|
109 text = "Edit"
|
|
110 menu_items = {
|
|
111 undo
|
|
112 redo
|
|
113 separator
|
|
114 new_menu_item{
|
|
115 text = "Cut"
|
|
116 accelerator = "meta X"
|
|
117 action_listener = action_listener(window.text_area.cut)
|
|
118 }
|
|
119 new_menu_item{
|
|
120 text = "Copy"
|
|
121 accelerator = "meta C"
|
|
122 action_listener = action_listener(window.text_area.copy)
|
|
123 }
|
|
124 new_menu_item{
|
|
125 text = "Paste"
|
|
126 accelerator = "meta V"
|
|
127 action_listener = action_listener(window.text_area.paste)
|
|
128 }
|
|
129 separator
|
|
130 new_menu_item{
|
|
131 text = "Indent"
|
|
132 accelerator = "meta CLOSE_BRACKET"
|
|
133 action_listener = action_listener(window.indent)
|
|
134 }
|
|
135 new_menu_item{
|
|
136 text = "Unindent"
|
|
137 accelerator = "meta OPEN_BRACKET"
|
|
138 action_listener = action_listener(window.unindent)
|
|
139 }
|
|
140 separator
|
|
141 new_menu_item{
|
|
142 text = "Select All"
|
|
143 accelerator = "meta A"
|
|
144 action_listener = action_listener(window.text_area.select_all)
|
|
145 }
|
|
146 separator
|
|
147 new_menu_item{
|
|
148 text = "Find and Replace"
|
|
149 accelerator = "meta F"
|
|
150 action_listener = action_listener(window.show_find_dialog)
|
|
151 }
|
|
152 }
|
20
|
153 }
|
21
|
154 new_menu{
|
|
155 text = "View"
|
|
156 menu_items = {
|
|
157 new_check_box_menu_item{
|
|
158 text = "Word Wrap"
|
|
159 state = window.text_area.line_wrap
|
|
160 action_listener = function(event)
|
|
161 window.text_area.line_wrap = event.source.state
|
|
162 end
|
|
163 }
|
|
164 new_check_box_menu_item{
|
|
165 text = "Show Whitespace"
|
|
166 action_listener = function(event)
|
|
167 window.text_area.show_whitespace(event.source.state)
|
|
168 end
|
|
169 }
|
|
170 new_menu_item{
|
|
171 text = "Show Cursor Column"
|
|
172 action_listener = function(_)
|
|
173 show_message_dialog( window.frame, "Cursor Column: "..window.cursor_column() )
|
|
174 end
|
|
175 }
|
|
176 new_menu_item{
|
|
177 text = "Goto Line"
|
|
178 accelerator = "meta G"
|
|
179 action_listener = function(_)
|
|
180 local input = show_input_dialog( window.frame, "Goto line" )
|
|
181 --logger.info("input "..input)
|
|
182 local line = input and to_number(input)
|
|
183 if line ~= nil then
|
|
184 window.goto(line)
|
|
185 end
|
|
186 end
|
|
187 }
|
|
188 }
|
20
|
189 }
|
21
|
190 }
|
|
191 }
|
0
|
192 end
|
|
193
|
18
|
194 local function make_find_dialog(window)
|
23
|
195 local dialog = new_dialog{
|
|
196 owner_frame = window.frame
|
25
|
197 --[[
|
23
|
198 content_pane = new_panel{
|
|
199 layout = function(this) return new_box_layout(this,"y_axis") end
|
22
|
200 children = {
|
23
|
201 new_panel{
|
25
|
202 layout = new_flow_layout()
|
|
203 children = {
|
|
204 new_text_field{
|
|
205 columns = 10
|
|
206 }
|
|
207 }
|
|
208 }
|
|
209 new_panel{
|
23
|
210 layout = new_flow_layout("left")
|
|
211 --border = create_empty_border(8,8,8,8)
|
|
212 border = create_line_border(int_to_color(0))
|
|
213 children = {
|
|
214 new_button{
|
|
215 text = "Find Next"
|
|
216 }
|
|
217 new_button{
|
|
218 text = "Find Previous"
|
|
219 }
|
|
220 }
|
22
|
221 }
|
|
222 }
|
|
223 }
|
25
|
224 ]]
|
|
225 --[[
|
|
226 content_pane = new_panel{
|
|
227 layout = new_grid_bag_layout()
|
|
228 alignment_x = "left"
|
|
229 border = create_line_border(int_to_color(0))
|
|
230 children = {
|
|
231 new_text_field{
|
|
232 constraints = new_grid_bag_constraints{
|
|
233 gridx = 1
|
|
234 gridy = 1
|
|
235 }
|
|
236 columns = 10
|
|
237 border = create_line_border(int_to_color(0))
|
|
238 }
|
|
239 new_panel{
|
|
240 constraints = new_grid_bag_constraints{
|
|
241 gridx = 1
|
|
242 gridy = 2
|
|
243 }
|
|
244 layout = new_flow_layout("left")
|
|
245 --border = create_empty_border(8,8,8,8)
|
|
246 border = create_line_border(int_to_color(0))
|
|
247 children = {
|
|
248 new_button{
|
|
249 text = "Find Next"
|
|
250 }
|
|
251 new_button{
|
|
252 text = "Find Previous"
|
|
253 }
|
|
254 }
|
|
255 }
|
|
256 }
|
|
257 }
|
|
258 ]]
|
|
259 ---[[
|
|
260 content_pane = new_panel{
|
|
261 layout = new_mig_layout("debug","[][grow]","[][grow,top]")
|
|
262 children = {
|
|
263 new_label{
|
|
264 text = "Find Next:"
|
|
265 }
|
|
266 new_text_field{
|
|
267 constraints = "wrap,growx"
|
|
268 columns = 20
|
|
269 }
|
|
270 new_panel{
|
|
271 constraints = "span,wrap"
|
|
272 layout = new_mig_layout("debug,insets 0")
|
|
273 --border = create_empty_border(8,8,8,8)
|
|
274 --border = create_line_border(int_to_color(0))
|
|
275 children = {
|
|
276 new_button{
|
|
277 constraints = "pad 0 -6 0 -6"
|
|
278 text = "Find Next"
|
|
279 }
|
|
280 new_button{
|
|
281 constraints = "pad 0 -18 0 -6, gap unrelated"
|
|
282 text = "Find Previous"
|
|
283 }
|
|
284 }
|
|
285 }
|
|
286 new_panel{
|
|
287 constraints = "span"
|
|
288 layout = function(this) return new_box_layout(this,"x_axis") end
|
|
289 children = {
|
|
290 new_button{
|
|
291 text = "Find Next"
|
|
292 }
|
|
293 new_button{
|
|
294 text = "Find Previous"
|
|
295 }
|
|
296 }
|
|
297 }
|
|
298 }
|
|
299 }
|
|
300 --]]
|
22
|
301 }
|
18
|
302 dialog.pack()
|
|
303 local was_shown = false
|
|
304 function window.show_find_dialog()
|
|
305 if not was_shown then
|
|
306 was_shown = true
|
|
307 dialog.move_into_owner()
|
|
308 end
|
|
309 dialog.visible = true
|
|
310 end
|
|
311 end
|
|
312
|
0
|
313 local n_windows = 0
|
5
|
314 local documents = {}
|
0
|
315
|
1
|
316 function new_window(file)
|
5
|
317 local window = {}
|
7
|
318 window.has_file = file~=nil and file.is_file()
|
24
|
319 local text_area = new_text_area{
|
|
320 rows = 10
|
|
321 columns = 20
|
|
322 wrap_style_word = true
|
|
323 line_wrap = true
|
|
324 tab_size = 4
|
|
325 font = { family="Monospaced", size=13 }
|
|
326 border = create_empty_border(0,4,0,4)
|
|
327 }
|
|
328 window.text_area = text_area
|
5
|
329 local title = file and file.canonical().to_string() or "new"
|
|
330 if file ~= nil then
|
|
331 local document = documents[title]
|
|
332 if document == nil then
|
|
333 documents[title] = text_area.document
|
|
334 else
|
|
335 text_area.document = document
|
|
336 end
|
|
337 if file.is_file() then
|
|
338 text_area.text = file.read_text()
|
20
|
339 text_area.document.clear_unedited()
|
5
|
340 end
|
1
|
341 end
|
24
|
342 text_area.set_selection(0)
|
|
343 local frame = new_frame{
|
|
344 content_pane = new_scroll_pane{
|
|
345 view = text_area
|
|
346 row_header_view = new_text_area_line_numbers{
|
|
347 text_area = text_area
|
|
348 foreground_color = int_to_color(0x888888)
|
|
349 border = create_empty_border(0,8,0,8)
|
|
350 }
|
|
351 }
|
|
352 }
|
|
353 window.frame = frame
|
|
354 frame.add_close_listener(function()
|
|
355 n_windows = n_windows - 1
|
|
356 if n_windows == 0 then
|
|
357 Luan.exit()
|
|
358 end
|
|
359 end)
|
10
|
360 local function set_title()
|
|
361 local s = title
|
|
362 if not text_area.document.is_unedited() then
|
|
363 s = s.." *"
|
|
364 end
|
|
365 frame.title = s
|
|
366 end
|
|
367 set_title()
|
24
|
368 window.set_title = set_title -- dont gc
|
10
|
369 text_area.document.add_undo_listener(set_title)
|
5
|
370 function window.open()
|
|
371 local file_chooser = frame.file_chooser_load()
|
|
372 if file ~= nil then
|
|
373 file_chooser.directory = file.parent()
|
|
374 end
|
|
375 file_chooser.visible = true
|
|
376 local new_file = file_chooser.file
|
|
377 if new_file ~= nil then
|
|
378 new_window(new_file)
|
|
379 end
|
|
380 end
|
|
381 function window.save()
|
|
382 if file == nil then
|
|
383 local file_chooser = frame.file_chooser_save()
|
|
384 file_chooser.visible = true
|
|
385 file = file_chooser.file
|
|
386 if file == nil then
|
7
|
387 return false
|
5
|
388 end
|
|
389 title = file.canonical().to_string()
|
|
390 frame.title = title
|
|
391 documents[title] = text_area.document
|
|
392 end
|
|
393 file.write_text(text_area.text)
|
10
|
394 text_area.document.set_unedited()
|
7
|
395 return true
|
|
396 end
|
|
397 function window.revert()
|
15
|
398 local selection = text_area.get_selection()
|
7
|
399 local text = file.read_text()
|
|
400 text_area.text = text
|
15
|
401 text_area.set_selection(min(selection,#text+1))
|
10
|
402 text_area.document.set_unedited()
|
5
|
403 end
|
15
|
404 local function selection_lines()
|
|
405 local start_seletion, end_selection = text_area.get_selection()
|
|
406 local end_ = end_selection == start_seletion and end_selection or end_selection - 1
|
|
407 local start_line = text_area.get_line_from_position(start_seletion)
|
|
408 local end_line = text_area.get_line_from_position(end_)
|
|
409 local start_pos = text_area.get_line_start_position(start_line)
|
|
410 local end_pos = text_area.get_line_end_position(end_line)
|
|
411 local text = text_area.text
|
|
412 text = sub_string(text,start_pos,end_pos-2)
|
|
413 return {
|
|
414 text = text
|
|
415 start_pos = start_pos
|
|
416 length = #text
|
|
417 lines = end_line - start_line + 1
|
|
418 start_seletion = start_seletion
|
|
419 end_selection = end_selection
|
|
420 }
|
|
421 end
|
|
422 function window.indent()
|
|
423 local r = selection_lines()
|
|
424 local text = r.text
|
|
425 local start_pos = r.start_pos
|
|
426 text = "\t"..replace(text,"\n","\n\t")
|
|
427 text_area.replace(start_pos,r.length,text)
|
|
428 --logger.info(stringify{text_area.get_selection()})
|
|
429 text_area.set_selection( r.start_seletion+1, r.end_selection+r.lines )
|
|
430 end
|
|
431 function window.unindent()
|
|
432 local r = selection_lines()
|
|
433 local text = r.text
|
|
434 text = "\n"..text
|
|
435 local start_seletion = r.start_seletion
|
|
436 if starts_with(text,"\n\t") then
|
|
437 start_seletion = start_seletion - 1
|
|
438 end
|
|
439 local len1 = #text
|
|
440 text = replace(text,"\n\t","\n")
|
|
441 local len2 = #text
|
|
442 local end_selection = r.end_selection - (len1 - len2)
|
|
443 text = sub_string(text,2)
|
|
444 text_area.replace(r.start_pos,r.length,text)
|
|
445 text_area.set_selection(start_seletion,end_selection)
|
|
446 end
|
16
|
447 function window.cursor_column()
|
|
448 local cursor_pos = text_area.get_selection()
|
|
449 local line = text_area.get_line_from_position(cursor_pos)
|
|
450 local start_line_pos = text_area.get_line_start_position(line)
|
|
451 return cursor_pos - start_line_pos + 1
|
|
452 end
|
17
|
453 function window.goto(line)
|
|
454 local pos = text_area.get_line_start_position(line)
|
|
455 text_area.set_selection(pos)
|
|
456 end
|
18
|
457 make_find_dialog(window)
|
1
|
458 local menu_bar = make_menu_bar(window)
|
0
|
459 frame.set_menu_bar(menu_bar)
|
|
460 frame.pack()
|
|
461 frame.visible = true
|
|
462 text_area.request_focus_in_window()
|
|
463 n_windows = n_windows + 1
|
|
464 end
|
|
465
|
1
|
466 Swing.run(function()
|
3
|
467 local args = Luan.arg
|
|
468 if #args == 0 then
|
|
469 new_window()
|
|
470 else
|
|
471 for _, arg in ipairs(args) do
|
|
472 local file = new_file(arg)
|
|
473 new_window(file)
|
|
474 end
|
|
475 end
|
1
|
476 end)
|