37
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local stringify = Luan.stringify or error()
|
|
4 local Math = require "luan:Math.luan"
|
|
5 local min = Math.min or error()
|
|
6 local String = require "luan:String.luan"
|
|
7 local sub_string = String.sub or error()
|
|
8 local replace = String.replace or error()
|
|
9 local starts_with = String.starts_with or error()
|
|
10 local Io = require "luan:Io.luan"
|
|
11 local new_text_area = require("luan:swing/Text_area.luan").new or error()
|
|
12 local new_frame = require("luan:swing/Frame.luan").new or error()
|
41
|
13 local new_dialog = require("luan:swing/Dialog.luan").new or error()
|
37
|
14 local new_panel = require("luan:swing/Component.luan").new_panel or error()
|
|
15 local Layout = require "luan:swing/Layout.luan"
|
|
16 local new_mig_layout = Layout.new_mig_layout or error()
|
|
17 local new_scroll_pane = require("luan:swing/Scroll_pane.luan").new or error()
|
|
18 local new_text_area_line_numbers = require("luan:swing/Text_area_line_numbers.luan").new or error()
|
|
19 local int_to_color = require("luan:swing/Color.luan").int_to_color or error()
|
|
20 local Border = require "luan:swing/Border.luan"
|
|
21 local create_empty_border = Border.create_empty_border or error()
|
|
22 local new_label = require("luan:swing/Label.luan").new or error()
|
|
23 local make_find_panel = require "classpath:luan_editor/find.luan"
|
|
24 local add_menu_bar = require "classpath:luan_editor/menu.luan"
|
|
25 local Swing = require "luan:swing/Swing.luan"
|
44
|
26 local run_later = Swing.run_later or error()
|
37
|
27 local File_chooser = require "luan:swing/File_chooser.luan"
|
|
28 local choose_file = File_chooser.awt_choose_file or error()
|
39
|
29 local Option_pane = require "luan:swing/Option_pane.luan"
|
|
30 local show_message_dialog = Option_pane.show_message_dialog or error()
|
41
|
31 local new_radio_button = require("luan:swing/Radio_button.luan").new or error()
|
|
32 local new_button_group = require("luan:swing/Button_group.luan").new or error()
|
37
|
33 local Logging = require "luan:logging/Logging.luan"
|
|
34 local logger = Logging.logger "editor/window"
|
|
35
|
|
36
|
|
37 local n_windows = 0
|
|
38 local documents = {}
|
|
39
|
|
40 local function bool(val,default)
|
|
41 if val ~= nil then
|
|
42 return val
|
|
43 else
|
|
44 return default
|
|
45 end
|
|
46 end
|
|
47
|
|
48 local config_file = Io.uri("file:"..Swing.home_dir.."/.luan_editor")
|
|
49 local config = {}
|
|
50 if config_file.exists() then
|
|
51 try
|
|
52 config = Luan.parse(config_file.read_text())
|
|
53 catch e
|
|
54 logger.error(e)
|
|
55 end
|
|
56 end
|
|
57 config.size = config.size or { width=700, height=700 }
|
|
58 config.line_wrap = bool( config.line_wrap, true )
|
|
59 config.whitespace_visible = bool( config.whitespace_visible, false )
|
|
60 config.tab_size = config.tab_size or 4
|
|
61
|
|
62 local function save_config()
|
|
63 config_file.write_text( stringify(config).."\n" )
|
|
64 end
|
|
65
|
41
|
66 local list_view = new_panel{
|
|
67 --layout = new_mig_layout("insets 0,wrap,fill")
|
44
|
68 layout = new_mig_layout("wrap","[grow]")
|
41
|
69 }
|
|
70 local list_scroll_pane = new_scroll_pane{
|
|
71 view = list_view
|
|
72 }
|
|
73 local list_window = new_dialog{
|
44
|
74 --preferred_size = { width=200, height=400 }
|
|
75 size = { width=200, height=400 }
|
|
76 --maximum_size = { width=200, height=400 }
|
41
|
77 content_pane = list_scroll_pane
|
43
|
78 focusable_window_state = false
|
41
|
79 }
|
44
|
80 local function show_list_window()
|
|
81 list_window.visible = true
|
|
82 list_scroll_pane.scroll_to_right()
|
|
83 end
|
|
84
|
41
|
85 local list_group = new_button_group()
|
42
|
86
|
41
|
87 local function new_list_window_item(text)
|
|
88 local rb = new_radio_button{
|
|
89 constraints = "grow"
|
|
90 text = text
|
|
91 horizontal_text_position = "left"
|
|
92 horizontal_alignment = "right"
|
|
93 }
|
|
94 list_view.add(rb)
|
|
95 list_group.add(rb)
|
|
96 list_scroll_pane.scroll_to_right()
|
44
|
97 list_window.validate()
|
41
|
98 return rb
|
|
99 end
|
42
|
100 local function remove_list_window_item(list_window_item)
|
|
101 list_view.remove(list_window_item)
|
|
102 list_group.remove(list_window_item)
|
44
|
103 list_window.validate()
|
42
|
104 end
|
41
|
105
|
44
|
106 local black = int_to_color(0x000000)
|
|
107 local blue = int_to_color(0x0000FF)
|
|
108 local grey = int_to_color(0x888888)
|
|
109
|
38
|
110 local function new_window(file,document)
|
37
|
111 local window = {}
|
39
|
112 if file == nil or not file.exists() then
|
|
113 window.has_file = false
|
|
114 elseif file.is_file() then
|
|
115 window.has_file = true
|
|
116 else
|
|
117 show_message_dialog(nil,"Not a file")
|
|
118 if n_windows == 0 then
|
|
119 Luan.exit()
|
|
120 else
|
|
121 return
|
|
122 end
|
|
123 end
|
|
124 window.has_file = file~=nil and file.exists()
|
37
|
125 local text_area = new_text_area{
|
|
126 wrap_style_word = true
|
|
127 line_wrap = config.line_wrap
|
|
128 whitespace_visible = config.whitespace_visible
|
|
129 tab_size = config.tab_size
|
|
130 font = { family="Monospaced", size=13 }
|
|
131 }
|
|
132 window.text_area = text_area
|
|
133 local title = file and file.canonical().to_string() or "new"
|
38
|
134 if document ~= nil then
|
|
135 text_area.document = document
|
|
136 elseif file ~= nil then
|
37
|
137 local document = documents[title]
|
39
|
138 if document ~= nil then
|
37
|
139 text_area.document = document
|
39
|
140 else
|
|
141 documents[title] = text_area.document
|
|
142 if file.exists() then
|
|
143 text_area.text = file.read_text()
|
|
144 text_area.document.clear_unedited()
|
|
145 end
|
37
|
146 end
|
|
147 end
|
|
148 text_area.set_selection(0)
|
44
|
149 local list_window_item = new_list_window_item(title)
|
|
150 list_window_item.add_action_listener(function(_)
|
|
151 text_area.request_focus()
|
|
152 end)
|
|
153 --window.list_window_item = list_window_item
|
37
|
154 local status_bar = new_label{
|
|
155 constraints = "span,growx"
|
|
156 text = " "
|
|
157 border = create_empty_border(2,16,4,16)
|
|
158 }
|
|
159 window.status_bar = status_bar
|
|
160 local find_panel = make_find_panel(window)
|
|
161 local frame = new_frame{
|
|
162 preferred_size = config.size
|
|
163 content_pane = new_panel{
|
|
164 layout = new_mig_layout("insets 0,wrap,fill,hidemode 3","","[][grow 0]")
|
|
165 children = {
|
|
166 new_scroll_pane{
|
|
167 constraints = "grow"
|
|
168 view = text_area
|
|
169 row_header_view = new_text_area_line_numbers{
|
|
170 text_area = text_area
|
44
|
171 foreground_color = grey
|
37
|
172 border = create_empty_border(0,8,0,8)
|
|
173 }
|
|
174 }
|
|
175 find_panel
|
|
176 status_bar
|
|
177 }
|
|
178 }
|
|
179 }
|
|
180 window.frame = frame
|
|
181 frame.add_close_listener(function()
|
|
182 n_windows = n_windows - 1
|
|
183 if n_windows == 0 then
|
|
184 Luan.exit()
|
|
185 end
|
44
|
186 remove_list_window_item(list_window_item)
|
37
|
187 end)
|
43
|
188 frame.add_window_focus_listener(function()
|
44
|
189 list_window_item.is_selected = true
|
|
190 list_window_item.scroll_into_view_vertically()
|
43
|
191 end)
|
37
|
192 frame.add_resize_stopped_listener( 200, function()
|
|
193 --logger.info(stringify(frame.size))
|
|
194 config.size = frame.size
|
|
195 save_config()
|
|
196 end)
|
|
197 frame.add_move_stopped_listener( 200, function()
|
|
198 --logger.info(stringify(frame.location))
|
|
199 config.location = frame.location
|
|
200 save_config()
|
|
201 end)
|
44
|
202 local function undo_listener()
|
|
203 local is_unedited = text_area.document.is_unedited()
|
|
204 if window.is_unedited == is_unedited then
|
|
205 return
|
|
206 end
|
|
207 window.is_unedited = is_unedited
|
37
|
208 local s = title
|
44
|
209 if not is_unedited then
|
37
|
210 s = s.." *"
|
|
211 end
|
|
212 frame.title = s
|
44
|
213 list_window_item.foreground_color = is_unedited and black or blue
|
37
|
214 end
|
44
|
215 undo_listener()
|
|
216 --window.undo_listener = undo_listener -- dont gc
|
|
217 text_area.document.add_undo_listener(undo_listener)
|
37
|
218 window.new = new_window
|
|
219 function window.open()
|
|
220 local new_file = choose_file{
|
|
221 action = "load"
|
|
222 parent = frame
|
|
223 directory = file and file.parent()
|
|
224 }
|
|
225 if new_file ~= nil then
|
|
226 new_window(new_file)
|
|
227 end
|
|
228 end
|
|
229 function window.save()
|
|
230 if file == nil then
|
|
231 file = choose_file{
|
|
232 action = "save"
|
|
233 parent = frame
|
|
234 }
|
|
235 if file == nil then
|
|
236 return false
|
|
237 end
|
|
238 title = file.canonical().to_string()
|
|
239 frame.title = title
|
|
240 documents[title] = text_area.document
|
|
241 end
|
39
|
242 try
|
|
243 file.write_text(text_area.text)
|
|
244 catch e
|
|
245 show_message_dialog( frame, e.get_message() )
|
|
246 return false
|
|
247 end
|
37
|
248 text_area.document.set_unedited()
|
|
249 return true
|
|
250 end
|
|
251 function window.revert()
|
|
252 local selection = text_area.get_selection()
|
|
253 local text = file.read_text()
|
|
254 text_area.text = text
|
|
255 text_area.set_selection(min(selection,#text+1))
|
|
256 text_area.document.set_unedited()
|
|
257 status_bar.text = "Reverted"
|
|
258 end
|
|
259 local function selection_lines()
|
|
260 local start_seletion, end_selection = text_area.get_selection()
|
|
261 local end_ = end_selection == start_seletion and end_selection or end_selection - 1
|
|
262 local start_line = text_area.get_line_from_position(start_seletion)
|
|
263 local end_line = text_area.get_line_from_position(end_)
|
|
264 local start_pos = text_area.get_line_start_position(start_line)
|
|
265 local end_pos = text_area.get_line_end_position(end_line)
|
|
266 local text = text_area.text
|
|
267 text = sub_string(text,start_pos,end_pos-2)
|
|
268 return {
|
|
269 text = text
|
|
270 start_pos = start_pos
|
|
271 length = #text
|
|
272 lines = end_line - start_line + 1
|
|
273 start_seletion = start_seletion
|
|
274 end_selection = end_selection
|
|
275 }
|
|
276 end
|
|
277 function window.indent()
|
|
278 local r = selection_lines()
|
|
279 local text = r.text
|
|
280 local start_pos = r.start_pos
|
|
281 text = "\t"..replace(text,"\n","\n\t")
|
|
282 text_area.replace(start_pos,r.length,text)
|
|
283 --logger.info(stringify{text_area.get_selection()})
|
|
284 text_area.set_selection( r.start_seletion+1, r.end_selection+r.lines )
|
|
285 end
|
|
286 function window.unindent()
|
|
287 local r = selection_lines()
|
|
288 local text = r.text
|
|
289 text = "\n"..text
|
|
290 local start_seletion = r.start_seletion
|
|
291 if starts_with(text,"\n\t") then
|
|
292 start_seletion = start_seletion - 1
|
|
293 end
|
|
294 local len1 = #text
|
|
295 text = replace(text,"\n\t","\n")
|
|
296 local len2 = #text
|
|
297 local end_selection = r.end_selection - (len1 - len2)
|
|
298 text = sub_string(text,2)
|
|
299 text_area.replace(r.start_pos,r.length,text)
|
|
300 text_area.set_selection(start_seletion,end_selection)
|
|
301 end
|
|
302 function window.cursor_column()
|
|
303 local cursor_pos = text_area.get_selection()
|
|
304 local line = text_area.get_line_from_position(cursor_pos)
|
|
305 local start_line_pos = text_area.get_line_start_position(line)
|
|
306 return cursor_pos - start_line_pos + 1
|
|
307 end
|
|
308 function window.goto(line)
|
|
309 local pos = text_area.get_line_start_position(line)
|
|
310 text_area.set_selection(pos)
|
|
311 end
|
|
312 function window.set_line_wrap(line_wrap)
|
|
313 text_area.line_wrap = line_wrap
|
|
314 config.line_wrap = line_wrap
|
|
315 save_config()
|
|
316 end
|
|
317 function window.set_whitespace_visible(whitespace_visible)
|
|
318 text_area.whitespace_visible = whitespace_visible
|
|
319 config.whitespace_visible = whitespace_visible
|
|
320 save_config()
|
|
321 end
|
|
322 function window.set_tab_size(tab_size)
|
|
323 text_area.tab_size = tab_size
|
|
324 config.tab_size = tab_size
|
|
325 save_config()
|
|
326 end
|
38
|
327 function window.duplicate()
|
|
328 local new = new_window(file,text_area.document)
|
|
329 new.text_area.set_selection( text_area.get_selection() )
|
|
330 end
|
44
|
331 window.show_list_window = show_list_window
|
37
|
332 add_menu_bar(window)
|
|
333 frame.pack()
|
|
334 if config.location ~= nil then
|
|
335 frame.location = config.location
|
|
336 end
|
|
337 frame.visible = true
|
|
338 text_area.request_focus_in_window()
|
|
339 n_windows = n_windows + 1
|
38
|
340 return window
|
37
|
341 end
|
|
342
|
|
343 return new_window
|