-
Notifications
You must be signed in to change notification settings - Fork 72
/
vte.sh.in
150 lines (124 loc) · 4.94 KB
/
vte.sh.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Copyright © 2012 Christian Persch
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Not bash or zsh?
[ -n "${BASH_VERSION:-}" -o -n "${ZSH_VERSION:-}" ] || return 0
# Not an interactive shell?
[[ $- == *i* ]] || return 0
# Not running under vte?
[ "${VTE_VERSION:-0}" -ge 3405 ] || return 0
# TERM not supported?
case "$TERM" in
xterm*|vte*|gnome*) :;;
*) return 0 ;;
esac
__vte_termprop_signal() {
local errsv="$?"
printf '\033]666;%s!\033\\' "$1"
return $errsv
}
__vte_termprop_set() {
local errsv="$?"
printf '\033]666;%s=%s\033\\' "$1" "$2"
return $errsv
}
__vte_termprop_reset() {
local errsv="$?"
printf '\033]666;%s\033\\' "$1"
return $errsv
}
__vte_osc7 () {
local errsv="$?"
printf "\033]7;file://%s%s\033\\" "${HOSTNAME}" "$(@libexecdir@/vte-urlencode-cwd)"
return $errsv
}
__vte_precmd() {
local errsv="$?"
__vte_termprop_set "vte.shell.postexec" "$?"
__vte_termprop_signal "vte.shell.precmd"
return $errsv;
}
__vte_prompt_command() {
local errsv="$?"
__vte_termprop_set "vte.shell.postexec" "$errsv"
__vte_osc7
local pwd='~'
[ "$PWD" != "$HOME" ] && pwd=${PWD/#$HOME\//\~\/}
pwd="${pwd//[[:cntrl:]]}"
printf "\033]0;%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${pwd}"
__vte_termprop_signal "vte.shell.precmd"
return $errsv
}
if [[ -n "${BASH_VERSION:-}" ]]; then
# Newer bash versions support PROMPT_COMMAND as an array. In this case
# only add the __vte_osc7 function to it, and leave setting the terminal
# title to the outside setup.
# On older bash, we can only overwrite the whole PROMPT_COMMAND, so must
# use the __vte_prompt_command function which also sets the title.
if [[ "$(declare -p PROMPT_COMMAND 2>&1)" =~ "declare -a" ]]; then
PROMPT_COMMAND+=(__vte_precmd)
PROMPT_COMMAND+=(__vte_osc7)
else
PROMPT_COMMAND="__vte_prompt_command"
fi
PS0=$(__vte_termprop_signal "vte.shell.preexec")
# Shell integration
if [[ "$PS1" != *\]133\;* ]]; then
# Enclose the primary prompt between
# ← OSC 133;D;retval ST (report exit status of previous command)
# ← OSC 133;A ST (mark beginning of prompt)
# → OSC 133;B ST (mark end of prompt, beginning of command line)
PS1='\[\e]133;D;$?\e\\\e]133;A\e\\\]'"$PS1"'\[\e]133;B\e\\\]'
# Prepend OSC 133;L ST for a conditional newline if the previous
# command's output didn't end in one.
# This is not done here by default, in order to provide the default
# visual behavior of shells. Uncomment if you want this feature.
#PS1='\[\e]133;L\e\\\]'"$PS1"
# iTerm2 doesn't touch the secondary prompt.
# Konsole encloses it between 133;A and 133;B.
# For efficient jumping between commands, we follow iTerm2 by default
# and don't mark PS2 as prompt. Uncomment if you want to mark it.
#PS2='\[\e]133;A\e\\\]'"$PS2"'\[\e]133;B\e\\\]'
# Mark the beginning of the command's output by OSC 133;C ST.
# '\r' ensures that the kernel's cooked mode has the right idea of
# the column, important for handling TAB followed by BS keypresses.
# Prepend to the user's PS0 to preserve whether it ends in '\r'.
# Note that bash doesn't support the \[ \] markers here.
PS0='\e]133;C\e\\\r'"${PS0:-}"
fi
elif [[ -n "${ZSH_VERSION:-}" ]]; then
precmd_functions+=(__vte_osc7)
precmd_functions+=(__vte_precmd)
# Shell integration (see the bash counterpart for more detailed comments)
if [[ "$PS1" != *\]133\;* ]]; then
# Enclose the primary prompt between D;retval, A and B.
PS1=$'%{\e]133;D;%?\e\\\e]133;A\e\\%}'"$PS1"$'%{\e]133;B\e\\%}'
# Prepend L for conditional newline (skipped).
#PS1=$'%{\e]133;L\e\\%}'"$PS1"
# Secondary prompt (skipped).
#PS2=$'%{\e]133;A\e\\%}'"$PS2"$'%{\e]133;B\e\\%}'
# Mark the beginning of output by C.
# The execution order is: the single function possibly hooked up
# in $preexec, followed by all the functions hooked up in the
# $preexec_functions array. Ensure that we are the very first.
__vte_preexec() {
local errsv="$?"
printf '\e]133;C\e\\\r'
return $errsv
}
preexec_functions=(__vte_preexec $preexec $preexec_functions)
unset preexec
fi
fi
return 0