Symbol search command


Small command I found using a lot, which I was talking about in my previous post. I’ve made it outside of fzf.kak just to see if it has any benefit, and I kinda like it, so I decided to post it here:

define-command symbol -params 1 -shell-script-candidates %{
    tags="${TMPDIR:-/tmp}/tags-${kak_buffile##*/}"; tags="${tags%.*}"
    ctags -f $tags $kak_buffile
    readtags -t $tags -l | cut -f 1 | awk '!x[$0]++' | grep -v -e "__anon.*"
    } %{ evaluate-commands %sh{
    tags="${TMPDIR:-/tmp}/tags-${kak_buffile##*/}"; tags="${tags%.*}"
    readtags -t $tags $1 | awk -F '\t|\n' '
        /^!TAGROOT\t/ { tagroot=$2 }
        /[^\t]+\t[^\t]+\t\/\^.*\$?\// {
            sub(".*\t/\\^", "", re); sub("\\$?/$", "", re); gsub("(\\{|\\}|\\\\E).*$", "", re);
            keys=re; gsub(/</, "<lt>", keys); gsub(/\t/, "<c-v><c-i>", keys);
            out = out " %{" $2 " {MenuInfo}" re "} %{evaluate-commands %{ try %{ edit %{" tagroot $2 "}; execute-keys %{/\\Q" keys "<ret>vc} } catch %{ echo %{unable to find tag} } } }"
        /[^\t]+\t[^\t]+\t[0-9]+/ { out = out " %{" $2 ":" $3 "} %{evaluate-commands %{ edit %{" tagroot $2 "} %{" $3 "}}}" }
        END { print ( length(out) == 0 ? "echo -markup %{{Error}no such tag " ENVIRON["tagname"] "}" : "menu -markup -auto-single " out ) }'
    rm $tags

Basically it generates tag file for current buffer in /tmp, parses it immediately and removes it. The only problem I’ve found it that we can’t use ctags-search without modifying options of where to look for tags file, and even if we use it, we can’t remove tags file, in the end since ctags-search parses it again, but we already exited before it finished. So I’ve taken this huge awk part from ctags.kak. I could just use ctags-search but it will provide me all tags base for whole project, when I just really want tags only for current file. I also strip anon tags, since they don’t really matter for me. I don’t really like that awk part, because I can’t understand it completely, but it works fine.

I think that builtin ctags-search command could work in similar way if no tags file found.