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