-- Drag-To-Tab
-- Author: Valentin Schmidt
-- Version: 0.3
-- A little Lua hack for SciTE, to be loaded on startup e.g. via Extman.
-- It allows to drag text selections to other buffers inside SciTE.
-- Just drag the selection - keeping mouse button pressed - to another tab
-- and wait for 2 seconds (period adjustable), then SciTE will switch to
-- the corresponding buffer and you can drag the selection into it.
-- Note: unfortunately for now dragging to position 0 (i.e. caret position
-- before the first char) doesn't work yet.
require 'winapiex'
-- config
local switchAfterSeconds = 2
-- constants
local WM_NCHITTEST = 132
local IDM_BUFFERLIST = 1200
local VK_LBUTTON = 1
-- vars
local lastTime = math.huge
local lastIndex = nil
-- find SciTE window
local SciTEWindow = winapiex.getActiveWindow()
if SciTEWindow==0 then os.exit(1) end
-- find SciTeTabCtrl
local SciTeTabCtrl = winapiex.findWindow('SciTeTabCtrl', nil, SciTEWindow)
if SciTeTabCtrl==0 then os.exit(1) end
----------------------------------------
-- @callback
----------------------------------------
function slotMessage (hwnd, uMsg, wParam, lParam)
-- check if left mouse button is pressed
if winapiex.getKeyState(VK_LBUTTON)>=0 then
lastIndex = nil
return
end
-- get mouse x-coord relative to SciTeTabCtrl
local x = math.fmod(lParam, 65536) - winapiex.getWindowRect(SciTeTabCtrl)
-- find tab index corresponding to mouse x-coord
local index, l, r
local i = 0
while true do
l,_,r,_ = winapiex.tcmItemRect(SciTeTabCtrl, i)
if r==0 then break end
if l<=x and r>=x then index = i break end
i = i+1
end
-- check if mouse with left button pressed is already over same tab for <switchAfterSeconds> seconds
local t = os.time()
if index~=nil and index==lastIndex then
if t-lastTime>=switchAfterSeconds then
-- switch to buffer corresponding to tab index
scite.MenuCommand(IDM_BUFFERLIST+index)
editor:ClearSelections()
end
else
lastIndex = index
lastTime = t
end
end
-- start listening for WM_NCHITTEST messages sent to SciTeTabCtrl
winapiex.msgListen(SciTeTabCtrl, {WM_NCHITTEST}, 'slotMessage')