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