From 2ee3496748cb4284d75c88b9f4e116bfe2dac872 Mon Sep 17 00:00:00 2001 From: zztrieuzz Date: Wed, 8 Nov 2023 17:06:43 +0700 Subject: [PATCH] Revert "Merge pull request #175 from Zeioth/ui-antibreak-revert" This reverts commit 41219492b2b700ae4963f648ef1b19241a2d61c9, reversing changes made to 7577078e999dba950ae066ed3af617e75eeb6d03. --- README.md | 22 +++++++---- lua/spectre/config.lua | 1 + lua/spectre/init.lua | 83 +++++++++++++++++++++--------------------- 3 files changed, 57 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 692bb48..7081895 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,9 @@ with `:e` because `sed` is replace outside vim. require('spectre').setup() ``` -Change any settings if you don't like them. **Don't just copy all** as +
+ Config + Change any settings if you don't like them. **Don't just copy all** as settings may change as the plugin is updated so it may be better use the default settings. @@ -255,11 +257,14 @@ require('spectre').setup({ }, replace_vim_cmd = "cdo", is_open_target_win = true, --open file on opener window - is_insert_mode = false -- start open panel on is_insert_mode + is_insert_mode = false, -- start open panel on is_insert_mode + is_block_ui_break = false -- mapping backspace and enter key to avoid ui break }) ``` +
+ ### Custom Functions ```lua @@ -328,8 +333,11 @@ Thanks to everyone who sponsors my projects and makes continued development and require('spectre.state_utils').status_line() ) ``` -- What if I have remapped keys in my neovim config? +- How to avoid ui break? +```lua +require('spectre').setup({ is_block_ui_break = true }) +``` > [Spectre hardcodes some mappings in order to work correctly](https://github.com/nvim-pack/nvim-spectre/blob/1abe23ec9b7bc3082164f4cb842d521ef70e080e/lua/spectre/init.lua#L175). You can remap them as described above. You are allowed to create as many mappings as you want. For name and description choose any value. 'map' and 'cmd' are the only important fields. - Is spectre compatible with the plugin mini.animate? @@ -338,8 +346,6 @@ Thanks to everyone who sponsors my projects and makes continued development and - Why is it called Spectre? -> I wanted to call it `Search Panel` but this name is not cool. - -> I got the name of a hero on a game. - -> Spectre has a skill to find enemy on global map so I use it:) + I wanted to call it `Search Panel` but this name is not cool. + I got the name of a hero on a game. + Spectre has a skill to find enemy on global map so I use it:) diff --git a/lua/spectre/config.lua b/lua/spectre/config.lua index d8d553f..8914116 100644 --- a/lua/spectre/config.lua +++ b/lua/spectre/config.lua @@ -193,6 +193,7 @@ local config = { replace_vim_cmd = "cdo", is_open_target_win = true, is_insert_mode = false, + is_block_ui_break = false } if vim.loop.os_uname().sysname == 'Darwin' then diff --git a/lua/spectre/init.lua b/lua/spectre/init.lua index a273a15..3ae0499 100644 --- a/lua/spectre/init.lua +++ b/lua/spectre/init.lua @@ -26,7 +26,7 @@ local state_utils = require('spectre.state_utils') local utils = require('spectre.utils') local ui = require('spectre.ui') local log = require('spectre._log') -local highlight = require('spectre.highlight') +require('spectre.highlight') local async = require('plenary.async') local scheduler = async.util.scheduler @@ -189,7 +189,6 @@ function M.mapping_buffer(bufnr) api.nvim_buf_set_keymap(bufnr, 'n', 'o', 'ji', map_opt) -- don't append line on can make the UI wrong api.nvim_buf_set_keymap(bufnr, 'n', 'O', 'ki', map_opt) api.nvim_buf_set_keymap(bufnr, 'n', 'u', "", map_opt) -- disable undo, It breaks the UI. - api.nvim_buf_set_keymap(bufnr, 'i', '', "", map_opt) -- disable ENTER on insert mode, it breaks the UI. api.nvim_buf_set_keymap(bufnr, 'n', '', "lua require('spectre').tab()", map_opt) api.nvim_buf_set_keymap(bufnr, 'n', '', "lua require('spectre').tab_shift()", map_opt) api.nvim_buf_set_keymap(bufnr, 'n', '?', "lua require('spectre').show_help()", map_opt) @@ -214,42 +213,46 @@ function M.mapping_buffer(bufnr) end, desc = "Ensure spectre state when its window is closed by any mean" }) - -- Anti UI breakage - -- * If the user enters insert mode on a forbidden line: leave insert mode. - -- * If the user passes over a forbidden line on insert mode: leave insert mode. - -- * Disable backspace jumping lines. - local backspace = vim.api.nvim_get_option('backspace') - local anti_insert_breakage_group = vim.api.nvim_create_augroup("SpectreAntiInsertBreakage", { clear = true }) - vim.api.nvim_create_autocmd({"InsertEnter", "CursorMovedI"}, { - group = anti_insert_breakage_group, - pattern = "*", - callback = function() - local current_filetype = vim.bo.filetype - if current_filetype == "spectre_panel" then - vim.cmd("set backspace=indent,start") - local line = vim.api.nvim_win_get_cursor(0)[1] - if line == 1 or line == 2 or line == 4 or line == 6 or line >= 8 - then - vim.api.nvim_feedkeys( - vim.api.nvim_replace_termcodes('', true, false, true), - 'n', true - ) + + if state.user_config.is_block_ui_break then + -- Anti UI breakage + -- * If the user enters insert mode on a forbidden line: leave insert mode. + -- * If the user passes over a forbidden line on insert mode: leave insert mode. + -- * Disable backspace jumping lines. + local backspace = vim.api.nvim_get_option('backspace') + local anti_insert_breakage_group = vim.api.nvim_create_augroup("SpectreAntiInsertBreakage", { clear = true }) + vim.api.nvim_create_autocmd({ "InsertEnter", "CursorMovedI" }, { + group = anti_insert_breakage_group, + pattern = "*", + callback = function() + local current_filetype = vim.bo.filetype + if current_filetype == "spectre_panel" then + vim.cmd("set backspace=indent,start") + local line = vim.api.nvim_win_get_cursor(0)[1] + if line == 1 or line == 2 or line == 4 or line == 6 or line >= 8 + then + vim.api.nvim_feedkeys( + vim.api.nvim_replace_termcodes('', true, false, true), + 'n', true + ) + end end - end - end, - desc = "spectre anti-insert-breakage → protect the user from breaking the UI while on insert mode." - }) - vim.api.nvim_create_autocmd({"WinLeave"}, { - group = anti_insert_breakage_group, - pattern = "*", - callback = function() - local current_filetype = vim.bo.filetype - if current_filetype == "spectre_panel" then - vim.cmd("set backspace=" .. backspace) - end - end, - desc = "spectre anti-insert-breakage → restore the 'backspace' option." - }) + end, + desc = "spectre anti-insert-breakage → protect the user from breaking the UI while on insert mode." + }) + vim.api.nvim_create_autocmd({ "WinLeave" }, { + group = anti_insert_breakage_group, + pattern = "*", + callback = function() + local current_filetype = vim.bo.filetype + if current_filetype == "spectre_panel" then + vim.cmd("set backspace=" .. backspace) + end + end, + desc = "spectre anti-insert-breakage → restore the 'backspace' option." + }) + api.nvim_buf_set_keymap(bufnr, 'i', '', "", map_opt) -- disable ENTER on insert mode, it breaks the UI. + end end local function hl_match(opts) @@ -714,15 +717,13 @@ end M.tab = function() local line = vim.api.nvim_win_get_cursor(0)[1] if line == 3 then vim.api.nvim_win_set_cursor(vim.api.nvim_get_current_win(), {5, 1}) end - if line == 5 then vim.api.nvim_win_set_cursor(vim.api.nvim_get_current_win(), {7, 1}) - end + if line == 5 then vim.api.nvim_win_set_cursor(vim.api.nvim_get_current_win(), {7, 1}) end end M.tab_shift = function() local line = vim.api.nvim_win_get_cursor(0)[1] if line == 5 then vim.api.nvim_win_set_cursor(vim.api.nvim_get_current_win(), {3, 1}) end - if line == 7 then vim.api.nvim_win_set_cursor(vim.api.nvim_get_current_win(), {5, 1}) - end + if line == 7 then vim.api.nvim_win_set_cursor(vim.api.nvim_get_current_win(), {5, 1}) end end return M