Remove setg/setb/setw and use set <scope>

(with scope a prefix of global,buffer or window)
This commit is contained in:
Maxime Coste 2013-10-30 09:38:40 +00:00
parent cc01aab8b8
commit feff965ac6
15 changed files with 64 additions and 76 deletions

View File

@ -353,7 +353,7 @@ Options are typed, their type can be
Options value can be changed using the +set+ commands: Options value can be changed using the +set+ commands:
-------------------------------------------------------------- --------------------------------------------------------------
:set{b,w,g} <option> <value> # buffer, window, or global scope :set [global,buffer,window] <option> <value> # buffer, window, or global scope
-------------------------------------------------------------- --------------------------------------------------------------
Option values can be different by scope, an option can have a global Option values can be different by scope, an option can have a global

View File

@ -70,7 +70,7 @@ This is a very simple example, most of the time, the echo command will as
well contains well contains
----- -----
setb filetype <...> set buffer filetype <...>
----- -----
and some hooks for this filetype will have been written and some hooks for this filetype will have been written
@ -101,7 +101,7 @@ decl str plugin_filename
%sh{ %sh{
# ask Kakoune to write current buffer to temporary file # ask Kakoune to write current buffer to temporary file
filename=$(mktemp -t kak-temp.XXXXXXXX) filename=$(mktemp -t kak-temp.XXXXXXXX)
echo "setb plugin_filename '$filename' echo "set buffer plugin_filename '$filename'
write '$filename'" write '$filename'"
} }
# End the %sh{} so that it's output gets executed by Kakoune. # End the %sh{} so that it's output gets executed by Kakoune.
@ -117,7 +117,7 @@ nop %sh{ ( # launch a detached shell
# generate completion option value # generate completion option value
completions="$line.$column@$kak_timestamp:$candidates" completions="$line.$column@$kak_timestamp:$candidates"
# write to Kakoune socket for the buffer that triggered the completion # write to Kakoune socket for the buffer that triggered the completion
echo "setb -buffer '${kak_bufname}' completions '$completions'" | echo "set buffer=${kak_bufname} completions '$completions'" |
socat stdin UNIX-SOCKET:/tmp/kak-${kak_session} socat stdin UNIX-SOCKET:/tmp/kak-${kak_session}
) >& /dev/null < /dev/null & } ) >& /dev/null < /dev/null & }
----- -----

View File

@ -460,47 +460,30 @@ void exec_commands_in_file(CommandParameters params,
CommandManager::instance().execute(file_content, context); CommandManager::instance().execute(file_content, context);
} }
void set_global_option(CommandParameters params, Context& context) static OptionManager& get_options(const String& scope, const Context& context)
{
if (prefix_match("global", scope))
return GlobalOptions::instance();
else if (prefix_match("buffer", scope))
return context.buffer().options();
else if (prefix_match("window", scope))
return context.window().options();
else if (prefix_match(scope, "buffer="))
return BufferManager::instance().get_buffer(scope.substr(7_byte)).options();
throw runtime_error("error: no such option container " + scope);
}
void set_option(CommandParameters params, Context& context)
{ {
ParametersParser parser(params, { { "add", false } }, ParametersParser parser(params, { { "add", false } },
ParametersParser::Flags::OptionsOnlyAtStart, ParametersParser::Flags::OptionsOnlyAtStart,
2, 2); 3, 3);
Option& opt = GlobalOptions::instance().get_local_option(parser[0]); Option& opt = get_options(parser[0], context).get_local_option(parser[1]);
if (parser.has_option("add")) if (parser.has_option("add"))
opt.add_from_string(parser[1]); opt.add_from_string(parser[2]);
else else
opt.set_from_string(parser[1]); opt.set_from_string(parser[2]);
}
void set_buffer_option(CommandParameters params, Context& context)
{
ParametersParser parser(params, { { "buffer", true}, { "add", false } },
ParametersParser::Flags::OptionsOnlyAtStart,
2, 2);
OptionManager& options = parser.has_option("buffer") ?
BufferManager::instance().get_buffer(parser.option_value("buffer")).options()
: context.buffer().options();
Option& opt = options.get_local_option(parser[0]);
if (parser.has_option("add"))
opt.add_from_string(parser[1]);
else
opt.set_from_string(parser[1]);
}
void set_window_option(CommandParameters params, Context& context)
{
ParametersParser parser(params, { { "add", false } },
ParametersParser::Flags::OptionsOnlyAtStart,
2, 2);
Option& opt = context.window().options().get_local_option(parser[0]);
if (parser.has_option("add"))
opt.add_from_string(parser[1]);
else
opt.set_from_string(parser[1]);
} }
void declare_option(CommandParameters params, Context& context) void declare_option(CommandParameters params, Context& context)
@ -873,21 +856,26 @@ void register_commands()
cm.register_command("echo", echo_message); cm.register_command("echo", echo_message);
cm.register_command("debug", write_debug_message); cm.register_command("debug", write_debug_message);
cm.register_commands({ "setg", "setglobal" }, set_global_option, cm.register_command("set", set_option,
PerArgumentCommandCompleter({ [](const Context& context, CommandParameters params, size_t token_to_complete, ByteCount pos_in_token)
[](const Context& context, const String& prefix, ByteCount cursor_pos) {
{ return GlobalOptions::instance().complete_option_name(prefix, cursor_pos); } if (token_to_complete == 0)
})); {
cm.register_commands({ "setb", "setbuffer" }, set_buffer_option, CandidateList res;
PerArgumentCommandCompleter({ for (auto scope : { "global", "buffer", "window" })
[](const Context& context, const String& prefix, ByteCount cursor_pos) {
{ return context.buffer().options().complete_option_name(prefix, cursor_pos); } if (params.size() == 0 or prefix_match(scope, params[0].substr(0_byte, pos_in_token)))
})); res.emplace_back(scope);
cm.register_commands({ "setw", "setwindow" }, set_window_option, }
PerArgumentCommandCompleter({ return res;
[](const Context& context, const String& prefix, ByteCount cursor_pos) }
{ return context.window().options().complete_option_name(prefix, cursor_pos); } else if (token_to_complete == 1)
})); {
OptionManager& options = get_options(params[0], context);
return options.complete_option_name(params[1], pos_in_token);
}
return CandidateList{};
} );
cm.register_commands({"ca", "colalias"}, define_color_alias); cm.register_commands({"ca", "colalias"}, define_color_alias);
cm.register_commands({"nc", "nameclient"}, set_client_name); cm.register_commands({"nc", "nameclient"}, set_client_name);

View File

@ -1,4 +1,4 @@
hook global BufCreate .*\.asciidoc %{ setb filetype asciidoc } hook global BufCreate .*\.asciidoc %{ set buffer filetype asciidoc }
hook global WinSetOption filetype=asciidoc %{ hook global WinSetOption filetype=asciidoc %{
addhl group asciidoc-highlight addhl group asciidoc-highlight

View File

@ -4,7 +4,7 @@ decl str clang_options
def clang-complete %{ def clang-complete %{
%sh{ %sh{
filename=$(mktemp -d -t kak-clang.XXXXXXXX)/buffer.cpp filename=$(mktemp -d -t kak-clang.XXXXXXXX)/buffer.cpp
echo "setb clang_filename $filename" echo "set buffer clang_filename $filename"
echo "write $filename" echo "write $filename"
} }
# end the previous %sh{} so that its output gets interpreted by kakoune # end the previous %sh{} so that its output gets interpreted by kakoune
@ -23,7 +23,7 @@ def clang-complete %{
for cmp in ${output}; do for cmp in ${output}; do
completions="${completions}:${cmp}" completions="${completions}:${cmp}"
done done
echo "eval -client $kak_client %[ echo completed; setb completions '${completions}' ]" | socat -u stdin UNIX-CONNECT:/tmp/kak-${kak_session} echo "eval -client $kak_client %[ echo completed; set buffer completions '${completions}' ]" | socat -u stdin UNIX-CONNECT:/tmp/kak-${kak_session}
) >& /dev/null < /dev/null & ) >& /dev/null < /dev/null &
} }
} }

View File

@ -1,11 +1,11 @@
hook global BufCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) %{ hook global BufCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) %{
setb filetype cpp set buffer filetype cpp
} }
hook global BufOpen .* %{ %sh{ hook global BufOpen .* %{ %sh{
mimetype="$(file -b --mime-type ${kak_bufname})" mimetype="$(file -b --mime-type ${kak_bufname})"
if [[ "${mimetype}" == "text/x-c++" || "${mimetype}" == "text/x-c" ]]; then if [[ "${mimetype}" == "text/x-c++" || "${mimetype}" == "text/x-c" ]]; then
echo setb filetype cpp; echo set buffer filetype cpp;
fi fi
} } } }

View File

@ -1,5 +1,5 @@
hook global BufCreate .*\.(diff|patch) %{ hook global BufCreate .*\.(diff|patch) %{
setb filetype diff set buffer filetype diff
} }
hook global WinSetOption filetype=diff %{ hook global WinSetOption filetype=diff %{

View File

@ -27,7 +27,7 @@ def -shell-params git %{ %sh{
echo "edit! -scratch *git* echo "edit! -scratch *git*
exec |cat<space>${tmpfile}<ret>gk exec |cat<space>${tmpfile}<ret>gk
nop %sh{rm ${tmpfile}} nop %sh{rm ${tmpfile}}
setb filetype '${filetype}'" set buffer filetype '${filetype}'"
[[ -n "$kak_opt_docsclient" ]] && echo "}" [[ -n "$kak_opt_docsclient" ]] && echo "}"
else else
@ -40,7 +40,7 @@ def -shell-params git %{ %sh{
( (
echo "eval -client '$kak_client' %{ echo "eval -client '$kak_client' %{
try %{ addhl flag_lines magenta git_blame_flags } catch %{} try %{ addhl flag_lines magenta git_blame_flags } catch %{}
setb -buffer '$kak_bufname' git_blame_flags '' set buffer=$kak_bufname git_blame_flags ''
}" | socat -u stdin UNIX-CONNECT:/tmp/kak-${kak_session} }" | socat -u stdin UNIX-CONNECT:/tmp/kak-${kak_session}
declare -A authors declare -A authors
declare -A dates declare -A dates
@ -51,7 +51,7 @@ def -shell-params git %{ %sh{
for (( i=1; $i < $count; i++ )); do for (( i=1; $i < $count; i++ )); do
flag="$flag:$(($line+$i))|black|$text" flag="$flag:$(($line+$i))|black|$text"
done done
echo "setb -add -buffer '$kak_bufname' git_blame_flags %{${flag}}" | socat -u stdin UNIX-CONNECT:/tmp/kak-${kak_session} echo "set buffer -add buffer=$kak_bufname git_blame_flags %{${flag}}" | socat -u stdin UNIX-CONNECT:/tmp/kak-${kak_session}
} }
git blame --incremental $kak_bufname | ( while read blame_line; do git blame --incremental $kak_bufname | ( while read blame_line; do
if [[ $blame_line =~ ([0-9a-f]{40}).([0-9]+).([0-9]+).([0-9]+) ]]; then if [[ $blame_line =~ ([0-9a-f]{40}).([0-9]+).([0-9]+).([0-9]+) ]]; then
@ -84,7 +84,7 @@ def -shell-params git %{ %sh{
flags="$flags:$line|red|-" flags="$flags:$line|red|-"
fi fi
done done
echo "setb git_diff_flags '$flags'" echo "set buffer git_diff_flags '$flags'"
} }
} }

View File

@ -1,5 +1,5 @@
hook global BufCreate .*COMMIT_EDITMSG %{ hook global BufCreate .*COMMIT_EDITMSG %{
setb filetype git-commit set buffer filetype git-commit
} }
hook global WinSetOption filetype=git-commit %{ hook global WinSetOption filetype=git-commit %{
@ -13,7 +13,7 @@ hook global WinSetOption filetype=(?!git-commit).* %{
} }
hook global BufCreate .*git-rebase-todo %{ hook global BufCreate .*git-rebase-todo %{
setb filetype git-rebase set buffer filetype git-rebase
} }
hook global WinSetOption filetype=git-rebase %{ hook global WinSetOption filetype=git-rebase %{

View File

@ -14,7 +14,7 @@ def -shell-params -file-completion \
[[ -n "$kak_opt_toolsclient" ]] && echo "eval -client '$kak_opt_toolsclient' %{" [[ -n "$kak_opt_toolsclient" ]] && echo "eval -client '$kak_opt_toolsclient' %{"
echo "edit! -fifo ${output} *grep* echo "edit! -fifo ${output} *grep*
setb filetype grep set buffer filetype grep
hook buffer BufClose .* %{ nop %sh{ rm -r $(dirname ${output}) } }" hook buffer BufClose .* %{ nop %sh{ rm -r $(dirname ${output}) } }"
[[ -n "$kak_opt_toolsclient" ]] && echo "}" [[ -n "$kak_opt_toolsclient" ]] && echo "}"

View File

@ -1,15 +1,15 @@
hook global BufCreate (.*/)?(kakrc|.*.kak) %{ hook global BufCreate (.*/)?(kakrc|.*.kak) %{
setb filetype kak set buffer filetype kak
} }
hook global WinSetOption filetype=kak %{ hook global WinSetOption filetype=kak %{
addhl group kak-highlight addhl group kak-highlight
addhl -group kak-highlight regex \<(hook|rmhooks|addhl|rmhl|addfilter|rmfilter|exec|eval|source|runtime|def|decl|echo|edit|set[gbw])\> 0:keyword addhl -group kak-highlight regex \<(hook|rmhooks|addhl|rmhl|addfilter|rmfilter|exec|eval|source|runtime|def|decl|echo|edit|set)\> 0:keyword
addhl -group kak-highlight regex \<(default|black|red|green|yellow|blue|magenta|cyan|white)\> 0:value addhl -group kak-highlight regex \<(default|black|red|green|yellow|blue|magenta|cyan|white)\> 0:value
addhl -group kak-highlight regex (?<=\<hook)\h+((global|buffer|window)|(\S+))\h+(\S+)\h+(\H+) 2:attribute 3:error 4:identifier 5:string addhl -group kak-highlight regex (?<=\<hook)\h+((global|buffer|window)|(\S+))\h+(\S+)\h+(\H+) 2:attribute 3:error 4:identifier 5:string
addhl -group kak-highlight regex (?<=\<set)\h+((global|buffer|window)|(\S+))\h+(\S+)\h+(\H+) 2:attribute 3:error 4:identifier 5:value
addhl -group kak-highlight regex (?<=\<regex)\h+(\S+) 1:string addhl -group kak-highlight regex (?<=\<regex)\h+(\S+) 1:string
addhl -group kak-highlight regex (["'])(?:\\\1|.)*?\1 0:string addhl -group kak-highlight regex (["'])(?:\\\1|.)*?\1 0:string
addhl -group kak-highlight regex (?<=\<set[gbw])\h+(\S+)\h+(\S+) 1:identifier 2:value
addhl -group kak-highlight regex (^|\h)\#[^\n]*\n 0:comment addhl -group kak-highlight regex (^|\h)\#[^\n]*\n 0:comment
} }

View File

@ -1,7 +1,7 @@
hook global BufOpen .* %{ %sh{ hook global BufOpen .* %{ %sh{
mimetype="$(file -b --mime-type ${kak_bufname})" mimetype="$(file -b --mime-type ${kak_bufname})"
if [[ "${mimetype}" == "message/rfc822" ]]; then if [[ "${mimetype}" == "message/rfc822" ]]; then
echo setb filetype mail; echo set buffer filetype mail;
fi fi
} } } }

View File

@ -9,7 +9,7 @@ def -shell-params make %{ %sh{
[[ -n "$kak_opt_toolsclient" ]] && echo "eval -client '$kak_opt_toolsclient' %{" [[ -n "$kak_opt_toolsclient" ]] && echo "eval -client '$kak_opt_toolsclient' %{"
echo "edit! -fifo ${output} *make* echo "edit! -fifo ${output} *make*
setb filetype make set buffer filetype make
hook buffer BufClose .* %{ nop %sh{ rm -r $(dirname ${output}) } }" hook buffer BufClose .* %{ nop %sh{ rm -r $(dirname ${output}) } }"
[[ -n "$kak_opt_toolsclient" ]] && echo "}" [[ -n "$kak_opt_toolsclient" ]] && echo "}"

View File

@ -6,7 +6,7 @@ hook global WinSetOption filetype=man %{
addhl -group man-highlight regex ^\h+-+[-a-zA-Z_]+ 0:yellow addhl -group man-highlight regex ^\h+-+[-a-zA-Z_]+ 0:yellow
addhl -group man-highlight regex [-a-zA-Z_.]+\(\d\) 0:green addhl -group man-highlight regex [-a-zA-Z_.]+\(\d\) 0:green
hook window -id man-hooks NormalKey <c-m> man hook window -id man-hooks NormalKey <c-m> man
setb tabstop 8 set buffer tabstop 8
} }
hook global WinSetOption filetype=(?!man).* %{ hook global WinSetOption filetype=(?!man).* %{
@ -28,7 +28,7 @@ def -shell-params man %{ %sh{
echo "edit! -scratch '*man*' echo "edit! -scratch '*man*'
exec |cat<space>${tmpfile}<ret>gk exec |cat<space>${tmpfile}<ret>gk
nop %sh{rm ${tmpfile}} nop %sh{rm ${tmpfile}}
setb filetype man" set buffer filetype man"
else else
echo "echo %{man '$@' failed: see *debug* buffer for details }" echo "echo %{man '$@' failed: see *debug* buffer for details }"
rm ${tmpfile} rm ${tmpfile}

View File

@ -1,11 +1,11 @@
hook global BufCreate .*\.(sh) %{ hook global BufCreate .*\.(sh) %{
setb filetype sh set buffer filetype sh
} }
hook global BufOpen .* %{ %sh{ hook global BufOpen .* %{ %sh{
mimetype="$(file -b --mime-type ${kak_bufname})" mimetype="$(file -b --mime-type ${kak_bufname})"
if [[ "${mimetype}" == "text/x-shellscript" ]]; then if [[ "${mimetype}" == "text/x-shellscript" ]]; then
echo setb filetype sh; echo set buffer filetype sh;
fi fi
} } } }