From 5bc1d471781eebd76d57246751b74707ac707134 Mon Sep 17 00:00:00 2001
From: bhagwan <bhagwan@disroot.org>
Date: Sun, 19 Jan 2025 07:20:27 -0800
Subject: [PATCH] fix(hide): better solution for storing last query on esc

This commit partially reverts 85fa30e.

Extracting last query from the prompt line is problematic,
especially with new fzf features such as `--info-command` and
`--input-border`.

Instead we call hide instantly (keeping the perf from the old commit)
and send <esc> to the terminal to execute the esc action and store
the last query using `{q}`.

Closes #1731
---
 lua/fzf-lua/profiles/hide.lua | 39 +++++++++++++++++++++++++++--------
 lua/fzf-lua/win.lua           |  1 -
 2 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/lua/fzf-lua/profiles/hide.lua b/lua/fzf-lua/profiles/hide.lua
index 294c2506..a256118b 100644
--- a/lua/fzf-lua/profiles/hide.lua
+++ b/lua/fzf-lua/profiles/hide.lua
@@ -5,7 +5,9 @@ return {
   keymap   = {
     builtin = {
       true,
-      ["<Esc>"] = "hide",
+      -- NOTE: we use a custom <Esc> callback that also sends esc to fzf
+      -- so we can store the last query on the execute-silent callback
+      -- ["<Esc>"] = "hide",
       ["<M-Esc>"] = "abort"
     }
   },
@@ -15,15 +17,34 @@ return {
         fzf.utils.warn("'hide' profile cannot work with tmux, ignoring.")
         return opts
       end
-      -- `execute-silent` actions are bugged with skim
-      if fzf.utils.has(opts, "sk") then return opts end
-      local histfile = opts.fzf_opts and opts.fzf_opts["--history"]
       opts.actions = opts.actions or {}
-      -- While we can use `keymap.builtin.<esc>` (to hide) this is better
-      -- as it captures the query when execute-silent action is called as
-      -- we add "{q}" as the first field index similar to `--print-query`
-      -- opts.actions["esc"] = { fn = fzf.actions.dummy_abort, desc = "hide" }
-      opts.actions["esc"] = false
+      if fzf.utils.has(opts, "sk") then
+        -- `execute-silent` actions are bugged with skim
+        -- Set esc to hide since we aren't using the custom callback
+        opts.actions["esc"] = false
+        opts.keymap.builtin["<Esc>"] = "hide"
+        return opts
+      end
+      local histfile = opts.fzf_opts and opts.fzf_opts["--history"]
+      opts.winopts = opts.winopts or {}
+      opts.winopts.on_create = function(e)
+        -- While we can use `keymap.builtin.<esc>` (to hide) this is better
+        -- as it captures the query when execute-silent action is called as
+        -- we add "{q}" as the first field index similar to `--print-query`
+        vim.keymap.set({ "t", "n" }, "<Esc>", function()
+          -- We hide the window first which happens instantly
+          -- and then send <Esc> directly to the term channel
+          fzf.hide()
+          vim.api.nvim_chan_send(vim.bo[e.bufnr].channel, "\x1b")
+        end, { buffer = e.bufnr, nowait = true })
+      end
+      opts.actions["esc"] = {
+        fn = fzf.actions.dummy_abort,
+        desc = "hide",
+        -- NOTE: we add this so esc action isn't converted in the
+        -- `tbl_map` below preventing fzf history append on esc
+        exec_silent = true,
+      }
       opts.actions = vim.tbl_map(function(act)
         act = type(act) == "function" and { fn = act } or act
         act = type(act) == "table" and type(act[1]) == "function"
diff --git a/lua/fzf-lua/win.lua b/lua/fzf-lua/win.lua
index 84a3f994..7a8497ea 100644
--- a/lua/fzf-lua/win.lua
+++ b/lua/fzf-lua/win.lua
@@ -1186,7 +1186,6 @@ function FzfWin.hide()
     self:close_preview(true)
     self._hidden_had_preview = true
   end
-  self:store_last_query()
   self:detach_fzf_buf()
   self:close(nil, true)
   -- Save self as `:close()` nullifies it