martanne / vis

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

Lua API: access and set all available options #1114

Closed rnpnr closed 10 months ago

rnpnr commented 11 months ago

the first point of this commit is to allow all options to be read from lua. this has a number of uses for plugin writers. they are grouped into a couple of tables depending on what they control: vis.options: table with global configuration win.options: table with window specific configuration

the second point is to allow you to set all these options as if they were simply lua variables. technically this is already possible by using vis:command("set ...") but personally I think this interface is cleaner. Note that this already possible for some things like the current mode (eg. vis.mode = vis.modes.VISUAL). examples: vis.options.ai = true vis.options.brk = " !?."

there are a number of related issues and pull requests: closes #803: Lua API: let plugins read the values of options closes #812: Window layout property supersedes/closes #717: Add ability to access tabwidth from Lua supersedes/closes #1066: expose UI layout and allow it to be set from lua API

rnpnr commented 11 months ago

Note: I mostly made this patch to familize myself more with how the Lua-C interface works. For instance this patch could have its size split in half if people think that vis:command("set ...") is sufficient.

I do think that the ability to read the options from Lua is useful and would like to merge at least that part.

ninewise commented 11 months ago

I like this. It's much more ergonomic to have a lua api for these settings. Code looks good on quick read as well.

rnpnr commented 11 months ago

In that case I can also add the ability to set the options with a table. For example:

vis.options = { expandtab = true, tabwidth = 4 }

It will be a very minor modification.

One other thing to note is that I had to rename some options to be valid in lua (eg. show-tabs -> showtabs). I think I should probably rename the options elsewhere to keep everything consistent.

rnpnr commented 10 months ago

I implemented the ability to set options from a table, for example:

vis.options = { autoindent = true, expandtab = false }

This ended up being slightly more complicated than I originally thought so I had to move the string parsing to separate functions so that it could be reused. I am still quite happy with result.

Additionally the options I needed to rename were also changed in sam.c so that everything is consistent.

While this is a big patch I think its overall a net positive. It will also mean that there will be no more requests for exposing single options that people need for writing plugins and that we won't need to make a breaking API change when we get sick of all the options being assigned to the top level vis and win tables (ie. vis.tabwidth, vis.autoindent etc).

mcepl commented 10 months ago

I am afraid this doesn't apply at all on the top of #675.

rnpnr commented 10 months ago

Its applied fine in my local testing branch. I think the problem is that

675 needs to be squashed. I aim to apply that one and possibly this one

later tonight.

milhnl commented 10 months ago

For those that want this behavior in their current version of vis, see https://github.com/milhnl/vis-options-backport