# Problem when creating grep like command

**URL:** <https://discuss.kakoune.com/t/problem-when-creating-grep-like-command/1839>\
**Category:** Help\
**Created:** [August 15, 2021, 7:37pm UTC](https://discuss.kakoune.com/t/problem-when-creating-grep-like-command/1839 "2021-08-15T19:37:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![TuM0xA-S](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/tum0xa-s/32/959_2.png) [@TuM0xA-S](https://discuss.kakoune.com/u/TuM0xA-S)\
**Post date:** [August 15, 2021, 7:37pm UTC](https://discuss.kakoune.com/t/problem-when-creating-grep-like-command/1839/1 "2021-08-15T19:37:58Z")

</div>

hi, another noob question  
so, this is command to run go build ./… and pipe output to grep buffer.

```
define-command -docstring %{
    go build ./...
} go-build %{ evaluate-commands %sh{
     output=$(mktemp -d "${TMPDIR:-/tmp}"/kak-go-build.XXXXXXXX)/fifo
     mkfifo ${output}
     ( go build ./... 2>&1 | tr -d '\r' > ${output} 2>&1 & ) > /dev/null 2>&1 < /dev/null

     printf %s\\n "evaluate-commands -try-client '$kak_opt_toolsclient' %{
               edit! -fifo ${output} *build*
               set-option buffer filetype grep
               set-option buffer grep_current_line 0
               hook -always -once buffer BufCloseFifo .* %{
                   try %{
                       exec '<a-K>^$<ret>'
                       echo -markup '{Error}build failed'
                   } catch %{
                       echo 'build ok'
                       db '*build*' 
                   }
                   nop %sh{ rm -r $(dirname ${output}) }
           }}"
}}

```

basically, it is copypaste from grep.kak with addition of echo command.  
point of using echos is that i can switch to another buffer, then have notification about result.  
problem: echos not work.  
it is because hook works in buffer context and it doesnt have window?

---

<div class="post-metadata">

**Author:** ![prion](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/prion/32/404_2.png) [@prion](https://discuss.kakoune.com/u/prion)\
**Post date:** [August 15, 2021, 11:24pm UTC](https://discuss.kakoune.com/t/problem-when-creating-grep-like-command/1839/2 "2021-08-15T23:24:35Z")

</div>

There’s already a make command built in, should just use it

```auto
hook global WinSetOption file type=go %{
    set option window makecmd 'go build and whatever else'
}

```

---

<div class="post-metadata">

**Author:** ![TuM0xA-S](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/tum0xa-s/32/959_2.png) [@TuM0xA-S](https://discuss.kakoune.com/u/TuM0xA-S)\
**Post date:** [August 16, 2021, 8:12am UTC](https://discuss.kakoune.com/t/problem-when-creating-grep-like-command/1839/3 "2021-08-16T08:12:25Z")

</div>

yeah, thx. It is better than copypaste. but why echos not work?

---

<div class="post-metadata">

**Author:** ![occivink](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/occivink/32/27_2.png) [@occivink](https://discuss.kakoune.com/u/occivink)\
**Post date:** [August 16, 2021, 8:19am UTC](https://discuss.kakoune.com/t/problem-when-creating-grep-like-command/1839/4 "2021-08-16T08:19:55Z")

</div>

> [@TuM0xA-S](#):
>
> it is because hook works in buffer context and it doesnt have window?

That’s correct, the hook is executed in a draft context without any knowledge of the current client. You can resolve this by doing `eval -client '$kak_client' %{ echo "build ok" }`, assuming you’re running the go-build command from the current client.

---

<div class="post-metadata">

**Author:** ![TuM0xA-S](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/tum0xa-s/32/959_2.png) [@TuM0xA-S](https://discuss.kakoune.com/u/TuM0xA-S)\
**Post date:** [August 16, 2021, 8:43am UTC](https://discuss.kakoune.com/t/problem-when-creating-grep-like-command/1839/5 "2021-08-16T08:43:45Z")

</div>

ok, this is exactly what i searching for.  
but,  
why that hook runs in disposable context if `-draft` switch not specified? i mean that hook have buffer scope too

```auto
hook buffer InsertKey .* %{
    echo 'works in buffer scope actually' # expected
}

```

```auto
hook buffer InsertKey .* %{eval %{
    echo 'works too (no -draft swith)' # expected
}}

```

```auto
hook buffer InsertKey .* %{eval -draft %{
    echo 'not work (have -draft swith)' # expected
}}

```

on what ‘executing in draft context’ depends?

---

<div class="post-metadata">

**Author:** ![occivink](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/occivink/32/27_2.png) [@occivink](https://discuss.kakoune.com/u/occivink)\
**Post date:** [August 16, 2021, 8:52am UTC](https://discuss.kakoune.com/t/problem-when-creating-grep-like-command/1839/6 "2021-08-16T08:52:56Z")

</div>

hooks run in different contexts based on what triggers them. InsertKey is tied to a client, so it runs in the client context. The `:doc hooks` page has some information about this (such as what context some hooks are run in), but not comprehensive.

It’s also not guaranteed what the absence of `-draft` means. Consider nested eval calls:

```auto
eval -draft %{
    eval %{ 
        echo hello
    }
}

```

Even though the innermost `eval` is not draft, the echo will not be printed. In this case it’s obvious why, but if you were to put the inner eval into a command, you would not be able to tell what goes wrong just by looking at the command. Similarly, you don’t see the full picture when you just declare a hook.

---

<div class="post-metadata">

**Author:** ![TuM0xA-S](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/tum0xa-s/32/959_2.png) [@TuM0xA-S](https://discuss.kakoune.com/u/TuM0xA-S)\
**Post date:** [August 16, 2021, 8:55am UTC](https://discuss.kakoune.com/t/problem-when-creating-grep-like-command/1839/7 "2021-08-16T08:55:47Z")

</div>

context == scope?  
scope can be: window, buffer, global  
context can be: ???  
by context you mean outer evals?

---

<div class="post-metadata">

**Author:** ![TuM0xA-S](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/tum0xa-s/32/959_2.png) [@TuM0xA-S](https://discuss.kakoune.com/u/TuM0xA-S)\
**Post date:** [August 16, 2021, 9:07am UTC](https://discuss.kakoune.com/t/problem-when-creating-grep-like-command/1839/8 "2021-08-16T09:07:39Z")

</div>

> [@TuM0xA-S](#):
>
> scope can be: window, buffer, global  
> context can be: ???

so,

1. scope defines where hook work
2. context defines how it work(tied or not tied to client)

---

<div class="post-metadata">

**Author:** ![Screwtapello](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/screwtapello/32/12_2.png) [@Screwtapello](https://discuss.kakoune.com/u/Screwtapello)\
**Post date:** [August 16, 2021, 9:59am UTC](https://discuss.kakoune.com/t/problem-when-creating-grep-like-command/1839/9 "2021-08-16T09:59:54Z")

</div>

> [@TuM0xA-S](#):
>
> context can be: ???

I haven’t found a concise definition of what a context is, but I wrote an introduction to them in another comment a while ago:

> [@Pipe \`buffer-next\` to kakoune session](https://discuss.kakoune.com/t/pipe-buffer-next-to-kakoune-session/1067/2):
>
> If you start up Kakoune, you can run :buffer-next and it will switch buffers. If you run :new, you get a new client running in a new window, and if you run :buffer-next there, the first window doesn’t switch buffers - :buffer-next must be run in the context of a particular client, and it only affects that client. Likewise, you can run :eval -draft %{ buffer-next }, and nothing happens. The documentation for :eval says that the -draft option “runs the commands in a draft context”; Kakoune creat…

---

<div class="post-metadata">

**Author:** ![occivink](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/occivink/32/27_2.png) [@occivink](https://discuss.kakoune.com/u/occivink)\
**Post date:** [August 16, 2021, 10:11am UTC](https://discuss.kakoune.com/t/problem-when-creating-grep-like-command/1839/10 "2021-08-16T10:11:08Z")

</div>

I think the best way to get an idea about what ‘context’ means is to look at the [corresponding class in the source code](https://github.com/mawww/kakoune/blob/master/src/context.hh#L52), in particular the member variables. As you can see, a context is a collection of window / client / buffer / selections, some of which may be empty, which would then cause the behavior to change.
