Skip to content

Commit 2d7cfc1

Browse files
committed
feat: save/restore 'hlsearch' setting when opening/closing UI
As noted in comment, may add more settings later, but this is the one that most annoys me, so starting with that.
1 parent 6033ca8 commit 2d7cfc1

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- SPDX-FileCopyrightText: Copyright 2025-present Greg Hurrell and contributors.
2+
-- SPDX-License-Identifier: BSD-2-Clause
3+
4+
local function includes(t, value)
5+
for _, candidate in ipairs(t) do
6+
if candidate == value then
7+
return true
8+
end
9+
end
10+
return false
11+
end
12+
13+
return includes
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
-- SPDX-FileCopyrightText: Copyright 2025-present Greg Hurrell and contributors.
2+
-- SPDX-License-Identifier: BSD-2-Clause
3+
4+
local includes = require('wincent.commandt.private.includes')
5+
6+
local Settings = {}
7+
8+
local settings = { 'hlsearch' }
9+
10+
local mt = {
11+
__index = Settings,
12+
__newindex = function(t, key, value)
13+
local saved = rawget(t, '_saved')
14+
if includes(settings, key) then
15+
if value == nil then
16+
if saved[key] ~= nil then
17+
-- Reset to previously saved value.
18+
vim.opt[key] = saved[key]
19+
saved[key] = nil
20+
end
21+
else
22+
if saved[key] == nil then
23+
-- Save current value.
24+
saved[key] = vim.o[key]
25+
end
26+
vim.opt[key] = value
27+
end
28+
else
29+
rawset(t, key, value)
30+
end
31+
end,
32+
}
33+
34+
function Settings.new()
35+
local m = {
36+
_saved = {},
37+
}
38+
setmetatable(m, mt)
39+
return m
40+
end
41+
42+
return Settings

lua/wincent/commandt/private/ui.lua

+10-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local ui = {}
55

66
local MatchListing = require('wincent.commandt.private.match_listing').MatchListing
77
local Prompt = require('wincent.commandt.private.prompt').Prompt
8+
local Settings = require('wincent.commandt.private.settings')
89

910
local candidate_count = nil
1011
local cmdline_enter_autocmd = nil
@@ -14,6 +15,7 @@ local match_listing = nil
1415
local prompt = nil
1516
local results = nil
1617
local selected = nil
18+
local settings = Settings.new()
1719

1820
-- Reverses `list` in place.
1921
local reverse = function(list)
@@ -35,6 +37,9 @@ end
3537
-- do anything that would move you out)
3638

3739
local close = function()
40+
-- Restore global settings.
41+
settings.hlsearch = nil
42+
3843
if match_listing then
3944
match_listing:close()
4045
match_listing = nil
@@ -69,16 +74,17 @@ ui.open = function(kind)
6974
end
7075
end
7176

72-
-- TODO save/restore global options, like `hlsearch' (which we want to turn off
73-
-- temporarily when our windows are visible) — either that, or figure out how to
74-
-- make the highlighting not utterly suck.
75-
-- in any case, review the list at ruby/command-t/lib/command-t/match_window.rb
7677
ui.show = function(finder, options)
7778
-- TODO validate options
7879
current_finder = finder
7980

8081
current_window = vim.api.nvim_get_current_win()
8182

83+
-- Temporarily override global settings.
84+
-- For now just 'hlsearch', but may add more later (see
85+
-- ruby/command-t/lib/command-t/match_window.rb)
86+
settings.hlsearch = false
87+
8288
-- Work around an autocommand bug. We don't reliably get `WinClosed` events,
8389
-- or if we do, our call to `nvim_del_autocmd()` doesn't always clean up for
8490
-- us. So, we add some window-related autocommands to a group which we always

0 commit comments

Comments
 (0)