From 3ef5bf53d5747c2369ff5124e9bdba95d7a72dee Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 16 Jun 2014 19:49:58 +0100 Subject: [PATCH 1/4] Add BufCloseFifo hook and use that to remove fifo directories --- README.asciidoc | 8 ++++++-- rc/git-tools.kak | 2 +- rc/grep.kak | 2 +- rc/make.kak | 2 +- src/buffer_utils.cc | 2 ++ 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/README.asciidoc b/README.asciidoc index add67f68..c88d5d52 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -677,10 +677,14 @@ existing hooks are: * +BufOpen+: A buffer for an existing file has been created, filename is used for filtering * +BufCreate+: A buffer has been created, filename is used for filtering - * +BufWritePre+: Executre just before a buffer is written, filename is + * +BufWritePre+: Executed just before a buffer is written, filename is used for filtering. - * +BufWritePost+: Executre just after a buffer is written, filename is + * +BufWritePost+: Executed just after a buffer is written, filename is used for filtering. + * +BufClose+: Executed when a buffer is deleted, while it is still valid. + * +BufCloseFifo+: Executed when a fifo buffer closes its fifo file descriptor + either because the buffer is being deleted, or because the writing + end has been closed. * +RuntimeError+: an error was encountered while executing an user command the error message is used for filtering * +KakBegin+: Kakoune started, this is called just after reading the user diff --git a/rc/git-tools.kak b/rc/git-tools.kak index b53238ed..a34022c7 100644 --- a/rc/git-tools.kak +++ b/rc/git-tools.kak @@ -39,7 +39,7 @@ def -shell-params \ echo "eval -try-client '$kak_opt_docsclient' %{ edit! -fifo ${output} *git* set buffer filetype '${filetype}' - hook buffer BufClose .* %{ nop %sh{ rm -r $(dirname ${output}) } } + hook buffer BufCloseFifo .* %{ nop %sh{ rm -r $(dirname ${output}) } } }" } diff --git a/rc/grep.kak b/rc/grep.kak index 728dce76..8029e956 100644 --- a/rc/grep.kak +++ b/rc/grep.kak @@ -14,7 +14,7 @@ def -shell-params -file-completion \ echo "eval -try-client '$kak_opt_toolsclient' %{ edit! -fifo ${output} -scroll *grep* set buffer filetype grep - hook buffer BufClose .* %{ nop %sh{ rm -r $(dirname ${output}) } } + hook buffer BufCloseFifo .* %{ nop %sh{ rm -r $(dirname ${output}) } } }" }} diff --git a/rc/make.kak b/rc/make.kak index c020a4e0..213328cf 100644 --- a/rc/make.kak +++ b/rc/make.kak @@ -9,7 +9,7 @@ def -shell-params make %{ %sh{ echo "eval -try-client '$kak_opt_toolsclient' %{ edit! -fifo ${output} -scroll *make* set buffer filetype make - hook buffer BufClose .* %{ nop %sh{ rm -r $(dirname ${output}) } } + hook buffer BufCloseFifo .* %{ nop %sh{ rm -r $(dirname ${output}) } } }" }} diff --git a/src/buffer_utils.cc b/src/buffer_utils.cc index f05db777..5f8cb9fd 100644 --- a/src/buffer_utils.cc +++ b/src/buffer_utils.cc @@ -64,6 +64,7 @@ Buffer* create_fifo_buffer(String name, int fd, bool scroll) buffer->flags() &= ~Buffer::Flags::Fifo; buffer->flags() &= ~Buffer::Flags::NoUndo; close(fifo); + buffer->run_hook_in_own_context("BufCloseFifo", ""); delete &watcher; } }); @@ -74,6 +75,7 @@ Buffer* create_fifo_buffer(String name, int fd, bool scroll) if (buffer->flags() & Buffer::Flags::Fifo) { close(watcher->fd()); + buffer->run_hook_in_own_context("BufCloseFifo", ""); delete watcher; } }); From 62707aa82d407436c517f241c38eed68b5d158af Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 18 Jun 2014 19:12:11 +0100 Subject: [PATCH 2/4] make.kak: display error description in the status line on errjump --- rc/make.kak | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rc/make.kak b/rc/make.kak index 213328cf..38850b64 100644 --- a/rc/make.kak +++ b/rc/make.kak @@ -28,12 +28,12 @@ decl str jumpclient def errjump -docstring 'Jump to error location' %{ try %{ exec gll "Entering directory" - exec s "Entering directory '([^']+)'.*\n([^:]+):(\d+):(\d+):[^\n]+\'" l - eval -try-client %opt{jumpclient} edit %rec{%reg{1}/%reg{2}} %reg{3} %reg{4} + exec s "Entering directory '([^']+)'.*\n([^:]+):(\d+):(\d+):([^\n]+)\'" l + eval -try-client %opt{jumpclient} %rec{edit %reg{1}/%reg{2} %reg{3} %reg{4}; echo -color Information '%reg{5}'} try %{ focus %opt{jumpclient} } } catch %{ - exec ghgl s "([^:]+):(\d+):(\d+):[^\n]+\'" l - eval -try-client %opt{jumpclient} edit %reg{1} %reg{2} %reg{3} + exec ghgl s "([^:]+):(\d+):(\d+):([^\n]+)\'" l + eval -try-client %opt{jumpclient} %rec{edit %reg{1} %reg{2} %reg{3}; echo -color Information '%reg{4}'} try %{ focus %opt{jumpclient} } } } From 9aa8c8a60a03396b7ed679537e3ed6b6de02ce8e Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 18 Jun 2014 19:28:48 +0100 Subject: [PATCH 3/4] Add a %val{...} expand type %val{name} gives access to values given in shell through $kak_ --- src/command_manager.cc | 5 +++++ src/shell_manager.cc | 30 +++++++++++++++++------------- src/shell_manager.hh | 1 + 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/command_manager.cc b/src/command_manager.cc index a99f89b3..874d0290 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -55,6 +55,7 @@ struct Token ShellExpand, RegisterExpand, OptionExpand, + ValExpand, CommandSeparator }; Token() : m_type(Type::Raw) {} @@ -150,6 +151,8 @@ Token::Type token_type(StringView type_name) return Token::Type::RegisterExpand; else if (type_name == "opt") return Token::Type::OptionExpand; + else if (type_name == "val") + return Token::Type::ValExpand; else if (type_name == "rec") return Token::Type::RawEval; else if (throw_on_invalid) @@ -316,6 +319,8 @@ String eval_token(const Token& token, Context& context, return RegisterManager::instance()[content].values(context)[0]; case Token::Type::OptionExpand: return context.options()[content].get_as_string(); + case Token::Type::ValExpand: + return ShellManager::instance().get_val(content, context); case Token::Type::RawEval: return eval(content, context, shell_params, env_vars); case Token::Type::Raw: diff --git a/src/shell_manager.cc b/src/shell_manager.cc index d7caceb5..08c34160 100644 --- a/src/shell_manager.cc +++ b/src/shell_manager.cc @@ -108,21 +108,12 @@ String ShellManager::pipe(StringView input, setenv(("kak_" + name).c_str(), local_var->second.c_str(), 1); else { - auto env_var = std::find_if( - m_env_vars.begin(), m_env_vars.end(), - [&](const std::pair& pair) - { return boost::regex_match(name.begin(), name.end(), - pair.first); }); - - if (env_var != m_env_vars.end()) + try { - try - { - String value = env_var->second(name, context); - setenv(("kak_"_str + name).c_str(), value.c_str(), 1); - } - catch (runtime_error&) {} + String value = get_val(name, context); + setenv(("kak_"_str + name).c_str(), value.c_str(), 1); } + catch (runtime_error&) {} } ++it; @@ -149,4 +140,17 @@ void ShellManager::register_env_var(StringView regex, m_env_vars.push_back({ Regex(regex.begin(), regex.end()), std::move(retriever) }); } +String ShellManager::get_val(StringView name, const Context& context) const +{ + auto env_var = std::find_if( + m_env_vars.begin(), m_env_vars.end(), + [&](const std::pair& pair) + { return boost::regex_match(name.begin(), name.end(), + pair.first); }); + + if (env_var == m_env_vars.end()) + throw runtime_error("no such env var: " + name); + return env_var->second(name, context); +} + } diff --git a/src/shell_manager.hh b/src/shell_manager.hh index 16a5f27e..f5d27a03 100644 --- a/src/shell_manager.hh +++ b/src/shell_manager.hh @@ -28,6 +28,7 @@ public: int* exit_status = nullptr); void register_env_var(StringView regex, EnvVarRetriever retriever); + String get_val(StringView name, const Context& context) const; private: std::vector> m_env_vars; From a5ff40c10716f3e5ef0de8536b5b436c47162975 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 18 Jun 2014 19:30:40 +0100 Subject: [PATCH 4/4] make.kak: Add :errnext and :errprev for jumping between errors --- rc/make.kak | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/rc/make.kak b/rc/make.kak index 38850b64..91de2f70 100644 --- a/rc/make.kak +++ b/rc/make.kak @@ -1,5 +1,6 @@ decl str makecmd make decl str toolsclient +decl -hidden int _make_current_error_line def -shell-params make %{ %sh{ output=$(mktemp -d -t kak-make.XXXXXXXX)/fifo @@ -9,6 +10,7 @@ def -shell-params make %{ %sh{ echo "eval -try-client '$kak_opt_toolsclient' %{ edit! -fifo ${output} -scroll *make* set buffer filetype make + set buffer _make_current_error_line 0 hook buffer BufCloseFifo .* %{ nop %sh{ rm -r $(dirname ${output}) } } }" }} @@ -29,11 +31,29 @@ def errjump -docstring 'Jump to error location' %{ try %{ exec gll "Entering directory" exec s "Entering directory '([^']+)'.*\n([^:]+):(\d+):(\d+):([^\n]+)\'" l + set buffer _make_current_error_line %val{cursor_line} eval -try-client %opt{jumpclient} %rec{edit %reg{1}/%reg{2} %reg{3} %reg{4}; echo -color Information '%reg{5}'} try %{ focus %opt{jumpclient} } } catch %{ exec ghgl s "([^:]+):(\d+):(\d+):([^\n]+)\'" l + set buffer _make_current_error_line %val{cursor_line} eval -try-client %opt{jumpclient} %rec{edit %reg{1} %reg{2} %reg{3}; echo -color Information '%reg{4}'} try %{ focus %opt{jumpclient} } } } + +def errnext -docstring 'Jump to next error' %{ + eval -try-client %opt{jumpclient} %{ + buffer '*make*' + exec %rec{%opt{_make_current_error_line}ggl/[0-9]+: error:} + errjump + } +} + +def errprev -docstring 'Jump to previous error' %{ + eval -try-client %opt{jumpclient} %{ + buffer '*make*' + exec %rec{%opt{_make_current_error_line}ggh[0-9]+: error:} + errjump + } +}