From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 16672 invoked by alias); 26 Jan 2014 07:51:16 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 18370 Received: (qmail 28019 invoked from network); 26 Jan 2014 07:51:10 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 From: Bart Schaefer Message-id: <140125235111.ZM24519@torch.brasslantern.com> Date: Sat, 25 Jan 2014 23:51:11 -0800 X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-users@zsh.org Subject: Timing out a completion, a different tack MIME-version: 1.0 Content-type: text/plain; charset=us-ascii The script below shows how to create a completer named _timeout that sends zsh an INT signal after a number of seconds configured in the "timeout" zstyle. It will also cause a message to be printed if the completion is interrupted from the keyboard. Add _timeout to the completer zstyle somewhere ahead of _complete and set a timeout zstyle for the appropriate (slow) context, and this will interrupt the completion when the timer expires if it's possible to do so. CAVEAT: On my linux desktop at home, this usually works as expected, but sometimes wedges zsh in the futex() system call. If this happens you are dead in the water and likely have to kill -9 the whole shell from another terminal. This is probably highly kernel-specific and might never happen to you, but you have been warned; this code is offered without warranty. # --- 8< --- 8<--- _timeout() { local timeout zstyle -s ":completion:${curcontext}:" timeout timeout || return 1 # Dirty trick: By unsetting localtraps, we can # push this trap up into our caller's context setopt localoptions nomonitor nolocaltraps trap ' zle -M "Killed by signal in ${funcstack[1]}"; zle -R; return 130' INT QUIT # Background job to kill ourself after 10 seconds { sleep $timeout; kill -INT $$; exit 0 } & # Cleanup routine to remove background job, eval for $! eval " _timeout_end() { unfunction _timeout_end kill $! 2>/dev/null return 0 }" comppostfuncs+=(_timeout_end) # This function adds no completions, so it must not return 0 return 1 } # Tweak this style as needed zstyle ':completion:*' completer _oldlist _timeout _complete _ignored