1# Copyright © 2006 Shaun McCance <shaunm@gnome.org>
2# Copyright © 2013 Peter De Wachter <pdewacht@gmail.com>
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17# Not bash or zsh?
18[ -n "$BASH_VERSION" -o -n "$ZSH_VERSION" ] || return 0
19
20# Not an interactive shell?
21[[ $- == *i* ]] || return 0
22
23# Not running under vte?
24[ "${VTE_VERSION:-0}" -ge 3405 ] || return 0
25
26__vte_urlencode() (
27  # This is important to make sure string manipulation is handled
28  # byte-by-byte.
29  LC_ALL=C
30  str="$1"
31  while [ -n "$str" ]; do
32    safe="${str%%[!a-zA-Z0-9/:_\.\-\!\'\(\)~]*}"
33    printf "%s" "$safe"
34    str="${str#"$safe"}"
35    if [ -n "$str" ]; then
36      printf "%%%02X" "'$str"
37      str="${str#?}"
38    fi
39  done
40)
41
42# Print a warning so that anyone who's added this manually to his PS1 can adapt.
43# The function will be removed in a later version.
44__vte_ps1() {
45  echo -n "(__vte_ps1 is obsolete)"
46}
47
48__vte_osc7 () {
49  printf "\033]7;file://%s%s\007" "${HOSTNAME:-}" "$(__vte_urlencode "${PWD}")"
50}
51
52__vte_prompt_command() {
53  local pwd='~'
54  [ "$PWD" != "$HOME" ] && pwd=${PWD/#$HOME\//\~\/}
55  printf "\033]0;%s@%s:%s\007%s" "${USER}" "${HOSTNAME%%.*}" "${pwd}" "$(__vte_osc7)"
56}
57
58case "$TERM" in
59  xterm*|vte*)
60    [ -n "$BASH_VERSION" ] && PROMPT_COMMAND="__vte_prompt_command"
61    [ -n "$ZSH_VERSION"  ] && precmd_functions+=(__vte_osc7)
62    ;;
63esac
64
65true
66