From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 9384 invoked by alias); 2 Aug 2011 15:18:53 -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: 16189 Received: (qmail 19285 invoked from network); 2 Aug 2011 15:18:50 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS autolearn=ham version=3.3.1 Received-SPF: none (ns1.primenet.com.au: domain at csr.com does not designate permitted sender hosts) Date: Tue, 2 Aug 2011 16:18:34 +0100 From: Peter Stephenson To: Subject: Re: zle insert problems Message-ID: <20110802161834.3a7d02ca@pwslap01u.europe.root.pri> In-Reply-To: <4E380FF2.3020309@gmx.net> References: <4E380791.2090807@gmx.net> <4E380FF2.3020309@gmx.net> Organization: Cambridge Silicon Radio X-Mailer: Claws Mail 3.7.9 (GTK+ 2.22.0; i386-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.103.11.26] X-Scanned-By: MailControl A-12-01-02 (www.mailcontrol.com) on 10.71.0.127 On Tue, 2 Aug 2011 16:55:46 +0200 Pascal Wittmann 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 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