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