qxxxb/vim-searchhi

how to toggle searchhi?

Opened this issue · 6 comments

The docs mention

It can also be toggled with custom autocommands.

but they do not show how. Could you share the recipe for a toggle? So far I can have

nmap <Space> <Plug>(searchhi-clear-all)

which is admittedly not much 😅. I was spoiled w/ vim-searchant's

nmap <Space> <Plug>SearchantToggle
qxxxb commented

I can't think of a very clean way to implement this right now, but here is a temporary solution

nmap <expr> <C-L> g:searchhi_status == 'listen' ? '<Plug>(searchhi-clear-all)' : '<Plug>(searchhi-n)'
vmap <expr> <C-L> g:searchhi_status == 'listen' ? '<Plug>(searchhi-v-clear-all)' : '<Plug>(searchhi-v-n)'

is it possible to just store state myself inside of my init.lua file? I could easily write my own function for it if I knew how to set/get a stateful boolean variable

qxxxb commented

What do you mean by state?

I mean a variable that can track whether the search has been toggled on or off. I guess I was thinking of something like this:

local search_highlight_enabled = false
function toggle_search_highlight()
  if search_highlight_enabled
  then
    -- clear all (from a function?)
    search_highlight_enabled = false
  else
    -- show all (from a function?)
    search_highlight_enabled = true
  end
end
util.map('n', '<Space>', 'lua: toggle_search_highlight()<CR>', { noremap = false })

but I just tried your workaround. I see that state is already tracked w/ g:searchhi_status 'listen' value. The issue is that toggling search back on always moves to the "next" search result

qxxxb commented

The issue is that toggling search back on always moves to the "next" search result

Yeah that's the tricky part - I'll try to find a solution to this soon

qxxxb commented

Oh I think this works

nmap <expr> <C-L> g:searchhi_status == 'listen' ? '<Plug>(searchhi-clear-all)' : ':<C-U>set hlsearch<CR><Plug>(searchhi-update)'
vmap <expr> <C-L> g:searchhi_status == 'listen' ? '<Plug>(searchhi-v-clear-all)' : ':<C-U>set hlsearch<CR><Plug>(searchhi-v-update)'