Update of /cvsroot/pywin32/pywin32/Pythonwin/pywin/scintilla
In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv4261/Pythonwin/pywin/scintilla
Modified Files:
Tag: py3k
IDLEenvironment.py bindings.py control.py document.py view.py
Log Message:
merge lots of changes (most via 2to3) from the trunk
Index: control.py
===================================================================
RCS file: /cvsroot/pywin32/pywin32/Pythonwin/pywin/scintilla/control.py,v
retrieving revision 1.17.2.4
retrieving revision 1.17.2.5
diff -C2 -d -r1.17.2.4 -r1.17.2.5
*** control.py 3 Jan 2009 04:45:17 -0000 1.17.2.4
--- control.py 5 Jan 2009 12:51:26 -0000 1.17.2.5
***************
*** 201,213 ****
def SCICallTipShow(self, text, pos=-1):
if pos==-1: pos = self.GetSel()[0]
- """
- if isinstance(text, str):
- # I'm really not sure what the correct encoding
- # to use is - but it has gotta be better than total
- # failure due to the array module
- text = text.encode("mbcs")
- """
buff = (text + "\0").encode(default_scintilla_encoding)
- ## addressBuffer = buff.buffer_info()[0]
self.SendScintilla(scintillacon.SCI_CALLTIPSHOW, pos, buff)
def SCICallTipCancel(self):
--- 201,205 ----
Index: view.py
===================================================================
RCS file: /cvsroot/pywin32/pywin32/Pythonwin/pywin/scintilla/view.py,v
retrieving revision 1.28.2.5
retrieving revision 1.28.2.6
diff -C2 -d -r1.28.2.5 -r1.28.2.6
*** view.py 27 Nov 2008 11:31:03 -0000 1.28.2.5
--- view.py 5 Jan 2009 12:51:26 -0000 1.28.2.6
***************
*** 387,396 ****
def SaveTextFile(self, filename):
doc = self.GetDocument()
! s = self.GetTextRange()
! # Save in binary mode so line endings are not translated.
! # Edit control uses '\r\n', and universal newlines mode replaces ALL '\r' with '\r\n'.
! f = open(filename, 'wb')
! f.write(s.encode('mbcs'))
! f.close()
doc.SetModifiedFlag(0)
return 1
--- 387,398 ----
def SaveTextFile(self, filename):
doc = self.GetDocument()
! # Open in binary mode as scintilla itself ensures the
! # line endings are already appropriate, and our doc save
! # method handles encoding, BOMs, etc.
! f = open(filename, 'wb')
! try:
! doc._SaveTextToFile(self, f)
! finally:
! f.close()
doc.SetModifiedFlag(0)
return 1
***************
*** 652,657 ****
hdcFormat = dc.GetHandleAttrib()
fr = struct.pack(fmt, hdcRender, hdcFormat, rc[0], rc[1], rc[2], rc[3], rc[0], rc[1], rc[2], rc[3], pageStart, lengthDoc)
- ## frBuff = array......array('b', fr)
- ## addressFrBuff = frBuff.buffer_info()[0]
nextPageStart = self.SendScintilla(EM_FORMATRANGE, draw, fr)
return nextPageStart
--- 654,657 ----
Index: bindings.py
===================================================================
RCS file: /cvsroot/pywin32/pywin32/Pythonwin/pywin/scintilla/bindings.py,v
retrieving revision 1.5.2.4
retrieving revision 1.5.2.5
diff -C2 -d -r1.5.2.4 -r1.5.2.5
*** bindings.py 11 Dec 2008 05:45:21 -0000 1.5.2.4
--- bindings.py 5 Jan 2009 12:51:26 -0000 1.5.2.5
***************
*** 148,155 ****
rc = 1
except:
- traceback.print_exc(chain=False)
message = "Firing event '%s' failed." % event
print(message)
!
self.report_error(message)
rc = 1 # Let any default handlers have a go!
--- 148,154 ----
rc = 1
except:
message = "Firing event '%s' failed." % event
print(message)
! traceback.print_exc()
self.report_error(message)
rc = 1 # Let any default handlers have a go!
Index: IDLEenvironment.py
===================================================================
RCS file: /cvsroot/pywin32/pywin32/Pythonwin/pywin/scintilla/IDLEenvironment.py,v
retrieving revision 1.11.2.3
retrieving revision 1.11.2.4
diff -C2 -d -r1.11.2.3 -r1.11.2.4
*** IDLEenvironment.py 27 Nov 2008 11:31:03 -0000 1.11.2.3
--- IDLEenvironment.py 5 Jan 2009 12:51:26 -0000 1.11.2.4
***************
*** 356,363 ****
raise TextError("Empty range")
self.edit.SetSel((pos, pos))
- self.edit.SCIAddText(text)
-
- """
-
# IDLE only deals with "\n" - we will be nicer
--- 356,359 ----
***************
*** 367,372 ****
self.edit.SCINewline()
self.edit.SCIAddText(bit)
- """
-
def delete(self, start, end=None):
--- 363,366 ----
Index: document.py
===================================================================
RCS file: /cvsroot/pywin32/pywin32/Pythonwin/pywin/scintilla/document.py,v
retrieving revision 1.6.4.4
retrieving revision 1.6.4.5
diff -C2 -d -r1.6.4.4 -r1.6.4.5
*** document.py 8 Sep 2008 04:08:27 -0000 1.6.4.4
--- document.py 5 Jan 2009 12:51:26 -0000 1.6.4.5
***************
*** 1,13 ****
import win32ui
from pywin.mfc import docview
! from pywin import is_platform_unicode, default_platform_encoding, default_scintilla_encoding
from . import scintillacon
import win32con
import string
import os
ParentScintillaDocument=docview.Document
class CScintillaDocument(ParentScintillaDocument):
"A SyntEdit document. "
def DeleteContents(self):
pass
--- 1,24 ----
import win32ui
from pywin.mfc import docview
! from pywin import default_scintilla_encoding
from . import scintillacon
import win32con
import string
import os
+ import codecs
+
+ crlf_bytes = "\r\n".encode("ascii")
+ lf_bytes = "\n".encode("ascii")
ParentScintillaDocument=docview.Document
class CScintillaDocument(ParentScintillaDocument):
"A SyntEdit document. "
+ def __init__(self, *args):
+ self.bom = None # the BOM, if any, read from the file.
+ # the encoding we detected from the source. Might have
+ # detected via the BOM or an encoding decl.
+ self.source_encoding = None
+ ParentScintillaDocument.__init__(self, *args)
+
def DeleteContents(self):
pass
***************
*** 15,24 ****
def OnOpenDocument(self, filename):
# init data members
! # print ("CScintillaDocument.OnOpenDocument", filename)
self.SetPathName(filename) # Must set this early!
try:
! f = open(filename, 'r')
try:
! text = f.read()
finally:
f.close()
--- 26,37 ----
def OnOpenDocument(self, filename):
# init data members
! #print "Opening", filename
self.SetPathName(filename) # Must set this early!
try:
! # load the text as binary we can get smart
! # about detecting any existing EOL conventions.
! f = open(filename, 'rb')
try:
! self._LoadTextFromFile(f)
finally:
f.close()
***************
*** 27,31 ****
return 0
- self._SetLoadedText(text)
return 1
--- 40,43 ----
***************
*** 43,57 ****
# File related functions
# Helper to transfer text from the MFC document to the control.
! def _SetLoadedText(self, text):
! # In universal newlines mode, line endings read from file are translated to '\n',
! # but edit control expects CR/LF ('\r\n').
! # Might be simpler to just tell scintilla to use '\n'
! # SendScintilla(scintillacon.SCI_SETEOLMODE, scintillacon.SC_EOL_LF)
! # and have eols automatically translated back when written to file
! if os.linesep != '\n':
! text=text.replace('\n', os.linesep)
! # Translate from unicode to UTF-8 bytes for Scintilla
! char_text = text.encode(default_scintilla_encoding)
view = self.GetFirstView()
--- 55,110 ----
# File related functions
# Helper to transfer text from the MFC document to the control.
! def _LoadTextFromFile(self, f):
! # detect EOL mode - we don't support \r only - so find the
! # first '\n' and guess based on the char before.
! l = f.readline()
! # If line ends with \r\n or has no line ending, use CRLF.
! if l.endswith(crlf_bytes) or not l.endswith(lf_bytes):
! eol_mode = scintillacon.SC_EOL_CRLF
! else:
! eol_mode = scintillacon.SC_EOL_LF
! # Detect the encoding.
! # XXX - todo - support pep263 encoding declarations as well as
! # the BOM detection here (but note that unlike our BOM, the
! # encoding declaration could change between loading and saving
! # - particularly with a new file - so it also needs to be
! # implemented at save time.)
! for bom, encoding in (
! (codecs.BOM_UTF8, "utf8"),
! (codecs.BOM_UTF16_LE, "utf_16_le"),
! (codecs.BOM_UTF16_BE, "utf_16_be"),
! ):
! if l.startswith(bom):
! self.bom = bom
! self.source_encoding = encoding
! l = l[len(bom):] # remove it.
! break
!
! # reading by lines would be too slow? Maybe we can use the
! # incremental encoders? For now just stick with loading the
! # entire file in memory.
! text = l + f.read()
!
! # Translate from source encoding to UTF-8 bytes for Scintilla
! source_encoding = self.source_encoding
! # This latin1 sucks until we get pep263 support; if we don't
! # know an encoding we just write as binary (maybe we should
! # try ascii to let the 'decoding failed' handling below to
! # provide a nice warning that the file is non-ascii)
! if source_encoding is None:
! source_encoding = 'latin1'
! # we could optimize this by avoiding utf8 to-ing and from-ing,
! # but then we would lose the ability to handle invalid utf8
! # (and even then, the use of encoding aliases makes this tricky)
! # To create an invalid utf8 file:
! # >>> open(filename, "wb").write(codecs.BOM_UTF8+"bad \xa9har\r\n")
! try:
! dec = text.decode(source_encoding)
! except UnicodeError:
! print("WARNING: Failed to decode bytes from %r encoding - treating as latin1" % source_encoding)
! dec = text.decode('latin1')
! # and put it back as utf8 - this shouldn't fail.
! text = dec.encode(default_scintilla_encoding)
view = self.GetFirstView()
***************
*** 62,68 ****
view.SetReadOnly(0)
view.SendScintilla(scintillacon.SCI_CLEARALL)
! view.SendMessage(scintillacon.SCI_ADDTEXT, char_text)
view.SendScintilla(scintillacon.SCI_SETUNDOCOLLECTION, 1, 0)
view.SendScintilla(win32con.EM_EMPTYUNDOBUFFER, 0, 0)
def FinalizeViewCreation(self, view):
--- 115,135 ----
view.SetReadOnly(0)
view.SendScintilla(scintillacon.SCI_CLEARALL)
! view.SendMessage(scintillacon.SCI_ADDTEXT, text)
view.SendScintilla(scintillacon.SCI_SETUNDOCOLLECTION, 1, 0)
view.SendScintilla(win32con.EM_EMPTYUNDOBUFFER, 0, 0)
+ # set EOL mode
+ view.SendScintilla(scintillacon.SCI_SETEOLMODE, eol_mode)
+
+ def _SaveTextToFile(self, view, f):
+ s = view.GetTextRange() # already decoded from scintilla's encoding
+ if self.bom:
+ f.write(self.bom)
+ source_encoding = self.source_encoding
+ if source_encoding is None:
+ source_encoding = 'latin1'
+
+ f.write(s.encode(source_encoding))
+ self.SetModifiedFlag(0)
+
def FinalizeViewCreation(self, view):
|