-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
It would be nice to support more advanced handling of mouse events in Lua (at the very least, provide mouse position info to lua). It would allow implementing various interesting extensions.
Some time ago I already started working on it in my luamouse branch: https://github.com/dmaluka/micro/commits/luamouse
I implemented it the easiest possible way: pass *tcell.EventMouse to lua to provide position, and export MouseReleased() function to distinguish click from move.
It's quite ugly but it works. I'm using it for searching the word under cursor upon right button click:
local config = import("micro/config")
local buffer = import("micro/buffer")
local util = import("micro/util")
local regexp = import("regexp")
function searchClick(bp, e)
if not bp:MouseReleased() then
return
end
local vloc = buffer.Loc(e:Position())
local loc = bp:LocFromVisual(vloc)
if util.IsWordChar(util.RuneAt(bp.Buf:LineBytes(loc.Y), loc.X)) then
bp.Cursor.Loc = loc
bp.Cursor:SelectWord()
bp:Search("\\b" .. regexp.QuoteMeta(bp.Cursor:GetSelection()) .. "\\b", true, true)
end
end
function init()
config.TryBindKey("MouseRight", "lua:search.searchClick", false)
end
Note that it doesn't allow handling mouse release events, which would be also nice (e.g. we could implement searching text upon selecting it via right button, like in Acme editor).
It seems we should use a more abstract approach than directly passing tcell.EventMouse to lua.