/
codedit_demo.lua
241 lines (200 loc) · 6.17 KB
/
codedit_demo.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
local player = require'cplayer'
local glue = require'glue'
local pp = require'pp'
local str = require'codedit'.str
local boxlayout = require'cplayer.boxlayout'
local editors = {}
local loaded
local root_dir = 'x:/luapower'
local font_files = {
root_dir .. '/FSEX300.ttf',
root_dir .. '/DejaVuSerif.ttf',
}
filename = root_dir .. '/codedt_demo.lua'
--text = '\tx\ty\tz\n\ta\tb'
--text = ' x y z\r\n a b\n\tc\td'
--text = glue.readfile'c:/temp.c'
--text = glue.readfile'c:/temp2.c'
text = ('hello world\r\n'):rep(100)
text = [==[
bbbbbb
--search forwards for:
--1) 1..n spaces followed by a non-space char
--2) 1..n non-space chars follwed by case 1
--3) 1..n word chars followed by a non-word char
--4) 1..n non-word chars followed by a word char
--if the next break should be on a different line, return nil.
function str.next_word_break_char(s, i, word_chars)
i = i or 0
assert(i >= 0)
if i == 0 then return 1 end
if i >= #s then return end
if str.isterm(s, i) then return end
local expect =
str.iswhitespace(s, i) and 'space'
or str.isword(s, i, word_chars) and 'word'
or 'nonword'
for i in str.chars(s, i) do
if str.isterm(s, i) then return end
if expect == 'space' then --case 1
if not str.iswhitespace(s, i) then --case 1 exit
return i
end
elseif str.iswhitespace(s, i) then --case 2 -> case 1
expect = 'space'
elseif
expect ~= (str.isword(s, i, word_chars) and 'word' or 'nonword')
then --case 3 and 4 exit
return i
end
end
return str.next_char(s, i)
end
]==] .. text
--text = glue.readfile(root_dir .. 'codedit.lua')
player.show_magnifier = false
player.y = 300
function player:on_render(cr)
if self.window.w ~= 800 then
--self.window.w = 800
--return
end
local editor_y = 40
local n = 2
local nav_w = 120
self.layout.default_w = nav_w
self.layout.default_h = 22
for i = 2, n do
local w = math.floor(self.w / n)
local h = self.h - editor_y - 20
local x = (i - 1) * w + 20
self.layout:push(x, 0)
local created = editors[i] and true or false
local editor = editors[i] or {
id = 'code_editor_' .. i,
filename = filename,
view = {
x = x, y = editor_y, w = w, h = h,
eol_markers = false, minimap = false, line_numbers = false,
_font_file = font_files[1],
_font_size = 16,
tabsize = 4,
},
text = text,
buffer = {
multiline = i > 1,
},
cursor = {
restrict_eof = false,
restrict_eol = false,
land_bof = false,
land_eof = false,
},
}
editor = self:code_editor(editor)
if not created then
editors[i] = editor
end
editor.view.x = nav_w + 10 + x
editor.view.y = editor_y
editor.view.w = w - nav_w - 40
editor.view.h = h
local s = editor.selection:isempty()
and editor.cursor.line .. ' : ' .. editor.cursor.i
or editor.selection.line1 .. ' : ' ..editor.selection.i1 .. ' - ' ..
editor.selection.line2 .. ' : ' ..editor.selection.i2
self:label{y = 10, text = s}
local s = editor.cursor.line <= #editor.buffer.lines
and editor.buffer.lines[editor.cursor.line] or ''
local i1 = editor.cursor.i
local eol = i1 >= (editor.buffer:eol(editor.cursor.line) or 1)
local i2 = not eol and str.next_char(s, i1) or #s + 1
local s = s:sub(i1, i2 - 1)
local s = pp.format(s):sub(2, -2)
self:label{x = 100, y = 10, text = s}
self:label{y = 24, text = editor.cursor.x, w = 0, h = 26}
self.layout:move(0, 40, 'vert')
--[[
self.layout:push(0, 0, 'h', nav_w / 3)
editor.cursor.restrict_eol = self:togglebutton{
id = 'restrict_eol' .. i,
text = 'restrict_eol', selected = editor.cursor.restrict_eol,
}
editor.cursor.restrict_eof = self:togglebutton{
id = 'restrict_eof' .. i,
text = 'restrict_eof', selected = editor.cursor.restrict_eof,
}
editor.cursor.land_bof = self:togglebutton{
id = 'land_bof' .. i,
text = 'land_bof', selected = editor.cursor.land_bof,
}
self.layout:pop()
]]
editor.cursor.land_eof = self:togglebutton{
id = 'land_eof' .. i,
text = 'land_eof', selected = editor.cursor.land_eof,
}
self:label{text = 'jump_tabstops'}
editor.cursor.jump_tabstops = self:mbutton{
id = 'jump_tabstops' .. i,
values = {'always', 'indent', 'never'}, selected = editor.cursor.jump_tabstops,
}
self:label{text = 'delete_tabstops'}
editor.cursor.delete_tabstops = self:mbutton{
id = 'delete_tabstops' .. i,
values = {'always', 'indent', 'never'}, selected = editor.cursor.delete_tabstops,
}
self:label{text = 'insert_tabs'}
editor.cursor.insert_tabs = self:mbutton{
id = 'insert_tabs' .. i,
values = {'always', 'indent', 'never'}, selected = editor.cursor.insert_tabs,
}
local font_file1 = editor.view:font_file()
local font_file = self:mbutton{
id = 'font_file' .. i,
values = font_files,
texts = {'Fixedsys', 'DejaVuSerif'},
selected = editor.view:font_file(),
}
if font_file ~= font_file1 then
editor.view:font_file(font_file)
end
editor.view.tabsize = self:slider{
text = 'tabsize',
id = 'tabsize' .. i,
i0 = 1, i1 = 16, i = editor.view.tabsize,
}
local font_size1 = editor.view:font_size()
local font_size = self:slider{
text = 'fontsize',
id = 'fontsize' .. i,
i0 = 6, i1 = 32, i = editor.view:font_size(),
}
if font_size ~= font_size1 then
editor.view:font_size(font_size)
end
editor.view.lang = self:mbutton{
id = 'lexer_' .. i,
x = x + 200 + nav_w, y = 10, w = 180, values = {'none', 'cpp', 'lua'},
selected = editor.view.lang or 'none',
}
editor.view.lang = editor.view.lang ~= 'none' and editor.view.lang or nil
editor.eol_markers = self:togglebutton{id = 'eol_markers'..i,
text = 'eol_markers',
selected = editor.eol_markers}
local s = editor.undo_stack.undo_group
and (editor.undo_stack.undo_group.type .. '\n\n') or ''
for i,g in ipairs(editor.undo_stack.undo_stack) do
s = s .. g.type .. '\n'
end
self:label{font_face = 'Fixedsys', text = s}
self.layout:pop()
end
--[[
v.linesize = self:slider{id = 'linesize', x = 10, y = 40, w = 80, h = 24, i0 = 10, i1 = 30, i = v.linesize}
b.line_terminator = self:mbutton{id = 'term', x = 10, y = 70, w = 80, h = 24,
values = {'\r\n', '\r', '\n'}, texts = {['\r\n'] = 'CRLF', ['\n'] = 'LF', ['\r'] = 'CR'},
selected = b.line_terminator}
]]
end
player:play()