diff --git a/doc/ChangeLog b/doc/ChangeLog index 4436200..38e818d 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,7 @@ +2006-07-13 Jörgen Stenarson + * Work to get selection between ironpython and cpython to work + * Some editing works but there are issues with control keys for ironpython + 2006-07-04 Jörgen Stenarson * First commit for ironpython. Typing in alphabet works but no special keys. diff --git a/pyreadline/__init__.py b/pyreadline/__init__.py index 2c8bb25..34393a6 100644 --- a/pyreadline/__init__.py +++ b/pyreadline/__init__.py @@ -6,7 +6,6 @@ # Distributed under the terms of the BSD License. The full license is in # the file COPYING, distributed as part of this software. #***************************************************************************** - import logger,clipboard,lineeditor,modes from rlmain import * __all__ = [ 'parse_and_bind', diff --git a/pyreadline/clipboard/__init__.py b/pyreadline/clipboard/__init__.py index 582adf3..4b54c3d 100644 --- a/pyreadline/clipboard/__init__.py +++ b/pyreadline/clipboard/__init__.py @@ -1,17 +1,73 @@ -from common import * success=False + try: from clipboard import GetClipboardText,SetClipboardText success=True except ImportError: - pass - -try: - from ironpython_clipboard import GetClipboardText,SetClipboardText - success=True -except ImportError: - pass + try: + from ironpython_clipboard import GetClipboardText,SetClipboardText + success=True + except ImportError: + pass + + +def send_data(lists): + SetClipboardText(make_tab(lists)) + + +def set_clipboard_text(toclipboard): + SetClipboardText(str(toclipboard)) + +def make_tab(lists): + if hasattr(lists,"tolist"): + lists=lists.tolist() + ut=[] + for rad in lists: + if type(rad) in [list,tuple]: + ut.append("\t".join(["%s"%x for x in rad])) + else: + ut.append("%s"%rad) + return "\n".join(ut) + +def make_list_of_list(txt): + def make_num(x): + try: + return int(x) + except ValueError: + try: + return float(x) + except ValueError: + try: + return complex(x) + except ValueError: + return x + return x + ut=[] + flag=False + for rad in [x for x in txt.split("\r\n") if x!=""]: + raden=[make_num(x) for x in rad.split("\t")] + if str in map(type,raden): + flag=True + ut.append(raden) + return ut,flag + + +def get_clipboard_text_and_convert(paste_list=False): + """Get txt from clipboard. if paste_list==True the convert tab separated + data to list of lists. Enclose list of list in array() if all elements are + numeric""" + txt=GetClipboardText() + if txt: + if paste_list and "\t" in txt: + array,flag=make_list_of_list(txt) + if flag: + txt=repr(array) + else: + txt="array(%s)"%repr(array) + txt="".join([c for c in txt if c not in " \t\r\n"]) + return txt + diff --git a/pyreadline/clipboard/clipboard.py b/pyreadline/clipboard/clipboard.py index 17c45d2..594bfa1 100644 --- a/pyreadline/clipboard/clipboard.py +++ b/pyreadline/clipboard/clipboard.py @@ -34,7 +34,7 @@ ################################################################################### from ctypes import * -from keysyms.winconstants import CF_TEXT, GHND +from pyreadline.keysyms.winconstants import CF_TEXT, GHND OpenClipboard = windll.user32.OpenClipboard EmptyClipboard = windll.user32.EmptyClipboard diff --git a/pyreadline/clipboard/common.py b/pyreadline/clipboard/common.py deleted file mode 100644 index 03722e8..0000000 --- a/pyreadline/clipboard/common.py +++ /dev/null @@ -1,58 +0,0 @@ - - -def send_data(lists): - SetClipboardText(make_tab(lists)) - - -def set_clipboard_text(toclipboard): - SetClipboardText(str(toclipboard)) - -def make_tab(lists): - if hasattr(lists,"tolist"): - lists=lists.tolist() - ut=[] - for rad in lists: - if type(rad) in [list,tuple]: - ut.append("\t".join(["%s"%x for x in rad])) - else: - ut.append("%s"%rad) - return "\n".join(ut) - -def make_list_of_list(txt): - def make_num(x): - try: - return int(x) - except ValueError: - try: - return float(x) - except ValueError: - try: - return complex(x) - except ValueError: - return x - return x - ut=[] - flag=False - for rad in [x for x in txt.split("\r\n") if x!=""]: - raden=[make_num(x) for x in rad.split("\t")] - if str in map(type,raden): - flag=True - ut.append(raden) - return ut,flag - - -def get_clipboard_text_and_convert(paste_list=False): - """Get txt from clipboard. if paste_list==True the convert tab separated - data to list of lists. Enclose list of list in array() if all elements are - numeric""" - txt=GetClipboardText() - if txt: - if paste_list and "\t" in txt: - array,flag=make_list_of_list(txt) - if flag: - txt=repr(array) - else: - txt="array(%s)"%repr(array) - txt="".join([c for c in txt if c not in " \t\r\n"]) - return txt - diff --git a/pyreadline/console/__init__.py b/pyreadline/console/__init__.py index 8602b69..bb79204 100644 --- a/pyreadline/console/__init__.py +++ b/pyreadline/console/__init__.py @@ -6,13 +6,12 @@ try: from console import * success=True except ImportError: - pass -try: - from ironpython_console import * - success=True -except ImportError: - pass + try: + from ironpython_console import * + success=True + except ImportError: + pass if not success: diff --git a/pyreadline/console/console.py b/pyreadline/console/console.py index 1cab1b4..b789ba1 100644 --- a/pyreadline/console/console.py +++ b/pyreadline/console/console.py @@ -26,7 +26,7 @@ except ImportError: raise ImportError("You need ctypes to run this code") # my code -from pyreadline.keysyms import make_keysym, make_keyinfo +from pyreadline.keysyms import make_KeyPress # some constants we need STD_INPUT_HANDLE = -10 @@ -459,7 +459,6 @@ class Console(object): '''Return next key press event from the queue, ignoring others.''' while 1: e = self.get() - log("getKeypress:%s,%s,%s"%(e.keyinfo,e.keycode,e.type)) if e.type == 'KeyPress' and e.keycode not in key_modifiers: log(e) if e.keysym == 'Next': @@ -469,6 +468,7 @@ class Console(object): else: return e elif e.type == 'KeyRelease' and e.keyinfo==(True, False, False, 83): + log("getKeypress:%s,%s,%s"%(e.keyinfo,e.keycode,e.type)) return e def getchar(self): @@ -544,6 +544,9 @@ for func in funcs: from event import Event +VkKeyScan = windll.user32.VkKeyScanA + + class event(Event): '''Represent events from the console.''' def __init__(self, console, input): @@ -557,9 +560,9 @@ class event(Event): self.char = '' self.keycode = 0 self.keysym = '??' - self.keyinfo = '' # a tuple with (control, meta, shift, keycode) for dispatch + self.keyinfo = None # a tuple with (control, meta, shift, keycode) for dispatch self.width = None - + if input.EventType == KEY_EVENT: if input.Event.KeyEvent.bKeyDown: self.type = "KeyPress" @@ -568,8 +571,8 @@ class event(Event): self.char = input.Event.KeyEvent.uChar.AsciiChar self.keycode = input.Event.KeyEvent.wVirtualKeyCode self.state = input.Event.KeyEvent.dwControlKeyState - self.keysym = make_keysym(self.keycode) - self.keyinfo = make_keyinfo(self.keycode, self.state) + self.keyinfo=make_KeyPress(self.char,self.state,self.keycode) + elif input.EventType == MOUSE_EVENT: if input.Event.MouseEvent.dwEventFlags & MOUSE_MOVED: self.type = "Motion" @@ -690,10 +693,16 @@ def install_readline(hook): if __name__ == '__main__': import time, sys + + + def p(char): + return chr(VkKeyScan(ord(char)) & 0xff) + c = Console(0) sys.stdout = c sys.stderr = c c.page() + print p("d"),p("D") c.pos(5, 10) c.write('hi there') print 'some printed output' diff --git a/pyreadline/console/event.py b/pyreadline/console/event.py index 599ff73..ddbfc25 100644 --- a/pyreadline/console/event.py +++ b/pyreadline/console/event.py @@ -19,3 +19,7 @@ class Event(object): else: s = 'unknown event type' return s + + +# def __str__(self): +# return "('%s',%s,%s,%s)"%(self.char,self.key,self.state,self.keyinfo) \ No newline at end of file diff --git a/pyreadline/console/ironpython_console.py b/pyreadline/console/ironpython_console.py index de59eff..40d0f66 100644 --- a/pyreadline/console/ironpython_console.py +++ b/pyreadline/console/ironpython_console.py @@ -12,7 +12,7 @@ # primitive debug printing that won't interfere with the screen import clr -clr.AddReference("IronPythonConsole.exe") +clr.AddReference("ipy.exe") import IronPythonConsole import sys @@ -25,8 +25,8 @@ import System from event import Event from pyreadline.logger import log -print "Codepage",System.Console.InputEncoding.CodePage -from pyreadline.keysyms import make_keysym, make_keyinfo +#print "Codepage",System.Console.InputEncoding.CodePage +from pyreadline.keysyms import make_keysym, make_keyinfo,make_KeyPress color=System.ConsoleColor @@ -61,7 +61,7 @@ class Console(object): self.serial=0 self.attr = System.Console.ForegroundColor self.saveattr = System.Console.ForegroundColor - log('initial attr=%x' % self.attr) + log('initial attr=%s' % self.attr) def __del__(self): '''Cleanup the console when finished.''' @@ -219,6 +219,16 @@ class Console(object): '''Fill Rectangle.''' pass #raise NotImplementedError + x0, y0, x1, y1 = rect + if attr is None: + attr = self.attr + if fill: + rowfill=fill[:1]*abs(x1-x0) + else: + rowfill=' '*abs(x1-x0) + for y in range(y0, y1): + System.Console.SetCursorPosition(x0,y) + self.write_color(rowfill,attr) def scroll(self, rect, dx, dy, attr=None, fill=' '): '''Scroll a rectangle.''' @@ -263,7 +273,7 @@ class Console(object): else: return sc.WindowWidth,sc.WindowHeight - def cursor(self, visible=None, size=None): + def cursor(self, visible=True, size=None): '''Set cursor on or off.''' System.Console.CursorVisible=visible @@ -285,14 +295,14 @@ class event(Event): self.height = 0 self.x = 0 self.y = 0 - self.char = chr(input.KeyChar) + self.char = str(input.KeyChar) self.keycode = input.Key self.state = input.Modifiers self.type="KeyRelease" self.keysym = make_keysym(self.keycode) - self.keyinfo = make_keyinfo(self.keycode, self.state) + self.keyinfo = make_KeyPress(self.char, self.state, self.keycode) def install_readline(hook): @@ -315,7 +325,7 @@ def getconsole(buffer=1): c = Console(buffer) return c -if __name__ == '_zx_main__': +if __name__ == '__main__': import time, sys c = Console(0) sys.stdout = c @@ -333,3 +343,4 @@ if __name__ == '_zx_main__': print e.Key,chr(e.KeyChar),ord(e.KeyChar),e.Modifiers del c +System.Console.Clear() diff --git a/pyreadline/keysyms/__init__.py b/pyreadline/keysyms/__init__.py index 795e9e9..113610b 100644 --- a/pyreadline/keysyms/__init__.py +++ b/pyreadline/keysyms/__init__.py @@ -5,13 +5,12 @@ try: from keysyms import * success=True except ImportError,x: - pass -try: - from ironpython_keysyms import * - success=True -except ImportError,x: - pass + try: + from ironpython_keysyms import * + success=True + except ImportError,x: + pass if not success: raise ImportError("Could not import keysym for local pythonversion",x) \ No newline at end of file diff --git a/pyreadline/keysyms/common.py b/pyreadline/keysyms/common.py new file mode 100644 index 0000000..444afeb --- /dev/null +++ b/pyreadline/keysyms/common.py @@ -0,0 +1,107 @@ +# -*- coding: utf-8 -*- +#***************************************************************************** +# Copyright (C) 2003-2006 Gary Bishop. +# Copyright (C) 2006 Jorgen Stenarson. +# +# Distributed under the terms of the BSD License. The full license is in +# the file COPYING, distributed as part of this software. +#***************************************************************************** +# table for translating virtual keys to X windows key symbols + +validkey =set(['cancel', 'backspace', 'tab', 'clear', + 'return', 'shift_l', 'control_l', 'alt_l', + 'pause', 'caps_lock', 'escape', 'space', + 'prior', 'next', 'end', 'home', + 'left', 'up', 'right', 'down', + 'select', 'print', 'execute', 'snapshot', + 'insert', 'delete', 'help', 'f1', + 'f2', 'f3', 'f4', 'f5', + 'f6', 'f7', 'f8', 'f9', + 'f10', 'f11', 'f12', 'f13', + 'f14', 'f15', 'f16', 'f17', + 'f18', 'f19', 'f20', 'f21', + 'f22', 'f23', 'f24', 'num_lock', + 'scroll_lock', 'vk_apps', 'vk_processkey','vk_attn', + 'vk_crsel', 'vk_exsel', 'vk_ereof', 'vk_play', + 'vk_zoom', 'vk_noname', 'vk_pa1', 'vk_oem_clear', + 'numpad0', 'numpad1', 'numpad2', 'numpad3', + 'numpad4', 'numpad5', 'numpad6', 'numpad7', + 'numpad8', 'numpad9', 'divide', 'multiply', + 'add', 'subtract', 'vk_decimal']) + +class KeyPress(object): + def __init__(self,char="",shift=False,control=False,meta=False,keyname=""): + if control or meta or shift: + char=char.upper() + self.info=dict(char=char, + shift=shift, + control=control, + meta=meta, + keyname=keyname) + + def create(name): + def get(self): + return self.info[name] + def set(self,value): + self.info[name]=value + return property(get,set) + char=create("char") + shift=create("shift") + control=create("control") + meta=create("meta") + keyname=create("keyname") + + def __repr__(self): + return "(%s,%s,%s,%s)"%self.tuple() + + def tuple(self): + if self.keyname: + return (self.control,self.meta,self.shift,self.keyname) + else: + if self.control or self.meta or self.shift: + return (self.control,self.meta,self.shift,self.char.upper()) + else: + return (self.control,self.meta,self.shift,self.char) + +def make_KeyPress_from_keydescr(keydescr): + keyinfo=KeyPress() + + while 1: + lkeyname = keydescr.lower() + if lkeyname.startswith('control-'): + keyinfo.control = True + keydescr = keydescr[8:] + elif lkeyname.startswith('ctrl-'): + keyinfo.control = True + keydescr = keydescr[5:] + elif keydescr.startswith('\\C-'): + keyinfo.control = True + keydescr = keydescr[3:] + elif keydescr.startswith('\\M-'): + keyinfo.meta = True + keydescr = keydescr[3:] + elif keydescr.startswith('\\e-'): + keydescr = "escape"+keydescr[3:] + elif lkeyname.startswith('meta-'): + keyinfo.meta = True + keydescr = keydescr[5:] + elif lkeyname.startswith('alt-'): + keyinfo.meta = True + keydescr = keydescr[4:] + elif lkeyname.startswith('shift-'): + keyinfo.shift = True + keydescr = keydescr[6:] + else: + if len(keydescr) > 1: + if keydescr.strip() in validkey: + keyinfo.keyname=keydescr.strip() + keyinfo.char="" + else: + raise IndexError("Not a valid key: '%s'"%keydescr) + else: + keyinfo.char=keydescr + return keyinfo + +if __name__=="__main__": + import startup + \ No newline at end of file diff --git a/pyreadline/keysyms/ironpython_keysyms.py b/pyreadline/keysyms/ironpython_keysyms.py index e9a9169..d5626bb 100644 --- a/pyreadline/keysyms/ironpython_keysyms.py +++ b/pyreadline/keysyms/ironpython_keysyms.py @@ -7,8 +7,12 @@ # the file COPYING, distributed as part of this software. #***************************************************************************** import System +from common import validkey,KeyPress,make_KeyPress_from_keydescr c32=System.ConsoleKey +Shift=System.ConsoleModifiers.Shift +Control=System.ConsoleModifiers.Control +Alt=System.ConsoleModifiers.Alt # table for translating virtual keys to X windows key symbols code2sym_map = {#c32.CANCEL: 'Cancel', c32.Backspace: 'BackSpace', @@ -184,3 +188,15 @@ def make_keyinfo(keycode, state): meta=False shift=False return (control, meta, shift, keycode) + + +def make_KeyPress(char,state,keycode): + + shift=bool(int(state)&int(Shift)) + control=bool(int(state)&int(Control)) + meta=bool(int(state)&int(Alt)) + keyname=code2sym_map.get(keycode,"").lower() + if control: + char=str(keycode) + return KeyPress(char,shift,control,meta,keyname) + diff --git a/pyreadline/keysyms/keysyms.py b/pyreadline/keysyms/keysyms.py index 6ad9d10..2328001 100644 --- a/pyreadline/keysyms/keysyms.py +++ b/pyreadline/keysyms/keysyms.py @@ -10,175 +10,119 @@ import winconstants as c32 from ctypes import windll import ctypes # table for translating virtual keys to X windows key symbols -code2sym_map = {c32.VK_CANCEL: 'Cancel', - c32.VK_BACK: 'BackSpace', - c32.VK_TAB: 'Tab', - c32.VK_CLEAR: 'Clear', - c32.VK_RETURN: 'Return', - c32.VK_SHIFT:'Shift_L', - c32.VK_CONTROL: 'Control_L', - c32.VK_MENU: 'Alt_L', - c32.VK_PAUSE: 'Pause', - c32.VK_CAPITAL: 'Caps_Lock', - c32.VK_ESCAPE: 'Escape', - c32.VK_SPACE: 'space', - c32.VK_PRIOR: 'Prior', - c32.VK_NEXT: 'Next', - c32.VK_END: 'End', - c32.VK_HOME: 'Home', - c32.VK_LEFT: 'Left', - c32.VK_UP: 'Up', - c32.VK_RIGHT: 'Right', - c32.VK_DOWN: 'Down', - c32.VK_SELECT: 'Select', - c32.VK_PRINT: 'Print', - c32.VK_EXECUTE: 'Execute', - c32.VK_SNAPSHOT: 'Snapshot', - c32.VK_INSERT: 'Insert', - c32.VK_DELETE: 'Delete', - c32.VK_HELP: 'Help', - c32.VK_F1: 'F1', - c32.VK_F2: 'F2', - c32.VK_F3: 'F3', - c32.VK_F4: 'F4', - c32.VK_F5: 'F5', - c32.VK_F6: 'F6', - c32.VK_F7: 'F7', - c32.VK_F8: 'F8', - c32.VK_F9: 'F9', - c32.VK_F10: 'F10', - c32.VK_F11: 'F11', - c32.VK_F12: 'F12', - c32.VK_F13: 'F13', - c32.VK_F14: 'F14', - c32.VK_F15: 'F15', - c32.VK_F16: 'F16', - c32.VK_F17: 'F17', - c32.VK_F18: 'F18', - c32.VK_F19: 'F19', - c32.VK_F20: 'F20', - c32.VK_F21: 'F21', - c32.VK_F22: 'F22', - c32.VK_F23: 'F23', - c32.VK_F24: 'F24', - c32.VK_NUMLOCK: 'Num_Lock,', - c32.VK_SCROLL: 'Scroll_Lock', - c32.VK_APPS: 'VK_APPS', - c32.VK_PROCESSKEY: 'VK_PROCESSKEY', - c32.VK_ATTN: 'VK_ATTN', - c32.VK_CRSEL: 'VK_CRSEL', - c32.VK_EXSEL: 'VK_EXSEL', - c32.VK_EREOF: 'VK_EREOF', - c32.VK_PLAY: 'VK_PLAY', - c32.VK_ZOOM: 'VK_ZOOM', - c32.VK_NONAME: 'VK_NONAME', - c32.VK_PA1: 'VK_PA1', - c32.VK_OEM_CLEAR: 'VK_OEM_CLEAR', - c32.VK_NUMPAD0: 'NUMPAD0', - c32.VK_NUMPAD1: 'NUMPAD1', - c32.VK_NUMPAD2: 'NUMPAD2', - c32.VK_NUMPAD3: 'NUMPAD3', - c32.VK_NUMPAD4: 'NUMPAD4', - c32.VK_NUMPAD5: 'NUMPAD5', - c32.VK_NUMPAD6: 'NUMPAD6', - c32.VK_NUMPAD7: 'NUMPAD7', - c32.VK_NUMPAD8: 'NUMPAD8', - c32.VK_NUMPAD9: 'NUMPAD9', - c32.VK_DIVIDE: 'Divide', - c32.VK_MULTIPLY: 'Multiply', - c32.VK_ADD: 'Add', - c32.VK_SUBTRACT: 'Subtract', - c32.VK_DECIMAL: 'VK_DECIMAL' + +from common import validkey,KeyPress,make_KeyPress_from_keydescr + +code2sym_map = {c32.VK_CANCEL: 'cancel', + c32.VK_BACK: 'backspace', + c32.VK_TAB: 'tab', + c32.VK_CLEAR: 'clear', + c32.VK_RETURN: 'return', + c32.VK_SHIFT: 'shift_l', + c32.VK_CONTROL: 'control_l', + c32.VK_MENU: 'alt_l', + c32.VK_PAUSE: 'pause', + c32.VK_CAPITAL: 'caps_lock', + c32.VK_ESCAPE: 'escape', + c32.VK_SPACE: 'space', + c32.VK_PRIOR: 'prior', + c32.VK_NEXT: 'next', + c32.VK_END: 'end', + c32.VK_HOME: 'home', + c32.VK_LEFT: 'left', + c32.VK_UP: 'up', + c32.VK_RIGHT: 'right', + c32.VK_DOWN: 'down', + c32.VK_SELECT: 'select', + c32.VK_PRINT: 'print', + c32.VK_EXECUTE: 'execute', + c32.VK_SNAPSHOT: 'snapshot', + c32.VK_INSERT: 'insert', + c32.VK_DELETE: 'delete', + c32.VK_HELP: 'help', + c32.VK_F1: 'f1', + c32.VK_F2: 'f2', + c32.VK_F3: 'f3', + c32.VK_F4: 'f4', + c32.VK_F5: 'f5', + c32.VK_F6: 'f6', + c32.VK_F7: 'f7', + c32.VK_F8: 'f8', + c32.VK_F9: 'f9', + c32.VK_F10: 'f10', + c32.VK_F11: 'f11', + c32.VK_F12: 'f12', + c32.VK_F13: 'f13', + c32.VK_F14: 'f14', + c32.VK_F15: 'f15', + c32.VK_F16: 'f16', + c32.VK_F17: 'f17', + c32.VK_F18: 'f18', + c32.VK_F19: 'f19', + c32.VK_F20: 'f20', + c32.VK_F21: 'f21', + c32.VK_F22: 'f22', + c32.VK_F23: 'f23', + c32.VK_F24: 'f24', + c32.VK_NUMLOCK: 'num_lock,', + c32.VK_SCROLL: 'scroll_lock', + c32.VK_APPS: 'vk_apps', + c32.VK_PROCESSKEY: 'vk_processkey', + c32.VK_ATTN: 'vk_attn', + c32.VK_CRSEL: 'vk_crsel', + c32.VK_EXSEL: 'vk_exsel', + c32.VK_EREOF: 'vk_ereof', + c32.VK_PLAY: 'vk_play', + c32.VK_ZOOM: 'vk_zoom', + c32.VK_NONAME: 'vk_noname', + c32.VK_PA1: 'vk_pa1', + c32.VK_OEM_CLEAR :'vk_oem_clear', + c32.VK_NUMPAD0: 'numpad0', + c32.VK_NUMPAD1: 'numpad1', + c32.VK_NUMPAD2: 'numpad2', + c32.VK_NUMPAD3: 'numpad3', + c32.VK_NUMPAD4: 'numpad4', + c32.VK_NUMPAD5: 'numpad5', + c32.VK_NUMPAD6: 'numpad6', + c32.VK_NUMPAD7: 'numpad7', + c32.VK_NUMPAD8: 'numpad8', + c32.VK_NUMPAD9: 'numpad9', + c32.VK_DIVIDE: 'divide', + c32.VK_MULTIPLY: 'multiply', + c32.VK_ADD: 'add', + c32.VK_SUBTRACT: 'subtract', + c32.VK_DECIMAL: 'vk_decimal' } -# function to handle the mapping -def make_keysym(keycode): - try: - sym = code2sym_map[keycode] - except KeyError: - sym = '' - return sym - -sym2code_map = {} -for code,sym in code2sym_map.iteritems(): - sym2code_map[sym.lower()] = code - -def key_text_to_keyinfo(keytext): - '''Convert a GNU readline style textual description of a key to keycode with modifiers''' - if keytext.startswith('"'): # " - return keyseq_to_keyinfo(keytext[1:-1]) - else: - return keyname_to_keyinfo(keytext) - VkKeyScan = windll.user32.VkKeyScanA def char_to_keyinfo(char, control=False, meta=False, shift=False): + k=KeyPress() vk = VkKeyScan(ord(char)) if vk & 0xffff == 0xffff: print 'VkKeyScan("%s") = %x' % (char, vk) raise ValueError, 'bad key' if vk & 0x100: - shift = True + k.shift = True if vk & 0x200: - control = True + k.control = True if vk & 0x400: - meta = True - return (control, meta, shift, vk & 0xff) + k.meta = True + k.char=chr(vk & 0xff) + return k -def keyname_to_keyinfo(keyname): - control = False - meta = False - shift = False - - while 1: - lkeyname = keyname.lower() - if lkeyname.startswith('control-'): - control = True - keyname = keyname[8:] - elif lkeyname.startswith('ctrl-'): - control = True - keyname = keyname[5:] - elif lkeyname.startswith('meta-'): - meta = True - keyname = keyname[5:] - elif lkeyname.startswith('alt-'): - meta = True - keyname = keyname[4:] - elif lkeyname.startswith('shift-'): - shift = True - keyname = keyname[6:] - else: - if len(keyname) > 1: - return (control, meta, shift, sym2code_map.get(keyname.lower()," ")) - else: - return char_to_keyinfo(keyname, control, meta, shift) - -def keyseq_to_keyinfo(keyseq): - res = [] - control = False - meta = False - shift = False - - while 1: - if keyseq.startswith('\\C-'): - control = True - keyseq = keyseq[3:] - elif keyseq.startswith('\\M-'): - meta = True - keyseq = keyseq[3:] - elif keyseq.startswith('\\e'): - res.append(char_to_keyinfo('\033', control, meta, shift)) - control = meta = shift = False - keyseq = keyseq[2:] - elif len(keyseq) >= 1: - res.append(char_to_keyinfo(keyseq[0], control, meta, shift)) - control = meta = shift = False - keyseq = keyseq[1:] - else: - return res[0] - -def make_keyinfo(keycode, state): +def make_KeyPress(char,state,keycode): control = (state & (4+8)) != 0 meta = (state & (1+2)) != 0 shift = (state & 0x10) != 0 - return (control, meta, shift, keycode) + if control: + char = chr(VkKeyScan(ord(char)) & 0xff) + try: + keyname=code2sym_map[keycode] + except KeyError: + keyname="" + return KeyPress(char,shift,control,meta,keyname) + +if __name__=="__main__": + import startup + \ No newline at end of file diff --git a/pyreadline/modes/basemode.py b/pyreadline/modes/basemode.py index 3d783f8..f3dde03 100644 --- a/pyreadline/modes/basemode.py +++ b/pyreadline/modes/basemode.py @@ -9,7 +9,7 @@ import os,re,math,glob import pyreadline.logger as logger from pyreadline.logger import log -from pyreadline.keysyms import key_text_to_keyinfo +from pyreadline.keysyms.common import make_KeyPress_from_keydescr import pyreadline.lineeditor.lineobj as lineobj import pyreadline.lineeditor.history as history import pyreadline.clipboard as clipboard @@ -75,13 +75,12 @@ class BaseMode(object): def _bind_key(self, key, func): '''setup the mapping from key to call the function.''' - keyinfo = key_text_to_keyinfo(key) -# print key,keyinfo,func.__name__ + keyinfo = make_KeyPress_from_keydescr(key.lower()).tuple() self.key_dispatch[keyinfo] = func def _bind_exit_key(self, key): '''setup the mapping from key to call the function.''' - keyinfo = key_text_to_keyinfo(key) + keyinfo = make_KeyPress_from_keydescr(key.lower()).tuple() self.exit_dispatch[keyinfo] = None def init_editing_mode(self, e): # (C-e) diff --git a/pyreadline/modes/emacs.py b/pyreadline/modes/emacs.py index 5f1fdef..38ebe16 100644 --- a/pyreadline/modes/emacs.py +++ b/pyreadline/modes/emacs.py @@ -9,13 +9,11 @@ import os import pyreadline.logger as logger from pyreadline.logger import log -from pyreadline.keysyms import key_text_to_keyinfo import pyreadline.lineeditor.lineobj as lineobj import pyreadline.lineeditor.history as history import basemode - class EmacsMode(basemode.BaseMode): mode="emacs" def __init__(self,rlobj): @@ -41,12 +39,12 @@ class EmacsMode(basemode.BaseMode): event.keyinfo = (control, True, shift, code) #Process exit keys. Only exit on empty line - if event.keyinfo in self.exit_dispatch: + if event.keyinfo.tuple() in self.exit_dispatch: if lineobj.EndOfLine(self.l_buffer) == 0: raise EOFError - - dispatch_func = self.key_dispatch.get(event.keyinfo,self.self_insert) - log("readline from keyboard:%s"%(event.keyinfo,)) + + dispatch_func = self.key_dispatch.get(event.keyinfo.tuple(),self.self_insert) + log("readline from keyboard:%s"%(event.keyinfo.tuple(),)) r = None if dispatch_func: r = dispatch_func(event) @@ -482,18 +480,6 @@ class EmacsMode(basemode.BaseMode): #Create key bindings: - def _bind_key(self, key, func): - '''setup the mapping from key to call the function.''' -# print key,func - keyinfo = key_text_to_keyinfo(key) -# print key,keyinfo,func.__name__ - self.key_dispatch[keyinfo] = func - - def _bind_exit_key(self, key): - '''setup the mapping from key to call the function.''' - keyinfo = key_text_to_keyinfo(key) - self.exit_dispatch[keyinfo] = None - def init_editing_mode(self, e): # (C-e) '''When in vi command mode, this causes a switch to emacs editing mode.''' diff --git a/pyreadline/modes/notemacs.py b/pyreadline/modes/notemacs.py index a6d5b0a..539e3a0 100644 --- a/pyreadline/modes/notemacs.py +++ b/pyreadline/modes/notemacs.py @@ -9,7 +9,6 @@ import os import pyreadline.logger as logger from pyreadline.logger import log -from pyreadline.keysyms import key_text_to_keyinfo import pyreadline.lineeditor.lineobj as lineobj import pyreadline.lineeditor.history as history import basemode @@ -564,17 +563,6 @@ class NotEmacsMode(basemode.BaseMode): #Create key bindings: - def _bind_key(self, key, func): - '''setup the mapping from key to call the function.''' - keyinfo = key_text_to_keyinfo(key) -# print key,keyinfo,func.__name__ - self.key_dispatch[keyinfo] = func - - def _bind_exit_key(self, key): - '''setup the mapping from key to call the function.''' - keyinfo = key_text_to_keyinfo(key) - self.exit_dispatch[keyinfo] = None - def init_editing_mode(self, e): # (C-e) '''When in vi command mode, this causes a switch to emacs editing mode.''' diff --git a/pyreadline/modes/vi.py b/pyreadline/modes/vi.py index a828bb9..f247f81 100644 --- a/pyreadline/modes/vi.py +++ b/pyreadline/modes/vi.py @@ -10,7 +10,6 @@ import os import pyreadline.logger as logger from pyreadline.logger import log -from pyreadline.keysyms import key_text_to_keyinfo import pyreadline.lineeditor.lineobj as lineobj import pyreadline.lineeditor.history as history import basemode @@ -113,7 +112,7 @@ class ViMode(basemode.BaseMode): self.vi_set_insert_mode (True) # make ' ' to ~ self insert for c in range(ord(' '), 127): - self._bind_key('"%s"' % chr(c), self.vi_key) + self._bind_key('%s' % chr(c), self.vi_key) self._bind_key('BackSpace', self.vi_backspace) self._bind_key('Escape', self.vi_escape) self._bind_key('Return', self.vi_accept_line) diff --git a/pyreadline/rlmain.py b/pyreadline/rlmain.py index 16e1793..612d6fc 100644 --- a/pyreadline/rlmain.py +++ b/pyreadline/rlmain.py @@ -12,7 +12,7 @@ import string import math import sys from glob import glob -import os +import os,pdb import re import traceback import operator @@ -20,7 +20,7 @@ import exceptions import clipboard,logger,console from logger import log -from keysyms import key_text_to_keyinfo +from pyreadline.keysyms.common import make_KeyPress_from_keydescr import pyreadline.lineeditor.lineobj as lineobj import pyreadline.lineeditor.history as history @@ -251,11 +251,16 @@ class Readline(object): out.append("%-20s: %s"%("bell_style",self.bell_style)) out.append("%-20s: %s"%("mark_directories",self.mark_directories)) out.append("------------- key bindings ------------") - out.append("%7s %7s %7s %7s %7s %7s"%("Control","Meta","Shift","Keycode","Character","Function")) - bindings=[(k[0],k[1],k[2],k[3],repr(chr(k[3])),v.__name__)for k,v in self.key_dispatch.iteritems()] + tablepat="%-7s %-7s %-7s %-15s %-15s " + out.append(tablepat%("Control","Meta","Shift","Keycode/char","Function")) + bindings=[(k[0],k[1],k[2],k[3],v.__name__)for k,v in self.mode.key_dispatch.iteritems()] + #print self.mode.key_dispatch + #bindings=[str(v) for k,v in self.mode.key_dispatch.iteritems()] bindings.sort() for key in bindings: - out.append("%7s %7s %7s %7d %7s %7s"%(key)) + pass + # out.append(str(key)) + out.append(tablepat%(key)) return out def _bell(self): @@ -331,18 +336,19 @@ class Readline(object): def setmode(name): self.mode=modes[name] def bind_key(key,name): + # print "bind",key,name if hasattr(modes[mode],name): - #print key,name + # print "can bind",key,name modes[mode]._bind_key(key,getattr(modes[mode],name)) def un_bind_key(key): - keyinfo = key_text_to_keyinfo(key) + keyinfo = make_KeyPress_from_keydescr(key).tuple() if keyinfo in modes[mode].key_dispatch: del modes[mode].key_dispatch[keyinfo] def bind_exit_key(key): modes[mode]._bind_exit_key(key) def un_bind_exit_key(key): - keyinfo = key_text_to_keyinfo(key) + keyinfo = make_KeyPress_from_keydescr(key).tuple() if keyinfo in modes[mode].exit_dispatch: del modes[mode].exit_dispatch[keyinfo] @@ -403,6 +409,7 @@ def CTRL(c): # create a Readline object to contain the state rl = Readline() + def GetOutputFile(): '''Return the console object used by readline so that it can be used for printing in color.''' return rl.console