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

PATCH: match-word-context



Quite a while ago I proposed a patch to match-words-by-style that would
allow more control of the context for styles used by the word functions.
I've decided I like it after all, but I've implemented it in a slightly
more self-contained fashion.  See the documentation.

I've also removed the workaround in match-words-by-style for a bug that
was fixed some time ago.

Index: Doc/Zsh/contrib.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/contrib.yo,v
retrieving revision 1.61
diff -u -r1.61 contrib.yo
--- Doc/Zsh/contrib.yo	9 Aug 2006 16:33:34 -0000	1.61
+++ Doc/Zsh/contrib.yo	14 Aug 2006 13:54:53 -0000
@@ -384,12 +384,13 @@
 tindex(up-case-word-match)
 tindex(down-case-word-match)
 tindex(select-word-style)
+tindex(match-word-context)
 tindex(match-words-by-style)
 xitem(tt(forward-word-match), tt(backward-word-match))
 xitem(tt(kill-word-match), tt(backward-kill-word-match))
 xitem(tt(transpose-words-match), tt(capitalize-word-match))
 xitem(tt(up-case-word-match), tt(down-case-word-match))
-item(tt(select-word-style), tt(match-words-by-style))(
+item(tt(select-word-style), tt(match-word-context), tt(match-words-by-style))(
 The eight `tt(-match)' functions are drop-in replacements for the
 builtin widgets without the suffix.  By default they behave in a similar
 way.  However, by the use of styles and the function tt(select-word-style),
@@ -463,7 +464,7 @@
 )
 enditem()
 
-The first three of those styles usually use tt($WORDCHARS), but the value
+The first three of those rules usually use tt($WORDCHARS), but the value
 in the parameter can be overridden by the style tt(word-chars), which works
 in exactly the same way as tt($WORDCHARS).  In addition, the style
 tt(word-class) uses character class syntax to group characters and takes
@@ -474,7 +475,7 @@
 including `tt(])', `tt(^)' and `tt(-)' as these are special inside
 character classes.
 
-The final style is tt(skip-chars).  This is mostly useful for
+The style tt(skip-chars) is mostly useful for
 tt(transpose-words) and similar functions.  If set, it gives a count of
 characters starting at the cursor position which will not be considered
 part of the word and are treated as space, regardless of what they actually
@@ -486,6 +487,17 @@
 the var(X) of tt(foo)var(X)tt(bar), where var(X) can be any character, then
 the resulting expression is tt(bar)var(X)tt(foo).
 
+Finer grained control can be obtained by setting the style
+tt(word-context) to an array of pairs of entries.  Each pair of entries
+consists of a var(pattern) and a var(subcontext).  The word the cursor is on is
+matched against each var(pattern) in turn until one matches; if it does,
+the context is extended by a colon and the corresponding var(subcontext).
+Note that the test is made against the original word on the line, with
+no stripping of quotes.  If the cursor is at the end of the line
+the test is maded against an empty string; if it is on whitespace
+between words the test is made against a single space.  Some examples
+are given below.
+
 Here are some examples of use of the styles, actually taken from the
 simplified interface in tt(select-word-style):
 
@@ -501,6 +513,21 @@
 Uses space-delimited words for widgets with the word `kill' in the name.
 Neither of the styles tt(word-chars) nor tt(word-class) is used in this case.
 
+Here are some examples of use of the tt(word-context) style to extend
+the context.
+
+example(zstyle ':zle:*' word-context "[[:space:]]" whitespace "*/*" file
+zstyle ':zle:transpose-words:whitespace' word-style shell
+zstyle ':zle:transpose-words:filename' word-style normal
+zstyle ':zle:transpose-words:filename' word-chars '')
+
+This provides two different ways of using tt(transpose-words) depending on
+whether the cursor is on whitespace between words or on a filename, here
+any word containing a tt(/).  On whitespace, complete arguments as defined
+by standard shell rules will be transposed.  In a filename, only
+alphanumerics will be transposed.  Elsewhere, words will be transposed
+using the default style for tt(:zle:transpose-words).
+
 The word matching and all the handling of tt(zstyle) settings is actually
 implemented by the function tt(match-words-by-style).  This can be used to
 create new user-defined widgets.  The calling function should set the local
@@ -527,6 +554,10 @@
 
 For example, tt(match-words-by-style -w shell -c 0) may be used to
 extract the command argument around the cursor.
+
+The tt(word-context) style is implemented by the function
+tt(match-word-context).  This should not usually need to be called
+directly.
 )
 tindex(delete-whole-word-match)
 item(tt(delete-whole-word-match))(
Index: Functions/Zle/match-words-by-style
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Zle/match-words-by-style,v
retrieving revision 1.5
diff -u -r1.5 match-words-by-style
--- Functions/Zle/match-words-by-style	9 Dec 2004 17:42:29 -0000	1.5
+++ Functions/Zle/match-words-by-style	14 Aug 2006 13:54:56 -0000
@@ -69,11 +69,12 @@
 
 local wordstyle spacepat wordpat1 wordpat2 opt charskip wordchars wordclass
 local match mbegin mend pat1 pat2 word1 word2 ws1 ws2 ws3 skip
-local nwords MATCH MBEGIN MEND
+local nwords MATCH MBEGIN MEND addcontext
 
-if [[ -z $curcontext ]]; then
-    local curcontext=:zle:match-words-by-style
-fi
+local curcontext=${curcontext:-:zle:match-words-by-style}
+
+autoload -U match-word-context
+match-word-context
 
 while getopts "w:s:c:C:" opt; do
   case $opt in
@@ -108,27 +109,12 @@
 	  # This splits the line into words as the shell understands them.
 	  bufwords=(${(z)LBUFFER})
 	  nwords=${#bufwords}
-	  # Work around bug: if stripping quotes failed, a bogus
-	  # space is appended.  Not a good test, since this may
-	  # be a quoted space, but it's hard to get right.
-	  wordpat1=${bufwords[-1]}
-	  if [[ ${wordpat1[-1]} = ' ' ]]; then
-	    wordpat1=${(q)wordpat1[1,-2]}
-	  else
-	    wordpat1="${(q)wordpat1}"
-	  fi
+	  wordpat1="${(q)bufwords[-1]}"
 
 	  # Take substring of RBUFFER to skip over $skip characters
 	  # from the cursor position.
 	  bufwords=(${(z)RBUFFER[1+$skip,-1]})
-	  # Work around bug again.
-	  wordpat2=${bufwords[1]}
-	  if [[ ${wordpat2[-1]} = ' ' ]]
-	  then
-	    wordpat2=${(q)wordpat2[1,-2]}
-	  else
-	    wordpat2="${(q)wordpat2}"
-	  fi
+	  wordpat2="${(q)bufwords[1]}"
 	  spacepat='[[:space:]]#'
 
 	  # Assume the words are at the top level, i.e. if we are inside

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php



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