''' ''' initialized = False macro_names_filenames = {} def __init__(): ''' get all macro names, imp0rt cPickle/pickle. ''' if initialized: return True else: global initialized try: flauxtext.imp0rt('cPickle') flauxtext.pickle = flauxtext.cPickle except: flauxtext.imp0rt('pickle') flauxtext.imp0rt('glob') basename = flauxtext.os.path.basename splitext = flauxtext.os.path.splitext for fn in flauxtext.glob.glob(flauxtext.os.path.join(flauxtext.plugindir, 'macros', '*.macro')): macro_names_filenames[splitext(basename(fn))[0]] = fn initialized = True def record(name=None): ''' ''' macro_list = [] number_control_q_s = [] number_keypresses = [0] def callback(wid, evt): macro_list.append((evt.keysym, evt.state)) number_keypresses[0] += 1 if (evt.keysym == 113) and (evt.state == flauxtext.gtk.CONTROL_MASK): if (len(number_control_q_s) == 0) or (number_control_q_s[-1] == (1 + number_keypresses[-1])): number_control_q_s.append(number_keypresses[-1]) else: number_control_q_s.pop() if len(number_control_q_s) == 3: # XXX unhook keypresses if name is None: # prompt for a name d = self.mk_dialog('name?', (flauxtext.gtk.STOCK_OK, 1, flauxtext.gtk.STOCK_CANCEL, 0)) e = flauxtext.gtk.Entry() d.vbox.pack_start(e) if d.run(): name = e.get_text() else: return True data = flauxtext.pickle.pickle(macro_list) macro_names_filenames[name] = data open(flauxtext.os.path.join(flauxtext.plugindir, 'macros', name + '.macro'), 'w').write(data) return True # don't call any other callbacks # XXX trap all keypresses to callback def play(name=None): ''' def macro(*a): ''' record/replay keyboard macros. if macro name in a, replay the specified macro. else: trap all key-presses until three sequential occurrences of control+q, then prompt for a macro name, write the pickled keysyms to plugindir/%s.macro ''' if a and isinstance(a[0], (str, unicode, flauxtext.gtk.Action)): macro = a[0] if isinstance(macro, flauxtext.gtk.Action): macro = macro.get_name() if macro in macro_names_filenames: play(macro) else: record(macro) else: record()