]> git.lyx.org Git - lyx.git/blobdiff - src/Text.cpp
After a hiatus, I'm returning to the rewrite of InsetCommandParams, the purpose of...
[lyx.git] / src / Text.cpp
index 6913f22d3a1eedfa0be001d896b8104621889a16..456584b8c75341811f06bd2d63dbcc167a7ebab1 100644 (file)
@@ -47,9 +47,6 @@
 #include "VSpace.h"
 #include "WordLangTuple.h"
 
-#include "frontends/FontMetrics.h"
-#include "frontends/Painter.h"
-
 #include "insets/InsetText.h"
 #include "insets/InsetBibitem.h"
 #include "insets/InsetCaption.h"
@@ -81,8 +78,6 @@ namespace lyx {
 using cap::cutSelection;
 using cap::pasteParagraphList;
 
-using frontend::FontMetrics;
-
 namespace {
 
 void readParToken(Buffer const & buf, Paragraph & par, Lexer & lex,
@@ -101,15 +96,15 @@ void readParToken(Buffer const & buf, Paragraph & par, Lexer & lex,
                font = Font(inherit_font, bp.language);
                change = Change(Change::UNCHANGED);
 
-               TextClass const & tclass = bp.getTextClass();
+               TextClass const & tclass = bp.textClass();
 
                if (layoutname.empty())
                        layoutname = tclass.defaultLayoutName();
 
-               if (par.forceEmptyLayout()) 
+               if (par.forceEmptyLayout()) {
                        // in this case only the empty layout is allowed
                        layoutname = tclass.emptyLayoutName();
-               else if (par.useEmptyLayout()) {
+               else if (par.useEmptyLayout()) {
                        // in this case, default layout maps to empty layout 
                        if (layoutname == tclass.defaultLayoutName())
                                layoutname = tclass.emptyLayoutName();
@@ -130,12 +125,12 @@ void readParToken(Buffer const & buf, Paragraph & par, Lexer & lex,
                                        tclass.defaultLayoutName();
                }
 
-               par.layout(bp.getTextClass()[layoutname]);
+               par.setLayout(bp.textClass()[layoutname]);
 
                // Test whether the layout is obsolete.
                LayoutPtr const & layout = par.layout();
                if (!layout->obsoleted_by().empty())
-                       par.layout(bp.getTextClass()[layout->obsoleted_by()]);
+                       par.setLayout(bp.textClass()[layout->obsoleted_by()]);
 
                par.params().read(lex);
 
@@ -345,8 +340,7 @@ bool Text::empty() const
 }
 
 
-double Text::spacing(Buffer const & buffer,
-               Paragraph const & par) const
+double Text::spacing(Buffer const & buffer, Paragraph const & par) const
 {
        if (par.params().spacing().isDefault())
                return buffer.params().spacing().getValue();
@@ -361,7 +355,7 @@ void Text::breakParagraph(Cursor & cur, bool inverse_logic)
        Paragraph & cpar = cur.paragraph();
        pit_type cpit = cur.pit();
 
-       TextClass const & tclass = cur.buffer().params().getTextClass();
+       TextClass const & tclass = cur.buffer().params().textClass();
        LayoutPtr const & layout = cpar.layout();
 
        // this is only allowed, if the current paragraph is not empty
@@ -569,6 +563,8 @@ void Text::insertChar(Cursor & cur, char_type c)
 
 void Text::charInserted(Cursor & cur)
 {
+       Paragraph & par = cur.paragraph();
+
        // Here we call finishUndo for every 20 characters inserted.
        // This is from my experience how emacs does it. (Lgb)
        static unsigned int counter;
@@ -578,6 +574,27 @@ void Text::charInserted(Cursor & cur)
                cur.finishUndo();
                counter = 0;
        }
+
+       // register word if a non-letter was entered
+       if (cur.pos() > 1
+           && par.isLetter(cur.pos() - 2)
+           && !par.isLetter(cur.pos() - 1)) {
+               // get the word in front of cursor
+               BOOST_ASSERT(this == cur.text());
+               CursorSlice focus = cur.top();
+               focus.backwardPos();
+               CursorSlice from = focus;
+               CursorSlice to = focus;
+               getWord(from, to, PREVIOUS_WORD);
+               if (focus == from || to == from)
+                       return;
+               docstring word
+               = par.asString(cur.buffer(), from.pos(), to.pos(), false);
+
+               // register words longer than 5 characters
+               if (word.length() > 5)
+                       cur.buffer().registerWord(word);
+       }
 }
 
 
@@ -911,9 +928,9 @@ bool Text::handleBibitems(Cursor & cur)
 
        // otherwise reset to default
        if (par.useEmptyLayout())
-               cur.paragraph().layout(bufparams.getTextClass().emptyLayout());
+               cur.paragraph().setLayout(bufparams.textClass().emptyLayout());
        else
-               cur.paragraph().layout(bufparams.getTextClass().defaultLayout());
+               cur.paragraph().setLayout(bufparams.textClass().defaultLayout());
        return true;
 }
 
@@ -928,11 +945,15 @@ bool Text::erase(Cursor & cur)
                // this is the code for a normal delete, not pasting
                // any paragraphs
                cur.recordUndo(DELETE_UNDO);
-               if(!par.eraseChar(cur.pos(), cur.buffer().params().trackChanges)) {
+               bool const was_inset = cur.paragraph().isInset(cur.pos());
+               if(!par.eraseChar(cur.pos(), cur.buffer().params().trackChanges))
                        // the character has been logically deleted only => skip it
                        cur.top().forwardPos();
-               }
-               cur.checkBufferStructure();
+
+               if (was_inset)
+                       updateLabels(cur.buffer());
+               else
+                       cur.checkBufferStructure();
                needsUpdate = true;
        } else {
                if (cur.pit() == cur.lastpit())
@@ -970,7 +991,7 @@ bool Text::backspacePos0(Cursor & cur)
        bool needsUpdate = false;
 
        BufferParams const & bufparams = cur.buffer().params();
-       TextClass const & tclass = bufparams.getTextClass();
+       TextClass const & tclass = bufparams.textClass();
        ParagraphList & plist = cur.text()->paragraphs();
        Paragraph const & par = cur.paragraph();
        Cursor prevcur = cur;
@@ -1043,8 +1064,12 @@ bool Text::backspace(Cursor & cur)
                // without the dreaded mechanism. (JMarc)
                setCursorIntern(cur, cur.pit(), cur.pos() - 1,
                                false, cur.boundary());
+               bool const was_inset = cur.paragraph().isInset(cur.pos());
                cur.paragraph().eraseChar(cur.pos(), cur.buffer().params().trackChanges);
-               cur.checkBufferStructure();
+               if (was_inset)
+                       updateLabels(cur.buffer());
+               else
+                       cur.checkBufferStructure();
        }
 
        if (cur.pos() == cur.lastpos())
@@ -1091,7 +1116,7 @@ bool Text::dissolveInset(Cursor & cur) {
                for (; it != it_end; it++)
                        it->changeLanguage(b.params(), latex_language, b.language());
 
-               pasteParagraphList(cur, plist, b.params().getTextClassPtr(),
+               pasteParagraphList(cur, plist, b.params().textClassIndex(),
                                   b.errorList("Paste"));
                // restore position
                cur.pit() = min(cur.lastpit(), spit);
@@ -1156,7 +1181,8 @@ void Text::write(Buffer const & buf, ostream & os) const
 }
 
 
-bool Text::read(Buffer const & buf, Lexer & lex, ErrorList & errorList)
+bool Text::read(Buffer const & buf, Lexer & lex, 
+               ErrorList & errorList, InsetText * insetPtr)
 {
        depth_type depth = 0;
 
@@ -1185,6 +1211,7 @@ bool Text::read(Buffer const & buf, Lexer & lex, ErrorList & errorList)
                        Paragraph par;
                        par.params().depth(depth);
                        par.setFont(0, Font(inherit_font, buf.params().language));
+                       par.setInsetOwner(insetPtr);
                        pars_.push_back(par);
 
                        // FIXME: goddamn InsetTabular makes us pass a Buffer