How to write zsh autocomplete that replaces last argument?

Viewed 206

I want to provide an autocomplete that will search for results online. The idea is that user enters some search term, presses tab, and gets results of an online search based on that term. The results don't necessarily begin with the term user entered, or even contain it (more likely that their descriptions contain the term).

Here's an illustration of possible flow:

  1. User enters search term and presses tab to trigger autocompletion
$ bookmark hello<tab>
  1. Autocomplete erases entered search term (because we search by description, not information that we want to enter), makes a network request, and presents autocomplete options.
$ bookmark 
www.hellomagazine.com                      -- HELLO! - Daily royal, ...
en.wikipedia.org/wiki/Hello_(Adele_song)   -- Hello (Adele song) - Wikipedia
...
  1. User selects one of possible options
$ bookmark www.hellomagazine.com 

Is this a good approach at all? Maybe things like this are usually done using a different pattern?

So I've encountered a few problems while doing it:

  1. When I use _arguments to present possible answers, zsh filters them by term that user already entered. Probably I need to erase entered last word first? How do I do it?
  2. Zsh sorts the results alphabetically, bu I want to present them by relevance (in order that I pass them to _arguments). How to do it?
  3. Zsh makes multiple network requests for the same term. First time user presses tab it makes a request but doesn't present suggestion list yet. On the second tab press the list of options is presented but it needs to repeat the same slow network request to do it. How do I cache or present results list the first time?
0 Answers
Related