gustavo-hms / luar

Script Kakoune using Lua
GNU Lesser General Public License v3.0
45 stars 3 forks source link

How to use %opt and %val inside lua #16

Closed sucrecacao closed 3 years ago

sucrecacao commented 3 years ago

How can I have the following code add the current selection length and not literally "%val{selection_length}":

lua %{
  kak.set_option('-add', 'buffer', 'lengths', '%val{selection_lengths}' ) 
}
gustavo-hms commented 3 years ago

Hi, @sucrecacao !

As we can read from the docs:

Expansions are processed when unquoted and anywhere inside double-quoted strings, but not inside unquoted words, inside single-quoted strings, or inside %-strings or other expansions.

So, the %val{selections_length} expansion is not processed inside the lua %{} code block simply because expansions are not processed inside %-strings. To achieve what you want, I see two options:

Use double quotes for the code block

The following works as expected:

lua " kak.set_option('-add', 'buffer', 'lengths', %val{selections_length}) "

Pass the expansion as an argument to the block

You can also do what the Luar docs suggests:

lua %val{selections_length} %{
    kak.set_option('-add', 'buffer', 'lengths', arg[1])
}

Both options work, so you can choose whichever best fits for you.