Skip to content

Commit

Permalink
feat(on_click): support binding lua callbacks to mouse clicks
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelot committed May 24, 2022
1 parent 08a9c4e commit 0271f3a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lua/heirline/statusline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local default_restrict = {
condition = true,
restrict = true,
pick_child = true,
on_click = true,
}

local StatusLine = {
Expand All @@ -30,6 +31,7 @@ function StatusLine:new(child, index)
new.init = child.init
new.provider = child.provider
new.stop_when = child.stop_when
new.on_click = child.on_click and vim.tbl_extend("keep", child.on_click, {})
new.restrict = child.restrict and vim.tbl_extend("keep", child.restrict, {})

if child.static then
Expand Down Expand Up @@ -111,6 +113,15 @@ function StatusLine:get_win_attr(attr, default)
return self[attr][winnr]
end

local function register_global_function(component, on_click)
if _G[on_click.name] and not on_click.update then
return
end
_G[on_click.name] = function(minwid, nclicks, button)
(on_click.callback)(component, minwid, nclicks, button)
end
end

function StatusLine:eval()
if self.condition and not self:condition() then
return ""
Expand All @@ -137,6 +148,11 @@ function StatusLine:eval()
table.insert(stl, hl_str_start .. provider_str .. hl_str_end)
end

if self.on_click then
register_global_function(self, self.on_click)
table.insert(stl, "%@v:lua." .. self.on_click.name .. "@")
end

local children_i
if self.pick_child then
children_i = self.pick_child
Expand All @@ -153,6 +169,10 @@ function StatusLine:eval()
table.insert(stl, out)
end

if self.on_click then
table.insert(stl, "%X")
end

self.stl = table.concat(stl, "")

return self.stl
Expand Down

0 comments on commit 0271f3a

Please sign in to comment.