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