]> git.lyx.org Git - lyx.git/blobdiff - src/paragraph.C
do not create invalid .lyx files when importing \i{}, \j{}, \l{} or \L{}.
[lyx.git] / src / paragraph.C
index 43a4f2722284a28f3bbad480867c224ff6110a2d..b030b7ff8fe796e6fb84c52a5f9326256510faa7 100644 (file)
@@ -56,6 +56,8 @@
 
 namespace lyx {
 
+using lyx::support::contains;
+using lyx::support::rsplit;
 using support::subst;
 
 using std::distance;
@@ -153,13 +155,12 @@ void Paragraph::write(Buffer const & buf, ostream & os,
        LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
 
        Change running_change = Change(Change::UNCHANGED);
-       time_type const curtime = current_time();
 
        int column = 0;
        for (pos_type i = 0; i <= size(); ++i) {
 
                Change change = pimpl_->lookupChange(i);
-               Changes::lyxMarkChange(os, column, curtime, running_change, change);
+               Changes::lyxMarkChange(os, column, running_change, change);
                running_change = change;
 
                if (i == size())
@@ -241,9 +242,9 @@ bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
 }
 
 
-int Paragraph::erase(pos_type start, pos_type end, bool trackChanges)
+int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
 {
-       return pimpl_->erase(start, end, trackChanges);
+       return pimpl_->eraseChars(start, end, trackChanges);
 }
 
 
@@ -568,7 +569,7 @@ int Paragraph::stripLeadingSpaces()
 
        int i = 0;
        while (!empty() && (isNewline(0) || isLineSeparator(0))
-               && (lookupChange(0).type != Change::DELETED)) {
+               && !isDeleted(0)) {
                eraseChar(0, false); // no change tracking here
                ++i;
        }
@@ -737,7 +738,21 @@ string const corrected_env(string const & suffix, string const & env,
                output += correction(env);
        else
                output += env;
-       return output + "}";
+       output += "}";
+       if (suffix == "\\begin")
+               output += "\n";
+       return output;
+}
+
+
+int adjust_column_count(string const & str, int oldcol)
+{
+       if (!contains(str, "\n"))
+               return oldcol + str.size();
+       else {
+               string tmp;
+               return rsplit(str, tmp, '\n').size();
+       }
 }
 
 } // namespace anon
@@ -783,7 +798,7 @@ int Paragraph::startTeXParParams(BufferParams const & bparams,
                else
                        output = corrected_env("\\begin", "flushright", ownerCode());
                os << from_ascii(output);
-               column += output.size();
+               column = adjust_column_count(output, column);
                break;
        } case LYX_ALIGN_RIGHT: {
                string output;
@@ -792,13 +807,13 @@ int Paragraph::startTeXParParams(BufferParams const & bparams,
                else
                        output = corrected_env("\\begin", "flushleft", ownerCode());
                os << from_ascii(output);
-               column += output.size();
+               column = adjust_column_count(output, column);
                break;
        } case LYX_ALIGN_CENTER: {
                string output;
                output = corrected_env("\\begin", "center", ownerCode());
                os << from_ascii(output);
-               column += output.size();
+               column = adjust_column_count(output, column);
                break;
        }
        }
@@ -838,26 +853,26 @@ int Paragraph::endTeXParParams(BufferParams const & bparams,
        case LYX_ALIGN_LEFT: {
                string output;
                if (getParLanguage(bparams)->babel() != "hebrew")
-                       output = corrected_env("\\par\\end", "flushleft", ownerCode());
+                       output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
                else
-                       output = corrected_env("\\par\\end", "flushright", ownerCode());
+                       output = corrected_env("\n\\par\\end", "flushright", ownerCode());
                os << from_ascii(output);
-               column += output.size();
+               column = adjust_column_count(output, column);
                break;
        } case LYX_ALIGN_RIGHT: {
                string output;
                if (getParLanguage(bparams)->babel() != "hebrew")
-                       output = corrected_env("\\par\\end", "flushright", ownerCode());
+                       output = corrected_env("\n\\par\\end", "flushright", ownerCode());
                else
-                       output = corrected_env("\\par\\end", "flushleft", ownerCode());
+                       output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
                os << from_ascii(output);
-               column += output.size();
+               column = adjust_column_count(output, column);
                break;
        } case LYX_ALIGN_CENTER: {
                string output;
-               output = corrected_env("\\par\\end", "center", ownerCode());
+               output = corrected_env("\n\\par\\end", "center", ownerCode());
                os << from_ascii(output);
-               column += output.size();
+               column = adjust_column_count(output, column);
                break;
        }
        }
@@ -921,7 +936,7 @@ bool Paragraph::simpleTeXOnePar(Buffer const & buf,
        // Do we have an open font change?
        bool open_font = false;
 
-       Change::Type running_change = Change::UNCHANGED;
+       Change::Type runningChangeType = Change::UNCHANGED;
 
        texrow.start(id(), 0);
 
@@ -1009,15 +1024,15 @@ bool Paragraph::simpleTeXOnePar(Buffer const & buf,
                        open_font = true;
                }
 
-               Change::Type change = pimpl_->lookupChange(i).type;
+               Change::Type changeType = pimpl_->lookupChange(i).type;
 
-               column += Changes::latexMarkChange(os, running_change,
-                       change, output);
-               running_change = change;
+               column += Changes::latexMarkChange(os, runningChangeType,
+                       changeType, output);
+               runningChangeType = changeType;
 
                // do not output text which is marked deleted
                // if change tracking output is not desired
-               if (output || running_change != Change::DELETED) {
+               if (output || runningChangeType != Change::DELETED) {
                        OutputParams rp = runparams;
                        rp.free_spacing = style->free_spacing;
                        rp.local_font = &font;
@@ -1026,13 +1041,13 @@ bool Paragraph::simpleTeXOnePar(Buffer const & buf,
                                                os, texrow, rp,
                                                font, running_font,
                                                basefont, outerfont, open_font,
-                                               running_change,
+                                               runningChangeType,
                                                *style, i, column, c);
                }
        }
 
        column += Changes::latexMarkChange(os,
-                       running_change, Change::UNCHANGED, output);
+                       runningChangeType, Change::UNCHANGED, output);
 
        // If we have an open font definition, we have to close it
        if (open_font) {
@@ -1241,7 +1256,6 @@ void Paragraph::simpleDocBookOnePar(Buffer const & buf,
                        if (style->pass_thru)
                                 os.put(c);
                        else
-                                // FIXME UNICODE
                                 os << sgml::escapeChar(c);
                }
                font_old = font;
@@ -1309,7 +1323,8 @@ bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
 void Paragraph::changeLanguage(BufferParams const & bparams,
                               Language const * from, Language const * to)
 {
-       for (pos_type i = 0; i < size(); ++i) {
+       // change language including dummy font change at the end
+       for (pos_type i = 0; i <= size(); ++i) {
                LyXFont font = getFontSettings(bparams, i);
                if (font.language() == from) {
                        font.setLanguage(to);
@@ -1393,7 +1408,6 @@ docstring const Paragraph::asString(Buffer const & buffer,
 
        for (pos_type i = beg; i < end; ++i) {
                value_type const c = getUChar(buffer.params(), i);
-               // FIXME: isPrintable does not work for lyx::char_type
                if (isPrintable(c))
                        os.put(c);
                else if (c == META_INSET)
@@ -1410,12 +1424,6 @@ void Paragraph::setInsetOwner(InsetBase * inset)
 }
 
 
-void Paragraph::setContentsFromPar(Paragraph const & par)
-{
-       pimpl_->setContentsFromPar(par);
-}
-
-
 Change const Paragraph::lookupChange(pos_type pos) const
 {
        BOOST_ASSERT(pos <= size());
@@ -1429,6 +1437,12 @@ bool Paragraph::isChanged(pos_type start, pos_type end) const
 }
 
 
+bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
+{
+       return pimpl_->isMergedOnEndOfParDeletion(trackChanges);
+}
+
+
 void Paragraph::setChange(Change const & change)
 {
        pimpl_->setChange(change);
@@ -1441,15 +1455,15 @@ void Paragraph::setChange(pos_type pos, Change const & change)
 }
 
 
-void Paragraph::acceptChange(pos_type start, pos_type end)
+void Paragraph::acceptChanges(pos_type start, pos_type end)
 {
-       return pimpl_->acceptChange(start, end);
+       return pimpl_->acceptChanges(start, end);
 }
 
 
-void Paragraph::rejectChange(pos_type start, pos_type end)
+void Paragraph::rejectChanges(pos_type start, pos_type end)
 {
-       return pimpl_->rejectChange(start, end);
+       return pimpl_->rejectChanges(start, end);
 }
 
 
@@ -1577,6 +1591,7 @@ char_type Paragraph::transformChar(char_type c, pos_type pos) const
 {
        if (!Encodings::is_arabic(c))
                if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isDigit(c))
+                       // FIXME UNICODE What does this do?
                        return c + (0xb0 - '0');
                else
                        return c;