]> git.lyx.org Git - lyx.git/blobdiff - src/Text.cpp
do what the FIXME suggested
[lyx.git] / src / Text.cpp
index 6e2ee99964f6bd948eebc9a79c0a153e4c2ee72a..5e18d6f18544fd41fb0ba7d924bd59f25a0cd689 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,10 +96,22 @@ void readParToken(Buffer const & buf, Paragraph & par, Lexer & lex,
                font = Font(inherit_font, bp.language);
                change = Change(Change::UNCHANGED);
 
-               TextClass const & tclass = bp.getTextClass();
+               DocumentClass const & tclass = bp.documentClass();
 
-               if (layoutname.empty()) {
+               if (layoutname.empty())
                        layoutname = tclass.defaultLayoutName();
+
+               if (par.forceEmptyLayout()) {
+                       // in this case only the empty layout is allowed
+                       layoutname = tclass.emptyLayoutName();
+               } else if (par.useEmptyLayout()) {
+                       // in this case, default layout maps to empty layout 
+                       if (layoutname == tclass.defaultLayoutName())
+                               layoutname = tclass.emptyLayoutName();
+               } else { 
+                       // otherwise, the empty layout maps to the default
+                       if (layoutname == tclass.emptyLayoutName())
+                               layoutname = tclass.defaultLayoutName();
                }
 
                bool hasLayout = tclass.hasLayout(layoutname);
@@ -113,15 +120,17 @@ void readParToken(Buffer const & buf, Paragraph & par, Lexer & lex,
                        errorList.push_back(ErrorItem(_("Unknown layout"),
                        bformat(_("Layout '%1$s' does not exist in textclass '%2$s'\nTrying to use the default instead.\n"),
                        layoutname, from_utf8(tclass.name())), par.id(), 0, par.size()));
-                       layoutname = tclass.defaultLayoutName();
+                       layoutname = par.useEmptyLayout() ? 
+                                       tclass.emptyLayoutName() :
+                                       tclass.defaultLayoutName();
                }
 
-               par.layout(bp.getTextClass()[layoutname]);
+               par.setLayout(bp.documentClass()[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.documentClass()[layout->obsoleted_by()]);
 
                par.params().read(lex);
 
@@ -211,7 +220,7 @@ void readParToken(Buffer const & buf, Paragraph & par, Lexer & lex,
                                inset.reset(new InsetSpecialChar);
                        else
                                inset.reset(new InsetSpace);
-                       inset->read(buf, lex);
+                       inset->read(lex);
                        par.insertInset(par.size(), inset.release(),
                                        font, change);
                }
@@ -219,15 +228,15 @@ void readParToken(Buffer const & buf, Paragraph & par, Lexer & lex,
                par.appendChar('\\', font, change);
        } else if (token == "\\linebreak") {
                auto_ptr<Inset> inset(new InsetLinebreak);
-               inset->read(buf, lex);
+               inset->read(lex);
                par.insertInset(par.size(), inset.release(), font, change);
        } else if (token == "\\newline") {
                auto_ptr<Inset> inset(new InsetNewline);
-               inset->read(buf, lex);
+               inset->read(lex);
                par.insertInset(par.size(), inset.release(), font, change);
        } else if (token == "\\LyXTable") {
                auto_ptr<Inset> inset(new InsetTabular(buf));
-               inset->read(buf, lex);
+               inset->read(lex);
                par.insertInset(par.size(), inset.release(), font, change);
        } else if (token == "\\hfill") {
                par.insertInset(par.size(), new InsetHFill, font, change);
@@ -331,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();
@@ -347,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();
+       DocumentClass const & tclass = cur.buffer().params().documentClass();
        LayoutPtr const & layout = cpar.layout();
 
        // this is only allowed, if the current paragraph is not empty
@@ -388,9 +396,11 @@ void Text::breakParagraph(Cursor & cur, bool inverse_logic)
        if (sensitive) {
                if (cur.pos() == 0)
                        // set to standard-layout
+               //FIXME Check if this should be emptyLayout() in some cases
                        pars_[cpit].applyLayout(tclass.defaultLayout());
                else
                        // set to standard-layout
+                       //FIXME Check if this should be emptyLayout() in some cases
                        pars_[next_par].applyLayout(tclass.defaultLayout());
        }
 
@@ -440,8 +450,8 @@ void Text::insertChar(Cursor & cur, char_type c)
                            !(contains(number_seperators, c) &&
                              cur.pos() != 0 &&
                              cur.pos() != cur.lastpos() &&
-                             tm.getDisplayFont(pit, cur.pos()).fontInfo().number() == FONT_ON &&
-                             tm.getDisplayFont(pit, cur.pos() - 1).fontInfo().number() == FONT_ON)
+                             tm.displayFont(pit, cur.pos()).fontInfo().number() == FONT_ON &&
+                             tm.displayFont(pit, cur.pos() - 1).fontInfo().number() == FONT_ON)
                           )
                                number(cur); // Set current_font.number to OFF
                } else if (isDigit(c) &&
@@ -459,7 +469,7 @@ void Text::insertChar(Cursor & cur, char_type c)
                                                tm.font_);
                                } else if (contains(number_seperators, c)
                                     && cur.pos() >= 2
-                                    && tm.getDisplayFont(pit, cur.pos() - 2).fontInfo().number() == FONT_ON) {
+                                    && tm.displayFont(pit, cur.pos() - 2).fontInfo().number() == FONT_ON) {
                                        setCharFont(buffer, pit, cur.pos() - 1, cur.current_font,
                                                tm.font_);
                                }
@@ -492,7 +502,7 @@ void Text::insertChar(Cursor & cur, char_type c)
        if ((cur.pos() >= 2) && (par.isLineSeparator(cur.pos() - 1))) {
                // get font in front and behind the space in question. But do NOT 
                // use getFont(cur.pos()) because the character c is not inserted yet
-               Font const pre_space_font  = tm.getDisplayFont(cur.pit(), cur.pos() - 2);
+               Font const pre_space_font  = tm.displayFont(cur.pit(), cur.pos() - 2);
                Font const & post_space_font = cur.real_current_font;
                bool pre_space_rtl  = pre_space_font.isVisibleRightToLeft();
                bool post_space_rtl = post_space_font.isVisibleRightToLeft();
@@ -505,7 +515,7 @@ void Text::insertChar(Cursor & cur, char_type c)
                                (pre_space_rtl == par.isRTL(buffer.params())) ?
                                pre_space_font.language() : post_space_font.language();
 
-                       Font space_font = tm.getDisplayFont(cur.pit(), cur.pos() - 1);
+                       Font space_font = tm.displayFont(cur.pit(), cur.pos() - 1);
                        space_font.setLanguage(lang);
                        par.setFont(cur.pos() - 1, space_font);
                }
@@ -553,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;
@@ -562,6 +574,15 @@ 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());
+               cur.paragraph().updateWords(cur.top());
+       }
 }
 
 
@@ -868,31 +889,34 @@ bool Text::handleBibitems(Cursor & cur)
 {
        if (cur.paragraph().layout()->labeltype != LABEL_BIBLIO)
                return false;
+
+       if (cur.pos() != 0)
+               return false;
+
+       BufferParams const & bufparams = cur.buffer().params();
+       Paragraph const & par = cur.paragraph();
+       Cursor prevcur = cur;
+       if (cur.pit() > 0) {
+               --prevcur.pit();
+               prevcur.pos() = prevcur.lastpos();
+       }
+       Paragraph const & prevpar = prevcur.paragraph();
+
        // if a bibitem is deleted, merge with previous paragraph
        // if this is a bibliography item as well
-       if (cur.pos() == 0) {
-               BufferParams const & bufparams = cur.buffer().params();
-               Paragraph const & par = cur.paragraph();
-               Cursor prevcur = cur;
-               if (cur.pit() > 0) {
-                       --prevcur.pit();
-                       prevcur.pos() = prevcur.lastpos();
-               }
-               Paragraph const & prevpar = prevcur.paragraph();
-               if (cur.pit() > 0 && par.layout() == prevpar.layout()) {
-                       cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
-                       mergeParagraph(bufparams, cur.text()->paragraphs(),
-                                      prevcur.pit());
-                       updateLabels(cur.buffer());
-                       setCursorIntern(cur, prevcur.pit(), prevcur.pos());
-                       cur.updateFlags(Update::Force);
-               // if not, reset the paragraph to default
-               } else
-                       cur.paragraph().layout(
-                               bufparams.getTextClass().defaultLayout());
+       if (cur.pit() > 0 && par.layout() == prevpar.layout()) {
+               cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
+               mergeParagraph(bufparams, cur.text()->paragraphs(),
+                                                       prevcur.pit());
+               updateLabels(cur.buffer());
+               setCursorIntern(cur, prevcur.pit(), prevcur.pos());
+               cur.updateFlags(Update::Force);
                return true;
-       }
-       return false;
+       } 
+
+       // otherwise reset to default
+       cur.paragraph().setEmptyOrDefaultLayout(bufparams.documentClass());
+       return true;
 }
 
 
@@ -906,11 +930,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())
@@ -948,7 +976,7 @@ bool Text::backspacePos0(Cursor & cur)
        bool needsUpdate = false;
 
        BufferParams const & bufparams = cur.buffer().params();
-       TextClass const & tclass = bufparams.getTextClass();
+       DocumentClass const & tclass = bufparams.documentClass();
        ParagraphList & plist = cur.text()->paragraphs();
        Paragraph const & par = cur.paragraph();
        Cursor prevcur = cur;
@@ -974,8 +1002,10 @@ bool Text::backspacePos0(Cursor & cur)
        // layouts. I think it is a real bug of all other
        // word processors to allow it. It confuses the user.
        // Correction: Pasting is always allowed with standard-layout
+       // or the empty layout.
        else if (par.layout() == prevpar.layout()
-                || par.layout() == tclass.defaultLayout()) {
+                || par.layout() == tclass.defaultLayout()
+                || par.layout() == tclass.emptyLayout()) {
                cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
                mergeParagraph(bufparams, plist, prevcur.pit());
                needsUpdate = true;
@@ -1019,8 +1049,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())
@@ -1067,7 +1101,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().documentClassPtr(),
                                   b.errorList("Paste"));
                // restore position
                cur.pit() = min(cur.lastpit(), spit);
@@ -1080,7 +1114,7 @@ bool Text::dissolveInset(Cursor & cur) {
 
 
 void Text::getWord(CursorSlice & from, CursorSlice & to,
-       word_location const loc)
+       word_location const loc) const
 {
        Paragraph const & from_par = pars_[from.pit()];
        switch (loc) {
@@ -1112,7 +1146,7 @@ void Text::getWord(CursorSlice & from, CursorSlice & to,
                break;
        }
        to = from;
-       Paragraph & to_par = pars_[to.pit()];
+       Paragraph const & to_par = pars_[to.pit()];
        while (to.pos() < to_par.size() && to_par.isLetter(to.pos()))
                ++to.pos();
 }
@@ -1124,7 +1158,7 @@ void Text::write(Buffer const & buf, ostream & os) const
        ParagraphList::const_iterator end = paragraphs().end();
        depth_type dth = 0;
        for (; pit != end; ++pit)
-               pit->write(buf, os, buf.params(), dth);
+               pit->write(os, buf.params(), dth);
 
        // Close begin_deeper
        for(; dth > 0; --dth)
@@ -1132,7 +1166,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;
 
@@ -1161,12 +1196,17 @@ 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
                        // not BufferParams
                        lyx::readParagraph(buf, pars_.back(), lex, errorList);
 
+                       // register the words in the global word list
+                       CursorSlice sl = CursorSlice(*insetPtr);
+                       sl.pit() = pars_.size() - 1;
+                       pars_.back().updateWords(sl);
                } else if (token == "\\begin_deeper") {
                        ++depth;
                } else if (token == "\\end_deeper") {
@@ -1268,7 +1308,7 @@ docstring Text::getPossibleLabel(Cursor & cur) const
        LayoutPtr layout = pars_[pit].layout();
 
        docstring text;
-       docstring par_text = pars_[pit].asString(cur.buffer(), false);
+       docstring par_text = pars_[pit].asString(false);
        string piece;
        // the return string of math matrices might contain linebreaks
        par_text = subst(par_text, '\n', '-');