From ef7bf74d4e7af2044741a6607d3bcb6f362b49f5 Mon Sep 17 00:00:00 2001
From: Luca Bilke <luca@bil.ke>
Date: Wed, 6 Mar 2024 18:46:17 +0100
Subject: [PATCH] telescope from git dir

---
 .config/nvim/init.lua               |  1 +
 .config/nvim/lua/config/keymaps.lua | 13 ++++---------
 .config/nvim/lua/funcs.lua          | 13 +++++++++++++
 3 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/.config/nvim/init.lua b/.config/nvim/init.lua
index 9f2469bb..79ff5085 100644
--- a/.config/nvim/init.lua
+++ b/.config/nvim/init.lua
@@ -5,3 +5,4 @@ require("config.autocmds")
 require("config.filetypes")
 require("funcs").set_title()
 require("funcs").set_maps(require("config.keymaps").maps())
+require("funcs").set_cwd()
diff --git a/.config/nvim/lua/config/keymaps.lua b/.config/nvim/lua/config/keymaps.lua
index 2cfc7cf5..eda0847f 100644
--- a/.config/nvim/lua/config/keymaps.lua
+++ b/.config/nvim/lua/config/keymaps.lua
@@ -405,7 +405,7 @@ M.maps = function()
         }
         maps.n["<Leader>fc"] = {
             function()
-                require("telescope.builtin").grep_string()
+                require("telescope.builtin").grep_string({ cwd = f.get_gwd() })
             end,
             desc = "Find word under cursor",
         }
@@ -417,13 +417,13 @@ M.maps = function()
         }
         maps.n["<Leader>ff"] = {
             function()
-                require("telescope.builtin").find_files({ hidden = true, no_ignore = true })
+                require("telescope.builtin").find_files({ hidden = true, no_ignore = true, cwd = f.get_gwd() })
             end,
             desc = "Find files",
         }
         maps.n["<Leader>fg"] = {
             function()
-                require("telescope.builtin").git_files({ hidden = true, no_ignore = true })
+                require("telescope.builtin").git_files({ hidden = true, no_ignore = true, cwd = f.get_gwd() })
             end,
             desc = "Find git files",
         }
@@ -439,12 +439,6 @@ M.maps = function()
             end,
             desc = "Find keymaps",
         }
-        maps.n["<Leader>fm"] = {
-            function()
-                require("telescope.builtin").man_pages()
-            end,
-            desc = "Find man",
-        }
         maps.n["<Leader>fo"] = {
             function()
                 require("telescope.builtin").oldfiles()
@@ -460,6 +454,7 @@ M.maps = function()
         maps.n["<Leader>fw"] = {
             function()
                 require("telescope.builtin").live_grep({
+                    cwd = f.get_gwd(),
                     additional_args = function(args)
                         return vim.list_extend(args, { "--hidden", "--no-ignore" })
                     end,
diff --git a/.config/nvim/lua/funcs.lua b/.config/nvim/lua/funcs.lua
index 34a3c7c3..a0e62796 100644
--- a/.config/nvim/lua/funcs.lua
+++ b/.config/nvim/lua/funcs.lua
@@ -564,4 +564,17 @@ function M.has_value(table, value)
 	return false
 end
 
+function M.get_gwd(path)
+	local find = { ".git" }
+	local opts = { stop = os.getenv("HOME"), upward = true, limit = 1, path = path }
+    local found = vim.fs.find(find, opts)
+	if found ~= nil then
+		return vim.fs.dirname(found[1])
+	end
+end
+
+function M.set_cwd()
+    vim.fn.chdir(M.get_gwd())
+end
+
 return M