On Thu, Oct 11, 2007 at 09:55:01AM +0200, Abdelrazak Younes wrote:

> >> I'd prefer:
> >>            if (Encodings::isGreekChar(next))
> >> +                  break;

Thanks for the suggestion. I also updated the patch in order to have
a sane behaviour with change tracking.

If there are no objections I will commit it.

-- 
Enrico
Index: src/Paragraph.cpp
===================================================================
--- src/Paragraph.cpp   (revision 20893)
+++ src/Paragraph.cpp   (working copy)
@@ -66,8 +66,10 @@ using std::ostream;
 namespace lyx {
 
 using support::contains;
+using support::prefixIs;
 using support::suffixIs;
 using support::rsplit;
+using support::rtrim;
 
 
 /////////////////////////////////////////////////////////////////////
@@ -187,6 +189,10 @@ public:
                             unsigned int & column,
                             Font const & font,
                             Layout const & style);
+       /// Output consecutive greek chars starting from \p c to \p os.
+       /// \return the number of characters written.
+       int greekChars(odocstream & os, value_type c, Change &,
+                      Encoding const &, pos_type &);
        ///
        void simpleTeXSpecialChars(Buffer const &, BufferParams const &,
                                   odocstream &,
@@ -627,6 +633,57 @@ bool Paragraph::Pimpl::simpleTeXBlanks(E
 }
 
 
+int Paragraph::Pimpl::greekChars(odocstream & os,
+                                value_type c,
+                                Change & runningChange,
+                                Encoding const & encoding,
+                                pos_type & i)
+{
+       // The latex command is "\textgreek{x}" and we have to retain
+       // "\textgreek{x" for the first char but only "x" for all subsequent
+       // chars (this also works when we are passed untranslated unicode).
+       docstring const latex1 = rtrim(encoding.latexChar(c), "}");
+       int length = latex1.length();
+       os << latex1;
+       while (i < size() - 1) {
+               char_type next = getChar(i + 1);
+               if (!Encodings::isGreekChar(next) ||
+                   runningChange != lookupChange(i + 1))
+                       break;
+               Font prev_font;
+               bool found = false;
+               FontList::const_iterator cit = fontlist.begin();
+               FontList::const_iterator end = fontlist.end();
+               for (; cit != end; ++cit) {
+                       if (cit->pos() >= i && !found) {
+                               prev_font = cit->font();
+                               found = true;
+                       }
+                       if (cit->pos() >= i + 1)
+                               break;
+               }
+               if (found && cit != end && prev_font != cit->font())
+                       break;
+               docstring const latex = encoding.latexChar(next);
+               docstring::size_type const j =
+                                       latex.find_first_of(from_ascii("{"));
+               if (j == docstring::npos)
+                       os << latex.substr(0, 1);
+               else
+                       os << latex.substr(j + 1, 1);
+               ++length;
+               ++i;
+       }
+       // When the language is Greek, we are passed the stright unicode,
+       // so we should not try to close the \textgreek command.
+       if (prefixIs(latex1, from_ascii("\\textgreek"))) {
+               os << '}';
+               ++length;
+       }
+       return length;
+}
+
+
 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
 {
        pos_type const len = str.length();
@@ -952,6 +1009,10 @@ void Paragraph::Pimpl::simpleTeXSpecialC
                                                break;
                                        }
                                }
+                               if (Encodings::isGreekChar(c)) {
+                                       column += greekChars(os, c, 
running_change, encoding, i) - 1;
+                                       break;
+                               }
                                docstring const latex = encoding.latexChar(c);
                                if (latex.length() > 1 &&
                                    latex[latex.length() - 1] != '}') {
Index: src/LaTeXFeatures.cpp
===================================================================
--- src/LaTeXFeatures.cpp       (revision 20893)
+++ src/LaTeXFeatures.cpp       (working copy)
@@ -186,6 +186,13 @@ static string const changetracking_none_
        "\\newcommand{\\lyxadded}[3]{#3}\n"
        "\\newcommand{\\lyxdeleted}[3]{}\n";
 
+static string const textgreek_def =
+       "\\DeclareRobustCommand{\\greektext}{%\n"
+       " \\fontencoding{LGR}\\selectfont\n"
+       " \\def\\encodingdefault{LGR}}\n"
+       "\\DeclareRobustCommand{\\textgreek}[1]{\\leavevmode{\\greektext #1}}\n"
+       "\\DeclareFontEncoding{LGR}{}{}\n";
+
 
 /////////////////////////////////////////////////////////////////////
 //
@@ -618,6 +625,9 @@ string const LaTeXFeatures::getMacros() 
        if (mustProvide("lyxarrow"))
                macros << lyxarrow_def << '\n';
 
+       if (mustProvide("textgreek"))
+               macros << textgreek_def << '\n';
+
        // quotes.
        if (mustProvide("quotesinglbase"))
                macros << quotesinglbase_def << '\n';
Index: src/Encoding.cpp
===================================================================
--- src/Encoding.cpp    (revision 20893)
+++ src/Encoding.cpp    (working copy)
@@ -401,6 +401,15 @@ bool Encodings::isCombiningChar(char_typ
 }
 
 
+bool Encodings::isGreekChar(char_type c)
+{
+       CharInfoMap::const_iterator const it = unicodesymbols.find(c);
+       if (it != unicodesymbols.end())
+               return it->second.preamble == "textgreek";
+       return false;
+}
+
+
 Encoding const * Encodings::getFromLyXName(string const & name) const
 {
        EncodingList::const_iterator it = encodinglist.find(name);
Index: src/Encoding.h
===================================================================
--- src/Encoding.h      (revision 20893)
+++ src/Encoding.h      (working copy)
@@ -142,6 +142,8 @@ public:
        static char_type transformChar(char_type c, Letter_Form form);
        /// Is this a combining char?
        static bool isCombiningChar(char_type c);
+       /// Is this a greek char?
+       static bool isGreekChar(char_type c);
        /**
         * Add the preamble snippet needed for the output of \p c to
         * \p features.
Index: lib/unicodesymbols
===================================================================
--- lib/unicodesymbols  (revision 20893)
+++ lib/unicodesymbols  (working copy)
@@ -684,6 +684,55 @@
 #0x036d ""                         "" "combining" # COMBINING LATIN SMALL 
LETTER T
 #0x036e ""                         "" "combining" # COMBINING LATIN SMALL 
LETTER V
 #0x036f ""                         "" "combining" # COMBINING LATIN SMALL 
LETTER X
+0x0391 "\\textgreek{A}"           "textgreek" "" # GREEK CAPITAL LETTER ALPHA
+0x0392 "\\textgreek{B}"           "textgreek" "" # GREEK CAPITAL LETTER BETA
+0x0393 "\\textgreek{G}"           "textgreek" "" # GREEK CAPITAL LETTER GAMMA
+0x0394 "\\textgreek{D}"           "textgreek" "" # GREEK CAPITAL LETTER DELTA
+0x0395 "\\textgreek{E}"           "textgreek" "" # GREEK CAPITAL LETTER EPSILON
+0x0396 "\\textgreek{Z}"           "textgreek" "" # GREEK CAPITAL LETTER ZETA
+0x0397 "\\textgreek{H}"           "textgreek" "" # GREEK CAPITAL LETTER ETA
+0x0398 "\\textgreek{J}"           "textgreek" "" # GREEK CAPITAL LETTER THETA
+0x0399 "\\textgreek{I}"           "textgreek" "" # GREEK CAPITAL LETTER IOTA
+0x039a "\\textgreek{K}"           "textgreek" "" # GREEK CAPITAL LETTER KAPPA
+0x039b "\\textgreek{L}"           "textgreek" "" # GREEK CAPITAL LETTER LAMDA
+0x039c "\\textgreek{M}"           "textgreek" "" # GREEK CAPITAL LETTER MU
+0x039d "\\textgreek{N}"           "textgreek" "" # GREEK CAPITAL LETTER NU
+0x039e "\\textgreek{X}"           "textgreek" "" # GREEK CAPITAL LETTER XI
+0x039f "\\textgreek{O}"           "textgreek" "" # GREEK CAPITAL LETTER OMICRON
+0x03a0 "\\textgreek{P}"           "textgreek" "" # GREEK CAPITAL LETTER PI
+0x03a1 "\\textgreek{R}"           "textgreek" "" # GREEK CAPITAL LETTER RHO
+0x03a3 "\\textgreek{S}"           "textgreek" "" # GREEK CAPITAL LETTER SIGMA
+0x03a4 "\\textgreek{T}"           "textgreek" "" # GREEK CAPITAL LETTER TAU
+0x03a5 "\\textgreek{U}"           "textgreek" "" # GREEK CAPITAL LETTER UPSILON
+0x03a6 "\\textgreek{F}"           "textgreek" "" # GREEK CAPITAL LETTER PHI
+0x03a7 "\\textgreek{Q}"           "textgreek" "" # GREEK CAPITAL LETTER CHI
+0x03a8 "\\textgreek{Y}"           "textgreek" "" # GREEK CAPITAL LETTER PSI
+0x03a9 "\\textgreek{W}"           "textgreek" "" # GREEK CAPITAL LETTER OMEGA
+0x03b1 "\\textgreek{a}"           "textgreek" "" # GREEK SMALL LETTER ALPHA
+0x03b2 "\\textgreek{b}"           "textgreek" "" # GREEK SMALL LETTER BETA
+0x03b3 "\\textgreek{g}"           "textgreek" "" # GREEK SMALL LETTER GAMMA
+0x03b4 "\\textgreek{d}"           "textgreek" "" # GREEK SMALL LETTER DELTA
+0x03b5 "\\textgreek{e}"           "textgreek" "" # GREEK SMALL LETTER EPSILON
+0x03b6 "\\textgreek{z}"           "textgreek" "" # GREEK SMALL LETTER ZETA
+0x03b7 "\\textgreek{h}"           "textgreek" "" # GREEK SMALL LETTER ETA
+0x03b8 "\\textgreek{j}"           "textgreek" "" # GREEK SMALL LETTER THETA
+0x03b9 "\\textgreek{i}"           "textgreek" "" # GREEK SMALL LETTER IOTA
+0x03ba "\\textgreek{k}"           "textgreek" "" # GREEK SMALL LETTER KAPPA
+0x03bb "\\textgreek{l}"           "textgreek" "" # GREEK SMALL LETTER LAMDA
+0x03bc "\\textgreek{m}"           "textgreek" "" # GREEK SMALL LETTER MU
+0x03bd "\\textgreek{n}"           "textgreek" "" # GREEK SMALL LETTER NU
+0x03be "\\textgreek{x}"           "textgreek" "" # GREEK SMALL LETTER XI
+0x03bf "\\textgreek{o}"           "textgreek" "" # GREEK SMALL LETTER OMICRON
+0x03c0 "\\textgreek{p}"           "textgreek" "" # GREEK SMALL LETTER PI
+0x03c1 "\\textgreek{r}"           "textgreek" "" # GREEK SMALL LETTER RHO
+0x03c2 "\\textgreek{c}"           "textgreek" "" # GREEK SMALL LETTER FINAL 
SIGMA
+0x03c3 "\\textgreek{s}"           "textgreek" "" # GREEK SMALL LETTER SIGMA
+0x03c4 "\\textgreek{t}"           "textgreek" "" # GREEK SMALL LETTER TAU
+0x03c5 "\\textgreek{u}"           "textgreek" "" # GREEK SMALL LETTER UPSILON
+0x03c6 "\\textgreek{f}"           "textgreek" "" # GREEK SMALL LETTER PHI
+0x03c7 "\\textgreek{q}"           "textgreek" "" # GREEK SMALL LETTER CHI
+0x03c8 "\\textgreek{y}"           "textgreek" "" # GREEK SMALL LETTER PSI
+0x03c9 "\\textgreek{w}"           "textgreek" "" # GREEK SMALL LETTER OMEGA
 0x0e3f "\\textbaht"               "textcomp" "" # THAI CURRENCY SYMBOL BAHT
 0x1e00 "\\textsubring{A}"         "tipa" "" # LATIN CAPITAL LETTER A WITH RING 
BELOW
 0x1e01 "\\textsubring{a}"         "tipa" "" # LATIN SMALL LETTER A WITH RING 
BELOW

Reply via email to