Skip to content

Commit 55977c1

Browse files
committed
feat(fzf-tmux): default to using "--tmux"
Replaces `fzf_bin = "fzf-tmux"` as "--tmux" is the preferred method for both fzf and skim. Fixed getting the correct width from tmux window/pane for accurate "flex" layout calculation based on % in setting (default to 50%). This commit also perpares us better for the upcoming "--popup" option in fzf in order to support zellij popups. Ref: junegunn/fzf#4145
1 parent b27cb58 commit 55977c1

File tree

4 files changed

+63
-32
lines changed

4 files changed

+63
-32
lines changed

Diff for: README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,10 @@ fzf_opts = {
568568
-- popup 80% width, 80% height (note `-p` requires tmux > 3.2)
569569
-- and removes the sides margin added by `fzf-tmux` (fzf#3162)
570570
-- for more options run `fzf-tmux --help`
571-
fzf_tmux_opts = { ["-p"] = "80%,80%", ["--margin"] = "0,0" },
571+
-- NOTE: since fzf v0.53 / sk v0.15 it is recommended to use
572+
-- native tmux integration by adding the below to `fzf_opts`
573+
-- fzf_opts = { ["--tmux"] = "center,80%,60%" }
574+
fzf_tmux_opts = { ["-p"] = "80%,80%", ["--margin"] = "0,0" },
572575
```
573576

574577
</details>

Diff for: lua/fzf-lua/config.lua

+21-27
Original file line numberDiff line numberDiff line change
@@ -279,18 +279,6 @@ function M.normalize_opts(opts, globals, __resume_key)
279279
end
280280
end
281281

282-
-- prioritize fzf-tmux split pane flags over the
283-
-- popup flag `-p` from fzf-lua defaults (#865)
284-
opts._is_fzf_tmux_popup = true
285-
if type(opts.fzf_tmux_opts) == "table" then
286-
for _, flag in ipairs({ "-u", "-d", "-l", "-r" }) do
287-
if opts.fzf_tmux_opts[flag] then
288-
opts._is_fzf_tmux_popup = false
289-
opts.fzf_tmux_opts["-p"] = nil
290-
end
291-
end
292-
end
293-
294282
-- Merge `winopts` with outputs from `winopts_fn`
295283
local winopts_fn = opts.winopts_fn or M.globals.winopts_fn
296284
if type(winopts_fn) == "function" then
@@ -648,6 +636,7 @@ function M.normalize_opts(opts, globals, __resume_key)
648636
["--highlight-line"] = true,
649637
}
650638
},
639+
["0.53"] = { fzf_opts = { ["--tmux"] = true } },
651640
["0.52"] = { fzf_opts = { ["--highlight-line"] = true } },
652641
["0.42"] = {
653642
fzf_opts = {
@@ -720,22 +709,27 @@ function M.normalize_opts(opts, globals, __resume_key)
720709
end
721710

722711
-- Are we using fzf-tmux? if so get available columns
723-
opts._is_fzf_tmux = vim.env.TMUX
724-
-- pre fzf v0.53 uses the fzf-tmux script
725-
and opts.fzf_bin:match("fzf%-tmux$") and 1
726-
-- fzf v0.53 added native tmux integration
727-
or utils.has(opts, "fzf", { 0, 53 }) and opts.fzf_opts["--tmux"] and 2
728-
-- skim v0.15.5 added native tmux integration
729-
or utils.has(opts, "sk", { 0, 15, 5 }) and opts.fzf_opts["--tmux"] and 2
730-
if opts._is_fzf_tmux then
731-
local out = utils.io_system({ "tmux", "display-message", "-p", "#{window_width}" })
732-
opts._tmux_columns = tonumber(out:match("%d+"))
733-
opts.winopts.split = nil
734-
if opts._is_fzf_tmux == 2 then
735-
-- native tmux integration is implemented using tmux popups
736-
opts._is_fzf_tmux_popup = true
712+
opts._is_fzf_tmux = (function()
713+
if not vim.env.TMUX then return end
714+
local is_tmux =
715+
(opts.fzf_bin:match("fzf%-tmux$") or opts.fzf_bin:match("sk%-tmux$")) and 1
716+
-- fzf v0.53 added native tmux integration
717+
or utils.has(opts, "fzf", { 0, 53 }) and opts.fzf_opts["--tmux"] and 2
718+
-- skim v0.15.5 added native tmux integration
719+
or utils.has(opts, "sk", { 0, 15, 5 }) and opts.fzf_opts["--tmux"] and 2
720+
if is_tmux == 1 then
721+
-- backward compat when using the `fzf-tmux` script: prioritize fzf-tmux
722+
-- split pane flags over the popup flag `-p` from fzf-lua defaults (#865)
723+
if type(opts.fzf_tmux_opts) == "table" then
724+
for _, flag in ipairs({ "-u", "-d", "-l", "-r" }) do
725+
if opts.fzf_tmux_opts[flag] then
726+
opts.fzf_tmux_opts["-p"] = nil
727+
end
728+
end
729+
end
737730
end
738-
end
731+
return is_tmux
732+
end)()
739733

740734
-- refresh highlights if background/colorscheme changed (#1092)
741735
if not M.__HLS_STATE

Diff for: lua/fzf-lua/profiles/fzf-tmux.lua

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
return {
22
desc = "fzf-native run inside a tmux popup",
3-
fzf_bin = "fzf-tmux",
4-
fzf_opts = { ["--border"] = "rounded" },
5-
fzf_tmux_opts = { ["-p"] = "80%,60%" },
3+
fzf_opts = { ["--border"] = "rounded", ["--tmux"] = "center,80%,60%" },
64
winopts = { preview = { default = "bat" } },
75
manpages = { previewer = "man_native" },
86
helptags = { previewer = "help_native" },

Diff for: lua/fzf-lua/win.lua

+37-1
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,49 @@ local strip_borderchars_hl = function(border)
266266
return borderchars
267267
end
268268

269+
function FzfWin:tmux_columns()
270+
local is_popup, is_hsplit, opt_val = (function()
271+
-- Backward compat using "fzf-tmux" script
272+
if self._o._is_fzf_tmux == 1 then
273+
for _, flag in ipairs({ "-l", "-r" }) do
274+
if self._o.fzf_tmux_opts[flag] then
275+
-- left/right split, not a popup, is an hsplit
276+
return false, true, self._o.fzf_tmux_opts[flag]
277+
end
278+
end
279+
for _, flag in ipairs({ "-u", "-d" }) do
280+
if self._o.fzf_tmux_opts[flag] then
281+
-- up/down split, not a popup, not an hsplit
282+
return false, false, self._o.fzf_tmux_opts[flag]
283+
end
284+
end
285+
-- Default is a popup with "-p" or without
286+
return true, false, self._o.fzf_tmux_opts["-p"]
287+
else
288+
return true, false, self._o.fzf_opts["--tmux"]
289+
end
290+
end)()
291+
local out = utils.io_system({
292+
"tmux", "display-message", "-p",
293+
is_popup and "#{window_width}" or "#{pane_width}"
294+
})
295+
local cols = tonumber(out:match("%d+"))
296+
-- Calc the correct width when using tmux popup or left|right splits
297+
-- fzf's defaults to "--tmux" is "center,50%" or "50%" for splits
298+
if is_popup or is_hsplit then
299+
local percent = type(opt_val) == "string" and tonumber(opt_val:match("(%d+)%%")) or 50
300+
cols = math.floor(cols * percent / 100)
301+
end
302+
return cols
303+
end
304+
269305
function FzfWin:columns(no_fullscreen)
270306
assert(self.winopts)
271307
-- When called from `core.preview_window` we need to get the no-fullscreen columns
272308
-- in order to get an accurate alternate layout trigger that will also be consistent
273309
-- when starting with `winopts.fullscreen == true`
274310
local winopts = no_fullscreen and self:normalize_winopts(false) or self.winopts
275-
return self._o._is_fzf_tmux and self._o._is_fzf_tmux_popup and self._o._tmux_columns
311+
return self._o._is_fzf_tmux and self:tmux_columns()
276312
or winopts.split and vim.api.nvim_win_get_width(self.fzf_winid or 0)
277313
or winopts.width
278314
end

0 commit comments

Comments
 (0)