diff --git a/README.md b/README.md index 29edc4d8..edb38b04 100644 --- a/README.md +++ b/README.md @@ -568,7 +568,10 @@ fzf_opts = { -- popup 80% width, 80% height (note `-p` requires tmux > 3.2) -- and removes the sides margin added by `fzf-tmux` (fzf#3162) -- for more options run `fzf-tmux --help` -fzf_tmux_opts = { ["-p"] = "80%,80%", ["--margin"] = "0,0" }, +-- NOTE: since fzf v0.53 / sk v0.15 it is recommended to use +-- native tmux integration by adding the below to `fzf_opts` +-- fzf_opts = { ["--tmux"] = "center,80%,60%" } +fzf_tmux_opts = { ["-p"] = "80%,80%", ["--margin"] = "0,0" }, ``` diff --git a/lua/fzf-lua/config.lua b/lua/fzf-lua/config.lua index 6d2898dd..0741ac4e 100644 --- a/lua/fzf-lua/config.lua +++ b/lua/fzf-lua/config.lua @@ -279,18 +279,6 @@ function M.normalize_opts(opts, globals, __resume_key) end end - -- prioritize fzf-tmux split pane flags over the - -- popup flag `-p` from fzf-lua defaults (#865) - opts._is_fzf_tmux_popup = true - if type(opts.fzf_tmux_opts) == "table" then - for _, flag in ipairs({ "-u", "-d", "-l", "-r" }) do - if opts.fzf_tmux_opts[flag] then - opts._is_fzf_tmux_popup = false - opts.fzf_tmux_opts["-p"] = nil - end - end - end - -- Merge `winopts` with outputs from `winopts_fn` local winopts_fn = opts.winopts_fn or M.globals.winopts_fn if type(winopts_fn) == "function" then @@ -648,6 +636,7 @@ function M.normalize_opts(opts, globals, __resume_key) ["--highlight-line"] = true, } }, + ["0.53"] = { fzf_opts = { ["--tmux"] = true } }, ["0.52"] = { fzf_opts = { ["--highlight-line"] = true } }, ["0.42"] = { fzf_opts = { @@ -720,22 +709,27 @@ function M.normalize_opts(opts, globals, __resume_key) end -- Are we using fzf-tmux? if so get available columns - opts._is_fzf_tmux = vim.env.TMUX - -- pre fzf v0.53 uses the fzf-tmux script - and opts.fzf_bin:match("fzf%-tmux$") and 1 - -- fzf v0.53 added native tmux integration - or utils.has(opts, "fzf", { 0, 53 }) and opts.fzf_opts["--tmux"] and 2 - -- skim v0.15.5 added native tmux integration - or utils.has(opts, "sk", { 0, 15, 5 }) and opts.fzf_opts["--tmux"] and 2 - if opts._is_fzf_tmux then - local out = utils.io_system({ "tmux", "display-message", "-p", "#{window_width}" }) - opts._tmux_columns = tonumber(out:match("%d+")) - opts.winopts.split = nil - if opts._is_fzf_tmux == 2 then - -- native tmux integration is implemented using tmux popups - opts._is_fzf_tmux_popup = true + opts._is_fzf_tmux = (function() + if not vim.env.TMUX then return end + local is_tmux = + (opts.fzf_bin:match("fzf%-tmux$") or opts.fzf_bin:match("sk%-tmux$")) and 1 + -- fzf v0.53 added native tmux integration + or utils.has(opts, "fzf", { 0, 53 }) and opts.fzf_opts["--tmux"] and 2 + -- skim v0.15.5 added native tmux integration + or utils.has(opts, "sk", { 0, 15, 5 }) and opts.fzf_opts["--tmux"] and 2 + if is_tmux == 1 then + -- backward compat when using the `fzf-tmux` script: prioritize fzf-tmux + -- split pane flags over the popup flag `-p` from fzf-lua defaults (#865) + if type(opts.fzf_tmux_opts) == "table" then + for _, flag in ipairs({ "-u", "-d", "-l", "-r" }) do + if opts.fzf_tmux_opts[flag] then + opts.fzf_tmux_opts["-p"] = nil + end + end + end end - end + return is_tmux + end)() -- refresh highlights if background/colorscheme changed (#1092) if not M.__HLS_STATE diff --git a/lua/fzf-lua/profiles/fzf-tmux.lua b/lua/fzf-lua/profiles/fzf-tmux.lua index 8b1e1364..5e1e651a 100644 --- a/lua/fzf-lua/profiles/fzf-tmux.lua +++ b/lua/fzf-lua/profiles/fzf-tmux.lua @@ -1,8 +1,6 @@ return { desc = "fzf-native run inside a tmux popup", - fzf_bin = "fzf-tmux", - fzf_opts = { ["--border"] = "rounded" }, - fzf_tmux_opts = { ["-p"] = "80%,60%" }, + fzf_opts = { ["--border"] = "rounded", ["--tmux"] = "center,80%,60%" }, winopts = { preview = { default = "bat" } }, manpages = { previewer = "man_native" }, helptags = { previewer = "help_native" }, diff --git a/lua/fzf-lua/win.lua b/lua/fzf-lua/win.lua index f0207552..72e9fcda 100644 --- a/lua/fzf-lua/win.lua +++ b/lua/fzf-lua/win.lua @@ -266,13 +266,49 @@ local strip_borderchars_hl = function(border) return borderchars end +function FzfWin:tmux_columns() + local is_popup, is_hsplit, opt_val = (function() + -- Backward compat using "fzf-tmux" script + if self._o._is_fzf_tmux == 1 then + for _, flag in ipairs({ "-l", "-r" }) do + if self._o.fzf_tmux_opts[flag] then + -- left/right split, not a popup, is an hsplit + return false, true, self._o.fzf_tmux_opts[flag] + end + end + for _, flag in ipairs({ "-u", "-d" }) do + if self._o.fzf_tmux_opts[flag] then + -- up/down split, not a popup, not an hsplit + return false, false, self._o.fzf_tmux_opts[flag] + end + end + -- Default is a popup with "-p" or without + return true, false, self._o.fzf_tmux_opts["-p"] + else + return true, false, self._o.fzf_opts["--tmux"] + end + end)() + local out = utils.io_system({ + "tmux", "display-message", "-p", + is_popup and "#{window_width}" or "#{pane_width}" + }) + local cols = tonumber(out:match("%d+")) + -- Calc the correct width when using tmux popup or left|right splits + -- fzf's defaults to "--tmux" is "center,50%" or "50%" for splits + if is_popup or is_hsplit then + local percent = type(opt_val) == "string" and tonumber(opt_val:match("(%d+)%%")) or 50 + cols = math.floor(cols * percent / 100) + end + return cols +end + function FzfWin:columns(no_fullscreen) assert(self.winopts) -- When called from `core.preview_window` we need to get the no-fullscreen columns -- in order to get an accurate alternate layout trigger that will also be consistent -- when starting with `winopts.fullscreen == true` local winopts = no_fullscreen and self:normalize_winopts(false) or self.winopts - return self._o._is_fzf_tmux and self._o._is_fzf_tmux_popup and self._o._tmux_columns + return self._o._is_fzf_tmux and self:tmux_columns() or winopts.split and vim.api.nvim_win_get_width(self.fzf_winid or 0) or winopts.width end