-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvi_ta_util.lua
57 lines (48 loc) · 1.28 KB
/
vi_ta_util.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
-- Utilities for dealing with textadept objects.
local M = {}
local lpeg = require 'lpeg'
-- Return the number of characters on this line, without
-- line endings.
function M.line_length(lineno)
return buffer.line_end_position[lineno] - buffer.position_from_line(lineno)
end
--- Return the buffer-specific vi state.
function M.buf_state(buf)
if not buf._vi then
buf._vi = {}
end
return buf._vi
end
-- Return the "word" under the cursor,
-- where word characters can be
-- specified, eg to search for filenames.
--
-- Parameters:
-- pos: the position in the current buffer.
-- chars: a string containing the characters to include.
--
-- Returns:
-- start, end, word: start and end positions, and the word found
-- or nothing.
function M.find_word_at(pos, chars)
local chartab = {}
for i=1,#chars do
chartab[chars:byte(i,i)] = true
end
local startpos, endpos
local p = pos
while chartab[buffer.char_at[p]] do
startpos = p
p = p - 1
end
-- No characters of the right class
if not startpos then return end
p = pos
while chartab[buffer.char_at[p]] do
endpos = p
p = p + 1
end
endpos = endpos + 1
return startpos, endpos, buffer:text_range(startpos, endpos)
end
return M