Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: ZSH feature request if not present



On Wed, 14 Oct 2015 18:14:01 +0300
ZyX <kp-pav@xxxxxxxxx> wrote:
> No, current directory is not saved. There was [some SO question][1]
> where it was discussed how to save pwd in history alongside with the
> command (using zshaddhistory hook). You need to take it and create a
> ZLE widget that searches among entries with needed pwd. No built-in
> functionality I know is present that may satisfy your request.

Here's my function zshaddhistory-local that does what I want, which is
save the history within the directory, but only if there's already a
file there already --- so this is only useful for a small number of
locations you're particularly interested in.  I've never been confident
it's of general enough use to put it in the distribution.  There's a
companion file history-beginning-local for binding to \ep to search the
local history in preference to the global one, which I haven't posted
--- this is where it really gets a bit of a mess since that essentially
re-implements the core of history-beginning-search-backward.

You need

autoload -Uz add-zsh-hook zshaddhistory-local
add-zsh-hook -Uz zshaddhistory zshaddhistory-local

pws


# This function is an adjunct to the history-beginning-local
# zle widget.  It saves any history entries that would be
# found by that widget in the local history file, provided that
# already exists.  It also saves the history globally unless
# the local-history-only style is set in the context.
#
# The context is :zhist: followed by the function name, a colon, the
# current directory from $PWD, and another colon.

emulate -L zsh
setopt extendedglob warncreateglobal

zmodload -i zsh/parameter

local name=${funcstack[1]} lhp f
local -a lhf lhc
integer lho

zstyle -a ":zhist:${name}:${PWD}:" local-history-file lhf || return 1
zstyle -a ":zhist:${name}:${PWD}:" local-history-commands lhc || return 1
zstyle -s ":zhist:${name}:${PWD}:" local-history-pattern lhp
zstyle -t ":zhist:${name}:${PWD}:" local-history-only && lho=1

for f in $lhf; do
  if [[ -w $f ]]; then
    local -a words

    words=(${(z)1})
    if [[ ${(Q)words[1]} = (${(j.|.)~lhc}) || \
      ( -n $lhp && $1 = ${~lhp} ) ]]; then
      # Save on the global history unless we're told not to.
      # If we define multiple zshaddhistory hooks we want a
      # a way of signalling that we've done this.  One way
      # of doing this would be to set a global parameter to $HISTCMD,
      # although that doesn't change if we've ignored the previous line.
      # Another way would be to have a zshaddhistoryhook that reset
      # a global parameter (since this is called first) and rely
      # on all the other hooks being in zshaddhistory_functions,
      # as they should be for neatness.
      (( lho )) || print -Sr -- ${1%%$'\n'}
      fc -p -- $f
      break
    fi
  fi
done



Messages sorted by: Reverse Date, Date, Thread, Author