Serge <[EMAIL PROTECTED]> writes:

> 1. Code window in eric does not accept cyrillic characters
> 2. It also does not display properly cyrillic UTF-8 characters

Please try the attached patch. It implements PEP 263 for Eric3,
putting the QScintilla widget into UTF-8 mode.

Notice that you will need to add an encoding declaration to your
files, e.g.

# -*- coding: utf-8 -*-

or

# -*- coding: koi8-r -*-

depending on what encoding you use in your files; if you have an UTF-8
BOM in the file, no further declaration is necessary.

Regards,
Martin

diff -ur eric/Editor.py /usr/lib/python/site-packages/eric3/Editor.py
--- eric/Editor.py	2002-12-13 13:52:23.000000000 +0100
+++ /usr/lib/python/site-packages/eric3/Editor.py	2002-12-19 15:18:14.000000000 +0100
@@ -7,8 +7,55 @@
 from EditorIcons import *
 from Icons import IconClose
 import Preferences
+import re
 
-
+coding_re = re.compile(r"coding[:=]\s*([-\w_.]+)")
+UTF8_BOM = '\xef\xbb\xbf'
+def get_coding(text):
+    for l in text.splitlines()[:2]:
+        m = coding_re.search(l)
+        if m:
+            return m.group(1)
+    return None
+
+def decode(text):
+    try:
+        if text.startswith(UTF8_BOM):
+            # UTF-8 with BOM
+            return unicode(text[3:], 'utf-8'), 'utf-8-bom'
+        coding = get_coding(text)
+        QMessageBox.warning(None, "hallo", coding)
+        if coding:
+            return unicode(text, coding), coding
+    except (UnicodeError, LookupError):
+        pass
+    # Assume Latin-1
+    return unicode(text, "latin-1"), 'latin-1-guessed'
+
+def encode(text, orig_coding):
+    if orig_coding == 'utf-8-bom':
+        return UTF8_BOM + text.encode("utf-8")
+    # Try saving as ASCII
+    try:
+        return text.encode('ascii')
+    except UnicodeError:
+        pass
+    # Try declared coding spec
+    coding = get_coding(self, text)
+    try:
+        if coding:
+            return text.encode(coding)
+    except (UnicodeError, LookupError):
+        # Error: Declared encoding is incorrect
+        pass
+    try:
+        if orig_coding == 'latin-1-guessed':
+            return text.encode('latin-1')
+    except UnicodeError:
+        pass
+    # Save as UTF-8 without BOM
+    return text.encode('utf-8')
+    
 class Editor(QextScintilla):
     """Editor(self,dbs,fn=None,parent=None,name=None,flags=0)
     
@@ -21,6 +68,7 @@
     """
     def __init__(self,dbs,fn=None,parent=None,name=None,flags=0):
         QextScintilla.__init__(self, parent, name, flags)
+        self.setUtf8(1)
         
         self.dbs = dbs
         self.fileName = fn
@@ -36,6 +84,7 @@
         self.breaks = {}    # key: marker handle, value: (lineno, condition)
         self.condHistory = QStringList()
         self.lexer = None
+        self.encoding = None
         
         self.connect(self, SIGNAL('modificationChanged(bool)'), 
                     self.handleModificationChanged)
@@ -89,10 +138,10 @@
                 
             QApplication.setOverrideCursor(Qt.waitCursor)
             
-            txt = f.read()
+            txt, self.encoding = decode(f.read())
             if not Preferences.getEditor("TabForIndentation"):
                 txt = txt.expandtabs(Preferences.getEditor("TabWidth"))
-            
+
             self.setText(txt)
             self.gotoLine(0)
             
@@ -374,7 +423,7 @@
         Public slot to write the text to file fn
         """
         fn = str(fn)
-        txt = str(self.text())
+        txt = encode(unicode(self.text()), self.encoding)
         # work around glitch in scintilla: always make sure, 
         # that the last line is terminated properly
         m = self.eolMode()

Reply via email to