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