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