From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25686 invoked from network); 14 Aug 2006 13:58:51 -0000 X-Spam-Checker-Version: SpamAssassin 3.1.4 (2006-07-25) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=AWL,BAYES_00, FORGED_RCVD_HELO autolearn=ham version=3.1.4 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 14 Aug 2006 13:58:51 -0000 Received-SPF: none (ns1.primenet.com.au: domain at sunsite.dk does not designate permitted sender hosts) Received: (qmail 36111 invoked from network); 14 Aug 2006 13:58:42 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 14 Aug 2006 13:58:42 -0000 Received: (qmail 5207 invoked by alias); 14 Aug 2006 13:58:40 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 22606 Received: (qmail 5198 invoked from network); 14 Aug 2006 13:58:39 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 14 Aug 2006 13:58:39 -0000 Received: (qmail 35797 invoked from network); 14 Aug 2006 13:58:39 -0000 Received: from cluster-c.mailcontrol.com (168.143.177.190) by a.mx.sunsite.dk with SMTP; 14 Aug 2006 13:58:36 -0000 Received: from cameurexb01.EUROPE.ROOT.PRI ([62.189.241.200]) by rly11c.srv.mailcontrol.com (MailControl) with ESMTP id k7EDwRGH019885 for ; Mon, 14 Aug 2006 14:58:33 +0100 Received: from news01.csr.com ([10.103.143.38]) by cameurexb01.EUROPE.ROOT.PRI with Microsoft SMTPSVC(6.0.3790.1830); Mon, 14 Aug 2006 14:58:28 +0100 Received: from news01.csr.com (localhost.localdomain [127.0.0.1]) by news01.csr.com (8.13.4/8.13.4) with ESMTP id k7EDwRlt024166 for ; Mon, 14 Aug 2006 14:58:27 +0100 Received: from csr.com (pws@localhost) by news01.csr.com (8.13.4/8.13.4/Submit) with ESMTP id k7EDwQMd024163 for ; Mon, 14 Aug 2006 14:58:26 +0100 Message-Id: <200608141358.k7EDwQMd024163@news01.csr.com> X-Authentication-Warning: news01.csr.com: pws owned process doing -bs To: zsh-workers@sunsite.dk (Zsh hackers list) Subject: PATCH: match-word-context Date: Mon, 14 Aug 2006 14:58:26 +0100 From: Peter Stephenson X-OriginalArrivalTime: 14 Aug 2006 13:58:28.0564 (UTC) FILETIME=[B818A940:01C6BFA9] Content-Type: text/plain MIME-Version: 1.0 X-Scanned-By: MailControl A-07-04-01 (www.mailcontrol.com) on 10.67.0.121 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 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