+ window_preexec() {
+ # Get the program name with its arguments.
+ local program_name=$1
+
+ # When sudo is used use real program name instead, but with an
+ # exclamation mark at the beginning.
+ local program_sudo=
+ if [[ $program_name == sudo* ]]; then
+ program_name=${program_name#sudo }
+ program_sudo=yes
+ fi
+ # Remove all arguments from the program name.
+ program_name=${program_name%% *}
+
+ # Ignore often used commands which are only running for a very short
+ # time. This prevents a "blinking" name when it's changed to "cd" for
+ # example and then some milliseconds later back to "zsh".
+ [[ $program_name == (cd*|ls|la|ll|clear|c) ]] && return
+
+ # Change my shortcuts so the real name of the program is displayed.
+ case $program_name in
+ e)
+ program_name=elinks
+ ;;
+ g)
+ program_name=git
+ ;;
+ m)
+ program_name=mutt
+ ;;
+ v|vi)
+ program_name=vim
+ ;;
+ esac
+
+ # Add an exclamation mark at the beginning if running with sudo or if
+ # running zsh as root.
+ if [[ -n $program_sudo || -n $window_root ]]; then
+ program_name=!$program_name
+ fi
+
+ # Add an at mark at the beginning if running through ssh on a
+ # different computer.
+ if [[ -n $SSH_CONNECTION ]]; then
+ program_name="@$program_name"
+ fi
+
+ # Set the window name to the currently running program.
+ window_title "$program_name"
+
+ # Tell precmd() to reset the window name when the program stops.
+ window_reset=yes
+ }
+
+ window_precmd() {
+ # Abort if no window name reset is necessary.
+ [[ -z $window_reset ]] && return
+
+ # Reset the window name to 'zsh'.
+ local name="zsh"
+ # If the function was called with an argument then reset the window
+ # name to '.zsh' (used by clear alias).
+ if [[ -n $1 ]]; then
+ name=".zsh"
+ fi
+
+ # Prepend prefixes like in window_preexec().
+ if [[ -n $SSH_CONNECTION ]]; then
+ window_title "@$name"
+ elif [[ -n $window_root ]]; then
+ window_title "!$name"
+ else
+ window_title $name
+ fi
+
+ # Just reset the name, so no screen reset necessary for the moment.
+ window_reset=
+ }
+
+ # Sets the window title. Works with screen, xterm and rxvt.
+ window_title() {
+ if [[ $TERM == screen* ]]; then
+ print -n "\ek$1\e\\"
+
+ # Update window name in stumpwm if running screen in X11 and when
+ # stumpwm is used.
+ if [[ -n $DISPLAY && -n $window_stumpwm ]]; then
+ echo "$1" | stumpish -e "title" > /dev/null
+ fi
+
+ elif [[ $TERM == xterm* || $TERM == rxvt* ]]; then
+ print -n "\e]2;$1\e\\"
+ fi
+ }
+
+ # Add the preexec() and precmd() hooks.
+ add-zsh-hook preexec window_preexec
+ add-zsh-hook precmd window_precmd
+fi