Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Custom auto complete



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



Messages sorted by: Reverse Date, Date, Thread, Author