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 }
|
|
148 separator
|
29
|
149 new_check_box_menu_item{
|
21
|
150 text = "Find and Replace"
|
|
151 accelerator = "meta F"
|
29
|
152 action_listener = function(event)
|
|
153 window.show_find_panel(event.source.state)
|
|
154 end
|
21
|
155 }
|
|
156 }
|
20
|
157 }
|
21
|
158 new_menu{
|
|
159 text = "View"
|
|
160 menu_items = {
|
|
161 new_check_box_menu_item{
|
|
162 text = "Word Wrap"
|
|
163 state = window.text_area.line_wrap
|
|
164 action_listener = function(event)
|
|
165 window.text_area.line_wrap = event.source.state
|
|
166 end
|
|
167 }
|
|
168 new_check_box_menu_item{
|
|
169 text = "Show Whitespace"
|
|
170 action_listener = function(event)
|
|
171 window.text_area.show_whitespace(event.source.state)
|
|
172 end
|
|
173 }
|
|
174 new_menu_item{
|
|
175 text = "Show Cursor Column"
|
|
176 action_listener = function(_)
|
|
177 show_message_dialog( window.frame, "Cursor Column: "..window.cursor_column() )
|
|
178 end
|
|
179 }
|
|
180 new_menu_item{
|
|
181 text = "Goto Line"
|
|
182 accelerator = "meta G"
|
|
183 action_listener = function(_)
|
|
184 local input = show_input_dialog( window.frame, "Goto line" )
|
|
185 local line = input and to_number(input)
|
|
186 if line ~= nil then
|
|
187 window.goto(line)
|
|
188 end
|
|
189 end
|
|
190 }
|
|
191 }
|
20
|
192 }
|
21
|
193 }
|
|
194 }
|
29
|
195 window.frame.set_menu_bar(menu_bar)
|
0
|
196 end
|
|
197
|
28
|
198 local function get_matches(text,s)
|
|
199 local n = #s
|
|
200 if n == 0 then
|
|
201 return nil
|
|
202 end
|
|
203 local matches = {}
|
|
204 local i = 0
|
|
205 while(true) do
|
|
206 local j = find(text,s,i)
|
|
207 if j == nil then
|
|
208 break
|
|
209 end
|
|
210 matches[#matches+1] = { start=j, end_=j+n }
|
|
211 i = j + n
|
|
212 end
|
|
213 return matches
|
|
214 end
|
|
215
|
29
|
216 local function make_find_panel(window)
|
28
|
217 local text_area = window.text_area
|
|
218 local find_field, output
|
|
219 local function find_match(event)
|
|
220 --logger.info("action "..event.action)
|
|
221 local s = find_field.text
|
|
222 local matches = get_matches( text_area.text, s )
|
|
223 if matches == nil then
|
|
224 output.text = ""
|
|
225 return
|
|
226 end
|
|
227 local n_matches = #matches
|
|
228 if n_matches == 0 then
|
|
229 output.text = "0 matches"
|
|
230 return
|
|
231 end
|
|
232 local action = event.action
|
|
233 if action == "next" then
|
|
234 local _, pos = text_area.get_selection()
|
|
235 for i, match in ipairs(matches) do
|
|
236 if match.start >= pos then
|
|
237 text_area.set_selection( match.start, match.end_ )
|
|
238 output.text = i.." of "..n_matches.." matches"
|
|
239 return
|
|
240 end
|
|
241 end
|
|
242 local match = matches[1]
|
|
243 text_area.set_selection( match.start, match.end_ )
|
|
244 output.text = "1 of "..n_matches.." matches; wrapped past end"
|
|
245 elseif action == "previous" then
|
|
246 local pos = text_area.get_selection()
|
|
247 for i in range(n_matches,1,-1) do
|
|
248 local match = matches[i]
|
|
249 if match.end_ <= 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[n_matches]
|
|
256 text_area.set_selection( match.start, match.end_ )
|
|
257 output.text = n_matches.." of "..n_matches.." matches; wrapped past end"
|
|
258 else
|
|
259 error(action)
|
|
260 end
|
|
261 end
|
|
262 find_field = new_text_field{
|
29
|
263 constraints = "growx"
|
30
|
264 show_whitespace = true
|
28
|
265 action = "next"
|
|
266 action_listener = find_match
|
|
267 }
|
|
268 output = new_label{
|
29
|
269 constraints = "span"
|
28
|
270 }
|
29
|
271 local find_panel = new_panel{
|
|
272 constraints = "growy 0,growx"
|
|
273 layout = new_mig_layout("","[][grow][grow 0]")
|
|
274 visible = false
|
|
275 children = {
|
|
276 new_label{
|
|
277 constraints = "right"
|
|
278 text = "Find:"
|
|
279 }
|
|
280 find_field
|
30
|
281 new_button{
|
|
282 constraints = "grow"
|
|
283 text = "Find Next"
|
|
284 action = "next"
|
|
285 action_listener = find_match
|
|
286 }
|
|
287 new_button{
|
|
288 constraints = "grow,wrap"
|
|
289 text = "Find Previous"
|
|
290 action = "previous"
|
|
291 action_listener = find_match
|
29
|
292 }
|
|
293 new_label{
|
|
294 constraints = "right"
|
|
295 text = "Replace:"
|
25
|
296 }
|
29
|
297 new_text_field{
|
|
298 constraints = "growx"
|
30
|
299 show_whitespace = true
|
29
|
300 }
|
|
301 new_button{
|
30
|
302 constraints = "grow"
|
29
|
303 text = "Replace"
|
|
304 }
|
30
|
305 new_button{
|
|
306 constraints = "grow,wrap"
|
|
307 text = "Replace All"
|
|
308 }
|
31
|
309 new_panel{
|
|
310 constraints = "span,wrap"
|
|
311 layout = new_mig_layout("insets 0,gap 16px")
|
|
312 children = {
|
|
313 new_check_box{
|
|
314 text = "Use Regex"
|
|
315 }
|
|
316 new_button{
|
|
317 text = "Convert Leading Tabs to Spaces"
|
|
318 }
|
|
319 new_button{
|
|
320 text = "Convert Leading Spaces to Tabs"
|
|
321 }
|
|
322 }
|
|
323 }
|
29
|
324 output
|
25
|
325 }
|
22
|
326 }
|
29
|
327 function window.show_find_panel(visible)
|
|
328 find_panel.visible = visible
|
|
329 if visible then
|
|
330 find_field.request_focus_in_window()
|
18
|
331 end
|
|
332 end
|
29
|
333 return find_panel
|
18
|
334 end
|
|
335
|
0
|
336 local n_windows = 0
|
5
|
337 local documents = {}
|
0
|
338
|
1
|
339 function new_window(file)
|
5
|
340 local window = {}
|
7
|
341 window.has_file = file~=nil and file.is_file()
|
24
|
342 local text_area = new_text_area{
|
|
343 wrap_style_word = true
|
|
344 line_wrap = true
|
|
345 tab_size = 4
|
|
346 font = { family="Monospaced", size=13 }
|
|
347 }
|
|
348 window.text_area = text_area
|
5
|
349 local title = file and file.canonical().to_string() or "new"
|
|
350 if file ~= nil then
|
|
351 local document = documents[title]
|
|
352 if document == nil then
|
|
353 documents[title] = text_area.document
|
|
354 else
|
|
355 text_area.document = document
|
|
356 end
|
|
357 if file.is_file() then
|
|
358 text_area.text = file.read_text()
|
20
|
359 text_area.document.clear_unedited()
|
5
|
360 end
|
1
|
361 end
|
24
|
362 text_area.set_selection(0)
|
29
|
363 local find_panel = make_find_panel(window)
|
24
|
364 local frame = new_frame{
|
29
|
365 preferred_size = { width=700, height=700 }
|
|
366 content_pane = new_panel{
|
|
367 layout = new_mig_layout("insets 0,wrap,fill,hidemode 3","","[][grow 0]")
|
|
368 children = {
|
|
369 new_scroll_pane{
|
|
370 constraints = "grow"
|
|
371 view = text_area
|
|
372 row_header_view = new_text_area_line_numbers{
|
|
373 text_area = text_area
|
|
374 foreground_color = int_to_color(0x888888)
|
|
375 border = create_empty_border(0,8,0,8)
|
|
376 }
|
|
377 }
|
|
378 find_panel
|
24
|
379 }
|
|
380 }
|
|
381 }
|
|
382 window.frame = frame
|
|
383 frame.add_close_listener(function()
|
|
384 n_windows = n_windows - 1
|
|
385 if n_windows == 0 then
|
|
386 Luan.exit()
|
|
387 end
|
|
388 end)
|
10
|
389 local function set_title()
|
|
390 local s = title
|
|
391 if not text_area.document.is_unedited() then
|
|
392 s = s.." *"
|
|
393 end
|
|
394 frame.title = s
|
|
395 end
|
|
396 set_title()
|
24
|
397 window.set_title = set_title -- dont gc
|
10
|
398 text_area.document.add_undo_listener(set_title)
|
5
|
399 function window.open()
|
|
400 local file_chooser = frame.file_chooser_load()
|
|
401 if file ~= nil then
|
|
402 file_chooser.directory = file.parent()
|
|
403 end
|
|
404 file_chooser.visible = true
|
|
405 local new_file = file_chooser.file
|
|
406 if new_file ~= nil then
|
|
407 new_window(new_file)
|
|
408 end
|
|
409 end
|
|
410 function window.save()
|
|
411 if file == nil then
|
|
412 local file_chooser = frame.file_chooser_save()
|
|
413 file_chooser.visible = true
|
|
414 file = file_chooser.file
|
|
415 if file == nil then
|
7
|
416 return false
|
5
|
417 end
|
|
418 title = file.canonical().to_string()
|
|
419 frame.title = title
|
|
420 documents[title] = text_area.document
|
|
421 end
|
|
422 file.write_text(text_area.text)
|
10
|
423 text_area.document.set_unedited()
|
7
|
424 return true
|
|
425 end
|
|
426 function window.revert()
|
15
|
427 local selection = text_area.get_selection()
|
7
|
428 local text = file.read_text()
|
|
429 text_area.text = text
|
15
|
430 text_area.set_selection(min(selection,#text+1))
|
10
|
431 text_area.document.set_unedited()
|
5
|
432 end
|
15
|
433 local function selection_lines()
|
|
434 local start_seletion, end_selection = text_area.get_selection()
|
|
435 local end_ = end_selection == start_seletion and end_selection or end_selection - 1
|
|
436 local start_line = text_area.get_line_from_position(start_seletion)
|
|
437 local end_line = text_area.get_line_from_position(end_)
|
|
438 local start_pos = text_area.get_line_start_position(start_line)
|
|
439 local end_pos = text_area.get_line_end_position(end_line)
|
|
440 local text = text_area.text
|
|
441 text = sub_string(text,start_pos,end_pos-2)
|
|
442 return {
|
|
443 text = text
|
|
444 start_pos = start_pos
|
|
445 length = #text
|
|
446 lines = end_line - start_line + 1
|
|
447 start_seletion = start_seletion
|
|
448 end_selection = end_selection
|
|
449 }
|
|
450 end
|
|
451 function window.indent()
|
|
452 local r = selection_lines()
|
|
453 local text = r.text
|
|
454 local start_pos = r.start_pos
|
|
455 text = "\t"..replace(text,"\n","\n\t")
|
|
456 text_area.replace(start_pos,r.length,text)
|
|
457 --logger.info(stringify{text_area.get_selection()})
|
|
458 text_area.set_selection( r.start_seletion+1, r.end_selection+r.lines )
|
|
459 end
|
|
460 function window.unindent()
|
|
461 local r = selection_lines()
|
|
462 local text = r.text
|
|
463 text = "\n"..text
|
|
464 local start_seletion = r.start_seletion
|
|
465 if starts_with(text,"\n\t") then
|
|
466 start_seletion = start_seletion - 1
|
|
467 end
|
|
468 local len1 = #text
|
|
469 text = replace(text,"\n\t","\n")
|
|
470 local len2 = #text
|
|
471 local end_selection = r.end_selection - (len1 - len2)
|
|
472 text = sub_string(text,2)
|
|
473 text_area.replace(r.start_pos,r.length,text)
|
|
474 text_area.set_selection(start_seletion,end_selection)
|
|
475 end
|
16
|
476 function window.cursor_column()
|
|
477 local cursor_pos = text_area.get_selection()
|
|
478 local line = text_area.get_line_from_position(cursor_pos)
|
|
479 local start_line_pos = text_area.get_line_start_position(line)
|
|
480 return cursor_pos - start_line_pos + 1
|
|
481 end
|
17
|
482 function window.goto(line)
|
|
483 local pos = text_area.get_line_start_position(line)
|
|
484 text_area.set_selection(pos)
|
|
485 end
|
29
|
486 add_menu_bar(window)
|
0
|
487 frame.pack()
|
|
488 frame.visible = true
|
|
489 text_area.request_focus_in_window()
|
|
490 n_windows = n_windows + 1
|
|
491 end
|
|
492
|
1
|
493 Swing.run(function()
|
3
|
494 local args = Luan.arg
|
|
495 if #args == 0 then
|
|
496 new_window()
|
|
497 else
|
|
498 for _, arg in ipairs(args) do
|
|
499 local file = new_file(arg)
|
|
500 new_window(file)
|
|
501 end
|
|
502 end
|
1
|
503 end)
|