zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Timing out a completion, a different tack
Date: Sat, 25 Jan 2014 23:51:11 -0800	[thread overview]
Message-ID: <140125235111.ZM24519@torch.brasslantern.com> (raw)

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


                 reply	other threads:[~2014-01-26  7:51 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=140125235111.ZM24519@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).