1. -- Drag-To-Tab
  2. -- Author: Valentin Schmidt
  3. -- Version: 0.3
  4.  
  5. -- A little Lua hack for SciTE, to be loaded on startup e.g. via Extman.
  6. -- It allows to drag text selections to other buffers inside SciTE.
  7. -- Just drag the selection - keeping mouse button pressed - to another tab
  8. -- and wait for 2 seconds (period adjustable), then SciTE will switch to
  9. -- the corresponding buffer and you can drag the selection into it.
  10.  
  11. -- Note: unfortunately for now dragging to position 0 (i.e. caret position
  12. -- before the first char) doesn't work yet.
  13.  
  14. require 'winapiex'
  15.  
  16. -- config
  17. local switchAfterSeconds = 2
  18.  
  19. -- constants
  20. local WM_NCHITTEST = 132
  21. local IDM_BUFFERLIST = 1200
  22. local VK_LBUTTON = 1
  23.  
  24. -- vars
  25. local lastTime = math.huge
  26. local lastIndex = nil
  27.  
  28. -- find SciTE window
  29. local SciTEWindow = winapiex.getActiveWindow()
  30. if SciTEWindow==0 then os.exit(1) end
  31.  
  32. -- find SciTeTabCtrl
  33. local SciTeTabCtrl = winapiex.findWindow('SciTeTabCtrl', nil, SciTEWindow)
  34. if SciTeTabCtrl==0 then os.exit(1) end
  35.  
  36. ----------------------------------------
  37. -- @callback
  38. ----------------------------------------
  39. function slotMessage (hwnd, uMsg, wParam, lParam)
  40.  
  41.   -- check if left mouse button is pressed
  42.   if winapiex.getKeyState(VK_LBUTTON)>=0 then
  43.     lastIndex = nil
  44.     return
  45.   end
  46.  
  47.   -- get mouse x-coord relative to SciTeTabCtrl
  48.   local x = math.fmod(lParam, 65536) - winapiex.getWindowRect(SciTeTabCtrl)
  49.  
  50.   -- find tab index corresponding to mouse x-coord
  51.   local index, l, r
  52.   local i = 0
  53.   while true do
  54.     l,_,r,_ = winapiex.tcmItemRect(SciTeTabCtrl, i)
  55.     if r==0 then break end
  56.     if l<=x and r>=x then index = i break end
  57.     i = i+1
  58.   end
  59.  
  60.   -- check if mouse with left button pressed is already over same tab for <switchAfterSeconds> seconds
  61.   local t = os.time()
  62.   if index~=nil and index==lastIndex then
  63.     if t-lastTime>=switchAfterSeconds then
  64.       -- switch to buffer corresponding to tab index
  65.       scite.MenuCommand(IDM_BUFFERLIST+index)
  66.       editor:ClearSelections()
  67.     end
  68.   else
  69.     lastIndex = index
  70.     lastTime = t
  71.   end
  72. end
  73.  
  74. -- start listening for WM_NCHITTEST messages sent to SciTeTabCtrl
  75. winapiex.msgListen(SciTeTabCtrl, {WM_NCHITTEST}, 'slotMessage')
  76.  
[raw code]