]> git.lyx.org Git - lyx.git/blobdiff - src/Text.cpp
Natbib authoryear uses (Ref1; Ref2) by default.
[lyx.git] / src / Text.cpp
index 25f58d55dd89a1915e6a6592b12da989f90f8d7e..38e865f039158e142972385bd41428aec59a0b5b 100644 (file)
@@ -30,7 +30,6 @@
 #include "Cursor.h"
 #include "CutAndPaste.h"
 #include "DispatchResult.h"
-#include "Encoding.h"
 #include "ErrorList.h"
 #include "FuncRequest.h"
 #include "factory.h"
@@ -46,7 +45,6 @@
 #include "ParIterator.h"
 #include "TextClass.h"
 #include "TextMetrics.h"
-#include "VSpace.h"
 #include "WordLangTuple.h"
 #include "WordList.h"
 
 #include "insets/InsetNewline.h"
 #include "insets/InsetNewpage.h"
 #include "insets/InsetArgument.h"
+#include "insets/InsetIPAMacro.h"
 #include "insets/InsetSpace.h"
 #include "insets/InsetSpecialChar.h"
 #include "insets/InsetTabular.h"
 
+#include "support/convert.h"
 #include "support/debug.h"
 #include "support/docstream.h"
 #include "support/gettext.h"
 
 #include <boost/next_prior.hpp>
 
+#include <limits>
 #include <sstream>
 
+
+// TODO: replace if in Text::readParToken() with compile time switch
+#if 0
+
+#include "support/metahash.h"
+
+typedef boost::mpl::string<'\\end','_lay','out'> end_layout;
+typedef boost::mpl::string<'\\end','in','set'>   end_inset;
+
+void foo()
+{
+       std::string token = "\\end_layout";
+
+       switch (boost::hash_value(token)) {
+               case lyx::support::hash_string<end_layout>::value:
+                       return;
+               case lyx::support::hash_string<end_inset>::value:
+                       return;
+               default: ;
+       };
+
+}
+#endif
+
+
 using namespace std;
 using namespace lyx::support;
 
@@ -117,7 +143,7 @@ void breakParagraphConservative(BufferParams const & bparams,
        tmp.setInsetOwner(&par.inInset());
        tmp.makeSameLayout(par);
 
-       LASSERT(pos <= par.size(), /**/);
+       LASSERT(pos <= par.size(), return);
 
        if (pos < par.size()) {
                // move everything behind the break position to the new paragraph
@@ -238,15 +264,28 @@ bool Text::isFirstInSequence(pit_type par_offset) const
 }
 
 
+int Text::getTocLevel(pit_type par_offset) const
+{
+       Paragraph const & par = pars_[par_offset];
+
+       if (par.layout().isEnvironment() && !isFirstInSequence(par_offset))
+               return Layout::NOT_IN_TOC;
+
+       return par.layout().toclevel;
+}
+
+
 Font const Text::outerFont(pit_type par_offset) const
 {
        depth_type par_depth = pars_[par_offset].getDepth();
        FontInfo tmpfont = inherit_font;
-
+       depth_type prev_par_depth = 0;
        // Resolve against environment font information
        while (par_offset != pit_type(pars_.size())
+              && par_depth != prev_par_depth
               && par_depth
               && !tmpfont.resolved()) {
+               prev_par_depth = par_depth;
                par_offset = outerHook(par_offset);
                if (par_offset != pit_type(pars_.size())) {
                        tmpfont.realize(pars_[par_offset].layout().font);
@@ -317,6 +356,7 @@ InsetText const & Text::inset() const
 }
 
 
+
 void Text::readParToken(Paragraph & par, Lexer & lex,
        string const & token, Font & font, Change & change, ErrorList & errorList)
 {
@@ -356,7 +396,13 @@ void Text::readParToken(Paragraph & par, Lexer & lex,
                // of this document. For example, when you apply class article to a beamer document,
                // all unknown layouts such as frame will be added to document class article so that
                // these layouts can keep their original names.
-               tclass.addLayoutIfNeeded(layoutname);
+               bool const added_one = tclass.addLayoutIfNeeded(layoutname);
+               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()));
+               }
 
                par.setLayout(bp.documentClass()[layoutname]);
 
@@ -445,6 +491,12 @@ void Text::readParToken(Paragraph & par, Lexer & lex,
                inset->read(lex);
                inset->setBuffer(*buf);
                par.insertInset(par.size(), inset.release(), font, change);
+       } else if (token == "\\IPAChar") {
+               auto_ptr<Inset> inset;
+               inset.reset(new InsetIPAChar);
+               inset->read(lex);
+               inset->setBuffer(*buf);
+               par.insertInset(par.size(), inset.release(), font, change);
        } else if (token == "\\backslash") {
                par.appendChar('\\', font, change);
        } else if (token == "\\LyXTable") {
@@ -530,7 +582,7 @@ class TextCompletionList : public CompletionList
 public:
        ///
        TextCompletionList(Cursor const & cur, WordList const * list)
-               : buffer_(cur.buffer()), pos_(0), list_(list)
+               : buffer_(cur.buffer()), list_(list)
        {}
        ///
        virtual ~TextCompletionList() {}
@@ -552,8 +604,6 @@ private:
        ///
        Buffer const * buffer_;
        ///
-       size_t pos_;
-       ///
        WordList const * list_;
 };
 
@@ -674,7 +724,7 @@ static void breakParagraph(Text & text, pit_type par_offset, pos_type pos,
 
 void Text::breakParagraph(Cursor & cur, bool inverse_logic)
 {
-       LASSERT(this == cur.text(), /**/);
+       LBUFERR(this == cur.text());
 
        Paragraph & cpar = cur.paragraph();
        pit_type cpit = cur.pit();
@@ -690,8 +740,7 @@ void Text::breakParagraph(Cursor & cur, bool inverse_logic)
                return;
        }
 
-       // a layout change may affect also the following paragraph
-       recUndo(cur, cur.pit(), undoSpan(cur.pit()) - 1);
+       cur.recordUndo();
 
        // Always break behind a space
        // It is better to erase the space (Dekel)
@@ -749,12 +798,12 @@ void Text::breakParagraph(Cursor & cur, bool inverse_logic)
 
 
 // needed to insert the selection
-void Text::insertStringAsLines(DocIterator const & dit, docstring const & str,
+void Text::insertStringAsLines(Cursor & cur, docstring const & str,
                Font const & font)
 {
        BufferParams const & bparams = owner_->buffer().params();
-       pit_type pit = dit.pit();
-       pos_type pos = dit.pos();
+       pit_type pit = cur.pit();
+       pos_type pos = cur.pos();
 
        // insert the string, don't insert doublespace
        bool space_inserted = true;
@@ -796,12 +845,13 @@ void Text::insertStringAsLines(DocIterator const & dit, docstring const & str,
                        space_inserted = (*cit == ' ');
                }
        }
+       setCursor(cur, pit, pos);
 }
 
 
 // turn double CR to single CR, others are converted into one
 // blank. Then insertStringAsLines is called
-void Text::insertStringAsParagraphs(DocIterator const & dit, docstring const & str,
+void Text::insertStringAsParagraphs(Cursor & cur, docstring const & str,
                Font const & font)
 {
        docstring linestr = str;
@@ -823,7 +873,7 @@ void Text::insertStringAsParagraphs(DocIterator const & dit, docstring const & s
                        newline_inserted = false;
                }
        }
-       insertStringAsLines(dit, linestr, font);
+       insertStringAsLines(cur, linestr, font);
 }
 
 
@@ -831,7 +881,7 @@ void Text::insertStringAsParagraphs(DocIterator const & dit, docstring const & s
 // same Paragraph one to the right and make a rebreak
 void Text::insertChar(Cursor & cur, char_type c)
 {
-       LASSERT(this == cur.text(), /**/);
+       LBUFERR(this == cur.text());
 
        cur.recordUndo(INSERT_UNDO);
 
@@ -940,6 +990,7 @@ void Text::insertChar(Cursor & cur, char_type c)
                                        "beginning of a paragraph. Please read the Tutorial."));
                        return;
                }
+               // LASSERT: Is it safe to continue here?
                LASSERT(cur.pos() > 0, /**/);
                if ((par.isLineSeparator(cur.pos() - 1) || par.isNewline(cur.pos() - 1))
                                && !par.isDeleted(cur.pos() - 1)) {
@@ -980,7 +1031,7 @@ void Text::charInserted(Cursor & cur)
            && !par.isWordSeparator(cur.pos() - 2)
            && par.isWordSeparator(cur.pos() - 1)) {
                // get the word in front of cursor
-               LASSERT(this == cur.text(), /**/);
+               LBUFERR(this == cur.text());
                cur.paragraph().updateWords();
        }
 }
@@ -991,7 +1042,7 @@ void Text::charInserted(Cursor & cur)
 
 bool Text::cursorForwardOneWord(Cursor & cur)
 {
-       LASSERT(this == cur.text(), /**/);
+       LBUFERR(this == cur.text());
 
        pos_type const lastpos = cur.lastpos();
        pit_type pit = cur.pit();
@@ -1017,7 +1068,7 @@ bool Text::cursorForwardOneWord(Cursor & cur)
                else while (pos != lastpos && !par.isWordSeparator(pos))
                             ++pos;
        } else {
-               LASSERT(pos < lastpos, /**/); // see above
+               LASSERT(pos < lastpos, return false); // see above
                if (!par.isWordSeparator(pos))
                        while (pos != lastpos && !par.isWordSeparator(pos))
                                ++pos;
@@ -1038,7 +1089,7 @@ bool Text::cursorForwardOneWord(Cursor & cur)
 
 bool Text::cursorBackwardOneWord(Cursor & cur)
 {
-       LASSERT(this == cur.text(), /**/);
+       LBUFERR(this == cur.text());
 
        pit_type pit = cur.pit();
        pos_type pos = cur.pos();
@@ -1079,7 +1130,7 @@ bool Text::cursorBackwardOneWord(Cursor & cur)
 
 bool Text::cursorVisLeftOneWord(Cursor & cur)
 {
-       LASSERT(this == cur.text(), /**/);
+       LBUFERR(this == cur.text());
 
        pos_type left_pos, right_pos;
        bool left_is_letter, right_is_letter;
@@ -1116,7 +1167,7 @@ bool Text::cursorVisLeftOneWord(Cursor & cur)
 
 bool Text::cursorVisRightOneWord(Cursor & cur)
 {
-       LASSERT(this == cur.text(), /**/);
+       LBUFERR(this == cur.text());
 
        pos_type left_pos, right_pos;
        bool left_is_letter, right_is_letter;
@@ -1155,7 +1206,7 @@ bool Text::cursorVisRightOneWord(Cursor & cur)
 
 void Text::selectWord(Cursor & cur, word_location loc)
 {
-       LASSERT(this == cur.text(), /**/);
+       LBUFERR(this == cur.text());
        CursorSlice from = cur.top();
        CursorSlice to = cur.top();
        getWord(from, to, loc);
@@ -1173,7 +1224,7 @@ void Text::selectWord(Cursor & cur, word_location loc)
 
 void Text::selectAll(Cursor & cur)
 {
-       LASSERT(this == cur.text(), /**/);
+       LBUFERR(this == cur.text());
        if (cur.lastpos() == 0 && cur.lastpit() == 0)
                return;
        // If the cursor is at the beginning, make sure the cursor ends there
@@ -1194,7 +1245,7 @@ void Text::selectAll(Cursor & cur)
 // selection is currently set
 bool Text::selectWordWhenUnderCursor(Cursor & cur, word_location loc)
 {
-       LASSERT(this == cur.text(), /**/);
+       LBUFERR(this == cur.text());
        if (cur.selection())
                return false;
        selectWord(cur, loc);
@@ -1204,7 +1255,7 @@ bool Text::selectWordWhenUnderCursor(Cursor & cur, word_location loc)
 
 void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
 {
-       LASSERT(this == cur.text(), /**/);
+       LBUFERR(this == cur.text());
 
        if (!cur.selection()) {
                bool const changed = cur.paragraph().isChanged(cur.pos());
@@ -1243,7 +1294,11 @@ void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
 
                pos_type left  = (pit == begPit ? begPos : 0);
                pos_type right = (pit == endPit ? endPos : parSize);
-
+               
+               if (left == right)
+                       // there is no change here
+                       continue;
+               
                if (op == ACCEPT) {
                        pars_[pit].acceptChanges(left, right);
                } else {
@@ -1355,7 +1410,7 @@ void Text::rejectChanges()
 
 void Text::deleteWordForward(Cursor & cur)
 {
-       LASSERT(this == cur.text(), /**/);
+       LBUFERR(this == cur.text());
        if (cur.lastpos() == 0)
                cursorForward(cur);
        else {
@@ -1371,7 +1426,7 @@ void Text::deleteWordForward(Cursor & cur)
 
 void Text::deleteWordBackward(Cursor & cur)
 {
-       LASSERT(this == cur.text(), /**/);
+       LBUFERR(this == cur.text());
        if (cur.lastpos() == 0)
                cursorBackward(cur);
        else {
@@ -1388,7 +1443,7 @@ void Text::deleteWordBackward(Cursor & cur)
 // Kill to end of line.
 void Text::changeCase(Cursor & cur, TextCase action)
 {
-       LASSERT(this == cur.text(), /**/);
+       LBUFERR(this == cur.text());
        CursorSlice from;
        CursorSlice to;
 
@@ -1517,7 +1572,7 @@ bool Text::erase(Cursor & cur)
 
 bool Text::backspacePos0(Cursor & cur)
 {
-       LASSERT(this == cur.text(), /**/);
+       LBUFERR(this == cur.text());
        if (cur.pit() == 0)
                return false;
 
@@ -1570,17 +1625,19 @@ bool Text::backspacePos0(Cursor & cur)
 
 bool Text::backspace(Cursor & cur)
 {
-       LASSERT(this == cur.text(), /**/);
+       LBUFERR(this == cur.text());
        bool needsUpdate = false;
        if (cur.pos() == 0) {
                if (cur.pit() == 0)
                        return dissolveInset(cur);
 
-               Paragraph & prev_par = pars_[cur.pit() - 1];
+               Cursor prev_cur = cur;
+               --prev_cur.pit();
 
-               if (!prev_par.isMergedOnEndOfParDeletion(cur.buffer()->params().trackChanges)) {
-                       prev_par.setChange(prev_par.size(), Change(Change::DELETED));
-                       setCursorIntern(cur, cur.pit() - 1, prev_par.size());
+               if (!prev_cur.paragraph().isMergedOnEndOfParDeletion(cur.buffer()->params().trackChanges)) {
+                       cur.recordUndo(ATOMIC_UNDO, prev_cur.pit(), prev_cur.pit());
+                       prev_cur.paragraph().setChange(prev_cur.lastpos(), Change(Change::DELETED));
+                       setCursorIntern(cur, prev_cur.pit(), prev_cur.lastpos());
                        return true;
                }
                // The cursor is at the beginning of a paragraph, so
@@ -1641,13 +1698,23 @@ bool Text::dissolveInset(Cursor & cur)
        spit += cur.pit();
        Buffer & b = *cur.buffer();
        cur.paragraph().eraseChar(cur.pos(), b.params().trackChanges);
+
        if (!plist.empty()) {
+               // see bug 7319
+               // we clear the cache so that we won't get conflicts with labels
+               // that get pasted into the buffer. we should update this before
+               // its being empty matters. if not (i.e., if we encounter bugs),
+               // then this should instead be:
+               //        cur.buffer().updateBuffer();
+               // but we'll try the cheaper solution here.
+               cur.buffer()->clearReferenceCache();
+
                // ERT paragraphs have the Language latex_language.
                // This is invalid outside of ERT, so we need to
                // change it to the buffer language.
                ParagraphList::iterator it = plist.begin();
                ParagraphList::iterator it_end = plist.end();
-               for (; it != it_end; it++)
+               for (; it != it_end; ++it)
                        it->changeLanguage(b.params(), latex_language, b.language());
 
                pasteParagraphList(cur, plist, b.params().documentClassPtr(),
@@ -1655,8 +1722,9 @@ bool Text::dissolveInset(Cursor & cur)
                // restore position
                cur.pit() = min(cur.lastpit(), spit);
                cur.pos() = min(cur.lastpos(), spos);
-       } else
-               cur.forceBufferUpdate();
+       }
+
+       cur.forceBufferUpdate();
 
        // Ensure the current language is set correctly (bug 6292)
        cur.text()->setCursor(cur, cur.pit(), cur.pos());
@@ -1755,10 +1823,11 @@ bool Text::read(Lexer & lex,
        return res;
 }
 
+
 // Returns the current font and depth as a message.
 docstring Text::currentState(Cursor const & cur) const
 {
-       LASSERT(this == cur.text(), /**/);
+       LBUFERR(this == cur.text());
        Buffer & buf = *cur.buffer();
        Paragraph const & par = cur.paragraph();
        odocstringstream os;
@@ -1880,7 +1949,7 @@ docstring Text::getPossibleLabel(Cursor const & cur) const
        // For captions, we just take the caption type
        Inset * caption_inset = cur.innerInsetOfType(CAPTION_CODE);
        if (caption_inset) {
-               string const & ftype = static_cast<InsetCaption *>(caption_inset)->type();
+               string const & ftype = static_cast<InsetCaption *>(caption_inset)->floattype();
                FloatList const & fl = cur.buffer()->params().documentClass().floats();
                if (fl.typeExist(ftype)) {
                        Floating const & flt = fl.getType(ftype);
@@ -1897,11 +1966,17 @@ docstring Text::getPossibleLabel(Cursor const & cur) const
        }
 
        if (!name.empty())
-               // FIXME refstyle
-               // We should allow customization of the separator or else change it
                text = name + ':' + text;
 
-       return text;
+       // We need a unique label
+       docstring label = text;
+       int i = 1;
+       while (cur.buffer()->insetLabel(label)) {
+                       label = text + '-' + convert<docstring>(i);
+                       ++i;
+               }
+
+       return label;
 }
 
 
@@ -1923,10 +1998,22 @@ docstring Text::asString(pit_type beg, pit_type end, int options) const
 }
 
 
+void Text::forToc(docstring & os, size_t maxlen, bool shorten) const
+{
+       if (maxlen == 0)
+               maxlen = std::numeric_limits<std::size_t>::max();
+       else
+               LASSERT(maxlen >= 8, maxlen = TOC_ENTRY_LENGTH);
+       for (size_t i = 0; i != pars_.size() && os.length() < maxlen; ++i)
+               pars_[i].forToc(os, maxlen);
+       if (shorten && os.length() >= maxlen)
+               os = os.substr(0, maxlen - 3) + from_ascii("...");
+}
+
 
 void Text::charsTranspose(Cursor & cur)
 {
-       LASSERT(this == cur.text(), /**/);
+       LBUFERR(this == cur.text());
 
        pos_type pos = cur.pos();
 
@@ -2026,7 +2113,7 @@ CompletionList const * Text::createCompletionList(Cursor const & cur) const
 
 bool Text::insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/)
 {      
-       LASSERT(cur.bv().cursor() == cur, /**/);
+       LBUFERR(cur.bv().cursor() == cur);
        cur.insert(s);
        cur.bv().cursor() = cur;
        if (!(cur.result().screenUpdate() & Update::Force))