-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy-completion-pyxbindman
executable file
·194 lines (185 loc) · 5.98 KB
/
py-completion-pyxbindman
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
#!/usr/bin/env python
'''
This script provides completion optinos for pyxbindman
'''
from xbindkeys import Xbindkeysrc, EnvironmentError
import sys
import os
import readline
import argparse
import subprocess
import signal
import xbindkeys
import re
from argparse import ArgumentError
if sys.argv[-1].startswith('--'):
sys.argv = sys.argv[:-1]
parser = argparse.ArgumentParser()
parser.add_argument('command', nargs='?')
parser.add_argument('keysym', nargs='?')
parser.add_argument(
'-d',
'--delete',
nargs='?',
metavar='KEYSYM_OR_KEYCODE',
)
parser.add_argument(
'-D',
'--delete-by-command',
nargs='?',
)
parser.add_argument(
'-f',
'--file',
default=os.path.join(os.getenv('HOME'), '.xbindkeysrc'),
nargs='?',
)
parser.add_argument(
'-r',
'--restart',
action='store_true',
)
parser.add_argument(
'-k',
'--kill',
action='store_true',
)
parser.add_argument(
'-c',
'--change',
action='store_true',
)
parser.add_argument(
'-s',
'--show',
nargs='?',
)
parser.add_argument(
'--cur-word',
)
parser.add_argument(
'--option-complete',
action='store_true'
)
shell_words_opitons = (
'-d',
'--delete',
'-D',
'--delete-by-command',
'-f',
'--file',
'-r',
'--restart',
'-k',
'--kill',
'-s',
'--show',
'-c',
'--change',
)
def print_completion(all_args):
'''
Prints a list of completion choices divided by newline character.
Used by /etc/bash_completion.d/pyxbindman
'''
args = all_args[0]
if args.file:
# Unquote file argument
if args.file.startswith('"') and args.file.endswith('"'):
args.file = args.file[1:-1]
if args.file.startswith("'") and args.file.endswith("'"):
args.file = args.file[1:-1]
if args.file:
file = args.file
else:
file = xbindkeys.DEFAULT_RC
xb = Xbindkeysrc(file)
mut_ex_args = (args.delete, args.delete_by_command, args.command,
args.restart, args.show, args.kill)
# Mutually exclusive arguments.
# args.command is also one of them, I have to test for it separately.
names_of_mut_ex_args = ('-d', '-D', '-k', '-r', '-s', '--delete',
'--delete-by-command', '--kill', '--restart', '--show')
# Exit if any two mutually exclusive arguments are present at the same time
num_of_mut_ex_args = 0
for arg in mut_ex_args:
if arg != None and arg != False:
num_of_mut_ex_args += 1
if num_of_mut_ex_args > 1:
sys.exit(0)
last_arg = sys.argv[-1]
if args.option_complete:
# Options for completing shell-words of arguments (anything that starts
# with a - (hyphen)
args.cur_word = re.sub(r'\\', r'', args.cur_word)
# argv contains all arguments of pyxbindman command except the current word
argv = filter(lambda a: a != args.cur_word, [re.sub(r'\\', r'', arg) for arg in sys.argv])
if '--cur-word' in argv:
del argv[argv.index('--cur-word')+1]
del argv[argv.index('--cur-word')]
if '--option-complete' in argv:
del argv[argv.index('--option-complete')]
no_exclusive_optional_args = frozenset(argv).isdisjoint(frozenset(names_of_mut_ex_args))
if no_exclusive_optional_args and (args.command == None or args.command == '-'):
for word in names_of_mut_ex_args:
print word
if (no_exclusive_optional_args
and not '-c' in argv
and not '--change' in argv):
print '-c'
print '--change'
if not '-f' in argv and not '--file' in argv:
print '-f'
print '--file'
if last_arg == '-D' or last_arg == '--delete-by-command' or args.delete_by_command != None:
# Options for -D argument
if args.cur_word == '' and args.delete_by_command != None:
sys.exit(0)
else:
for item in xb.get_bindings():
print item['command']
elif last_arg == '-d' or last_arg == '--delete':
for item in xb.get_bindings():
print item['keysym']
elif last_arg == '-s' or last_arg == '--show' or args.show != None:
if (args.show != None
and (args.show.startswith("m")
or args.show.startswith("'m")
or args.show.startswith('"m'))):
# Autocomplete over keycodes if arguments starts with m
for item in xb.get_bindings():
print item['keycode']
for item in xb.get_bindings():
print item['command']
for item in xb.get_bindings():
print item['keysym']
elif args.delete != None:
if (args.delete.startswith('m') or
args.delete.startswith('"m') or
args.delete.startswith("'m")):
for item in xb.get_bindings():
print item['keycode']
else:
for item in xb.get_bindings():
print item['keysym']
elif args.cur_word == args.command or args.command == None:
# If args.command is being completed
if args.change:
for item in xb.get_bindings():
print item['command']
else:
process = subprocess.Popen(['bash', '-c', 'compgen -ac'], stdout=subprocess.PIPE)
(out, err) = process.communicate()
for line in out.splitlines():
print line
elif args.cur_word == args.keysym or args.keysym == None:
# If args.command is being completed
if args.keysym != None and (args.keysym.startswith('m') or
args.keysym.startswith('"m') or
args.keysym.startswith("'m")):
for item in xb.get_bindings():
print item['keycode']
for item in xb.get_bindings():
print item['keysym']
if __name__ == '__main__':
print_completion(parser.parse_known_args())