mirror of
https://github.com/wassname/pyreadline.git
synced 2026-08-11 11:23:56 +08:00
pyreadline: more fixes to unicode
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
2007-11-09 Jörgen Stenarson <jorgen.stenarson -at- bostream.nu>
|
||||
* More fixes to unicode handling.
|
||||
|
||||
2007-11-09 Jörgen Stenarson <jorgen.stenarson -at- bostream.nu>
|
||||
* 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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)<ord("A"):
|
||||
chr="?"
|
||||
s = u"%s char='%s'%d keysym='%s' keycode=%d:%x state=%x keyinfo=%s" % \
|
||||
(self.type, chr, ord(self.char), self.keysym, self.keycode, self.keycode,
|
||||
self.state, self.keyinfo)
|
||||
elif self.type in ['Motion', 'Button']:
|
||||
s = '%s x=%d y=%d state=%x' % (self.type, self.x, self.y, self.state)
|
||||
s = u'%s x=%d y=%d state=%x' % (self.type, self.x, self.y, self.state)
|
||||
elif self.type == 'Configure':
|
||||
s = '%s w=%d h=%d' % (self.type, self.width, self.height)
|
||||
s = u'%s w=%d h=%d' % (self.type, self.width, self.height)
|
||||
elif self.type in ['FocusIn', 'FocusOut']:
|
||||
s = self.type
|
||||
elif self.type == 'Menu':
|
||||
s = '%s state=%x' % (self.type, self.state)
|
||||
s = u'%s state=%x' % (self.type, self.state)
|
||||
else:
|
||||
s = 'unknown event type'
|
||||
s = u'unknown event type'
|
||||
return s
|
||||
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ try:
|
||||
except NameError:
|
||||
from sets import Set as set
|
||||
|
||||
|
||||
from pyreadline.unicode_helper import ensure_unicode
|
||||
|
||||
validkey =set(['cancel', 'backspace', 'tab', 'clear',
|
||||
'return', 'shift_l', 'control_l', 'alt_l',
|
||||
@@ -61,7 +61,7 @@ class KeyPress(object):
|
||||
keyname=create("keyname")
|
||||
|
||||
def __repr__(self):
|
||||
return "(%s,%s,%s,%s)"%self.tuple()
|
||||
return u"(%s,%s,%s,%s)"%tuple(map(ensure_unicode,self.tuple()))
|
||||
|
||||
def tuple(self):
|
||||
if self.keyname:
|
||||
|
||||
@@ -125,7 +125,6 @@ class LineHistory(object):
|
||||
startpos=self.history_cursor
|
||||
if _ignore_leading_spaces:
|
||||
res=[(idx,line.lstrip()) for idx,line in enumerate(self.history[startpos:0:-1]) if line.lstrip().startswith(searchfor.lstrip())]
|
||||
logger.log_sock(res)
|
||||
else:
|
||||
res=[(idx,line) for idx,line in enumerate(self.history[startpos:0:-1]) if line.startswith(searchfor)]
|
||||
if res:
|
||||
@@ -156,7 +155,6 @@ class LineHistory(object):
|
||||
pyreadline.rl._clear_after()
|
||||
|
||||
event = c.getkeypress()
|
||||
log_sock(str(event),"history")
|
||||
|
||||
if event.keyinfo.keyname == 'backspace':
|
||||
if len(query) > 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): # ()
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user