Below is a proof-of-concept for getting multiple encodings in a LyX
document to work. As before it still relies on the user setting a
suitable language for the text in the different encoding.
It seems to work for me though it's difficult to test due to what I
think is in fact a Red Hat bug.
Please ignore the quality of the code: I would like some comments on the
approach though. Does this seem reasonable ?
regards
john
Index: lyxfunc.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/lyxfunc.C,v
retrieving revision 1.388
diff -u -r1.388 lyxfunc.C
--- lyxfunc.C 18 Dec 2002 14:24:31 -0000 1.388
+++ lyxfunc.C 19 Dec 2002 05:31:59 -0000
@@ -43,6 +43,7 @@
#include "TextCache.h"
#include "lyxfind.h"
#include "undo_funcs.h"
+#include "language.h"
#include "ParagraphParameters.h"
#include "insets/insetcommand.h"
@@ -239,7 +240,7 @@
}
if (action == LFUN_SELFINSERT) {
- char c = keysym->getISOEncoded();
+ char c =
+keysym->getISOEncoded(TEXT(false)->cursor.par()->getFont(view()->buffer()->params,
+(TEXT(false)->cursor.pos())).language()->encoding());
string argument;
// FIXME: why ...
Index: kbsequence.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/kbsequence.C,v
retrieving revision 1.28
diff -u -r1.28 kbsequence.C
--- kbsequence.C 1 Dec 2002 22:59:17 -0000 1.28
+++ kbsequence.C 19 Dec 2002 05:31:59 -0000
@@ -176,7 +176,7 @@
char kb_sequence::getLastKeyEncoded() const
{
- return getsym()->getISOEncoded();
+ return 0; // FIXME return getsym()->getISOEncoded();
}
Index: frontends/LyXKeySym.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/LyXKeySym.h,v
retrieving revision 1.7
diff -u -r1.7 LyXKeySym.h
--- frontends/LyXKeySym.h 12 Dec 2002 13:46:05 -0000 1.7
+++ frontends/LyXKeySym.h 19 Dec 2002 05:32:01 -0000
@@ -15,6 +15,8 @@
#include "LString.h"
#include <boost/shared_ptr.hpp>
+class Encoding;
+
/**
* This is a base class for representing a keypress.
* Each frontend has to implement this to provide
@@ -47,7 +49,7 @@
* This converts the LyXKeySym to a 8-bit encoded character.
* This relies on user to use the right encoding.
*/
- virtual char getISOEncoded() const = 0;
+ virtual char getISOEncoded(Encoding const * enc) const = 0;
};
Index: frontends/qt2/QLyXKeySym.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/qt2/QLyXKeySym.h,v
retrieving revision 1.9
diff -u -r1.9 QLyXKeySym.h
--- frontends/qt2/QLyXKeySym.h 12 Dec 2002 13:46:06 -0000 1.9
+++ frontends/qt2/QLyXKeySym.h 19 Dec 2002 05:32:01 -0000
@@ -23,6 +23,7 @@
#include <qstring.h>
class QKeyEvent;
+class Encoding;
/**
* Qt-specific key press.
@@ -58,7 +59,7 @@
* This converts the LyXKeySym to a 8-bit encoded character.
* This relies on user to use the right encoding.
*/
- virtual char getISOEncoded() const;
+ virtual char getISOEncoded(Encoding const *) const;
///
int key() const {
return key_;
Index: frontends/qt2/QLyXKeySym.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/qt2/QLyXKeySym.C,v
retrieving revision 1.15
diff -u -r1.15 QLyXKeySym.C
--- frontends/qt2/QLyXKeySym.C 17 Dec 2002 20:54:42 -0000 1.15
+++ frontends/qt2/QLyXKeySym.C 19 Dec 2002 05:32:02 -0000
@@ -19,13 +19,13 @@
#include "qlkey.h"
#include "debug.h"
#include "qt_helpers.h"
+#include "encoding.h"
#include <qevent.h>
#include <qtextcodec.h>
using std::endl;
-
QLyXKeySym::QLyXKeySym()
: LyXKeySym(), key_(0)
{
@@ -71,21 +71,112 @@
string QLyXKeySym::getSymbolName() const
{
- string sym(qkey_to_string(key_));
+ return qkey_to_string(key_);
+}
+
+
+namespace {
- if (sym.empty()) {
- lyxerr[Debug::KEY] << "sym empty in getSymbolName()" << endl;
- if (!text_.isEmpty())
- sym = fromqstr(text_);
+ /* Qt encodings left :
+ * Found codec SJIS
+ * Found codec JIS7
+ * Found codec GB18030
+ * Found codec GBK
+ * Found codec eucKR
+ * Found codec eucJP
+ * Found codec Big5
+ * Found codec ISO 8859-8
+ * Found codec TSCII
+ * Found codec ISO 8859-11
+ * Found codec Apple Roman
+ * Found codec CP 1258
+ * Found codec CP 1257
+ * Found codec CP 1256
+ * Found codec CP 1254
+ * Found codec CP 1253
+ * Found codec CP 1252
+ * Found codec CP 1250
+ * Found codec CP 874
+ * Found codec IBM 850
+ * Found codec ISO 8859-14
+ * Found codec ISO 8859-13
+ * Found codec ISO 8859-10
+ * Found codec ISO 8859-8-I
+ * Found codec UTF-8
+ */
+
+ /* lyx encodings left:
+ * tis620-0, pt154
+ */
+QTextCodec * getCodec(string const & lyxname)
+{
+ QTextCodec * codec = 0;
+
+ if (lyxname == "iso8859-1")
+ codec = QTextCodec::codecForName("ISO 8859-1");
+ else if (lyxname == "iso8859-2")
+ codec = QTextCodec::codecForName("ISO 8859-2");
+ else if (lyxname == "iso8859-3")
+ codec = QTextCodec::codecForName("ISO 8859-3");
+ else if (lyxname == "iso8859-4")
+ codec = QTextCodec::codecForName("ISO 8859-4");
+ else if (lyxname == "iso8859-5")
+ codec = QTextCodec::codecForName("ISO 8859-5");
+ else if (lyxname == "iso8859-6")
+ codec = QTextCodec::codecForName("ISO 8859-6");
+ else if (lyxname == "iso8859-7")
+ codec = QTextCodec::codecForName("ISO 8859-7");
+ else if (lyxname == "iso8859-9")
+ codec = QTextCodec::codecForName("ISO 8859-9");
+ else if (lyxname == "iso8859-15")
+ codec = QTextCodec::codecForName("ISO 8859-15");
+ else if (lyxname == "cp1255")
+ codec = QTextCodec::codecForName("CP 1255");
+ else if (lyxname == "cp1251")
+ codec = QTextCodec::codecForName("CP 1251");
+ else if (lyxname == "koi8")
+ codec = QTextCodec::codecForName("KOI8-R");
+ else if (lyxname == "koi8-u")
+ codec = QTextCodec::codecForName("KOI8-U");
+
+ if (!codec) {
+ codec = QTextCodec::codecForLocale();
+ lyxerr[Debug::KEY] << "Encoding " << lyxname
+ << " not found. Using default locale codec "
+ << fromqstr(codec->name()) << endl;
}
- lyxerr[Debug::KEY] << "getSymbolName() -> " << sym << endl;
- return sym;
+
+ lyxerr[Debug::KEY] << "encoding check found codec "
+ << fromqstr(codec->name()) << endl;
+ return codec;
+}
+
}
-char QLyXKeySym::getISOEncoded() const
+char QLyXKeySym::getISOEncoded(const Encoding * encoding) const
{
- unsigned char const c = fromqstr(text_)[0];
+#if 0
+ int i = 0;
+ QTextCodec * codec = QTextCodec::codecForIndex(i);
+ while (codec) {
+ if (codec->canEncode(text_)) {
+ lyxerr[Debug::KEY] << "Found codec " << codec->name() << endl;
+ }
+ codec = QTextCodec::codecForIndex(++i);
+ }
+
+ if (!codec) {
+ lyxerr[Debug::KEY] << "No codec found !" << endl;
+ return 0;
+ }
+#endif
+
+ QTextCodec * codec = getCodec(encoding->Name());
+
+ QCString tmpstr = codec->fromUnicode(text_);
+ char const * tmpcstr = tmpstr;
+ unsigned char const c = tmpcstr[0];
lyxerr[Debug::KEY] << "ISOEncoded returning value " << int(c) << endl;
return c;
}
@@ -107,10 +198,14 @@
bool operator==(LyXKeySym const & k1, LyXKeySym const & k2)
{
- // note we ignore text_ here (non-strict ==), because
- // text_ is not filled out by keymap initialisation
+ QLyXKeySym const & q1(static_cast<QLyXKeySym const &>(k1));
+ QLyXKeySym const & q2(static_cast<QLyXKeySym const &>(k2));
- return static_cast<QLyXKeySym const &>(k1).key()
- == static_cast<QLyXKeySym const &>(k2).key();
+ // we do not have enough info for a fair comparison, so return
+ // false. This works out OK because unknown text from Qt will
+ // get inserted anyway after the isText() check
+ if (q1.key() == Qt::Key_unknown || q2.key() == Qt::Key_unknown)
+ return false;
+ return q1.key() == q2.key();
}