PATCH: fix complist interactive mode overwriting buffer

Mikael Magnusson <[email protected]>
Newsgroups gmane.comp.shells.zsh.devel
Message-ID <[email protected]>
This has been discussed a few times starting with 49242, and continued
at 50412, 50422, 52312 and 52657.

reproduce recipe:
% zmodload zsh/complist
% bindkey '^I' menu-select
% MENUMODE=interactive
% touch test{1,2}
% : ; foobar
    ^ 1. Type the above line in its entirety.
      2. Place the cursor before the ;
      3. Press Tab.
      4. Press Enter.
% : test1 bar
    ^ Completion is written over existing buffer contents.

The main confusing thing to note is that do_single looks at minfo.cur,
whether you pass it or not, because minfo is a global variable. When it
is not NULL, it uses minfo.pos and minfo.len which are stale in these
particular situations. When it is instead NULL, it recalculates them
correctly for us, which is what we want.

The two different hunks are separate code paths for accepting the
completion or navigating to another match.
---
 Src/Zle/complist.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/Src/Zle/complist.c b/Src/Zle/complist.c
index 6a46497954..f2462c259e 100644
--- a/Src/Zle/complist.c
+++ b/Src/Zle/complist.c
@@ -2640,6 +2640,7 @@ domenuselect(Hookdef dummy, Chdata dat)
     	}
 	do_last_key = 0;
 
+	int was_inter = (mode == MM_INTER);
 	if (!cmd || cmd == Th(z_sendbreak)) {
 	    zbeep();
             molbeg = -1;
@@ -3407,6 +3408,8 @@ domenuselect(Hookdef dummy, Chdata dat)
 		acc = 1;
 	    break;
 	}
+	if (was_inter)
+	    minfo.cur = NULL;
 	do_single(**p);
 	mselect = (**p)->gnum;
     }
@@ -3423,7 +3426,12 @@ domenuselect(Hookdef dummy, Chdata dat)
         clearlist = listshown = 1;
     if (acc && validlist && minfo.cur) {
 	menucmp = lastambig = hasoldlist = 0;
-	do_single(*(minfo.cur));
+	if (mode == MM_INTER) {
+	    Cmatch *cur = minfo.cur;
+	    minfo.cur = NULL;
+	    do_single(*cur);
+	} else
+	    do_single(*(minfo.cur));
     }
     if (wasnext || broken) {
 	menucmp = 1;
-- 
2.38.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.