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