forked from rgieseke/ta-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enclose.lua
53 lines (47 loc) · 1.32 KB
/
enclose.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
-- This module adds functions for enclosing selections with a single key
-- and for inserting single chars with a short cut.
module('_m.common.enclose', package.seeall)
-- ## Setup
local events = _G.events
local m_editing = _m.textadept.editing
local keys = _G.keys
local string_char = _G.string.char
-- Table with char codes as indices.
braces = { -- () [] {}
[40] = 1, [91] = 1, [123] = 1,
[41] = 1, [93] = 1, [125] = 1,
}
-- ## Commands
-- Enclose selected text.<br>
-- Parameters:<br>
-- _left_: char on the left<br>
-- _right_: char on the right
function enclose_selection(left, right)
if buffer:get_sel_text() == '' then
return false
else
m_editing.enclose(left, right)
end
end
-- Encloses selected text and keeps the selection for another enclosure.
-- If nothing is selected, the char is inserted. Useful to avoid automatically
-- matched braces.<br>
-- Parameters:<br>
-- _left_: char on the left<br>
-- _right_: char on the right
function paste_or_grow_enclose (left, right)
if buffer:get_sel_text() == '' then
buffer:add_text(left)
return
else
start = buffer.anchor
stop = buffer.current_pos
if start > stop then
start, stop = stop, start
end
add_start = #left
add_stop = #right
m_editing.enclose(left, right)
buffer:set_sel(start, stop + add_start + add_stop)
end
end