Str-to-str-map usage


For example I have the following in Kakoune:

declare-option -hidden str-to-str-map mymap foo=bar baz=whatevs

I would like to read the value with the key, something like:

echo %opt{mymap[foo]}

It should return bar. The syntax above does not work. How can I do that? Is it even possible?


AFAIK You can either

  1. Use the shell to parse the arguments via a %sh block
  2. Use a plugin like to do the same but with a general purpose programming language.
  3. Insert the option text into a buffer, split on whitespace, select the key using regex, something like
    exec -draft "o%opt{ui_options}<a-x>S\h+<ret><a-k>(^foo=)\K.*<ret>""vy<a-x>d"
    would add bar to the v register. Obviously very ugly but it can be hidden by a function easily enough.
  4. Rather than using a single str-to-str-map it’s very possible that you can just use a bunch of options.
declare-option -hidden str mymap_foo bar
declare-option -hidden str mymap_baz whatevs
def -params 1 mymap %{ eval echo "%%opt{mymap_%arg{1}}" }
# both echo 'bar'
echo %opt{mymap_foo}
mymap 'foo'

I agree that there should be proper way to index the dictionary and return bar, a dictionary that can only be indexed when writing is pretty limited.

Here’s a shell function that grabs the value associated with a given key, and an example of how to use it:

eval %sh{
    map_index() {
        eval set -- "$@"
        for entry; do
            case "$entry" in
                    printf "%s\n" "${entry#*=}"

    printf "echo key ncurses_assistant has value: %s\n" \
        "$(map_index ncurses_assistant "$kak_quoted_opt_ui_options")"
1 Like

I get the map from an external script so i do not want to go back to
shell scope, so this third option should be the way to go. Thanks!

I’ve seen something similar in kak-lsp codebase:

Oh hey, I wrote that too! :slight_smile: Although I see there’s been some bugfixes since then.

1 Like

In that case does it not make more sense to modify the output of the script somehow?