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