### vim:ft=zsh:foldmethod=marker
###
### rename xterm from within GNU-screen

local bel esc
bel=$'\a'
esc=$'\e'

if [[ -z ${DISPLAY} ]] ; then
    printf 'xtrename only makes sense in X11.\n'
    return 1
fi
if [[ -z ${1} ]] ; then
    printf 'usage: xtrename() "title for xterm"\n'
    printf '  renames the title of xterm from _within_ screen.\n'
    printf '  Also works without screen.\n'
    return 0
fi

case ${TERM} in
    screen*)
        printf '%s' "${esc}P${esc}"']0;'
        printf '%s' "$1"
        print -n "\C-G${esc}"\\
        ;;
    *)
        printf '%s' "${esc}"']0;'
        printf "%s" "$1"
        printf '%s' "${bel}"
        ;;
esac
return 0