]> git.lyx.org Git - lyx.git/blobdiff - src/Text.cpp
Account for old versions of Pygments
[lyx.git] / src / Text.cpp
index 00b575b07a70174413c2385c899048450e4e30f8..483f945e039e7eb145b5d1660a42753e011f8405 100644 (file)
@@ -240,6 +240,21 @@ bool Text::isFirstInSequence(pit_type par_offset) const
 }
 
 
+pit_type Text::lastInSequence(pit_type pit) const
+{
+       depth_type const depth = pars_[pit].getDepth();
+       pit_type newpit = pit;
+
+       while (size_t(newpit + 1) < pars_.size() &&
+              (pars_[newpit + 1].getDepth() > depth ||
+               (pars_[newpit + 1].getDepth() == depth &&
+                pars_[newpit + 1].layout() == pars_[pit].layout())))
+               ++newpit;
+
+       return newpit;
+}
+
+
 int Text::getTocLevel(pit_type par_offset) const
 {
        Paragraph const & par = pars_[par_offset];
@@ -376,8 +391,8 @@ void Text::readParToken(Paragraph & par, Lexer & lex,
                if (added_one) {
                        // Warn the user.
                        docstring const s = bformat(_("Layout `%1$s' was not found."), layoutname);
-                       errorList.push_back(
-                               ErrorItem(_("Layout Not Found"), s, par.id(), 0, par.size()));
+                       errorList.push_back(ErrorItem(_("Layout Not Found"), s,
+                                                     {par.id(), 0}, {par.id(), -1}));
                }
 
                par.setLayout(bp.documentClass()[layoutname]);
@@ -403,7 +418,7 @@ void Text::readParToken(Paragraph & par, Lexer & lex,
                        lex.eatLine();
                        docstring line = lex.getDocString();
                        errorList.push_back(ErrorItem(_("Unknown Inset"), line,
-                                           par.id(), 0, par.size()));
+                                                     {par.id(), 0}, {par.id(), -1}));
                }
        } else if (token == "\\family") {
                lex.next();
@@ -449,6 +464,9 @@ void Text::readParToken(Paragraph & par, Lexer & lex,
        } else if (token == "\\strikeout") {
                lex.next();
                font.fontInfo().setStrikeout(setLyXMisc(lex.getString()));
+       } else if (token == "\\xout") {
+               lex.next();
+               font.fontInfo().setXout(setLyXMisc(lex.getString()));
        } else if (token == "\\uuline") {
                lex.next();
                font.fontInfo().setUuline(setLyXMisc(lex.getString()));
@@ -520,8 +538,7 @@ void Text::readParToken(Paragraph & par, Lexer & lex,
                                          "missing until the corresponding tracked changes "
                                          "are merged or this user edits the file again.\n"),
                                        aid),
-                               par.id(), par.size(), par.size() + 1
-                               ));
+                               {par.id(), par.size()}, {par.id(), par.size() + 1}));
                        bp.addAuthor(Author(aid));
                }
                if (token == "\\change_inserted")
@@ -531,9 +548,10 @@ void Text::readParToken(Paragraph & par, Lexer & lex,
        } else {
                lex.eatLine();
                errorList.push_back(ErrorItem(_("Unknown token"),
-                       bformat(_("Unknown token: %1$s %2$s\n"), from_utf8(token),
-                       lex.getDocString()),
-                       par.id(), 0, par.size()));
+                                             bformat(_("Unknown token: %1$s %2$s\n"),
+                                                     from_utf8(token),
+                                                     lex.getDocString()),
+                                             {par.id(), 0}, {par.id(), -1}));
        }
 }
 
@@ -586,7 +604,7 @@ class TextCompletionList : public CompletionList
 {
 public:
        ///
-       TextCompletionList(Cursor const & cur, WordList const * list)
+       TextCompletionList(Cursor const & cur, WordList const & list)
                : buffer_(cur.buffer()), list_(list)
        {}
        ///
@@ -597,19 +615,19 @@ public:
        ///
        virtual size_t size() const
        {
-               return list_->size();
+               return list_.size();
        }
        ///
        virtual docstring const & data(size_t idx) const
        {
-               return list_->word(idx);
+               return list_.word(idx);
        }
 
 private:
        ///
        Buffer const * buffer_;
        ///
-       WordList const * list_;
+       WordList const & list_;
 };
 
 
@@ -738,12 +756,17 @@ void Text::breakParagraph(Cursor & cur, bool inverse_logic)
        Layout const & layout = cpar.layout();
 
        if (cur.lastpos() == 0 && !cpar.allowEmpty()) {
-               if (changeDepthAllowed(cur, DEC_DEPTH))
+               if (changeDepthAllowed(cur, DEC_DEPTH)) {
                        changeDepth(cur, DEC_DEPTH);
-               else {
+                       pit_type const prev = depthHook(cpit, cpar.getDepth());
+                       docstring const & lay = pars_[prev].layout().name();
+                       if (lay != layout.name())
+                               setLayout(cur, lay);
+               } else {
                        docstring const & lay = cur.paragraph().usePlainLayout()
                            ? tclass.plainLayoutName() : tclass.defaultLayoutName();
-                       setLayout(cur, lay);
+                       if (lay != layout.name())
+                               setLayout(cur, lay);
                }
                return;
        }
@@ -1037,11 +1060,6 @@ void Text::insertChar(Cursor & cur, char_type c)
                        par.eraseChar(pos - 1, cur.buffer()->params().track_changes);
                        c = 0x2014;
                        pos--;
-               } else if (par.getChar(pos - 1) == 0x2014) {
-                       // convert "----" to "-"
-                       par.eraseChar(pos - 1, cur.buffer()->params().track_changes);
-                       c = '-';
-                       pos--;
                }
        }
 
@@ -1446,7 +1464,7 @@ void Text::rejectChanges()
 }
 
 
-void Text::deleteWordForward(Cursor & cur)
+void Text::deleteWordForward(Cursor & cur, bool const force)
 {
        LBUFERR(this == cur.text());
        if (cur.lastpos() == 0)
@@ -1456,13 +1474,15 @@ void Text::deleteWordForward(Cursor & cur)
                cur.selection(true);
                cursorForwardOneWord(cur);
                cur.setSelection();
-               cutSelection(cur, true, false);
-               cur.checkBufferStructure();
+               if (force || !cur.confirmDeletion()) {
+                       cutSelection(cur, true, false);
+                       cur.checkBufferStructure();
+               }
        }
 }
 
 
-void Text::deleteWordBackward(Cursor & cur)
+void Text::deleteWordBackward(Cursor & cur, bool const force)
 {
        LBUFERR(this == cur.text());
        if (cur.lastpos() == 0)
@@ -1472,8 +1492,10 @@ void Text::deleteWordBackward(Cursor & cur)
                cur.selection(true);
                cursorBackwardOneWord(cur);
                cur.setSelection();
-               cutSelection(cur, true, false);
-               cur.checkBufferStructure();
+               if (force || !cur.confirmDeletion()) {
+                       cutSelection(cur, true, false);
+                       cur.checkBufferStructure();
+               }
        }
 }
 
@@ -1723,20 +1745,24 @@ bool Text::dissolveInset(Cursor & cur)
        cur.recordUndoInset();
        cur.setMark(false);
        cur.selHandle(false);
-       // save position
+       // save position inside inset
        pos_type spos = cur.pos();
        pit_type spit = cur.pit();
        ParagraphList plist;
        if (cur.lastpit() != 0 || cur.lastpos() != 0)
                plist = paragraphs();
        cur.popBackward();
-       // store cursor offset
+       // update cursor offset
        if (spit == 0)
                spos += cur.pos();
        spit += cur.pit();
-       Buffer & b = *cur.buffer();
-       cur.paragraph().eraseChar(cur.pos(), b.params().track_changes);
+       // remember position outside inset to delete inset later
+       // we do not do it now to avoid memory reuse issues (see #10667).
+       DocIterator inset_it = cur;
+       // jump over inset
+       ++cur.pos();
 
+       Buffer & b = *cur.buffer();
        if (!plist.empty()) {
                // see bug 7319
                // we clear the cache so that we won't get conflicts with labels
@@ -1757,17 +1783,20 @@ bool Text::dissolveInset(Cursor & cur)
 
                pasteParagraphList(cur, plist, b.params().documentClassPtr(),
                                   b.errorList("Paste"));
-               // restore position
-               cur.pit() = min(cur.lastpit(), spit);
-               cur.pos() = min(cur.lastpos(), spos);
        }
 
-       cur.forceBufferUpdate();
+       // delete the inset now
+       inset_it.paragraph().eraseChar(inset_it.pos(), b.params().track_changes);
 
+       // restore position
+       cur.pit() = min(cur.lastpit(), spit);
+       cur.pos() = min(cur.lastpos(), spos);
        // Ensure the current language is set correctly (bug 6292)
        cur.text()->setCursor(cur, cur.pit(), cur.pos());
        cur.clearSelection();
        cur.resetAnchor();
+       cur.forceBufferUpdate();
+
        return true;
 }
 
@@ -2037,20 +2066,36 @@ docstring Text::asString(pit_type beg, pit_type end, int options) const
 void Text::shortenForOutliner(docstring & str, size_t const maxlen)
 {
        support::truncateWithEllipsis(str, maxlen);
-       docstring::iterator it = str.begin();
-       docstring::iterator end = str.end();
-       for (; it != end; ++it)
-               if ((*it) == L'\n' || (*it) == L'\t')
-                       (*it) = L' ';   
+       for (char_type & c : str)
+               if (c == L'\n' || c == L'\t')
+                       c = L' ';
 }
 
 
 void Text::forOutliner(docstring & os, size_t const maxlen,
-                                          bool const shorten) const
+                       bool const shorten) const
+{
+       pit_type end = pars_.size() - 1;
+       if (0 <= end && !pars_[0].labelString().empty())
+               os += pars_[0].labelString() + ' ';
+       forOutliner(os, maxlen, 0, end, shorten);
+}
+
+
+void Text::forOutliner(docstring & os, size_t const maxlen,
+                       pit_type pit_start, pit_type pit_end,
+                       bool const shorten) const
 {
        size_t tmplen = shorten ? maxlen + 1 : maxlen;
-       for (size_t i = 0; i != pars_.size() && os.length() < tmplen; ++i)
-               pars_[i].forOutliner(os, tmplen, false);
+       pit_type end = min(size_t(pit_end), pars_.size() - 1);
+       bool first = true;
+       for (pit_type i = pit_start; i <= end && os.length() < tmplen; ++i) {
+               if (!first)
+                       os += ' ';
+               // This function lets the first label be treated separately
+               pars_[i].forOutliner(os, tmplen, false, !first);
+               first = false;
+       }
        if (shorten)
                shortenForOutliner(os, maxlen);
 }
@@ -2151,7 +2196,7 @@ bool Text::completionSupported(Cursor const & cur) const
 
 CompletionList const * Text::createCompletionList(Cursor const & cur) const
 {
-       WordList const * list = theWordList(cur.getFont().language()->lang());
+       WordList const & list = theWordList(cur.getFont().language()->lang());
        return new TextCompletionList(cur, list);
 }