From 41f19f8dc9949269e1fc10531890d27e3ea73c8f Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 7 Apr 2019 09:43:40 +1000 Subject: [PATCH] Add support for %file{...} expansions This should make the use case exposed in #2836 implementable. --- doc/pages/expansions.asciidoc | 5 +++++ src/command_manager.cc | 5 +++++ src/command_manager.hh | 1 + test/compose/file-expansion/cmd | 1 + test/compose/file-expansion/in | 1 + test/compose/file-expansion/out | 1 + 6 files changed, 14 insertions(+) create mode 100644 test/compose/file-expansion/cmd create mode 100644 test/compose/file-expansion/in create mode 100644 test/compose/file-expansion/out diff --git a/doc/pages/expansions.asciidoc b/doc/pages/expansions.asciidoc index 49bd1d77..9bdb0ee1 100644 --- a/doc/pages/expansions.asciidoc +++ b/doc/pages/expansions.asciidoc @@ -167,6 +167,11 @@ TIP: These environment variables are also available in other contexts where Kakoune uses a shell command, such as the `|`, `!` or `$` normal mode commands (See <>). +== File expansions + +Expansions with the type `file` will expand to the content of the filename +given in argument as read from the host filesystem. + == Value expansions Expansions with the type `val` give access to Kakoune internal data that is diff --git a/src/command_manager.cc b/src/command_manager.cc index f777eeda..5e8c8196 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -5,6 +5,7 @@ #include "buffer_utils.hh" #include "context.hh" #include "flags.hh" +#include "file.hh" #include "optional.hh" #include "option_types.hh" #include "ranges.hh" @@ -168,6 +169,8 @@ Token::Type token_type(StringView type_name, bool throw_on_invalid) return Token::Type::ValExpand; else if (type_name == "arg") return Token::Type::ArgExpand; + else if (type_name == "file") + return Token::Type::FileExpand; else if (throw_on_invalid) throw parse_error{format("unknown expand '{}'", type_name)}; else @@ -327,6 +330,8 @@ expand_token(const Token& token, const Context& context, const ShellContext& she throw runtime_error("invalid argument index"); return {arg < params.size() ? params[arg] : String{}}; } + case Token::Type::FileExpand: + return {read_file(content)}; case Token::Type::RawEval: return {expand(content, context, shell_context)}; case Token::Type::Raw: diff --git a/src/command_manager.hh b/src/command_manager.hh index 3270b217..0468a1fc 100644 --- a/src/command_manager.hh +++ b/src/command_manager.hh @@ -51,6 +51,7 @@ struct Token OptionExpand, ValExpand, ArgExpand, + FileExpand, CommandSeparator }; diff --git a/test/compose/file-expansion/cmd b/test/compose/file-expansion/cmd new file mode 100644 index 00000000..121f644f --- /dev/null +++ b/test/compose/file-expansion/cmd @@ -0,0 +1 @@ +:echo -to-file commands 'foo bar':execute-keys i %file{commands} esc> diff --git a/test/compose/file-expansion/in b/test/compose/file-expansion/in new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/test/compose/file-expansion/in @@ -0,0 +1 @@ + diff --git a/test/compose/file-expansion/out b/test/compose/file-expansion/out new file mode 100644 index 00000000..d675fa44 --- /dev/null +++ b/test/compose/file-expansion/out @@ -0,0 +1 @@ +foo bar