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
|
33
|
227 matches[#matches+1] = { start=j1, end_=j2 }
|
|
228 i = j2
|
28
|
229 end
|
|
230 return matches
|
|
231 end
|
|
232
|
29
|
233 local function make_find_panel(window)
|
28
|
234 local text_area = window.text_area
|
34
|
235 local find_field, replace_field, regex_check_box, output
|
28
|
236 local function find_match(event)
|
|
237 --logger.info("action "..event.action)
|
|
238 local s = find_field.text
|
33
|
239 if #s == 0 then
|
28
|
240 output.text = ""
|
|
241 return
|
|
242 end
|
33
|
243 if not regex_check_box.is_selected then
|
|
244 s = regex_quote(s)
|
|
245 end
|
|
246 local matches
|
|
247 try
|
|
248 matches = get_matches( text_area.text, s )
|
|
249 catch e
|
|
250 output.text = "Regex error: "..e.get_message()
|
|
251 return
|
|
252 end
|
28
|
253 local n_matches = #matches
|
|
254 if n_matches == 0 then
|
|
255 output.text = "0 matches"
|
|
256 return
|
|
257 end
|
|
258 local action = event.action
|
|
259 if action == "next" then
|
|
260 local _, pos = text_area.get_selection()
|
|
261 for i, match in ipairs(matches) do
|
|
262 if match.start >= 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[1]
|
|
269 text_area.set_selection( match.start, match.end_ )
|
|
270 output.text = "1 of "..n_matches.." matches; wrapped past end"
|
|
271 elseif action == "previous" then
|
|
272 local pos = text_area.get_selection()
|
|
273 for i in range(n_matches,1,-1) do
|
|
274 local match = matches[i]
|
|
275 if match.end_ <= pos then
|
|
276 text_area.set_selection( match.start, match.end_ )
|
|
277 output.text = i.." of "..n_matches.." matches"
|
|
278 return
|
|
279 end
|
|
280 end
|
|
281 local match = matches[n_matches]
|
|
282 text_area.set_selection( match.start, match.end_ )
|
|
283 output.text = n_matches.." of "..n_matches.." matches; wrapped past end"
|
|
284 else
|
|
285 error(action)
|
|
286 end
|
|
287 end
|
34
|
288 local function replace_match(_)
|
|
289 local find = find_field.text
|
|
290 if #find == 0 then
|
|
291 output.text = ""
|
|
292 return
|
|
293 end
|
|
294 local replace = replace_field.text
|
|
295 if not regex_check_box.is_selected then
|
|
296 find = regex_quote(find)
|
|
297 replace = regex_quote_replacement(replace)
|
|
298 end
|
|
299 local r
|
|
300 try
|
|
301 r = regex(find)
|
|
302 catch e
|
|
303 output.text = "Regex error: "..e.get_message()
|
|
304 return
|
|
305 end
|
|
306 local selection = text_area.selected_text
|
|
307 local new, n = r.gsub(selection,replace)
|
|
308 if n > 0 then
|
|
309 text_area.selected_text = new
|
|
310 end
|
|
311 output.text = n.." replacements"
|
|
312 end
|
28
|
313 find_field = new_text_field{
|
29
|
314 constraints = "growx"
|
30
|
315 show_whitespace = true
|
28
|
316 action = "next"
|
|
317 action_listener = find_match
|
|
318 }
|
34
|
319 replace_field = new_text_field{
|
|
320 constraints = "growx"
|
|
321 show_whitespace = true
|
|
322 action_listener = replace_match
|
|
323 }
|
33
|
324 regex_check_box = new_check_box{
|
|
325 text = "Use Regex"
|
|
326 }
|
28
|
327 output = new_label{
|
29
|
328 constraints = "span"
|
28
|
329 }
|
29
|
330 local find_panel = new_panel{
|
|
331 constraints = "growy 0,growx"
|
|
332 layout = new_mig_layout("","[][grow][grow 0]")
|
|
333 visible = false
|
|
334 children = {
|
|
335 new_label{
|
|
336 constraints = "right"
|
|
337 text = "Find:"
|
|
338 }
|
|
339 find_field
|
30
|
340 new_button{
|
|
341 constraints = "grow"
|
|
342 text = "Find Next"
|
|
343 action = "next"
|
|
344 action_listener = find_match
|
|
345 }
|
|
346 new_button{
|
|
347 constraints = "grow,wrap"
|
|
348 text = "Find Previous"
|
|
349 action = "previous"
|
|
350 action_listener = find_match
|
29
|
351 }
|
|
352 new_label{
|
|
353 constraints = "right"
|
|
354 text = "Replace:"
|
25
|
355 }
|
34
|
356 replace_field
|
29
|
357 new_button{
|
30
|
358 constraints = "grow"
|
29
|
359 text = "Replace"
|
34
|
360 tool_tip_text = "Replace matches in selected text"
|
|
361 action_listener = replace_match
|
29
|
362 }
|
30
|
363 new_button{
|
|
364 constraints = "grow,wrap"
|
|
365 text = "Replace All"
|
|
366 }
|
31
|
367 new_panel{
|
|
368 constraints = "span,wrap"
|
|
369 layout = new_mig_layout("insets 0,gap 16px")
|
|
370 children = {
|
33
|
371 regex_check_box
|
31
|
372 new_button{
|
32
|
373 text = "Learn About Regular Expressions"
|
31
|
374 }
|
|
375 }
|
|
376 }
|
29
|
377 output
|
25
|
378 }
|
22
|
379 }
|
29
|
380 function window.show_find_panel(visible)
|
|
381 find_panel.visible = visible
|
|
382 if visible then
|
|
383 find_field.request_focus_in_window()
|
18
|
384 end
|
|
385 end
|
33
|
386 function window.find_case_insensitive(_)
|
|
387 find_panel.visible = true
|
|
388 window.find_menu_item.is_selected = true
|
|
389 regex_check_box.is_selected = true
|
|
390 find_field.text = "(?i)\Q\E"
|
|
391 find_field.set_selection(7)
|
|
392 find_field.request_focus_in_window()
|
34
|
393 output.text = ""
|
33
|
394 end
|
29
|
395 return find_panel
|
18
|
396 end
|
|
397
|
0
|
398 local n_windows = 0
|
5
|
399 local documents = {}
|
0
|
400
|
1
|
401 function new_window(file)
|
5
|
402 local window = {}
|
7
|
403 window.has_file = file~=nil and file.is_file()
|
24
|
404 local text_area = new_text_area{
|
|
405 wrap_style_word = true
|
|
406 line_wrap = true
|
|
407 tab_size = 4
|
|
408 font = { family="Monospaced", size=13 }
|
|
409 }
|
|
410 window.text_area = text_area
|
5
|
411 local title = file and file.canonical().to_string() or "new"
|
|
412 if file ~= nil then
|
|
413 local document = documents[title]
|
|
414 if document == nil then
|
|
415 documents[title] = text_area.document
|
|
416 else
|
|
417 text_area.document = document
|
|
418 end
|
|
419 if file.is_file() then
|
|
420 text_area.text = file.read_text()
|
20
|
421 text_area.document.clear_unedited()
|
5
|
422 end
|
1
|
423 end
|
24
|
424 text_area.set_selection(0)
|
29
|
425 local find_panel = make_find_panel(window)
|
24
|
426 local frame = new_frame{
|
29
|
427 preferred_size = { width=700, height=700 }
|
|
428 content_pane = new_panel{
|
|
429 layout = new_mig_layout("insets 0,wrap,fill,hidemode 3","","[][grow 0]")
|
|
430 children = {
|
|
431 new_scroll_pane{
|
|
432 constraints = "grow"
|
|
433 view = text_area
|
|
434 row_header_view = new_text_area_line_numbers{
|
|
435 text_area = text_area
|
|
436 foreground_color = int_to_color(0x888888)
|
|
437 border = create_empty_border(0,8,0,8)
|
|
438 }
|
|
439 }
|
|
440 find_panel
|
24
|
441 }
|
|
442 }
|
|
443 }
|
|
444 window.frame = frame
|
|
445 frame.add_close_listener(function()
|
|
446 n_windows = n_windows - 1
|
|
447 if n_windows == 0 then
|
|
448 Luan.exit()
|
|
449 end
|
|
450 end)
|
10
|
451 local function set_title()
|
|
452 local s = title
|
|
453 if not text_area.document.is_unedited() then
|
|
454 s = s.." *"
|
|
455 end
|
|
456 frame.title = s
|
|
457 end
|
|
458 set_title()
|
24
|
459 window.set_title = set_title -- dont gc
|
10
|
460 text_area.document.add_undo_listener(set_title)
|
5
|
461 function window.open()
|
|
462 local file_chooser = frame.file_chooser_load()
|
|
463 if file ~= nil then
|
|
464 file_chooser.directory = file.parent()
|
|
465 end
|
|
466 file_chooser.visible = true
|
|
467 local new_file = file_chooser.file
|
|
468 if new_file ~= nil then
|
|
469 new_window(new_file)
|
|
470 end
|
|
471 end
|
|
472 function window.save()
|
|
473 if file == nil then
|
|
474 local file_chooser = frame.file_chooser_save()
|
|
475 file_chooser.visible = true
|
|
476 file = file_chooser.file
|
|
477 if file == nil then
|
7
|
478 return false
|
5
|
479 end
|
|
480 title = file.canonical().to_string()
|
|
481 frame.title = title
|
|
482 documents[title] = text_area.document
|
|
483 end
|
|
484 file.write_text(text_area.text)
|
10
|
485 text_area.document.set_unedited()
|
7
|
486 return true
|
|
487 end
|
|
488 function window.revert()
|
15
|
489 local selection = text_area.get_selection()
|
7
|
490 local text = file.read_text()
|
|
491 text_area.text = text
|
15
|
492 text_area.set_selection(min(selection,#text+1))
|
10
|
493 text_area.document.set_unedited()
|
5
|
494 end
|
15
|
495 local function selection_lines()
|
|
496 local start_seletion, end_selection = text_area.get_selection()
|
|
497 local end_ = end_selection == start_seletion and end_selection or end_selection - 1
|
|
498 local start_line = text_area.get_line_from_position(start_seletion)
|
|
499 local end_line = text_area.get_line_from_position(end_)
|
|
500 local start_pos = text_area.get_line_start_position(start_line)
|
|
501 local end_pos = text_area.get_line_end_position(end_line)
|
|
502 local text = text_area.text
|
|
503 text = sub_string(text,start_pos,end_pos-2)
|
|
504 return {
|
|
505 text = text
|
|
506 start_pos = start_pos
|
|
507 length = #text
|
|
508 lines = end_line - start_line + 1
|
|
509 start_seletion = start_seletion
|
|
510 end_selection = end_selection
|
|
511 }
|
|
512 end
|
|
513 function window.indent()
|
|
514 local r = selection_lines()
|
|
515 local text = r.text
|
|
516 local start_pos = r.start_pos
|
|
517 text = "\t"..replace(text,"\n","\n\t")
|
|
518 text_area.replace(start_pos,r.length,text)
|
|
519 --logger.info(stringify{text_area.get_selection()})
|
|
520 text_area.set_selection( r.start_seletion+1, r.end_selection+r.lines )
|
|
521 end
|
|
522 function window.unindent()
|
|
523 local r = selection_lines()
|
|
524 local text = r.text
|
|
525 text = "\n"..text
|
|
526 local start_seletion = r.start_seletion
|
|
527 if starts_with(text,"\n\t") then
|
|
528 start_seletion = start_seletion - 1
|
|
529 end
|
|
530 local len1 = #text
|
|
531 text = replace(text,"\n\t","\n")
|
|
532 local len2 = #text
|
|
533 local end_selection = r.end_selection - (len1 - len2)
|
|
534 text = sub_string(text,2)
|
|
535 text_area.replace(r.start_pos,r.length,text)
|
|
536 text_area.set_selection(start_seletion,end_selection)
|
|
537 end
|
16
|
538 function window.cursor_column()
|
|
539 local cursor_pos = text_area.get_selection()
|
|
540 local line = text_area.get_line_from_position(cursor_pos)
|
|
541 local start_line_pos = text_area.get_line_start_position(line)
|
|
542 return cursor_pos - start_line_pos + 1
|
|
543 end
|
17
|
544 function window.goto(line)
|
|
545 local pos = text_area.get_line_start_position(line)
|
|
546 text_area.set_selection(pos)
|
|
547 end
|
29
|
548 add_menu_bar(window)
|
0
|
549 frame.pack()
|
|
550 frame.visible = true
|
|
551 text_area.request_focus_in_window()
|
|
552 n_windows = n_windows + 1
|
|
553 end
|
|
554
|
1
|
555 Swing.run(function()
|
3
|
556 local args = Luan.arg
|
|
557 if #args == 0 then
|
|
558 new_window()
|
|
559 else
|
|
560 for _, arg in ipairs(args) do
|
|
561 local file = new_file(arg)
|
|
562 new_window(file)
|
|
563 end
|
|
564 end
|
1
|
565 end)
|