Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: zed improvements



Here are some improvements to zed.  The original idea was that it could
use `always' instead of a subshell, but that's only a tiny part of the
patch.

I have a feeling the documentation may be a bit indigestible as written.
I'll reread it when I've forgotten what it does.

- `zle -b' allows you to set up the keymaps without running zed
  (so you can easily add your own bindings),
- the widget zed-set-file-name allows you to change the name of the
  file you are writing to in case you don't want to overwrite the
  original,
- the loop to read in an autoloadable functions has been replaced by
  the equivalent but faster `autoload +X'.

Index: Doc/Zsh/contrib.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/contrib.yo,v
retrieving revision 1.31
diff -u -r1.31 contrib.yo
--- Doc/Zsh/contrib.yo	6 Apr 2004 09:16:59 -0000	1.31
+++ Doc/Zsh/contrib.yo	22 Jun 2004 14:43:41 -0000
@@ -1311,11 +1311,12 @@
 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 @@
 
 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)
Index: Functions/Misc/zed
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Misc/zed,v
retrieving revision 1.7
diff -u -r1.7 zed
--- Functions/Misc/zed	6 Apr 2004 09:16:59 -0000	1.7
+++ Functions/Misc/zed	22 Jun 2004 14:43:41 -0000
@@ -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
 
-[[ $1 = -f || $0 = fned ]] && fun=1
-[[ $1 = -(|-|f) ]] && shift
-
-[[ -z "$1" ]] && echo 'Usage: "zed filename" or "zed -f function"' && return 1
+while getopts "fb" opt; do
+  case $opt in
+    (f)
+    fun=1
+    ;;
+
+    (b)
+    bind=1
+    ;;
+  esac
+done
+shift $(( OPTIND - 1 ))
+
+[[ $0 = fned ]] && fun=1
+(( bind )) && okargs=0
+
+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 @@
   # 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 @@
   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
Index: Functions/Zle/zed-set-file-name
===================================================================
RCS file: Functions/Zle/zed-set-file-name
diff -N Functions/Zle/zed-set-file-name
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Functions/Zle/zed-set-file-name	22 Jun 2004 14:43:41 -0000
@@ -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

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************



Messages sorted by: Reverse Date, Date, Thread, Author