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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
import os
import code
import logging
import threading
import subprocess
import buffer
import hooks
import settings
class Command:
"""base class for commands"""
def __init__(self, prehook=None, posthook=None):
self.prehook = prehook
self.posthook = posthook
self.undoable = False
self.help = self.__doc__
def apply(self, caller):
pass
class ShutdownCommand(Command):
"""shuts the MUA down cleanly"""
def apply(self, ui):
ui.shutdown()
class OpenThreadCommand(Command):
"""open a new thread-view buffer"""
def __init__(self, thread, **kwargs):
self.thread = thread
Command.__init__(self, **kwargs)
def apply(self, ui):
ui.logger.info('open thread view for %s' % self.thread)
sb = buffer.SingleThreadBuffer(ui, self.thread)
ui.buffer_open(sb)
class SearchCommand(Command):
"""open a new search buffer"""
def __init__(self, query, **kwargs):
"""
@param query initial querystring
"""
self.query = query
Command.__init__(self, **kwargs)
def apply(self, ui):
sb = buffer.SearchBuffer(ui, self.query)
ui.buffer_open(sb)
class SearchPromptCommand(Command):
"""prompt the user for a querystring, then start a search"""
def apply(self, ui):
querystring = ui.prompt('search threads:')
ui.logger.info("got %s" % querystring)
if querystring:
cmd = factory('search', query=querystring)
ui.apply_command(cmd)
class EditCommand(Command):
"""
opens editor
TODO tempfile handling etc
"""
def __init__(self, path, spawn=False, **kwargs):
self.path = path
self.spawn = settings.spawn_editor or spawn
Command.__init__(self, **kwargs)
def apply(self, ui):
def afterwards():
ui.logger.info('Editor was closed')
cmd = ExternalCommand(settings.editor_cmd % self.path,
spawn=self.spawn,
onExit=afterwards)
ui.apply_command(cmd)
class PagerCommand(Command):
"""opens pager"""
def __init__(self, path, spawn=False, **kwargs):
self.path = path
self.spawn = settings.spawn_pager or spawn
Command.__init__(self, **kwargs)
def apply(self, ui):
def afterwards():
ui.logger.info('pager was closed')
cmd = ExternalCommand(settings.pager_cmd %self.path,
spawn=self.spawn,
onExit=afterwards)
ui.apply_command(cmd)
class ExternalCommand(Command):
"""calls external command"""
def __init__(self, commandstring, spawn=False, refocus=True, onExit=None, **kwargs):
self.commandstring = commandstring
self.spawn = spawn
self.refocus = refocus
self.onExit = onExit
Command.__init__(self, **kwargs)
def apply(self, ui):
def call(onExit, popenArgs):
callerbuffer = ui.current_buffer
ui.logger.info('CALLERBUFFER: %s'%callerbuffer)
proc = subprocess.Popen(*popenArgs,shell=True)
proc.wait()
if callable(onExit):
onExit()
if self.refocus and callerbuffer in ui.buffers:
ui.logger.info('TRY TO REFOCUS: %s'%callerbuffer)
ui.buffer_focus(callerbuffer)
return
if self.spawn:
cmd = settings.terminal_cmd % self.commandstring
thread = threading.Thread(target=call, args=(self.onExit, (cmd,)))
thread.start()
else:
ui.mainloop.screen.stop()
cmd = self.commandstring
logging.debug(cmd)
call(self.onExit,(cmd,))
ui.mainloop.screen.start()
class OpenPythonShellCommand(Command):
"""
opens an interactive shell for introspection
"""
def apply(self, ui):
ui.mainloop.screen.stop()
code.interact(local=locals())
ui.mainloop.screen.start()
class BufferCloseCommand(Command):
"""
close a buffer
@param buffer the selected buffer
"""
def __init__(self, buffer=None, **kwargs):
self.buffer = buffer
Command.__init__(self, **kwargs)
def apply(self, ui):
if not self.buffer:
self.buffer = ui.current_buffer
ui.buffer_close(self.buffer)
ui.buffer_focus(ui.current_buffer)
class BufferFocusCommand(Command):
"""
focus a buffer
@param buffer the selected buffer
"""
def __init__(self, buffer=None, offset=0, **kwargs):
self.buffer = buffer
self.offset = offset
Command.__init__(self, **kwargs)
def apply(self, ui):
if not self.buffer:
self.buffer = ui.current_buffer
i = ui.buffers.index(self.buffer)
l = len(ui.buffers)
ui.buffer_focus(ui.buffers[(i + self.offset) % l])
class BufferListCommand(Command):
"""
open a bufferlist
"""
def __init__(self, filtfun=None, **kwargs):
self.filtfun = filtfun
Command.__init__(self, **kwargs)
def apply(self, ui):
b = buffer.BufferListBuffer(ui, self.filtfun)
ui.buffers.append(b)
b.rebuild()
ui.buffer_focus(b)
class TagListCommand(Command):
"""
open a taglist
"""
def __init__(self, filtfun=None, **kwargs):
self.filtfun = filtfun
Command.__init__(self, **kwargs)
def apply(self, ui):
tags = ui.dbman.get_all_tags()
b = buffer.TagListBuffer(ui, tags, self.filtfun)
ui.buffers.append(b)
b.rebuild()
ui.buffer_focus(b)
class ToggleThreadTagCommand(Command):
"""
"""
def __init__(self, thread, tag, **kwargs):
self.thread = thread
self.tag = tag
Command.__init__(self, **kwargs)
def apply(self, ui):
if self.tag in self.thread.get_tags():
self.thread.remove_tags([self.tag])
else:
self.thread.add_tags([self.tag])
#refresh selected threadline
sbuffer = ui.current_buffer
threadwidget = sbuffer.get_selected_threadline()
#threadwidget.reload_tag(ui.dbman) #threads seem to cache their tags
threadwidget.rebuild() #rebuild and redraw the line
#TODO: remove line from searchlist if thread doesn't match the query
#qs="(%s) AND thread:%s"%(sbuffer.querystring,self.thread.get_thread_id())
#if ui.dbman.count_messages(qs) == 0:
# ui.logger.debug('remove: %s'%self.thread)
# #sbuffer.threadlist.remove(threadwidget)
#sbuffer.rebuild()
commands = {
'buffer_close': (BufferCloseCommand, {}),
'buffer_list': (BufferListCommand, {}),
'buffer_focus': (BufferFocusCommand, {}),
'buffer_next': (BufferFocusCommand, {'offset': 1}),
'buffer_prev': (BufferFocusCommand, {'offset': -1}),
'open_inbox': (SearchCommand, {'query': 'tag:inbox'}),
'open_unread': (SearchCommand, {'query': 'tag:unread'}),
'open_search': (SearchPromptCommand, {}),
'open_thread': (OpenThreadCommand, {}),
'search': (SearchCommand, {}),
'shutdown': (ShutdownCommand, {}),
'shell': (OpenPythonShellCommand, {}),
'view_log': (PagerCommand, {'path': 'debug.log'}),
'call_editor': (EditCommand, {}),
'call_pager': (PagerCommand, {}),
'open_taglist': (TagListCommand, {}),
'toggle_thread_tag': (ToggleThreadTagCommand, {'tag': 'inbox'})
}
def factory(cmdname, **kwargs):
if cmdname in commands:
(cmdclass, parms) = commands[cmdname]
parms = parms.copy()
parms.update(kwargs)
for (key, value) in kwargs.items():
if callable(value):
try:
parms[key] = value()
except:
parms[key] = None
else:
parms[key] = value
prehook = hooks.get_hook('pre-' + cmdname)
if prehook:
parms['prehook'] = prehook
posthook = hooks.get_hook('post-' + cmdname)
if posthook:
parms['posthook'] = hooks.get_hook('post-' + cmdname)
logging.debug('cmd parms %s' % parms)
return cmdclass(**parms)
else:
logging.error('there is no command %s' % cmdname)
|