martanne / vis

A vi-like editor based on Plan 9's structural regular expressions
Other
4.19k stars 260 forks source link

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

Closed roguh closed 1 month ago

roguh commented 3 months ago

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?

fischerling commented 3 months ago

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.

VehementHam commented 2 months ago

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 commented 2 months ago

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

VehementHam commented 1 month ago

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 commented 1 month ago

Working on it now.

roguh commented 1 month ago

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 commented 1 month ago

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

VehementHam commented 1 month ago

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

mcepl commented 1 month ago

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.

roguh commented 1 month ago

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

VehementHam commented 1 month ago

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

VehementHam commented 1 month ago

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.