From 6076c474f2427ec9a75cecab1fc95f7cd9f066f6 Mon Sep 17 00:00:00 2001 From: Peter Stephenson Date: Thu, 24 Jul 2014 10:49:50 +0100 Subject: unposted: replace-argument can take negative prefix to count from end --- ChangeLog | 6 ++++++ Doc/Zsh/contrib.yo | 1 + Functions/Zle/replace-argument | 16 +++++++++------- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index e9deb6e55..563708769 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2014-07-24 Peter Stephenson + + * unposted: Functions/Zle/replace-argument, Doc/Zsh/contrib.yo: + allow negative numeric prefix to count backwards from last + argument. + 2014-07-23 Peter Stephenson * Jai Keerthan: users/18981: Completion/Unix/Command/_tmux: diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo index 4ee404c1b..1c1a66a3b 100644 --- a/Doc/Zsh/contrib.yo +++ b/Doc/Zsh/contrib.yo @@ -2339,6 +2339,7 @@ is not executed). Arguments are as delimited by standard shell syntax, If a numeric argument is given, that specifies the argument to be replaced. 0 means the command name, as in history expansion. +A negative numeric argument counts backward from the last word. If no numeric argument is given, the current argument is replaced; this is the last argument if the previous history line is being used. diff --git a/Functions/Zle/replace-argument b/Functions/Zle/replace-argument index b43fc39bb..0ef3de7b5 100644 --- a/Functions/Zle/replace-argument +++ b/Functions/Zle/replace-argument @@ -12,21 +12,23 @@ if (( ${#BUFFER} == 0 )); then CURSOR=${#BUFFER} fi -local widget=$WIDGET -integer numeric cursor=CURSOR +local widget=$WIDGET numeric +integer cursor=CURSOR if (( ${+NUMERIC} )); then numeric=$NUMERIC -else - numeric=-1 fi local reply REPLY REPLY2 integer index split-shell-arguments -if (( numeric >= 0 )); then - index=$(( 2 + 2*numeric )) +if [[ -n $numeric ]]; then + if (( numeric < 0 )); then + (( index = ${#reply} - 1 + 2*(numeric+1) )) + else + (( index = 2 + 2*numeric )) + fi else - index=$((REPLY & ~1 )) + (( index = REPLY & ~1 )) fi local edit -- cgit 1.4.1