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