* [PATCH] enable number argument for transpose-words
@ 2016-08-01 10:32 Han Pingtian
2016-08-12 2:54 ` Han Pingtian
0 siblings, 1 reply; 9+ messages in thread
From: Han Pingtian @ 2016-08-01 10:32 UTC (permalink / raw)
To: zsh-workers
Looks like number argument doesn't work for transpose-words widget: if
the number is even, there is no words transposed; if the number is odd,
the behavior is the same of no number argument given.
With this patch, current word will be transposed with the preious nth
word if number argument n given.
Signed-off-by: Han Pingtian <hanpt@linux.vnet.ibm.com>
---
Src/Zle/zle_word.c | 90 +++++++++++++++++++++++++++++++-----------------------
1 file changed, 51 insertions(+), 39 deletions(-)
diff --git a/Src/Zle/zle_word.c b/Src/Zle/zle_word.c
index 3c1f26c..2a15171 100644
--- a/Src/Zle/zle_word.c
+++ b/Src/Zle/zle_word.c
@@ -678,46 +678,54 @@ killword(char **args)
int
transposewords(UNUSED(char **args))
{
- int p1, p2, p3, p4, len, x = zlecs, pos;
+ int p1, p2, p3, p4, pt, len, x = zlecs, pos;
ZLE_STRING_T temp, pp;
int n = zmult;
int neg = n < 0, ocs = zlecs;
if (neg)
n = -n;
- while (n--) {
- while (x != zlell && zleline[x] != ZWC('\n') && !ZC_iword(zleline[x]))
- INCPOS(x);
- if (x == zlell || zleline[x] == ZWC('\n')) {
- x = zlecs;
- while (x) {
- if (ZC_iword(zleline[x]))
- break;
- pos = x;
- DECPOS(pos);
- if (zleline[pos] == ZWC('\n'))
- break;
- x = pos;
- }
- if (!x)
- return 1;
+
+ while (x != zlell && zleline[x] != ZWC('\n') && !ZC_iword(zleline[x]))
+ INCPOS(x);
+
+ if (x == zlell || zleline[x] == ZWC('\n')) {
+ x = zlecs;
+ while (x) {
+ if (ZC_iword(zleline[x]))
+ break;
pos = x;
DECPOS(pos);
if (zleline[pos] == ZWC('\n'))
- return 1;
- }
- for (p4 = x; p4 != zlell && ZC_iword(zleline[p4]); INCPOS(p4))
- ;
- for (p3 = p4; p3; ) {
- pos = p3;
- DECPOS(pos);
- if (!ZC_iword(zleline[pos]))
break;
- p3 = pos;
+ x = pos;
}
- if (!p3)
+ if (!x)
return 1;
- for (p2 = p3; p2; ) {
+ pos = x;
+ DECPOS(pos);
+ if (zleline[pos] == ZWC('\n'))
+ return 1;
+ }
+
+ for (p4 = x; p4 != zlell && ZC_iword(zleline[p4]); INCPOS(p4))
+ ;
+
+ for (p3 = p4; p3; ) {
+ pos = p3;
+ DECPOS(pos);
+ if (!ZC_iword(zleline[pos]))
+ break;
+ p3 = pos;
+ }
+
+ if (!p3)
+ return 1;
+
+ pt = p3;
+
+ while (n--) {
+ for (p2 = pt; p2; ) {
pos = p2;
DECPOS(pos);
if (ZC_iword(zleline[pos]))
@@ -733,20 +741,24 @@ transposewords(UNUSED(char **args))
break;
p1 = pos;
}
- pp = temp = (ZLE_STRING_T)zhalloc((p4 - p1)*ZLE_CHAR_SIZE);
- len = p4 - p3;
- ZS_memcpy(pp, zleline + p3, len);
- pp += len;
- len = p3 - p2;
- ZS_memcpy(pp, zleline + p2, len);
- pp += len;
- ZS_memcpy(pp, zleline + p1, p2 - p1);
+ pt = p1;
+ }
- ZS_memcpy(zleline + p1, temp, p4 - p1);
+ pp = temp = (ZLE_STRING_T)zhalloc((p4 - p1)*ZLE_CHAR_SIZE);
+ len = p4 - p3;
+ ZS_memcpy(pp, zleline + p3, len);
+ pp += len;
+ len = p3 - p2;
+ ZS_memcpy(pp, zleline + p2, len);
+ pp += len;
+ ZS_memcpy(pp, zleline + p1, p2 - p1);
+
+ ZS_memcpy(zleline + p1, temp, p4 - p1);
- zlecs = p4;
- }
if (neg)
zlecs = ocs;
+ else
+ zlecs = p4;
+
return 0;
}
--
2.5.5
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] enable number argument for transpose-words
2016-08-01 10:32 [PATCH] enable number argument for transpose-words Han Pingtian
@ 2016-08-12 2:54 ` Han Pingtian
2016-08-12 9:29 ` Peter Stephenson
0 siblings, 1 reply; 9+ messages in thread
From: Han Pingtian @ 2016-08-12 2:54 UTC (permalink / raw)
To: zsh-workers
On Mon, Aug 01, 2016 at 06:32:12PM +0800, Han Pingtian wrote:
> Looks like number argument doesn't work for transpose-words widget: if
> the number is even, there is no words transposed; if the number is odd,
> the behavior is the same of no number argument given.
>
> With this patch, current word will be transposed with the preious nth
> word if number argument n given.
>
> Signed-off-by: Han Pingtian <hanpt@linux.vnet.ibm.com>
> ---
> Src/Zle/zle_word.c | 90 +++++++++++++++++++++++++++++++-----------------------
> 1 file changed, 51 insertions(+), 39 deletions(-)
>
> diff --git a/Src/Zle/zle_word.c b/Src/Zle/zle_word.c
> index 3c1f26c..2a15171 100644
> --- a/Src/Zle/zle_word.c
> +++ b/Src/Zle/zle_word.c
> @@ -678,46 +678,54 @@ killword(char **args)
> int
> transposewords(UNUSED(char **args))
> {
> - int p1, p2, p3, p4, len, x = zlecs, pos;
> + int p1, p2, p3, p4, pt, len, x = zlecs, pos;
> ZLE_STRING_T temp, pp;
> int n = zmult;
> int neg = n < 0, ocs = zlecs;
>
> if (neg)
> n = -n;
> - while (n--) {
> - while (x != zlell && zleline[x] != ZWC('\n') && !ZC_iword(zleline[x]))
> - INCPOS(x);
> - if (x == zlell || zleline[x] == ZWC('\n')) {
> - x = zlecs;
> - while (x) {
> - if (ZC_iword(zleline[x]))
> - break;
> - pos = x;
> - DECPOS(pos);
> - if (zleline[pos] == ZWC('\n'))
> - break;
> - x = pos;
> - }
> - if (!x)
> - return 1;
> +
> + while (x != zlell && zleline[x] != ZWC('\n') && !ZC_iword(zleline[x]))
> + INCPOS(x);
> +
> + if (x == zlell || zleline[x] == ZWC('\n')) {
> + x = zlecs;
> + while (x) {
> + if (ZC_iword(zleline[x]))
> + break;
> pos = x;
> DECPOS(pos);
> if (zleline[pos] == ZWC('\n'))
> - return 1;
> - }
> - for (p4 = x; p4 != zlell && ZC_iword(zleline[p4]); INCPOS(p4))
> - ;
> - for (p3 = p4; p3; ) {
> - pos = p3;
> - DECPOS(pos);
> - if (!ZC_iword(zleline[pos]))
> break;
> - p3 = pos;
> + x = pos;
> }
> - if (!p3)
> + if (!x)
> return 1;
> - for (p2 = p3; p2; ) {
> + pos = x;
> + DECPOS(pos);
> + if (zleline[pos] == ZWC('\n'))
> + return 1;
> + }
> +
> + for (p4 = x; p4 != zlell && ZC_iword(zleline[p4]); INCPOS(p4))
> + ;
> +
> + for (p3 = p4; p3; ) {
> + pos = p3;
> + DECPOS(pos);
> + if (!ZC_iword(zleline[pos]))
> + break;
> + p3 = pos;
> + }
> +
> + if (!p3)
> + return 1;
> +
> + pt = p3;
> +
> + while (n--) {
> + for (p2 = pt; p2; ) {
> pos = p2;
> DECPOS(pos);
> if (ZC_iword(zleline[pos]))
> @@ -733,20 +741,24 @@ transposewords(UNUSED(char **args))
> break;
> p1 = pos;
> }
> - pp = temp = (ZLE_STRING_T)zhalloc((p4 - p1)*ZLE_CHAR_SIZE);
> - len = p4 - p3;
> - ZS_memcpy(pp, zleline + p3, len);
> - pp += len;
> - len = p3 - p2;
> - ZS_memcpy(pp, zleline + p2, len);
> - pp += len;
> - ZS_memcpy(pp, zleline + p1, p2 - p1);
> + pt = p1;
> + }
>
> - ZS_memcpy(zleline + p1, temp, p4 - p1);
> + pp = temp = (ZLE_STRING_T)zhalloc((p4 - p1)*ZLE_CHAR_SIZE);
> + len = p4 - p3;
> + ZS_memcpy(pp, zleline + p3, len);
> + pp += len;
> + len = p3 - p2;
> + ZS_memcpy(pp, zleline + p2, len);
> + pp += len;
> + ZS_memcpy(pp, zleline + p1, p2 - p1);
> +
> + ZS_memcpy(zleline + p1, temp, p4 - p1);
>
> - zlecs = p4;
> - }
> if (neg)
> zlecs = ocs;
> + else
> + zlecs = p4;
> +
> return 0;
> }
> --
> 2.5.5
Any suggestions to this one, please? Thanks in advance.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] enable number argument for transpose-words
2016-08-12 2:54 ` Han Pingtian
@ 2016-08-12 9:29 ` Peter Stephenson
2016-08-12 10:25 ` Kamil Dudka
2016-08-15 2:22 ` Han Pingtian
0 siblings, 2 replies; 9+ messages in thread
From: Peter Stephenson @ 2016-08-12 9:29 UTC (permalink / raw)
To: zsh-workers
On Fri, 12 Aug 2016 10:54:02 +0800
Han Pingtian <hanpt@linux.vnet.ibm.com> wrote:
> Any suggestions to this one, please? Thanks in advance.
Thanks, I've committed that. This describes what it does.
pws
diff --git a/Doc/Zsh/zle.yo b/Doc/Zsh/zle.yo
index eaca1b3..aa219dc 100644
--- a/Doc/Zsh/zle.yo
+++ b/Doc/Zsh/zle.yo
@@ -1948,6 +1948,12 @@ to the left.
tindex(transpose-words)
item(tt(transpose-words) (tt(ESC-T ESC-t)) (unbound) (unbound))(
Exchange the current word with the one before it.
+
+With a positive numeric argument em(N), the word before the cursor is
+transposed with the following em(N) words.
+
+With a negative numeric argument em(-N), the word after the cursor
+is transposed with the preceding em(N) words.
)
tindex(vi-unindent)
item(tt(vi-unindent) (unbound) (tt(<)) (unbound))(
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] enable number argument for transpose-words
2016-08-12 9:29 ` Peter Stephenson
@ 2016-08-12 10:25 ` Kamil Dudka
2016-08-12 10:41 ` Peter Stephenson
2016-08-15 2:22 ` Han Pingtian
1 sibling, 1 reply; 9+ messages in thread
From: Kamil Dudka @ 2016-08-12 10:25 UTC (permalink / raw)
To: zsh-workers; +Cc: Peter Stephenson
On Friday, August 12, 2016 10:29:18 Peter Stephenson wrote:
> On Fri, 12 Aug 2016 10:54:02 +0800
>
> Han Pingtian <hanpt@linux.vnet.ibm.com> wrote:
> > Any suggestions to this one, please? Thanks in advance.
>
> Thanks, I've committed that. This describes what it does.
>
> pws
>
> diff --git a/Doc/Zsh/zle.yo b/Doc/Zsh/zle.yo
> index eaca1b3..aa219dc 100644
> --- a/Doc/Zsh/zle.yo
> +++ b/Doc/Zsh/zle.yo
> @@ -1948,6 +1948,12 @@ to the left.
> tindex(transpose-words)
> item(tt(transpose-words) (tt(ESC-T ESC-t)) (unbound) (unbound))(
> Exchange the current word with the one before it.
> +
> +With a positive numeric argument em(N), the word before the cursor is
> +transposed with the following em(N) words.
> +
> +With a negative numeric argument em(-N), the word after the cursor
> +is transposed with the preceding em(N) words.
> )
> tindex(vi-unindent)
> item(tt(vi-unindent) (unbound) (tt(<)) (unbound))(
Is the body of 'while (n--)' loop guaranteed to be executed at least once?
Otherwise p1 and p2 may be used uninitialized by code following after the
loop. I would suggest to initialize explicitly or add assert for the value
of 'n'. The following warnings started to be reported after this commit:
Error: CLANG_WARNING:
Src/Zle/zle_word.c:747:43: warning: The right operand of '-' is a garbage value
# pp = temp = (ZLE_STRING_T)zhalloc((p4 - p1)*ZLE_CHAR_SIZE);
# ^ ~~
# 745| }
# 746|
# 747|-> pp = temp = (ZLE_STRING_T)zhalloc((p4 - p1)*ZLE_CHAR_SIZE);
# 748| len = p4 - p3;
# 749| ZS_memcpy(pp, zleline + p3, len);
Error: COMPILER_WARNING:
Src/Zle/zle_word.c:747:43: warning: ‘p1’ may be used uninitialized in this function [-Wuninitialized]
# 745| }
# 746|
# 747|-> pp = temp = (ZLE_STRING_T)zhalloc((p4 - p1)*ZLE_CHAR_SIZE);
# 748| len = p4 - p3;
# 749| ZS_memcpy(pp, zleline + p3, len);
Error: COMPILER_WARNING:
Src/Zle/zle_word.c: scope_hint: In function ‘transposewords’
Src/Zle/zle_word.c:751:9: warning: ‘p2’ may be used uninitialized in this function [-Wuninitialized]
# 749| ZS_memcpy(pp, zleline + p3, len);
# 750| pp += len;
# 751|-> len = p3 - p2;
# 752| ZS_memcpy(pp, zleline + p2, len);
# 753| pp += len;
Full build log at: https://travis-ci.org/kdudka/csbuild-zsh/builds/151747546
Kamil
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] enable number argument for transpose-words
2016-08-12 10:25 ` Kamil Dudka
@ 2016-08-12 10:41 ` Peter Stephenson
2016-08-12 13:10 ` Kamil Dudka
0 siblings, 1 reply; 9+ messages in thread
From: Peter Stephenson @ 2016-08-12 10:41 UTC (permalink / raw)
To: zsh-workers
On Fri, 12 Aug 2016 12:25:55 +0200
Kamil Dudka <kdudka@redhat.com> wrote:
> Is the body of 'while (n--)' loop guaranteed to be executed at least once?
>
> Otherwise p1 and p2 may be used uninitialized by code following after the
> loop.
It's probably reasonable to do the following...
pws
diff --git a/Src/Zle/zle_word.c b/Src/Zle/zle_word.c
index 2a15171..e4a878e 100644
--- a/Src/Zle/zle_word.c
+++ b/Src/Zle/zle_word.c
@@ -722,7 +722,7 @@ transposewords(UNUSED(char **args))
if (!p3)
return 1;
- pt = p3;
+ p1 = p2 = pt = p3;
while (n--) {
for (p2 = pt; p2; ) {
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] enable number argument for transpose-words
2016-08-12 10:41 ` Peter Stephenson
@ 2016-08-12 13:10 ` Kamil Dudka
0 siblings, 0 replies; 9+ messages in thread
From: Kamil Dudka @ 2016-08-12 13:10 UTC (permalink / raw)
To: Peter Stephenson; +Cc: zsh-workers
On Friday, August 12, 2016 11:41:42 Peter Stephenson wrote:
> On Fri, 12 Aug 2016 12:25:55 +0200
>
> Kamil Dudka <kdudka@redhat.com> wrote:
> > Is the body of 'while (n--)' loop guaranteed to be executed at least once?
> >
> > Otherwise p1 and p2 may be used uninitialized by code following after the
> > loop.
>
> It's probably reasonable to do the following...
>
> pws
>
> diff --git a/Src/Zle/zle_word.c b/Src/Zle/zle_word.c
> index 2a15171..e4a878e 100644
> --- a/Src/Zle/zle_word.c
> +++ b/Src/Zle/zle_word.c
> @@ -722,7 +722,7 @@ transposewords(UNUSED(char **args))
> if (!p3)
> return 1;
>
> - pt = p3;
> + p1 = p2 = pt = p3;
>
> while (n--) {
> for (p2 = pt; p2; ) {
Appears fixed. Thanks!
Kamil
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] enable number argument for transpose-words
2016-08-12 9:29 ` Peter Stephenson
2016-08-12 10:25 ` Kamil Dudka
@ 2016-08-15 2:22 ` Han Pingtian
2016-08-15 8:39 ` Peter Stephenson
1 sibling, 1 reply; 9+ messages in thread
From: Han Pingtian @ 2016-08-15 2:22 UTC (permalink / raw)
To: zsh-workers
On Fri, Aug 12, 2016 at 10:29:18AM +0100, Peter Stephenson wrote:
> On Fri, 12 Aug 2016 10:54:02 +0800
> Han Pingtian <hanpt@linux.vnet.ibm.com> wrote:
> > Any suggestions to this one, please? Thanks in advance.
>
> Thanks, I've committed that. This describes what it does.
>
> pws
>
> diff --git a/Doc/Zsh/zle.yo b/Doc/Zsh/zle.yo
> index eaca1b3..aa219dc 100644
> --- a/Doc/Zsh/zle.yo
> +++ b/Doc/Zsh/zle.yo
> @@ -1948,6 +1948,12 @@ to the left.
> tindex(transpose-words)
> item(tt(transpose-words) (tt(ESC-T ESC-t)) (unbound) (unbound))(
> Exchange the current word with the one before it.
> +
> +With a positive numeric argument em(N), the word before the cursor is
> +transposed with the following em(N) words.
> +
> +With a negative numeric argument em(-N), the word after the cursor
> +is transposed with the preceding em(N) words.
> )
> tindex(vi-unindent)
> item(tt(vi-unindent) (unbound) (tt(<)) (unbound))(
Thanks. But both positive and negative numeric argument would transpose
current word with preceding N/-N word. The negative argument only make
cursor doesn't change its position after the transpostion.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] enable number argument for transpose-words
2016-08-15 2:22 ` Han Pingtian
@ 2016-08-15 8:39 ` Peter Stephenson
2016-08-15 9:01 ` Peter Stephenson
0 siblings, 1 reply; 9+ messages in thread
From: Peter Stephenson @ 2016-08-15 8:39 UTC (permalink / raw)
To: zsh-workers
On Mon, 15 Aug 2016 10:22:50 +0800
Han Pingtian <hanpt@linux.vnet.ibm.com> wrote:
> > diff --git a/Doc/Zsh/zle.yo b/Doc/Zsh/zle.yo
> > index eaca1b3..aa219dc 100644
> > --- a/Doc/Zsh/zle.yo
> > +++ b/Doc/Zsh/zle.yo
> > @@ -1948,6 +1948,12 @@ to the left.
> > tindex(transpose-words)
> > item(tt(transpose-words) (tt(ESC-T ESC-t)) (unbound) (unbound))(
> > Exchange the current word with the one before it.
> > +
> > +With a positive numeric argument em(N), the word before the cursor is
> > +transposed with the following em(N) words.
> > +
> > +With a negative numeric argument em(-N), the word after the cursor
> > +is transposed with the preceding em(N) words.
> > )
> > tindex(vi-unindent)
> > item(tt(vi-unindent) (unbound) (tt(<)) (unbound))(
> Thanks. But both positive and negative numeric argument would transpose
> current word with preceding N/-N word. The negative argument only make
> cursor doesn't change its position after the transpostion.
Ah, I had the key bound as
zle -N transpose-words-between transpose-words-about-point-match
bindkey '\et' transpose-words-between
with the following function (there are lots of different possible
behaviours in this case).
I'll find a form of words for the new standard behaviour.
pws
# Transpose words, matching the words using match-words-by-style, q.v.
# The group of word characters preceeding the cursor (not necessarily
# immediately) are transposed with the group of word characters following
# the cursor (again, not necessarily immediately).
#
# Note the style skip-chars, used in the context of the current widget.
# This gives a number of character starting from the cursor position
# which are never considered part of a word and hence are always left
# alone. The default is 0 and typically the only useful alternative
# is one. This would have the effect that `fooXbar' with the cursor
# on X would be turned into `barXfoo' with the cursor still on the X,
# regardless of what the character X is.
autoload match-words-by-style
local curcontext=":zle:$WIDGET" skip
local -a matched_words
integer count=${NUMERIC:-1} neg
(( count < 0 )) && (( count = -count, neg = 1 ))
while (( count-- > 0 )); do
match-words-by-style
[[ -z "$matched_words[2]$matched_words[5]" ]] && return 1
if (( !${NUMERIC:-0} )); then
# No prefix, don't advance cursor.
LBUFFER="$matched_words[1]$matched_words[5]$matched_words[3]"
RBUFFER="$matched_words[4]$matched_words[2]${(j..)matched_words[6,7]}"
elif (( neg )); then
LBUFFER="$matched_words[1]"
RBUFFER="$matched_words[5]${(j..)matched_words[3,4]}\
$matched_words[2]${(j..)matched_words[6,7]}"
else
LBUFFER="$matched_words[1]$matched_words[5]${(j..)matched_words[3,4]}\
$matched_words[2]"
RBUFFER="${(j..)matched_words[6,7]}"
fi
done
return 0
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] enable number argument for transpose-words
2016-08-15 8:39 ` Peter Stephenson
@ 2016-08-15 9:01 ` Peter Stephenson
0 siblings, 0 replies; 9+ messages in thread
From: Peter Stephenson @ 2016-08-15 9:01 UTC (permalink / raw)
To: zsh-workers
diff --git a/Doc/Zsh/zle.yo b/Doc/Zsh/zle.yo
index fad5e15..0102cb0 100644
--- a/Doc/Zsh/zle.yo
+++ b/Doc/Zsh/zle.yo
@@ -1949,11 +1949,14 @@ tindex(transpose-words)
item(tt(transpose-words) (tt(ESC-T ESC-t)) (unbound) (unbound))(
Exchange the current word with the one before it.
-With a positive numeric argument em(N), the word before the cursor is
-transposed with the following em(N) words.
-
-With a negative numeric argument em(-N), the word after the cursor
-is transposed with the preceding em(N) words.
+With a positive numeric argument em(N), the word around the cursor, or
+following it if the cursor is between words, is transposed with the
+preceding em(N) words. The cursor is put at the end of the resulting
+group of words.
+
+With a negative numeric argument em(-N), the effect is the same as using
+a positive argument em(N) except that the original cursor position is
+retained, regardless of how the words are rearranged.
)
tindex(vi-unindent)
item(tt(vi-unindent) (unbound) (tt(<)) (unbound))(
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2016-08-15 9:01 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-01 10:32 [PATCH] enable number argument for transpose-words Han Pingtian
2016-08-12 2:54 ` Han Pingtian
2016-08-12 9:29 ` Peter Stephenson
2016-08-12 10:25 ` Kamil Dudka
2016-08-12 10:41 ` Peter Stephenson
2016-08-12 13:10 ` Kamil Dudka
2016-08-15 2:22 ` Han Pingtian
2016-08-15 8:39 ` Peter Stephenson
2016-08-15 9:01 ` Peter Stephenson
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).