From 3309121d6cca1fad74a7b177bfaceaa1ca919eab Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Sat, 13 Jul 2024 11:16:04 +0800 Subject: Update --- vim/lua/mappings.lua | 211 +++++++++++++++++++++++++-------------------------- 1 file changed, 105 insertions(+), 106 deletions(-) (limited to 'vim/lua') diff --git a/vim/lua/mappings.lua b/vim/lua/mappings.lua index 07107e5..5eea577 100644 --- a/vim/lua/mappings.lua +++ b/vim/lua/mappings.lua @@ -20,10 +20,10 @@ vim.keymap.set("n", "b", "Telescope buffers", { desc = "telesco vim.keymap.set("n", "//", "Telescope current_buffer_fuzzy_find", { desc = "telescope find in current buffer" }) vim.keymap.set("n", "sf", "Telescope find_files", { desc = "telescope find files" }) vim.keymap.set( - "n", - "sF", - "Telescope find_files follow=true no_ignore=true hidden=true", - { desc = "telescope find all files" } + "n", + "sF", + "Telescope find_files follow=true no_ignore=true hidden=true", + { desc = "telescope find all files" } ) vim.keymap.set("n", "sg", "Telescope live_grep", { desc = "telescope live grep" }) vim.keymap.set("n", "gf", "Telescope git_files", { desc = "telescope git files" }) @@ -35,110 +35,109 @@ vim.keymap.set("n", "sk", "Telescope keymaps", { desc = "telesc vim.keymap.set("n", "pt", "Telescope terms", { desc = "telescope pick hidden term" }) vim.keymap.set("n", "ss", function() - local current_filetype = vim.bo.filetype - local cwd = os.getenv("HOME") .. "/snippets" - require("telescope.builtin").find_files({ - prompt_title = "Press to edit a snippet", - default_text = current_filetype == "" and "" or current_filetype .. "_", - cwd = cwd, - attach_mappings = function(prompt_bufnr, map) - local get_prompt_or_entry = function() - local file_list = require("telescope.actions.state").get_selected_entry() - if file_list then - return file_list[1] - else - local current_picker = require("telescope.actions.state").get_current_picker(prompt_bufnr) - return current_picker:_get_prompt() - end - end - - local edit_snippet = function() - local file = get_prompt_or_entry() - require("telescope.actions").close(prompt_bufnr) - local prefix_filetype = string.match(file, "([^_]+)") - vim.cmd(":vs") - vim.cmd(":e " .. cwd .. "/" .. file) - vim.bo.filetype = prefix_filetype - vim.bo.bufhidden = "wipe" - vim.cmd("set filetype?") - end - - local insert_selected_snippet = function() - local file = get_prompt_or_entry() - local path = cwd .. "/" .. file - if vim.fn.filereadable(path) ~= 0 then - local snippet_content = vim.fn.readfile(path) - require("telescope.actions").close(prompt_bufnr) - vim.fn.setreg('"', snippet_content) - print("Snippet saved to register") - else - edit_snippet() - end - end - - map("i", "", insert_selected_snippet) - map("i", "", edit_snippet) - map("n", "", insert_selected_snippet) - - return true - end, - }) + local current_filetype = vim.bo.filetype + local cwd = os.getenv("HOME") .. "/snippets" + require("telescope.builtin").find_files({ + prompt_title = "Press to edit a snippet", + default_text = current_filetype == "" and "" or current_filetype .. "_", + cwd = cwd, + attach_mappings = function(prompt_bufnr, map) + local get_prompt_or_entry = function() + local file_list = require("telescope.actions.state").get_selected_entry() + if file_list then + return file_list[1] + else + local current_picker = require("telescope.actions.state").get_current_picker(prompt_bufnr) + return current_picker:_get_prompt() + end + end + + local edit_snippet = function() + local file = get_prompt_or_entry() + require("telescope.actions").close(prompt_bufnr) + local prefix_filetype = string.match(file, "([^_]+)") + vim.cmd(":vs") + vim.cmd(":e " .. cwd .. "/" .. file) + vim.bo.filetype = prefix_filetype + vim.bo.bufhidden = "wipe" + vim.cmd("set filetype?") + end + + local insert_selected_snippet = function() + local file = get_prompt_or_entry() + local path = cwd .. "/" .. file + if vim.fn.filereadable(path) ~= 0 then + local snippet_content = vim.fn.readfile(path) + require("telescope.actions").close(prompt_bufnr) + vim.fn.setreg('"', snippet_content) + print("Snippet saved to register") + else + edit_snippet() + end + end + + map("i", "", insert_selected_snippet) + map("i", "", edit_snippet) + map("n", "", insert_selected_snippet) + + return true + end, + }) end, { desc = "[S]earch [S]nippets" }) vim.keymap.set("n", "sd", function() - require("telescope.builtin").oldfiles({ - prompt_title = "CD to", - attach_mappings = function(prompt_bufnr, map) - local cd_prompt = function() - local file = require("telescope.actions.state").get_selected_entry()[1] - local path = string.match(file, "(.*[/\\])") - require("telescope.actions").close(prompt_bufnr) - vim.api.nvim_feedkeys(":cd " .. path, "n", true) - end - - map("i", "", cd_prompt) - map("n", "", cd_prompt) - - return true - end, - }) + require("telescope.builtin").oldfiles({ + prompt_title = "CD to", + attach_mappings = function(prompt_bufnr, map) + local cd_prompt = function() + local file = require("telescope.actions.state").get_selected_entry()[1] + local path = string.match(file, "(.*[/\\])") + require("telescope.actions").close(prompt_bufnr) + vim.api.nvim_feedkeys(":cd " .. path, "n", true) + end + + map("i", "", cd_prompt) + map("n", "", cd_prompt) + + return true + end, + }) end, { desc = "Search Directory" }) -- new terminals vim.keymap.set({ "n", "t" }, "", function() - require("nvchad.term").toggle({ pos = "vsp", id = "vtoggleTerm" }) + require("nvchad.term").toggle({ pos = "vsp", id = "vtoggleTerm" }) end, { desc = "terminal toggleable vertical term" }) vim.keymap.set("t", "", "", { desc = "terminal escape terminal mode" }) vim.keymap.set({ "t" }, "", "|_i", { desc = "terminal toggleable vertical term" }) vim.keymap.set({ "n", "t" }, "", function() - require("nvchad.term").toggle({ pos = "sp", id = "htoggleTerm" }) + require("nvchad.term").toggle({ pos = "sp", id = "htoggleTerm" }) end, { desc = "terminal new horizontal term" }) vim.keymap.set({ "n", "t" }, "", function() - require("nvchad.term").toggle({ pos = "float", id = "floatTerm" }) + require("nvchad.term").toggle({ pos = "float", id = "floatTerm" }) end, { desc = "terminal toggle floating term" }) -- whichkey -vim.keymap.set("n", "wK", "WhichKey ", { desc = "whichkey all keymaps" }) -vim.keymap.set("n", "wk", function() - vim.cmd("WhichKey " .. vim.fn.input("WhichKey: ")) +vim.keymap.set("n", "sK", function() + vim.cmd("WhichKey " .. vim.fn.input("WhichKey: ")) end, { desc = "whichkey query lookup" }) -- blankline vim.keymap.set("n", "cc", function() - local config = { scope = {} } - config.scope.exclude = { language = {}, node_type = {} } - config.scope.include = { node_type = {} } - local node = require("ibl.scope").get(vim.api.nvim_get_current_buf(), config) - - if node then - local start_row, _, end_row, _ = node:range() - if start_row ~= end_row then - vim.api.nvim_win_set_cursor(vim.api.nvim_get_current_win(), { start_row + 1, 0 }) - vim.api.nvim_feedkeys("_", "n", true) - end - end + local config = { scope = {} } + config.scope.exclude = { language = {}, node_type = {} } + config.scope.include = { node_type = {} } + local node = require("ibl.scope").get(vim.api.nvim_get_current_buf(), config) + + if node then + local start_row, _, end_row, _ = node:range() + if start_row ~= end_row then + vim.api.nvim_win_set_cursor(vim.api.nvim_get_current_win(), { start_row + 1, 0 }) + vim.api.nvim_feedkeys("_", "n", true) + end + end end, { desc = "blankline jump to current context" }) @@ -150,7 +149,7 @@ vim.keymap.set("n", "", ":HopChar1") -- [ Aerial ]"" "AerialNext", {}) vim.keymap.set("n", "gL", "Telescope aerial") vim.keymap.set("n", "gl", function() - require("aerial").toggle({ direction = "left" }) + require("aerial").toggle({ direction = "left" }) end) @@ -160,23 +159,23 @@ vim.keymap.set("n", "z", ":TZAtaraxis") -- Use floating window for translation vim.keymap.set("v", "tz", function() - vim.cmd('norm o^zt"ty') - local translated_text = vim.fn.system("trans -t zh-TW -b", vim.fn.getreg("t")) - local lines = vim.split(translated_text, "\n") - table.remove(lines) - - local new_buf = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(new_buf, 0, -1, true, lines) - - vim.api.nvim_open_win(new_buf, true, { - relative = "cursor", - width = 80, - height = #lines, - row = #lines, - col = 0, - }) - - vim.cmd("setl nocul nonu nornu") - vim.cmd("hi ActiveWindow guibg=#2a5a6a guifg=White | setl winhighlight=Normal:ActiveWindow") - vim.cmd(":silent! %s/\\%x1b\\[[0-9;]*m//g") + vim.cmd('norm o^zt"ty') + local translated_text = vim.fn.system("trans -t zh-TW -b", vim.fn.getreg("t")) + local lines = vim.split(translated_text, "\n") + table.remove(lines) + + local new_buf = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(new_buf, 0, -1, true, lines) + + vim.api.nvim_open_win(new_buf, true, { + relative = "cursor", + width = 80, + height = #lines, + row = #lines, + col = 0, + }) + + vim.cmd("setl nocul nonu nornu") + vim.cmd("hi ActiveWindow guibg=#2a5a6a guifg=White | setl winhighlight=Normal:ActiveWindow") + vim.cmd(":silent! %s/\\%x1b\\[[0-9;]*m//g") end, { desc = "Description" }) -- cgit v1.2.3-70-g09d2