zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <pws@csr.com>
To: zsh-users@sunsite.dk
Subject: Re: swapping keys
Date: Mon, 20 Feb 2006 14:50:41 +0000	[thread overview]
Message-ID: <EXCHANGE03q2Y3mEWPQ0000f00b@exchange03.csr.com> (raw)
In-Reply-To: <20060220141907.GA3663@let.rug.nl>

Francisco Borges wrote:
> One possible solution is to switch keys like "`~" and "1!" (I have to
> confess I have only swapped "`~").
> 
> Since I am at it... Is it possible to swap keys inside zsh?

Yes, it's fairly straightforward.  As written it doesn't work for
multibyte characters (e.g. you can't swap with a Euro or Yen or Pound
Sterling in multibyte mode).  That wouldn't be too hard to fix (but it's
not as simple as just removing the test: you need a loop over bytes in
the argument).


# start
# swapkeys X Y
#   swap keys X and Y so typing X gives Y and vice versa
# swapkeys -d X Y
#   completely remove a previous swapping

emulate -L zsh
setopt cbases

integer delete i
local key hexkey

if [[ $1 = -d ]]; then
  delete=1
  shift
fi

if [[ $# -ne 2 || ${#1} -ne 1 || ${#2} -ne 1 ]]; then
  print "Usage: $0 [-d] key1 key2" >&2
  return 1
fi

for (( i = 1; i <= 2; i++ )); do
  key=$argv[i]
  hexkey=$(( [#16] #key ))
  if (( delete )); then
    zle -D insert-key-$hexkey
    bindkey $key self-insert
  else
    eval "insert-key-$hexkey () { LBUFFER+=\$'\\x${hexkey##0x}'; }"
    zle -N insert-key-$hexkey
    bindkey ${argv[3-i]} insert-key-$hexkey
  fi
done
# end

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php


      reply	other threads:[~2006-02-20 14:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-02-19 18:23 alias hl='$(history -n -2 -2) ' zzapper
2006-02-20 11:18 ` Peter Stephenson
2006-02-20 13:17   ` zzapper
2006-02-20 14:19     ` swapping keys Francisco Borges
2006-02-20 14:50       ` Peter Stephenson [this message]

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=EXCHANGE03q2Y3mEWPQ0000f00b@exchange03.csr.com \
    --to=pws@csr.com \
    --cc=zsh-users@sunsite.dk \
    /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).