Peter Stephenson | 14 Jul 19:38
Favicon

history-beginning-local function

This function allows you to have a local history file so that, for
example, you can pick "make" commands appropriate to a local directory
without rendering the history unusable for other operations.  It uses
the "fc -p" command that Wayne added.

The main problem is that you have to create and update the local history
file yourself since commands aren't saved to it.  This is tricky since
it happens in the main shell after zle returns.  We'd need a hook
function of some sort to be able to do the effect of fc -p around the
point the history is saved, unless there's a smarter way of doing it.

There are probably more subtle problems.

# history-beginning-local
#
#   Mostly a drop-in replacement for history-beginning-search-backward
#   or -forward, depending on the name.
#
#   Allows a local history file to be searched for certain commands to execute.
#   The main restriction is that at the moment commands are not saved
#   to the local history file, which needs to be primed by hand
#   (for example, by selecting lines from the global history file).
#
#   The style local-history-file should be set to a file that
#   will contain the local history, in the same format as $HISTFILE,
#   while local-history-commands should be set to a list of commands
#   (or patterns matching commands) that should use the local history
#   file.  Both must be set.
#
#   If the style local-history-only is not set the global history
(Continue reading)

Richard Hartmann | 14 Jul 20:48

Re: history-beginning-local function

On Mon, Jul 14, 2008 at 19:42, Peter Stephenson <pws <at> csr.com> wrote:

> This function allows you to have a local history file so that, for
> example, you can pick "make" commands appropriate to a local directory
> without rendering the history unusable for other operations.  It uses
> the "fc -p" command that Wayne added.

Nice & useful! This is also a first step in the direction of the
$MAGIC_SESSION_COOKIE functionality. I am CCing Robert Knight as
a gentle poking about this :)

Thanks Peter!
Richard

PS: Yes, I did read the part about manual seeding of the file. I still like it
a lot!

Richard Hartmann | 15 Jul 11:46

Re: history-beginning-local function

On Mon, Jul 14, 2008 at 20:48, Richard Hartmann
<richih.mailinglist <at> gmail.com> wrote:

> PS: Yes, I did read the part about manual seeding of the file. I still like it
> a lot!

The part I apparently did not read was 'local directory'. Sorry, for the
noise, I misinterpreted the purpose of this function.

Richard

Peter Stephenson | 15 Jul 12:10
Favicon

Re: history-beginning-local function

"Richard Hartmann" wrote:
> On Mon, Jul 14, 2008 at 20:48, Richard Hartmann
> <richih.mailinglist <at> gmail.com> wrote:
> 
> 
> > PS: Yes, I did read the part about manual seeding of the file. I still like
>  it
> > a lot!
> 
> The part I apparently did not read was 'local directory'. Sorry, for the
> noise, I misinterpreted the purpose of this function.

Actually, it wouldn't be hard to add a session cookie to the context
giving you separate history files for separate sessions---the context
mechanism is flexible enough that this is really quite easy.  I'm
already adding the directory to the end of the context.  The history
file variable could be a list, so you you could specify

  zstyle ":zhist:$SOME_SESSION_COOKIE:*" local-history-file \
    .zsh_local_history ~/.history_$SOME_SESSION_COOKIE

or whatever.  (I've decided something other than :zle: would be more
appropriate.)

There's no reason why the resulting mechanism shouldn't have a "local"
history in a more general sense than I originally intended.

--

-- 
Peter Stephenson <pws <at> csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
(Continue reading)

Bart Schaefer | 14 Jul 22:44

Re: history-beginning-local function

On Mon, Jul 14, 2008 at 10:42 AM, Peter Stephenson <pws <at> csr.com> wrote:
> This function allows you to have a local history file [...]
>
> The main problem is that you have to create and update the local history
> file yourself since commands aren't saved to it.

One possibility would be to put something in chpwd that remembers the
history number upon entering a directory and, upon leaving it, appends
the higher-numbered history entries to the local file.

Another approach might be to have chpwd change the global HISTFILE.
Then use inc_append_history so that commands in each directory are
appended as you go.  That leaves just the problem of trapping shell
exit and writing the full history to the original HISTFILE (assuming
that's still desirable).

Peter Stephenson | 14 Jul 23:27
Favicon

PATCH: zshaddhistoryhook (was Re: history-beginning-local function)

On Mon, 14 Jul 2008 18:42:20 +0100
Peter Stephenson <pws <at> csr.com> wrote:
> The main problem is that you have to create and update the local history
> file yourself since commands aren't saved to it.  This is tricky since
> it happens in the main shell after zle returns.  We'd need a hook
> function of some sort to be able to do the effect of fc -p around the
> point the history is saved, unless there's a smarter way of doing it.

(Switched to zsh-workers.)

I think I've found quite a neat way of doing this, which kills another
bird (that's been perching on a nearby rooftop for a long time
occasionally looking hungrily into the fishpond) with the same stone and
very little new, er, fish.  However, although this does seem to work,
I'm not 100% sure this doesn't have odd effects in the history.  Wayne
may have some notion.

This adds the hook zshaddhistory at the point an interactive history
line is saved.  It has two bits of magic.

Firstly, you can return non-zero from one of the hook functions (i.e the
zshaddhistory function itself or one of the functions listed
in zshaddhistory_functions) to ignore the line in exactly the same way
as the existing mechanism for ignoring history entries.  This kills the
other bird.

Secondly, if you call "fc -p" within the function it switches the
history context for the current line handling, and the calling function
will obligingly pop the context back the way it was before.  So we can
nicely reuse this mechanism to do all the dirty work for us.  This sets
(Continue reading)

Peter Stephenson | 17 Jul 13:32
Favicon

Re: PATCH: zshaddhistoryhook (was Re: history-beginning-local function)

On Mon, 14 Jul 2008 22:27:00 +0100
Peter Stephenson <p.w.stephenson <at> ntlworld.com> wrote:
> This adds the hook zshaddhistory at the point an interactive history
> line is saved.  It has two bits of magic.

This has been neatly doing what I need it to for a couple of days, so I've
committed it.  I'll eventually get around to adding some tidied up
functions to use it.  They might deserve a separate subdirectory of
Functions.

--

-- 
Peter Stephenson <pws <at> csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


Gmane