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