zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <Peter.Stephenson@csr.com>
To: <zsh-users@zsh.org>
Subject: Re: zle insert problems
Date: Tue, 2 Aug 2011 16:18:34 +0100	[thread overview]
Message-ID: <20110802161834.3a7d02ca@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <4E380FF2.3020309@gmx.net>

On Tue, 2 Aug 2011 16:55:46 +0200
Pascal Wittmann <PascalWittmann@gmx.net> wrote:
> Ok, that would work in this simple example. But actually I want to do
> some more stuff (the code above was just a minimal working example),
> here is the function:
> 
>  9 replace-pacman-command() {
> 10   if [[ $LBUFFER = "pacman"* ]]; then
> 11      zle beginning-of-line
> 12      zle forward-word
> 13      zle delete-word
> 14      zle -U -- $@
> 15      zle end-of-line
> 16   fi
> 17 }
> 18
> 19 replace-pacman-command-insert() {
> 20   replace-pacman-command "-S"
> 21 }
> 22
> 23 zle -N replace-pacman-command-insert
> 24 bindkey "^[i" replace-pacman-command-insert

To back up Mikael and answer your original question: yes, you're doing
this the wrong way.  The commands you're executing move the cursor and
edit the buffer as for user interaction, which will be painful because
actually there is no user interaction.  All you're trying to do is edit
the text in the buffer, without intervention from the user.  So you're
much better off operating on BUFFER or equivalent, where instead of a
sequence of operations you can encode the whole operation in patterns,
in fact one pattern.  This does require you to know a bit about zsh
patterns, but the following should be enough to get you going.

I'm not sure why you need this only to work when the first word is
"pacman", given the user is initiating this explicitly so should be able
to decide whether they want the word replaced, but to follow what you're
doing...  I'm assuming you have a recent version of zsh (any version of
4.3 within the last few years is certainly OK).

replace-pacman-command() {
   # Ensure extended globbing is on.
   emulate -L zsh
   setopt extendedglob
   # The variables used by the extendeglob (#b) matching option.
   local -a match mbegin mend
   # Match the line in three parts.
   # - "pacman" plus any characters not part of a word.
   # - Any set of characters that are part of the word.
   # - The rest
   # You could replace "pacman" with [[:WORD:]]## which matches
   # any number of word charaters at this point.
   # Translation:
   # (#b) - turn on match references ("backreferences").
   # (....) - each of these now produces a word in the match array
   # [[:WORD:]] - Any character that can be in a word.  This
   #              bit won't work in zsh 4.2, but you can make something up.
   # [[:WORD:]]## - Same, with as may repetitions as will match, but at
   #                least one occurrence.
   # [^[:WORD:]] - Any character that can't be in a word
   if [[ $BUFFER = (#b)("pacman"[^[:WORD:]]##)([[:WORD:]]##)(*) ]]; then
      # Replace the middle part, keeping the rest.
      BUFFER="${match[1]}$1${match[3]}"
   fi
   # Always put the cursor at the end of the line.
   # (Not actually needed for the rest of the logic to work.)
   zle end-of-line
}

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


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom
More information can be found at www.csr.com. Follow CSR on Twitter at http://twitter.com/CSR_PLC and read our blog at www.csr.com/blog


      parent reply	other threads:[~2011-08-02 15:18 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-02 14:20 Pascal Wittmann
2011-08-02 14:34 ` Mikael Magnusson
2011-08-02 14:55   ` Pascal Wittmann
2011-08-02 15:08     ` Manuel Presnitz
2011-08-02 15:09     ` Jérémie Roquet
2011-08-02 15:28       ` Pascal Wittmann
2011-08-02 15:55         ` Peter Stephenson
2011-08-02 16:45         ` Bart Schaefer
2011-08-02 17:03           ` Pascal Wittmann
2011-08-02 15:17     ` Manuel Presnitz
2011-08-02 15:18     ` 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=20110802161834.3a7d02ca@pwslap01u.europe.root.pri \
    --to=peter.stephenson@csr.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).