From 083100056939b7b29f857bb2e41e6f7947da247c Mon Sep 17 00:00:00 2001 From: Paul Ackersviller Date: Sun, 11 Nov 2007 02:17:23 +0000 Subject: Merge of users/11083: functions for handling splitting of shell arguments in ZLE. --- Functions/Zle/modify-current-argument | 51 ++++++++++++++++++++++++++++++ Functions/Zle/split-shell-arguments | 58 +++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 Functions/Zle/modify-current-argument create mode 100644 Functions/Zle/split-shell-arguments (limited to 'Functions') diff --git a/Functions/Zle/modify-current-argument b/Functions/Zle/modify-current-argument new file mode 100644 index 000000000..be244ccc1 --- /dev/null +++ b/Functions/Zle/modify-current-argument @@ -0,0 +1,51 @@ +# Take an expression suitable for interpolation in double quotes that +# performs a replacement on the parameter "ARG". Replaces the +# shell argument (which may be a quoted string) under or before the +# cursor with that. Ensure the expression is suitable quoted. +# +# For example, to uppercase the entire shell argument: +# modify-current-word '${(U)ARG}' +# To strip the current quoting from the word (whether backslashes or +# single, double or dollar quotes) and use single quotes instead: +# modify-current-word '${(qq)${(Q)ARG}}' + +# Retain most options from the calling function for the eval. +# Reset some that might confuse things. +setopt localoptions noksharrays multibyte + +local -a reply +integer REPLY REPLY2 + +autoload -U split-shell-arguments +split-shell-arguments + +# Can't do this unless there's some text under or left of us. +(( REPLY < 2 )) && return 1 + +# Get the index of the word we want. +if (( REPLY & 1 )); then + # Odd position; need previous word. + (( REPLY-- )) + # Pretend position was just after the end of it. + (( REPLY2 = ${#reply[REPLY]} + 1 )) +fi + +# Length of all characters before current. +# Force use of character (not index) counting and join without IFS. +integer wordoff="${(cj..)#reply[1,REPLY-1]}" + +# Replacement for current word. This could do anything to ${reply[REPLY]}. +local ARG="${reply[REPLY]}" repl +eval repl=\"$1\" +# New line: all words before and after current word, with +# no additional spaces since we've already got the whitespace +# and the replacement word in the middle. +BUFFER="${(j..)reply[1,REPLY-1]}${repl}${(j..)reply[REPLY+1,-1]}" + +# Keep cursor at same position in replaced word. +# Redundant here, but useful if $repl changes the length. +# Limit to the next position after the end of the word. +integer repmax=$(( ${#repl} + 1 )) +# Remember CURSOR starts from offset 0 for some reason, so +# subtract 1 from positions. +(( CURSOR = wordoff + (REPLY2 > repmax ? repmax : REPLY2) - 1 )) diff --git a/Functions/Zle/split-shell-arguments b/Functions/Zle/split-shell-arguments new file mode 100644 index 000000000..e8a231732 --- /dev/null +++ b/Functions/Zle/split-shell-arguments @@ -0,0 +1,58 @@ +# Split a command line into shell arguments and whitespace in $reply. +# Odd elements (starting from 1) are whitespace, even elements +# are shell arguments (possibly quoted strings). Whitespace at +# start and end is always included in the array but may be an empty string. +# $REPLY holds NO_KSH_ARRAYS index of current word in $reply. +# $REPLY2 holds NO_KSH_ARRAYS index of current character in current word. +# Hence ${reply[$REPLY][$REPLY2]} is the character under the cursor. +# +# reply, REPLY, REPLY2 should therefore be local to the enclosing function. +# +# The following formula replaces the current shell word, or previous word +# if the cursor is on whitespace, by uppercasing all characters. + +emulate -L zsh +setopt extendedglob + +local -a bufwords lbufwords +local word +integer pos=1 cpos=$((CURSOR+1)) opos iword ichar + +bufwords=(${(z)BUFFER}) + +reply=() +while [[ ${BUFFER[pos]} = [[:space:]] ]]; do + (( pos++ )) +done +reply+=${BUFFER[1,pos-1]} +(( cpos < pos )) && (( iword = 1, ichar = cpos )) + +for word in "${bufwords[@]}"; do + (( opos = pos )) + (( pos += ${#word} )) + reply+=("$word") + if (( iword == 0 && cpos < pos )); then + (( iword = ${#reply} )) + (( ichar = cpos - opos + 1 )) + fi + + (( opos = pos )) + while [[ ${BUFFER[pos]} = [[:space:]] ]]; do + (( pos++ )) + done + reply+=("${BUFFER[opos,pos-1]}") + if (( iword == 0 && cpos < pos )); then + (( iword = ${#reply} )) + (( ichar = cpos - opos + 1 )) + fi +done + +if (( iword == 0 )); then + # At the end of the line, so off the indexable positions + # (but still a valid cursor position). + (( REPLY = ${#reply} )) + (( REPLY2 = 1 )) +else + (( REPLY = iword )) + (( REPLY2 = ichar )) +fi -- cgit 1.4.1