-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnome-shell-keybindings
executable file
·205 lines (192 loc) · 4.73 KB
/
gnome-shell-keybindings
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/sh
# Copyright (c) 2018 Andrew Steel <copyright@andrewsteel.net>
#
# SPDX-License-Identifier: GPL-3.0-only
#
# The GNU General Public License Version 3 only:
#
# This is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License Version 3
# as published by the Free Software Foundation.
#
# This software 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.
#
# For the full license, see <https://www.gnu.org/licenses/>.
# gnome-shell-keybindings:
# List, export, and import GNOME keybindings.
IFS="$(printf ' \t\n!')"; IFS="${IFS%!}"
unset -v \
COLOR_BOLD \
COLOR_DIM \
COLOR_RESET
if test -t 1; then
COLOR_BOLD='\033[1m'
COLOR_DIM='\033[2m'
COLOR_RESET='\033[0m'
fi
dump_keybindings() {(
KEYBINDINGS="${1:?}"
index='0'
for KEYBINDING in ${KEYBINDINGS}; do
test "${index}" -gt '0' \
&& printf '\n'
printf '[%s]\n' "$(
printf '%s\n' "${KEYBINDING}" \
| sed 's|/\(.*\)/|\1|'
)"
dconf dump "${KEYBINDING}" \
| tail -n '+2' \
| sed -n '
/^\[/q;
/./p;
'
index="$(( index + 1 ))"
done
)}
get_keybindings() {(
unarray() {(
# 1: Split indexes with newlines.
# 2: Strip array markup.
sed 's/'\''[^'\'']*'\''[^[:space:],]*/&\n/g' \
| sed '
s/^[[:space:]]*\[[[:space:]]*//;
s/[[:space:]]*\][[:space:]]*$//;
s/^[[:space:],]*'\''//;
s/'\''[[:space:],]*//;
'
)}
gsettings get \
org.gnome.settings-daemon.plugins.media-keys custom-keybindings \
| sed -n '/^[^@]/p' \
| unarray
)}
list_keybindings() {(
KEYBINDINGS="${1:?}"
index='0'
for KEYBINDING in ${KEYBINDINGS}; do
test "${index}" -gt 0 \
&& printf -- '--\n'
printf '%b/%b%s%b\n' \
"${COLOR_RESET}${COLOR_DIM}" \
"${COLOR_RESET}${COLOR_BOLD}" \
"$(basename "${KEYBINDING}")" \
"${COLOR_RESET}"
for KEY in 'name' 'binding' 'command'; do
printf '%b%s%b:%b %b%s%b\n' \
"${COLOR_RESET}${COLOR_BOLD}" \
"${KEY}" \
"${COLOR_RESET}${COLOR_DIM}" \
"${COLOR_RESET}" \
"${COLOR_RESET}${COLOR_DIM}" \
"$(
gsettings get \
"org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:${KEYBINDING}" \
"${KEY}" \
)" \
"${COLOR_RESET}"
done
index="$(( index + 1 ))"
done
)}
load_keybindings() {(
# We neeed to sort the paths for the keybindings numerically. Although
# complicated by the presence of non-numeric characters, this is still possible
# with standard `sort`. We set the field separator to `/`, grab the 8th field,
# and start sorting numerically (with the `-n` switch) from the 7th character.
#
# The diagram below depicts how the field and character counts map respectively
# against the standard keybindings paths.
#
# 1 2 3 4 5 6 7 8
# /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom18/
# 1234567
DUMP="$(cat)"
KEYBINDINGS_ARRAY="$(
KEYBINDINGS="$(
NEW_KEYBINDINGS="$(
printf '%s\n' "${DUMP}" \
| sed '
s/^[[:space:]]*//;
s/[[:space:]]*$//;
' \
| sed -n '/^\[/p' \
| sed 's|\[\(.*\)\]|/\1/|'
)"
printf '%s\n' "$(get_keybindings)" "${NEW_KEYBINDINGS}" \
| sed '/^$/d' \
| sort -n -t '/' -k 8.7 \
| uniq
)"
printf '%s' "['${KEYBINDINGS}']" \
| tr '\n' ',' \
| sed 's/,/'\''& '\''/g'
)"
gsettings set \
org.gnome.settings-daemon.plugins.media-keys custom-keybindings \
"${KEYBINDINGS_ARRAY}"
printf '%s\n' "${DUMP}" \
| dconf load '/'
)}
usage() {(
printf '%s\n' \
"Usage: $(basename "${0}") [OPTION]..." \
'' \
'List, export, and import GNOME keybindings.' \
'' \
' -e, --export export configuration to stdout' \
' --help display this help' \
' -i, --import import configuration from stdin' \
' -l, --list (default) display the current configuration'
)}
unset -v \
switch_mode
while test "${#}" -gt 0; do
case "${1}" in
( '-e' \
| '--e' \
| '--ex' \
| '--exp' \
| '--expo' \
| '--expor' \
| '--export' )
switch_mode='export'
;;
('-g')
switch_mode='get'
;;
( '--h' \
| '--he' \
| '--hel' \
| '--help' )
switch_mode='help'
;;
( '-i' \
| '--i' \
| '--im' \
| '--imp' \
| '--impo' \
| '--impor' \
| '--import' )
switch_mode='import'
;;
( '-l' \
| '--l' \
| '--li' \
| '--lis' \
| '--list' )
switch_mode='list'
;;
esac
shift
done
MODE="${switch_mode:-list}"
case "${MODE}" in
('export') dump_keybindings "$(get_keybindings)" ;;
('get') get_keybindings ;;
('help') usage ;;
('import') load_keybindings ;;
('list') list_keybindings "$(get_keybindings)" ;;
esac