about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Stephenson <pws@users.sourceforge.net>2004-06-23 16:14:24 +0000
committerPeter Stephenson <pws@users.sourceforge.net>2004-06-23 16:14:24 +0000
commitfd1676f3973eb2c72655f4249675c0f39dac73c6 (patch)
tree2ac6dc03d858db65684d16be848925c419723c8f
parentceb7a0983360e53fed050367db92b96786d32597 (diff)
downloadzsh-fd1676f3973eb2c72655f4249675c0f39dac73c6.tar.gz
zsh-fd1676f3973eb2c72655f4249675c0f39dac73c6.tar.xz
zsh-fd1676f3973eb2c72655f4249675c0f39dac73c6.zip
20089: various zed improvements
-rw-r--r--ChangeLog5
-rw-r--r--Doc/Zsh/contrib.yo34
-rw-r--r--Functions/Misc/zed64
-rw-r--r--Functions/Zle/zed-set-file-name9
4 files changed, 80 insertions, 32 deletions
diff --git a/ChangeLog b/ChangeLog
index c03905189..483d57d19 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2004-06-23  Peter Stephenson  <pws@csr.com>
+
+	* 20089: Doc/Zsh/contrib.yo, Functions/Misc/zed,
+	Functions/Zle/zed-set-file-name: various zed enhancements.
+
 2004-06-22  Clint Adams  <clint@zsh.org>
 
 	* 20091: Src/Modules/pcre.c: silence gcc "unused parameter" warnings.
diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo
index 56eb62cfb..5ab627352 100644
--- a/Doc/Zsh/contrib.yo
+++ b/Doc/Zsh/contrib.yo
@@ -1311,11 +1311,12 @@ referred to in tt(ZCALCPROMPT) as `tt(%1v)'.  The default prompt is
 See the comments in the function for a few extra tips.
 )
 findex(zed)
-item(tt(zed) [ tt(-f) ] var(name))(
+xitem(tt(zed) [ tt(-f) ] var(name))
+item(tt(zed -b))(
 This function uses the ZLE editor to edit a file or function.
 
-Only one var(name) argument is recognized (additional arguments are
-ignored).  If the tt(-f) option is given, the name is taken to be that of
+Only one var(name) argument is allowed.
+If the tt(-f) option is given, the name is taken to be that of
 a function; if the function is marked for autoloading, tt(zed) searches
 for it in the tt(fpath) and loads it.  Note that functions edited this way
 are installed into the current shell, but em(not) written back to the
@@ -1331,18 +1332,29 @@ is run.  They can be used to provide special key bindings used only in zed.
 
 If it creates the keymap, tt(zed) rebinds the return key to insert a line
 break and `tt(^X^W)' to accept the edit in the tt(zed) keymap, and binds
-`tt(ZZ)' to accept the edit in the tt(zed-vicmd) keymap.  If the tt(zed)
-keymap is created by hand, the user will need to bind:
+`tt(ZZ)' to accept the edit in the tt(zed-vicmd) keymap.
 
-example(zle -M zed '^M' self-insert-unmeta
-zle -M zed '^X^W' accept-line)
-
-for this behaviour, and if tt(zed-vicmd) is created by hand,
-
-example(zle -M zed-vicmd 'ZZ' accept-line)
+The bindings alone can be installed by running `tt(zed -b)'.  This is
+suitable for putting into a startup file.  Note that, if rerun,
+this will overwrite the existing tt(zed) and tt(zed-vicmd) keymaps.
 
 Completion is available, and styles may be set with the context prefix
 `tt(:completion:zed)'.
+
+A zle widget tt(zed-set-file-name) is available.  This can be called by
+name from within zed using `tt(\ex zed-set-file-name)' (note, however, that
+because of zed's rebindings you will have to type tt(^j) at the end instead
+of the return key), or can be bound to a key in either of the tt(zed) or
+tt(zed-vicmd) keymaps after `tt(zed -b)' has been run.  When the widget is
+called, it prompts for a new name for the file being edited.  When zed
+exits the file will be written under that name and the original file will
+be left alone.  The widget has no effect with `tt(zed -f)'.
+
+While tt(zed-set-file-name) is running, zed uses the keymap
+tt(zed-normal-keymap), which is linked from the main keymap in effect
+at the time zed initialised its bindings.  (This is to make the return key
+operate normally.)  The result is that if the main keymap has been changed,
+the widget won't notice.  This is not a concern for most users.
 )
 findex(zcp)
 findex(zln)
diff --git a/Functions/Misc/zed b/Functions/Misc/zed
index eef8ee218..b1aa8f4eb 100644
--- a/Functions/Misc/zed
+++ b/Functions/Misc/zed
@@ -5,28 +5,46 @@
 # Edit small files with the command line editor.
 # Use ^X^W to save, ^C to abort.
 # Option -f: edit shell functions.  (Also if called as fned.)
-#
-# Completion: use
-# compctl -f -x 'w[1,-f]' -F -- zed
-#
 
-local var fun
+local var opt zed_file_name
 # We do not want timeout while we are editing a file
-integer TMOUT=0
+integer TMOUT=0 okargs=1 fun bind
+
+while getopts "fb" opt; do
+  case $opt in
+    (f)
+    fun=1
+    ;;
+
+    (b)
+    bind=1
+    ;;
+  esac
+done
+shift $(( OPTIND - 1 ))
 
-[[ $1 = -f || $0 = fned ]] && fun=1
-[[ $1 = -(|-|f) ]] && shift
+[[ $0 = fned ]] && fun=1
+(( bind )) && okargs=0
 
-[[ -z "$1" ]] && echo 'Usage: "zed filename" or "zed -f function"' && return 1
+if (( $# != okargs )); then
+    echo 'Usage:
+zed filename
+zed -f function
+zed -b'
+    return 1
+fi
 
 local curcontext=zed:::
 
 zstyle -m ":completion:zed:*" insert-tab '*' ||
     zstyle ":completion:zed:*" insert-tab yes
 
-if ! bindkey -M zed >&/dev/null; then
+if (( bind )) || ! bindkey -M zed >&/dev/null; then
   # Make the zed keymap a copy of the current main.
   bindkey -N zed main
+  # Save the current main.  In zle widgets called from
+  # zed we may want to set this temporally.
+  bindkey -A main zed-normal-keymap
 
   # Assign some default keys.
   # Depending on your stty's, you may be able to use ^J as accept-line, else:
@@ -35,13 +53,20 @@ if ! bindkey -M zed >&/dev/null; then
   # a nicety.
   bindkey -M zed '^x^w' accept-line
   bindkey -M zed '^M' self-insert-unmeta
+
+  # Make zed-set-file-name available.
+  # Assume it's in fpath; there's no error at this point if it isn't
+  autoload -U zed-set-file-name
+  zle -N zed-set-file-name
 fi
-if ! bindkey -M zed-vicmd >&/dev/null; then
+if (( bind )) || ! bindkey -M zed-vicmd >&/dev/null; then
   bindkey -N zed-vicmd vicmd
 
   bindkey -M zed-vicmd "ZZ" accept-line
 fi
 
+(( bind )) && return 0
+
 # don't mangle !'s
 setopt localoptions nobanghist
 
@@ -49,25 +74,22 @@ if ((fun)) then
   var="$(functions $1)"
   # If function is undefined but autoloadable, load it
   if [[ $var = *\#\ undefined* ]] then
-    local dir
-    for dir in $fpath; do
-      if [[ -f $dir/$1 ]] then
-	var="$1() {
-$(<$dir/$1)
-}"
-	break
-      fi
-    done
+      autoload +X $1
   elif [[ -z $var ]] then
     var="$1() {
 }"
   fi
   vared -M zed -m zed-vicmd var && eval function "$var"
 else
+  zed_file_name=$1
   [[ -f $1 ]] && var="$(<$1)"
   while vared -M zed -m zed-vicmd var
   do
-    (print -r -- "$var" >| $1) && break
+    {
+      print -r -- "$var" >| $zed_file_name
+    } always {
+      (( TRY_BLOCK_ERROR = 0 ))
+    } && break
     echo -n -e '\a'
   done
 fi
diff --git a/Functions/Zle/zed-set-file-name b/Functions/Zle/zed-set-file-name
new file mode 100644
index 000000000..ca7758c37
--- /dev/null
+++ b/Functions/Zle/zed-set-file-name
@@ -0,0 +1,9 @@
+emulate -L zsh
+
+autoload -U read-from-minibuffer
+
+zle -K zed-normal-keymap
+
+local REPLY
+read-from-minibuffer "File name: "
+zed_file_name=$REPLY