martanne / vis

A vi-like editor based on Plan 9's structural regular expressions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

roguh opened this issue · 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?

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.

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.

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

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

Working on it now.

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?

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

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.

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

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

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.