0
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
3
|
3 local ipairs = Luan.ipairs or error()
|
0
|
4 local Io = require "luan:Io.luan"
|
|
5 local print = Io.print or error()
|
3
|
6 local new_file = Io.schemes.file or error()
|
7
|
7 local Math = require "luan:Math.luan"
|
|
8 local min = Math.min or error()
|
0
|
9 local Swing = require "luan:swing/Swing.luan"
|
|
10 local new_frame = require("luan:swing/Frame.luan").new or error()
|
|
11 local new_label = require("luan:swing/Label.luan").new or error()
|
|
12 local new_text_area = require("luan:swing/Text_area.luan").new or error()
|
|
13 local new_scroll_pane = require("luan:swing/Scroll_pane.luan").new or error()
|
|
14 local new_list = require("luan:swing/List.luan").new or error()
|
|
15 local new_menu_bar = require("luan:swing/Menu_bar.luan").new or error()
|
|
16 local new_menu = require("luan:swing/Menu.luan").new or error()
|
|
17 local new_menu_item = require("luan:swing/Menu_item.luan").new or error()
|
2
|
18 local new_check_box_menu_item = require("luan:swing/Check_box_menu_item.luan").new or error()
|
0
|
19 local Logging = require "luan:logging/Logging.luan"
|
|
20 local logger = Logging.logger "editor"
|
|
21
|
|
22
|
|
23 local new_window
|
|
24
|
1
|
25 local function make_menu_bar(window)
|
7
|
26 local revert
|
0
|
27 local menu_bar = new_menu_bar()
|
4
|
28 do
|
1
|
29 local file_menu = new_menu()
|
|
30 file_menu.text = "File"
|
4
|
31 do
|
1
|
32 local new_file = new_menu_item()
|
|
33 new_file.text = "New File"
|
|
34 new_file.accelerator = "meta N"
|
5
|
35 new_file.add_action_listener(new_window)
|
1
|
36 file_menu.add(new_file)
|
4
|
37 end
|
|
38 do
|
1
|
39 local open = new_menu_item()
|
|
40 open.text = "Open..."
|
|
41 open.accelerator = "meta O"
|
5
|
42 open.add_action_listener(window.open)
|
1
|
43 file_menu.add(open)
|
4
|
44 end
|
|
45 do
|
|
46 local save = new_menu_item()
|
|
47 save.text = "Save"
|
|
48 save.accelerator = "meta S"
|
7
|
49 save.add_action_listener(function()
|
|
50 if window.save() then
|
|
51 revert.set_enabled(true)
|
|
52 end
|
|
53 end)
|
4
|
54 file_menu.add(save)
|
|
55 end
|
7
|
56 do
|
|
57 revert = new_menu_item()
|
|
58 revert.text = "Revert"
|
|
59 revert.set_enabled(window.has_file)
|
|
60 revert.add_action_listener(window.revert)
|
|
61 file_menu.add(revert)
|
|
62 end
|
1
|
63 menu_bar.add(file_menu)
|
4
|
64 end
|
|
65 do
|
6
|
66 local edit_menu = new_menu()
|
|
67 edit_menu.text = "Edit"
|
|
68 do
|
|
69 local cut = new_menu_item()
|
|
70 cut.text = "Cut"
|
|
71 cut.accelerator = "meta X"
|
|
72 cut.add_action_listener(window.text_area.cut)
|
|
73 edit_menu.add(cut)
|
|
74 end
|
|
75 do
|
|
76 local copy = new_menu_item()
|
|
77 copy.text = "Copy"
|
|
78 copy.accelerator = "meta C"
|
|
79 copy.add_action_listener(window.text_area.copy)
|
|
80 edit_menu.add(copy)
|
|
81 end
|
|
82 do
|
|
83 local paste = new_menu_item()
|
|
84 paste.text = "Paste"
|
|
85 paste.accelerator = "meta V"
|
|
86 paste.add_action_listener(window.text_area.paste)
|
|
87 edit_menu.add(paste)
|
|
88 end
|
|
89 edit_menu.add_separator()
|
|
90 do
|
|
91 local select_all = new_menu_item()
|
|
92 select_all.text = "Select All"
|
|
93 select_all.accelerator = "meta A"
|
|
94 select_all.add_action_listener(window.text_area.select_all)
|
|
95 edit_menu.add(select_all)
|
|
96 end
|
|
97 menu_bar.add(edit_menu)
|
|
98 end
|
|
99 do
|
2
|
100 local view_menu = new_menu()
|
|
101 view_menu.text = "View"
|
4
|
102 do
|
2
|
103 local word_wrap = new_check_box_menu_item()
|
|
104 word_wrap.text = "Word Wrap"
|
|
105 word_wrap.state = window.text_area.line_wrap
|
|
106 word_wrap.add_action_listener(function()
|
|
107 window.text_area.line_wrap = word_wrap.state
|
|
108 end)
|
|
109 view_menu.add(word_wrap)
|
4
|
110 end
|
2
|
111 menu_bar.add(view_menu)
|
4
|
112 end
|
0
|
113 return menu_bar
|
|
114 end
|
|
115
|
|
116 local n_windows = 0
|
5
|
117 local documents = {}
|
0
|
118
|
1
|
119 function new_window(file)
|
5
|
120 local window = {}
|
7
|
121 window.has_file = file~=nil and file.is_file()
|
0
|
122 local frame = new_frame()
|
5
|
123 local title = file and file.canonical().to_string() or "new"
|
|
124 frame.title = title
|
0
|
125 frame.add_close_listener(function()
|
|
126 n_windows = n_windows - 1
|
|
127 if n_windows == 0 then
|
|
128 Luan.exit()
|
|
129 end
|
|
130 end)
|
|
131 local text_area = new_text_area()
|
5
|
132 window.text_area = text_area
|
|
133 if file ~= nil then
|
|
134 local document = documents[title]
|
|
135 if document == nil then
|
|
136 documents[title] = text_area.document
|
|
137 else
|
|
138 text_area.document = document
|
|
139 end
|
|
140 if file.is_file() then
|
|
141 text_area.text = file.read_text()
|
|
142 end
|
1
|
143 end
|
0
|
144 text_area.rows = 10
|
|
145 text_area.columns = 20
|
|
146 text_area.wrap_style_word = true
|
|
147 text_area.line_wrap = true
|
|
148 text_area.tab_size = 4
|
|
149 text_area.set_font{ family="Monospaced", size=13 }
|
7
|
150 text_area.caret_position = 0
|
0
|
151 --print(text_area.line_count)
|
|
152 local scroll_pane = new_scroll_pane(text_area)
|
|
153 local list = new_list()
|
|
154 list.add_element("1")
|
|
155 list.add_element("2")
|
|
156 list.add_element("3")
|
|
157 --scroll_pane.set_row_header_view(list)
|
|
158 frame.add(scroll_pane)
|
5
|
159 function window.open()
|
|
160 local file_chooser = frame.file_chooser_load()
|
|
161 if file ~= nil then
|
|
162 file_chooser.directory = file.parent()
|
|
163 end
|
|
164 file_chooser.visible = true
|
|
165 local new_file = file_chooser.file
|
|
166 if new_file ~= nil then
|
|
167 new_window(new_file)
|
|
168 end
|
|
169 end
|
|
170 function window.save()
|
|
171 if file == nil then
|
|
172 local file_chooser = frame.file_chooser_save()
|
|
173 file_chooser.visible = true
|
|
174 file = file_chooser.file
|
|
175 if file == nil then
|
7
|
176 return false
|
5
|
177 end
|
|
178 title = file.canonical().to_string()
|
|
179 frame.title = title
|
|
180 documents[title] = text_area.document
|
|
181 end
|
|
182 file.write_text(text_area.text)
|
7
|
183 return true
|
|
184 end
|
|
185 function window.revert()
|
|
186 local caret_position = text_area.caret_position
|
|
187 local text = file.read_text()
|
|
188 text_area.text = text
|
|
189 text_area.caret_position = min(caret_position,#text)
|
5
|
190 end
|
1
|
191 local menu_bar = make_menu_bar(window)
|
0
|
192 frame.set_menu_bar(menu_bar)
|
|
193 frame.pack()
|
|
194 frame.visible = true
|
|
195 text_area.request_focus_in_window()
|
|
196 n_windows = n_windows + 1
|
|
197 end
|
|
198
|
1
|
199 Swing.run(function()
|
3
|
200 local args = Luan.arg
|
|
201 if #args == 0 then
|
|
202 new_window()
|
|
203 else
|
|
204 for _, arg in ipairs(args) do
|
|
205 local file = new_file(arg)
|
|
206 new_window(file)
|
|
207 end
|
|
208 end
|
1
|
209 end)
|