mawww / kakoune

mawww's experiment for a better code editor
http://kakoune.org
The Unlicense
9.97k stars 714 forks source link

Add a `-quoting` switch to `evaluate-commands` and `execute-keys` commands #3782

Open alexherbo2 opened 4 years ago

alexherbo2 commented 4 years ago

Raw

evaluate-commands -quoting raw %sh{
  echo '"echo" "Tchou"'
}

Error

No such command: '"echo" "Tchou"'

Kakoune

evaluate-commands -quoting kakoune %sh{
  echo '"echo" %[Tchou]'
}

Shell

evaluate-commands -quoting shell %sh{
  printf '"echo" "Tchou"'
}

JSON

evaluate-commands -quoting json %sh{
  jq -n '["echo", "Tchou"]'
}
krobelus commented 4 years ago

Your -quoting raw is evaluate-commands -verbatim and -quoting kakoune is the default.

The JSON one is interesting because it makes word boundaries very explicit. I wonder what's the use case.

evaluate-commands -quoting shell %sh{ printf '"echo" "Tchou"' }

Shell expansion seems out of scope for evaluate-commands (should it expand $() as well?). It can be done in the sh block, although it's not very nice.

evaluate-commands %sh{
    kakquote() { printf "%s" "$*" | sed "s/'/''/g; 1s/^/'/; \$s/\$/'/"; }

    shell_quoted_command=$(printf '"echo" "Tchou"')

    eval set -- "$shell_quoted_command"
    for arg
    do
        kakquote "$arg"
        printf ' '
    done
}
alexherbo2 commented 4 years ago

Some programs easily output shell quoted arguments, I assume we should not re-evaluate the resulting string (your $(...) comment).