Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add -quote argument to kakoune #5211

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/kak.1
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
.Op Ar file ...
.
.Nm
.Fl quote Sy raw|shell|kakoune
.Op Ar string ...
.
.Nm
.Fl p Ar session_id
.
.Nm
Expand Down Expand Up @@ -151,6 +155,12 @@ Begin in
.Em readonly mode ,
all the buffers opened will not be written to disk.
.
.It Fl quote Sy raw|shell|kakoune Op Ar string ...
Escape one or more
.Ar strings
for interpolation into kakoune or shell scripts.
Multiple strings will be escaped first and then joined with spaces inbetween.
.
.It Sy + Ns Ar line Ns Oo Sy \&: Ns Ar column Oc | Sy +:
Specify a target line and column for the first file.
When the plus sign is followed by only a colon, then the cursor is sent
Expand Down
10 changes: 4 additions & 6 deletions rc/detection/modeline.kak
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ declare-option -docstring "amount of lines that will be checked at the beginning

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

# Translate a vim option into the corresponding kakoune one
translate_opt_vim() {
local key="$1"
Expand Down Expand Up @@ -54,7 +52,7 @@ define-command -hidden modeline-parse-impl %{
return;;
esac

printf 'set-option buffer %s %s\n' "${key}" "$(kakquote "${value}")"
printf 'set-option buffer %s %s\n' "${key}" "$(kak -quote kakoune -- "${value}")"
}

# Pass a few whitelisted options to kakoune directly
Expand All @@ -64,19 +62,19 @@ define-command -hidden modeline-parse-impl %{

case "${key}" in
scrolloff|tabstop|indentwidth|autowrap_column|eolformat|filetype|BOM|spell_lang);;
*) printf 'echo -debug %s' "$(kakquote "Unsupported kakoune variable: ${key}")" \
*) printf 'echo -debug %s' "$(kak -quote kakoune -- "Unsupported kakoune variable: ${key}")" \
| kak -p "${kak_session}"
return;;
esac

printf 'set-option buffer %s %s\n' "${key}" "$(kakquote "${value}")"
printf 'set-option buffer %s %s\n' "${key}" "$(kak -quote kakoune -- "${value}")"
}

case "${kak_selection}" in
*vi:*|*vim:*) type_selection="vim";;
*kak:*|*kakoune:*) type_selection="kakoune";;
*)
printf 'fail %s\n' "$(kakquote "Unsupported modeline format: ${kak_selection}")"
printf 'fail %s\n' "$(kak -quote kakoune -- "Unsupported modeline format: ${kak_selection}")"
exit 1 ;;
esac

Expand Down
15 changes: 6 additions & 9 deletions rc/tools/git.kak
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ define-command -params 1.. \
exit 1
}
}
kakquote() {
printf "%s" "$1" | sed "s/'/''/g; 1s/^/'/; \$s/\$/'/"
}

show_git_cmd_output() {
local filetype
Expand Down Expand Up @@ -255,7 +252,7 @@ define-command -params 1.. \
n=$#
eval set -- "$(cat ${kak_response_fifo})" "$@"
if [ $# -eq $((n+1)) ]; then
echo fail -- "$(kakquote "$1")"
echo fail -- "$(kak -quote kakoune -- "$1")"
exit
fi
commit=$1
Expand All @@ -266,15 +263,15 @@ define-command -params 1.. \
# Log commit and file name because they are only echoed briefly
# and not shown elsewhere (we don't have a :messages buffer).
message="Blaming $file as of $(git rev-parse --short $commit)"
echo "echo -debug -- $(kakquote "$message")"
echo "echo -debug -- $(kak -quote kakoune -- "$message")"
on_close_fifo="
execute-keys -client ${kak_client} ${cursor_line}g<a-h>${cursor_column}lh
evaluate-commands -client ${kak_client} %{
set-option buffer git_blob $(kakquote "$commit:$file")
git blame $(for arg; do kakquote "$arg"; printf " "; done)
set-option buffer git_blob $(kak -quote kakoune -- "$commit:$file")
git blame $(kak -quote kakoune -- "$@")
hook -once window NormalIdle .* %{
execute-keys vv
echo -markup -- $(kakquote "{Information}{\\}$message. Press <ret> to jump to blamed commit")
echo -markup -- $(kak -quote kakoune -- "{Information}{\\}$message. Press <ret> to jump to blamed commit")
}
}
" show_git_cmd_output show "$commit:$file"
Expand Down Expand Up @@ -565,7 +562,7 @@ define-command -params 1.. \
'
eval set -- "$(cat ${kak_response_fifo})"
if [ $# -eq 1 ]; then
echo fail -- "$(kakquote "$1")"
echo fail -- "$(kak -quote kakoune -- "$1")"
exit
fi
starting_commit=$1
Expand Down
57 changes: 28 additions & 29 deletions rc/tools/menu.kak
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ define-command menu -params 1.. -docstring %{
printf %s "$2"
exit
fi
shellquote() {
printf "'%s'" "$(printf %s "$1" | sed "s/'/'\\\\''/g; s/§/§§/g; $2")"
}
cases=
select_cases=
completion=
Expand All @@ -43,43 +40,45 @@ define-command menu -params 1.. -docstring %{
command=$2
completion="${completion}${title}${nl}"
cases="${cases}
($(shellquote "$title" s/¶/¶¶/g))
printf '%s\\n' $(shellquote "$command" s/¶/¶¶/g)
$(kak -quote shell -- "$title"))
printf '%s\\n' $(kak -quote shell -- "$command")
;;"
if $select_cmds; then
select_command=$3
select_cases="${select_cases}
($(shellquote "$title" s/¶/¶¶/g))
printf '%s\\n' $(shellquote "$select_command" s/¶/¶¶/g)
$(kak -quote shell -- "$title"))
printf '%s\\n' $(kak -quote shell -- "$select_command")
;;"
fi
shift $stride
done
printf "\
prompt '' %%§
evaluate-commands %%sh¶
case \"\$kak_text\" in \
%s
(*) echo fail -- no such item: \"'\$(printf %%s \"\$kak_text\" | sed \"s/'/''/g\")'\" ;;
esac
§" "$cases"

printf "%s" "prompt '' $(kak -quote kakoune -- "
evaluate-commands %sh$(kak -quote kakoune -- '
case "$kak_text" in
'"${cases}"'
*) echo fail -- no such item: "$(kak -quote kakoune -- "$kak_text")"
esac
')
")"

if $select_cmds; then
printf " \
-on-change %%§
evaluate-commands %%sh¶
case \"\$kak_text\" in \
%s
(*) : ;;
esac
§" "$select_cases"
printf "%s" " -on-change $(kak -quote kakoune -- "
evaluate-commands %sh$(kak -quote kakoune -- '
case "$kak_text" in
'"$select_cases"'
*) : ;;
esac
')
")"
fi

if [ -n "$on_abort" ]; then
printf " -on-abort '%s'" "$(printf %s "$on_abort" | sed "s/'/''/g")"
printf "%s" " -on-abort $(kak -quote kakoune -- "$on_abort")"
fi
printf ' -menu -shell-script-candidates %%§
printf %%s %s
§\n' "$(shellquote "$completion")"

printf "%s\n" " -menu -shell-script-candidates $(kak -quote kakoune -- "
printf %s $(kak -quote shell -- "$completion")
")"
}
}
8 changes: 8 additions & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,7 @@ int main(int argc, char* argv[])
{ "debug", { ArgCompleter{}, "initial debug option value" } },
{ "version", { {}, "display kakoune version and exit" } },
{ "ro", { {}, "readonly mode" } },
{ "quote", { ArgCompleter{}, "echo each argument with the given quoting (raw, kakoune, or shell)" } },
{ "help", { {}, "display a help message and quit" } } }
};

Expand Down Expand Up @@ -1153,6 +1154,13 @@ int main(int argc, char* argv[])
return 0;
}

if (auto quoting = parser.get_switch("quote"))
{
write_stdout(join(parser | transform(quoter(option_from_string(Meta::Type<Quoting>{}, *quoting))),
' ', false));
return 0;
}

if (auto session = parser.get_switch("p"))
{
for (auto opt : { "c", "n", "s", "d", "e", "E", "ro" })
Expand Down