zsh-workers
 help / color / mirror / code / Atom feed
* Re-reading history from ZLE
@ 2022-08-24 15:20 martin f krafft
  2022-08-24 21:16 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: martin f krafft @ 2022-08-24 15:20 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 1140 bytes --]

Folks,

If a user widget modifies the history, e.g. appends a line using 
`print -s`, then the current ZLE instance does not find out about 
it: history expansion won't have access to it, and the `up-history` 
command doesn't "find" it.

```
% _test_hist() {
     print -s "echo foo"
     zle -M "The last line of history is now 'echo foo'
     zle reset-prompt
   }
% zle -N _test_hist
% bindkey '\et' _test_hist

   # now hit esc-t
% _
The last line of history is now 'echo foo'
```

The cursor is indicated by the underscore. If I execute 
`up-history`, I get the `bindkey` command line. `!$` will contain 
"_test_hist". Curiously, `!!` will run `echo foo`. I suspect this is 
due to those being different ways to access history for historical 
reasons, but it's an inconsistency nonetheless.

Is there a reason why `reset-prompt` shouldn't re-initialise ZLE's 
understanding of history?

Thanks,

-- 
@martinkrafft | https://matrix.to/#/#madduck:madduck.net
  
"the worst part of being old is remembering when you was young."
                               -- alvin straight (the straight story)
  
spamtraps: madduck.bogus@madduck.net

[-- Attachment #2: Type: text/html, Size: 2491 bytes --]

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

* Re: Re-reading history from ZLE
  2022-08-24 15:20 Re-reading history from ZLE martin f krafft
@ 2022-08-24 21:16 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2022-08-24 21:16 UTC (permalink / raw)
  To: zsh-workers

On Wed, Aug 24, 2022 at 8:26 AM martin f krafft <madduck@madduck.net> wrote:
>
> Is there a reason why reset-prompt shouldn't re-initialise ZLE's understanding of history?

History isn't updated until ZLE exits (accept-line, etc.).  The
"current event" is not actually in the history, it's treated specially
as a pending entry; almost anything you do with a widget (including
print -s) is effectively part of that pending entry.

There is one exception:  The "fc" command updates the history
immediately.  So if you rewrite your widget as ...

_test_hist() {
    fc -R =(<<<"echo foo")
    zle -M "The last line of history is now 'echo foo'
    zle reset-prompt
  }

... then you'll get what you want, including seeing the value of %h
(history number) in your prompt increase by one.

Beware that "fc -R" may ignore lines beginning with ": " because it's
attempting to intuit whether the file is using extended history
format, and if you're adding a multi-line entry you'll want to prefix
all the newlines with backslashes, etc.


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

end of thread, other threads:[~2022-08-24 21:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-24 15:20 Re-reading history from ZLE martin f krafft
2022-08-24 21:16 ` 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).