]> git.lyx.org Git - features.git/blobdiff - src/paragraph.C
clear()->erase() ; lots of using directives for cxx
[features.git] / src / paragraph.C
index 28faee543a6ce62355f44daa6afc0e9c27f0da22..e1833d2248a9bcd40869a11219ffa0441ec264f3 100644 (file)
@@ -38,6 +38,8 @@ using std::ostream;
 using std::endl;
 using std::fstream;
 using std::ios;
+using std::lower_bound;
+using std::upper_bound;
 
 int tex_code_break_column = 72;  // needs non-zero initialization. set later.
 // this is a bad idea, but how can LyXParagraph find its buffer to get
@@ -167,7 +169,10 @@ void LyXParagraph::writeFile(ostream & os, BufferParams const & params,
                if (added_space_bottom.kind() != VSpace::NONE)
                        os << "\\added_space_bottom "
                           << added_space_bottom.asLyXCommand() << " ";
-                       
+
+               // Maybe the paragraph has special spacing
+               spacing.writeFile(os, true);
+               
                // The labelwidth string used in lists.
                if (!labelwidthstring.empty())
                        os << "\\labelwidthstring "
@@ -284,12 +289,6 @@ void LyXParagraph::writeFile(ostream & os, BufferParams const & params,
                        os << "\n\\hfill \n";
                        column = 0;
                        break;
-#if 0
-               case META_PROTECTED_SEPARATOR: 
-                       os << "\n\\protected_separator \n";
-                       column = 0;
-                       break;
-#endif
                case '\\':
                        os << "\n\\backslash \n";
                        column = 0;
@@ -327,20 +326,25 @@ void LyXParagraph::writeFile(ostream & os, BufferParams const & params,
 
 void LyXParagraph::validate(LaTeXFeatures & features) const
 {
+       BufferParams const & params = features.bufferParams();
+       
        // this will be useful later
        LyXLayout const & layout =
-               textclasslist.Style(current_view->buffer()->params.textclass, 
+               textclasslist.Style(params.textclass, 
                                    GetLayout());
        
        // check the params.
        if (line_top || line_bottom)
                features.lyxline = true;
+       if (!spacing.isDefault())
+               features.setspace = true;
        
        // then the layouts
        features.layout[GetLayout()] = true;
 
        // then the fonts
-       Language const * doc_language = current_view->buffer()->params.language_info;
+       Language const * doc_language = params.language_info;
+       
        for (FontList::const_iterator cit = fontlist.begin();
             cit != fontlist.end(); ++cit) {
                if ((*cit).font.noun() == LyXFont::ON) {
@@ -387,8 +391,8 @@ void LyXParagraph::validate(LaTeXFeatures & features) const
         if (layout.needprotect 
            && next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
                features.NeedLyXFootnoteCode = true;
-        if ((current_view->buffer()->params.paragraph_separation == BufferParams::PARSEP_INDENT) &&
-            (pextra_type == LyXParagraph::PEXTRA_MINIPAGE))
+        if (params.paragraph_separation == BufferParams::PARSEP_INDENT
+            && pextra_type == LyXParagraph::PEXTRA_MINIPAGE)
                features.NeedLyXMinipageIndent = true;
         if (table && table->NeedRotating())
                features.rotating = true;
@@ -429,7 +433,7 @@ void LyXParagraph::CutIntoMinibuffer(LyXParagraph::size_type pos)
                        // find the entry
                        InsetList::iterator it = lower_bound(insetlist.begin(),
                                                             insetlist.end(),
-                                                            InsetTable(pos,0));
+                                                            pos, matchIT());
                        if (it != insetlist.end() && (*it).pos == pos)
                                (*it).inset = 0;
                } else {
@@ -470,20 +474,21 @@ void LyXParagraph::Clear()
 
        added_space_top = VSpace(VSpace::NONE);
        added_space_bottom = VSpace(VSpace::NONE);
-
+       spacing.set(Spacing::Default);
+       
        align = LYX_ALIGN_LAYOUT;
        depth = 0;
        noindent = false;
 
         pextra_type = PEXTRA_NONE;
-        pextra_width.clear();
-        pextra_widthp.clear();
+        pextra_width.erase();
+        pextra_widthp.erase();
         pextra_alignment = MINIPAGE_ALIGN_TOP;
         pextra_hfill = false;
         pextra_start_minipage = false;
 
-        labelstring.clear();
-       labelwidthstring.clear();
+        labelstring.erase();
+       labelwidthstring.erase();
        layout = 0;
        bibkey = 0;
        
@@ -510,6 +515,9 @@ LyXParagraph::~LyXParagraph()
 
         // ale970302
        delete bibkey;
+       //
+       //lyxerr << "LyXParagraph::paragraph_id = "
+       //       << LyXParagraph::paragraph_id << endl;
 }
 
 
@@ -531,7 +539,7 @@ void LyXParagraph::Erase(LyXParagraph::size_type pos)
                        // find the entry
                        InsetList::iterator it = lower_bound(insetlist.begin(),
                                                             insetlist.end(),
-                                                            InsetTable(pos,0));
+                                                            pos, matchIT());
                        if (it != insetlist.end() && (*it).pos == pos) {
                                delete (*it).inset;
                                insetlist.erase(it);
@@ -564,7 +572,7 @@ void LyXParagraph::Erase(LyXParagraph::size_type pos)
                // Update the inset table.
                for (InsetList::iterator it = upper_bound(insetlist.begin(),
                                                          insetlist.end(),
-                                                         InsetTable(pos,0));
+                                                         pos, matchIT());
                     it != insetlist.end(); ++it)
                        --(*it).pos;
        } else {
@@ -601,7 +609,7 @@ void LyXParagraph::InsertChar(LyXParagraph::size_type pos, char c)
        // Update the inset table.
        for (InsetList::iterator it = lower_bound(insetlist.begin(),
                                                  insetlist.end(),
-                                                 InsetTable(pos,0));
+                                                 pos, matchIT());
             it != insetlist.end(); ++it)
                ++(*it).pos;
 }
@@ -632,12 +640,14 @@ void LyXParagraph::InsertInset(LyXParagraph::size_type pos,
                // Add a new entry in the inset table.
                InsetList::iterator it = lower_bound(insetlist.begin(),
                                                     insetlist.end(),
-                                                    InsetTable(pos,0));
+                                                    pos, matchIT());
                if (it != insetlist.end() && (*it).pos == pos)
                        lyxerr << "ERROR (LyXParagraph::InsertInset): "
                                "there is an inset in position: " << pos << endl;
                else
-                       insetlist.insert(it,InsetTable(pos,inset));
+                       insetlist.insert(it, InsetTable(pos,inset));
+               if (inset_owner)
+                       inset->setOwner(inset_owner);
        }
 }
 
@@ -668,7 +678,7 @@ Inset * LyXParagraph::GetInset(LyXParagraph::size_type pos)
        // Find the inset.
        InsetList::iterator it = lower_bound(insetlist.begin(),
                                             insetlist.end(),
-                                            InsetTable(pos,0));
+                                            pos, matchIT());
        if (it != insetlist.end() && (*it).pos == pos)
                return (*it).inset;
 
@@ -700,7 +710,7 @@ Inset const * LyXParagraph::GetInset(LyXParagraph::size_type pos) const
        // Find the inset.
        InsetList::const_iterator cit = lower_bound(insetlist.begin(),
                                                    insetlist.end(),
-                                                   InsetTable(pos,0));
+                                                   pos, matchIT());
        if (cit != insetlist.end() && (*cit).pos == pos)
                return (*cit).inset;
 
@@ -1391,9 +1401,6 @@ void LyXParagraph::BreakParagraph(LyXParagraph::size_type pos,
                        ++pos_first;
 
                pos_end = pos_first + par->text.size() - 1;
-               // The constructor has already reserved 500 elements
-               //if (pos_end > pos)
-               //      tmp->text.reserve(pos_end - pos);
 
                for (i = j = pos; i <= pos_end; ++i) {
                        par->CutIntoMinibuffer(i - pos_first);
@@ -1442,6 +1449,8 @@ void LyXParagraph::MakeSameLayout(LyXParagraph const * par)
        pagebreak_top = par->pagebreak_top;
        added_space_top = par->added_space_top;
 
+       spacing = par->spacing;
+       
         pextra_type = par->pextra_type;
         pextra_width = par->pextra_width;
         pextra_widthp = par->pextra_widthp;
@@ -1527,6 +1536,8 @@ bool LyXParagraph::HasSameLayout(LyXParagraph const * par) const
                par->pagebreak_top == pagebreak_top &&
                par->added_space_top == added_space_top &&
 
+               par->spacing == spacing &&
+               
                 par->pextra_type == pextra_type &&
                 par->pextra_width == pextra_width && 
                 par->pextra_widthp == pextra_widthp && 
@@ -1559,15 +1570,6 @@ void LyXParagraph::BreakParagraphConservative(LyXParagraph::size_type pos)
                while (ParFromPos(pos_first) != par)
                        ++pos_first;
                size_type pos_end = pos_first + par->text.size() - 1;
-               // make sure there is enough memory for the now larger
-               // paragraph. This is not neccessary, because
-               // InsertFromMinibuffer will enlarge the memory (it uses
-               // InsertChar of course). But doing it by hand
-               // is MUCH faster! (only one time, not thousend times!!)
-               // Not needed since the constructor aleady have
-               // reserved 500 elements in text.
-               //if (pos_end > pos)
-               //      tmp->text.reserve(pos_end - pos);
 
                size_type i, j;
                for (i = j = pos; i <= pos_end; ++i) {
@@ -1668,6 +1670,7 @@ int LyXParagraph::GetEndLabel() const
        return END_LABEL_NO_LABEL;
 }
 
+
 LyXTextClass::size_type LyXParagraph::GetLayout() const
 {
        return FirstPhysicalPar()->layout;
@@ -1769,10 +1772,12 @@ void LyXParagraph::SetLayout(LyXTextClass::size_type new_layout)
                * npar = 0;
 
         par->layout = new_layout;
-       par->labelwidthstring.clear();
+       par->labelwidthstring.erase();
        par->align = LYX_ALIGN_LAYOUT;
        par->added_space_top = VSpace(VSpace::NONE);
        par->added_space_bottom = VSpace(VSpace::NONE);
+       par->spacing.set(Spacing::Default);
+       
        /* table stuff -- begin*/ 
        if (table) 
                par->layout = 0;
@@ -1900,15 +1905,15 @@ LyXParagraph const * LyXParagraph::DepthHook(int deth) const
 int LyXParagraph::AutoDeleteInsets()
 {
        int count = 0;
-       unsigned int i = 0;
-       while (i < insetlist.size()) {
-               if (insetlist[i].inset && insetlist[i].inset->AutoDelete()) {
-                       Erase(insetlist[i].pos); 
-                       // Erase() calls to insetlist.erase(&insetlist[i])
-                       // so i shouldn't be increased.
+       InsetList::size_type index = 0;
+       while (index < insetlist.size()) {
+               if (insetlist[index].inset && insetlist[index].inset->AutoDelete()) {
+                       Erase(insetlist[index].pos); 
+                       // Erase() calls to insetlist.erase(&insetlist[index])
+                       // so index shouldn't be increased.
                        ++count;
                } else
-                       ++i;
+                       ++index;
        }
        return count;
 }
@@ -1918,7 +1923,7 @@ Inset * LyXParagraph::ReturnNextInsetPointer(LyXParagraph::size_type & pos)
 {
        InsetList::iterator it = lower_bound(insetlist.begin(),
                                             insetlist.end(),
-                                            InsetTable(pos, 0));
+                                            pos, matchIT());
        if (it != insetlist.end()) {
                pos = (*it).pos;
                return (*it).inset;
@@ -1931,6 +1936,15 @@ Inset * LyXParagraph::ReturnNextInsetPointer(LyXParagraph::size_type & pos)
 int LyXParagraph::GetPositionOfInset(Inset * inset) const
 {
        // Find the entry.
+       // We could use lower_bound here too, we just need to add
+       // the approp. operator() to matchIT (and change the name
+       // of that struct). Code would then be:
+       // InsetList::const_iterator cit = lower_bound(insetlist.begin(),
+       //                                             insetlist.end(),
+       //                                             inset, matchIT());
+       // if ((*cit).inset == inset) {
+       //         return (*cit).pos;
+       // }
        for (InsetList::const_iterator cit = insetlist.begin();
             cit != insetlist.end(); ++cit) {
                if ((*cit).inset == inset) {
@@ -1968,6 +1982,12 @@ LyXParagraph * LyXParagraph::TeXOnePar(ostream & os, TexRow & texrow,
                texrow.newline();
        }
 
+       if (!spacing.isDefault()
+           && (!Previous() || !Previous()->HasSameLayout(this))) {
+               os << spacing.writeEnvirBegin() << "\n";
+               texrow.newline();
+       }
+       
        if (tex_code_break_column && style.isCommand()){
                os << '\n';
                texrow.newline();
@@ -2036,12 +2056,9 @@ LyXParagraph * LyXParagraph::TeXOnePar(ostream & os, TexRow & texrow,
                        while (par &&
                               par->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
                               par->footnoteflag != footnoteflag) {
-                               LyXDirection dir = (is_rtl)
-                                       ? LYX_DIR_RIGHT_TO_LEFT 
-                                       : LYX_DIR_LEFT_TO_RIGHT;
                                par = par->TeXFootnote(os, texrow, foot,
                                                       foot_texrow, foot_count,
-                                                      dir);
+                                                      is_rtl);
                                par->SimpleTeXOnePar(os, texrow);
                                is_rtl = par->GetFontSettings(par->size()-1).isRightToLeft();
                                if (par->next &&
@@ -2057,7 +2074,7 @@ LyXParagraph * LyXParagraph::TeXOnePar(ostream & os, TexRow & texrow,
                       && par->footnoteflag != footnoteflag) {
                        par = par->TeXFootnote(os, texrow,
                                               foot, foot_texrow, foot_count,
-                                              LYX_DIR_LEFT_TO_RIGHT);
+                                              false);
                        par->SimpleTeXOnePar(os, texrow);
                        par = par->next;
                }
@@ -2111,7 +2128,8 @@ LyXParagraph * LyXParagraph::TeXOnePar(ostream & os, TexRow & texrow,
                        || par->pextra_type != pextra_type))
                        break;
        default:
-               if (!(footnoteflag != LyXParagraph::NO_FOOTNOTE
+               // we don't need it for the last paragraph!!!
+               if (next && !(footnoteflag != LyXParagraph::NO_FOOTNOTE
                      && footnotekind != LyXParagraph::FOOTNOTE
                      && footnotekind != LyXParagraph::MARGIN
                      && (table
@@ -2147,7 +2165,14 @@ LyXParagraph * LyXParagraph::TeXOnePar(ostream & os, TexRow & texrow,
                texrow.newline();
        }
 
-       if (!(footnoteflag != LyXParagraph::NO_FOOTNOTE && par &&
+       if (!spacing.isDefault()
+           && (!par || !par->HasSameLayout(this))) {
+               os << spacing.writeEnvirEnd() << "\n";
+               texrow.newline();
+       }
+       
+       // we don't need it for the last paragraph!!!
+       if (next && !(footnoteflag != LyXParagraph::NO_FOOTNOTE && par &&
               par->footnoteflag == LyXParagraph::NO_FOOTNOTE)) {
                os << '\n';
                texrow.newline();
@@ -2423,6 +2448,9 @@ bool LyXParagraph::SimpleTeXOneTablePar(ostream & os, TexRow & texrow)
        
        texrow.start(this, 0);
 
+       bool is_rtl = getParLanguage()->RightToLeft;
+       bool first_in_cell = true;
+               
        for (size_type i = 0; i < size(); ++i) {
                char c = GetChar(i);
                if (table->IsContRow(current_cell_number + 1)) {
@@ -2431,7 +2459,13 @@ bool LyXParagraph::SimpleTeXOneTablePar(ostream & os, TexRow & texrow)
                        continue;
                }
                ++column;
-               
+
+               if (first_in_cell && is_rtl) {
+                       os << "\\R{";
+                       column += 3;
+                       first_in_cell = false;
+               }
+
                // Fully instantiated font
                LyXFont font = getFont(i);
                last_font = running_font;
@@ -2491,6 +2525,11 @@ bool LyXParagraph::SimpleTeXOneTablePar(ostream & os, TexRow & texrow)
                                                 current_cell_number,
                                                 column, texrow);
                        }
+                       if (is_rtl && !first_in_cell) {
+                               os << "}";
+                               first_in_cell = true;
+                       }
+
                        // if this cell follow only ContRows till end don't
                        // put the EndOfCell because it is put after the
                        // for(...)
@@ -2521,6 +2560,8 @@ bool LyXParagraph::SimpleTeXOneTablePar(ostream & os, TexRow & texrow)
                running_font.latexWriteEndChanges(os, basefont, basefont);
        }
        ++current_cell_number;
+       if (is_rtl && !first_in_cell)
+               os << "}";
        tmp = table->TexEndOfCell(os, current_cell_number);
        for (; tmp > 0; --tmp)
                texrow.newline();
@@ -2644,13 +2685,8 @@ bool LyXParagraph::linuxDocConvertChar(char c, string & sgml_string)
        bool retval = false;
        switch (c) {
        case LyXParagraph::META_HFILL:
-               sgml_string.clear();
+               sgml_string.erase();
                break;
-#if 0
-       case LyXParagraph::META_PROTECTED_SEPARATOR: 
-               sgml_string = ' ';
-               break;
-#endif
        case LyXParagraph::META_NEWLINE:
                sgml_string = '\n';
                break;
@@ -2698,7 +2734,7 @@ bool LyXParagraph::linuxDocConvertChar(char c, string & sgml_string)
                sgml_string = ' ';
                break;
        case '\0': // Ignore :-)
-               sgml_string.clear();
+               sgml_string.erase();
                break;
        default:
                sgml_string = c;
@@ -3097,20 +3133,13 @@ void LyXParagraph::SimpleTeXSpecialChars(ostream & os, TexRow & texrow,
                            && running_font.isRightToLeft()) {
                                os << "\\L{";
                                close = true;
-                       } else if (inset->LyxCode() == Inset::NUMBER_CODE
-                                  && running_font.isRightToLeft()) {
-                               os << "{\\beginL ";
-                               close = true;
                        }
 
                        int tmp = inset->Latex(os, style.isCommand(),
                                               style.free_spacing);
 
                        if (close)
-                               if (inset->LyxCode() == Inset::NUMBER_CODE)
-                                       os << "\\endL}";
-                               else
-                                       os << "}";
+                               os << "}";
 
                        if (tmp) {
                                column = 0;
@@ -3148,11 +3177,6 @@ void LyXParagraph::SimpleTeXSpecialChars(ostream & os, TexRow & texrow,
                        // but I'll leave it as a switch statement
                        // so its simpler to extend. (ARRae)
                        switch (c) {
-#if 0
-                       case LyXParagraph::META_PROTECTED_SEPARATOR: 
-                               os << ' ';
-                               break;
-#endif
                        default:
                                // make sure that we will not print
                                // error generating chars to the tex
@@ -3167,11 +3191,6 @@ void LyXParagraph::SimpleTeXSpecialChars(ostream & os, TexRow & texrow,
                } else {
                        // Plain mode (i.e. not LaTeX)
                        switch (c) {
-#if 0
-                       case LyXParagraph::META_PROTECTED_SEPARATOR: 
-                               os << '~';
-                               break;
-#endif
                        case '\\': 
                                os << "\\textbackslash{}";
                                column += 15;
@@ -3381,10 +3400,9 @@ bool LyXParagraph::RoffContTableRows(ostream & os,
                        switch (c) {
                        case LyXParagraph::META_INSET:
                                if ((inset = GetInset(i))) {
-#if 1
 #ifdef HAVE_SSTREAM
                                        stringstream ss(ios::in | ios::out);
-                                       inset->Latex(ss, -1);
+                                       inset->Ascii(ss);
                                        ss.seekp(0);
                                        ss.get(c);
                                        while (!ss) {
@@ -3396,7 +3414,7 @@ bool LyXParagraph::RoffContTableRows(ostream & os,
                                        }
 #else
                                        strstream ss;
-                                       inset->Latex(ss, -1);
+                                       inset->Ascii(ss);
                                        ss.seekp(0);
                                        ss.get(c);
                                        while (!ss) {
@@ -3407,27 +3425,6 @@ bool LyXParagraph::RoffContTableRows(ostream & os,
                                                ss.get(c);
                                        }
                                        delete [] ss.str();
-#endif
-#else
-                                       fstream fs(fname2.c_str(),
-                                                  ios::in|ios::out);
-                                       if (!fs) {
-                                               WriteAlert(_("LYX_ERROR:"),
-                                                          _("Cannot open temporary file:"),
-                                                          fname2);
-                                               return false;
-                                       }
-                                       inset->Latex(fs, -1);
-                                       fs.seekp(0);
-                                       fs.get(c);
-                                       while (!fs) {
-                                               if (c == '\\')
-                                                       os << "\\\\";
-                                               else
-                                                       os << c;
-                                               fs.get(c);
-                                       }
-                                       fs.close();
 #endif
                                }
                                break;
@@ -3435,10 +3432,6 @@ bool LyXParagraph::RoffContTableRows(ostream & os,
                                break;
                        case LyXParagraph::META_HFILL: 
                                break;
-#if 0
-                       case LyXParagraph::META_PROTECTED_SEPARATOR:
-                               break;
-#endif
                        case '\\': 
                                os << "\\\\";
                                break;
@@ -3467,7 +3460,9 @@ LyXParagraph * LyXParagraph::TeXDeeper(ostream & os, TexRow & texrow,
        lyxerr[Debug::LATEX] << "TeXDeeper...     " << this << endl;
        LyXParagraph * par = this;
 
-       while (par && par->depth == depth) {
+       while (par &&
+              (par->depth == depth) &&
+              (par->footnoteflag == footnoteflag)) {
                if (par->IsDummy())
                        lyxerr << "ERROR (LyXParagraph::TeXDeeper)" << endl;
                if (textclasslist.Style(current_view->buffer()->params.textclass, 
@@ -3720,7 +3715,8 @@ LyXParagraph * LyXParagraph::TeXEnvironment(ostream & os, TexRow & texrow,
        } while (par
                 && par->layout == layout
                 && par->depth == depth
-                && par->pextra_type == pextra_type);
+                && par->pextra_type == pextra_type
+                && par->footnoteflag == footnoteflag);
  
        if (style.isEnvironment()) {
                os << "\\end{" << style.latexname() << '}';
@@ -3773,7 +3769,7 @@ LyXParagraph * LyXParagraph::TeXEnvironment(ostream & os, TexRow & texrow,
 LyXParagraph * LyXParagraph::TeXFootnote(ostream & os, TexRow & texrow,
                                         ostream & foot, TexRow & foot_texrow,
                                         int & foot_count,
-                                        LyXDirection parent_direction)
+                                        bool parent_is_rtl)
 {
        lyxerr[Debug::LATEX] << "TeXFootnote...  " << this << endl;
        if (footnoteflag == LyXParagraph::NO_FOOTNOTE)
@@ -3812,12 +3808,12 @@ LyXParagraph * LyXParagraph::TeXFootnote(ostream & os, TexRow & texrow,
        }
 
        bool need_closing = false;
-       LyXDirection direction = getParDirection();
-       if (direction != parent_direction) {
-               if (direction == LYX_DIR_LEFT_TO_RIGHT)
-                       os << "\\L{";
-               else
+       bool is_rtl = isRightToLeftPar();
+       if (is_rtl != parent_is_rtl) {
+               if (is_rtl)
                        os << "\\R{";
+               else
+                       os << "\\L{";
                need_closing = true;
        }
        
@@ -4092,8 +4088,8 @@ void LyXParagraph::UnsetPExtraType()
                return;
     
        pextra_type = PEXTRA_NONE;
-       pextra_width.clear();
-       pextra_widthp.clear();
+       pextra_width.erase();
+       pextra_widthp.erase();
 
        if (textclasslist.Style(current_view->buffer()->params.textclass, 
                                layout).isEnvironment()) {
@@ -4117,8 +4113,8 @@ void LyXParagraph::UnsetPExtraType()
                while (par && (par->layout == layout)
                       && (par->depth == depth)) {
                        par->pextra_type = PEXTRA_NONE;
-                       par->pextra_width.clear();
-                       par->pextra_widthp.clear();
+                       par->pextra_width.erase();
+                       par->pextra_widthp.erase();
                        par = par->NextAfterFootnote();
                        if (par && (par->depth > depth))
                                par->UnsetPExtraType();
@@ -4194,47 +4190,52 @@ bool LyXParagraph::IsWord(size_type pos ) const
        return IsWordChar(GetChar(pos)) ;
 }
 
+
 Language const * LyXParagraph::getParLanguage() const 
 {
-       if (!table && size() > 0)
-               return FirstPhysicalPar()->GetFirstFontSettings().language();
+       if (size() > 0)
+               if (!table)
+                       return FirstPhysicalPar()->GetFirstFontSettings()
+                               .language();
+               else {
+                       for (size_type pos = 0; pos < size(); ++pos)
+                               if (IsNewline(pos))
+                                       return GetFontSettings(pos).language();
+                       return GetFirstFontSettings().language();
+               }
        else if (previous)
                return previous->getParLanguage();
        else
                return current_view->buffer()->params.language_info;
 }
 
-Language const * LyXParagraph::getLetterLanguage(size_type pos) const
+
+bool LyXParagraph::isRightToLeftPar() const
 {
-       return GetFontSettings(pos).language();
+       return lyxrc.rtl_support && !table && getParLanguage()->RightToLeft;
 }
 
-LyXDirection LyXParagraph::getParDirection() const
+
+void LyXParagraph::ChangeLanguage(Language const * from, Language const * to)
 {
-       if (!lyxrc.rtl_support || table)
-               return LYX_DIR_LEFT_TO_RIGHT;
-       else if (getParLanguage()->RightToLeft)
-               return LYX_DIR_RIGHT_TO_LEFT;
-       else
-               return LYX_DIR_LEFT_TO_RIGHT;
+       for(size_type i = 0; i < size(); ++i) {
+               LyXFont font = GetFontSettings(i);
+               if (font.language() == from) {
+                       font.setLanguage(to);
+                       SetFont(i, font);
+               }
+       }
 }
 
-LyXDirection
-LyXParagraph::getLetterDirection(LyXParagraph::size_type pos) const
+
+bool LyXParagraph::isMultiLingual()
 {
-       if (!lyxrc.rtl_support)
-               return LYX_DIR_LEFT_TO_RIGHT;
-       else if (table && IsNewline(pos))
-               return LYX_DIR_LEFT_TO_RIGHT;
-
-       bool is_rtl =  GetFontSettings(pos).isVisibleRightToLeft();
-       if (IsLineSeparator(pos) && 0 < pos && pos < Last() - 1
-           && !IsLineSeparator(pos + 1)
-           && !(table && IsNewline(pos + 1))
-           && ( GetFontSettings(pos - 1).isVisibleRightToLeft() != is_rtl
-                || GetFontSettings(pos + 1).isVisibleRightToLeft() != is_rtl))
-               return getParDirection();
-       else
-               return (is_rtl) ? LYX_DIR_RIGHT_TO_LEFT
-                       : LYX_DIR_LEFT_TO_RIGHT;
+       Language const * doc_language =
+               current_view->buffer()->params.language_info;
+       for(size_type i = 0; i < size(); ++i) {
+               LyXFont font = GetFontSettings(i);
+               if (font.language() != doc_language)
+                       return true;
+       }
+       return false;
 }