]> git.lyx.org Git - lyx.git/blobdiff - src/paragraph.C
add missing writeNormal() methods to some insets
[lyx.git] / src / paragraph.C
index da35bb359141c2936f07ca5ad9b68c4910f3e63a..36d87fbc28bbd45290ebf0e5325d3f29eef533f7 100644 (file)
@@ -35,6 +35,7 @@
 #include "insets/insetbib.h"
 #include "insets/insettext.h"
 #include "support/filetools.h"
+#include "support/lstrings.h"
 #include "lyx_gui_misc.h"
 #include "texrow.h"
 #include "support/lyxmanip.h"
@@ -107,35 +108,36 @@ Paragraph::Paragraph(Paragraph * par)
 }
 
 
-Paragraph::Paragraph(Paragraph const & lp)
-       : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this))
+Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
+       : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
 {
-       for (int i = 0; i < 10; ++i) setCounter(i , 0);
+       for (int i = 0; i < 10; ++i)
+               setCounter(i , 0);
        enumdepth = 0;
        itemdepth = 0;
        next_ = 0;
        previous_ = 0;
-       clear();
-
-       makeSameLayout(&lp);
 
        // this is because of the dummy layout of the paragraphs that
        // follow footnotes
        layout = lp.layout;
 
         // ale970302
-       if (lp.bibkey)
+       if (lp.bibkey) {
                bibkey = static_cast<InsetBibKey *>
                        (lp.bibkey->clone(*current_view->buffer()));
-       else
+       } else {
                bibkey = 0;
+       }
        
        // copy everything behind the break-position to the new paragraph
 
        insetlist = lp.insetlist;
        for (InsetList::iterator it = insetlist.begin();
             it != insetlist.end(); ++it)
-               it->inset = it->inset->clone(*current_view->buffer());
+       {
+               it->inset = it->inset->clone(*current_view->buffer(), same_ids);
+       }
 }
 
 
@@ -149,7 +151,7 @@ Paragraph::~Paragraph()
 
        for (InsetList::iterator it = insetlist.begin();
             it != insetlist.end(); ++it) {
-               delete (*it).inset;
+               delete it->inset;
        }
 
         // ale970302
@@ -163,8 +165,8 @@ Paragraph::~Paragraph()
 
 
 void Paragraph::writeFile(Buffer const * buf, ostream & os,
-                            BufferParams const & bparams,
-                            depth_type dth) const
+                          BufferParams const & bparams,
+                          depth_type dth) const
 {
        // The beginning or end of a deeper (i.e. nested) area?
        if (dth != params().depth()) {
@@ -250,7 +252,11 @@ void Paragraph::writeFile(Buffer const * buf, ostream & os,
                // Write font changes
                LyXFont font2 = getFontSettings(bparams, i);
                if (font2 != font1) {
+#ifndef INHERIT_LANGUAGE
                        font2.lyxWriteChanges(font1, os);
+#else
+                       font2.lyxWriteChanges(font1, bparams.language, os);
+#endif
                        column = 0;
                        font1 = font2;
                }
@@ -335,29 +341,39 @@ void Paragraph::validate(LaTeXFeatures & features) const
        
        for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
             cit != pimpl_->fontlist.end(); ++cit) {
-               if ((*cit).font().noun() == LyXFont::ON) {
+               if (cit->font().noun() == LyXFont::ON) {
                        lyxerr[Debug::LATEX] << "font.noun: "
-                                            << (*cit).font().noun()
+                                            << cit->font().noun()
                                             << endl;
                        features.noun = true;
                        lyxerr[Debug::LATEX] << "Noun enabled. Font: "
-                                            << (*cit).font().stateText(0)
+                                            << cit->font().stateText(0)
                                             << endl;
                }
-               switch ((*cit).font().color()) {
+               switch (cit->font().color()) {
                case LColor::none:
                case LColor::inherit:
                case LColor::ignore:
+                       // probably we should put here all interface colors used for
+                       // font displaying! For now I just add this ones I know of (Jug)
+               case LColor::latex:
+               case LColor::note:
                        break;
                default:
                        features.color = true;
                        lyxerr[Debug::LATEX] << "Color enabled. Font: "
-                                            << (*cit).font().stateText(0)
+                                            << cit->font().stateText(0)
                                             << endl;
                }
 
-               Language const * language = (*cit).font().language();
-               if (language->babel() != doc_language->babel()) {
+               Language const * language = cit->font().language();
+               if (language->babel() != doc_language->babel() &&
+                   language != ignore_language &&
+#ifdef INHERIT_LANGUAGE
+                   language != inherit_language &&
+#endif
+                   language != latex_language)
+               {
                        features.UsedLanguages.insert(language);
                        lyxerr[Debug::LATEX] << "Found language "
                                             << language->babel() << endl;
@@ -365,10 +381,18 @@ void Paragraph::validate(LaTeXFeatures & features) const
        }
 
        // then the insets
+       LyXLayout const & layout =
+                textclasslist.Style(bparams.textclass,
+                                    getLayout());
+
        for (InsetList::const_iterator cit = insetlist.begin();
             cit != insetlist.end(); ++cit) {
-               if ((*cit).inset)
-                       (*cit).inset->validate(features);
+               if (cit->inset) {
+                       cit->inset->validate(features);
+                       if (layout.needprotect &&
+                           cit->inset->lyxCode() == Inset::FOOT_CODE)
+                               features.NeedLyXFootnoteCode = true;
+               }
        }
 }
 
@@ -412,8 +436,8 @@ void Paragraph::cutIntoMinibuffer(BufferParams const & bparams,
                                lower_bound(insetlist.begin(),
                                            insetlist.end(),
                                            search_elem, Pimpl::matchIT());
-                       if (it != insetlist.end() && (*it).pos == pos)
-                               (*it).inset = 0;
+                       if (it != insetlist.end() && it->pos == pos)
+                               it->inset = 0;
                } else {
                        minibuffer_inset = 0;
                        minibuffer_char = ' ';
@@ -429,12 +453,15 @@ void Paragraph::cutIntoMinibuffer(BufferParams const & bparams,
 bool Paragraph::insertFromMinibuffer(Paragraph::size_type pos)
 {
        if ((minibuffer_char == Paragraph::META_INSET) &&
-           !insertInsetAllowed(minibuffer_inset))
+           !insetAllowed(minibuffer_inset->lyxCode()))
                return false;
        if (minibuffer_char == Paragraph::META_INSET)
                insertInset(pos, minibuffer_inset, minibuffer_font);
-       else
-               insertChar(pos, minibuffer_char, minibuffer_font);
+       else {
+               LyXFont f = minibuffer_font;
+               if (checkInsertChar(f))
+                       insertChar(pos, minibuffer_char, f);
+       }
        return true;
 }
 
@@ -457,43 +484,48 @@ void Paragraph::erase(Paragraph::size_type pos)
 }
 
 
-void Paragraph::insertChar(Paragraph::size_type pos,
-                             Paragraph::value_type c)
+bool Paragraph::checkInsertChar(LyXFont & font)
+{
+       if (pimpl_->inset_owner)
+               return pimpl_->inset_owner->checkInsertChar(font);
+       return true;
+}
+
+
+void Paragraph::insertChar(Paragraph::size_type pos, Paragraph::value_type c)
 {
        LyXFont const f(LyXFont::ALL_INHERIT);
        insertChar(pos, c, f);
 }
 
 
-void Paragraph::insertChar(Paragraph::size_type pos,
-                             Paragraph::value_type c,
-                             LyXFont const & font)
+void Paragraph::insertChar(Paragraph::size_type pos, Paragraph::value_type c,
+                           LyXFont const & font)
 {
        pimpl_->insertChar(pos, c, font);
 }
 
 
-void Paragraph::insertInset(Paragraph::size_type pos,
-                              Inset * inset)
+void Paragraph::insertInset(Paragraph::size_type pos, Inset * inset)
 {
        LyXFont const f(LyXFont::ALL_INHERIT);
        insertInset(pos, inset, f);
 }
 
 
-void Paragraph::insertInset(Paragraph::size_type pos,
-                              Inset * inset, LyXFont const & font)
+void Paragraph::insertInset(Paragraph::size_type pos, Inset * inset,
+                            LyXFont const & font)
 {
        pimpl_->insertInset(pos, inset, font);
 }
 
 
-bool Paragraph::insertInsetAllowed(Inset * inset)
+bool Paragraph::insetAllowed(Inset::Code code)
 {
        //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
        
        if (pimpl_->inset_owner)
-               return pimpl_->inset_owner->insertInsetAllowed(inset);
+               return pimpl_->inset_owner->insetAllowed(code);
        return true;
 }
 
@@ -507,10 +539,10 @@ Inset * Paragraph::getInset(Paragraph::size_type pos)
        InsetList::iterator it = lower_bound(insetlist.begin(),
                                             insetlist.end(),
                                             search_inset, Pimpl::matchIT());
-       if (it != insetlist.end() && (*it).pos == pos)
-               return (*it).inset;
+       if (it != insetlist.end() && it->pos == pos)
+               return it->inset;
 
-       lyxerr << "ERROR (Paragraph::GetInset): "
+       lyxerr << "ERROR (Paragraph::getInset): "
                "Inset does not exist: " << pos << endl;
        //::raise(SIGSTOP);
        
@@ -532,8 +564,8 @@ Inset const * Paragraph::getInset(Paragraph::size_type pos) const
        InsetList::const_iterator cit = lower_bound(insetlist.begin(),
                                                    insetlist.end(),
                                                    search_inset, Pimpl::matchIT());
-       if (cit != insetlist.end() && (*cit).pos == pos)
-               return (*cit).inset;
+       if (cit != insetlist.end() && cit->pos == pos)
+               return cit->inset;
 
        lyxerr << "ERROR (Paragraph::GetInset): "
                "Inset does not exist: " << pos << endl;
@@ -549,7 +581,7 @@ Inset const * Paragraph::getInset(Paragraph::size_type pos) const
 
 // Gets uninstantiated font setting at position.
 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
-                                           Paragraph::size_type pos) const
+                                         Paragraph::size_type pos) const
 {
        lyx::Assert(pos <= size());
        
@@ -557,13 +589,19 @@ LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
        Pimpl::FontList::const_iterator cit = lower_bound(pimpl_->fontlist.begin(),
                                                   pimpl_->fontlist.end(),
                                                   search_font, Pimpl::matchFT());
-       if (cit != pimpl_->fontlist.end())
-               return (*cit).font();
-       
-       if (pos == size() && size())
-               return getFontSettings(bparams, pos - 1);
-       
-       return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
+       LyXFont retfont;
+       if (cit != pimpl_->fontlist.end()) {
+               retfont = cit->font();
+       } else if (pos == size() && size()) {
+               retfont = getFontSettings(bparams, pos - 1);
+       } else
+               retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
+#ifdef INHERIT_LANGUAGE
+       if (retfont.language() == inherit_language)
+               retfont.setLanguage(bparams.language);
+#endif
+
+       return retfont;
 }
 
 
@@ -587,9 +625,10 @@ LyXFont const Paragraph::getFirstFontSettings() const
 // If position is -1, we get the layout font of the paragraph.
 // If position is -2, we get the font of the manual label of the paragraph.
 LyXFont const Paragraph::getFont(BufferParams const & bparams,
-                             Paragraph::size_type pos) const
+                                Paragraph::size_type pos) const
 {
-       LyXFont tmpfont;
+       lyx::Assert(pos >= 0);
+       
        LyXLayout const & layout =
                textclasslist.Style(bparams.textclass, 
                                    getLayout());
@@ -597,66 +636,76 @@ LyXFont const Paragraph::getFont(BufferParams const & bparams,
        if (layout.labeltype == LABEL_MANUAL)
                main_body = beginningOfMainBody();
 
-       if (pos >= 0) {
-               LyXFont layoutfont;
-               if (pos < main_body)
-                       layoutfont = layout.labelfont;
-               else
-                       layoutfont = layout.font;
-               tmpfont = getFontSettings(bparams, pos);
-               tmpfont.realize(layoutfont);
-       } else {
-               // process layoutfont for pos == -1 and labelfont for pos < -1
-               if (pos == -1)
-                       tmpfont = layout.font;
-               else
-                       tmpfont = layout.labelfont;
-               tmpfont.setLanguage(getParLanguage(bparams));
-       }
+       LyXFont layoutfont;
+       if (pos < main_body)
+               layoutfont = layout.labelfont;
+       else
+               layoutfont = layout.font;
+       
+       LyXFont tmpfont = getFontSettings(bparams, pos);
+#ifndef INHERIT_LANGUAGE
+       tmpfont.realize(layoutfont);
+#else
+       tmpfont.realize(layoutfont, bparams.language);
+#endif
 
-       // check for environment font information
-       char par_depth = getDepth();
-       Paragraph const * par = this;
-       while (par && par->getDepth() && !tmpfont.resolved()) {
-               par = par->outerHook();
-               if (par) {
-                       tmpfont.realize(textclasslist.
-                                       Style(bparams.textclass,
-                                             par->getLayout()).font);
-                       par_depth = par->getDepth();
-               }
-       }
+       return pimpl_->realizeFont(tmpfont, bparams);
+}
+
+
+LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
+{
+       LyXLayout const & layout =
+               textclasslist.Style(bparams.textclass, getLayout());
+       
+       LyXFont tmpfont = layout.labelfont;
+       tmpfont.setLanguage(getParLanguage(bparams));
+
+       return pimpl_->realizeFont(tmpfont, bparams);
+}
+
+
+LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
+{
+       LyXLayout const & layout =
+               textclasslist.Style(bparams.textclass, 
+                                   getLayout());
+
+       LyXFont tmpfont = layout.font;
+       tmpfont.setLanguage(getParLanguage(bparams));
 
-       tmpfont.realize(textclasslist
-                       .TextClass(bparams.textclass)
-                       .defaultfont());
-       return tmpfont;
+       return pimpl_->realizeFont(tmpfont, bparams);
 }
 
 
 /// Returns the height of the highest font in range
 LyXFont::FONT_SIZE
 Paragraph::highestFontInRange(Paragraph::size_type startpos,
-                                Paragraph::size_type endpos) const
+                              Paragraph::size_type endpos,
+                             LyXFont::FONT_SIZE const def_size) const
 {
-       LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
        if (pimpl_->fontlist.empty())
-               return maxsize;
+               return def_size;
 
+       LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
        Pimpl::FontTable end_search(endpos, LyXFont());
-       Pimpl::FontList::const_iterator end_it = lower_bound(pimpl_->fontlist.begin(),
-                                                     pimpl_->fontlist.end(),
-                                                     end_search, Pimpl::matchFT());
+       Pimpl::FontList::const_iterator end_it =
+               lower_bound(pimpl_->fontlist.begin(),
+                           pimpl_->fontlist.end(),
+                           end_search, Pimpl::matchFT());
        if (end_it != pimpl_->fontlist.end())
                ++end_it;
 
        Pimpl::FontTable start_search(startpos, LyXFont());
-       for (Pimpl::FontList::const_iterator cit =
-                    lower_bound(pimpl_->fontlist.begin(),
-                                pimpl_->fontlist.end(),
-                                start_search, Pimpl::matchFT());
-            cit != end_it; ++cit) {
-               LyXFont::FONT_SIZE size = (*cit).font().size();
+       Pimpl::FontList::const_iterator cit =
+               lower_bound(pimpl_->fontlist.begin(),
+                           pimpl_->fontlist.end(),
+                           start_search, Pimpl::matchFT());
+       
+       for (; cit != end_it; ++cit) {
+               LyXFont::FONT_SIZE size = cit->font().size();
+               if (size == LyXFont::INHERIT_SIZE)
+                       size = def_size;
                if (size > maxsize && size <= LyXFont::SIZE_HUGER)
                        maxsize = size;
        }
@@ -706,52 +755,6 @@ Paragraph::getUChar(BufferParams const & bparams,
 }
 
 
-// return an string of the current word, and the end of the word in lastpos.
-string const Paragraph::getWord(Paragraph::size_type & lastpos) const
-{
-       lyx::Assert(lastpos >= 0);
-
-       // the current word is defined as starting at the first character
-       // from the immediate left of lastpospos which meets the definition
-       // of IsLetter(), continuing to the last character to the right
-       // of this meeting IsLetter.
-
-       string theword;
-
-       // grab a word
-               
-       // move back until we have a letter
-
-       //there's no real reason to have firstpos & lastpos as
-       //separate variables as this is written, but maybe someon
-       // will want to return firstpos in the future.
-
-       //since someone might have typed a punctuation first
-       int firstpos = lastpos;
-       
-       while ((firstpos >= 0) && !isLetter(firstpos))
-               --firstpos;
-
-       // now find the beginning by looking for a nonletter
-       
-       while ((firstpos>= 0) && isLetter(firstpos))
-               --firstpos;
-
-       // the above is now pointing to the preceeding non-letter
-       ++firstpos;
-       lastpos = firstpos;
-
-       // so copy characters into theword  until we get a nonletter
-       // note that this can easily exceed lastpos, wich means
-       // that if used in the middle of a word, the whole word
-       // is included
-
-       while (isLetter(lastpos)) theword += getChar(lastpos++);
-       
-       return theword;
-}
-
-
 void Paragraph::setFont(Paragraph::size_type pos,
                           LyXFont const & font)
 {
@@ -860,7 +863,7 @@ void Paragraph::breakParagraph(BufferParams const & bparams,
        // create a new paragraph
        Paragraph * tmp = new Paragraph(this);
        // remember to set the inset_owner
-       tmp->setInsetOwner(InInset());
+       tmp->setInsetOwner(inInset());
        
        // this is an idea for a more userfriendly layout handling, I will
        // see what the users say
@@ -921,10 +924,8 @@ void Paragraph::breakParagraph(BufferParams const & bparams,
 void Paragraph::makeSameLayout(Paragraph const * par)
 {
        layout = par->layout;
-       params().makeSame(par->params());
-       
-       // This can be changed after NEW_INSETS is in effect. (Lgb)
-       setLabelWidthString(par->params().labelWidthString());
+       // move to pimpl?
+       params() = par->params();
 }
 
 
@@ -1219,8 +1220,8 @@ int Paragraph::getPositionOfInset(Inset * inset) const
        // Find the entry.
        for (InsetList::const_iterator cit = insetlist.begin();
             cit != insetlist.end(); ++cit) {
-               if ((*cit).inset == inset) {
-                       return (*cit).pos;
+               if (cit->inset == inset) {
+                       return cit->pos;
                }
        }
        if (inset == bibkey)
@@ -1297,7 +1298,7 @@ Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
                   << "}" << endl;
                texrow.newline();
        }
-       
+
        switch (style.latextype) {
        case LATEX_COMMAND:
                os << '\\'
@@ -1328,7 +1329,10 @@ Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
        // Is this really needed ? (Dekel)
        // We do not need to use to change the font for the last paragraph
        // or for a command.
-       LyXFont font = getFont(bparams, size() - 1);
+       LyXFont const font =
+               (size() == 0
+                ? getLayoutFont(bparams)
+                : getFont(bparams, size() - 1));
 
        bool is_command = textclasslist.Style(bparams.textclass,
                                              getLayout()).isCommand();
@@ -1342,8 +1346,9 @@ Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
                os << "}";
 
        if (language->babel() != doc_language->babel() &&
-           (!next_
-            || next_->getParLanguage(bparams)->babel() != language->babel())) {
+           (!next_ ||
+            next_->getParLanguage(bparams)->babel() != language->babel()))
+       {
                os << endl 
                   << subst(lyxrc.language_command_end, "$$lang",
                            doc_language->babel());
@@ -1377,8 +1382,7 @@ Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
        
        further_blank_line = false;
        if (params().lineBottom()) {
-               os << "\\lyxline{\\" << getFont(bparams,
-                                               size() - 1).latexSize() << '}';
+               os << "\\lyxline{\\" << font.latexSize() << '}';
                further_blank_line = true;
        }
 
@@ -1441,9 +1445,9 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
        if (main_body > 0) {
                os << '[';
                ++column;
-               basefont = getFont(bparams, -2); // Get label font
+               basefont = getLabelFont(bparams);
        } else {
-               basefont = getFont(bparams, -1); // Get layout font
+               basefont = getLayoutFont(bparams);
        }
 
        if (main_body >= 0
@@ -1472,7 +1476,7 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
                                        column += running_font.latexWriteEndChanges(os, basefont, basefont);
                                        open_font = false;
                                }
-                               basefont = getFont(bparams, -1); // Now use the layout font
+                               basefont = getLayoutFont(bparams);
                                running_font = basefont;
                                os << ']';
                                ++column;
@@ -1495,24 +1499,24 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
                        case LYX_ALIGN_LEFT:
                                if (getParLanguage(bparams)->babel() != "hebrew") {
                                        os << "\\begin{flushleft}";
-                                       column+= 17;
+                                       column += 17;
                                } else {
                                        os << "\\begin{flushright}";
-                                       column+= 18;
+                                       column += 18;
                                }
                                break;
                        case LYX_ALIGN_RIGHT:
                                if (getParLanguage(bparams)->babel() != "hebrew") {
                                        os << "\\begin{flushright}";
-                                       column+= 18;
+                                       column += 18;
                                } else {
                                        os << "\\begin{flushleft}";
-                                       column+= 17;
+                                       column += 17;
                                }
                                break;
                        case LYX_ALIGN_CENTER:
                                os << "\\begin{center}";
-                               column+= 14;
+                               column += 14;
                                break;
                        }        
                }
@@ -1522,18 +1526,21 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
                // Fully instantiated font
                LyXFont font = getFont(bparams, i);
 
-               LyXFont last_font = running_font;
+               LyXFont const last_font = running_font;
 
                // Spaces at end of font change are simulated to be
                // outside font change, i.e. we write "\textXX{text} "
                // rather than "\textXX{text }". (Asger)
-               if (open_font && c == ' ' && i <= size() - 2 
-                   && !getFont(bparams, i + 1).equalExceptLatex(running_font) 
-                   && !getFont(bparams, i + 1).equalExceptLatex(font)) {
-                       font = getFont(bparams, i + 1);
+               if (open_font && c == ' ' && i <= size() - 2) {
+                       LyXFont const next_font = getFont(bparams, i + 1);
+                       if (next_font != running_font
+                           && next_font != font) {
+                               font = next_font;
+                       }
                }
+               
                // We end font definition before blanks
-               if (!font.equalExceptLatex(running_font) && open_font) {
+               if (font != running_font && open_font) {
                        column += running_font.latexWriteEndChanges(os,
                                                                    basefont,
                                                                    (i == main_body-1) ? basefont : font);
@@ -1551,8 +1558,7 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
                }
 
                // Do we need to change font?
-               if (!font.equalExceptLatex(running_font)
-                   && i != main_body-1) {
+               if (font != running_font && i != main_body - 1) {
                        column += font.latexWriteStartChanges(os, basefont,
                                                              last_font);
                        running_font = font;
@@ -1562,18 +1568,14 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
                if (c == Paragraph::META_NEWLINE) {
                        // newlines are handled differently here than
                        // the default in SimpleTeXSpecialChars().
-                       if (!style.newline_allowed
-#ifndef NO_LATEX
-                           || font.latex() == LyXFont::ON
-#endif
-                               ) {
+                       if (!style.newline_allowed) {
                                os << '\n';
                        } else {
                                if (open_font) {
                                        column += running_font.latexWriteEndChanges(os, basefont, basefont);
                                        open_font = false;
                                }
-                               basefont = getFont(bparams, -1);
+                               basefont = getLayoutFont(bparams);
                                running_font = basefont;
                                if (font.family() == 
                                    LyXFont::TYPEWRITER_FAMILY) {
@@ -1597,15 +1599,24 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
 
        // If we have an open font definition, we have to close it
        if (open_font) {
+#ifdef FIXED_LANGUAGE_END_DETECTION
                if (next_) {
                        running_font
                                .latexWriteEndChanges(os, basefont,
-                                                     next_->getFont(bparams,
-                                                                    0));
+                                                     next_->getFont(bparams,
+                                                     0));
                } else {
                        running_font.latexWriteEndChanges(os, basefont,
-                                                         basefont);
+                                                         basefont);
                }
+#else
+#ifdef WITH_WARNINGS
+//#warning For now we ALWAYS have to close the foreign font settings if they are
+//#warning there as we start another \selectlanguage with the next paragraph if
+//#warning we are in need of this. This should be fixed sometime (Jug)
+#endif
+               running_font.latexWriteEndChanges(os, basefont,  basefont);
+#endif
        }
 
        // Needed if there is an optional argument but no contents.
@@ -1841,9 +1852,17 @@ bool Paragraph::isWord(size_type pos ) const
 Language const *
 Paragraph::getParLanguage(BufferParams const & bparams) const 
 {
-       if (size() > 0)
+       if (size() > 0) {
+#ifndef INHERIT_LANGUAGE
                return getFirstFontSettings().language();
-       else if (previous_)
+#else
+               Language const * lang = getFirstFontSettings().language();
+#warning We should make this somewhat better, any ideas? (Jug)
+               if (lang == inherit_language || lang == ignore_language)
+                       lang = bparams.language;
+               return lang;
+#endif
+       } else if (previous_)
                return previous_->getParLanguage(bparams);
        else
                return bparams.language;
@@ -1875,7 +1894,12 @@ bool Paragraph::isMultiLingual(BufferParams const & bparams)
        Language const * doc_language = bparams.language;
        for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
             cit != pimpl_->fontlist.end(); ++cit)
-               if ((*cit).font().language() != doc_language)
+               if (cit->font().language() != ignore_language &&
+                   cit->font().language() != latex_language &&
+#ifdef INHERIT_LANGUAGE
+                       cit->font().language() != inherit_language &&
+#endif
+                       cit->font().language() != doc_language)
                        return true;
        return false;
 }
@@ -1897,7 +1921,7 @@ string const Paragraph::asString(Buffer const * buffer, bool label)
                        s += c;
                else if (c == META_INSET &&
                         getInset(i)->lyxCode() == Inset::MATH_CODE) {
-                       std::ostringstream ost;
+                       ostringstream ost;
                        getInset(i)->ascii(buffer, ost);
                        s += subst(ost.str().c_str(),'\n',' ');
                }
@@ -1911,18 +1935,23 @@ string const Paragraph::asString(Buffer const * buffer, bool label)
 
 
 string const Paragraph::asString(Buffer const * buffer, 
-                           Paragraph::size_type beg,
-                           Paragraph::size_type end)
+                                Paragraph::size_type beg,
+                                Paragraph::size_type end,
+                                bool label)
 {
-       std::ostringstream ost;
+       ostringstream ost;
 
-       if (beg == 0 && !params().labelString().empty())
+       if (beg == 0 && label && !params().labelString().empty())
                ost << params().labelString() << ' ';
 
        for (Paragraph::size_type i = beg; i < end; ++i) {
                value_type const c = getUChar(buffer->params, i);
                if (IsPrintable(c))
                        ost << c;
+               else if (c == META_NEWLINE)
+                       ost << '\n';
+               else if (c == META_HFILL)
+                       ost << '\t'; 
                else if (c == META_INSET) {
                        getInset(i)->ascii(buffer, ost);
                }
@@ -1937,8 +1966,8 @@ void Paragraph::setInsetOwner(Inset * i)
        pimpl_->inset_owner = i;
        for (InsetList::const_iterator cit = insetlist.begin();
             cit != insetlist.end(); ++cit) {
-               if ((*cit).inset)
-                       (*cit).inset->setOwner(i);
+               if (cit->inset)
+                       cit->inset->setOwner(i);
        }
 }
 
@@ -1948,10 +1977,10 @@ void Paragraph::deleteInsetsLyXText(BufferView * bv)
        // then the insets
        for (InsetList::const_iterator cit = insetlist.begin();
             cit != insetlist.end(); ++cit) {
-               if ((*cit).inset) {
-                       if ((*cit).inset->isTextInset()) {
+               if (cit->inset) {
+                       if (cit->inset->isTextInset()) {
                                static_cast<UpdatableInset *>
-                                       ((*cit).inset)->deleteLyXText(bv, true);
+                                       (cit->inset)->deleteLyXText(bv, true);
                        }
                }
        }
@@ -1962,11 +1991,12 @@ void Paragraph::resizeInsetsLyXText(BufferView * bv)
 {
        // then the insets
        for (InsetList::const_iterator cit = insetlist.begin();
-            cit != insetlist.end(); ++cit) {
-               if ((*cit).inset) {
-                       if ((*cit).inset->isTextInset()) {
+            cit != insetlist.end(); ++cit)
+       {
+               if (cit->inset) {
+                       if (cit->inset->isTextInset()) {
                                static_cast<UpdatableInset *>
-                                       ((*cit).inset)->resizeLyXText(bv, true);
+                                       (cit->inset)->resizeLyXText(bv, true);
                        }
                }
        }
@@ -2019,7 +2049,7 @@ bool Paragraph::isFirstInSequence() const
 }
 
 
-Inset * Paragraph::InInset()
+Inset * Paragraph::inInset() const
 {
        return pimpl_->inset_owner;
 }
@@ -2057,9 +2087,7 @@ void Paragraph::setChar(size_type pos, value_type c)
 
 Paragraph::inset_iterator::inset_iterator(Paragraph::InsetList::iterator const & iter)
  : it(iter) 
-{
-
-}
+{}
 
 
 Paragraph::inset_iterator Paragraph::inset_iterator_begin()
@@ -2079,8 +2107,14 @@ ParagraphParameters & Paragraph::params()
        return pimpl_->params;
 }
 
+
 ParagraphParameters const & Paragraph::params() const
 {
        return pimpl_->params;
 }
 
+
+Paragraph * Paragraph::getParFromID(int id) const
+{
+       return pimpl_->getParFromID(id);
+}