summary refs log tree commit diff
path: root/Functions/Zle
diff options
context:
space:
mode:
authorPeter Stephenson <pws@users.sourceforge.net>2002-07-05 10:49:46 +0000
committerPeter Stephenson <pws@users.sourceforge.net>2002-07-05 10:49:46 +0000
commit363079af7600095d2accf6d2d6eebe68a97b0d81 (patch)
tree496962f30da40f5dfcfb33e1801f45d21d8c3ad3 /Functions/Zle
parent135db923be1336fc8d83b94678097c8c963cce0a (diff)
downloadzsh-363079af7600095d2accf6d2d6eebe68a97b0d81.tar.gz
zsh-363079af7600095d2accf6d2d6eebe68a97b0d81.tar.xz
zsh-363079af7600095d2accf6d2d6eebe68a97b0d81.zip
17414: narrow-to-region save and restore.
Diffstat (limited to 'Functions/Zle')
-rw-r--r--Functions/Zle/narrow-to-region135
1 files changed, 89 insertions, 46 deletions
diff --git a/Functions/Zle/narrow-to-region b/Functions/Zle/narrow-to-region
index baa8b48c2..1f8438789 100644
--- a/Functions/Zle/narrow-to-region
+++ b/Functions/Zle/narrow-to-region
@@ -8,75 +8,118 @@
 # 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.
 #   -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.
+#   -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.
 
 emulate -L zsh
 setopt extendedglob
 
-local lbuffer rbuffer predisplay=$PREDISPLAY postdisplay=$POSTDISPLAY
-integer start end swap cursor=$CURSOR mark=$MARK stat
+local _ntr_lbuffer _ntr_rbuffer 
+local _ntr_predisplay=$PREDISPLAY _ntr_postdisplay=$POSTDISPLAY
+integer _ntr_start _ntr_end _ntr_swap _ntr_cursor=$CURSOR _ntr_mark=$MARK
+integer _ntr_stat
 
-local opt pretext posttext usepretext useposttext nonempty
+local _ntr_opt _ntr_pretext _ntr_posttext _ntr_usepretext _ntr_useposttext
+local _ntr_nonempty _ntr_save _ntr_restore
 
-while getopts "np:P:" opt; do
-  case $opt in
-    (n) nonempty=1
+while getopts "np:P:R:S:" _ntr_opt; do
+  case $_ntr_opt in
+    (n) _ntr_nonempty=1
+	;;
+    (p) _ntr_pretext=$OPTARG _ntr_usepretext=1
 	;;
-    (p) pretext=$OPTARG usepretext=1
+    (P) _ntr_posttext=$OPTARG _ntr_useposttext=1
 	;;
-    (P) posttext=$OPTARG useposttext=1
+    (R) _ntr_restore=$OPTARG
+        ;;
+    (S)	_ntr_save=$OPTARG
 	;;
-    (*) [[ $opt != '?' ]] && print "$0: unhandled option: $opt" >&2
+    (*) [[ $_ntr_opt != '?' ]] && print "$0: unhandled option: $_ntr_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
+if [[ $_ntr_restore = _ntr_* || $_ntr_save = _ntr_* ]]; then
+  zle -M "$0: _ntr_ prefix is reserved" >&2
   return 1
-elif (( start > end )); then
-  swap=start
-  start=end
-  end=swap
 fi
 
-(( end++, cursor -= start, mark -= start ))
+if [[ -n $_ntr_save || -z $_ntr_restore ]]; then
 
-lbuffer=${BUFFER[1,start]}
-if [[ -z $usepretext || ( -n $nonempty && -z $lbuffer ) ]]; then
-  pretext=$lbuffer
+  if (( $# )); then
+    if (( $# != 2 )); then
+      zle -M "$0: supply zero or two arguments"
+      return 1
+    fi
+    _ntr_start=$1
+    _ntr_end=$2
+  else
+    _ntr_start=$MARK
+    _ntr_end=$CURSOR
+  fi
+
+  if (( _ntr_start == _ntr_end )); then
+    return 1
+  elif (( _ntr_start > _ntr_end )); then
+    _ntr_swap=_ntr_start
+    _ntr_start=_ntr_end
+    _ntr_end=_ntr_swap
+  fi
+
+  (( _ntr_end++, _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]}
+  if [[ -z $_ntr_useposttext || ( -n $_ntr_nonempty && -z $_ntr_rbuffer ) ]]
+  then
+    _ntr_posttext=$_ntr_rbuffer
+  fi
+  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
+  fi
 fi
-rbuffer=${BUFFER[end,-1]}
-if [[ -z $useposttext || ( -n $nonempty && -z $rbuffer ) ]]; then
-  posttext=$rbuffer
+
+if [[ -z $_ntr_save && -z $_ntr_restore ]]; then
+  zle recursive-edit
+  _ntr_stat=$?
 fi
-PREDISPLAY="$predisplay$pretext"
-POSTDISPLAY="$posttext$postdisplay"
-BUFFER=${BUFFER[start+1,end-1]}
-CURSOR=$cursor
-MARK=$mark
 
-zle recursive-edit
-stat=$?
+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.
+      return 1
+    fi
+  fi
 
-PREDISPLAY=$predisplay
-POSTDISPLAY=$postdisplay
-LBUFFER="$lbuffer$LBUFFER"
-RBUFFER="$RBUFFER$rbuffer"
+  PREDISPLAY=$_ntr_predisplay
+  POSTDISPLAY=$_ntr_postdisplay
+  LBUFFER="$_ntr_lbuffer$LBUFFER"
+  RBUFFER="$RBUFFER$_ntr_rbuffer"
+fi
 
-return $stat
+return $_ntr_stat