]> git.lyx.org Git - lyx.git/blobdiff - src/paragraph.C
fix some C++ parsing bugs
[lyx.git] / src / paragraph.C
index ad90baabfdc910980836242b02aaa21993c9960a..b83a8a77a5331fed5ce6f47a447ee440dd6795d3 100644 (file)
@@ -15,7 +15,6 @@
 #include "lyxrc.h"
 #include "layout.h"
 #include "language.h"
-#include "tex-strings.h"
 #include "buffer.h"
 #include "bufferparams.h"
 #include "debug.h"
@@ -165,58 +164,7 @@ void Paragraph::write(Buffer const * buf, ostream & os,
        // First write the layout
        os << "\n\\layout " << layout()->name() << '\n';
 
-       // Maybe some vertical spaces.
-       if (params().spaceTop().kind() != VSpace::NONE)
-               os << "\\added_space_top "
-                  << params().spaceTop().asLyXCommand() << ' ';
-       if (params().spaceBottom().kind() != VSpace::NONE)
-               os << "\\added_space_bottom "
-                  << params().spaceBottom().asLyXCommand() << ' ';
-
-       // Maybe the paragraph has special spacing
-       params().spacing().writeFile(os, true);
-
-       // The labelwidth string used in lists.
-       if (!params().labelWidthString().empty())
-               os << "\\labelwidthstring "
-                  << params().labelWidthString() << '\n';
-
-       // Lines above or below?
-       if (params().lineTop())
-               os << "\\line_top ";
-       if (params().lineBottom())
-               os << "\\line_bottom ";
-
-       // Pagebreaks above or below?
-       if (params().pagebreakTop())
-               os << "\\pagebreak_top ";
-       if (params().pagebreakBottom())
-               os << "\\pagebreak_bottom ";
-
-       // Start of appendix?
-       if (params().startOfAppendix())
-               os << "\\start_of_appendix ";
-
-       // Noindent?
-       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;
-               switch (params().align()) {
-               case LYX_ALIGN_LEFT: h = 1; break;
-               case LYX_ALIGN_RIGHT: h = 2; break;
-               case LYX_ALIGN_CENTER: h = 3; break;
-               default: h = 0; break;
-               }
-               os << "\\align " << string_align[h] << ' ';
-       }
+       params().write(os);
 
        LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
 
@@ -261,14 +209,6 @@ void Paragraph::write(Buffer const * buf, ostream & os,
                                }
                }
                break;
-               case META_NEWLINE:
-                       os << "\n\\newline \n";
-                       column = 0;
-                       break;
-               case META_HFILL:
-                       os << "\n\\hfill \n";
-                       column = 0;
-                       break;
                case '\\':
                        os << "\n\\backslash \n";
                        column = 0;
@@ -433,7 +373,6 @@ void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font, C
 bool Paragraph::insetAllowed(Inset::Code code)
 {
        //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
-
        if (pimpl_->inset_owner)
                return pimpl_->inset_owner->insetAllowed(code);
        return true;
@@ -443,7 +382,6 @@ bool Paragraph::insetAllowed(Inset::Code code)
 Inset * Paragraph::getInset(pos_type pos)
 {
        lyx::Assert(pos < size());
-
        return insetlist.get(pos);
 }
 
@@ -451,7 +389,6 @@ Inset * Paragraph::getInset(pos_type pos)
 Inset const * Paragraph::getInset(pos_type pos) const
 {
        lyx::Assert(pos < size());
-
        return insetlist.get(pos);
 }
 
@@ -470,11 +407,11 @@ LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
        }
 
        LyXFont retfont;
-       if (cit != end) {
+       if (cit != end)
                retfont = cit->font();
-       } else if (pos == size() && !empty()) {
+       else if (pos == size() && !empty())
                retfont = getFontSettings(bparams, pos - 1);
-       else
+       else
                retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
 
        return retfont;
@@ -505,10 +442,10 @@ LyXFont const Paragraph::getFont(BufferParams const & bparams,
 
        LyXLayout_ptr const & lout = layout();
 
-       pos_type const main_body = beginningOfMainBody();
+       pos_type const body_pos = beginningOfBody();
 
        LyXFont layoutfont;
-       if (pos < main_body)
+       if (pos < body_pos)
                layoutfont = lout->labelfont;
        else
                layoutfont = lout->font;
@@ -733,10 +670,8 @@ void Paragraph::makeSameLayout(Paragraph const * par)
 
 int Paragraph::stripLeadingSpaces()
 {
-       if (layout()->free_spacing ||
-           isFreeSpacing()) {
+       if (layout()->free_spacing || isFreeSpacing())
                return 0;
-       }
 
        int i = 0;
        while (!empty() && (isNewline(0) || isLineSeparator(0))) {
@@ -793,15 +728,13 @@ Paragraph::depth_type Paragraph::getDepth() const
 
 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
 {
-       bool const isenv = layout()->isEnvironment();
-
-       if (isenv)
+       if (layout()->isEnvironment())
                return params().depth() + 1;
        else
                return params().depth();
-
 }
 
+
 char Paragraph::getAlign() const
 {
        return params().align();
@@ -841,14 +774,7 @@ void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
 }
 
 
-// if the layout of a paragraph contains a manual label, the beginning of the
-// main body is the beginning of the second word. This is what the par-
-// function returns. If the layout does not contain a label, the main
-// body always starts with position 0. This differentiation is necessary,
-// because there cannot be a newline or a blank <= the beginning of the
-// main body in TeX.
-
-int Paragraph::beginningOfMainBody() const
+int Paragraph::beginningOfBody() const
 {
        if (layout()->labeltype != LABEL_MANUAL)
                return 0;
@@ -857,19 +783,22 @@ 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() && !isNewline(i)) {
                ++i;
                char previous_char = 0;
                char temp = 0;
-               if (i < size()
-                   && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
-                       // Yes, this  ^ is supposed to be "= " not "=="
-                       ++i;
-                       while (i < size()
-                              && previous_char != ' '
-                              && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
+               if (i < size()) {
+                       previous_char = getChar(i);
+                       if (!isNewline(i)) {
                                ++i;
-                               previous_char = temp;
+                               while (i < size() && previous_char != ' ') {
+                                       temp = getChar(i);
+                                       if (isNewline(i))
+                                               break;
+
+                                       ++i;
+                                       previous_char = temp;
+                               }
                        }
                }
        }
@@ -1099,19 +1028,19 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
        LyXFont basefont;
 
        // Maybe we have to create a optional argument.
-       pos_type main_body;
+       pos_type body_pos;
 
        // FIXME: can we actually skip this check and just call
-       // beginningOfMainBody() ??
+       // beginningOfBody() ??
        if (style->labeltype != LABEL_MANUAL) {
-               main_body = 0;
+               body_pos = 0;
        } else {
-               main_body = beginningOfMainBody();
+               body_pos = beginningOfBody();
        }
 
        unsigned int column = 0;
 
-       if (main_body > 0) {
+       if (body_pos > 0) {
                os << '[';
                ++column;
                basefont = getLabelFont(bparams);
@@ -1144,8 +1073,8 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
        for (pos_type i = 0; i < size(); ++i) {
                ++column;
                // First char in paragraph or after label?
-               if (i == main_body) {
-                       if (main_body > 0) {
+               if (i == body_pos) {
+                       if (body_pos > 0) {
                                if (open_font) {
                                        column += running_font.latexWriteEndChanges(os, basefont, basefont);
                                        open_font = false;
@@ -1190,7 +1119,7 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
                {
                        column += running_font.latexWriteEndChanges(os,
                                                                    basefont,
-                                                                   (i == main_body-1) ? basefont : font);
+                                                                   (i == body_pos-1) ? basefont : font);
                        running_font = basefont;
                        open_font = false;
                }
@@ -1198,7 +1127,7 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
                // Blanks are printed before start of fontswitch
                if (c == ' ') {
                        // Do not print the separation of the optional argument
-                       if (i != main_body - 1) {
+                       if (i != body_pos - 1) {
                                pimpl_->simpleTeXBlanks(os, texrow, i,
                                                       column, font, *style);
                        }
@@ -1207,7 +1136,7 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
                // Do we need to change font?
                if ((font != running_font ||
                     font.language() != running_font.language()) &&
-                       i != main_body - 1)
+                       i != body_pos - 1)
                {
                        column += font.latexWriteStartChanges(os, basefont,
                                                              last_font);
@@ -1220,38 +1149,12 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
                column += Changes::latexMarkChange(os, running_change, change);
                running_change = change;
 
-               if (c == Paragraph::META_NEWLINE) {
-                       // newlines are handled differently here than
-                       // the default in SimpleTeXSpecialChars().
-                       if (!style->newline_allowed) {
-                               os << '\n';
-                       } else {
-                               if (open_font) {
-                                       column += running_font.latexWriteEndChanges(os, basefont, basefont);
-                                       open_font = false;
-                               }
-                               basefont = getLayoutFont(bparams);
-                               running_font = basefont;
-                               if (font.family() ==
-                                   LyXFont::TYPEWRITER_FAMILY) {
-                                       os << '~';
-                               }
-                               if (moving_arg)
-                                       os << "\\protect ";
-
-                               os << "\\\\\n";
-                       }
-                       texrow.newline();
-                       texrow.start(this, i + 1);
-                       column = 0;
-               } else {
-                       pimpl_->simpleTeXSpecialChars(buf, bparams,
-                                                     os, texrow, moving_arg,
-                                                     font, running_font,
-                                                     basefont, open_font,
-                                                     running_change,
-                                                     *style, i, column, c);
-               }
+               pimpl_->simpleTeXSpecialChars(buf, bparams,
+                                             os, texrow, moving_arg,
+                                             font, running_font,
+                                             basefont, open_font,
+                                             running_change,
+                                             *style, i, column, c);
        }
 
        column += Changes::latexMarkChange(os,
@@ -1280,7 +1183,7 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
        }
 
        // Needed if there is an optional argument but no contents.
-       if (main_body > 0 && main_body == size()) {
+       if (body_pos > 0 && body_pos == size()) {
                os << "]~";
                return_value = false;
        }
@@ -1298,7 +1201,8 @@ bool Paragraph::simpleTeXOnePar(Buffer const * buf,
 
 bool Paragraph::isHfill(pos_type pos) const
 {
-       return IsHfillChar(getChar(pos));
+       return IsInsetChar(getChar(pos))
+              && getInset(pos)->lyxCode() == Inset::HFILL_CODE;
 }
 
 
@@ -1310,7 +1214,8 @@ bool Paragraph::isInset(pos_type pos) const
 
 bool Paragraph::isNewline(pos_type pos) const
 {
-       return pos >= 0 && IsNewlineChar(getChar(pos));
+       return IsInsetChar(getChar(pos))
+              && getInset(pos)->lyxCode() == Inset::NEWLINE_CODE;
 }
 
 
@@ -1431,25 +1336,20 @@ string const Paragraph::asString(Buffer const * buffer, bool label) const
 string const Paragraph::asString(Buffer const * buffer,
                                 pos_type beg, pos_type end, bool label) const
 {
-       ostringstream ost;
+       ostringstream os;
 
        if (beg == 0 && label && !params().labelString().empty())
-               ost << params().labelString() << ' ';
+               os << params().labelString() << ' ';
 
        for (pos_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);
-               }
+                       os << c;
+               else if (c == META_INSET)
+                       getInset(i)->ascii(buffer, os);
        }
 
-       return STRCONV(ost.str());
+       return STRCONV(os.str());
 }
 
 
@@ -1458,10 +1358,9 @@ void Paragraph::setInsetOwner(Inset * i)
        pimpl_->inset_owner = i;
        InsetList::iterator it = insetlist.begin();
        InsetList::iterator end = insetlist.end();
-       for (; it != end; ++it) {
+       for (; it != end; ++it)
                if (it.getInset())
                        it.getInset()->setOwner(i);
-       }
 }