-
Notifications
You must be signed in to change notification settings - Fork 148
/
moveline.lua
44 lines (40 loc) · 1.65 KB
/
moveline.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
return {
name = "Move line up/down",
description = "Adds moving line or selection up or down using `Ctrl-Shift-Up/Down`.",
author = "Paul Kulchenko",
version = 0.11,
onEditorKeyDown = function(self, editor, event)
local key = event:GetKeyCode()
local mod = event:GetModifiers()
if (key == wx.WXK_UP or key == wx.WXK_DOWN)
and (mod == wx.wxMOD_CONTROL + wx.wxMOD_SHIFT) then
local line1 = editor:LineFromPosition(editor:GetSelectionStart())
local line2 = editor:LineFromPosition(editor:GetSelectionEnd())
local cut, insert
if key == wx.WXK_UP and line1 > 0 then
cut, insert = line1-1, line2
elseif key == wx.WXK_DOWN and line2 < editor:GetLineCount()-1 then
insert, cut = line1, line2+1
else
return
end
local line = editor:GetLine(cut)
editor:BeginUndoAction()
editor:DeleteRange(editor:PositionFromLine(cut), #line)
local pos = editor:PositionFromLine(insert)
local current, anchor = editor:GetCurrentPos(), editor:GetAnchor()
-- inserting at current position requires a fix as the cursor is
-- anchored to the beginning of the line, which won't move
if pos == current then editor:SetCurrentPos(current+1) end
if pos == anchor then editor:SetAnchor(anchor+1) end
editor:SetTargetStart(pos)
editor:SetTargetEnd(pos)
editor:ReplaceTarget(line)
if pos == current then editor:SetCurrentPos(editor:GetCurrentPos()-1) end
if pos == anchor then editor:SetAnchor(editor:GetAnchor()-1) end
editor:EnsureCaretVisible()
editor:EndUndoAction()
return false -- don't apply "default" handling
end
end,
}