zsh-users
 help / color / mirror / code / Atom feed
* s/pattern/pattern/g on the commandline ?
@ 2012-02-25 18:24 meino.cramer
  2012-02-25 18:35 ` Jérémie Roquet
  2012-02-27 10:38 ` Peter Stephenson
  0 siblings, 2 replies; 18+ messages in thread
From: meino.cramer @ 2012-02-25 18:24 UTC (permalink / raw)
  To: zsh-users

Hi,

suppose I have very long and ugly commandline like this one:

    cat file\ from\ \#windows \ part1 \a.txt file\ from\ \#windows \ part2 \a.txt file\ from\ \#windows \ part3 \a.txt file\ from\ \#windows \ part4 \a.txt > allAbout_a.txt


and after summiting this ugly line I want redo the same but with a
slightly differen input like this:


    cat file\ from\ \#windows \ part1 \b.txt file\ from\ \#windows \ part2 \b.txt file\ from\ \#windows \ part3 \b.txt file\ from\ \#windows \ part4 \b.txt > allAbout_b.txt


which is - by the way - simply done with vim, with which I am editing
this posting currently.

My question is:
Is there any way to submit commands like 's/<searched>/<replacement>/g' for being applied the whole commandline in one go?

Thank you very much in advance for any help!
have a nice weekend!
Best regards,
mcc





^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: s/pattern/pattern/g on the commandline ?
  2012-02-25 18:24 s/pattern/pattern/g on the commandline ? meino.cramer
@ 2012-02-25 18:35 ` Jérémie Roquet
  2012-02-25 18:41   ` Jérémie Roquet
  2012-02-27 10:38 ` Peter Stephenson
  1 sibling, 1 reply; 18+ messages in thread
From: Jérémie Roquet @ 2012-02-25 18:35 UTC (permalink / raw)
  To: meino.cramer; +Cc: zsh-users

Hi,

2012/2/25  <meino.cramer@gmx.de>:
> suppose I have very long and ugly commandline like this one:
>
>    cat file\ from\ \#windows \ part1 \a.txt file\ from\ \#windows \ part2 \a.txt file\ from\ \#windows \ part3 \a.txt file\ from\ \#windows \ part4 \a.txt > allAbout_a.txt
>
>
> and after summiting this ugly line I want redo the same but with a
> slightly differen input like this:
>
>
>    cat file\ from\ \#windows \ part1 \b.txt file\ from\ \#windows \ part2 \b.txt file\ from\ \#windows \ part3 \b.txt file\ from\ \#windows \ part4 \b.txt > allAbout_b.txt
>
>
> which is - by the way - simply done with vim, with which I am editing
> this posting currently.
>
> My question is:
> Is there any way to submit commands like 's/<searched>/<replacement>/g' for being applied the whole commandline in one go?

!!:gs/<searched>/<replacement>/

Best regards,

-- 
Jérémie


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: s/pattern/pattern/g on the commandline ?
  2012-02-25 18:35 ` Jérémie Roquet
@ 2012-02-25 18:41   ` Jérémie Roquet
  2012-02-25 18:46     ` Moritz Bunkus
                       ` (2 more replies)
  0 siblings, 3 replies; 18+ messages in thread
From: Jérémie Roquet @ 2012-02-25 18:41 UTC (permalink / raw)
  To: meino.cramer; +Cc: zsh-users

2012/2/25 Jérémie Roquet <arkanosis@gmail.com>:
> !!:gs/<searched>/<replacement>/

By the way, that's for the last command, but you can also use:

!-2:gs/<searched>/<replacement>

… for the command before;

!-3:gs/<searched>/<replacement>

… and so on, as well as:

!42:gs/<searched>/<replacement>

… for the 42nd command in your history (the command's number is
displayed in your prompt if you have %! in it but you can use the
“history” command as well).

Best regards,

-- 
Jérémie


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: s/pattern/pattern/g on the commandline ?
  2012-02-25 18:41   ` Jérémie Roquet
@ 2012-02-25 18:46     ` Moritz Bunkus
  2012-02-25 19:03       ` Damien Thébault
                         ` (2 more replies)
       [not found]     ` <20120226075234.GB3020@solfire>
       [not found]     ` <20120227041300.GA3063@solfire>
  2 siblings, 3 replies; 18+ messages in thread
From: Moritz Bunkus @ 2012-02-25 18:46 UTC (permalink / raw)
  To: zsh-users

Hey,

while we're at it: is there a way to do history expansion without
actually submitting the modified entry? Meaning I would like to verify
that my regex fu was strong enough to produce the correct command
before submitting it. "!!:gs/what/ever" executes the command directly
if I'm not mistaken.

Kind regards,
mo


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: s/pattern/pattern/g on the commandline ?
  2012-02-25 18:46     ` Moritz Bunkus
@ 2012-02-25 19:03       ` Damien Thébault
  2012-02-25 19:19         ` Jérémie Roquet
  2012-02-25 19:24       ` Phil Pennock
  2012-02-26 18:32       ` Bart Schaefer
  2 siblings, 1 reply; 18+ messages in thread
From: Damien Thébault @ 2012-02-25 19:03 UTC (permalink / raw)
  To: Moritz Bunkus; +Cc: zsh-users

Simply pressing tab at the end of the line instead of enter will
replace the command with the changed line without submitting it.

On Sat, Feb 25, 2012 at 19:46, Moritz Bunkus <moritz@bunkus.org> wrote:
> Hey,
>
> while we're at it: is there a way to do history expansion without
> actually submitting the modified entry? Meaning I would like to verify
> that my regex fu was strong enough to produce the correct command
> before submitting it. "!!:gs/what/ever" executes the command directly
> if I'm not mistaken.
>
> Kind regards,
> mo



-- 
Damien Thebault


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: s/pattern/pattern/g on the commandline ?
  2012-02-25 19:03       ` Damien Thébault
@ 2012-02-25 19:19         ` Jérémie Roquet
  2012-02-25 19:36           ` Moritz Bunkus
  0 siblings, 1 reply; 18+ messages in thread
From: Jérémie Roquet @ 2012-02-25 19:19 UTC (permalink / raw)
  To: Damien Thébault; +Cc: Moritz Bunkus, zsh-users

2012/2/25 Damien Thébault <damien.thebault@gmail.com>:
> Simply pressing tab at the end of the line instead of enter will
> replace the command with the changed line without submitting it.

And “undo” (ctr+_ with emacs bindings) will restore non-expanded line
so you can fix it if needed.

Best regards,

-- 
Jérémie


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: s/pattern/pattern/g on the commandline ?
  2012-02-25 18:46     ` Moritz Bunkus
  2012-02-25 19:03       ` Damien Thébault
@ 2012-02-25 19:24       ` Phil Pennock
  2012-02-25 20:30         ` Christoph (Stucki) von Stuckrad
  2012-02-26 18:32       ` Bart Schaefer
  2 siblings, 1 reply; 18+ messages in thread
From: Phil Pennock @ 2012-02-25 19:24 UTC (permalink / raw)
  To: Moritz Bunkus; +Cc: zsh-users

On 2012-02-25 at 19:46 +0100, Moritz Bunkus wrote:
> while we're at it: is there a way to do history expansion without
> actually submitting the modified entry? Meaning I would like to verify
> that my regex fu was strong enough to produce the correct command
> before submitting it. "!!:gs/what/ever" executes the command directly
> if I'm not mistaken.

setopt hist_verify

Note: if you *really* want to be able to edit your command-line with
something with the full power of a text editor, then sometimes there's
no substitute for a text-editor.

bindkey '^[v' edit-command-line

Then pressing esc-v will invoke $VISUAL (else $EDITOR, else vi) as a
text-editor for adjusting the current command-line.  So <up> and then
<esc>-v will let you do as much as you need.

Be careful to not suspect the text-editor, only save and exit when
you're done.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: s/pattern/pattern/g on the commandline ?
  2012-02-25 19:19         ` Jérémie Roquet
@ 2012-02-25 19:36           ` Moritz Bunkus
  0 siblings, 0 replies; 18+ messages in thread
From: Moritz Bunkus @ 2012-02-25 19:36 UTC (permalink / raw)
  To: zsh-users

Hey,

that's 100% what I was looking for. Thanks a lot!

m.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: s/pattern/pattern/g on the commandline ?
  2012-02-25 19:24       ` Phil Pennock
@ 2012-02-25 20:30         ` Christoph (Stucki) von Stuckrad
  0 siblings, 0 replies; 18+ messages in thread
From: Christoph (Stucki) von Stuckrad @ 2012-02-25 20:30 UTC (permalink / raw)
  To: zsh-users; +Cc: Moritz Bunkus

On Sat, 25 Feb 2012, Phil Pennock wrote:

> Then pressing esc-v will invoke $VISUAL (else $EDITOR, else vi) as a
> text-editor for adjusting the current command-line.  So <up> and then
> <esc>-v will let you do as much as you need.
> 
> Be careful to not suspect the text-editor, only save and exit when
> you're done.

Same thing, if you've not yet bound the key, can be done by 'fc' command.

Even more: if I notice I'll use the same things a few times soon,
I often do 'fc -# -1' (with '#' some *number* of lines back)
putting the last '#' number of lines into the editor at once.

Then e.g. put an 'doit() {' as first line, '}' as last, and edit the
repeated lines into something with '${1}' instead of the 'part to edit'.
Writing it back makes it into function 'doit' and I'll need not worry
about editing the right thing, just give the new as parameter.
The complete function will stay 'in one line of history', so I can even
repeat editing, if need be, or can be re-edited with 'zed -f doit'.

Stucki

-- 
Christoph von Stuckrad      * * |nickname |Mail <stucki@mi.fu-berlin.de> \
Freie Universitaet Berlin   |/_*|'stucki' |Tel(Mo.,Mi.):+49 30 838-75 459|
Mathematik & Informatik EDV |\ *|if online|  (Di,Do,Fr):+49 30 77 39 6600|
Takustr. 9 / 14195 Berlin   * * |on IRCnet|Fax(home):   +49 30 77 39 6601/


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: s/pattern/pattern/g on the commandline ?
       [not found]     ` <20120226075234.GB3020@solfire>
@ 2012-02-26 15:17       ` Jérémie Roquet
  0 siblings, 0 replies; 18+ messages in thread
From: Jérémie Roquet @ 2012-02-26 15:17 UTC (permalink / raw)
  To: meino.cramer; +Cc: Zsh Users

Hi,

2012/2/26  <meino.cramer@gmx.de>:
> ...an additional question:
>
> Suppose I had entered such an ugly looooong commandline about two days
> ago. I did not remember when it was and what the context was.
> And: I have a very looong history file.
>
> I want to recycle that line and do the s//g-trick on it to get a shiny
> new fresh commmandline for the next action.

If the command in your long line is unusual (let's say “makecoffee”),
you could use:

!makecoffee:gs/<before>/<after>

> When I press ^r<pattern> I easily can find that line, it will
> displayed instantly but without its numbering. In the moment the
> commandline is displayed I loose the ability to do the s//g-trick.
>
> Is there a way to browse the commandlines like with ^r only to get its
> numbering to apply the s//g-trick to the choosen line ?

Hacking history-incremental-pattern-search-backward is an option, but
that wouldn't be easy.

Instead, you could use:

fc -lm <pattern>

…to list previous commands matching the pattern, with their numbers.

Also, what I do in non trivial cases, is either:
 - use edit-command-line as pointed out by Phil: you just have to
ctrl+r the line you want and then edit it using a real text editor, or
 - use a custom widget to run sed (or awk, or perl, or…) on the
current line : you just have to ctrl+r the line you want then to use
whatever key you've bound the widget to to run sed on it.

If you put this in your .zshrc:

function sed-line ()
{
   read-from-minibuffer 'Sed: '
   if [[ -n $REPLY ]]; then
      BUFFER=`echo $BUFFER | sed $REPLY`
   fi
}
zle -N sed-line
bindkey "^[e" sed-line

… you can then use meta-e to run a sed command on the line currently
being edited in your shell.

Best regards,

-- 
Jérémie


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: s/pattern/pattern/g on the commandline ?
  2012-02-25 18:46     ` Moritz Bunkus
  2012-02-25 19:03       ` Damien Thébault
  2012-02-25 19:24       ` Phil Pennock
@ 2012-02-26 18:32       ` Bart Schaefer
  2012-02-27  3:14         ` Darryl Zurn
  2 siblings, 1 reply; 18+ messages in thread
From: Bart Schaefer @ 2012-02-26 18:32 UTC (permalink / raw)
  To: zsh-users

On Feb 25,  7:46pm, Moritz Bunkus wrote:
}
} while we're at it: is there a way to do history expansion without
} actually submitting the modified entry?

I kept waiting for someone to mention this, but no one has yet ...

14.1.4 Modifiers
----------------

p
     Print the new command but do not execute it.  Only works with
     history expansion.

You can stack modifiers, so

	!!:p:gs/what/ever
or
	!!:gs/what/ever/:p

will both work. Note you need the trailing slash in the second case.
Other delimiters work for s/// -- you can even use a colon, so

	!:2:s:what:ever::p

means to take the second word of the previous command, change "what"
to "ever" in that word, and then display the result (and enter it
into the history list) without actually executing it.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: s/pattern/pattern/g on the commandline ?
  2012-02-26 18:32       ` Bart Schaefer
@ 2012-02-27  3:14         ` Darryl Zurn
  0 siblings, 0 replies; 18+ messages in thread
From: Darryl Zurn @ 2012-02-27  3:14 UTC (permalink / raw)
  To: zsh-users

I found this syntax easier for me to remember:

$	 echo this and this
this and this
$	^this^that^
echo that and this
that and this
$	^this^that^:G
echo that and that
that and that


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: s/pattern/pattern/g on the commandline ?
  2012-02-25 18:24 s/pattern/pattern/g on the commandline ? meino.cramer
  2012-02-25 18:35 ` Jérémie Roquet
@ 2012-02-27 10:38 ` Peter Stephenson
  2012-03-29 12:06   ` Vincent Lefevre
  1 sibling, 1 reply; 18+ messages in thread
From: Peter Stephenson @ 2012-02-27 10:38 UTC (permalink / raw)
  To: zsh-users

On Sat, 25 Feb 2012 19:24:50 +0100
<meino.cramer@gmx.de> wrote:
> Is there any way to submit commands like
> 's/<searched>/<replacement>/g' for being applied the whole commandline
> in one go?

Just as a postscript, it's worth noting the ZLE widgets replace-string
(which can also be loaded under the names replace-pattern or
replace-regexp) and replace-string-again.  This allows you to do this
interactively from the command line.  See the zshcontrib manual page.

I use


# actually autoloaded
zlewidget() {
  # bindkey KEY to new WIDGET, possibly implemented by optional FUNCTION.
  # FUNCTION defaults to WIDGET and will be marked for autoload -Uz.

  local key=$1
  local widget=$2
  local function=${3:-$2}

  autoload -Uz $function
  zle -N $widget $function
  bindkey $key $widget
}
zlewidget "\e%" replace-regexp replace-string
zlewidget '\e&' replace-regexp-again replace-string-again


(though in fact I don't tend to use \e& because (i) I rarely need to
replace the same thing multiple times (ii) I can't remember what the
last thing I replaced is anyway.)

-- 
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] 18+ messages in thread

* Re: s/pattern/pattern/g on the commandline ?
       [not found]     ` <20120227041300.GA3063@solfire>
@ 2012-02-28 13:09       ` Jérémie Roquet
  2012-02-29 13:48         ` Christoph (Stucki) von Stuckrad
  0 siblings, 1 reply; 18+ messages in thread
From: Jérémie Roquet @ 2012-02-28 13:09 UTC (permalink / raw)
  To: meino.cramer; +Cc: Zsh Users

Hi,

2012/2/27  <meino.cramer@gmx.de>:
> is there a way the prevent execution of the altered commandline when
> coming back from fc?

Not that I know of. Maybe someone else on the list could help…

Best regards,

-- 
Jérémie


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: s/pattern/pattern/g on the commandline ?
  2012-02-28 13:09       ` Jérémie Roquet
@ 2012-02-29 13:48         ` Christoph (Stucki) von Stuckrad
  2012-02-29 15:55           ` Bart Schaefer
  0 siblings, 1 reply; 18+ messages in thread
From: Christoph (Stucki) von Stuckrad @ 2012-02-29 13:48 UTC (permalink / raw)
  To: zsh-users

On Tue, 28 Feb 2012, Jérémie Roquet wrote:

> > is there a way the prevent execution of the altered commandline when
> > coming back from fc?
> 
> Not that I know of. Maybe someone else on the list could help…

It depends on what is wanted ... 'Just don't do anything' (without error)
can be done by saving just a single Line of ':'.
And if you have 'jobcontrol', hitting ^Z stopps the editor and thereby
looses the conection between the tempfile of fc and command execution.
Seemingly a non-zero-returnconde of the editor prevents execution,
so exiting e.g. 'vim' by ':cq' (setting retcode = 1) does this.

BUT I never fond out, how to put the edited lines into the history
BUT NOT EXCUTE them (once I'm in the editor by fc...).
So in this case I always saved to some other place and aborted editing.

Stucki

-- 
Christoph von Stuckrad      * * |nickname |Mail <stucki@mi.fu-berlin.de> \
Freie Universitaet Berlin   |/_*|'stucki' |Tel(Mo.,Mi.):+49 30 838-75 459|
Mathematik & Informatik EDV |\ *|if online|  (Di,Do,Fr):+49 30 77 39 6600|
Takustr. 9 / 14195 Berlin   * * |on IRCnet|Fax(home):   +49 30 77 39 6601/


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: s/pattern/pattern/g on the commandline ?
  2012-02-29 13:48         ` Christoph (Stucki) von Stuckrad
@ 2012-02-29 15:55           ` Bart Schaefer
  0 siblings, 0 replies; 18+ messages in thread
From: Bart Schaefer @ 2012-02-29 15:55 UTC (permalink / raw)
  To: zsh-users

On Feb 29,  2:48pm, Christoph (Stucki) von Stuckrad wrote:
}
} BUT I never fond out, how to put the edited lines into the history
} BUT NOT EXCUTE them (once I'm in the editor by fc...).
} So in this case I always saved to some other place and aborted editing.

fc_noexec() {
  integer skip_count=0
  setopt DEBUG_BEFORE_CMD
  TRAPDEBUG() {
    if (( skip_count > 1 ))
    then
	unfunction TRAPDEBUG
	setopt ERR_EXIT no_DEBUG_BEFORE_CMD
	return 1
    else
	(( skip_count++ ))
        return 0
    fi
  }
  fc "$@"
}

I think there's a small bug with DEBUG_BEFORE_CMD in that the debug
trap is executed *twice* for the single command "fc", which is why
the (( skip_count > 1 )) is in there.  If that behavior changes, the
test will also need to change.

Also I've assumed DEBUG_BEFORE_CMD is normally off, tweak that if
you have it on.

-- 
Barton E. Schaefer


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: s/pattern/pattern/g on the commandline ?
  2012-02-27 10:38 ` Peter Stephenson
@ 2012-03-29 12:06   ` Vincent Lefevre
  2012-03-29 20:23     ` Peter Stephenson
  0 siblings, 1 reply; 18+ messages in thread
From: Vincent Lefevre @ 2012-03-29 12:06 UTC (permalink / raw)
  To: zsh-users

This is a bit old, but...

On 2012-02-27 10:38:44 +0000, Peter Stephenson wrote:
> On Sat, 25 Feb 2012 19:24:50 +0100
> <meino.cramer@gmx.de> wrote:
> > Is there any way to submit commands like
> > 's/<searched>/<replacement>/g' for being applied the whole commandline
> > in one go?
> 
> Just as a postscript, it's worth noting the ZLE widgets replace-string
> (which can also be loaded under the names replace-pattern or
> replace-regexp) and replace-string-again.  This allows you to do this
> interactively from the command line.  See the zshcontrib manual page.
> 
> I use
> 
> 
> # actually autoloaded
> zlewidget() {
>   # bindkey KEY to new WIDGET, possibly implemented by optional FUNCTION.
>   # FUNCTION defaults to WIDGET and will be marked for autoload -Uz.
> 
>   local key=$1
>   local widget=$2
>   local function=${3:-$2}
> 
>   autoload -Uz $function
>   zle -N $widget $function
>   bindkey $key $widget
> }
> zlewidget "\e%" replace-regexp replace-string

But after such a replacement, if one wants to revert, "undo" doesn't
work.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: s/pattern/pattern/g on the commandline ?
  2012-03-29 12:06   ` Vincent Lefevre
@ 2012-03-29 20:23     ` Peter Stephenson
  0 siblings, 0 replies; 18+ messages in thread
From: Peter Stephenson @ 2012-03-29 20:23 UTC (permalink / raw)
  To: zsh-users

On Thu, 29 Mar 2012 14:06:24 +0200
Vincent Lefevre <vincent@vinc17.net> wrote:
> > zlewidget "\e%" replace-regexp replace-string
> 
> But after such a replacement, if one wants to revert, "undo" doesn't
> work.

(It does work, if you persevere, it just sucks.)

This has been in my mind on and off (mostly off) for some time.

The following adds the UNDO_CHANGE_NO zle parameter which returns the
current state of undo and an optional argument to undo which indicates
a change number back to which the editing history should be wound.

I'm a bit suspicious of this since it seemed to work perfectly first
time.  This nearly always means I screwed something major up.

Index: Doc/Zsh/zle.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/zle.yo,v
retrieving revision 1.96
diff -p -u -r1.96 zle.yo
--- Doc/Zsh/zle.yo	5 Jan 2012 20:01:25 -0000	1.96
+++ Doc/Zsh/zle.yo	29 Mar 2012 20:17:47 -0000
@@ -877,6 +877,12 @@ any predisplay string should be highligh
 Note that the effect of tt(region_highlight) is not saved and disappears
 as soon as the line is accepted.
 )
+vindex(UNDO_CHANGE_NO)
+item(tt(UNDO_CHANGE_NO) (integer))
+A number representing the state of the undo history.  The only use
+of this is passing as an argument to the tt(undo) widget in order to
+undo back to the recorded point.  Read-only.
+)
 vindex(WIDGET)
 item(tt(WIDGET) (scalar))(
 The name of the widget currently being executed; read-only.
@@ -2164,7 +2170,10 @@ command is typed.  By default it beeps.
 )
 tindex(undo)
 item(tt(undo) (^_ ^Xu ^X^U) (unbound) (unbound))(
-Incrementally undo the last text modification.
+Incrementally undo the last text modification.  When called from a
+user-defined widget, takes an optional argument indicating a previous state
+of the undo history as returned by the tt(UNDO_CHANGE_NO) variable;
+modifications are undone until that state is reached.
 )
 tindex(redo)
 item(tt(redo))(
Index: Functions/Zle/replace-string
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Zle/replace-string,v
retrieving revision 1.6
diff -p -u -r1.6 replace-string
--- Functions/Zle/replace-string	7 Nov 2011 11:13:50 -0000	1.6
+++ Functions/Zle/replace-string	29 Mar 2012 20:17:47 -0000
@@ -4,6 +4,9 @@ setopt extendedglob
 autoload -Uz read-from-minibuffer replace-string-again
 
 local p1  p2
+integer changeno=$UNDO_CHANGE_NO
+
+{
 
 if [[ -n $_replace_string_src ]]; then
   p1="[$_replace_string_src -> $_replace_string_rep]"$'\n'
@@ -30,4 +33,10 @@ if [[ -n $REPLY ]]; then
   typeset -g _replace_string_rep=$REPLY
 fi
 
+} always {
+  # Undo back to the original line; we don't want the
+  # undo history of editing the strings left.
+  zle undo $changeno
+}
+
 replace-string-again $curwidget
Index: Src/Zle/zle.h
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/zle.h,v
retrieving revision 1.43
diff -p -u -r1.43 zle.h
--- Src/Zle/zle.h	18 Feb 2011 22:08:46 -0000	1.43
+++ Src/Zle/zle.h	29 Mar 2012 20:17:47 -0000
@@ -269,6 +269,7 @@ struct change {
     ZLE_STRING_T ins;		/* characters to insert */
     int insl;			/* no. of characters in ins */
     int old_cs, new_cs;		/* old and new cursor positions */
+    zlong changeno;             /* unique number of this change */
 };
 
 #define CH_NEXT (1<<0)   /* next structure is also part of this change */
Index: Src/Zle/zle_params.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/zle_params.c,v
retrieving revision 1.36
diff -p -u -r1.36 zle_params.c
--- Src/Zle/zle_params.c	27 Mar 2011 17:55:41 -0000	1.36
+++ Src/Zle/zle_params.c	29 Mar 2012 20:17:47 -0000
@@ -93,6 +93,8 @@ static const struct gsu_integer pending_
 { get_pending, NULL, zleunsetfn };
 static const struct gsu_integer region_active_gsu =
 { get_region_active, set_region_active, zleunsetfn };
+static const struct gsu_integer undo_change_no_gsu =
+{ get_undo_current_change, NULL, zleunsetfn };
 
 static const struct gsu_array killring_gsu =
 { get_killring, set_killring, unset_killring };
@@ -133,6 +135,8 @@ static struct zleparam {
     { "RBUFFER", PM_SCALAR,  GSU(rbuffer_gsu), NULL },
     { "REGION_ACTIVE", PM_INTEGER, GSU(region_active_gsu), NULL},
     { "region_highlight", PM_ARRAY, GSU(region_highlight_gsu), NULL },
+    { "UNDO_CHANGE_NO", PM_INTEGER | PM_READONLY, GSU(undo_change_no_gsu),
+      NULL },
     { "WIDGET", PM_SCALAR | PM_READONLY, GSU(widget_gsu), NULL },
     { "WIDGETFUNC", PM_SCALAR | PM_READONLY, GSU(widgetfunc_gsu), NULL },
     { "WIDGETSTYLE", PM_SCALAR | PM_READONLY, GSU(widgetstyle_gsu), NULL },
Index: Src/Zle/zle_utils.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/zle_utils.c,v
retrieving revision 1.62
diff -p -u -r1.62 zle_utils.c
--- Src/Zle/zle_utils.c	13 May 2011 11:59:05 -0000	1.62
+++ Src/Zle/zle_utils.c	29 Mar 2012 20:17:47 -0000
@@ -1359,6 +1359,10 @@ static struct change *changes, *curchang
 
 static struct change *nextchanges, *endnextchanges;
 
+/* incremented to provide a unique change number */
+
+static zlong undo_changeno;
+
 /**/
 void
 initundo(void)
@@ -1368,6 +1372,7 @@ initundo(void)
     curchange->prev = curchange->next = NULL;
     curchange->del = curchange->ins = NULL;
     curchange->dell = curchange->insl = 0;
+    curchange->changeno = undo_changeno = 0;
     lastline = zalloc((lastlinesz = linesz) * ZLE_CHAR_SIZE);
     ZS_memcpy(lastline, zleline, (lastll = zlell));
     lastcs = zlecs;
@@ -1492,6 +1497,7 @@ mkundoent(void)
 	ch->flags = 0;
 	ch->prev = NULL;
     }
+    ch->changeno = ++undo_changeno;
     endnextchanges = ch;
 }
 
@@ -1512,8 +1518,15 @@ setlastline(void)
 
 /**/
 int
-undo(UNUSED(char **args))
+undo(char **args)
 {
+    zlong last_change = (zlong)0;
+
+    if (*args)
+    {
+	last_change = zstrtol(*args, NULL, 0);
+    }
+
     handleundo();
     do {
 	if(!curchange->prev)
@@ -1522,7 +1535,8 @@ undo(UNUSED(char **args))
 	    curchange = curchange->prev;
 	else
 	    break;
-    } while(curchange->flags & CH_PREV);
+    } while (*args ? curchange->changeno != last_change :
+	     (curchange->flags & CH_PREV));
     setlastline();
     return 0;
 }
@@ -1637,3 +1651,15 @@ zlecallhook(char *name, char *arg)
     errflag = saverrflag;
     retflag = savretflag;
 }
+
+/*
+ * Return the number corresponding to the last change made.
+ */
+
+/**/
+zlong
+get_undo_current_change(UNUSED(Param pm))
+{
+    return undo_changeno;
+}
+

- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2012-03-29 20:23 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-25 18:24 s/pattern/pattern/g on the commandline ? meino.cramer
2012-02-25 18:35 ` Jérémie Roquet
2012-02-25 18:41   ` Jérémie Roquet
2012-02-25 18:46     ` Moritz Bunkus
2012-02-25 19:03       ` Damien Thébault
2012-02-25 19:19         ` Jérémie Roquet
2012-02-25 19:36           ` Moritz Bunkus
2012-02-25 19:24       ` Phil Pennock
2012-02-25 20:30         ` Christoph (Stucki) von Stuckrad
2012-02-26 18:32       ` Bart Schaefer
2012-02-27  3:14         ` Darryl Zurn
     [not found]     ` <20120226075234.GB3020@solfire>
2012-02-26 15:17       ` Jérémie Roquet
     [not found]     ` <20120227041300.GA3063@solfire>
2012-02-28 13:09       ` Jérémie Roquet
2012-02-29 13:48         ` Christoph (Stucki) von Stuckrad
2012-02-29 15:55           ` Bart Schaefer
2012-02-27 10:38 ` Peter Stephenson
2012-03-29 12:06   ` Vincent Lefevre
2012-03-29 20:23     ` 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).