]> git.lyx.org Git - lyx.git/blobdiff - src/Paragraph.cpp
GuiToc::initialiseParams(): Fix list type parsing
[lyx.git] / src / Paragraph.cpp
index 78b58c51a75980b61003c3b9f310b471c8fbc2c9..b5f31281f4da9ee0dff6d11b8825bdb8a68a63f1 100644 (file)
@@ -53,7 +53,6 @@
 #include "support/lstrings.h"
 #include "support/Messages.h"
 #include "support/textutils.h"
-#include "support/unicode.h"
 
 #include <sstream>
 #include <vector>
@@ -183,10 +182,6 @@ public:
        ///
        ParagraphParameters params_;
 
-       /// position of the paragraph in the buffer. Only macros from
-       /// paragraphs strictly smaller are visible in this paragraph
-       unsigned int macrocontext_position_;
-       
        /// for recording and looking up changes
        Changes changes_;
 
@@ -232,7 +227,6 @@ Paragraph::Private::Private(Paragraph * owner)
        : owner_(owner), inset_owner_(0), begin_of_body_(0)
 {
        id_ = paragraph_id++;
-       macrocontext_position_ = 0;
        text_.reserve(100);
 }
 
@@ -255,15 +249,13 @@ bool Paragraph::isChanged(pos_type start, pos_type end) const
 }
 
 
-bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const {
+bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
+{
        // keep the logic here in sync with the logic of eraseChars()
-
-       if (!trackChanges) {
+       if (!trackChanges)
                return true;
-       }
-
-       Change change = d->changes_.lookup(size());
 
+       Change const change = d->changes_.lookup(size());
        return change.type == Change::INSERTED && change.author == 0;
 }
 
@@ -287,8 +279,8 @@ void Paragraph::setChange(Change const & change)
 
        if (change.type != Change::DELETED) {
                for (pos_type pos = 0; pos < size(); ++pos) {
-                       if (isInset(pos))
-                               getInset(pos)->setChange(change);
+                       if (Inset * inset = getInset(pos))
+                               inset->setChange(change);
                }
        }
 }
@@ -297,22 +289,18 @@ void Paragraph::setChange(Change const & change)
 void Paragraph::setChange(pos_type pos, Change const & change)
 {
        BOOST_ASSERT(pos >= 0 && pos <= size());
-
        d->changes_.set(change, pos);
 
        // see comment in setChange(Change const &) above
-
-       if (change.type != Change::DELETED &&
-           pos < size() && isInset(pos)) {
-               getInset(pos)->setChange(change);
-       }
+       if (change.type != Change::DELETED && pos < size())
+                       if (Inset * inset = getInset(pos))
+                               inset->setChange(change);
 }
 
 
 Change const & Paragraph::lookupChange(pos_type pos) const
 {
        BOOST_ASSERT(pos >= 0 && pos <= size());
-
        return d->changes_.lookup(pos);
 }
 
@@ -327,17 +315,15 @@ void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start,
                switch (lookupChange(pos).type) {
                        case Change::UNCHANGED:
                                // accept changes in nested inset
-                               if (pos < size() && isInset(pos))
-                                       getInset(pos)->acceptChanges(bparams);
-
+                               if (Inset * inset = getInset(pos))
+                                       inset->acceptChanges(bparams);
                                break;
 
                        case Change::INSERTED:
                                d->changes_.set(Change(Change::UNCHANGED), pos);
                                // also accept changes in nested inset
-                               if (pos < size() && isInset(pos)) {
-                                       getInset(pos)->acceptChanges(bparams);
-                               }
+                               if (Inset * inset = getInset(pos))
+                                       inset->acceptChanges(bparams);
                                break;
 
                        case Change::DELETED:
@@ -365,9 +351,8 @@ void Paragraph::rejectChanges(BufferParams const & bparams,
                switch (lookupChange(pos).type) {
                        case Change::UNCHANGED:
                                // reject changes in nested inset
-                               if (pos < size() && isInset(pos)) {
-                                       getInset(pos)->rejectChanges(bparams);
-                               }
+                               if (Inset * inset = getInset(pos))
+                                               inset->rejectChanges(bparams);
                                break;
 
                        case Change::INSERTED:
@@ -915,7 +900,7 @@ bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
                os.put(c);
                // In T1 encoding, these characters exist
                // but we should avoid ligatures
-               if (i + 1 > int(text_.size()) || text_[i + 1] != c)
+               if (i + 1 >= int(text_.size()) || text_[i + 1] != c)
                        return true;
                os << "\\,{}";
                column += 3;
@@ -1126,9 +1111,7 @@ void Paragraph::write(Buffer const & buf, ostream & os,
                char_type const c = d->text_[i];
                switch (c) {
                case META_INSET:
-               {
-                       Inset const * inset = getInset(i);
-                       if (inset)
+                       if (Inset const * inset = getInset(i)) {
                                if (inset->directWrite()) {
                                        // international char, let it write
                                        // code directly so it's shorter in
@@ -1142,8 +1125,8 @@ void Paragraph::write(Buffer const & buf, ostream & os,
                                        os << "\n\\end_inset\n\n";
                                        column = 0;
                                }
-               }
-               break;
+                       }
+                       break;
                case '\\':
                        os << "\n\\backslash\n";
                        column = 0;
@@ -1163,11 +1146,9 @@ void Paragraph::write(Buffer const & buf, ostream & os,
                        }
                        // this check is to amend a bug. LyX sometimes
                        // inserts '\0' this could cause problems.
-                       if (c != '\0') {
-                               vector<char> tmp = ucs4_to_utf8(c);
-                               tmp.push_back('\0');
-                               os << &tmp[0];
-                       } else
+                       if (c != '\0')
+                               os << to_utf8(docstring(1, c));
+                       else
                                lyxerr << "ERROR (Paragraph::writeFile):"
                                        " NULL char in structure." << endl;
                        ++column;
@@ -1519,7 +1500,7 @@ void Paragraph::setLabelWidthString(docstring const & s)
 docstring const Paragraph::translateIfPossible(docstring const & s,
                BufferParams const & bparams) const
 {
-       if (!support::isAscii(s) || s.empty()) {
+       if (!isAscii(s) || s.empty()) {
                // This must be a user defined layout. We cannot translate
                // this, since gettext accepts only ascii keys.
                return s;
@@ -1961,8 +1942,7 @@ bool Paragraph::latex(Buffer const & buf,
                    runparams.encoding->package() == Encoding::none &&
                    font.language()->encoding()->package() == Encoding::none) {
                        pair<bool, int> const enc_switch = switchEncoding(os, bparams,
-                                       runparams, *(runparams.encoding),
-                                       *(font.language()->encoding()));
+                                       runparams, *(font.language()->encoding()));
                        if (enc_switch.first) {
                                column += enc_switch.second;
                                runparams.encoding = font.language()->encoding();
@@ -2025,9 +2005,24 @@ bool Paragraph::latex(Buffer const & buf,
                                        texrow, rp, running_font,
                                        basefont, outerfont, open_font,
                                        runningChange, *style, i, column);
-               else
-                       d->latexSpecialChar(os, rp, running_font, runningChange,
-                               *style, i, column);
+               else {
+                       try {
+                               d->latexSpecialChar(os, rp, running_font, runningChange,
+                                       *style, i, column);
+                       } catch (EncodingException & e) {
+                               if (runparams.dryrun) {
+                                       os << "<" << _("LyX Warning: ")
+                                          << _("uncodable character") << " '";
+                                       os.put(c);
+                                       os << "'>";
+                               } else {
+                                       // add location information and throw again.
+                                       e.par_id = id();
+                                       e.pos = i;
+                                       throw(e);
+                               }
+                       }
+               }
 
                // Set the encoding to that returned from simpleTeXSpecialChars (see
                // comment for encoding member in OutputParams.h)
@@ -2076,8 +2071,7 @@ bool Paragraph::latex(Buffer const & buf,
 bool Paragraph::emptyTag() const
 {
        for (pos_type i = 0; i < size(); ++i) {
-               if (isInset(i)) {
-                       Inset const * inset = getInset(i);
+               if (Inset const * inset = getInset(i)) {
                        InsetCode lyx_code = inset->lyxCode();
                        if (lyx_code != TOC_CODE &&
                            lyx_code != INCLUDE_CODE &&
@@ -2101,8 +2095,7 @@ bool Paragraph::emptyTag() const
 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
 {
        for (pos_type i = 0; i < size(); ++i) {
-               if (isInset(i)) {
-                       Inset const * inset = getInset(i);
+               if (Inset const * inset = getInset(i)) {
                        InsetCode lyx_code = inset->lyxCode();
                        if (lyx_code == LABEL_CODE) {
                                InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
@@ -2110,7 +2103,6 @@ string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) cons
                                return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
                        }
                }
-
        }
        return string();
 }
@@ -2120,8 +2112,7 @@ pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputPara
 {
        pos_type i;
        for (i = 0; i < size(); ++i) {
-               if (isInset(i)) {
-                       Inset const * inset = getInset(i);
+               if (Inset const * inset = getInset(i)) {
                        inset->docbook(buf, os, runparams);
                } else {
                        char_type c = d->text_[i];
@@ -2181,8 +2172,7 @@ void Paragraph::simpleDocBookOnePar(Buffer const & buf,
                        }
                }
 
-               if (isInset(i)) {
-                       Inset const * inset = getInset(i);
+               if (Inset const * inset = getInset(i)) {
                        inset->docbook(buf, os, runparams);
                } else {
                        char_type c = d->text_[i];
@@ -2208,36 +2198,35 @@ void Paragraph::simpleDocBookOnePar(Buffer const & buf,
 
 bool Paragraph::isHfill(pos_type pos) const
 {
-       return isInset(pos)
-               && getInset(pos)->lyxCode() == HFILL_CODE;
+       Inset const * inset = getInset(pos);
+       return inset && inset->lyxCode() == HFILL_CODE;
 }
 
 
 bool Paragraph::isNewline(pos_type pos) const
 {
-       return isInset(pos)
-               && getInset(pos)->lyxCode() == NEWLINE_CODE;
+       Inset const * inset = getInset(pos);
+       return inset && inset->lyxCode() == NEWLINE_CODE;
 }
 
 
 bool Paragraph::isLineSeparator(pos_type pos) const
 {
        char_type const c = d->text_[pos];
-       return isLineSeparatorChar(c)
-               || (c == META_INSET && getInset(pos) &&
-               getInset(pos)->isLineSeparator());
+       if (isLineSeparatorChar(c))
+               return true;
+       Inset const * inset = getInset(pos);
+       return inset && inset->isLineSeparator();
 }
 
 
 /// Used by the spellchecker
 bool Paragraph::isLetter(pos_type pos) const
 {
-       if (isInset(pos))
-               return getInset(pos)->isLetter();
-       else {
-               char_type const c = d->text_[pos];
-               return isLetterChar(c) || isDigit(c);
-       }
+       if (Inset const * inset = getInset(pos))
+               return inset->isLetter();
+       char_type const c = d->text_[pos];
+       return isLetterChar(c) || isDigit(c);
 }
 
 
@@ -2350,8 +2339,7 @@ Inset * Paragraph::inInset() const
 
 InsetCode Paragraph::ownerCode() const
 {
-       return d->inset_owner_ ?
-               d->inset_owner_->lyxCode() : NO_CODE;
+       return d->inset_owner_ ? d->inset_owner_->lyxCode() : NO_CODE;
 }
 
 
@@ -2490,18 +2478,6 @@ int Paragraph::checkBiblio(bool track_changes)
 }
 
 
-unsigned int Paragraph::macrocontextPosition() const
-{
-       return d->macrocontext_position_;
-}
-
-
-void Paragraph::setMacrocontextPosition(unsigned int pos)
-{
-       d->macrocontext_position_ = pos;
-}
-
-
 void Paragraph::checkAuthors(AuthorList const & authorList)
 {
        d->changes_.checkAuthors(authorList);
@@ -2543,18 +2519,20 @@ Inset * Paragraph::releaseInset(pos_type pos)
 
 Inset * Paragraph::getInset(pos_type pos)
 {
-       return d->insetlist_.get(pos);
+       return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
+                ? d->insetlist_.get(pos) : 0;
 }
 
 
 Inset const * Paragraph::getInset(pos_type pos) const
 {
-       return d->insetlist_.get(pos);
+       return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
+                ? d->insetlist_.get(pos) : 0;
 }
 
 
 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
-               pos_type right, TextCase action)
+               pos_type right, TextCase action)
 {
        // process sequences of modified characters; in change
        // tracking mode, this approach results in much better