diff options
Diffstat (limited to 'Functions/Zle')
-rw-r--r-- | Functions/Zle/bracketed-paste-magic | 192 | ||||
-rw-r--r-- | Functions/Zle/edit-command-line | 15 | ||||
-rw-r--r-- | Functions/Zle/incremental-complete-word | 2 | ||||
-rw-r--r-- | Functions/Zle/narrow-to-region | 90 | ||||
-rw-r--r-- | Functions/Zle/read-from-minibuffer | 5 | ||||
-rw-r--r-- | Functions/Zle/smart-insert-last-word | 12 | ||||
-rw-r--r-- | Functions/Zle/url-quote-magic | 6 |
7 files changed, 287 insertions, 35 deletions
diff --git a/Functions/Zle/bracketed-paste-magic b/Functions/Zle/bracketed-paste-magic new file mode 100644 index 000000000..da106d1ac --- /dev/null +++ b/Functions/Zle/bracketed-paste-magic @@ -0,0 +1,192 @@ +# Starting with zsh-5.0.9, ZLE began to recognize the "bracketed paste" +# capability of terminal emulators, that is, the sequences $'\e[200~' to +# start a paste and $'\e[201~' to indicate the end of the pasted text. +# Pastes are handled by the bracketed-paste widget and insert literally +# into the editor buffer rather than being interpreted as keystrokes. +# +# This disables some common usages where the self-insert widget has been +# replaced in order to accomplish some extra processing. An example is +# the contributed url-quote-magic widget. The bracketed-paste-magic +# widget replaces bracketed-paste with a wrapper that re-enables these +# self-insert actions, and other actions as selected by the zstyles +# described below. +# +# Setup: +# autoload -Uz bracketed-paste-magic +# zle -N bracketed-paste bracketed-paste-magic + +# The following zstyles may be set to control processing of pasted text. +# +# active-widgets +# Looked up in the context :bracketed-paste-magic to obtain a list of +# patterns that match widget names that should be activated during the +# paste. All other key sequences are processed as self-insert-unmeta. +# The default is 'self-*' so any user-defined widgets named with that +# prefix are active along with the builtin self-insert. If this style is +# not set (note: it must be explicitly deleted after loading this +# function, otherwise it becomes set by default) or has no value, no +# widgets are active and the pasted text is inserted literally. If the +# value includes undefined-key, any unknown sequences are discarded from +# the pasted text. +# +# inactive-keys +# This is the inverse of active-widgets, it lists key sequences that +# always use self-insert-unmeta even when bound to an active-widget. +# Note that this is a list of literal key sequences, not patterns. +# This style is in context :bracketed-paste-magic and has no default. +# +# paste-init +# paste-finish +# Also looked up in the context :bracketed-paste-magic, these styles +# each are a list of function names. They are executed in widget +# context but are called as functions (NOT as widgets with "zle name"). +# They also run in zsh emulation context set by bracketed-paste-magic. +# As with hooks, the functions are called in order until one of them +# returns a nonzero exit status. The parameter PASTED contains the +# current state of the pasted text, other ZLE parameters are as usual. +# Although a nonzero status stops each list of functions, it does NOT +# abort the entire paste operation; use "zle send-break" for that. + +# IMPORTANT: During processing of the paste (after paste-init and +# before paste-finish), BUFFER starts empty and history is restricted, +# so cursor motions etc. may not pass outside of the pasted content. +# However, the paste-init functions have access to the full history and +# the original BUFFER, so they may for example move words from BUFFER +# into PASTED to make those words visible to the active-widgets. + +# Establish default values for styles, but only if not already set +zstyle -m :bracketed-paste-magic active-widgets '*' || + zstyle ':bracketed-paste-magic' active-widgets 'self-*' + +# Helper/example paste-init for exposing a word prefix inside PASTED. +# Useful with url-quote-magic if you have http://... on the line and +# are pasting additional text on the end of the URL. +# +# Usage: +# zstyle :bracketed-paste-magic paste-init backward-extend-paste +# +# TODO: rewrite this using match-words-by-style +# +backward-extend-paste() { + : emulate -LR zsh # Already set by bracketed-paste-magic + integer bep_mark=$MARK bep_region=$REGION_ACTIVE + if (( REGION_ACTIVE && MARK < CURSOR )); then + zle .exchange-point-and-mark + fi + if (( CURSOR )); then + local -a bep_words=( ${(z)LBUFFER} ) + if [[ -n $bep_words[-1] && $LBUFFER = *$bep_words[-1] ]]; then + PASTED=$bep_words[-1]$PASTED + LBUFFER=${LBUFFER%${bep_words[-1]}} + fi + fi + if (( MARK > bep_mark )); then + zle .exchange-point-and-mark + fi + REGION_ACTIVE=$bep_region +} + +# Example paste-finish for quoting the pasted text. +# +# Usage e.g.: +# zstyle :bracketed-paste-magic paste-finish quote-paste +# zstyle :bracketed-paste-magic:finish quote-style qqq +# +# Using "zstyle -e" to examine $PASTED lets you choose different quotes +# depending on context. +# +# To forcibly turn off numeric prefix quoting, use e.g.: +# zstyle :bracketed-paste-magic:finish quote-style none +# +quote-paste() { + : emulate -LR zsh # Already set by bracketed-paste-magic + local qstyle + # If there's a quoting style, be sure .bracketed-paste leaves it alone + zstyle -s :bracketed-paste-magic:finish quote-style qstyle && NUMERIC=1 + case $qstyle in + (b) PASTED=${(b)PASTED};; + (q-) PASTED=${(q-)PASTED};; + (\\|q) PASTED=${(q)PASTED};; + (\'|qq) PASTED=${(qq)PASTED};; + (\"|qqq) PASTED=${(qqq)PASTED};; + (\$|qqqq) PASTED=${(qqqq)PASTED};; + (Q) PASTED=${(Q)PASTED};; + esac +} + +# Now the actual function + +bracketed-paste-magic() { + emulate -LR zsh + local -a bpm_hooks bpm_inactive + local PASTED bpm_func bpm_active + + # Set PASTED and run the paste-init functions + zle .bracketed-paste PASTED + if zstyle -a :bracketed-paste-magic paste-init bpm_hooks; then + for bpm_func in $bpm_hooks; do + if (( $+functions[$bpm_func] )); then + $bpm_func || break + fi + done + fi + + # Save context, create a clean slate for the paste + integer bpm_mark=$MARK bpm_cursor=$CURSOR bpm_region=$REGION_ACTIVE + integer bpm_numeric=${NUMERIC:-1} + local bpm_buffer=$BUFFER + fc -p -a /dev/null 0 0 + BUFFER= + + zstyle -a :bracketed-paste-magic inactive-keys bpm_inactive + if zstyle -s :bracketed-paste-magic active-widgets bpm_active '|'; then + # There are active widgets. Reprocess $PASTED as keystrokes. + NUMERIC=1 + zle -U - $PASTED + while [[ -n $PASTED ]] && zle .read-command; do + PASTED=${PASTED#$KEYS} + if [[ $KEYS = ${(~j:|:)${(b)bpm_inactive}} ]]; then + zle .self-insert-unmeta + else + case $REPLY in + (${~bpm_active}) zle $REPLY;; + (*) zle .self-insert-unmeta;; + esac + fi + done + PASTED=$BUFFER + fi + + # Restore state + BUFFER=$bpm_buffer + MARK=$bpm_mark + CURSOR=$bpm_cursor + REGION_ACTIVE=$bpm_region + NUMERIC=$bpm_numeric + fc -P + + # PASTED has been updated, run the paste-finish functions + if zstyle -a :bracketed-paste-magic paste-finish bpm_hooks; then + for bpm_func in $bpm_hooks; do + if (( $+functions[$bpm_func] )); then + $bpm_func || break + fi + done + fi + + # Reprocess $PASTED as an actual paste this time + zle -U - $PASTED$'\e[201~' # append paste-end marker + zle .bracketed-paste + zle .split-undo + + # Arrange to display highlighting if necessary + if [[ -n ${(M)zle_highlight:#paste:*} ]]; then + zle -R + zle .read-command && zle -U - $KEYS + fi +} + +# Handle zsh autoloading conventions +if [[ $zsh_eval_context = *loadautofunc && ! -o kshautoload ]]; then + bracketed-paste-magic "$@" +fi diff --git a/Functions/Zle/edit-command-line b/Functions/Zle/edit-command-line index 100af9601..2c7f34b8b 100644 --- a/Functions/Zle/edit-command-line +++ b/Functions/Zle/edit-command-line @@ -8,7 +8,20 @@ () { exec </dev/tty - ${=${VISUAL:-${EDITOR:-vi}}} $1 + + # Compute the cursor's position in bytes, not characters. + setopt localoptions nomultibyte + integer byteoffset=$(( $#PREBUFFER + $#LBUFFER + 1 )) + + # Open the editor, placing the cursor at the right place if we know how. + local editor=${${VISUAL:-${EDITOR:-vi}}} + case $editor in + (*vim*) ${=editor} -c "normal! ${byteoffset}go" -- $1;; + (*emacs*) ${=editor} $1 -eval "(goto-char ${byteoffset})";; + (*) ${=editor} $1;; + esac + + # Replace the buffer with the editor output. print -Rz - "$(<$1)" } =(<<<"$PREBUFFER$BUFFER") diff --git a/Functions/Zle/incremental-complete-word b/Functions/Zle/incremental-complete-word index 67a9d4744..ccc007543 100644 --- a/Functions/Zle/incremental-complete-word +++ b/Functions/Zle/incremental-complete-word @@ -69,7 +69,7 @@ incremental-complete-word() { '#key' -ne '#\\C-g' ]]; do twid=$wid if [[ "$key" = ${~stop} ]]; then - zle -U "$key" + zle -U - "$key" return elif [[ "$key" = ${~brk} ]]; then return diff --git a/Functions/Zle/narrow-to-region b/Functions/Zle/narrow-to-region index 86fd7ac13..261d821a9 100644 --- a/Functions/Zle/narrow-to-region +++ b/Functions/Zle/narrow-to-region @@ -5,39 +5,52 @@ # Optionally accepts exactly two arguments, which are used instead of # $CURSOR and $MARK as limits to the range. # +# Upon exit, $MARK is always the start of the edited range and $CURSOR +# the end of the range, even if they began in the opposite order. +# # Other options: -# -p pretext show `pretext' instead of the buffer text before the region. -# -P posttext show `posttext' instead of the buffer text after the region. -# Either or both may be empty. +# -p pretext show "pretext" instead of the buffer text before the region. +# -P posttext show "posttext" instead of the buffer text after the region. +# Either or both may be empty. # -n Only replace the text before or after the region with # the -p or -P options if the text was not empty. +# -l lbufvar $lbufvar is assigned the value of $LBUFFER and +# -r rbufvar $rbufvar is assigned the value of $RBUFFER +# from any recursive edit (i.e. not with -S or -R). Neither +# lbufvar nor rbufvar may begin with the prefix "_ntr_". # -S statevar # -R statevar -# Save or restore the state in/from the parameter named statevar. In -# either case no recursive editing takes place; this will typically be -# done within the calling function between calls with -S and -R. The -# statevar may not begin with the prefix _ntr_ which is reserved for -# parameters within narrow-to-region. +# Save or restore the state in/from the parameter named statevar. In +# either case no recursive editing takes place; this will typically be +# done within the calling function between calls with -S and -R. The +# statevar may not begin with the prefix "_ntr_" which is reserved for +# parameters within narrow-to-region. -emulate -L zsh -setopt extendedglob +# set the minimum of options to avoid changing behaviour away from +# user preferences from within recursive-edit +setopt localoptions noshwordsplit noksharrays -local _ntr_lbuffer _ntr_rbuffer +local _ntr_newbuf _ntr_lbuf_return _ntr_rbuf_return local _ntr_predisplay=$PREDISPLAY _ntr_postdisplay=$POSTDISPLAY +integer _ntr_savelim=UNDO_LIMIT_NO _ntr_changeno _ntr_histno=HISTNO integer _ntr_start _ntr_end _ntr_swap _ntr_cursor=$CURSOR _ntr_mark=$MARK integer _ntr_stat local _ntr_opt _ntr_pretext _ntr_posttext _ntr_usepretext _ntr_useposttext -local _ntr_nonempty _ntr_save _ntr_restore +local _ntr_nonempty _ntr_save _ntr_restore _ntr_lbuffer _ntr_rbuffer -while getopts "np:P:R:S:" _ntr_opt; do +while getopts "l:np:P:r:R:S:" _ntr_opt; do case $_ntr_opt in + (l) _ntr_lbuf_return=$OPTARG + ;; (n) _ntr_nonempty=1 ;; (p) _ntr_pretext=$OPTARG _ntr_usepretext=1 ;; (P) _ntr_posttext=$OPTARG _ntr_useposttext=1 ;; + (r) _ntr_rbuf_return=$OPTARG + ;; (R) _ntr_restore=$OPTARG ;; (S) _ntr_save=$OPTARG @@ -49,7 +62,8 @@ while getopts "np:P:R:S:" _ntr_opt; do done (( OPTIND > 1 )) && shift $(( OPTIND - 1 )) -if [[ $_ntr_restore = _ntr_* || $_ntr_save = _ntr_* ]]; then +if [[ $_ntr_restore = _ntr_* || $_ntr_save = _ntr_* || + $_ntr_lbuf_return = _ntr_* || $_ntr_rbuf_return = _ntr_* ]]; then zle -M "$0: _ntr_ prefix is reserved" >&2 return 1 fi @@ -58,7 +72,7 @@ if [[ -n $_ntr_save || -z $_ntr_restore ]]; then if (( $# )); then if (( $# != 2 )); then - zle -M "$0: supply zero or two arguments" + zle -M "$0: supply zero or two arguments" >&2 return 1 fi _ntr_start=$1 @@ -74,50 +88,68 @@ if [[ -n $_ntr_save || -z $_ntr_restore ]]; then _ntr_end=_ntr_swap fi - (( _ntr_end++, _ntr_cursor -= _ntr_start, _ntr_mark -= _ntr_start )) + (( _ntr_cursor -= _ntr_start, _ntr_mark -= _ntr_start )) _ntr_lbuffer=${BUFFER[1,_ntr_start]} if [[ -z $_ntr_usepretext || ( -n $_ntr_nonempty && -z $_ntr_lbuffer ) ]] then _ntr_pretext=$_ntr_lbuffer fi - _ntr_rbuffer=${BUFFER[_ntr_end,-1]} + _ntr_rbuffer=${BUFFER[_ntr_end+1,-1]} if [[ -z $_ntr_useposttext || ( -n $_ntr_nonempty && -z $_ntr_rbuffer ) ]] then _ntr_posttext=$_ntr_rbuffer fi + _ntr_changeno=$UNDO_CHANGE_NO PREDISPLAY="$_ntr_predisplay$_ntr_pretext" POSTDISPLAY="$_ntr_posttext$_ntr_postdisplay" - BUFFER=${BUFFER[_ntr_start+1,_ntr_end-1]} - CURSOR=$_ntr_cursor - MARK=$_ntr_mark if [[ -n $_ntr_save ]]; then - eval "$_ntr_save=(${(qq)_ntr_predisplay} ${(qq)_ntr_postdisplay} -${(qq)_ntr_lbuffer} ${(qq)_ntr_rbuffer})" || return 1 + builtin typeset -ga $_ntr_save + set -A $_ntr_save "${_ntr_predisplay}" "${_ntr_postdisplay}" \ + "${_ntr_savelim}" "${_ntr_changeno}" \ + "${_ntr_start}" "${_ntr_end}" "${_ntr_histno}" || return 1 fi + + BUFFER=${BUFFER[_ntr_start+1,_ntr_end]} + CURSOR=$_ntr_cursor + MARK=$_ntr_mark + zle split-undo + UNDO_LIMIT_NO=$UNDO_CHANGE_NO fi if [[ -z $_ntr_save && -z $_ntr_restore ]]; then zle recursive-edit _ntr_stat=$? + + [[ -n $_ntr_lbuf_return ]] && + builtin typeset -g ${_ntr_lbuf_return}="${LBUFFER}" + [[ -n $_ntr_rbuf_return ]] && + builtin typeset -g ${_ntr_rbuf_return}="${RBUFFER}" fi if [[ -n $_ntr_restore || -z $_ntr_save ]]; then if [[ -n $_ntr_restore ]]; then - if ! eval "_ntr_predisplay=\${${_ntr_restore}[1]} -_ntr_postdisplay=\${${_ntr_restore}[2]} -_ntr_lbuffer=\${${_ntr_restore}[3]} -_ntr_rbuffer=\${${_ntr_restore}[4]}"; then - zle -M Failed. + if ! { _ntr_predisplay="${${(@P)_ntr_restore}[1]}" + _ntr_postdisplay="${${(@P)_ntr_restore}[2]}" + _ntr_savelim="${${(@P)_ntr_restore}[3]}" + _ntr_changeno="${${(@P)_ntr_restore}[4]}" + _ntr_start="${${(@P)_ntr_restore}[5]}" + _ntr_end="${${(@P)_ntr_restore}[6]}" + _ntr_histno="${${(@P)_ntr_restore}[7]}" }; then + zle -M Failed. >&2 return 1 fi fi + _ntr_newbuf="$BUFFER" + HISTNO=_ntr_histno + zle undo $_ntr_changeno PREDISPLAY=$_ntr_predisplay POSTDISPLAY=$_ntr_postdisplay - LBUFFER="$_ntr_lbuffer$LBUFFER" - RBUFFER="$RBUFFER$_ntr_rbuffer" + BUFFER[_ntr_start+1,_ntr_end]="$_ntr_newbuf" + (( MARK = _ntr_start, CURSOR = _ntr_start + ${#_ntr_newbuf} )) + UNDO_LIMIT_NO=_ntr_savelim fi return $_ntr_stat diff --git a/Functions/Zle/read-from-minibuffer b/Functions/Zle/read-from-minibuffer index 8fec1105e..09dc68f97 100644 --- a/Functions/Zle/read-from-minibuffer +++ b/Functions/Zle/read-from-minibuffer @@ -20,7 +20,7 @@ done (( OPTIND > 1 )) && shift $(( OPTIND - 1 )) local readprompt="$1" lbuf_init="$2" rbuf_init="$3" -integer changeno=$UNDO_CHANGE_NO +integer savelim=$UNDO_LIMIT_NO changeno=$UNDO_CHANGE_NO { # Use anonymous function to make sure special values get restored, @@ -43,6 +43,8 @@ integer changeno=$UNDO_CHANGE_NO else local NUMERIC unset NUMERIC + zle split-undo + UNDO_LIMIT_NO=$UNDO_CHANGE_NO zle recursive-edit -K main stat=$? (( stat )) || REPLY=$BUFFER @@ -52,6 +54,7 @@ integer changeno=$UNDO_CHANGE_NO # 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 + UNDO_LIMIT_NO=savelim } return $stat diff --git a/Functions/Zle/smart-insert-last-word b/Functions/Zle/smart-insert-last-word index 27b0849ee..67adea760 100644 --- a/Functions/Zle/smart-insert-last-word +++ b/Functions/Zle/smart-insert-last-word @@ -47,13 +47,15 @@ setopt extendedglob nohistignoredups zle auto-suffix-retain # Not strictly necessary: -# (($+_ilw_hist)) || integer -g _ilw_hist _ilw_count _ilw_cursor _ilw_lcursor +# (($+_ilw_hist)) || integer -g _ilw_hist _ilw_count _ilw_cursor _ilw_lcursor _ilw_changeno integer cursor=$CURSOR lcursor=$CURSOR local lastcmd pattern numeric=$NUMERIC # Save state for repeated calls -if (( HISTNO == _ilw_hist && cursor == _ilw_cursor )); then +if (( HISTNO == _ilw_hist && cursor == _ilw_cursor && + UNDO_CHANGE_NO == _ilw_changeno )) +then NUMERIC=$[_ilw_count+1] lcursor=$_ilw_lcursor else @@ -61,7 +63,8 @@ else _ilw_lcursor=$lcursor fi # Handle the up to three arguments of .insert-last-word -if (( $+1 )); then +if (( $+1 )) +then if (( $+3 )); then ((NUMERIC = -($1))) else @@ -117,3 +120,6 @@ fi LBUFFER[lcursor+1,cursor+1]=$lastcmd[-NUMERIC] _ilw_cursor=$CURSOR + +# This is necessary to update UNDO_CHANGE_NO immediately +zle split-undo && _ilw_changeno=$UNDO_CHANGE_NO diff --git a/Functions/Zle/url-quote-magic b/Functions/Zle/url-quote-magic index fbcc7c19c..362d15c44 100644 --- a/Functions/Zle/url-quote-magic +++ b/Functions/Zle/url-quote-magic @@ -8,6 +8,12 @@ # autoload -Uz url-quote-magic # zle -N self-insert url-quote-magic +# As of zsh-5.0.9, the following may also be necessary in order to apply +# quoting to copy-pasted URLs: +# autload -Uz bracketed-paste-magic +# zle -N bracketed-paste bracketed-paste-magic +# See also backward-extend-paste in bracketed-paste-magic source file. + # A number of zstyles may be set to control the quoting behavior. # # url-metas |