zsh-users
 help / color / mirror / code / Atom feed
* How to (properly) remove the last entry from history with command_not_found_handler
@ 2014-07-20  9:04 Jochen Keil
  2014-07-20 17:45 ` Peter Stephenson
  2014-07-20 18:13 ` Bart Schaefer
  0 siblings, 2 replies; 5+ messages in thread
From: Jochen Keil @ 2014-07-20  9:04 UTC (permalink / raw)
  To: zsh-users

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

Dear list,

I'm trying to remove the last entry from my $HISTFILE when the command
was not found. My current attempt involves the
command_not_found_handler() hook:

function command_not_found_handler()
{
    sed -i '$d' $HISTFILE
    return 127
}

However, I'm not sure if it's ok to manipulate the history file directly
with sed, or if I'm risking corruption.

Thank you very much and best wishes,

  Jochen



-- 
To see a world in a grain of sand,
And a heaven in a wild flower,
Hold infinity in the palm of your hand,
And eternity in an hour.

- William Blake, Auguries of Innocence


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 181 bytes --]

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

* Re: How to (properly) remove the last entry from history with command_not_found_handler
  2014-07-20  9:04 How to (properly) remove the last entry from history with command_not_found_handler Jochen Keil
@ 2014-07-20 17:45 ` Peter Stephenson
  2014-07-20 18:13 ` Bart Schaefer
  1 sibling, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2014-07-20 17:45 UTC (permalink / raw)
  To: zsh-users

On Sun, 20 Jul 2014 11:04:12 +0200
Jochen Keil <jrk@sternenrei.ch> wrote:
> I'm trying to remove the last entry from my $HISTFILE when the command
> was not found. My current attempt involves the
> command_not_found_handler() hook:
> 
> function command_not_found_handler()
> {
>     sed -i '$d' $HISTFILE
>     return 127
> }
> 
> However, I'm not sure if it's ok to manipulate the history file directly
> with sed, or if I'm risking corruption.

You're can hit problems if you have multiple shells running at once.
You'd need to lock the file --- it is possible to do that in a way
that's compatibile with how the shell works, either creating a symlink
with the name of the history file with .LOCK on the end or, if you have
HISTFCNTLLOCK set, using flock, which you can get access to with
"zsystem flock".

Another approach is to have a zshaddhistory hook --- either a function
or zshaddhistory or a function named in the array
zshaddhistory_functions --- decide whether the command isn't going to be
found.  But that's quite a lot of work, too: you need to split off the
first word, look it up as an alias, possibly recursively, then decide if
the result is a reserved word, an external command, a function, an
assignment... all possible but takes some work.

I couldn't think of a way to make zshaddhistory and
command_not_found_handler together.

pws


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

* Re: How to (properly) remove the last entry from history with command_not_found_handler
  2014-07-20  9:04 How to (properly) remove the last entry from history with command_not_found_handler Jochen Keil
  2014-07-20 17:45 ` Peter Stephenson
@ 2014-07-20 18:13 ` Bart Schaefer
  2014-07-20 20:48   ` Jochen Keil
  1 sibling, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2014-07-20 18:13 UTC (permalink / raw)
  To: zsh-users

On Jul 20, 11:04am, Jochen Keil wrote:
}
} function command_not_found_handler()
} {
}     sed -i '$d' $HISTFILE
}     return 127
} }
} 
} However, I'm not sure if it's ok to manipulate the history file directly
} with sed, or if I'm risking corruption.

This is likely to be a problem if the failing command spans more than
one line (e.g., a "for" loop with command-not-found in the middle of
it) or if you are using SHARE_HISTORY where there's a race condition
among shells reading the history and shells (re)writing it.

There really isn't any way to "properly" remove an entry once it has been
written to the history file.  You might try some variation of this:

  zshaddhistory() {  whence ${${(z)1}[1]} >/dev/null || return 2 }

There are still problems with that for multi-line commands but the right
way to achieve your desired behavior is to avoid writing out the commands
you don't want, not to write them and then delete them again.


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

* Re: Re: How to (properly) remove the last entry from history with command_not_found_handler
  2014-07-20 18:13 ` Bart Schaefer
@ 2014-07-20 20:48   ` Jochen Keil
  2014-07-21 17:46     ` ZyX
  0 siblings, 1 reply; 5+ messages in thread
From: Jochen Keil @ 2014-07-20 20:48 UTC (permalink / raw)
  To: Bart Schaefer, zsh-users

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

Hello Bart,

On 20.07.2014 20:13, Bart Schaefer wrote:
> On Jul 20, 11:04am, Jochen Keil wrote:
> }
> } function command_not_found_handler()
> } {
> }     sed -i '$d' $HISTFILE
> }     return 127
> } }
> }
> } However, I'm not sure if it's ok to manipulate the history file directly
> } with sed, or if I'm risking corruption.
>
> This is likely to be a problem if the failing command spans more than
> one line (e.g., a "for" loop with command-not-found in the middle of
> it) or if you are using SHARE_HISTORY where there's a race condition
> among shells reading the history and shells (re)writing it.
>
> There really isn't any way to "properly" remove an entry once it has been
> written to the history file.  You might try some variation of this:
>
>   zshaddhistory() {  whence ${${(z)1}[1]} >/dev/null || return 2 }
>
> There are still problems with that for multi-line commands but the right
> way to achieve your desired behavior is to avoid writing out the commands
> you don't want, not to write them and then delete them again.

It was my first thought too, to avoid adding the command at all.
However, I misunderstood/misinterpreted the explanation of the
zshaddhistory hook in the manual. Anyhow, your version works very well,
thank you!

Nevertheless, I start to see why this is probably a not so good idea (or
at least pretty hard to implement). For example:

  $ echo "test"; echox "test"

This is similar to the multi-line example. One basically needs to split
up the command at semicolons and newlines. Then one needs to decide
which of the subcommands is correct and which one not. Finally one needs
to decide if an altered version of the command should go to the history
file or none at all.

I think the possible gains are not worth the effort. I'll stick to your
solution as it covers most of the cases which I care most about: When
I've accidentally mistyped a command or entered other garbage.

Thank you very much again and best wishes,

  Jochen


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 181 bytes --]

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

* Re: How to (properly) remove the last entry from history with command_not_found_handler
  2014-07-20 20:48   ` Jochen Keil
@ 2014-07-21 17:46     ` ZyX
  0 siblings, 0 replies; 5+ messages in thread
From: ZyX @ 2014-07-21 17:46 UTC (permalink / raw)
  To: Jochen Keil, Bart Schaefer, zsh-users



21.07.2014, 00:52, "Jochen Keil" <jrk@sternenrei.ch>:
> Hello Bart,
>
> On 20.07.2014 20:13, Bart Schaefer wrote:
>>  On Jul 20, 11:04am, Jochen Keil wrote:
>>  }
>>  } function command_not_found_handler()
>>  } {
>>  }     sed -i '$d' $HISTFILE
>>  }     return 127
>>  } }
>>  }
>>  } However, I'm not sure if it's ok to manipulate the history file directly
>>  } with sed, or if I'm risking corruption.
>>
>>  This is likely to be a problem if the failing command spans more than
>>  one line (e.g., a "for" loop with command-not-found in the middle of
>>  it) or if you are using SHARE_HISTORY where there's a race condition
>>  among shells reading the history and shells (re)writing it.
>>
>>  There really isn't any way to "properly" remove an entry once it has been
>>  written to the history file.  You might try some variation of this:
>>
>>    zshaddhistory() {  whence ${${(z)1}[1]} >/dev/null || return 2 }
>>
>>  There are still problems with that for multi-line commands but the right
>>  way to achieve your desired behavior is to avoid writing out the commands
>>  you don't want, not to write them and then delete them again.
>
> It was my first thought too, to avoid adding the command at all.
> However, I misunderstood/misinterpreted the explanation of the
> zshaddhistory hook in the manual. Anyhow, your version works very well,
> thank you!
>
> Nevertheless, I start to see why this is probably a not so good idea (or
> at least pretty hard to implement). For example:
>
>   $ echo "test"; echox "test"
>
> This is similar to the multi-line example. One basically needs to split
> up the command at semicolons and newlines. Then one needs to decide
> which of the subcommands is correct and which one not. Finally one needs
> to decide if an altered version of the command should go to the history
> file or none at all.
>
> I think the possible gains are not worth the effort. I'll stick to your
> solution as it covers most of the cases which I care most about: When
> I've accidentally mistyped a command or entered other garbage.
>
> Thank you very much again and best wishes,
>
>   Jochen

Are you sure you need to *remove* that entry? I can suggest another approach: defer adding history item until you are sure you can add it:

    _record_history_item() {
        typeset -g _LASTHISTITEM="${1%%$'\n'}"
        return 2  # Never record history at this point
    }
    zshaddhistory_functions+=(_record_history_item)

    _add_history_precmd() {
        if test -n "$_LASTHISTITEM" ; then
            print -sr -- "$_LASTHISTITEM"
        endif
    }
    precmd_functions+=(_add_history_precmd)

Then all your handler need is to use

    _LASTHISTITEM=

to drop history item. I did not test my code though.


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

end of thread, other threads:[~2014-07-21 17:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-20  9:04 How to (properly) remove the last entry from history with command_not_found_handler Jochen Keil
2014-07-20 17:45 ` Peter Stephenson
2014-07-20 18:13 ` Bart Schaefer
2014-07-20 20:48   ` Jochen Keil
2014-07-21 17:46     ` ZyX

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