gustavo-hms / peneira

A fuzzy finder crafted for Kakoune
GNU Lesser General Public License v2.1
35 stars 6 forks source link

Weird behaviour with bash block #8

Closed eko234 closed 2 years ago

eko234 commented 2 years ago

Hi, hope you are doing well, I was trying to do some simple script to fuzzy go to tags inside a file, but I think something might be off with the way the bash block is being parsed, I read in the source that there are some stuff going around with some positional args, maybe I'm wrong but I wanted to know if you may help me, my scrip is as follows:


define-command tagss %{
  peneira "GOTOTAG: " %{
    grep -n "$kak_opt_comment_line TAG:" $kak_bufname | awk -F"TAG:" '{print $2}' | xargs -0 echo
  } %{
    exec %sh{echo $1 | awk -F ":" '{printf"%s%s", $1, "g"}'}
  }
}

also , it seems that you can't assign variables in the bash block, thanks for your work, hope you can help me, have a nice one.

eko234 commented 2 years ago

I think the $n args are colliding or somthing.

eko234 commented 2 years ago

I could work it around with a separated bash script, but it would be nice to be able to have it working inside the command definition

gustavo-hms commented 2 years ago

Hi, @eko234 !

I think the $n args are colliding or somthing.

I guess you are right. The problem is that Kakoune converts $n (for n in 1..) to the n-th argument of the surrounding command being defined. Let me show you:

define-command teste -params 2 %{
    echo %sh{ printf "%s: %s" $1 $2 }
}

teste um dois

The above excerpt will print um: dois in the prompt. That's because the $1 and $2 inside the %sh{} expansion get converted to the first and second arguments of the teste command.

So, in your code, the problematic part is the awk script: awk -F"TAG:" '{print $2}'. Kakoune looks at that $2 and thinks you are referencing the second argument of the tagss command you are just defining.

Try replacing it by something like the following:

    grep -n "$kak_opt_comment_line TAG:" $kak_bufname | sed -e "s/.\+TAG: \(.\+\)/\1/"

Does that solve your problem?

eko234 commented 2 years ago

Hi, yes it does, it is indeed a kakoune thing, but it might be worth mentioning in the readme I think, I guess sometimes we need to hack things around :), thanks again, have a nice one.

eko234 commented 2 years ago

for the record, I ended up defining the script in a kakoune option and runing it inside peneira in a bash block like this %{ bash -c "$kak_option_myscript" }, it works well and you don't have to mess with external files.

gustavo-hms commented 2 years ago

You've just remembered me I need to update the docs to make $n behaviour more explicit as you suggested. I hope I'll have some time to work in it this weekend.