Tag stack

One feature from Vim that I was missing in Kakoune is the tag stack. Here is a very basic implementation:

declare-option str-list tagstack ""
declare-option str tagstack_cmd "ctags-search"

define-command -override -params 1 tagstack-push %{
  set-option global tagstack "%val{buffile}" "%val{cursor_line}" \
    "%val{cursor_column}" "%arg{1}" %opt{tagstack}
  try %{
    %opt{tagstack_cmd} "%arg{1}"
  } catch %{
    tagstack-pop
    fail "tag not found %arg{1}"
  }
}

define-command -override tagstack-pop %{
  evaluate-commands %sh{
    eval "set -- $kak_quoted_opt_tagstack"
    if [ "$1" ]; then
      echo edit "$1" "$2" "$3"
      shift 4
    else
      echo fail "reached bottom of tag stack"
    fi
    echo set global tagstack "$@"
  }
}

define-command -override tagstack-print %{
  info -title "tag stack" -- %sh{
    eval "set -- $kak_quoted_opt_tagstack"
    count=$((($# / 4)))
    while [ "$1" ]; do
      printf "%2d\t%-30s\t%s:%s:%s\n" $count "$4" "$1" "$2" "$3"
      count=$((count - 1))
      shift 4
    done
  }
}

# suggested mapping for those, who do not use the t command
# map global normal t "<a-i>w: tagstack-push <c-r>.<ret>"
# map global normal T ": tagstack-pop<ret>"
# map global normal <a-t> ": tagstack-print<ret>"

It actually doesn’t do very much except for saving the position from where i jumped to a tag. It is kind of like a second jump list. But this covers about 99% of my use cases for the Vim tag stack. You can change the tagstack_cmd to any command you want, that takes an argument and does a jump.

2 Likes

Awesome little commands, I would caution people on binding over “t” as that is a fairly heavy used command for many of us.

Yes, thank you! I updated the comment about it. I used t in Vim very much, but in Kakoune it is not as useful because <a-.> does not jump to the next match and you cannot jump back. And in most cases <a-]> does exactly the same thing that I used t for in Vim.