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
|
2
|
52 local view_menu = new_menu()
|
|
53 view_menu.text = "View"
|
4
|
54 do
|
2
|
55 local word_wrap = new_check_box_menu_item()
|
|
56 word_wrap.text = "Word Wrap"
|
|
57 word_wrap.state = window.text_area.line_wrap
|
|
58 word_wrap.add_action_listener(function()
|
|
59 window.text_area.line_wrap = word_wrap.state
|
|
60 end)
|
|
61 view_menu.add(word_wrap)
|
4
|
62 end
|
2
|
63 menu_bar.add(view_menu)
|
4
|
64 end
|
0
|
65 return menu_bar
|
|
66 end
|
|
67
|
|
68 local n_windows = 0
|
5
|
69 local documents = {}
|
0
|
70
|
1
|
71 function new_window(file)
|
5
|
72 local window = {}
|
0
|
73 local frame = new_frame()
|
5
|
74 local title = file and file.canonical().to_string() or "new"
|
|
75 frame.title = title
|
0
|
76 frame.add_close_listener(function()
|
|
77 n_windows = n_windows - 1
|
|
78 if n_windows == 0 then
|
|
79 Luan.exit()
|
|
80 end
|
|
81 end)
|
|
82 local text_area = new_text_area()
|
5
|
83 window.text_area = text_area
|
|
84 if file ~= nil then
|
|
85 local document = documents[title]
|
|
86 if document == nil then
|
|
87 documents[title] = text_area.document
|
|
88 else
|
|
89 text_area.document = document
|
|
90 end
|
|
91 if file.is_file() then
|
|
92 text_area.text = file.read_text()
|
|
93 end
|
1
|
94 end
|
0
|
95 text_area.rows = 10
|
|
96 text_area.columns = 20
|
|
97 text_area.wrap_style_word = true
|
|
98 text_area.line_wrap = true
|
|
99 text_area.tab_size = 4
|
|
100 text_area.set_font{ family="Monospaced", size=13 }
|
1
|
101 text_area.select(0,0)
|
0
|
102 --print(text_area.line_count)
|
|
103 local scroll_pane = new_scroll_pane(text_area)
|
|
104 local list = new_list()
|
|
105 list.add_element("1")
|
|
106 list.add_element("2")
|
|
107 list.add_element("3")
|
|
108 --scroll_pane.set_row_header_view(list)
|
|
109 frame.add(scroll_pane)
|
5
|
110 function window.open()
|
|
111 local file_chooser = frame.file_chooser_load()
|
|
112 if file ~= nil then
|
|
113 file_chooser.directory = file.parent()
|
|
114 end
|
|
115 file_chooser.visible = true
|
|
116 local new_file = file_chooser.file
|
|
117 if new_file ~= nil then
|
|
118 new_window(new_file)
|
|
119 end
|
|
120 end
|
|
121 function window.save()
|
|
122 if file == nil then
|
|
123 local file_chooser = frame.file_chooser_save()
|
|
124 file_chooser.visible = true
|
|
125 file = file_chooser.file
|
|
126 if file == nil then
|
|
127 return
|
|
128 end
|
|
129 title = file.canonical().to_string()
|
|
130 frame.title = title
|
|
131 documents[title] = text_area.document
|
|
132 end
|
|
133 file.write_text(text_area.text)
|
|
134 end
|
1
|
135 local menu_bar = make_menu_bar(window)
|
0
|
136 frame.set_menu_bar(menu_bar)
|
|
137 frame.pack()
|
|
138 frame.visible = true
|
|
139 text_area.request_focus_in_window()
|
|
140 n_windows = n_windows + 1
|
|
141 end
|
|
142
|
1
|
143 Swing.run(function()
|
3
|
144 local args = Luan.arg
|
|
145 if #args == 0 then
|
|
146 new_window()
|
|
147 else
|
|
148 for _, arg in ipairs(args) do
|
|
149 local file = new_file(arg)
|
|
150 new_window(file)
|
|
151 end
|
|
152 end
|
1
|
153 end)
|