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