* zle insert problems @ 2011-08-02 14:20 Pascal Wittmann 2011-08-02 14:34 ` Mikael Magnusson 0 siblings, 1 reply; 11+ messages in thread From: Pascal Wittmann @ 2011-08-02 14:20 UTC (permalink / raw) To: zsh-users [-- Attachment #1: Type: text/plain, Size: 659 bytes --] Hi, I'm trying to build something like this: function do-something() { zle beginning-of-line zle -U "something" zle end-of-line } zle -N do-something bindkey "^[i]" do-something I assumed the commands are executed sequentially if I press M-i. In fact they are but the strings given to "zle -U" are written after the current widget is executed. So the result is: % this is a test pressing M-i % this is a testsomething Is there a way to flush this data earlier to get a result like: % sometingthis is a test Or is my approach totaly wrong? (This is the first time I use zle) Thanks in advance Pascal [-- Attachment #2: OpenPGP digital signature --] [-- Type: application/pgp-signature, Size: 198 bytes --] ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: zle insert problems 2011-08-02 14:20 zle insert problems Pascal Wittmann @ 2011-08-02 14:34 ` Mikael Magnusson 2011-08-02 14:55 ` Pascal Wittmann 0 siblings, 1 reply; 11+ messages in thread From: Mikael Magnusson @ 2011-08-02 14:34 UTC (permalink / raw) To: Pascal Wittmann; +Cc: zsh-users On 2 August 2011 16:20, Pascal Wittmann <PascalWittmann@gmx.net> wrote: > Hi, > > I'm trying to build something like this: > > function do-something() { > zle beginning-of-line > zle -U "something" > zle end-of-line > } > zle -N do-something > bindkey "^[i]" do-something You can modify the buffer directly via the BUFFER variable, so you could just say BUFFER="something $BUFFER" and it should do what you want. buffer. -- Mikael Magnusson ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: zle insert problems 2011-08-02 14:34 ` Mikael Magnusson @ 2011-08-02 14:55 ` Pascal Wittmann 2011-08-02 15:08 ` Manuel Presnitz ` (3 more replies) 0 siblings, 4 replies; 11+ messages in thread From: Pascal Wittmann @ 2011-08-02 14:55 UTC (permalink / raw) To: zsh-users [-- Attachment #1: Type: text/plain, Size: 1216 bytes --] On 08/02/2011 04:34 PM, Mikael Magnusson wrote: > On 2 August 2011 16:20, Pascal Wittmann <PascalWittmann@gmx.net> wrote: >> Hi, >> >> I'm trying to build something like this: >> >> function do-something() { >> zle beginning-of-line >> zle -U "something" >> zle end-of-line >> } >> zle -N do-something >> bindkey "^[i]" do-something > > You can modify the buffer directly via the BUFFER variable, so you > could just say BUFFER="something $BUFFER" and it should do what you > want. buffer. > 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 I think doing this directly via BUFFER gets quickly unreadable. [-- Attachment #2: OpenPGP digital signature --] [-- Type: application/pgp-signature, Size: 198 bytes --] ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: zle insert problems 2011-08-02 14:55 ` Pascal Wittmann @ 2011-08-02 15:08 ` Manuel Presnitz 2011-08-02 15:09 ` Jérémie Roquet ` (2 subsequent siblings) 3 siblings, 0 replies; 11+ messages in thread From: Manuel Presnitz @ 2011-08-02 15:08 UTC (permalink / raw) To: Pascal Wittmann, zsh-users > I think doing this directly via BUFFER gets quickly unreadable. What about that?! replace-pacman-command() { if [[ $LBUFFER = "pacman"* ]]; then LBUFFER="$@ ${${(z)LBUFFER}[2,-1]}" fi } ${${(z)LBUFFER}[2,-1]} takes everything but the first shell word of $LBUFFER. Greets, Manuel. -- Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: zle insert problems 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:17 ` Manuel Presnitz 2011-08-02 15:18 ` Peter Stephenson 3 siblings, 1 reply; 11+ messages in thread From: Jérémie Roquet @ 2011-08-02 15:09 UTC (permalink / raw) To: Zsh Users; +Cc: Pascal Wittmann 2011/8/2 Pascal Wittmann <PascalWittmann@gmx.net>: > 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 > > I think doing this directly via BUFFER gets quickly unreadable. replace-pacman-command() { BUFFER=${BUFFER/pacman [-a-zA-Z]#/pacman $@} } ? Best regards, -- Jérémie ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: zle insert problems 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 0 siblings, 2 replies; 11+ messages in thread From: Pascal Wittmann @ 2011-08-02 15:28 UTC (permalink / raw) To: zsh-users [-- Attachment #1: Type: text/plain, Size: 276 bytes --] On 08/02/2011 05:09 PM, Jérémie Roquet wrote: > replace-pacman-command() { > BUFFER=${BUFFER/pacman [-a-zA-Z]#/pacman $@} > } > Thanks, this is quite readable! I'll take this solution but just to have a explicit no: There is no way doing this only via zle? [-- Attachment #2: OpenPGP digital signature --] [-- Type: application/pgp-signature, Size: 198 bytes --] ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: zle insert problems 2011-08-02 15:28 ` Pascal Wittmann @ 2011-08-02 15:55 ` Peter Stephenson 2011-08-02 16:45 ` Bart Schaefer 1 sibling, 0 replies; 11+ messages in thread From: Peter Stephenson @ 2011-08-02 15:55 UTC (permalink / raw) To: zsh-users On Tue, 2 Aug 2011 17:28:10 +0200 Pascal Wittmann <PascalWittmann@gmx.net> wrote: > On 08/02/2011 05:09 PM, Jérémie Roquet wrote: > > replace-pacman-command() { > > BUFFER=${BUFFER/pacman [-a-zA-Z]#/pacman $@} > > } > > > > Thanks, this is quite readable! > > I'll take this solution but just to have a explicit no: There is no way > doing this only via zle? That's not really a meaningful question. It's not possible to do it via zle *commands*, no, but the whole point of having the zle variables is some things are best done that way rather than by commands. In other words, what you were trying to do as a zle command to insert a string at the cursor position is actually written as: LBUFFER+="string" This is still using zle; it's basically equivalent to the non-existent command zle insert-string "string" but much more flexible. -- 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 ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: zle insert problems 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 1 sibling, 1 reply; 11+ messages in thread From: Bart Schaefer @ 2011-08-02 16:45 UTC (permalink / raw) To: zsh-users On Aug 2, 5:28pm, Pascal Wittmann wrote: } } I'll take this solution but just to have a explicit no: There is no way } doing this only via zle? I'm going to disagree with PWS a little here (horrors!) and assert that it's OK to use zle commands to emulate user interactions in the case where you want to precisely mimic a command's behavior. For example it's much easier to do zle vi-backward-word than it is to analyze $LBUFFER to figure out where that cursor motion would have left you. The whole set of widget actions isn't flushed to the screen until the widget finishes or something like "zle -R" is run, so it's not as if the user sees the cursor jumping about. This is particularly true if you're working with multi-line buffers or want to move up and down in the history. Also you can mix zle actions with direct assignments to the various control parameters. So your original example could be written do-something() { CURSOR=0 LBUFFER="something" zle end-of-line } Thus, similarly, replace-pacman-command() { if [[ $LBUFFER = "pacman"* ]]; then CURSOR=0 zle forward-word zle delete-word LBUFFER+="$*" zle end-of-line fi } Another reason for using zle commands is that they properly maintain the relative positions of $MARK and $CURSOR, which get lost when you modify LBUFFER directly. However, there isn't a zle command to insert arbitrary characters. ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: zle insert problems 2011-08-02 16:45 ` Bart Schaefer @ 2011-08-02 17:03 ` Pascal Wittmann 0 siblings, 0 replies; 11+ messages in thread From: Pascal Wittmann @ 2011-08-02 17:03 UTC (permalink / raw) To: zsh-users [-- Attachment #1: Type: text/plain, Size: 505 bytes --] On 08/02/2011 06:45 PM, Bart Schaefer wrote: > On Aug 2, 5:28pm, Pascal Wittmann wrote: > } > } I'll take this solution but just to have a explicit no: There is no way > } doing this only via zle? > > […] > > Another reason for using zle commands is that they properly maintain > the relative positions of $MARK and $CURSOR, which get lost when you > modify LBUFFER directly. However, there isn't a zle command to insert > arbitrary characters. Thank you for this enlightening post! [-- Attachment #2: OpenPGP digital signature --] [-- Type: application/pgp-signature, Size: 198 bytes --] ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: zle insert problems 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:17 ` Manuel Presnitz 2011-08-02 15:18 ` Peter Stephenson 3 siblings, 0 replies; 11+ messages in thread From: Manuel Presnitz @ 2011-08-02 15:17 UTC (permalink / raw) To: Pascal Wittmann, zsh-users Sorry, "delete-word" deletes the following word, so: LBUFFER="${${(z)LBUFFER}[1]} $@ ${${(z)LBUFFER}[3,-1]}" Ok, not so nice anymore... -- Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: zle insert problems 2011-08-02 14:55 ` Pascal Wittmann ` (2 preceding siblings ...) 2011-08-02 15:17 ` Manuel Presnitz @ 2011-08-02 15:18 ` Peter Stephenson 3 siblings, 0 replies; 11+ messages in thread From: Peter Stephenson @ 2011-08-02 15:18 UTC (permalink / raw) To: zsh-users 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 ^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2011-08-02 17:02 UTC | newest] Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2011-08-02 14:20 zle insert problems 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
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).