zsh-workers
 help / color / mirror / code / Atom feed
* Custom auto complete
@ 2014-08-03 16:17 Domagoj Pintaric
  2014-08-04 20:39 ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Domagoj Pintaric @ 2014-08-03 16:17 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 1081 bytes --]

Hello,

I wold like to write my own zsh auto complete function/code but i need some
reference to start with. This is is what i want to do:

I wold like to parse the command that is currently typed and if there is a
"?" char followed by a <TAB> key press to show a menu of options to
compete. So for example if I start typing:

> cp ?<TAB>

i would like zsh to shows me a menu of strings to replace the "?" char
with. Options would also have to be numerated so it would possible to write:

>cp ?1<TAB>

The strings I would like to be displayed would be stored in a file.

If found some tutorials on how to write my own auto complete functions but
the problem is I want this to work anywhere in the command string that is
currently typed not just if i start with that command.

I understand that the "?" char might have to be escaped or something but
it's not important that the "trigger" char is "?" it can be something else.

So my question is how do i get zsh to display a custom auto complete menu
when I press <TAB> and the previous char is, for example, "?".

Regards,
Domagoj

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Custom auto complete
  2014-08-03 16:17 Custom auto complete Domagoj Pintaric
@ 2014-08-04 20:39 ` Bart Schaefer
  2014-08-04 20:57   ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2014-08-04 20:39 UTC (permalink / raw)
  To: Domagoj Pintaric, zsh-workers

On Aug 3,  6:17pm, Domagoj Pintaric wrote:
}
} I wold like to parse the command that is currently typed and if there
} is a "?" char followed by a <TAB> key press to show a menu of options
} to compete.

You should be able to do this by writing a completer function, that is,
a function to be added to the "completer" zstyle.

Something like this:

    _query() {
      local -a answers; answers=( ${(f)"$(<answerfile)"} )
      case ${words[CURRENT]} in
      # Handle '?<digit>' first using backreferences
      ((#b)\?(<->)) answers=( ${answers[$match[1]]} ) ;& # fall through
      # Now anything starting with '?'
      (\?*) _message "Querying answerfile"
            compstate[insert]=menu
            compadd -U -a answers  # -U to replace "?" with the result
            ;;
      # Else fail so the next completer is tried
      (*) return 1;
      esac
    }

Just add _query to whatever is in your existing style at the point where
you want this to be tried, e.g.:

    zstyle ':completion:*' completer _query _expand _complete


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Custom auto complete
  2014-08-04 20:39 ` Bart Schaefer
@ 2014-08-04 20:57   ` Bart Schaefer
  2014-08-05 22:37     ` Domagoj Pintaric
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2014-08-04 20:57 UTC (permalink / raw)
  To: Domagoj Pintaric, zsh-workers

On Aug 4,  1:39pm, Bart Schaefer wrote:
}
}       # Now anything starting with '?'
}       (\?*) _message "Querying answerfile"
}             compstate[insert]=menu
}             compadd -U -a answers  # -U to replace "?" with the result
}             ;;

Hmm, it occurs to me that this should probably be in the order

            compadd -U -a answers  # -U to replace "?" with the result
            compstate[insert]=menu

so that the initial value of compstate[insert] is visible to compadd
before we change it.  I don't know exactly when that would matter, but
the doc for compstate[insert] implies that it might.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Custom auto complete
  2014-08-04 20:57   ` Bart Schaefer
@ 2014-08-05 22:37     ` Domagoj Pintaric
  2014-08-06  3:45       ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Domagoj Pintaric @ 2014-08-05 22:37 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 2276 bytes --]

Hi,

The solution you posted sort of dose what I want. With this code I have to
type ?<some char(s)> in order for it to work. If I just type ? and hit tab
it dose not work.
I get that's because of  ( \?* ) and I tried (\?) but it dose not work.
Interesting, if I use for example ( \! ) that works as expected. Is the '?'
special for some reason?


Also I found another way of getting what I want I think it's a more
intuitive solution than the whole '?' idea. I found that I can write my own
widget and bind it to a key so this is what I did:

zle -C custom-complete menu-expand-or-complete _parse_sb_file

_parse_sb_file() {
WORDS=( ${(f)"$(<$SB_FILE_PATH)"} )
 ARRAY=()
integer POS=1
for ITEM in ${WORDS}; do
 ARRAY[${POS}]=${POS}") "${WORDS[${POS}]}
(( POS++ ))
        done
compadd -l -d ARRAY -a -S '' -- WORDS
}

bindkey ${CTRLTAB_KEY} custom-complete

This works but I have some issues with this to:

1) if I press ctrl-tab I can not select the options with arrow keys I can
just switch them with ctrl-tab, unless I first run for example "cp <TAB>"
then I get the menu and I can select options. And if I after that (after cp
<TAB> showed the menu with select) use ctrl-tab I get menu with select? Not
sure why I first have invoke another menu for ctrl-tab to work in a same
manner?

2) if I have this string, for example, in a file "cp file1 file2" it is
inserted like this "cp\ file1\ file2". The white spaces are escaped, how
can I disable this, and tell zsh to insert a string as it is.

Thanks for your help.

Regards,
Domagoj




On Mon, Aug 4, 2014 at 10:57 PM, Bart Schaefer <schaefer@brasslantern.com>
wrote:

> On Aug 4,  1:39pm, Bart Schaefer wrote:
> }
> }       # Now anything starting with '?'
> }       (\?*) _message "Querying answerfile"
> }             compstate[insert]=menu
> }             compadd -U -a answers  # -U to replace "?" with the result
> }             ;;
>
> Hmm, it occurs to me that this should probably be in the order
>
>             compadd -U -a answers  # -U to replace "?" with the result
>             compstate[insert]=menu
>
> so that the initial value of compstate[insert] is visible to compadd
> before we change it.  I don't know exactly when that would matter, but
> the doc for compstate[insert] implies that it might.
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Custom auto complete
  2014-08-05 22:37     ` Domagoj Pintaric
@ 2014-08-06  3:45       ` Bart Schaefer
  0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2014-08-06  3:45 UTC (permalink / raw)
  To: Domagoj Pintaric; +Cc: zsh-workers

On Aug 6, 12:37am, Domagoj Pintaric wrote:
}
} The solution you posted sort of dose what I want. With this code I have to
} type ?<some char(s)> in order for it to work. If I just type ? and hit tab
} it dose not work.

Hm.  I did test it.  Could you post the actual function?

} I get that's because of  ( \?* ) and I tried (\?) but it dose not work.
} Interesting, if I use for example ( \! ) that works as expected. Is the '?'
} special for some reason?

Depends on the order of your completer style.  I only tried with _query
as the first one.  If e.g. you have _expand in there ahead of _query,
_expand will try to use the "?" on the command line as a glob pattern.

You haven't really told us enough details to make a diagnosis.  On the
other hand you seem to prefer the custom key binding, so maybe it does
not matter.

} zle -C custom-complete menu-expand-or-complete _parse_sb_file

Why did you chose menu-expand-or-complete here, rather than just
menu-complete?  When would you be using this binding for expansion?

See also below ...

} _parse_sb_file () {
}         WORDS=(${(f)"$(<$SB_FILE_PATH)"}) 
}         ARRAY=() 
}         integer POS=1
}         for ITEM in ${WORDS}
}         do
}                 ARRAY[${POS}]=${POS}") "${WORDS[${POS}]} 
}                 (( POS++ ))
}         done
}         compadd -l -d ARRAY -a -S '' -- WORDS
} }
} 
} bindkey ${CTRLTAB_KEY} custom-complete
} 
} This works but I have some issues with this to:
} 
} 1) if I press ctrl-tab I can not select the options with arrow keys I can
} just switch them with ctrl-tab, unless I first run for example "cp <TAB>"

This is very likely because you haven't yet loaded the zsh/complist module
the first time you try this, or you've loaded it but not set $MENUSELECT.
When you invoke the completion system with <TAB> it loads the module and
sets the variable for you, and thereafter menu completion notices that
$MENUSELECT has become set and enters the selection list.

If you load the module yourself, you can bind to the menu-select widget
and force entry into the selection list every time:

    zmodload zsh/complist
    zle -C custom-complete menu-select _parse_sb_file

} 2) if I have this string, for example, in a file "cp file1 file2" it is
} inserted like this "cp\ file1\ file2". The white spaces are escaped, how
} can I disable this, and tell zsh to insert a string as it is.

You need the -Q option when calling "compadd".  However, note that when
deciding to re-enter completion, the command line is going to be split
on spaces, even if those spaces were inserted by a previous completion.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-08-06  3:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-03 16:17 Custom auto complete Domagoj Pintaric
2014-08-04 20:39 ` Bart Schaefer
2014-08-04 20:57   ` Bart Schaefer
2014-08-05 22:37     ` Domagoj Pintaric
2014-08-06  3:45       ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).