zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Greg Klanderman <gak@klanderman.net>
Cc: Zsh list <zsh-workers@zsh.org>
Subject: Re: additionally saving history for each directory?
Date: Mon, 8 Feb 2021 21:31:52 -0800	[thread overview]
Message-ID: <CAH+w=7avNOEJMfHQUBAp14nczCqg9EueBBCvd_7NWuuiOciJJA@mail.gmail.com> (raw)
In-Reply-To: <87sg662frk.fsf@lwm.klanderman.net>

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.


  reply	other threads:[~2021-02-09  5:32 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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       ` Greg Klanderman
2021-02-09  5:31         ` Bart Schaefer [this message]
2021-02-09 19:20           ` Greg Klanderman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7avNOEJMfHQUBAp14nczCqg9EueBBCvd_7NWuuiOciJJA@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=gak@klanderman.net \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).