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