forked from jugglerchris/textadept-vi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vi_entry.lua
408 lines (373 loc) · 11.9 KB
/
vi_entry.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
local M = {
MAX_COMPLETION_LINES = 5
}
local redux = require'textredux'
-- Define a style for highlighting completions
redux.core.style.string_hl = redux.core.style.string .. { back="#FFFFFF" }
local function ve_refresh(buf)
-- Disable all margins, except one we'll use for the :
buf.margin_width_n[0] = 0
buf.margin_width_n[1] = 0
buf.margin_width_n[2] = 0
buf.margin_width_n[3] = 0
buf.margin_width_n[4] = #buf.data.prompt
buf.margin_type_n[4] = buf.MARGIN_TEXT
buf.margin_text[0] = buf.data.prompt
buf.margin_style[0] = 8 -- error (red)
buf:add_text(buf.data.text, redux.core.style.comment)
buf:goto_pos(buf.data.pos)
local linesize = CURSES and 1 or 20
local offset = CURSES and 4 or 100
if buf.data.completions then
local cs = buf.data.completions
local comp_offset = buf.data.completions_offset
local count = #buf.data.completions - comp_offset
buf:append_text('\n')
if count > 10 then count = 10 end
for i=1,count do
local comp_idx = i+comp_offset
local style = (comp_idx == buf.data.completions_sel) and
redux.core.style.string_hl or redux.core.style.string
buf:append_text(cs[comp_idx].."\n", style)
end
local lines = buf.line_count
if lines > M.MAX_COMPLETION_LINES+1 then lines = M.MAX_COMPLETION_LINES+1 end
view.size = ui.size[2] - offset - linesize * (lines-1)
else
-- go back to one line if necessary
view.size = ui.size[2] - offset
end
end
-- Return a saved version of splits so that we can regenerate them.
local function save_splits(splits)
if splits[1] and splits[2] then
-- it's a split
splits[1] = save_splits(splits[1])
splits[2] = save_splits(splits[2])
return splits
else
local curview = view
ui.goto_view(_VIEWS[splits])
local result = {
buffer=splits.buffer,
current=(splits == curview),
pos=splits.buffer.current_pos,
anchor=splits.buffer.anchor,
firstline = splits.buffer.first_visible_line,
}
ui.goto_view(_VIEWS[curview])
return result
end
end
local function save_views()
local split_views = ui:get_split_table()
return save_splits(split_views)
end
local function unsplit_all()
while #_VIEWS > 1 do
view:unsplit()
end
end
-- Returns the view which should be current, or nil
local function restore_into(v, saved)
if saved[1] and saved[2] then
local cur1, cur2
-- restore the split
local first, second = v:split(saved.vertical)
first.size = saved.size
cur1 = restore_into(first, saved[1])
cur2 = restore_into(second, saved[2])
-- return current if found in theis branch
return cur1 or cur2
else
local buf = saved.buffer
if _BUFFERS[buf] then
ui.goto_view(_VIEWS[v])
v:goto_buffer(_BUFFERS[buf])
buffer.first_visible_line = saved.firstline
buffer.current_pos = saved.pos
buffer.anchor = saved.anchor
if saved.current then return v end
else
ui.print("Buffer not found:", buf.filename, buf)
for k,v in pairs(_BUFFERS) do
if type(v) == 'table' then
ui.print(k,v, v.filename)
else
ui.print(k,v)
end
end
end
end
end
-- Restore split state, but with the current buffer on the bottom line.
local function restore_saved(saved)
local old, new = view:split()
old.size = ui.size[2] - 4
local cur = restore_into(old,saved)
return new, cur
end
local function common_prefix(s1, s2)
local len = #s1
if #s2 < len then len = #s2 end
local prefixlen = 0
for i=1,len do
if s1:sub(i,i) == s2:sub(i,i) then
prefixlen = i
else
break
end
end
return s1:sub(1, prefixlen)
end
-- Replace the current word
local function replace_word(buf, repl)
local t = buf.data.text
local pos = buf.data.pos
local preceding = t:sub(1, pos)
local startpos, to_complete, endpos = preceding:match("^.-()(%S*)()$")
t = t:sub(1, startpos-1) .. repl .. t:sub(endpos)
buf.data.text = t
buf.data.pos = startpos + #repl - 1
buf:refresh()
end
-- expand=nil/false means only show completions, don't update buffer.
local function complete_now(expand)
local buf = buffer._textredux
if not buf.data.complete then
return
end
buf.data.completions = nil
buf.data.completions_sel = 0
local t = buf.data.text
local pos = buf.data.pos
local preceding = t:sub(1, pos)
local startpos, to_complete, endpos = preceding:match("^.-()(%S*)()$")
local first_word = t:match("^(%S*)")
local completions = buf.data.complete(to_complete, first_word) or {}
if #completions == 1 and expand then
local repl = completions[1]
replace_word(buf, repl)
elseif #completions >= 1 then
-- See if there's a common prefix
local prefix = ""
if expand then
prefix = completions[1]
for i=2,#completions do
prefix = common_prefix(prefix, completions[i])
if #prefix == 0 then break end
end
end
if #prefix > #to_complete then
replace_word(buf, prefix)
else
-- No common prefix, so show completions.
buf.data.completions = completions
buf.data.completions_offset = 0
buf:refresh()
end
end
end
-- Show the next N completions
local function complete_advance()
local buf = buffer._textredux
local offset = buf.data.completions_offset
local completions = buf.data.completions
offset = offset + M.MAX_COMPLETION_LINES
if offset >= #completions then
offset = 0
end
buf.data.completions_offset = offset
-- If the user has started selecting completions, move it along.
if buf.data.completions_sel and buf.data.completions_sel > 0 then
buf.data.completions_sel = buf.data.completions_offset + 1
end
buf:refresh()
end
local function do_enter()
local buf = buffer._textredux
if buf.data.completions_sel and buf.data.completions_sel ~= 0 then
-- We've selected a completion.
local chosen = buf.data.completions[buf.data.completions_sel]
buf.data.completions = nil
buf.data.completions_sel = 0
replace_word(buf, chosen)
else
local saved = buf.data.saved
local cmd = buf:get_text()
local handler = buf.data.handler
local hist = buf.data.context._history
local histsaveidx = buf.data.histsaveidx
buf:close()
unsplit_all()
local newcur = restore_into(view,saved)
if newcur and _VIEWS[newcur] then
ui.goto_view(_VIEWS[newcur])
end
-- Save the command in the history
hist[histsaveidx] = cmd
handler(cmd)
end
end
local ve_keys = {
['\t'] = function()
local buf = buffer._textredux
if buf.data.completions ~= nil and #buf.data.completions > 1 then
complete_advance()
else
complete_now(true)
end
end,
['\b'] = function()
local buf = buffer._textredux
local t = buf.data.text
local pos = buffer.current_pos
if pos >= 1 then
t = t:sub(1, pos-1) .. t:sub(pos+1, -1)
buf.data.text = t
buf.data.pos = pos - 1
buf:refresh()
end
end,
cu = function()
-- Clear to start of line
local buf = buffer._textredux
local t = buf.data.text
local pos = buffer.current_pos
if pos > 0 then
t = t:sub(pos+1, -1)
buf.data.text = t
buf.data.pos = 0
buf:refresh()
end
end,
esc = function()
local buf = buffer._textredux
local saved = buf.data.saved
buf:close()
unsplit_all()
local newcur = restore_into(view,saved)
if newcur and _VIEWS[newcur] then
ui.goto_view(_VIEWS[newcur])
end
end,
['\r'] = do_enter,
['\n'] = do_enter,
up = function()
local buf = buffer._textredux
if buf.data.completions then
local sel_line = buf.data.completions_sel
sel_line = sel_line - 1
if sel_line < 1 then
sel_line = #buf.data.completions
end
buf.data.completions_sel = sel_line
local min_visible = buf.data.completions_offset + 1
local max_visible = buf.data.completions_offset + M.MAX_COMPLETION_LINES
if sel_line > max_visible or sel_line < min_visible then
buf.data.completions_offset = math.floor((sel_line - 1) / M.MAX_COMPLETION_LINES) * M.MAX_COMPLETION_LINES
end
else
local idx = buf.data.histidx
local hist = buf.data.context._history
-- Save this item
if idx == buf.data.histsaveidx then
buf.data.context._history[idx] = buf.data.text
end
if idx > 1 then
idx = idx - 1
buf.data.histidx = idx
end
buf.data.text = hist[idx]
buf.data.pos = #buf.data.text
end
buf:refresh()
end,
down = function()
local buf = buffer._textredux
if buf.data.completions then
local sel_line = buf.data.completions_sel
sel_line = sel_line + 1
if sel_line > #buf.data.completions then
sel_line = 1
end
buf.data.completions_sel = sel_line
local min_visible = buf.data.completions_offset + 1
local max_visible = buf.data.completions_offset + M.MAX_COMPLETION_LINES
if sel_line > max_visible or sel_line < min_visible then
buf.data.completions_offset = math.floor((sel_line - 1) / M.MAX_COMPLETION_LINES) * M.MAX_COMPLETION_LINES
end
else
local idx = buf.data.histidx
local hist = buf.data.context._history
if idx < buf.data.histsaveidx then
idx = idx + 1
buf.data.histidx = idx
buf.data.text = hist[idx]
buf.data.pos = #buf.data.text
end
end
buf:refresh()
end
}
local function set_key(k)
ve_keys[k] = function()
local buf = buffer._textredux
local t = buf.data.text
local pos = buffer.current_pos
t = t:sub(1, pos) .. k .. t:sub(pos+1, -1)
buf.data.text = t
buf.data.pos = pos + 1
if buf.data.completions then
complete_now()
end
buf:refresh()
end
end
local function set_key_range(first, last)
local i = string.byte(first)
while i <= string.byte(last) do
set_key(string.char(i))
i = i + 1
end
end
-- Set all ASCII printable keys to just insert.
set_key_range(' ', '\x7e')
local function do_start(context)
local buf = redux.core.buffer.new('entry')
buf.on_refresh = ve_refresh
for k,v in pairs(ve_keys) do
buf.keys[k] = v
end
buf.data = {
prompt=context._prompt,
text = '',
pos=0,
handler=context._handler,
complete=context._complete,
context=context,
histidx=#context._history+1,
histsaveidx=#context._history+1,
}
local saved = save_views()
unsplit_all()
local new, old
--first, second = view:split()
local newview, prevcur = restore_saved(saved)
newview.size = ui.size[2] - 4
ui.goto_view(_VIEWS[newview])
buf.data.saved = saved
buf:show()
end
-- Create a new entry context
function M.new(prompt, handler, complete)
local result = {
_prompt=prompt,
_handler=handler,
_complete=complete,
_history={},
start = do_start,
}
return result
end
return M