0
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
3
|
3 local ipairs = Luan.ipairs or error()
|
15
|
4 local stringify = Luan.stringify or error()
|
|
5 local String = require "luan:String.luan"
|
|
6 local sub_string = String.sub or error()
|
|
7 local replace = String.replace or error()
|
|
8 local starts_with = String.starts_with or error()
|
17
|
9 local to_number = String.to_number or error()
|
0
|
10 local Io = require "luan:Io.luan"
|
|
11 local print = Io.print or error()
|
3
|
12 local new_file = Io.schemes.file or error()
|
7
|
13 local Math = require "luan:Math.luan"
|
|
14 local min = Math.min or error()
|
0
|
15 local Swing = require "luan:swing/Swing.luan"
|
|
16 local new_frame = require("luan:swing/Frame.luan").new or error()
|
|
17 local new_label = require("luan:swing/Label.luan").new or error()
|
|
18 local new_text_area = require("luan:swing/Text_area.luan").new or error()
|
|
19 local new_scroll_pane = require("luan:swing/Scroll_pane.luan").new or error()
|
18
|
20 local new_text_area_line_numbers = require("luan:swing/Text_area_line_numbers.luan").new or error()
|
0
|
21 local new_menu_bar = require("luan:swing/Menu_bar.luan").new or error()
|
|
22 local new_menu = require("luan:swing/Menu.luan").new or error()
|
|
23 local new_menu_item = require("luan:swing/Menu_item.luan").new or error()
|
2
|
24 local new_check_box_menu_item = require("luan:swing/Check_box_menu_item.luan").new or error()
|
12
|
25 local int_to_color = require("luan:swing/Color.luan").int_to_color or error()
|
18
|
26 local Border = require "luan:swing/Border.luan"
|
|
27 local create_empty_border = Border.create_empty_border or error()
|
|
28 local create_line_border = Border.create_line_border or error()
|
|
29 local Layout = require "luan:swing/Layout.luan"
|
|
30 local new_flow_layout = Layout.new_flow_layout or error()
|
|
31 local new_box_layout = Layout.new_box_layout or error()
|
16
|
32 local Option_pane = require "luan:swing/Option_pane.luan"
|
|
33 local show_message_dialog = Option_pane.show_message_dialog or error()
|
17
|
34 local show_input_dialog = Option_pane.show_input_dialog or error()
|
18
|
35 local new_dialog = require("luan:swing/Dialog.luan").new or error()
|
|
36 local new_panel = require("luan:swing/Component.luan").new_panel or error()
|
|
37 local new_button = require("luan:swing/Button.luan").new or error()
|
0
|
38 local Logging = require "luan:logging/Logging.luan"
|
|
39 local logger = Logging.logger "editor"
|
|
40
|
|
41
|
|
42 local new_window
|
|
43
|
20
|
44 local function action_listener(fn)
|
|
45 return function(_)
|
|
46 fn()
|
|
47 end
|
|
48 end
|
|
49
|
1
|
50 local function make_menu_bar(window)
|
0
|
51 local menu_bar = new_menu_bar()
|
4
|
52 do
|
1
|
53 local file_menu = new_menu()
|
|
54 file_menu.text = "File"
|
4
|
55 do
|
19
|
56 local new_file = new_menu_item{
|
|
57 text = "New File"
|
|
58 accelerator = "meta N"
|
20
|
59 action_listener = function(_)
|
|
60 new_window()
|
|
61 end
|
19
|
62 }
|
1
|
63 file_menu.add(new_file)
|
4
|
64 end
|
|
65 do
|
19
|
66 local open = new_menu_item{
|
|
67 text = "Open..."
|
|
68 accelerator = "meta O"
|
20
|
69 action_listener = action_listener(window.open)
|
19
|
70 }
|
1
|
71 file_menu.add(open)
|
4
|
72 end
|
10
|
73 local revert
|
4
|
74 do
|
19
|
75 local save = new_menu_item{
|
|
76 text = "Save"
|
|
77 accelerator = "meta S"
|
20
|
78 action_listener = function(_)
|
19
|
79 if window.save() then
|
|
80 revert.set_enabled(true)
|
|
81 end
|
7
|
82 end
|
19
|
83 }
|
4
|
84 file_menu.add(save)
|
|
85 end
|
7
|
86 do
|
19
|
87 revert = new_menu_item{
|
|
88 text = "Revert"
|
|
89 enabled = window.has_file
|
20
|
90 action_listener = action_listener(window.revert)
|
19
|
91 }
|
7
|
92 file_menu.add(revert)
|
|
93 end
|
1
|
94 menu_bar.add(file_menu)
|
4
|
95 end
|
|
96 do
|
6
|
97 local edit_menu = new_menu()
|
|
98 edit_menu.text = "Edit"
|
9
|
99 local document = window.text_area.document
|
|
100 local undo, redo
|
|
101 do
|
19
|
102 undo = new_menu_item{
|
|
103 text = "Undo"
|
|
104 accelerator = "meta Z"
|
20
|
105 action_listener = action_listener(document.undo)
|
19
|
106 }
|
9
|
107 edit_menu.add(undo)
|
|
108 end
|
|
109 do
|
19
|
110 redo = new_menu_item{
|
|
111 text = "Redo"
|
|
112 accelerator = "meta shift Z"
|
20
|
113 action_listener = action_listener(document.redo)
|
19
|
114 }
|
9
|
115 edit_menu.add(redo)
|
|
116 end
|
10
|
117 local function update_undo_redo()
|
|
118 undo.set_enabled(document.can_undo())
|
|
119 redo.set_enabled(document.can_redo())
|
|
120 end
|
11
|
121 edit_menu.dont_gc(update_undo_redo)
|
9
|
122 update_undo_redo()
|
10
|
123 document.add_undo_listener(update_undo_redo)
|
9
|
124 edit_menu.add_separator()
|
6
|
125 do
|
19
|
126 local cut = new_menu_item{
|
|
127 text = "Cut"
|
|
128 accelerator = "meta X"
|
20
|
129 action_listener = action_listener(window.text_area.cut)
|
19
|
130 }
|
6
|
131 edit_menu.add(cut)
|
|
132 end
|
|
133 do
|
19
|
134 local copy = new_menu_item{
|
|
135 text = "Copy"
|
|
136 accelerator = "meta C"
|
20
|
137 action_listener = action_listener(window.text_area.copy)
|
19
|
138 }
|
6
|
139 edit_menu.add(copy)
|
|
140 end
|
|
141 do
|
19
|
142 local paste = new_menu_item{
|
|
143 text = "Paste"
|
|
144 accelerator = "meta V"
|
20
|
145 action_listener = action_listener(window.text_area.paste)
|
19
|
146 }
|
6
|
147 edit_menu.add(paste)
|
|
148 end
|
|
149 edit_menu.add_separator()
|
|
150 do
|
20
|
151 local indent = new_menu_item{
|
|
152 text = "Indent"
|
|
153 accelerator = "meta CLOSE_BRACKET"
|
|
154 action_listener = action_listener(window.indent)
|
|
155 }
|
15
|
156 edit_menu.add(indent)
|
|
157 end
|
|
158 do
|
20
|
159 local unindent = new_menu_item{
|
|
160 text = "Unindent"
|
|
161 accelerator = "meta OPEN_BRACKET"
|
|
162 action_listener = action_listener(window.unindent)
|
|
163 }
|
15
|
164 edit_menu.add(unindent)
|
|
165 end
|
|
166 edit_menu.add_separator()
|
|
167 do
|
20
|
168 local select_all = new_menu_item{
|
|
169 text = "Select All"
|
|
170 accelerator = "meta A"
|
|
171 action_listener = action_listener(window.text_area.select_all)
|
|
172 }
|
6
|
173 edit_menu.add(select_all)
|
|
174 end
|
18
|
175 edit_menu.add_separator()
|
|
176 do
|
20
|
177 local find = new_menu_item{
|
|
178 text = "Find and Replace"
|
|
179 accelerator = "meta F"
|
|
180 action_listener = action_listener(window.show_find_dialog)
|
|
181 }
|
18
|
182 edit_menu.add(find)
|
|
183 end
|
6
|
184 menu_bar.add(edit_menu)
|
|
185 end
|
|
186 do
|
2
|
187 local view_menu = new_menu()
|
|
188 view_menu.text = "View"
|
4
|
189 do
|
20
|
190 local word_wrap = new_check_box_menu_item{
|
|
191 text = "Word Wrap"
|
|
192 state = window.text_area.line_wrap
|
|
193 action_listener = function(event)
|
|
194 window.text_area.line_wrap = event.source.state
|
|
195 end
|
|
196 }
|
2
|
197 view_menu.add(word_wrap)
|
4
|
198 end
|
14
|
199 do
|
|
200 local show_whitespace = new_check_box_menu_item()
|
|
201 show_whitespace.text = "Show Whitespace"
|
20
|
202 show_whitespace.add_action_listener(function(_)
|
14
|
203 window.text_area.show_whitespace(show_whitespace.state)
|
|
204 end)
|
|
205 view_menu.add(show_whitespace)
|
|
206 end
|
16
|
207 do
|
|
208 local show_column = new_menu_item()
|
|
209 show_column.text = "Show Cursor Column"
|
20
|
210 show_column.add_action_listener(function(_)
|
16
|
211 show_message_dialog( window.frame, "Cursor Column: "..window.cursor_column() )
|
|
212 end)
|
|
213 view_menu.add(show_column)
|
|
214 end
|
17
|
215 do
|
|
216 local goto = new_menu_item()
|
|
217 goto.text = "Goto Line"
|
|
218 goto.accelerator = "meta G"
|
20
|
219 goto.add_action_listener(function(_)
|
18
|
220 local input = show_input_dialog( window.frame, "Goto line" )
|
17
|
221 --logger.info("input "..input)
|
|
222 local line = input and to_number(input)
|
|
223 if line ~= nil then
|
|
224 window.goto(line)
|
|
225 end
|
|
226 end)
|
|
227 view_menu.add(goto)
|
|
228 end
|
2
|
229 menu_bar.add(view_menu)
|
4
|
230 end
|
0
|
231 return menu_bar
|
|
232 end
|
|
233
|
18
|
234 local function make_find_dialog(window)
|
|
235 local dialog = new_dialog(window.frame)
|
|
236 local root = dialog.component
|
|
237 root.set_layout(new_box_layout(root,"y_axis"))
|
|
238 do
|
|
239 local buttons = new_panel()
|
|
240 buttons.set_layout(new_flow_layout("left"))
|
|
241 --buttons.border = create_empty_border(8,8,8,8)
|
|
242 buttons.border = create_line_border(int_to_color(0))
|
|
243
|
|
244 local find_next = new_button()
|
|
245 find_next.text = "Find Next"
|
|
246 buttons.add(find_next)
|
|
247
|
|
248 local find_prev = new_button()
|
|
249 find_prev.text = "Find Previous"
|
|
250 buttons.add(find_prev)
|
|
251
|
|
252 root.add(buttons)
|
|
253 end
|
|
254 dialog.pack()
|
|
255 local was_shown = false
|
|
256 function window.show_find_dialog()
|
|
257 if not was_shown then
|
|
258 was_shown = true
|
|
259 dialog.move_into_owner()
|
|
260 end
|
|
261 dialog.visible = true
|
|
262 end
|
|
263 end
|
|
264
|
0
|
265 local n_windows = 0
|
5
|
266 local documents = {}
|
0
|
267
|
1
|
268 function new_window(file)
|
5
|
269 local window = {}
|
7
|
270 window.has_file = file~=nil and file.is_file()
|
0
|
271 local frame = new_frame()
|
16
|
272 window.frame = frame
|
5
|
273 local title = file and file.canonical().to_string() or "new"
|
0
|
274 frame.add_close_listener(function()
|
|
275 n_windows = n_windows - 1
|
|
276 if n_windows == 0 then
|
|
277 Luan.exit()
|
|
278 end
|
|
279 end)
|
|
280 local text_area = new_text_area()
|
5
|
281 window.text_area = text_area
|
|
282 if file ~= nil then
|
|
283 local document = documents[title]
|
|
284 if document == nil then
|
|
285 documents[title] = text_area.document
|
|
286 else
|
|
287 text_area.document = document
|
|
288 end
|
|
289 if file.is_file() then
|
|
290 text_area.text = file.read_text()
|
20
|
291 text_area.document.clear_unedited()
|
5
|
292 end
|
1
|
293 end
|
10
|
294 local function set_title()
|
|
295 local s = title
|
|
296 if not text_area.document.is_unedited() then
|
|
297 s = s.." *"
|
|
298 end
|
|
299 frame.title = s
|
|
300 end
|
|
301 set_title()
|
11
|
302 text_area.dont_gc(set_title)
|
10
|
303 text_area.document.add_undo_listener(set_title)
|
0
|
304 text_area.rows = 10
|
|
305 text_area.columns = 20
|
|
306 text_area.wrap_style_word = true
|
|
307 text_area.line_wrap = true
|
|
308 text_area.tab_size = 4
|
|
309 text_area.set_font{ family="Monospaced", size=13 }
|
15
|
310 text_area.set_selection(0)
|
0
|
311 --print(text_area.line_count)
|
|
312 local scroll_pane = new_scroll_pane(text_area)
|
13
|
313 local line_numbers = new_text_area_line_numbers(text_area)
|
|
314 line_numbers.foreground_color = int_to_color(0x888888)
|
|
315 line_numbers.border = create_empty_border(0,8,0,8)
|
|
316 scroll_pane.set_row_header_view(line_numbers)
|
0
|
317 frame.add(scroll_pane)
|
5
|
318 function window.open()
|
|
319 local file_chooser = frame.file_chooser_load()
|
|
320 if file ~= nil then
|
|
321 file_chooser.directory = file.parent()
|
|
322 end
|
|
323 file_chooser.visible = true
|
|
324 local new_file = file_chooser.file
|
|
325 if new_file ~= nil then
|
|
326 new_window(new_file)
|
|
327 end
|
|
328 end
|
|
329 function window.save()
|
|
330 if file == nil then
|
|
331 local file_chooser = frame.file_chooser_save()
|
|
332 file_chooser.visible = true
|
|
333 file = file_chooser.file
|
|
334 if file == nil then
|
7
|
335 return false
|
5
|
336 end
|
|
337 title = file.canonical().to_string()
|
|
338 frame.title = title
|
|
339 documents[title] = text_area.document
|
|
340 end
|
|
341 file.write_text(text_area.text)
|
10
|
342 text_area.document.set_unedited()
|
7
|
343 return true
|
|
344 end
|
|
345 function window.revert()
|
15
|
346 local selection = text_area.get_selection()
|
7
|
347 local text = file.read_text()
|
|
348 text_area.text = text
|
15
|
349 text_area.set_selection(min(selection,#text+1))
|
10
|
350 text_area.document.set_unedited()
|
5
|
351 end
|
15
|
352 local function selection_lines()
|
|
353 local start_seletion, end_selection = text_area.get_selection()
|
|
354 local end_ = end_selection == start_seletion and end_selection or end_selection - 1
|
|
355 local start_line = text_area.get_line_from_position(start_seletion)
|
|
356 local end_line = text_area.get_line_from_position(end_)
|
|
357 local start_pos = text_area.get_line_start_position(start_line)
|
|
358 local end_pos = text_area.get_line_end_position(end_line)
|
|
359 local text = text_area.text
|
|
360 text = sub_string(text,start_pos,end_pos-2)
|
|
361 return {
|
|
362 text = text
|
|
363 start_pos = start_pos
|
|
364 length = #text
|
|
365 lines = end_line - start_line + 1
|
|
366 start_seletion = start_seletion
|
|
367 end_selection = end_selection
|
|
368 }
|
|
369 end
|
|
370 function window.indent()
|
|
371 local r = selection_lines()
|
|
372 local text = r.text
|
|
373 local start_pos = r.start_pos
|
|
374 text = "\t"..replace(text,"\n","\n\t")
|
|
375 text_area.replace(start_pos,r.length,text)
|
|
376 --logger.info(stringify{text_area.get_selection()})
|
|
377 text_area.set_selection( r.start_seletion+1, r.end_selection+r.lines )
|
|
378 end
|
|
379 function window.unindent()
|
|
380 local r = selection_lines()
|
|
381 local text = r.text
|
|
382 text = "\n"..text
|
|
383 local start_seletion = r.start_seletion
|
|
384 if starts_with(text,"\n\t") then
|
|
385 start_seletion = start_seletion - 1
|
|
386 end
|
|
387 local len1 = #text
|
|
388 text = replace(text,"\n\t","\n")
|
|
389 local len2 = #text
|
|
390 local end_selection = r.end_selection - (len1 - len2)
|
|
391 text = sub_string(text,2)
|
|
392 text_area.replace(r.start_pos,r.length,text)
|
|
393 text_area.set_selection(start_seletion,end_selection)
|
|
394 end
|
16
|
395 function window.cursor_column()
|
|
396 local cursor_pos = text_area.get_selection()
|
|
397 local line = text_area.get_line_from_position(cursor_pos)
|
|
398 local start_line_pos = text_area.get_line_start_position(line)
|
|
399 return cursor_pos - start_line_pos + 1
|
|
400 end
|
17
|
401 function window.goto(line)
|
|
402 local pos = text_area.get_line_start_position(line)
|
|
403 text_area.set_selection(pos)
|
|
404 end
|
18
|
405 make_find_dialog(window)
|
1
|
406 local menu_bar = make_menu_bar(window)
|
0
|
407 frame.set_menu_bar(menu_bar)
|
|
408 frame.pack()
|
|
409 frame.visible = true
|
|
410 text_area.request_focus_in_window()
|
|
411 n_windows = n_windows + 1
|
|
412 end
|
|
413
|
1
|
414 Swing.run(function()
|
3
|
415 local args = Luan.arg
|
|
416 if #args == 0 then
|
|
417 new_window()
|
|
418 else
|
|
419 for _, arg in ipairs(args) do
|
|
420 local file = new_file(arg)
|
|
421 new_window(file)
|
|
422 end
|
|
423 end
|
1
|
424 end)
|