]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView.C
fix some C++ parsing bugs
[lyx.git] / src / BufferView.C
index 35967e636e816fa134f504fcc671879eb42448d2..a041a3d7164bbef0d799625c54e6f39731e64636 100644 (file)
@@ -9,15 +9,12 @@
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
 #include "BufferView.h"
 #include "BufferView_pimpl.h"
 
 #include "BufferView_pimpl.h"
 #include "LaTeX.h"
+#include "ParagraphParameters.h"
 #include "WordLangTuple.h"
 #include "buffer.h"
 #include "bufferlist.h"
@@ -29,6 +26,7 @@
 #include "lyxlex.h"
 #include "lyxtext.h"
 #include "undo_funcs.h"
+#include "changes.h"
 
 #include "frontends/Alert.h"
 #include "frontends/Dialogs.h"
@@ -38,6 +36,7 @@
 
 #include "insets/insetcommand.h" // ChangeRefs
 #include "insets/inseterror.h"
+#include "insets/updatableinset.h"
 
 #include "support/FileInfo.h"
 #include "support/filetools.h"
@@ -104,6 +103,14 @@ void BufferView::buffer(Buffer * b)
 }
 
 
+void BufferView::reload()
+{
+       string const fn = buffer()->fileName();
+       if (bufferlist.close(buffer()))
+               buffer(bufferlist.loadLyXFile(fn));
+}
+
+
 void BufferView::resize()
 {
        if (pimpl_->buffer_) {
@@ -154,6 +161,12 @@ bool BufferView::available() const
 }
 
 
+Change const BufferView::getCurrentChange()
+{
+       return pimpl_->getCurrentChange();
+}
+
+
 void BufferView::beforeChange(LyXText * text)
 {
        pimpl_->beforeChange(text);
@@ -184,6 +197,12 @@ void BufferView::update(LyXText * text, UpdateCodes f)
 }
 
 
+void BufferView::update(UpdateCodes f)
+{
+       pimpl_->update(f);
+}
+
+
 void BufferView::switchKeyMap()
 {
        pimpl_->switchKeyMap();
@@ -264,13 +283,6 @@ void BufferView::scroll(int lines)
 }
 
 
-int BufferView::defaultHeight() const
-{
-       return text->defaultHeight();
-}
-
-
-
 // Inserts a file into current document
 bool BufferView::insertLyXFile(string const & filen)
        //
@@ -300,8 +312,7 @@ bool BufferView::insertLyXFile(string const & filen)
 
        ifstream ifs(fname.c_str());
        if (!ifs) {
-               Alert::alert(_("Error!"),
-                          _("Cannot open specified file: "),
+               Alert::err_alert(_("Error! Cannot open specified file:"),
                           MakeDisplayPath(fname, 50));
                return false;
        }
@@ -314,12 +325,13 @@ bool BufferView::insertLyXFile(string const & filen)
        bool res = true;
 
        if (c == '#') {
+               // FIXME: huh ? No we won't !
                lyxerr[Debug::INFO] << "Will insert file with header" << endl;
-               res = buffer()->readFile(lex, fname, text->cursor.par());
+               res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
        } else {
                lyxerr[Debug::INFO] << "Will insert file without header"
                                    << endl;
-               res = buffer()->readLyXformat2(lex, text->cursor.par());
+               res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
        }
 
        resize();
@@ -343,7 +355,7 @@ bool BufferView::removeAutoInsets()
        // loop once at most.  However for safety we iterate rather than just
        // make this an if () conditional.
        while ((cursor_par_prev || cursor_par_next)
-              && text->setCursor(this,
+              && text->setCursor(
                                  cursor_par_prev ? cursor_par_prev : cursor_par_next,
                                  0)) {
                // We just removed cursor_par so have to fix the "cursor"
@@ -373,7 +385,7 @@ bool BufferView::removeAutoInsets()
                Paragraph * par_prev = par ? par->previous() : 0;
                bool removed = false;
 
-               if (text->setCursor(this, par, 0)
+               if (text->setCursor(par, 0)
                    && cursor_par == par_prev) {
                        // The previous setCursor line was deleted and that
                        // was the cursor_par line.  This can only happen if an
@@ -437,14 +449,14 @@ bool BufferView::removeAutoInsets()
                }
                if (removed) {
                        found = true;
-                       text->redoParagraph(this);
+                       text->redoParagraph();
                }
        }
 
        // It is possible that the last line is empty if it was cursor_par
        // and/or only had an error inset on it.  So we set the cursor to the
        // start of the doc to force its removal and ensure a valid saved cursor
-       if (text->setCursor(this, text->ownerParagraph(), 0)
+       if (text->setCursor(text->ownerParagraph(), 0)
            && 0 == cursor_par_next) {
                cursor_par = cursor_par_prev;
                cursor_pos = cursor_par->size();
@@ -457,7 +469,7 @@ bool BufferView::removeAutoInsets()
        }
 
        // restore the original cursor in its corrected location.
-       text->setCursorIntern(this, cursor_par, cursor_pos);
+       text->setCursorIntern(cursor_par, cursor_pos);
 
        return found;
 }
@@ -498,13 +510,13 @@ void BufferView::insertErrors(TeXErrors & terr)
 
                freezeUndo();
                InsetError * new_inset = new InsetError(msgtxt);
-               text->setCursorIntern(this, texrowpar, tmppos);
-               text->insertInset(this, new_inset);
-               text->fullRebreak(this);
+               text->setCursorIntern(texrowpar, tmppos);
+               text->insertInset(new_inset);
+               text->fullRebreak();
                unFreezeUndo();
        }
        // Restore the cursor position
-       text->setCursorIntern(this, cursor.par(), cursor.pos());
+       text->setCursorIntern(cursor.par(), cursor.pos());
 }
 
 
@@ -523,7 +535,7 @@ void BufferView::setCursorFromRow(int row)
        } else {
                texrowpar = buffer()->getParFromID(tmpid);
        }
-       text->setCursor(this, texrowpar, tmppos);
+       text->setCursor(texrowpar, tmppos);
 }
 
 
@@ -546,9 +558,9 @@ bool BufferView::gotoLabel(string const & label)
                if (find(labels.begin(),labels.end(),label)
                     != labels.end()) {
                        beforeChange(text);
-                       text->setCursor(this, it.getPar(), it.getPos());
+                       text->setCursor(it.getPar(), it.getPos());
                        text->selection.cursor = text->cursor;
-                       update(text, BufferView::SELECT|BufferView::FITCUR);
+                       update(text, BufferView::SELECT);
                        return true;
                }
        }
@@ -564,11 +576,11 @@ void BufferView::undo()
        owner()->message(_("Undo"));
        hideCursor();
        beforeChange(text);
-       update(text, BufferView::SELECT|BufferView::FITCUR);
+       update(text, BufferView::SELECT);
        if (!textUndo(this))
                owner()->message(_("No further undo information"));
        else
-               update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
+               update(text, BufferView::SELECT);
        switchKeyMap();
 }
 
@@ -581,11 +593,11 @@ void BufferView::redo()
        owner()->message(_("Redo"));
        hideCursor();
        beforeChange(text);
-       update(text, BufferView::SELECT|BufferView::FITCUR);
+       update(text, BufferView::SELECT);
        if (!textRedo(this))
                owner()->message(_("No further redo information"));
        else
-               update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
+               update(text, BufferView::SELECT);
        switchKeyMap();
 }
 
@@ -602,9 +614,9 @@ void BufferView::copyEnvironment()
 void BufferView::pasteEnvironment()
 {
        if (available()) {
-               text->pasteEnvironmentType(this);
+               text->pasteEnvironmentType();
                owner()->message(_("Paragraph environment type set"));
-               update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
+               update(text, BufferView::SELECT);
        }
 }
 
@@ -617,7 +629,7 @@ WordLangTuple const BufferView::nextWord(float & value)
                return WordLangTuple();
        }
 
-       return text->selectNextWordToSpellcheck(this, value);
+       return text->selectNextWordToSpellcheck(value);
 }
 
 
@@ -630,9 +642,9 @@ void BufferView::selectLastWord()
        hideCursor();
        beforeChange(text);
        text->selection.cursor = cur;
-       text->selectSelectedWord(this);
+       text->selectSelectedWord();
        toggleSelection(false);
-       update(text, BufferView::SELECT|BufferView::FITCUR);
+       update(text, BufferView::SELECT);
 }
 
 
@@ -642,9 +654,9 @@ void BufferView::endOfSpellCheck()
 
        hideCursor();
        beforeChange(text);
-       text->selectSelectedWord(this);
+       text->selectSelectedWord();
        text->clearSelection();
-       update(text, BufferView::SELECT|BufferView::FITCUR);
+       update(text, BufferView::SELECT);
 }
 
 
@@ -655,23 +667,27 @@ void BufferView::replaceWord(string const & replacestring)
 
        LyXText * tt = getLyXText();
        hideCursor();
-       update(tt, BufferView::SELECT|BufferView::FITCUR);
+       update(tt, BufferView::SELECT);
 
        // clear the selection (if there is any)
        toggleSelection(false);
-       update(tt, BufferView::SELECT|BufferView::FITCUR);
+       update(tt, BufferView::SELECT);
 
        // clear the selection (if there is any)
        toggleSelection(false);
-       tt->replaceSelectionWithString(this, replacestring);
+       tt->replaceSelectionWithString(replacestring);
 
-       tt->setSelectionOverString(this, replacestring);
+       tt->setSelectionRange(replacestring.length());
 
        // Go back so that replacement string is also spellchecked
        for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
                tt->cursorLeft(this);
        }
-       update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
+       update(tt, BufferView::SELECT);
+
+       // FIXME: should be done through LFUN
+       buffer()->markDirty();
+       fitCursor();
 }
 // End of spellchecker stuff
 
@@ -703,12 +719,12 @@ bool BufferView::lockInset(UpdatableInset * inset)
                        InsetList::iterator end = pit->insetlist.end();
                        for (; it != end; ++it) {
                                if (it.getInset() == inset) {
-                                       text->setCursorIntern(this, &*pit, it.getPos());
+                                       text->setCursorIntern(&*pit, it.getPos());
                                        theLockingInset(inset);
                                        return true;
                                }
                                if (it.getInset()->getInsetFromID(id)) {
-                                       text->setCursorIntern(this, &*pit, it.getPos());
+                                       text->setCursorIntern(&*pit, it.getPos());
                                        it.getInset()->edit(this);
                                        return theLockingInset()->lockInsetInInset(this, inset);
                                }
@@ -730,7 +746,7 @@ void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
                    cursor.par()->isInset(cursor.pos() - 1) &&
                    (cursor.par()->getInset(cursor.pos() - 1) ==
                     locking_inset))
-                       text->setCursor(this, cursor,
+                       text->setCursor(cursor,
                                        cursor.par(), cursor.pos() - 1);
                LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
                LyXText * txt = getLyXText();
@@ -781,18 +797,18 @@ int BufferView::unlockInset(UpdatableInset * inset)
                // make sure we update the combo !
                owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
                // Tell the paragraph dialog that we changed paragraph
-               owner()->getDialogs().updateParagraph();
+               dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
                finishUndo();
                return 0;
        } else if (inset && theLockingInset() &&
                   theLockingInset()->unlockInsetInInset(this, inset)) {
                // Tell the paragraph dialog that we changed paragraph
-               owner()->getDialogs().updateParagraph();
+               dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
                // owner inset has updated the layout combo
                finishUndo();
                return 0;
        }
-       return bufferlist.unlockInset(inset);
+       return 1;
 }
 
 
@@ -808,9 +824,9 @@ void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
 }
 
 
-void BufferView::updateInset(Inset * inset, bool mark_dirty)
+void BufferView::updateInset(Inset * inset)
 {
-       pimpl_->updateInset(inset, mark_dirty);
+       pimpl_->updateInset(inset);
 }
 
 
@@ -846,14 +862,14 @@ bool BufferView::ChangeInsets(Inset::Code code,
                        // The test it.size()==1 was needed to prevent crashes.
                        // How to set the cursor corretly when it.size()>1 ??
                        if (it.size() == 1) {
-                               text->setCursorIntern(this, par, 0);
-                               text->redoParagraphs(this, text->cursor,
+                               text->setCursorIntern(par, 0);
+                               text->redoParagraphs(text->cursor,
                                                     text->cursor.par()->next());
-                               text->fullRebreak(this);
+                               text->fullRebreak();
                        }
                }
        }
-       text->setCursorIntern(this, cursor.par(), cursor.pos());
+       text->setCursorIntern(cursor.par(), cursor.pos());
        return need_update;
 }
 
@@ -870,12 +886,12 @@ bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
 }
 
 
-bool BufferView::ChangeCitationsIfUnique(string const & from,
-                                        string const & to)
+bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
 {
        typedef pair<string, string> StringPair;
 
-       vector<StringPair> keys = buffer()->getBibkeyList();
+       vector<StringPair> keys;
+       buffer()->fillWithBibKeys(keys);
        if (count_if(keys.begin(), keys.end(),
                     lyx::equal_1st_in_pair<StringPair>(from))
            > 1)
@@ -933,6 +949,18 @@ Language const * BufferView::getParentLanguage(Inset * inset) const
 }
 
 
+Encoding const * BufferView::getEncoding() const
+{
+       LyXText * t = getLyXText();
+       if (!t)
+               return 0;
+
+       LyXCursor const & c= t->cursor;
+       LyXFont const font = c.par()->getFont(buffer()->params, c.pos());
+       return font.language()->encoding();
+}
+
+
 void BufferView::haveSelection(bool sel)
 {
        pimpl_->workarea().haveSelection(sel);