zsh-workers
 help / color / mirror / code / Atom feed
* %! prompt code shows incorrect number
@ 2021-05-17  4:48 Mikael Magnusson
  2021-05-17  5:05 ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Mikael Magnusson @ 2021-05-17  4:48 UTC (permalink / raw)
  To: zsh-workers

With two shells, incappendhistory and sharehistory active, this can happen:

PS1='%! '
29 echo right
right
31 echo $history[29]
echo left

Needless to say, this is surprising. Can it be fixed easily? fc -l reports:
   29* echo left
   30  echo right

Perhaps we could import events as numbers that are unused instead of
overriding the number we have told the user is going to be used for
the command they run?

(I am posting this without any investigation on my part as of yet, I
may look into it later...)
(If you're testing this in zsh -f, remember to: setopt rcs;
HISTSIZE=100; SAVEHIST=100; HISTFILE=/tmp/eventstest; setopt
incappendhistory sharehistory)

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: %! prompt code shows incorrect number
  2021-05-17  4:48 %! prompt code shows incorrect number Mikael Magnusson
@ 2021-05-17  5:05 ` Bart Schaefer
  2021-05-21 18:58   ` Mikael Magnusson
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2021-05-17  5:05 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Zsh hackers list

On Sun, May 16, 2021 at 9:49 PM Mikael Magnusson <mikachu@gmail.com> wrote:
>
> Perhaps we could import events as numbers that are unused instead of
> overriding the number we have told the user is going to be used for
> the command they run?

Then the events would be out of order, which sort of ruins the
"incremental" part.

I don't think there's any simple fix here.  Possibly reprint the
prompt at (approximately) preexec time to show the correct number?


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: %! prompt code shows incorrect number
  2021-05-17  5:05 ` Bart Schaefer
@ 2021-05-21 18:58   ` Mikael Magnusson
  2021-05-21 23:55     ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Mikael Magnusson @ 2021-05-21 18:58 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

On 5/17/21, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Sun, May 16, 2021 at 9:49 PM Mikael Magnusson <mikachu@gmail.com> wrote:
>>
>> Perhaps we could import events as numbers that are unused instead of
>> overriding the number we have told the user is going to be used for
>> the command they run?
>
> Then the events would be out of order, which sort of ruins the
> "incremental" part.
>
> I don't think there's any simple fix here.  Possibly reprint the
> prompt at (approximately) preexec time to show the correct number?

I've gone with the below hack for now. What I want to be able to do is
copy the history number from the prompt and paste it back into the
terminal, and have my paste handler grab the corresponding entry from
$history and paste it into the prompt for me. With the hack I can
simply add "l" to this query and it seems to work as I want... I'm not
submitting this for inclusion, just as is.

I don't think there's any existing way to check if a history entry is
external other than parsing the * out of fc -l output?

diff --git a/Src/Modules/parameter.c b/Src/Modules/parameter.c
index ef9148d7b6..6c73daf557 100644
--- a/Src/Modules/parameter.c
+++ b/Src/Modules/parameter.c
@@ -1158,6 +1158,7 @@ getpmhistory(UNUSED(HashTable ht), const char *name)
     Histent he;
     const char *p;
     int ok = 1;
+    int local = 0;

     pm = (Param) hcalloc(sizeof(struct param));
     pm->node.nam = dupstring(name);
@@ -1169,13 +1170,18 @@ getpmhistory(UNUSED(HashTable ht), const char *name)
 	    ok = 0;
 	else {
 	    for (p = name; *p && idigit(*p); p++);
-	    if (*p)
+	    if (p[0] == 'l' && !p[1])
+		local = 1;
+	    else if (*p)
 		ok = 0;
 	}
     }
-    if (ok && (he = quietgethist(atoi(name))))
+    if (ok && (he = quietgethist(atoi(name)))) {
+	if (local && (he->node.flags & HIST_FOREIGN)) {
+	    while ((he = down_histent(he)) && (he->node.flags & HIST_FOREIGN));
+	}
 	pm->u.str = dupstring(he->node.nam);
-    else {
+    } else {
 	pm->u.str = dupstring("");
 	pm->node.flags |= (PM_UNSET|PM_SPECIAL);
     }


-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: %! prompt code shows incorrect number
  2021-05-21 18:58   ` Mikael Magnusson
@ 2021-05-21 23:55     ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2021-05-21 23:55 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Zsh hackers list

On Fri, May 21, 2021 at 11:58 AM Mikael Magnusson <mikachu@gmail.com> wrote:
>
> I've gone with the below hack for now. What I want to be able to do is
> copy the history number from the prompt and paste it back into the
> terminal

I take it this means the history number from some previous prompt.  So
the code in your patch starts at the history number requested, and if
that points to a foreign entry, scans forward until it finds a local
one.

> I don't think there's any existing way to check if a history entry is
> external other than parsing the * out of fc -l output?

It shouldn't need THAT much parsing.  Approximately (in
bracketed-paste-magic idiom):
  historynum=${${(z)"$(fc -lIL $PASTED -1)"}[1]}
  # dash ell eye L for those with sans-serif fonts
  PASTED=$history[historynum]

I don't have shared history to test with, but that should work, with
the caveat that if you paste a number that is less than
$((HISTCMD-HISTSIZE)) you'll just get the oldest local history entry
(which I think is the same with your patch).

Of course if you have thousands of lines of history and paste a really
long-ago number, that's going to use a lot of memory.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-05-21 23:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-17  4:48 %! prompt code shows incorrect number Mikael Magnusson
2021-05-17  5:05 ` Bart Schaefer
2021-05-21 18:58   ` Mikael Magnusson
2021-05-21 23:55     ` Bart Schaefer

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).