diff --git a/config/nvim/after/plugin/telescope.lua b/config/nvim/after/plugin/telescope.lua index ec99f0a..597c4c4 100644 --- a/config/nvim/after/plugin/telescope.lua +++ b/config/nvim/after/plugin/telescope.lua @@ -79,6 +79,27 @@ telescope.setup { local builtin = require 'telescope.builtin' +local selected_range = function() + local _, s_row, s_col, _ = unpack(vim.fn.getpos('v')) + local _, e_row, e_col, _ = unpack(vim.fn.getpos('.')) + + local mode = vim.api.nvim_get_mode().mode + local visual_line = (mode == 'V' or mode == 'CTRL-V') + + if s_row < e_row or (s_row == e_row and s_col <= e_col) then + if visual_line then s_col, e_col = 1, #vim.fn.getline(e_row) end + return s_row - 1, s_col - 1, e_row - 1, e_col + else + if visual_line then e_col, s_col = 1, #vim.fn.getline(s_row) end + return e_row - 1, e_col - 1, s_row - 1, s_col + end +end + +local selected_text = function() + local r0, c0, r1, c1 = selected_range() + return vim.fn.join(vim.api.nvim_buf_get_text(0, r0, c0, r1, c1, {}), '\n') +end + local custom = { all_files = function() builtin.find_files { @@ -97,6 +118,14 @@ local custom = { } end, + grep_visual = function() + local selection = selected_text() + builtin.grep_string { + prompt_title = string.format('  Grep: %s', selection), + search = selection, + } + end, + man_pages = function() -- Fix for macOS Ventura onwards (macOS 13.x <-> Darwin 22.x). -- See: https://github.com/nvim-telescope/telescope.nvim/issues/2326#issuecomment-1407502328 @@ -129,6 +158,8 @@ map('n', 'fh', builtin.help_tags, { desc = ' [F]ind [H]elp tags' }) map('n', 'fk', builtin.keymaps, { desc = ' [F]ind [K]eymaps' }) map('n', 'fm', custom.man_pages, { desc = ' [F]ind [M]an pages' }) map('n', 'fo', builtin.vim_options, { desc = ' [F]ind vim [O]ptions' }) +map('n', 'fs', builtin.grep_string, { desc = ' [F]ind [S]tring' }) +map('v', 'fs', custom.grep_visual, { desc = ' [F]ind visual [S]election' })