def _readline_from_keyboard(self): c = self.console def nop(e): pass while 1: self._update_line() try: event = c.getkeypress() except KeyboardInterrupt: from pyreadline.keysyms.common import KeyPress from pyreadline.console.event import Event event = Event(0, 0) event.char = "c" event.keyinfo = KeyPress("c", shift=False, control=True, meta=False, keyname=None) log_sock("KBDIRQ") if self.allow_ctrl_c: now = time.time() if (now - self.ctrl_c_timeout ) < self.ctrl_c_tap_time_interval: raise else: self.ctrl_c_timeout = now pass else: raise if self.next_meta: self.next_meta = False control, meta, shift, code = event.keyinfo event.keyinfo = (control, True, shift, code) #Process exit keys. Only exit on empty line keyinfo = event.keyinfo.tuple() if keyinfo in self.exit_dispatch: if lineobj.EndOfLine(self.l_buffer) == 0: raise EOFError if len(keyinfo[-1]) > 1: default = nop else: default = self.self_insert dispatch_func = self.key_dispatch.get(keyinfo, default) log("readline from keyboard:%s,%s" % (keyinfo, dispatch_func)) log_sock("%s|%s" % (format(keyinfo), dispatch_func.__name__), "bound_function") r = None if dispatch_func: r = dispatch_func(event) self._keylog(dispatch_func, self.l_buffer) self.l_buffer.push_undo() self.previous_func = dispatch_func if r: self._update_line() break
def handle_ctrl_c(self): from pyreadline.keysyms.common import KeyPress from pyreadline.console.event import Event log_sock("KBDIRQ") event = Event(0, 0) event.char = "c" event.keyinfo = KeyPress("c", shift=False, control=True, meta=False, keyname=None) if self.allow_ctrl_c: now = time.time() if (now - self.ctrl_c_timeout) < self.ctrl_c_tap_time_interval: log_sock("Raise KeyboardInterrupt") raise KeyboardInterrupt else: self.ctrl_c_timeout = now else: raise KeyboardInterrupt dispatch_func = self.key_dispatch.get(event.keyinfo.tuple(), None) if dispatch_func: r = dispatch_func(event) self._keylog(dispatch_func, self.l_buffer) self.l_buffer.push_undo()
def set_history_length(self, value): log_sock( "set_history_length: old:%d new:%d" % (self._history_length, value), "history", ) self._history_length = value
def set_history_cursor(self, value): log_sock( "set_history_cursor: old:%d new:%d" % (self._history_cursor, value), "history", ) self._history_cursor = value
def peek(self): '''Check event queue.''' Cevent = INPUT_RECORD() count = c_int(0) status = self.PeekConsoleInputA(self.hin, byref(Cevent), 1, byref(count)) log_sock("%s %s %s"%(status,count,Cevent)) if status and count == 1: return event(self, Cevent)
def rectangle(self, rect, attr=None, fill=' '): '''Fill Rectangle.''' log_sock("rect:%s"%[rect]) x0, y0, x1, y1 = rect n = c_int(0) if attr is None: attr = self.attr for y in range(y0, y1): pos = self.fixcoord(x0, y) self.FillConsoleOutputAttribute(self.hout, attr, x1-x0, pos, byref(n)) self.FillConsoleOutputCharacterA(self.hout, ord(fill[0]), x1-x0, pos, byref(n))
def input (self, keytext): if keytext[0:1] == '"' and keytext[-1:] == '"': lst_key = ['"%s"' % c for c in keytext[1:-1]] else: lst_key = [keytext] for key in lst_key: keyinfo, event = keytext_to_keyinfo_and_event (key) dispatch_func = self.key_dispatch.get(keyinfo.tuple(),self.self_insert) self.tested_commands[dispatch_func.__name__]=dispatch_func log_sock("keydisp: %s %s"%( key,dispatch_func.__name__),"debug") dispatch_func (event) self.previous_func=dispatch_func
def __init__(self, newbuffer=0): '''Initialize the Console object. newbuffer=1 will allocate a new buffer so the old content will be restored on exit. ''' self.serial=0 self.attr = System.Console.ForegroundColor self.saveattr = winattr[str(System.Console.ForegroundColor).lower()] self.savebg=System.Console.BackgroundColor log('initial attr=%s' % self.attr) log_sock("%s"%self.saveattr)
def __init__(self, newbuffer=0): '''Initialize the Console object. newbuffer=1 will allocate a new buffer so the old content will be restored on exit. ''' self.serial = 0 self.attr = System.Console.ForegroundColor self.saveattr = winattr[str(System.Console.ForegroundColor).lower()] self.savebg = System.Console.BackgroundColor log('initial attr=%s' % self.attr) log_sock("%s" % self.saveattr)
def input(self, keytext): if keytext[0:1] == '"' and keytext[-1:] == '"': lst_key = ['"%s"' % c for c in keytext[1:-1]] else: lst_key = [keytext] for key in lst_key: keyinfo, event = keytext_to_keyinfo_and_event(key) dispatch_func = self.key_dispatch.get(keyinfo.tuple(), self.self_insert) self.tested_commands[dispatch_func.__name__] = dispatch_func log_sock("keydisp: %s %s" % (key, dispatch_func.__name__), "debug") dispatch_func(event) self.previous_func = dispatch_func
def get(self): '''Get next event from queue.''' inputHookFunc = c_int.from_address(self.inputHookPtr).value Cevent = INPUT_RECORD() count = c_int(0) while 1: if inputHookFunc: call_function(inputHookFunc, ()) status = self.ReadConsoleInputW(self.hin, byref(Cevent), 1, byref(count)) if status and count.value == 1: e = event(self, Cevent) log_sock(ensure_unicode(e.keyinfo),"keypress") return e
def get(self): '''Get next event from queue.''' inputHookFunc = c_int.from_address(self.inputHookPtr).value Cevent = INPUT_RECORD() count = c_int(0) while 1: if inputHookFunc: call_function(inputHookFunc, ()) status = self.ReadConsoleInputA(self.hin, byref(Cevent), 1, byref(count)) if status and count.value == 1: e = event(self, Cevent) log_sock(str(e.keyinfo),"keypress") return e
def __init__(self, console, input): '''Initialize an event from the Windows input structure.''' self.type = '??' self.serial = console.next_serial() self.width = 0 self.height = 0 self.x = 0 self.y = 0 self.char = str(input.KeyChar) self.keycode = input.Key self.state = input.Modifiers log_sock("%s,%s,%s"%(input.Modifiers,input.Key,input.KeyChar),"console") self.type="KeyRelease" self.keysym = make_keysym(self.keycode) self.keyinfo = make_KeyPress(self.char, self.state, self.keycode)
def getkeypress(self): '''Return next key press event from the queue, ignoring others.''' ck=System.ConsoleKey while 1: e = System.Console.ReadKey(True) if e.Key == System.ConsoleKey.PageDown: #PageDown self.scroll_window(12) elif e.Key == System.ConsoleKey.PageUp:#PageUp self.scroll_window(-12) elif str(e.KeyChar)=="\000":#Drop deadkeys log_sock("Deadkey: %s"%e) return event(self,e) pass else: return event(self,e)
def getkeypress(self): '''Return next key press event from the queue, ignoring others.''' ck = System.ConsoleKey while 1: e = System.Console.ReadKey(True) if e.Key == System.ConsoleKey.PageDown: #PageDown self.scroll_window(12) elif e.Key == System.ConsoleKey.PageUp: #PageUp self.scroll_window(-12) elif str(e.KeyChar) == "\000": #Drop deadkeys log_sock("Deadkey: %s" % e) return event(self, e) pass else: return event(self, e)
def _i_search(self, searchfun, direction, init_event): c = self.console line = self.l_buffer.get_line_text() log_sock(str(line)) query = '' if (self.previous_func != self.history_search_forward and self.previous_func != self.history_search_backward): self.query = ''.join(self.l_buffer[0:Point].get_line_text()) hc_start = self._history.history_cursor #+ direction while 1: x, y = self.prompt_end_pos c.pos(0, y) if direction < 0: prompt = 'reverse-i-search' else: prompt = 'forward-i-search' scroll = c.write_scrolling("%s`%s': %s" % (prompt, query, line)) self._update_prompt_pos(scroll) self._clear_after() event = c.getkeypress() if event.keyinfo.keyname == 'backspace': query = query[:-1] if len(query) > 0: #self._history.history_cursor = hc_start #forces search to restart when search empty line=searchfun(query) else: self._bell() line="" #empty query means no search result elif event.char in string.letters + string.digits + string.punctuation + ' ': #self._history.history_cursor = hc_start query += event.char line=searchfun(query) elif event.keyinfo == init_event.keyinfo: self._history.history_cursor += direction line=searchfun(query) else: if event.keyinfo.keyname != 'return': self._bell() break px, py = self.prompt_begin_pos c.pos(0, py) self.l_buffer.set_line(line) self._print_prompt() self._history.history_cursor=len(self._history.history)
def _readline_from_keyboard(self): c = self.console def nop(e): pass while 1: self._update_line() lbuf = self.l_buffer log_sock("point:%d mark:%d selection_mark:%d" % (lbuf.point, lbuf.mark, lbuf.selection_mark)) try: event = c.getkeypress() log_sock(u">>%s" % event) except KeyboardInterrupt: from pyreadline.keysyms.common import KeyPress from pyreadline.console.event import Event event = Event(0, 0) event.char = "c" event.keyinfo = KeyPress("c", shift=False, control=True, meta=False, keyname=None) log_sock("KBDIRQ") if self.allow_ctrl_c: now = time.time() if (now - self.ctrl_c_timeout) < self.ctrl_c_tap_time_interval: raise else: self.ctrl_c_timeout = now pass else: raise if self.next_meta: self.next_meta = False control, meta, shift, code = event.keyinfo event.keyinfo = (control, True, shift, code) # Process exit keys. Only exit on empty line keyinfo = event.keyinfo.tuple() if keyinfo in self.exit_dispatch: if lineobj.EndOfLine(self.l_buffer) == 0: raise EOFError if len(keyinfo[-1]) > 1: default = nop else: default = self.self_insert dispatch_func = self.key_dispatch.get(keyinfo, default) log("readline from keyboard:%s,%s" % (keyinfo, dispatch_func)) log_sock((u"%s|%s" % (ensure_unicode(format(keyinfo)), dispatch_func.__name__)), "bound_function") r = None if dispatch_func: r = dispatch_func(event) self._keylog(dispatch_func, self.l_buffer) self.l_buffer.push_undo() self.previous_func = dispatch_func if r: self._update_line() break
def _search(self, direction, partial): try: if (self.lastcommand != self.history_search_forward and self.lastcommand != self.history_search_backward): self.query = ''.join(partial[0:partial.point].get_line_text()) hcstart = max(self.history_cursor, 0) log_sock("hcstart %s" % hcstart, "history") hc = self.history_cursor + direction while (direction < 0 and hc >= 0) or (direction > 0 and hc < len(self.history)): h = self.history[hc] if not self.query: self.history_cursor = hc result = lineobj.ReadLineTextBuffer(h, point=len( h.get_line_text())) return result elif h.get_line_text().startswith( self.query) and h != partial.get_line_text(): self.history_cursor = hc result = lineobj.ReadLineTextBuffer(h, point=partial.point) return result hc += direction else: if len(self.history) == 0: pass elif hc >= len(self.history) and not self.query: self.history_cursor = len(self.history) return lineobj.ReadLineTextBuffer("", point=0) elif self.history[max(min(hcstart, len(self.history) - 1), 0)].get_line_text().startswith( self.query) and self.query: return lineobj.ReadLineTextBuffer(self.history[max( min(hcstart, len(self.history) - 1), 0)], point=partial.point) else: return lineobj.ReadLineTextBuffer(partial, point=partial.point) return lineobj.ReadLineTextBuffer(self.query, point=min( len(self.query), partial.point)) except IndexError: log_sock("hcstart:%s %s" % (hcstart, len(self.history)), "history") raise
def _readline_from_keyboard_poll(self): c = self.console def nop(e): pass try: event = c.getkeypress() except KeyboardInterrupt: event = self.handle_ctrl_c() self._update_line() return False if self.next_meta: self.next_meta = False control, meta, shift, code = event.keyinfo event.keyinfo = (control, True, shift, code) #Process exit keys. Only exit on empty line keyinfo = event.keyinfo.tuple() if keyinfo in self.exit_dispatch: if lineobj.EndOfLine(self.l_buffer) == 0: raise EOFError if len(keyinfo[-1]) > 1: default = nop else: default = self.self_insert dispatch_func = self.key_dispatch.get(keyinfo, default) log("readline from keyboard:%s,%s" % (keyinfo, dispatch_func)) log_sock("%s|%s" % (format(keyinfo), dispatch_func.__name__), "bound_function") r = None if dispatch_func: r = dispatch_func(event) self._keylog(dispatch_func, self.l_buffer) self.l_buffer.push_undo() self.previous_func = dispatch_func self._update_line() if r: return True return False
def digit_argument(self, e): # (M-0, M-1, ... M--) '''Add this digit to the argument already accumulating, or start a new argument. M-- starts a negative argument.''' args = e.char c = self.console line = self.l_buffer.get_line_text() oldprompt = self.prompt def nop(e): pass while 1: x, y = self.prompt_end_pos c.pos(0, y) self.prompt = "(arg: %s) " % args self._print_prompt() self._update_line() event = c.getkeypress() if event.keyinfo.keyname == 'enter': break elif event.char in "0123456789": args += event.char else: self.argument = int(args) keyinfo = event.keyinfo.tuple() if len(keyinfo[-1]) > 1: default = nop else: default = self.self_insert dispatch_func = self.key_dispatch.get(keyinfo, default) log_sock("%s|%s" % (dispatch_func, str(keyinfo))) dispatch_func(event) break log_sock("END arg=%s" % (self.argument)) self.prompt = oldprompt x, y = self.prompt_end_pos c.pos(0, y) self._print_prompt() self._update_line()
def _non_i_search(self, direction, current): c = pyreadline.rl.console line = current.get_line_text() query = '' while 1: c.pos(*pyreadline.rl.prompt_end_pos) scroll = c.write_scrolling(":%s" % query) pyreadline.rl._update_prompt_pos(scroll) pyreadline.rl._clear_after() event = c.getkeypress() log_sock(str(event), "history") if event.keyinfo.keyname == 'backspace': if len(query) > 0: query = query[:-1] else: break elif event.char in string.letters + string.digits + string.punctuation + ' ': query += event.char elif event.keyinfo.keyname == 'return': break else: pyreadline.rl._bell() log_sock(query, "history") res = "" if query: if direction == -1: res = self.reverse_search_history(query) else: res = self.forward_search_history(query) log_sock(res, "history") return lineobj.ReadLineTextBuffer(res, point=0)
def test_complete(self): import rlcompleter logger.sock_silent = False log_sock("-" * 50, "debug") r = EmacsModeTest() r.completer = rlcompleter.Completer().complete r._bind_key("tab", r.complete) r.input('"exi(ksdjksjd)"') r.input('Control-a') r.input('Right') r.input('Right') r.input('Right') r.input('Tab') self.assert_line(r, "exit(ksdjksjd)", 4) r.input('Escape') r.input('"exi"') r.input('Control-a') r.input('Right') r.input('Right') r.input('Right') r.input('Tab') self.assert_line(r, "exit", 4)
def test_complete (self): import rlcompleter logger.sock_silent=False log_sock("-"*50,"debug") r=EmacsModeTest() r.completer=rlcompleter.Completer().complete r._bind_key("tab",r.complete) r.input('"exi(ksdjksjd)"') r.input('Control-a') r.input('Right') r.input('Right') r.input('Right') r.input('Tab') self.assert_line(r,"exit(ksdjksjd)",4) r.input('Escape') r.input('"exi"') r.input('Control-a') r.input('Right') r.input('Right') r.input('Right') r.input('Tab') self.assert_line(r,"exit",4)
def get_history_length(self): value = self._history_length log_sock("get_history_length:%d" % value, "history") return value
def _get(self): top = System.Console.WindowTop log_sock("WindowTop:%s" % top, "console") return top
def _set(self, value): top = System.Console.WindowTop log_sock("Set WindowTop:old:%s,new:%s" % (top, value), "console")
def set_history_cursor(self, value): log_sock("set_history_cursor: old:%d new:%d" % (self._history_cursor, value), "history") self._history_cursor = value
def set_history_length(self, value): log_sock("set_history_length: old:%d new:%d" % (self._history_length, value), "history") self._history_length = value
def _get(self): top=System.Console.WindowTop log_sock("WindowTop:%s"%top,"console") return top
def get_history_cursor(self): value = self._history_cursor log_sock("get_history_cursor:%d" % value, "history") return value
def _set(self,value): top=System.Console.WindowTop log_sock("Set WindowTop:old:%s,new:%s"%(top,value),"console")