conf: use shutil.move instead of os.rename for saving
[urk.git] / scripts / completion.py
blob68c2e97c8176494ff638453cbd4a932d807b54a4
1 import windows
2 import chaninfo
3 import events
4 from conf import conf
6 def channel_completer(window, left, right, text):
7 if isinstance(window, windows.ChannelWindow):
8 yield window.id
10 for w in windows.get_with(wclass=windows.ChannelWindow, network=window.network):
11 if w is not window:
12 yield w.id
14 for w in windows.get_with(wclass=windows.ChannelWindow):
15 if w.network is not window.network:
16 yield w.id
18 # normal server commands
19 srv_commands = ('ping', 'join', 'part', 'mode', 'server', 'kick',
20 'quit', 'nick', 'privmsg', 'notice', 'topic')
22 def command_completer(window, left, right, text):
23 for c in srv_commands:
24 yield '/%s' % c
26 if 'CMDS' in window.network.isupport:
27 for c in window.network.isupport['CMDS'].split(','):
28 yield '/%s' % c.lower()
30 for c in events.events:
31 if c.startswith('Command') and c != 'Command':
32 yield '/%s' % c[7:].lower()
34 def nick_completer(window, left, right, text):
35 if type(window) == windows.QueryWindow:
36 yield window.id
38 recent_speakers = getattr(window, 'recent_speakers', ())
40 for nick in recent_speakers:
41 if chaninfo.ison(window.network, window.id, nick):
42 yield nick
44 for nick in chaninfo.nicks(window.network, window.id):
45 if nick not in recent_speakers:
46 yield nick
48 def script_completer(window, left, right, text):
49 return events.loaded.iterkeys()
51 def network_completer(window, left, right, text):
52 return conf.get('networks', {}).iterkeys()
54 def get_completer_for(window):
55 input = window.input
57 left, right = input.text[:input.cursor], input.text[input.cursor:]
59 text = left.split(' ')[-1]
61 while True:
62 suffix = ''
63 if text and text[0] in window.network.isupport.get('CHANTYPES', '#&+'):
64 candidates = channel_completer(window, left, right, text)
66 elif input.text.startswith('/reload '):
67 candidates = script_completer(window, left, right, text)
69 elif input.text.startswith('/edit '):
70 candidates = script_completer(window, left, right, text)
72 elif input.text.startswith('/server '):
73 candidates = network_completer(window, left, right, text)
75 elif text.startswith('/'):
76 candidates = command_completer(window, left, right, text)
77 suffix = ' '
79 else:
80 candidates = nick_completer(window, left, right, text)
82 if left == text:
83 suffix = ': '
84 else:
85 suffix = ' '
87 if text:
88 before = left[:-len(text)]
89 else:
90 before = left
92 insert_text = '%s%s%s%s' % (before, '%s', suffix, right)
93 cursor_pos = len(before + suffix)
95 original = window.input.text, window.input.cursor
97 for cand in candidates:
98 if cand.lower().startswith(text.lower()):
99 window.input.text, window.input.cursor = insert_text % cand, cursor_pos + len(cand)
100 yield None
102 window.input.text, window.input.cursor = original
103 yield None
105 # generator--use recent_completer.next() to continue cycling through whatever
106 recent_completer = None
108 def onKeyPress(e):
109 global recent_completer
111 if e.key == 'Tab':
112 if not recent_completer:
113 recent_completer = get_completer_for(e.window)
115 recent_completer.next()
117 else:
118 recent_completer = None
120 def onActive(e):
121 global recent_completer
123 recent_completer = None
125 def onText(e):
126 if chaninfo.ischan(e.network, e.target):
127 if not hasattr(e.window, 'recent_speakers'):
128 e.window.recent_speakers = []
130 for nick in e.window.recent_speakers:
131 if nick == e.source or not chaninfo.ison(e.network, e.target, nick):
132 e.window.recent_speakers.remove(nick)
134 e.window.recent_speakers.insert(0, e.source)
136 onAction = onText