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