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

Re: Word-forward/word-backward as in bash / emacs



06/04/2012 12:36 AM, Peter Stephenson написал:
On Sun, 03 Jun 2012 23:28:03 +0300
Konstantine Rybnikov<k-bx@xxxxxxxx>  wrote:
Only answer I got is to do "select-word-style bash", which is absolutely
not the same as bash (I also provided an example there that explains "why").
So, with "select-word-style bash" that forward-word thing is the only
difference, right?  It seemed to be when I tried your example.

I actually noted forward-word was a bit funny when I wrote the function
forward-word-match that implements it when you uses select-word-style.
See if this does the trick --- it patches the function forward-word-match
which will be installed in your $fpath.

I've made the old behaviour a separate (non-default) option for the sake
of this, but if this is what's needed we probably need to think a bit
more about when it should apply.  The original behaviour works better
with zsh's normal set up because then more characters are considered
part of a word, so skipping non-word-characters at the start is less
likely to have side effects.

One possibility would be to reverse the sense of the option (to
skip-whitespace-first), but to set it to "true" from select word-style
if it's not explicitly set and the new style is "bash".  That keeps
compatibility in both cases.  However, there are other ways of doing it.

(I also standardised the indentation while I was there, so there's more
changed than is really necessary.)

Index: Functions/Zle/forward-word-match
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Zle/forward-word-match,v
retrieving revision 1.3
diff -p -u -r1.3 forward-word-match
--- Functions/Zle/forward-word-match	28 Jul 2010 13:33:59 -0000	1.3
+++ Functions/Zle/forward-word-match	3 Jun 2012 21:22:36 -0000
@@ -8,32 +8,35 @@ local -a matched_words
  integer count=${NUMERIC:-1}

  if (( count<  0 )); then
-    (( NUMERIC = -count ))
-    zle ${WIDGET/forward/backward}
-    return
+  (( NUMERIC = -count ))
+  zle ${WIDGET/forward/backward}
+  return
  fi

  while (( count-- )); do
-
-    match-words-by-style
-
+  match-words-by-style
+
+  if zstyle -t $curcontext skip-whitespace-last; then
      # For some reason forward-word doesn't work like the other word
      # commands; it skips whitespace only after any matched word
      # characters.
-
      if [[ -n $matched_words[4] ]]; then
-        # just skip the whitespace
-	word=$matched_words[4]
-    else
-        # skip the word and trailing whitespace
-	word=$matched_words[5]$matched_words[6]
-    fi
-
-    if [[ -n $word ]]; then
-	(( CURSOR += ${#word} ))
+      # just skip the whitespace
+      word=$matched_words[4]
      else
-	return 1
+      # skip the word and trailing whitespace
+      word=$matched_words[5]$matched_words[6]
      fi
+  else
+    # more standard behaviour: skip leading whitespace and the word.
+    word=$matched_words[4]$matched_words[5]
+  fi
+
+  if [[ -n $word ]]; then
+    (( CURSOR += ${#word} ))
+  else
+    return 1
+  fi
  done

  return 0
Ok, it looks like Moritz Bunkus updated he's answer with working implementation of forward-word-match. You're right, the only difference if forward-word things.

Just tried your patch -- yes, looks like it works as expected. Thanks!



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