Skip to content

Commit

Permalink
feat(on_click)!: add minwid field and document how to pass window han…
Browse files Browse the repository at this point in the history
…dler; remove winid from callback arguments.
  • Loading branch information
rebelot committed Aug 21, 2022
1 parent 5b5ef26 commit 8ad1050
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
30 changes: 15 additions & 15 deletions cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Each component may contain _any_ of the following fields:
- `on_click`:
- Type: `table` with the following fields:
- `callback`: (vim/)lua function to be called on mouse click(s). The function
has the signature `function(self, winid, minwid, nclicks, button)`
has the signature `function(self, minwid, nclicks, button)`
(see `:h 'statusline'` description for `@`). If a `string` is provided,
it is interpreted as the _raw_ function name (`v:lua.` is not prepended)
of an already defined function accessible from vim global scope.
Expand All @@ -150,7 +150,8 @@ Each component may contain _any_ of the following fields:
- `update`: whether the function should be registered even if
it already exists in the global namespace.
This is useful for dynamically registering different callbacks.
Omit this field if you are registering only one function.
- `minwid`: integer data that can be passed to callback.
Useful to pass window/buffer handlers. Type: `number` or `function -> number`
Type: `boolean` (optional).
- Description: Specify a function to be called when clicking on the component (including its progeny);
Lua functions are automatically registered in the global scope with the name provided
Expand Down Expand Up @@ -1585,16 +1586,15 @@ The following is the recommended way of achieving that:

```lua
on_click = {
callback = function(_, winid)
-- winid is the window id of the window the component was clicked from
-- get the window id of the window in which the component was evaluated
minwid = function()
return vim.api.nvim_get_current_win()
end,
-- A dynamic name + update are required whenever
-- we need to register a closure for each instance of the
-- component displayed in the current tab.
name = function(self)
return "heirline_button_name" .. self.winnr
callback = function(_, minwid)
-- winid is the window id of the window the component was clicked from
local winid = minwid
-- do something with the window id
end,
update = true,
}
```

Expand All @@ -1611,13 +1611,13 @@ local CloseButton = {
provider = "",
hl = { fg = "gray" },
on_click = {
callback = function(_, winid)
vim.api.nvim_win_close(winid, true)
minwid = function()
return vim.api.nvim_get_current_win()
end,
name = function(self)
return "heirline_close_button_" .. self.winnr
callback = function(_, minwid)
vim.api.nvim_win_close(minwid, true)
end,
update = true,
name = "heirline_winbar_close_button"
},
},
}
Expand Down
6 changes: 3 additions & 3 deletions lua/heirline/statusline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ end
---@return string
local function register_global_function(component)
local on_click = component.on_click
local winid = vim.api.nvim_get_current_win()

if type(on_click.callback) == "string" then
return on_click.callback
Expand All @@ -190,7 +189,7 @@ local function register_global_function(component)
end

_G[func_name] = function(minwid, nclicks, button)
on_click.callback(component, winid, minwid, nclicks, button)
on_click.callback(component, minwid, nclicks, button)
end
return "v:lua." .. func_name
end
Expand Down Expand Up @@ -270,7 +269,8 @@ function StatusLine:eval()

if self.on_click then
local func_name = register_global_function(self)
table.insert(stl, "%@" .. func_name .. "@")
local minwid = type(self.on_click.minwid) == "function" and self.on_click.minwid(self) or self.on_click.minwid or ""
table.insert(stl, "%" .. minwid .. "@" .. func_name .. "@")
end

if self.provider then
Expand Down

0 comments on commit 8ad1050

Please sign in to comment.