about summary refs log tree commit diff
path: root/Functions
diff options
context:
space:
mode:
Diffstat (limited to 'Functions')
-rw-r--r--Functions/Zle/.distfiles42
-rw-r--r--Functions/Zle/modify-current-argument51
-rw-r--r--Functions/Zle/split-shell-arguments58
3 files changed, 132 insertions, 19 deletions
diff --git a/Functions/Zle/.distfiles b/Functions/Zle/.distfiles
index 38914d723..4f01a8c01 100644
--- a/Functions/Zle/.distfiles
+++ b/Functions/Zle/.distfiles
@@ -1,24 +1,28 @@
 DISTFILES_SRC='
 .distfiles
-backward-kill-word-match       backward-word-match            
-capitalize-word-match          copy-earlier-word              
-cycle-completion-positions     define-composed-chars          
-delete-whole-word-match        down-case-word-match           
-down-line-or-beginning-search  edit-command-line              
-forward-word-match             history-beginning-search-menu  
-history-pattern-search         history-search-end             
-incarg                         incremental-complete-word      
-insert-composed-char           insert-files                   
-insert-unicode-char            keeper                         
-keymap+widget                  kill-word-match                
+backward-kill-word-match       backward-word-match
+capitalize-word-match          copy-earlier-word
+cycle-completion-positions     define-composed-chars
+delete-whole-word-match        down-case-word-match
+down-line-or-beginning-search  edit-command-line
+forward-word-match             history-beginning-search-menu
+history-pattern-search         history-search-end
+incarg                         incremental-complete-word
+insert-composed-char           insert-files
+insert-unicode-char            keeper
+keymap+widget                  kill-word-match
 match-word-context
-match-words-by-style           narrow-to-region               
-narrow-to-region-invisible     predict-on                     
-quote-and-complete-word        read-from-minibuffer           
-replace-string                 select-word-style              
+match-words-by-style
+modify-current-argument
+narrow-to-region
+narrow-to-region-invisible     predict-on
+quote-and-complete-word        read-from-minibuffer
+replace-string                 select-word-style
 replace-string-again
-smart-insert-last-word         transpose-words-match          
-up-case-word-match             up-line-or-beginning-search    
-url-quote-magic                which-command                  
-zed-set-file-name              
+smart-insert-last-word
+split-shell-arguments
+transpose-words-match
+up-case-word-match             up-line-or-beginning-search
+url-quote-magic                which-command
+zed-set-file-name
 '
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