Hi,
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?
Thanks,
prion
November 3, 2020, 2:16am
2
AFAIK You can either
Use the shell to parse the arguments via a %sh
block
Use a plugin like https://github.com/gustavo-hms/luar to do the same but with a general purpose programming language.
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.
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() {
key="$1"
shift
eval set -- "$@"
for entry; do
case "$entry" in
"$key"=*)
printf "%s\n" "${entry#*=}"
return
;;
esac
done
}
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 ! Although I see there’s been some bugfixes since then.
1 Like
prion
November 3, 2020, 9:11pm
7
In that case does it not make more sense to modify the output of the script somehow?