Giter Club home page Giter Club logo

luar's Introduction

Luar

Luar is a minimalist plugin to script Kakoune using Lua. It's not designed to expose Kakoune's internals like Vis or Neovim do. Instead, it's conceived with Kakoune's extension model in mind. It does so by defining a sole command (lua) which can execute whatever string is passed to it in an external lua interpreter. By doing so, it can act as a complement for the %sh{} expansion when you need to run some logic inside Kakoune.

Usage

First of all, require the provided module:

require-module luar

The luar module exports a lua command, which executes the code passed to it in an external lua interpreter. The code is interpreted as the body of an anonymous function, and whatever this anonymous function returns replaces the current selections. So, the following code:

lua %{
    return "Olá!"
}

replaces your selections with Olá!.

In the same vein, if you have, say, three selections, the code:

lua %{
    return 17, 19, 23
}

replaces each selection with 17, 19 and 23 respectivelly. The same can be achieved by returning a single table with three elements:

lua %{
    return {17, 19, 23}
}

The two forms are equivalent.

If, on the other hand, you return nothing, the content of the selections won't be modified:

lua %{
    if true then
        return
    end
}

The anonymous function can take arguments by passing values before the %{} block:

lua 17 19 %{
    return arg[1] + arg[2]
}

The above code will replace all the selections with 36. As you can see, the arguments can be accessed with the arg table.

As a convenience, you can use the provided args function to name your arguments:

lua 17 19 %{
    local first, second = args()
    return second - first
}

Since Kakoune does not process expansions inside these lua %{} blocks, you need to pass expansions as arguments if you need to inspect Kakoune's state:

lua %val{client} %{
    local client = args()
    return string.format("I'm client “%s”", client)
}

Finally, you can run all commands defined in Kakoune (including third party ones) from lua code using the provided kak module:

lua %{
    kak.set_register("/", "Search this!")
    kak.execute_keys('%s<ret>cSearch that!<esc>')
}

As you can see, hyphens are replaced by underscores in command names.

External modules

Since Lua modules are just plain tables and require is just a simple function, you can import modules everywhere in your program, not just at the beginning of a file. In particular, you can import external modules inside the :lua command. For instance, if you need to parse the contents of a file, you can use the elegant LPeg library:

lua %val{buffile} %{
    local lpeg = require "lpeg"

    local function parse(file)
        -- do the lpeg's magic here
    end

    local tree = parse(arg[1])
    -- ...
}

You can also use this functionality to split your plugin into separate modules and use :lua to glue them together. To make that easier, luar provides the addpackagepath convenience function. It configures the lua interpreter to search for lua modules in the provided directory. It's meant to be used like this:

declare-option -hidden str my_plugin_path %sh{ dirname $kak_source }

define-command my-command %{
    lua %opt{my_plugin_path} %{
        addpackagepath(arg[1])
        local module = require "my_local_module"
        -- ...
    }
}

Some examples

The following examples are for didactic purposes. There are other ways to achieve the same results.

Suppose you want to execute ctags-update-tags whenever you write to a file, but only if there's already a tags file in the current directory. Using :lua you can write the following lines to your kakrc:

hook global BufWritePost .* %{
    lua %{
        if io.open("tags") then kak.ctags_update_tags() end
    }
}

Now suppose you want to define a mapping to toggle the highlight of search patterns in the current window when you press F2. To achieve that, you can do something like this:

declare-option -hidden bool highlight_search_on false

define-command highlight-search-toggle %{
    lua %opt{highlight_search_on} %{
        local is_on = args()

        if is_on then
            kak.remove_highlighter("window/highlight-search")
        else
            kak.add_highlighter("window/highlight-search", "dynregex", "%reg{/}", "0:default,+ub")
        end

        kak.set_option("window", "highlight_search_on", not is_on)
    }
}

map global normal <F2> ': highlight-search-toggle<ret>'

You can find more examples searching Github by topic.

Installation

You must have a lua interpreter installed on your system. Then you can add the following line to your kakrc (supposing you use plug.kak):

plug "gustavo-hms/luar" %{
    require-module luar
}

Configuration

You can also change the Lua interpreter used by this plugin by changing the luar_interpreter option, e.g.:

# use luajit to run all Lua snippets
set-option global luar_interpreter luajit

luar's People

Contributors

alexherbo2 avatar drgmr avatar enricozb avatar gustavo-aqg avatar gustavo-hms avatar mralusw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

luar's Issues

do kak module methods return values?

Hi, I'm trying out luar, and I wanted to know if the methods provided by the kak module actually return values or if they might take higher order functions...

I'm particularly interested in using stuff like on-key and being able to stay in a lua block to define some commands, I'd guess it doesn't, but also wanted to know your perspective on the subject.

Can I go Fennel with luar?

I'd be interested in using Fennel as you use lua in lua %{} blocks. Fennel is a Lisp that compiles down to Lua, so perhaps it could be possible to make a wrapper that will use luar underneath. What do you think?

Install lua interpreter on demand

The fzf plugin for vim has a handy feature: it can automatically install a version of the fzf binary on the user's machine, freeing the user of the burden of dealing with the plugin dependency and making it works out of the box.

The lua interpreter is very tiny (around 300 Kb in a 64 bits Linux machine) and can be downloaded much faster than fzf, almost unnoticeably. So, it would be nice if Luar could detect that a lua interpreter is missing and automatically installs one. This has the potential to make people less worried about publishing Kakoune plugins written using Luar.

It could be something in the following lines:

  • on ModuleLoaded, a script would check if a lua executable is available. If so, nothing happens;
  • if, on the other hand, a lua executable could not be found, download a tar file from here, unpack it in a predetermined location and set a hidden option to inform the lua command to use the custom binary instead of relying on a system installation.
  • optionally, allow the user to set an option informing Luar to not download the interpreter automatically.

Add a way to pass named parameters

It could be nice to access the arguments in the Lua block with a name instead of its position.

By the way, I do not fully understand why the client variable is not a list and why args is a function and not just a variable.

lua %val{client} %{
  local client = args()
}

Confusion when passing %val{selections} as an argument

When writting:

    lua %val{selections} %val{main_reg_hash} %{
        sels = arg[1]
        main_index = arg[2]

I would expect sels to be a table with selections, instead I get the value of my first selection because it is arg holding the table instead of arg[1]. Then, how can I get the value of my second argument?

Confusion in the doc

I dont' understand when caling lua replace the selection and when it echo the return value, both case are shown in the readme.

How to use %opt and %val inside lua

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}' ) 
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.