zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-workers@zsh.org
Subject: "drop-in replacement" and transpose-words-match
Date: Mon, 11 Jan 2016 23:32:59 -0800	[thread overview]
Message-ID: <160111233259.ZM6719@torch.brasslantern.com> (raw)

Of the eight match-words-by-style functions, transpose-words-match is the
only one that is not really a drop-in, because it adopts the paradigm of
match-words-by-style where a cursor in the middle of a word splits that
word (treating the LBUFFER part as "word before the cursor" and RBUFFER
part as "word after the cursor").

There are some useful things about this behavior, e.g., "skip-chars 1" 
where you can make "fooXbar" swap as "barXfoo", but there are also some
drawbacks -- cases where transpose-words would do something but -match
does nothing or does something very different.  So how about this:  if
the skip-chars style is set (even to zero), then keep the cursor where
it is and split the word, otherwise act like the builtin.  The subword
flavor of word-style can similarly be checked.

Also, it looks to me as though transpose-words-match assumes zsh emulation
but (unlike e.g. select-word-style) never declares it.  Also "skip" is a
local in match-words-by-style, it doesn't help to declare it here.

This still isn't perfect -- if you are on or immediately after a space
in the middle of a quoted string, for example, you'll swap around that
space.  I haven't decided on the best way to deal with that.  But this
works much better if you are at the end of a line and invoke transpose:
it swaps the two words to the left, like the builtin.


diff --git a/Functions/Zle/transpose-words-match b/Functions/Zle/transpose-words-match
index c1db310..4d2ac71 100644
--- a/Functions/Zle/transpose-words-match
+++ b/Functions/Zle/transpose-words-match
@@ -11,14 +11,23 @@
 # on X would be turned into `barXfoo' with the cursor still on the X,
 # regardless of what the character X is.
 
+emulate -L zsh
 autoload -Uz match-words-by-style
 
-local curcontext=":zle:$WIDGET" skip
+local curcontext=":zle:$WIDGET"
 local -a matched_words
 integer count=${NUMERIC:-1} neg
 
 (( count < 0 )) && (( count = -count, neg = 1 ))
 
+if [[ $WIDGET == transpose-words ]]; then
+  # default is to be a drop-in replacement, check styles for change
+  zstyle -m $curcontext skip-chars \* ||
+  zstyle -m $curcontext word-style '*subword*' ||
+  { [[ $LBUFFER[-1] != [[:space:]] && $RBUFFER[1] != [[:space:]] ||
+       -z ${RBUFFER//[[:space:]]/} ]] && zle backward-word }
+fi
+
 while (( count-- > 0 )); do
     match-words-by-style
 


             reply	other threads:[~2016-01-12  7:32 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-12  7:32 Bart Schaefer [this message]
2016-01-18  2:25 ` Daniel Shahaf
2016-01-18 16:29   ` Bart Schaefer
2016-01-18 17:04 ` Sebastian Gniazdowski
2016-01-19  6:31   ` Bart Schaefer
2016-01-19  8:28     ` Sebastian Gniazdowski
2016-01-20  3:56       ` Bart Schaefer
2016-01-23 23:53         ` Daniel Shahaf
2016-01-24  6:20           ` Slow highlighting (Re: "drop-in replacement" and transpose-words-match) Bart Schaefer
2016-01-26 22:50             ` Daniel Shahaf
2016-01-27  4:31               ` Bart Schaefer
2016-01-27  5:10                 ` Mikael Magnusson
2016-01-29  9:18                 ` Daniel Shahaf
2016-02-10 16:32             ` Sebastian Gniazdowski
2016-02-10 18:18               ` Bart Schaefer
2016-02-10 18:37                 ` Sebastian Gniazdowski
2016-02-11 10:43                 ` Sebastian Gniazdowski
2016-02-11 12:07                   ` Sebastian Gniazdowski
2016-02-14 14:34                     ` Daniel Shahaf
2016-02-11 16:11                   ` Sebastian Gniazdowski
2016-02-12  7:34                     ` Sebastian Gniazdowski
2016-02-12 10:05                     ` Bart Schaefer
2016-02-14 14:34                     ` Daniel Shahaf
2016-02-12  9:41                   ` Bart Schaefer
2016-05-06 13:15                     ` Sebastian Gniazdowski
2016-02-14 14:34                   ` Avoiding github link bitrot " Daniel Shahaf
2016-01-20  7:47       ` "drop-in replacement" and transpose-words-match Daniel Shahaf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=160111233259.ZM6719@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).