From 4e54648add79f7cb0c0fe81e46f49817d4555f2a Mon Sep 17 00:00:00 2001 From: Peter Stephenson Date: Thu, 17 Jul 2014 09:45:46 +0100 Subject: 32866: new replace-argument ZLE function. Also a couple of read-from-minibuffer fixes: don't pass numeric argument to recursive edit, and hide the minibuffer edit from the undo history. --- ChangeLog | 9 ++++++++ Doc/Zsh/contrib.yo | 25 +++++++++++++++++++++ Functions/Zle/read-from-minibuffer | 9 ++++++++ Functions/Zle/replace-argument | 46 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 Functions/Zle/replace-argument diff --git a/ChangeLog b/ChangeLog index a2f0e20cd..33bb0fd87 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2014-07-17 Peter Stephenson + + * 32866 (plus extra undo fix in read-from-minibuffer): + Doc/Zsh/contrib.yo, Functions/Zle/read-from-minibuffer, + Functions/Zle/replace-argument: new replace-argument function; + fixes in read-from-minibuffer not to pass through numeric + argument to recursive edit and to hide minibuffer edit from + undo history. + 2014-07-08 Peter Stephenson * Dominic Hopf: 32837: Config/defs.mk.in: improve handling of diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo index 2fcfbbdc7..4ee404c1b 100644 --- a/Doc/Zsh/contrib.yo +++ b/Doc/Zsh/contrib.yo @@ -2328,6 +2328,31 @@ The name is a slight misnomer, as in fact the shell's own minibuffer is not used. Hence it is still possible to call tt(executed-named-cmd) and similar functions while reading a value. ) +tindex(replace-argument) +tindex(replace-argument-edit) +item(tt(replace-argument), tt(replace-argument-edit)) +( +The function tt(replace-argument) can be used to replace a command +line argument in the current command line or, if the current command +line is empty, in the last command line executed (the new command line +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. + +If no numeric argument is given, the current argument is replaced; +this is the last argument if the previous history line is being used. + +The function prompts for a replacement argument. + +If the widget contains the string tt(edit), for example is defined as + +example(zle -N replace-argument-edit replace-argument) + +then the function presents the current value of the argument for +editing, otherwise the editing buffer for the replacement is +initially empty. +) tindex(replace-string) tindex(replace-string-again) tindex(replace-pattern) diff --git a/Functions/Zle/read-from-minibuffer b/Functions/Zle/read-from-minibuffer index 57e926884..8fec1105e 100644 --- a/Functions/Zle/read-from-minibuffer +++ b/Functions/Zle/read-from-minibuffer @@ -20,7 +20,9 @@ done (( OPTIND > 1 )) && shift $(( OPTIND - 1 )) local readprompt="$1" lbuf_init="$2" rbuf_init="$3" +integer changeno=$UNDO_CHANGE_NO +{ # Use anonymous function to make sure special values get restored, # even if this function is called as a widget. # local +h ensures special parameters stay special. @@ -39,10 +41,17 @@ local readprompt="$1" lbuf_init="$2" rbuf_init="$3" read -k $keys stat=$? else + local NUMERIC + unset NUMERIC zle recursive-edit -K main stat=$? (( stat )) || REPLY=$BUFFER fi } +} always { + # This removes the edits relating to the read from the undo history. + # These aren't useful once we get back to the main editing buffer. + zle undo $changeno +} return $stat diff --git a/Functions/Zle/replace-argument b/Functions/Zle/replace-argument new file mode 100644 index 000000000..b43fc39bb --- /dev/null +++ b/Functions/Zle/replace-argument @@ -0,0 +1,46 @@ +# Replace an argument to a command, delimited by normal shell syntax. +# Prompts for the replacement. +# With no numeric argument, replace the current argument. +# With a numeric argument, replace that argument: 0 = command word, +# as in history expansion. +# If editing buffer is empty, use previous history line. + +autoload -Uz split-shell-arguments read-from-minibuffer + +if (( ${#BUFFER} == 0 )); then + (( HISTNO-- )) + CURSOR=${#BUFFER} +fi + +local widget=$WIDGET +integer numeric 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 )) +else + index=$((REPLY & ~1 )) +fi + +local edit +if [[ $widget = *edit* ]]; then + edit=$reply[$index] +fi +read-from-minibuffer "Replace $reply[$index] with: " $edit || return 1 + +integer diff=$(( ${#REPLY} - ${#reply[$index]} )) +reply[$index]=$REPLY + +BUFFER=${(j..)reply} +if (( cursor > REPLY2 )); then + (( CURSOR = cursor + diff )) +else + (( CURSOR = REPLY2 )) +fi -- cgit 1.4.1