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