mirror of
git://git.code.sf.net/p/zsh/code
synced 2025-01-10 20:11:16 +01:00
17410: narrow-to-region widgets
This commit is contained in:
parent
76c1e26713
commit
603956acdc
4 changed files with 125 additions and 0 deletions
|
@ -1,5 +1,9 @@
|
|||
2002-07-04 Peter Stephenson <pws@csr.com>
|
||||
|
||||
* 17410: Doc/Zsh/contrib.yo, Functions/Zle/narrow-to-region,
|
||||
Functions/Zle/narrow-to-region-invisible: use 17390/17405 to
|
||||
create narrow-to-region widget/function.
|
||||
|
||||
* 17405: Src/Zle/zle_main.c, Src/Zle/zle_params.c, Doc/Zsh/zle.yo:
|
||||
adapt 17390 so that PREDISPLAY and POSTDISPLAY are reset when zle
|
||||
is entered.
|
||||
|
|
|
@ -475,6 +475,40 @@ into the command line.
|
|||
|
||||
example(bindkey '^Xf' insert-files)
|
||||
)
|
||||
tindex(narrow-to-region)
|
||||
tindex(narrow-to-region-invisible)
|
||||
xitem(tt(narrow-to-region [ -p) var(pre) tt(] [ -P) var(post) tt(] [ -n ] [) var(start) var(end) tt(]))
|
||||
item(tt(narrow-to-region-invisible))(
|
||||
Narrow the editable portion of the buffer to the region between the cursor
|
||||
and the mark, which may be in either order. The region may not be empty.
|
||||
|
||||
tt(narrow-to-region) may be used as a widget or called as a function from a
|
||||
user-defined widget; by default, the text outside the editable area remains
|
||||
visible. Various options and arguments are available when it is called as
|
||||
a function.
|
||||
|
||||
The options tt(-p) var(pretext) and tt(-P) var(posttext) may be
|
||||
used to replace the text before and after the display for the duration of
|
||||
the function; either or both may be an empty string.
|
||||
|
||||
If the option tt(-n) is also given, var(pretext) or var(posttext) will only
|
||||
be inserted if there is text before or after the region respectively which
|
||||
will be made invisible.
|
||||
|
||||
Two numeric arguments may be given which will be used instead of the cursor
|
||||
and mark positions.
|
||||
|
||||
tt(narrow-to-region-invisible) is a simple widget which calls
|
||||
tt(narrow-to-region) with arguments which replace any text outside the
|
||||
region with `tt(...)'.
|
||||
|
||||
On return from both widgets, the display is restored by any zle command
|
||||
which would usually cause the line to be accepted or aborted. Hence an
|
||||
additional such command is required to accept or abort the current line.
|
||||
|
||||
The return status of both widgets is zero if the line was accepted, else
|
||||
non-zero.
|
||||
)
|
||||
tindex(predict-on)
|
||||
tindex(predict-off)
|
||||
item(tt(predict-on))(
|
||||
|
|
82
Functions/Zle/narrow-to-region
Normal file
82
Functions/Zle/narrow-to-region
Normal file
|
@ -0,0 +1,82 @@
|
|||
# Restrict the start of the editable line to the region between cursor
|
||||
# and mark (including the character at the end). Can be bound used as
|
||||
# a zle widget, or called as a function from another widget.
|
||||
#
|
||||
# Optionally accepts exactly two arguments, which are used instead of
|
||||
# $CURSOR and $MARK as limits to the range.
|
||||
#
|
||||
# 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.
|
||||
# -n Only replace the text before or after the region with
|
||||
# the -p or -P options if the text was not empty.
|
||||
# Either or both may be empty.
|
||||
|
||||
emulate -L zsh
|
||||
setopt extendedglob
|
||||
|
||||
local lbuffer rbuffer predisplay=$PREDISPLAY postdisplay=$POSTDISPLAY
|
||||
integer start end swap cursor=$CURSOR mark=$MARK stat
|
||||
|
||||
local opt pretext posttext usepretext useposttext nonempty
|
||||
|
||||
while getopts "np:P:" opt; do
|
||||
case $opt in
|
||||
(n) nonempty=1
|
||||
;;
|
||||
(p) pretext=$OPTARG usepretext=1
|
||||
;;
|
||||
(P) posttext=$OPTARG useposttext=1
|
||||
;;
|
||||
(*) [[ $opt != '?' ]] && print "$0: unhandled option: $opt" >&2
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
(( OPTIND > 1 )) && shift $(( OPTIND - 1 ))
|
||||
|
||||
if (( $# )); then
|
||||
if (( $# != 2 )); then
|
||||
zle -M "$0: supply zero or two arguments"
|
||||
return 1
|
||||
fi
|
||||
start=$1
|
||||
end=$2
|
||||
else
|
||||
start=$MARK
|
||||
end=$CURSOR
|
||||
fi
|
||||
|
||||
if (( start == end )); then
|
||||
return 1
|
||||
elif (( start > end )); then
|
||||
swap=start
|
||||
start=end
|
||||
end=swap
|
||||
fi
|
||||
|
||||
(( end++, cursor -= start, mark -= start ))
|
||||
|
||||
lbuffer=${BUFFER[1,start]}
|
||||
if [[ -z $usepretext || ( -n $nonempty && -z $lbuffer ) ]]; then
|
||||
pretext=$lbuffer
|
||||
fi
|
||||
rbuffer=${BUFFER[end,-1]}
|
||||
if [[ -z $useposttext || ( -n $nonempty && -z $rbuffer ) ]]; then
|
||||
posttext=$rbuffer
|
||||
fi
|
||||
PREDISPLAY="$predisplay$pretext"
|
||||
POSTDISPLAY="$posttext$postdisplay"
|
||||
BUFFER=${BUFFER[start+1,end-1]}
|
||||
CURSOR=$cursor
|
||||
MARK=$mark
|
||||
|
||||
zle recursive-edit
|
||||
stat=$?
|
||||
|
||||
PREDISPLAY=$predisplay
|
||||
POSTDISPLAY=$postdisplay
|
||||
LBUFFER="$lbuffer$LBUFFER"
|
||||
RBUFFER="$RBUFFER$rbuffer"
|
||||
|
||||
return $stat
|
5
Functions/Zle/narrow-to-region-invisible
Normal file
5
Functions/Zle/narrow-to-region-invisible
Normal file
|
@ -0,0 +1,5 @@
|
|||
# As narrow-to-region, but replaces the text outside the editable region
|
||||
# with `...' if it was non-empty. Can be used directly as a widget.
|
||||
|
||||
autoload -U narrow-to-region
|
||||
narrow-to-region -p '...' -P '...' -n
|
Loading…
Reference in a new issue