]> git.lyx.org Git - lyx.git/blobdiff - src/paragraph.C
redraw fix 1.
[lyx.git] / src / paragraph.C
index 055f0d435982d8b0aa3515d5475e76aa1b3f6e18..97405c5e9421c23753a5fece4d9da068c975a67b 100644 (file)
@@ -189,12 +189,7 @@ void Paragraph::writeFile(Buffer const * buf, ostream & os,
        }
 
        // First write the layout
-       string lay = layout();
-       if (lay.empty()) {
-               lay = textclasslist[bparams.textclass].defaultLayoutName();
-       }
-
-       os << "\n\\layout " << layout() << "\n";
+       os << "\n\\layout " << layout()->name() << "\n";
 
        // Maybe some vertical spaces.
        if (params().spaceTop().kind() != VSpace::NONE)
@@ -232,6 +227,10 @@ void Paragraph::writeFile(Buffer const * buf, ostream & os,
        if (params().noindent())
                os << "\\noindent ";
 
+       // Do we have a manual left indent?
+       if (!params().leftIndent().zero())
+               os << "\\leftindent " << params().leftIndent().asString() << " ";
+
        // Alignment?
        if (params().align() != LYX_ALIGN_LAYOUT) {
                int h = 0;
@@ -342,7 +341,7 @@ void Paragraph::validate(LaTeXFeatures & features) const
                features.require("setspace");
 
        // then the layouts
-       features.useLayout(layout());
+       features.useLayout(layout()->name());
 
        // then the fonts
        Language const * doc_language = bparams.language;
@@ -389,15 +388,18 @@ void Paragraph::validate(LaTeXFeatures & features) const
                }
        }
 
+       if (!params().leftIndent().zero())
+               features.require("ParagraphLeftIndent");
+
        // then the insets
-       LyXLayout const & lout = textclasslist[bparams.textclass][layout()];
+       LyXLayout_ptr const & lout = layout();
 
        InsetList::const_iterator icit = insetlist.begin();
        InsetList::const_iterator iend = insetlist.end();
        for (; icit != iend; ++icit) {
                if (icit->inset) {
                        icit->inset->validate(features);
-                       if (lout.needprotect &&
+                       if (lout->needprotect &&
                            icit->inset->lyxCode() == Inset::FOOT_CODE)
                                features.require("NeedLyXFootnoteCode");
                }
@@ -637,17 +639,17 @@ LyXFont const Paragraph::getFont(BufferParams const & bparams,
 {
        lyx::Assert(pos >= 0);
 
-       LyXLayout const & lout =
-               textclasslist[bparams.textclass][layout()];
+       LyXLayout_ptr const & lout = layout();
+
        pos_type main_body = 0;
-       if (lout.labeltype == LABEL_MANUAL)
+       if (lout->labeltype == LABEL_MANUAL)
                main_body = beginningOfMainBody();
 
        LyXFont layoutfont;
        if (pos < main_body)
-               layoutfont = lout.labelfont;
+               layoutfont = lout->labelfont;
        else
-               layoutfont = lout.font;
+               layoutfont = lout->font;
 
        LyXFont tmpfont = getFontSettings(bparams, pos);
 #ifndef INHERIT_LANGUAGE
@@ -662,10 +664,9 @@ LyXFont const Paragraph::getFont(BufferParams const & bparams,
 
 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
 {
-       LyXLayout const & lout =
-               textclasslist[bparams.textclass][layout()];
+       LyXLayout_ptr const & lout = layout();
 
-       LyXFont tmpfont = lout.labelfont;
+       LyXFont tmpfont = lout->labelfont;
        tmpfont.setLanguage(getParLanguage(bparams));
 
        return pimpl_->realizeFont(tmpfont, bparams);
@@ -674,10 +675,9 @@ LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
 
 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
 {
-       LyXLayout const & lout =
-               textclasslist[bparams.textclass][layout()];
+       LyXLayout_ptr const & lout = layout();
 
-       LyXFont tmpfont = lout.font;
+       LyXFont tmpfont = lout->font;
        tmpfont.setLanguage(getParLanguage(bparams));
 
        return pimpl_->realizeFont(tmpfont, bparams);
@@ -870,8 +870,7 @@ void Paragraph::breakParagraph(BufferParams const & bparams,
 {
        // create a new paragraph
        Paragraph * tmp = new Paragraph(this);
-       tmp->layout(textclasslist[bparams.textclass].defaultLayoutName());
-
+       tmp->layout(textclasslist[bparams.textclass].defaultLayout());
        // remember to set the inset_owner
        tmp->setInsetOwner(inInset());
 
@@ -884,7 +883,8 @@ void Paragraph::breakParagraph(BufferParams const & bparams,
                tmp->setLabelWidthString(params().labelWidthString());
        }
 
-       bool isempty = textclasslist[bparams.textclass][layout()].keepempty;
+       bool isempty = (layout()->keepempty && !size());
+
        if (!isempty && (size() > pos || !size() || flag == 2)) {
                tmp->layout(layout());
                tmp->params().align(params().align());
@@ -924,7 +924,8 @@ void Paragraph::breakParagraph(BufferParams const & bparams,
 
                bibkey = 0;
                params().clear();
-               layout(textclasslist[bparams.textclass].defaultLayoutName());
+
+               layout(textclasslist[bparams.textclass].defaultLayout());
 
                // layout stays the same with latex-environments
                if (flag) {
@@ -944,9 +945,9 @@ void Paragraph::makeSameLayout(Paragraph const * par)
 }
 
 
-int Paragraph::stripLeadingSpaces(lyx::textclass_type tclass)
+int Paragraph::stripLeadingSpaces()
 {
-       if (textclasslist[tclass][layout()].free_spacing ||
+       if (layout()->free_spacing ||
            isFreeSpacing()) {
                return 0;
        }
@@ -1029,14 +1030,14 @@ void Paragraph::pasteParagraph(BufferParams const & bparams)
 }
 
 
-int Paragraph::getEndLabel(BufferParams const & bparams) const
+int Paragraph::getEndLabel() const
 {
        Paragraph const * par = this;
        depth_type par_depth = getDepth();
        while (par) {
-               string const & layout = par->layout();
-               int const endlabeltype =
-                       textclasslist[bparams.textclass][layout].endlabeltype;
+               LyXLayout_ptr const & layout = par->layout();
+               int const endlabeltype = layout->endlabeltype;
+
                if (endlabeltype != END_LABEL_NO_LABEL) {
                        if (!next_)
                                return endlabeltype;
@@ -1064,9 +1065,9 @@ Paragraph::depth_type Paragraph::getDepth() const
 }
 
 
-Paragraph::depth_type Paragraph::getMaxDepthAfter(Buffer const * buffer) const
+Paragraph::depth_type Paragraph::getMaxDepthAfter() const
 {
-       bool const isenv = textclasslist[buffer->params.textclass][layout()].isEnvironment();
+       bool const isenv = layout()->isEnvironment();
 
        if (isenv)
                return params().depth() + 1;
@@ -1109,7 +1110,7 @@ void Paragraph::setLabelWidthString(string const & s)
 }
 
 
-void Paragraph::applyLayout(string const & new_layout)
+void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
 {
        layout(new_layout);
        params().labelWidthString(string());
@@ -1133,8 +1134,7 @@ int Paragraph::beginningOfMainBody() const
        // and remember the previous character to
        // remove unnecessary GetChar() calls
        pos_type i = 0;
-       if (i < size()
-           && getChar(i) != Paragraph::META_NEWLINE) {
+       if (i < size() && getChar(i) != Paragraph::META_NEWLINE) {
                ++i;
                char previous_char = 0;
                char temp = 0;
@@ -1213,7 +1213,7 @@ Paragraph::InsetIterator(pos_type pos)
        InsetList::iterator it = insetlist.begin();
        InsetList::iterator end = insetlist.end();
        for (; it != end; ++it) {
-               if (it->pos == pos)
+               if (it->pos >= pos)
                        break;
        }
 
@@ -1247,14 +1247,14 @@ Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
        lyxerr[Debug::LATEX] << "TeXOnePar...     " << this << endl;
        Inset const * in = inInset();
        bool further_blank_line = false;
-       LyXLayout style;
+       LyXLayout_ptr style;
 
        // well we have to check if we are in an inset with unlimited
        // lenght (all in one row) if that is true then we don't allow
        // any special options in the paragraph and also we don't allow
        // any environment other then "Standard" to be valid!
        if ((in == 0) || !in->forceDefaultParagraphs(in)) {
-               style = textclasslist[bparams.textclass][layout()];
+               style = layout();
 
                if (params().startOfAppendix()) {
                        os << "\\appendix\n";
@@ -1267,7 +1267,7 @@ Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
                        texrow.newline();
                }
 
-               if (tex_code_break_column && style.isCommand()) {
+               if (tex_code_break_column && style->isCommand()) {
                        os << '\n';
                        texrow.newline();
                }
@@ -1302,7 +1302,7 @@ Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
 
        if (language->babel() != previous_language->babel()
            // check if we already put language command in TeXEnvironment()
-           && !(style.isEnvironment()
+           && !(style->isEnvironment()
                 && (!previous() || previous()->layout() != layout() ||
                         previous()->params().depth() != params().depth())))
        {
@@ -1333,11 +1333,11 @@ Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
                texrow.newline();
        }
 
-       switch (style.latextype) {
+       switch (style->latextype) {
        case LATEX_COMMAND:
                os << '\\'
-                  << style.latexname()
-                  << style.latexparam();
+                  << style->latexname()
+                  << style->latexparam();
                break;
        case LATEX_ITEM_ENVIRONMENT:
                if (bibkey) {
@@ -1367,9 +1367,9 @@ Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
                (size() == 0
                 ? getLayoutFont(bparams) : getFont(bparams, size() - 1));
 
-       bool is_command = style.isCommand();
+       bool is_command = style->isCommand();
 
-       if (style.resfont.size() != font.size() && next_ && !is_command) {
+       if (style->resfont.size() != font.size() && next_ && !is_command) {
                if (!need_par)
                        os << "{";
                os << "\\" << font.latexSize() << " \\par}";
@@ -1378,7 +1378,7 @@ Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
        } else if (is_command)
                os << "}";
 
-       switch (style.latextype) {
+       switch (style->latextype) {
        case LATEX_ITEM_ENVIRONMENT:
        case LATEX_LIST_ENVIRONMENT:
                if (next_ && (params().depth() < next_->params().depth())) {
@@ -1461,7 +1461,7 @@ Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
 
 // This could go to ParagraphParameters if we want to
 int Paragraph::startTeXParParams(BufferParams const & bparams,
-                                ostream & os) const
+                                ostream & os, bool moving_arg) const
 {
        int column = 0;
 
@@ -1470,6 +1470,22 @@ int Paragraph::startTeXParParams(BufferParams const & bparams,
                column += 10;
        }
 
+       switch (params().align()) {
+       case LYX_ALIGN_NONE:
+       case LYX_ALIGN_BLOCK:
+       case LYX_ALIGN_LAYOUT:
+       case LYX_ALIGN_SPECIAL:
+               break;
+       case LYX_ALIGN_LEFT:
+       case LYX_ALIGN_RIGHT:
+       case LYX_ALIGN_CENTER:
+               if (moving_arg) {
+                       os << "\\protect";
+                       column = 8;
+               }
+               break;
+       }
+       
        switch (params().align()) {
        case LYX_ALIGN_NONE:
        case LYX_ALIGN_BLOCK:
@@ -1505,10 +1521,26 @@ int Paragraph::startTeXParParams(BufferParams const & bparams,
 
 // This could go to ParagraphParameters if we want to
 int Paragraph::endTeXParParams(BufferParams const & bparams,
-                              ostream & os) const
+                              ostream & os, bool moving_arg) const
 {
        int column = 0;
 
+       switch (params().align()) {
+       case LYX_ALIGN_NONE:
+       case LYX_ALIGN_BLOCK:
+       case LYX_ALIGN_LAYOUT:
+       case LYX_ALIGN_SPECIAL:
+               break;
+       case LYX_ALIGN_LEFT:
+       case LYX_ALIGN_RIGHT:
+       case LYX_ALIGN_CENTER:
+               if (moving_arg) {
+                       os << "\\protect";
+                       column = 8;
+               }
+               break;
+       }
+       
        switch (params().align()) {
        case LYX_ALIGN_NONE:
        case LYX_ALIGN_BLOCK:
@@ -1552,7 +1584,7 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
 
        bool return_value = false;
 
-       LyXLayout style;
+       LyXLayout_ptr style;
 
        // well we have to check if we are in an inset with unlimited
        // lenght (all in one row) if that is true then we don't allow
@@ -1564,14 +1596,14 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
        if (asdefault) {
                style = textclasslist[bparams.textclass].defaultLayout();
        } else {
-               style = textclasslist[bparams.textclass][layout()];
+               style = layout();
        }
 
        LyXFont basefont;
 
        // Maybe we have to create a optional argument.
        pos_type main_body;
-       if (style.labeltype != LABEL_MANUAL)
+       if (style->labeltype != LABEL_MANUAL)
                main_body = 0;
        else
                main_body = beginningOfMainBody();
@@ -1586,7 +1618,7 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
                basefont = getLayoutFont(bparams);
        }
 
-       moving_arg |= style.needprotect;
+       moving_arg |= style->needprotect;
 
        // Which font is currently active?
        LyXFont running_font(basefont);
@@ -1597,12 +1629,12 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
 
        // if the paragraph is empty, the loop will not be entered at all
        if (!size()) {
-               if (style.isCommand()) {
+               if (style->isCommand()) {
                        os << '{';
                        ++column;
                }
                if (!asdefault)
-                       column += startTeXParParams(bparams, os);
+                       column += startTeXParParams(bparams, os, moving_arg);
 
        }
 
@@ -1620,13 +1652,14 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
                                os << ']';
                                ++column;
                        }
-                       if (style.isCommand()) {
+                       if (style->isCommand()) {
                                os << '{';
                                ++column;
                        }
 
                        if (!asdefault)
-                               column += startTeXParParams(bparams, os);
+                               column += startTeXParParams(bparams, os,
+                                                           moving_arg);
                }
 
                value_type c = getChar(i);
@@ -1664,7 +1697,7 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
                        // Do not print the separation of the optional argument
                        if (i != main_body - 1) {
                                pimpl_->simpleTeXBlanks(os, texrow, i,
-                                                      column, font, style);
+                                                      column, font, *style);
                        }
                }
 
@@ -1682,7 +1715,7 @@ 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) {
+                       if (!style->newline_allowed) {
                                os << '\n';
                        } else {
                                if (open_font) {
@@ -1697,6 +1730,7 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
                                }
                                if (moving_arg)
                                        os << "\\protect ";
+
                                os << "\\\\\n";
                        }
                        texrow.newline();
@@ -1707,7 +1741,7 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
                                                      os, texrow, moving_arg,
                                                      font, running_font,
                                                      basefont, open_font,
-                                                     style, i, column, c);
+                                                     *style, i, column, c);
                }
        }
 
@@ -1740,7 +1774,7 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
        }
 
        if (!asdefault) {
-               column += endTeXParParams(bparams, os);
+               column += endTeXParParams(bparams, os, moving_arg);
        }
 
        lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
@@ -1818,8 +1852,7 @@ Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
 {
        lyxerr[Debug::LATEX] << "TeXEnvironment...     " << this << endl;
 
-       LyXLayout const & style =
-               textclasslist[bparams.textclass][layout()];
+       LyXLayout_ptr const & style = layout();
 
        Language const * language = getParLanguage(bparams);
        Language const * doc_language = bparams.language;
@@ -1844,21 +1877,29 @@ Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
                }
        }
 
-       if (style.isEnvironment()) {
-               if (style.latextype == LATEX_LIST_ENVIRONMENT) {
-                       os << "\\begin{" << style.latexname() << "}{"
+       bool leftindent_open = false;
+       if (!params().leftIndent().zero()) {
+               os << "\\begin{LyXParagraphLeftIndent}{" <<
+                       params().leftIndent().asLatexString() << "}\n";
+               texrow.newline();
+               leftindent_open = true;
+       }
+
+       if (style->isEnvironment()) {
+               if (style->latextype == LATEX_LIST_ENVIRONMENT) {
+                       os << "\\begin{" << style->latexname() << "}{"
                           << params().labelWidthString() << "}\n";
-               } else if (style.labeltype == LABEL_BIBLIO) {
+               } else if (style->labeltype == LABEL_BIBLIO) {
                        // ale970405
-                       os << "\\begin{" << style.latexname() << "}{"
+                       os << "\\begin{" << style->latexname() << "}{"
                           <<  bibitemWidest(buf)
                           << "}\n";
-               } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
-                       os << "\\begin{" << style.latexname() << '}'
-                          << style.latexparam() << '\n';
+               } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
+                       os << "\\begin{" << style->latexname() << '}'
+                          << style->latexparam() << '\n';
                } else
-                       os << "\\begin{" << style.latexname() << '}'
-                          << style.latexparam() << '\n';
+                       os << "\\begin{" << style->latexname() << '}'
+                          << style->latexparam() << '\n';
                texrow.newline();
        }
        Paragraph * par = this;
@@ -1866,11 +1907,12 @@ Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
                par = par->TeXOnePar(buf, bparams, os, texrow, false);
 
                if (par && par->params().depth() > params().depth()) {
-                       if (textclasslist[bparams.textclass][par->layout()].isParagraph()
+                           if (par->layout()->isParagraph()) {
+
                            // Thinko!
                            // How to handle this? (Lgb)
                            //&& !suffixIs(os, "\n\n")
-                               ) {
+                                   //) {
                                // There should be at least one '\n' already
                                // but we need there to be two for Standard
                                // paragraphs that are depth-increment'ed to be
@@ -1888,10 +1930,16 @@ Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
                }
        } while (par
                 && par->layout() == layout()
-                && par->params().depth() == params().depth());
+                && par->params().depth() == params().depth()
+                && par->params().leftIndent() == params().leftIndent());
 
-       if (style.isEnvironment()) {
-               os << "\\end{" << style.latexname() << "}\n";
+       if (style->isEnvironment()) {
+               os << "\\end{" << style->latexname() << "}\n";
+               texrow.newline();
+       }
+
+       if (leftindent_open) {
+               os << "\\end{LyXParagraphLeftIndent}\n";
                texrow.newline();
        }
 
@@ -1984,7 +2032,9 @@ Paragraph::getParLanguage(BufferParams const & bparams) const
 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
 {
        return lyxrc.rtl_support
-               && getParLanguage(bparams)->RightToLeft();
+               && getParLanguage(bparams)->RightToLeft()
+               && !(inInset() && inInset()->owner() &&
+                    inInset()->owner()->lyxCode() == Inset::ERT_CODE);
 }
 
 
@@ -2145,16 +2195,14 @@ void  Paragraph::id(int id_arg)
 }
 
 
-string const & Paragraph::layout() const
+LyXLayout_ptr const & Paragraph::layout() const
 {
        return layout_;
 }
 
 
-void Paragraph::layout(string const & new_layout)
+void Paragraph::layout(LyXLayout_ptr const & new_layout)
 {
-       lyx::Assert(!new_layout.empty());
-
        layout_ = new_layout;
 }