#zdep zshrc.d/options,zshrc.d/vars
### vim:ft=zsh:foldmethod=marker
###
### zfunct - chpwd/precmd/preexec etc.
###
### Frank Terbeck <ft@bewatermyfriend.org>
### Last-Modified: Fri Feb 12 22:18:54 2010
###
### URI: <http://ft.bewatermyfriend.org/comp/zsh.html>
###
# print the canonical file name of an existing file
function realpath() {
emulate -L zsh
local file="$1"
if [[ ! -e ${file} ]]; then
printf 'File does not exist: %s\n' ${file}
return 1
fi
print ${file}(:A)
return 0
}
function zle_cd_back() {
if (( ${#cmdline} == 1 )) && [[ ${cmdline[1]} == .# ]] ; then
BUFFER='cd '
for (( i = 1; i <= ${#${cmdline[1]}}; i++ )); do
BUFFER="${BUFFER}../"
done
BUFFER=${BUFFER%/}
fi
}
zle -N zle_cd_back
zstyle ':acceptline:preprocess' actions zle_cd_back
function screen_printf () {
local format esc
esc=$'\e'
format="$1"
shift
printf '%s' "${esc}k"
printf "${format}" "$@"
printf '%s' "${esc}"\\
}
function cdm () {
local tmp
if [[ -z "${TMUX}" ]]; then
printf 'Not inside of `tmux'\''. Giving up.\n'
return 1
fi
if [[ -n "$1" ]]; then
[[ "$1" == . ]] && tmp="${PWD}" || tmp="$1"
else
tmp="${HOME}"
fi
cd "${tmp}"
tmp="${PWD}"
tmux "set-option" "default-path" "${tmp}"
[[ -n "${DISPLAY}" ]] && tmp=on || tmp=off
tmux "set-option" "set-titles" "${tmp}"
return 0
}
### put the current program (or 'zsh') into screen's hardstatus
if ! zis_437 "atleast" ; then
function precmd () {
if [[ "$TERM" == screen* ]]; then
if [[ -n ${SSH_CLIENT} ]] ; then
screen_printf '%s: %s' "${(%):-"%m"}" "zsh"
else
screen_printf '%s' "zsh"
fi
fi
}
else
# Okay then. If we're this new, we got add-zsh-hook and vcs_info.
# So, let's play.
function precmd_status () {
local zsh
if [[ "$TERM" == screen* ]]; then
if [[ -n ${vcs_info_msg_1_} ]] ; then
zsh=${vcs_info_msg_1_}
else
zsh='zsh'
fi
if [[ -n ${SSH_CLIENT} ]] ; then
screen_printf '%s: %s' "${(%):-"%m"}" "${zsh}"
else
screen_printf '%s' "${zsh}"
fi
fi
}
fi
function preexec_status () {
setopt localoptions extendedglob
local CMD
local -a words
words=( ${(z)1} )
while (( ${#words} > 0 )) ; do
case ${words[1]} in
*=*) shift words ;;
sudo) shift words ;;
-*) shift words ;;
*) break ;;
esac
done
case ${words[1]} in
git|cvs|svn|bzr|hg|p4)
CMD="${words[1]} ${words[2]}" ;;
*) CMD="${words[1]}" ;;
esac
if [[ -n ${SSH_CLIENT} ]] ; then
screen_printf '%s: %s' "${(%):-"%m"}" "${CMD}"
else
screen_printf '%s' "${CMD}"
fi
}
function preexec () {
if [[ "$TERM" == screen* ]]; then
preexec_status "$@"
fi
}
# Directory based profiles.
# This is what went into grml's zshrc, too.
if zis_433 'atleast'; then
CHPWD_PROFILE='default'
function chpwd_profiles() {
# Say you want certain settings to be active in certain directories.
# This is what you want.
#
# zstyle ':chpwd:profiles:/usr/src/grml(|/|/*)' profile grml
# zstyle ':chpwd:profiles:/usr/src/debian(|/|/*)' profile debian
#
# When that's done and you enter a directory that matches the pattern
# in the third part of the context, a function called chpwd_profile_grml,
# for example, is called (if it exists).
#
# If no pattern patches (read: no profile is detected) the profile is
# set to 'default', which means chpwd_profile_default is attempted to
# be called.
#
# A word about the context (the ':chpwd:profiles:*' stuff in the zstyle
# command) which is used: The third part in the context is matched against
# ${PWD}. That's why using a pattern such as /foo/bar(|/|/*) makes sense.
# Because that way the profile is detected for all these values of ${PWD}:
# /foo/bar
# /foo/bar/
# /foo/bar/baz
# So, if you want to make double damn sure a profile works in /foo/bar
# and everywhere deeper in that tree, just use (|/|/*) and be happy.
#
# The name of the detected profile will be available in a variable called
# 'profile' in your functions. You don't need to do anything, it'll just
# be there.
#
# Then there is the parameter $CHPWD_PROFILE is set to the profile, that
# was is currently active. That way you can avoid running code for a
# profile that is already active, by running code such as the following
# at the start of your function:
#
# function chpwd_profile_grml() {
# [[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
# ...
# }
#
# The initial value for $CHPWD_PROFILE is 'default'.
#
# Version requirement:
# This feature requires zsh 4.3.3 or newer.
# If you use this feature and need to know whether it is active in your
# current shell, there are several ways to do that. Here are two simple
# ways:
#
# a) If knowing if the profiles feature is active when zsh starts is
# good enough for you, you can put the following snippet into your
# zshrc.local:
#
# (( ${+functions[chpwd_profiles]} )) && print "directory profiles active"
#
# b) If that is not good enough, and you would prefer to be notified
# whenever a profile changes, you can solve that by making sure you
# start *every* profile function you create like this:
#
# function chpwd_profile_myprofilename() {
# [[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
# print "chpwd(): Switching to profile: $profile"
# ...
# }
#
# That makes sure you only get notified if a profile is *changed*,
# not everytime you change directory, which would probably piss
# you off fairly quickly. :-)
#
# There you go. Now have fun with that.
local -x profile
zstyle -s ":chpwd:profiles:${PWD}" profile profile || profile='default'
if (( ${+functions[chpwd_profile_$profile]} )) ; then
chpwd_profile_${profile}
fi
CHPWD_PROFILE="${profile}"
return 0
}
chpwd_functions+=( chpwd_profiles )
fi