# Calling \`fzf\` command from user script

**URL:** <https://discuss.kakoune.com/t/calling-fzf-command-from-user-script/692>\
**Category:** Help\
**Tags:** fzf\
**Created:** [August 19, 2019, 7:48am UTC](https://discuss.kakoune.com/t/calling-fzf-command-from-user-script/692 "2019-08-19T07:48:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vbauerster](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vbauerster](https://discuss.kakoune.com/u/vbauerster)\
**Post date:** [August 19, 2019, 7:48am UTC](https://discuss.kakoune.com/t/calling-fzf-command-from-user-script/692/1 "2019-08-19T07:48:58Z")

</div>

I’ve defined my command named `go-decls`, which calls `fzf` command internally. `fzf` command is part of `fzf` plugin by @andreyorst.  
Now when I call my command for the very first time, like `:go-decls<ret>`, it says `no such command: fzf`.  
I figured out that I need first run any of `fzf` plugin’s command, then my command works.  
I suppose `fzf` module should be loaded first, before I can call `fzf` command in my script.  
How do I augment my command, so it works regardless?

---

<div class="post-metadata">

**Author:** ![andreyorst](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/andreyorst/32/29_2.png) [@andreyorst](https://discuss.kakoune.com/u/andreyorst)\
**Post date:** [August 19, 2019, 7:52am UTC](https://discuss.kakoune.com/t/calling-fzf-command-from-user-script/692/2 "2019-08-19T07:52:59Z")

</div>

just prepend `require-module fzf` before call. If module is already required there’s no significant overhead of calling `require-module` again.

If you 100% want to avoid calling `require-module` every time, you probably can override command inside command like so:

```
define-command fzf-go-decl %{
    require-module fzf
    fzf ...
    define-command -override fzf-go-decl %{
        fzf ...
    }
}

```

But this is much code duplication here, so I would just do:

```
define-command fzf-go-decl %{
    require-module fzf
    fzf-go-decl-impl
    define-command -override fzf-go-decl %{
        fzf-go-decl-impl
    }
}

define-command fzf-go-decl-impl %{
    fzf ...
}

```

If you’re using mappings to call it from `fzf-user-mode`, it’s easier, because you can put your command to the list of mappings of `fzf`, and you will be able to call it from here without requiring:

```
hook global ModuleLoaded fzf %§
    map global fzf -docstring "Go declarations" 'G' '<esc>: fzf-go-decl<ret>'
    define-command fzf-go-decl %{
        fzf ...
    }
§

```

Doing it through mapping is a **recommended way** to extend `fzf.kak`, because by doing this, you’re deferring your configuration in a proper way.

By requiring module too early you can break the rest of `fzf` modules. That is, if you require module `fzf` after fzf.kak and your function was sourced and called, but the rest of files weren’t sourrced yet. I guess this is hard to achieve, if you’re using `plug.kak` but if sourcing plugin manually or via `autoload` this is possible.

---

<div class="post-metadata">

**Author:** ![vbauerster](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vbauerster](https://discuss.kakoune.com/u/vbauerster)\
**Post date:** [August 19, 2019, 8:18am UTC](https://discuss.kakoune.com/t/calling-fzf-command-from-user-script/692/3 "2019-08-19T08:18:49Z")

</div>

Thank you! It works now.

I was thinking to map it to `fzf` menu with `ModuleLoaded` hook, but it is specific to `go` filetype.  
Can I have `fzf` menu item available on specific filetype only?

PS: I do use `plug.kak`

---

<div class="post-metadata">

**Author:** ![vbauerster](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vbauerster](https://discuss.kakoune.com/u/vbauerster)\
**Post date:** [August 19, 2019, 8:27am UTC](https://discuss.kakoune.com/t/calling-fzf-command-from-user-script/692/4 "2019-08-19T08:27:48Z")

</div>

This is what I got, after your recommendation:

```auto
define-command go-decls %{
    require-module fzf
    go-decls-impl
    define-command -override go-decls %{
        go-decls-impl
    }
}

define-command -hidden go-decls-impl %{ evaluate-commands %sh{
    output=$(mktemp ${TMPDIR:-/tmp}/kak-go-decls.XXXXXX)
    motion -mode decls -include func,type -file ${kak_bufname} | jq -c '[.decls[]]' > ${output}

    filter='.[] | "\(.filename):\(.line):\(.col):\t\(.keyword) \(.ident)"'
    items_command="cat $output | jq -r '${filter}' | nl -b a -n ln"
    pcmd=$(printf 'n=$(echo {} | cut -f 1); cat %s | jq -r .[$(($n-1))].full | gofmt' "$output")
    preview="--preview '${pcmd}'"
    # echo "echo -debug items_command: $items_command"
    # echo "echo -debug preview: ${preview}"

    printf "%s\n" "fzf -kak-cmd %{fzf-sk-grep-handler} -items-cmd %{$items_command} -preview -preview-cmd %{$preview} -fzf-args %{--expect $kak_opt_fzf_window_map} -filter %{cut -f 2,3} -post-action %{buffer %opt{fzf_sk_first_file}}"
}}

```

I really like this functionality to outline go file. It may be not perfect in terms of implementation, but seems to work well.

---

<div class="post-metadata">

**Author:** ![andreyorst](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/andreyorst/32/29_2.png) [@andreyorst](https://discuss.kakoune.com/u/andreyorst)\
**Post date:** [August 19, 2019, 8:51am UTC](https://discuss.kakoune.com/t/calling-fzf-command-from-user-script/692/5 "2019-08-19T08:51:46Z")

</div>

> [@vbauerster](#):
>
> I was thinking to map it to `fzf` menu with `ModuleLoaded` hook, but it is specific to `go` filetype.  
> Can I have `fzf` menu item available on specific filetype only?

This is bit trickier. I guess you should go for command overriding, like you did in the answer above this reply, and wrap it to the `go` module loaded hook. But again, calling `require-module` should not add overhead if module was loaded, so you can skip the overriding part and just always call `reuire-module` in your command.

---

<div class="post-metadata">

**Author:** ![andreyorst](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/andreyorst/32/29_2.png) [@andreyorst](https://discuss.kakoune.com/u/andreyorst)\
**Post date:** [August 19, 2019, 1:39pm UTC](https://discuss.kakoune.com/t/calling-fzf-command-from-user-script/692/6 "2019-08-19T13:39:57Z")

</div>

I Guess you sould go for `hook ModuleLoaded go` which will `try` to `require-module fzf` and `define-command fzf-go-decls` and `catch` possible error on requiring `fzf`.

If this code will be evaluated after `fzf.kak` it will work fine. If not, it will throw an error which you’ll be able to see.
