zsh-workers
 help / color / mirror / Atom feed
* Re: additionally saving history for each directory?
       [not found]     ` <878s8yksp0.fsf@lwm.klanderman.net>
@ 2021-02-09  1:26       ` Greg Klanderman
  2021-02-09  5:31         ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Greg Klanderman @ 2021-02-09  1:26 UTC (permalink / raw)
  To: Zsh list

Figured I'd follow up with this on workers since I never got a
response on whether

save_per_directory_history () {
  setopt localoptions incappendhistory
  fc -p -a .zsh_local_history 1000
  print -sr -- ${1%%$'\n'}
}

should work in zshaddhistory_functions for saving local history.

I'm seeing the local history file getting created if it does not
exist, but nothing is ever written to it (zero size).

If I remove the 'print -sr' the local history files do not get
created, so it seems like the print should be adding the history
line..

thank you,
Greg


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

* Re: additionally saving history for each directory?
  2021-02-09  1:26       ` additionally saving history for each directory? Greg Klanderman
@ 2021-02-09  5:31         ` Bart Schaefer
  2021-02-09 19:20           ` Greg Klanderman
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2021-02-09  5:31 UTC (permalink / raw)
  To: Greg Klanderman; +Cc: Zsh list

On Mon, Feb 8, 2021 at 5:26 PM Greg Klanderman <gak@klanderman.net> wrote:
>
> Figured I'd follow up with this on workers since I never got a
> response on whether
>
> save_per_directory_history () {
>   setopt localoptions incappendhistory
>   fc -p -a .zsh_local_history 1000
>   print -sr -- ${1%%$'\n'}
> }
>
> should work in zshaddhistory_functions for saving local history.

It doesn't work because you added the -a option to the fc command.

     A hook function may call 'fc -p ...' to switch the history context
     so that the history is saved in a different file from the that in
     the global HISTFILE parameter.  This is handled specially: the
     history context is automatically restored after the processing of
     the history line is finished.

(Hm, typo there, "from the that")

The history file isn't actually written until the hooks have all
finished running, because the exit status of the hook function can
control whether the event gets written at all.  "fc -p -a" closes the
alternate history when the function returns, which is before the
decision to write has been made, so nothing gets written.

The other non-obvious (and not really documented) thing about using
"fc -p" in the zshaddhistory hook is that it has to be the last line
in the function.  It appears from some prodding that you can follow it
with another history hook function in zshaddhistory_functions, but if
anything follows it in the same function, it doesn't work.  I suspect
this is unintentional, but it seems to be the reason that the
documentation has the example in the order that it does:

          zshaddhistory() {
            print -sr -- ${1%%$'\n'}
            fc -p .zsh_local_history
          }

Further, even if you put the "fc -p" before the "print -s", the hook
magic causes the line to be added to the original internal history
rather than to a new list created by "fc -p", so "fc -AI" or similar
have nothing to write into .zsh_local_history.

The only way to do it is to use "fc -p" without "-a" and as the last
command in the function.  And, I suspect although I got a bit lost
trying all the permutations, incappendhistory has to be a global
setopt rather than a local one, again because the local scope has
already ended before the write decision is made.


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

* Re: additionally saving history for each directory?
  2021-02-09  5:31         ` Bart Schaefer
@ 2021-02-09 19:20           ` Greg Klanderman
  0 siblings, 0 replies; 3+ messages in thread
From: Greg Klanderman @ 2021-02-09 19:20 UTC (permalink / raw)
  To: zsh-workers

>>>>> On February 9, 2021 Bart Schaefer <schaefer@brasslantern.com>
>>>>> wrote:

> On Mon, Feb 8, 2021 at 5:26 PM Greg Klanderman <gak@klanderman.net>
> wrote:
>>  Figured I'd follow up with this on workers since I never got a
>> response on whether
>>  save_per_directory_history () { setopt localoptions
>> incappendhistory fc -p -a .zsh_local_history 1000 print -sr --
>> ${1%%$'\n'}
>> }
>>  should work in zshaddhistory_functions for saving local history.

> It doesn't work because you added the -a option to the fc command.

>      A hook function may call 'fc -p ...' to switch the history
>      context so that the history is saved in a different file from
>      the that in the global HISTFILE parameter.  This is handled
>      specially: the history context is automatically restored after
>      the processing of the history line is finished.

Thanks for looking into this Bart..

I think you are saying that it is just an unintended side effect of
how the history hook magic works that makes what I did not work?
But based on the docs, it seems like what I tried should work?

So does this mean it is not possible to write the history line to
more than 2 files?

> The history file isn't actually written until the hooks have all
> finished running, because the exit status of the hook function can
> control whether the event gets written at all.  "fc -p -a" closes
> the alternate history when the function returns, which is before the
> decision to write has been made, so nothing gets written.

Of course, but I'm not trying to change where the default history line
is saved.. I'm trying to temporarily switch to a different history,
write the line there, and have the default history restored.

When I do

           zshaddhistory() {
             print -sr -- ${1%%$'\n'}
             fc -p .zsh_local_history
           }

does the print not print to the default history file immediately, or
is it being delayed pending the return value of all the hooks for
whether to actually write?

> The other non-obvious (and not really documented) thing about using
> "fc -p" in the zshaddhistory hook is that it has to be the last line
> in the function.  It appears from some prodding that you can follow
> it with another history hook function in zshaddhistory_functions,
> but if anything follows it in the same function, it doesn't work.  I
> suspect this is unintentional, but it seems to be the reason that
> the documentation has the example in the order that it does:

>           zshaddhistory() {
>             print -sr -- ${1%%$'\n'}
>             fc -p .zsh_local_history
>           }

> Further, even if you put the "fc -p" before the "print -s", the hook
> magic causes the line to be added to the original internal history
> rather than to a new list created by "fc -p", so "fc -AI" or similar
> have nothing to write into .zsh_local_history.

Interesting.  If you happened to dig deep enough to have paged in
where all this magic is happening, could you give me a few pointers to
how it is working?

> The only way to do it is to use "fc -p" without "-a" and as the last
> command in the function.

> And, I suspect although I got a bit lost
> trying all the permutations, incappendhistory has to be a global
> setopt rather than a local one, again because the local scope has
> already ended before the write decision is made.

Sure, I would expect that for the regular automatic history write
which happens at the end of the zshaddhistory_functions, but not an
explicit print -s in that local scope.  Unless that print -s is also
being delayed until the end of zshaddhistory_functions..

Anyway, I have incappendhistory on globally so at least that should
not be an issue!

cheers,
Greg


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

end of thread, other threads:[~2021-02-09 19:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <24543.52705.896312.857217@lwm.klanderman.net>
     [not found] ` <CAH+w=7bVbYNqSHzWAtJ36f5s4WYn+YGJW-oWk8ppPkQtQQ70rw@mail.gmail.com>
     [not found]   ` <877dp9omok.fsf@lwm.klanderman.net>
     [not found]     ` <878s8yksp0.fsf@lwm.klanderman.net>
2021-02-09  1:26       ` additionally saving history for each directory? Greg Klanderman
2021-02-09  5:31         ` Bart Schaefer
2021-02-09 19:20           ` Greg Klanderman

zsh-workers

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-workers

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-workers zsh-workers/ http://inbox.vuxu.org/zsh-workers \
		zsh-workers@zsh.org
	public-inbox-index zsh-workers

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


code repositories for the project(s) associated with this inbox:

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

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git