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

Re: Loading functions



On Fri, 20 Apr 2012 11:01:11 +0200
"Christoph (Stucki) von Stuckrad" <stucki@xxxxxxxxxxxxxxx> wrote:
> On Thu, 19 Apr 2012, Mark wrote:
> 
> > Then I realised that ideally my shell would "do something" (e.g.
> > automatically load functions, set env vars) when I go into a directory
> > or any subdirectory and "undo" it (unload these functions, unset the
> > variables) upon leaving the directory. Which ultimately leads to my next
> > question: can zsh be instructed to have a particular environment that is
> > local to certain directories?
> 
> You will have to insert code into the 'hook-function 'chpwd' for
> example by creating a directory of 'environments' like '~/.vim/direnv.d/*'
> and put 'setters' and 'unsetters' into files with some naming scheme,
> and let 'chpwd' source those files according to some schema (tests
> regexps on name, etc.).  This might be realy useful I think...

This is certainly the general solution.

One small part of the answer --- which isn't actually something you
specifically asked for but I find extremely useful for maintaining
histories of "make" and related stuff in different directories --- is to
allow history to be local to a directory.  I do this with special
history search widgets in the line editor together with a function that
decides if the history should be saved locally.  This is independent of
anything to do with directory-local variables, functions etc.

I've attached the two functions needed.  Hope I haven't left out
anything crucial.

My own configuration looks something like this (I've omitted anything
related to commands only present locally):

autoload -Uz add-zsh-hook
add-zsh-hook -Uz zshaddhistory zshaddhistory-local
zle -N history-beginning-local-backward history-beginning-local
zle -N history-beginning-local-forward history-beginning-local
bindkey '\ep' history-beginning-local-backward
bindkey '\en' history-beginning-local-forward

# Use .zsh_local_history as the local history file.
# Note the file must be created by hand by touching it to
# enable local history in a directory.
zstyle ':zhist:*' local-history-file .zsh_local_history
# The following commands maintain a local history
zstyle ':zhist:*' local-history-commands make gmake gdb gcc
# Anything with a relative path is stored in local history.
zstyle ':zhist:*' local-history-pattern '(|[[:alnum:]]## )(.|..)/*'

-- 
Peter Stephenson <pws@xxxxxxx>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom
More information can be found at www.csr.com. Follow CSR on Twitter at http://twitter.com/CSR_PLC and read our blog at www.csr.com/blog
# 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 style local-history-file should be set to a list of files that
#   can contain the local history, in the same format as $HISTFILE.
#   The files are searched in order until the first is find.  They
#   can contain a relative or absolute path; clearly an absolute
#   path to an existing file will always match.
#
#   local-history-commands should be set to a list of commands
#   (or patterns matching commands) that should use the local history
#   file; this can be "*" to handle all commands.  Both styles be set.
#
#   Alternatively (or in addition), local-history-pattern is a scalar
#   that gives a pattern that must match the command line exactly to
#   initiate use of the mechanism.  Typically this will end in a "*".
#
#   If the style local-history-only is not set the global history
#   will be searched if there is no match in the local history.
#   The global history is tried again from the most recent entry;
#   no ordering is implied between the two histories.  (Note
#   this style is also checked by zshaddhistory-local to decide
#   whether to save a history entry to the global history.)
#
#   If the style local-history-verbose is set a notice is printed
#   below the command line if the local history was searched.
#
#   Styles use the standard form of components separated by colons.
#   The components are
#    - The string "zhist": used rather than "zle" to make this consistent
#      across different functions handling history in different parts of
#      the shell.
#    - The widget name
#    - The current directory as given by $PWD.
#   There is a terminating colon.  It is recommended that a wildcard be
#   used at the end to protect against future enhancements.
#
#   For example,
#    zstyle ':zhist:*' local-history-file .zsh-local-history
#    zstyle ':zhist:*' local-history-commands make gcc gdb

emulate -L zsh
setopt extendedglob

local w f new lhp curline
local -a lhf lhc
integer lhv lho restore

typeset -gA __history_beginning_matches
if [[ $WIDGET != $LASTWIDGET ]]; then
  __history_beginning_matches=()
fi
integer oldcursor=CURSOR

zstyle -a ":zhist:${WIDGET}:${PWD}:" local-history-file lhf || return 1
zstyle -a ":zhist:${WIDGET}:${PWD}:" local-history-commands lhc || return 1
zstyle -s ":zhist:${WIDGET}:${PWD}:" local-history-pattern lhp
zstyle -t ":zhist:${WIDGET}:${PWD}:" local-history-verbose && lhv=1
zstyle -t ":zhist:${WIDGET}:${PWD}:" local-history-only && lho=1
# try / always block for restoring history
{
  for f in $lhf; do
    if [[ -f $f ]]; then
      integer iline
      local -a words found

      words=(${(z)BUFFER})
      if [[ ${(Q)words[1]} = (${(j.|.)~lhc}) || \
	    ( -n $lhp && ${BUFFER} = ${~lhp} ) ]]; then
	(( restore = 1 ))
	fc -p $f

	# Search history for pattern.
	# As $history is an associative array we can get all matches.
	if [[ $WIDGET = *forw* ]]; then
	  # Searching forward.  Look back through matches until we
	  # get back to the current history number.
	  found=(${(kOn)history[(R)${LBUFFER}*]})
	  for iline in $found; do
	    (( $iline <= HISTNO )) && break
	    # Skip duplicates.
	    curline=$history[$iline]
	    [[ $curline = $BUFFER ]] && continue
	    [[ -n ${__history_beginning_matches[$curline]} ]] && continue
	    new=$iline
	  done
	else
	  # Searching backward.  Look forward through matches until we
	  # reach the current history number.
	  found=(${(kon)history[(R)${LBUFFER}*]})
	  for iline in $found; do
	    (( $iline >= HISTNO )) && break
	    # Skip duplicates.
	    curline=$history[$iline]
	    [[ $curline = $BUFFER ]] && continue
	    [[ -n ${__history_beginning_matches[$curline]} ]] && continue
	    new=$iline
	  done
	fi
	[[ -n $new ]] && break
	fc -P
	(( restore = 0 ))
      fi
    fi
  done
  if [[ -n $new ]]; then
    # Match found.  Move to line.
    HISTNO=$new
    __history_beginning_matches[$history[$HISTNO]]=1
    if [[ $WIDGET = *-end* ]]; then
      zle end-of-line
    else
      (( CURSOR = oldcursor ))
    fi
    (( lhv )) && zle -M "Matched in local history"
    return 0
  elif (( lho )); then
    (( lhv )) && zle -M "No match in local history"
    return 1
  else
    (( lhv )) && zle -M "No match in local history; falling through"
  fi
} always {
  (( restore )) && fc -P
}

if [[ $WIDGET = *forw* ]]; then
  zle history-beginning-search-forward
else
  zle history-beginning-search-backward
fi
local stat=$?

if (( $stat == 0 )); then
  __history_beginning_matches[$history[$HISTNO]]=1
fi

if [[ $WIDGET = *-end* ]]; then
  zle end-of-line
else
  (( CURSOR = oldcursor ))
fi

return $stat
# end
# 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

zmodload -i zsh/parameter

local name=${funcstack[1]} lhp
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:${WIDGET}:${PWD}:" local-history-pattern lhp
zstyle -t ":zhist:${name}:${PWD}:" local-history-only && lho=1

for f in $lhf; do
  if [[ -f $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