vis icon indicating copy to clipboard operation
vis copied to clipboard

[Feature Request]: How would I set an option in insert mode only?

Open roguh opened this issue 11 months ago • 8 comments

What feature would you like to see?

I would like to implement "smart hybrid line numbers" in vis, as described for vim here.

This means I want absolute line numbers set number in insert mode, and relative numbers in all other modes set relativenumber. I don't see an event for when a mode changes, is there a way to accomplish this with the Lua API?

roguh avatar Mar 21 '24 02:03 roguh

You could do use a frequent event like vis.events.HIGHLIGHT.

Try something like:

do
  local last_mode = nil
  vis.events.subscribe(vis.events.WIN_HIGHLIGHT, function(win)
    if vis.mode == last_mode then
      return
    end

    last_mode = vis.mode
    if last_mode == vis.modes.INSERT then
      win.options.numbers = true
      win.options.relativenumbers = false
    else
      win.options.numbers = false
      win.options.relativenumbers = true
    end
  end)
end

in your visrc.lua.

It is not perfect because the number change lags one redraw behind, but maybe it helps.

fischerling avatar Mar 22 '24 08:03 fischerling

Or you could remap all of the bindings that take you to insert mode. You would have them do what they normally do, but also run the command to turn on numbers. Then <Esc> in insert mode would run the command to turn on relative numbers.

vis:map(vis.modes.NORMAL, "i", ":set numbers<Enter><vis-mode-insert>")
vis:map(vis.modes.INSERT, "<Escape>", "<vis-mode-normal>:set relativenumber<Enter>")

Of course you would have to do that with ALL of the bind that take you into insert mode.

Tested the code snippit and it works with no noticable delay.

VehementHam avatar Apr 30 '24 02:04 VehementHam

If you take my suggestion, I recommend turning it into a plugin.

VehementHam avatar Apr 30 '24 02:04 VehementHam

In fact this sounds like a feature I want, so if you give me a few days, I will create a plugin for it.

VehementHam avatar Apr 30 '24 17:04 VehementHam

Working on it now.

VehementHam avatar May 12 '24 17:05 VehementHam

Thank you! I unfortunately haven't had time to do this, but I can test out your plug-in when it's ready.

Out of curiosity, what feature were you looking to use this for?

roguh avatar May 12 '24 21:05 roguh

I think an event coming from the C source code would be ideal, I might try to add an event for when the mode changes.

https://github.com/martanne/vis/blob/master/vis-core.h#L226

roguh avatar May 13 '24 21:05 roguh

https://git.disroot.org/oink/vis-smart-numbers

VehementHam avatar May 14 '24 16:05 VehementHam

Obviously, this is not an issue to be tracked here, but a question which belongs more to the vis email list. Whichever way, @VehementHam ’s plugin is probably a good solution for your problem anyway.

@rnpnr , this could be probably be closed.

mcepl avatar May 14 '24 20:05 mcepl

Good call, @mcepl. I just closed it and can email next time.

roguh avatar May 15 '24 17:05 roguh

If you are using my plugin, you should run a git pull. I pushed some important changes.

VehementHam avatar May 17 '24 21:05 VehementHam

I would like to mention that there is issues with the Change Operator, because it does not change to insert mode upon press, but instead waits for the next keybind to be pressed, and depending on what it is, changes to instert mode.

Other than that, the plugin works fine. I would love to see a mode changing event be added in the future.

VehementHam avatar May 19 '24 18:05 VehementHam