From 0716080c10d58614d618e66407760cb6d9bda666 Mon Sep 17 00:00:00 2001 From: jstenar <> Date: Sat, 10 Nov 2007 00:13:26 +0000 Subject: [PATCH] pyreadline: more fixes to unicode --- doc/ChangeLog | 3 +++ pyreadline/console/console.py | 7 +++---- pyreadline/console/event.py | 15 +++++++++------ pyreadline/keysyms/common.py | 4 ++-- pyreadline/lineeditor/history.py | 6 ------ pyreadline/lineeditor/lineobj.py | 9 ++++++++- pyreadline/logger.py | 5 +++-- pyreadline/modes/emacs.py | 10 +++++----- pyreadline/rlmain.py | 7 +++++++ pyreadline/unicode_helper.py | 2 +- 10 files changed, 41 insertions(+), 27 deletions(-) diff --git a/doc/ChangeLog b/doc/ChangeLog index ed111cf..80c6a5f 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,6 @@ +2007-11-09 Jörgen Stenarson + * More fixes to unicode handling. + 2007-11-09 Jörgen Stenarson * Fixes to make clipboard play nice with unicode. Quick that treats the clipboard as being str with pyreadline_encoding data. Thus convert to unicode when pasting and diff --git a/pyreadline/console/console.py b/pyreadline/console/console.py index 577e4f6..0df20cb 100644 --- a/pyreadline/console/console.py +++ b/pyreadline/console/console.py @@ -432,7 +432,6 @@ class Console(object): 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: @@ -495,7 +494,7 @@ class Console(object): status = self.ReadConsoleInputW(self.hin, byref(Cevent), 1, byref(count)) if status and count.value == 1: e = event(self, Cevent) - log_sock(unicode(e.keyinfo),"keypress") + log_sock(ensure_unicode(e.keyinfo),"keypress") return e def getkeypress(self): @@ -533,7 +532,6 @@ class Console(object): Cevent = INPUT_RECORD() count = c_int(0) status = self.PeekConsoleInputW(self.hin, byref(Cevent), 1, byref(count)) - log_sock("%s %s %s"%(status,count,Cevent)) if status and count == 1: return event(self, Cevent) @@ -753,5 +751,6 @@ if __name__ == '__main__': c.write('hi there') print 'some printed output' for i in range(10): - c.getkeypress() + q=c.getkeypress() + print q del c diff --git a/pyreadline/console/event.py b/pyreadline/console/event.py index ddbfc25..ce3d4b6 100644 --- a/pyreadline/console/event.py +++ b/pyreadline/console/event.py @@ -5,19 +5,22 @@ class Event(object): def __repr__(self): '''Display an event for debugging.''' if self.type in ['KeyPress', 'KeyRelease']: - s = "%s char='%s'%d keysym='%s' keycode=%d:%x state=%x keyinfo=%s" % \ - (self.type, self.char, ord(self.char), self.keysym, self.keycode, self.keycode, + chr=self.char + if ord(chr) 0: @@ -169,7 +167,6 @@ class LineHistory(object): break else: pyreadline.rl._bell() - log_sock(query,"history") res="" if query: if direction==-1: @@ -177,7 +174,6 @@ class LineHistory(object): else: res=self.forward_search_history(query) - log_sock(res,"history") return lineobj.ReadLineTextBuffer(res,point=0) def non_incremental_reverse_search_history(self,current): # (M-p) @@ -198,7 +194,6 @@ class LineHistory(object): 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] @@ -223,7 +218,6 @@ class LineHistory(object): 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 history_search_forward(self,partial): # () diff --git a/pyreadline/lineeditor/lineobj.py b/pyreadline/lineeditor/lineobj.py index f4d02f8..e7c3486 100644 --- a/pyreadline/lineeditor/lineobj.py +++ b/pyreadline/lineeditor/lineobj.py @@ -11,6 +11,10 @@ import wordmatcher import pyreadline.clipboard as clipboard from pyreadline.logger import log,log_sock from pyreadline.unicode_helper import ensure_unicode + +kill_ring_to_clipboard=False #set to true to copy every addition to kill ring to clipboard + + class NotAWordError(IndexError): pass @@ -397,6 +401,7 @@ class ReadLineTextBuffer(TextLine): self.selection_mark=-1 self.enable_selection=True self.kill_ring=[] + def __repr__(self): return 'ReadLineTextBuffer("%s",point=%s,mark=%s,selection_mark=%s)'%(self.line_buffer,self.point,self.mark,self.selection_mark) @@ -741,7 +746,9 @@ class ReadLineTextBuffer(TextLine): ############## Kill ring def add_to_kill_ring(self,txt): self.kill_ring=[txt] - + if kill_ring_to_clipboard: + clipboard.SetClipboardText(txt.get_line_text()) + def paste_from_kill_ring(self): if self.kill_ring: diff --git a/pyreadline/logger.py b/pyreadline/logger.py index cb5d46f..a3283ea 100644 --- a/pyreadline/logger.py +++ b/pyreadline/logger.py @@ -7,6 +7,7 @@ #***************************************************************************** import socket +from pyreadline.unicode_helper import ensure_text _logfile=False def start_log(on,filename): @@ -36,9 +37,9 @@ def log_sock(s,event_type=None): pass else: if event_type is None: - logsocket.sendto(str(s),(host,port)) + logsocket.sendto(ensure_text(s),(host,port)) elif event_type in show_event: - logsocket.sendto(str(s),(host,port)) + logsocket.sendto(ensure_text(s),(host,port)) else: pass diff --git a/pyreadline/modes/emacs.py b/pyreadline/modes/emacs.py index 4768fdb..b29a7b8 100644 --- a/pyreadline/modes/emacs.py +++ b/pyreadline/modes/emacs.py @@ -14,6 +14,8 @@ import pyreadline.lineeditor.lineobj as lineobj import pyreadline.lineeditor.history as history import basemode import string +from pyreadline.unicode_helper import ensure_unicode + def format(keyinfo): if len(keyinfo[-1])!=1: k=keyinfo+(-1,) @@ -45,9 +47,10 @@ class EmacsMode(basemode.BaseMode): while 1: self._update_line() lbuf=self.l_buffer - log_sock("point:%s mark:%s selection_mark:%s"%(lbuf.point,lbuf.mark,lbuf.selection_mark)) + 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 @@ -81,7 +84,7 @@ class EmacsMode(basemode.BaseMode): 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__)).encode(sys.stdout.encoding),"bound_function") + log_sock((u"%s|%s"%(ensure_unicode(format(keyinfo)),dispatch_func.__name__)),"bound_function") r = None if dispatch_func: r = dispatch_func(event) @@ -157,7 +160,6 @@ class EmacsMode(basemode.BaseMode): 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): @@ -405,10 +407,8 @@ class EmacsMode(basemode.BaseMode): 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) diff --git a/pyreadline/rlmain.py b/pyreadline/rlmain.py index bbe5dfd..f7683fd 100644 --- a/pyreadline/rlmain.py +++ b/pyreadline/rlmain.py @@ -352,6 +352,12 @@ class Readline(object): if keyinfo in modes[mode].exit_dispatch: del modes[mode].exit_dispatch[keyinfo] + + + def setkill_ring_to_clipboard(killring): + import pyreadline.lineeditor.lineobj + pyreadline.lineeditor.lineobj.kill_ring_to_clipboard=killring + def sethistoryfilename(filename): self._history.history_filename=os.path.expanduser(filename) def setbellstyle(mode): @@ -404,6 +410,7 @@ class Readline(object): "set_input_color":set_input_color, "allow_ctrl_c":allow_ctrl_c, "ctrl_c_tap_time_interval":ctrl_c_tap_time_interval, + "kill_ring_to_clipboard":setkill_ring_to_clipboard, } if os.path.isfile(inputrcpath): try: diff --git a/pyreadline/unicode_helper.py b/pyreadline/unicode_helper.py index 1c8a9ec..75928c6 100644 --- a/pyreadline/unicode_helper.py +++ b/pyreadline/unicode_helper.py @@ -22,6 +22,6 @@ def ensure_unicode(text): def ensure_text(text): """Convert unicode to str using pyreadline_codepage""" - if isinstance(text, str): + if isinstance(text, unicode): return text.encode(pyreadline_codepage, "replace") return text