~kirkland/byobu/trunk

« back to all changes in this revision

Viewing changes to usr/bin/byobu-ctrl-a.in

  • Committer: kirkland at ubuntu
  • Date: 2023-11-22 22:45:43 UTC
  • Revision ID: kirkland@ubuntu.com-20231122224543-v5igclwtj4xga8tm
Permanently moved Byobu source code repository to https://github.com/dustinkirkland/byobu

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/sh
2
 
#
3
 
#    byobu-ctrl-a - set the ctrl-a behavior
4
 
#    Copyright (C) 2011 Canonical Ltd.
5
 
#
6
 
#    Authors: Dustin Kirkland <kirkland@byobu.org>
7
 
#
8
 
#    This program is free software: you can redistribute it and/or modify
9
 
#    it under the terms of the GNU General Public License as published by
10
 
#    the Free Software Foundation, version 3 of the License.
11
 
#
12
 
#    This program is distributed in the hope that it will be useful,
13
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
#    GNU General Public License for more details.
16
 
#
17
 
#    You should have received a copy of the GNU General Public License
18
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
 
20
 
Usage() {
21
 
        cat <<EOF
22
 
Usage: ${0##*/} [mode]
23
 
   mode is one of 'screen' or 'emacs'
24
 
   if not specified, prompt the user
25
 
EOF
26
 
}
27
 
 
28
 
PKG="byobu"
29
 
[ -r "$HOME/.byoburc" ] && . "$HOME/.byoburc"
30
 
[ -z "${BYOBU_PREFIX}" ] && export BYOBU_PREFIX="@prefix@" || export BYOBU_PREFIX
31
 
. "${BYOBU_PREFIX}/lib/${PKG}/include/common"
32
 
 
33
 
bind_to=""
34
 
keybindings="$BYOBU_CONFIG_DIR/keybindings"
35
 
[ "$BYOBU_BACKEND" = "tmux" ] && keybindings="$keybindings.tmux"
36
 
touch "$keybindings"
37
 
 
38
 
# If the user has already chosen an escape sequence, then
39
 
# presumably they want ctrl-a to operate in emacs mode
40
 
case "$BYOBU_BACKEND" in
41
 
        "screen")
42
 
                if grep -qs "^escape" "$keybindings"; then
43
 
                        # Check for escape definition in local keybindings config
44
 
                        bind_to="emacs"
45
 
                fi
46
 
        ;;
47
 
        "tmux")
48
 
                if grep -qs "^set -g prefix" "$keybindings"e;  then
49
 
                        # Check for escape definition in local keybindings config
50
 
                        bind_to="emacs"
51
 
                fi
52
 
                # Check for some other escape sequence in tmux keys
53
 
                if $BYOBU_BACKEND list-keys 2>/dev/null | grep -qs "^bind-key\s\+[^a]\s\+send-prefix"; then
54
 
                        bind_to="emacs"
55
 
                fi
56
 
        ;;
57
 
esac
58
 
 
59
 
case "${1}" in
60
 
        -h|--help) Usage; exit 0;;
61
 
        screen) bind_to="screen";;
62
 
        emacs) bind_to="emacs";;
63
 
        "") :;;
64
 
        *) { Usage printf "%s\n" "Bad argument $1"; } 1>&2; exit 1;;
65
 
esac
66
 
 
67
 
if [ "${2}" ]; then
68
 
        KEY=$(printf "$2" | $BYOBU_SED 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/')
69
 
        key=$(printf "$2" | $BYOBU_SED 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/')
70
 
else
71
 
        KEY="A"
72
 
        key="a"
73
 
fi
74
 
 
75
 
while [ -z "$bind_to" ]; do
76
 
        echo
77
 
        echo "Configure Byobu's ctrl-a behavior..."
78
 
        echo
79
 
        echo "When you press ctrl-a in Byobu, do you want it to operate in:"
80
 
        echo "    (1) Screen mode (GNU Screen's default escape sequence)"
81
 
        echo "    (2) Emacs mode  (go to beginning of line)"
82
 
        echo
83
 
        echo "Note that:"
84
 
        echo "  - F12 also operates as an escape in Byobu"
85
 
        echo "  - You can press F9 and choose your escape character"
86
 
        echo "  - You can run 'byobu-ctrl-a' at any time to change your selection"
87
 
        echo
88
 
        printf "Select [1 or 2]: "
89
 
        s=$(head -n1)
90
 
        echo
91
 
        case "$s" in
92
 
                1) bind_to="screen"; break;;
93
 
                2) bind_to="emacs"; break;;
94
 
        esac
95
 
done
96
 
 
97
 
case "$bind_to" in
98
 
        emacs)
99
 
                case "$BYOBU_BACKEND" in
100
 
                        screen)
101
 
                                $BYOBU_SED_INLINE -e "/^register x /d" -e "/^bindkey /d" -e "/^escape /d" "$keybindings"
102
 
                                echo "bindkey \"^${KEY}\"" >> "$keybindings"
103
 
                                $BYOBU_BACKEND -X at 0 source "$BYOBU_CONFIG_DIR/profile"
104
 
                        ;;
105
 
                        tmux)
106
 
                                $BYOBU_SED_INLINE -e "/^set -g prefix/d" -e "/ send-prefix/d" -e "/^unbind-key -n C-${key}/d" "$keybindings"
107
 
                                echo "set -g prefix F12" >> "$keybindings"
108
 
                                echo "unbind-key -n C-${key}" >> "$keybindings"
109
 
                                $BYOBU_BACKEND source "$BYOBU_PREFIX/share/byobu/profiles/tmuxrc" 2>/dev/null
110
 
                        ;;
111
 
                esac
112
 
                echo "INFO: ctrl-a will now operate in emacs mode"
113
 
        ;;
114
 
        screen)
115
 
                case "$BYOBU_BACKEND" in
116
 
                        screen)
117
 
                                $BYOBU_SED_INLINE -e "/^register x /d" -e "/^bindkey \"^\"/d" -e "/^escape /d" "$keybindings"
118
 
                                echo "bindkey \"^${KEY}\"" >> "$keybindings"
119
 
                                echo "escape \"^${KEY}${key}\"" >> "$keybindings"
120
 
                                echo "register x \"^${KEY}\"" >> "$keybindings"
121
 
                                $BYOBU_BACKEND -X at 0 source "$BYOBU_CONFIG_DIR/profile"
122
 
                        ;;
123
 
                        tmux)
124
 
                                $BYOBU_SED_INLINE -e "/^set -g prefix/d" -e "/ send-prefix/d" -e "/^unbind-key -n C-${key}/d" "$keybindings"
125
 
                                echo "unbind-key -n C-${key}" >> "$keybindings"
126
 
                                if $BYOBU_BACKEND -V | grep " 1.5"; then
127
 
                                        # tmux 1.5 supports a list of prefixes
128
 
                                        echo "set -g prefix ^${KEY},F12" >> "$keybindings"
129
 
                                else
130
 
                                        # tmux 1.6 and above supports prefix and prefix2
131
 
                                        echo "set -g prefix ^${KEY}" >> "$keybindings"
132
 
                                        echo "set -g prefix2 F12" >> "$keybindings"
133
 
                                fi
134
 
                                echo "bind ${key} send-prefix" >> "$keybindings"
135
 
                                $BYOBU_BACKEND source "$BYOBU_PREFIX/share/byobu/profiles/tmuxrc" 2>/dev/null
136
 
                        ;;
137
 
                esac
138
 
                echo "INFO: ctrl-a will now operate in GNU Screen mode"
139
 
        ;;
140
 
        *)
141
 
                echo "Error: bad value for binding: $bind_to"
142
 
        ;;
143
 
esac
144
 
 
145
 
if [ -z "${2}" ]; then
146
 
        echo "To modify this behavior again later, run 'byobu-ctrl-a'"
147
 
        echo
148
 
fi
149
 
 
150
 
# vi: syntax=sh ts=4 noexpandtab