]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView.C
don't rm emergency saves ever
[lyx.git] / src / BufferView.C
index 618d55580e3066ea80195c833acf650b62f5156e..5e8763cfc0feb34f1df3749de5ffb0a341946d41 100644 (file)
@@ -14,6 +14,7 @@
 
 #include "BufferView_pimpl.h"
 #include "LaTeX.h"
+#include "ParagraphParameters.h"
 #include "WordLangTuple.h"
 #include "buffer.h"
 #include "bufferlist.h"
@@ -26,6 +27,7 @@
 #include "lyxtext.h"
 #include "undo_funcs.h"
 #include "changes.h"
+#include "paragraph_funcs.h"
 
 #include "frontends/Alert.h"
 #include "frontends/Dialogs.h"
@@ -42,6 +44,7 @@
 #include "support/lyxfunctional.h" // equal_1st_in_pair
 #include "support/types.h"
 #include "support/lyxalgo.h" // lyx_count
+#include "support/BoostFormat.h"
 
 #include <fstream>
 
@@ -105,7 +108,7 @@ void BufferView::buffer(Buffer * b)
 void BufferView::reload()
 {
        string const fn = buffer()->fileName();
-       if (bufferlist.close(buffer()))
+       if (bufferlist.close(buffer(), false))
                buffer(bufferlist.loadLyXFile(fn));
 }
 
@@ -196,6 +199,12 @@ void BufferView::update(LyXText * text, UpdateCodes f)
 }
 
 
+void BufferView::update(UpdateCodes f)
+{
+       pimpl_->update(f);
+}
+
+
 void BufferView::switchKeyMap()
 {
        pimpl_->switchKeyMap();
@@ -214,18 +223,6 @@ int BufferView::workWidth() const
 }
 
 
-void BufferView::showCursor()
-{
-       pimpl_->showCursor();
-}
-
-
-void BufferView::hideCursor()
-{
-       pimpl_->hideCursor();
-}
-
-
 void BufferView::toggleSelection(bool b)
 {
        pimpl_->toggleSelection(b);
@@ -295,9 +292,16 @@ bool BufferView::insertLyXFile(string const & filen)
        FileInfo const fi(fname);
 
        if (!fi.readable()) {
-               Alert::alert(_("Error!"),
-                          _("Specified file is unreadable: "),
-                          MakeDisplayPath(fname, 50));
+               string const file = MakeDisplayPath(fname, 50);
+#if USE_BOOST_FORMAT
+               boost::format fmt(_("The specified document\n%1$s\ncould not be read."));
+               fmt % file;
+               string text = fmt.str();
+#else
+               string text = _("The specified document\n");
+               text += file + _(" could not be read.");
+#endif
+               Alert::error(_("Could not read document"), text);
                return false;
        }
 
@@ -305,8 +309,16 @@ bool BufferView::insertLyXFile(string const & filen)
 
        ifstream ifs(fname.c_str());
        if (!ifs) {
-               Alert::err_alert(_("Error! Cannot open specified file:"),
-                          MakeDisplayPath(fname, 50));
+               string const file = MakeDisplayPath(fname, 50);
+#if USE_BOOST_FORMAT
+               boost::format fmt(_("Could not open the specified document\n%1$s."));
+               fmt % file;
+               string text = fmt.str();
+#else
+               string text = _("Could not open the specified document\n");
+               text += file + ".";
+#endif
+               Alert::error(_("Could not open file"), text);
                return false;
        }
 
@@ -318,12 +330,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();
@@ -334,7 +347,7 @@ bool BufferView::insertLyXFile(string const & filen)
 bool BufferView::removeAutoInsets()
 {
        // keep track of which pos and par the cursor was on
-       Paragraph * cursor_par = text->cursor.par();
+       Paragraph * cursor_par = &*text->cursor.par();
        Paragraph * cursor_par_prev = cursor_par ? cursor_par->previous() : 0;
        Paragraph * cursor_par_next = cursor_par ? cursor_par->next() : 0;
        pos_type cursor_pos = text->cursor.pos();
@@ -347,7 +360,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"
@@ -377,7 +390,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
@@ -441,14 +454,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->ownerParagraphs().begin(), 0)
            && 0 == cursor_par_next) {
                cursor_par = cursor_par_prev;
                cursor_pos = cursor_par->size();
@@ -461,7 +474,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;
 }
@@ -491,10 +504,10 @@ void BufferView::insertErrors(TeXErrors & terr)
                Paragraph * texrowpar = 0;
 
                if (tmpid == -1) {
-                       texrowpar = text->ownerParagraph();
+                       texrowpar = &*text->ownerParagraphs().begin();
                        tmppos = 0;
                } else {
-                       texrowpar = buffer()->getParFromID(tmpid);
+                       texrowpar = &*buffer()->getParFromID(tmpid);
                }
 
                if (texrowpar == 0)
@@ -502,13 +515,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());
 }
 
 
@@ -522,12 +535,12 @@ void BufferView::setCursorFromRow(int row)
        Paragraph * texrowpar;
 
        if (tmpid == -1) {
-               texrowpar = text->ownerParagraph();
+               texrowpar = &*text->ownerParagraphs().begin();
                tmppos = 0;
        } else {
-               texrowpar = buffer()->getParFromID(tmpid);
+               texrowpar = &*buffer()->getParFromID(tmpid);
        }
-       text->setCursor(this, texrowpar, tmppos);
+       text->setCursor(texrowpar, tmppos);
 }
 
 
@@ -537,12 +550,7 @@ bool BufferView::insertInset(Inset * inset, string const & lout)
 }
 
 
-// This is also a buffer property (ale)
-// Not so sure about that. a goto Label function can not be buffer local, just
-// think how this will work in a multiwindow/buffer environment, all the
-// cursors in all the views showing this buffer will move. (Lgb)
-// OK, then no cursor action should be allowed in buffer. (ale)
-bool BufferView::gotoLabel(string const & label)
+void BufferView::gotoLabel(string const & label)
 {
        for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
             it != buffer()->inset_iterator_end(); ++it) {
@@ -550,13 +558,12 @@ 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);
-                       return true;
+                       update(text, BufferView::SELECT);
+                       return;
                }
        }
-       return false;
 }
 
 
@@ -566,13 +573,12 @@ void BufferView::undo()
                return;
 
        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();
 }
 
@@ -583,36 +589,16 @@ void BufferView::redo()
                return;
 
        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();
 }
 
 
-void BufferView::copyEnvironment()
-{
-       if (available()) {
-               text->copyEnvironmentType();
-               owner()->message(_("Paragraph environment type copied"));
-       }
-}
-
-
-void BufferView::pasteEnvironment()
-{
-       if (available()) {
-               text->pasteEnvironmentType(this);
-               owner()->message(_("Paragraph environment type set"));
-               update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
-       }
-}
-
-
 // these functions are for the spellchecker
 WordLangTuple const BufferView::nextWord(float & value)
 {
@@ -621,7 +607,7 @@ WordLangTuple const BufferView::nextWord(float & value)
                return WordLangTuple();
        }
 
-       return text->selectNextWordToSpellcheck(this, value);
+       return text->selectNextWordToSpellcheck(value);
 }
 
 
@@ -631,12 +617,11 @@ void BufferView::selectLastWord()
                return;
 
        LyXCursor cur = text->selection.cursor;
-       hideCursor();
        beforeChange(text);
        text->selection.cursor = cur;
-       text->selectSelectedWord(this);
+       text->selectSelectedWord();
        toggleSelection(false);
-       update(text, BufferView::SELECT|BufferView::FITCUR);
+       update(text, BufferView::SELECT);
 }
 
 
@@ -644,11 +629,10 @@ void BufferView::endOfSpellCheck()
 {
        if (!available()) return;
 
-       hideCursor();
        beforeChange(text);
-       text->selectSelectedWord(this);
+       text->selectSelectedWord();
        text->clearSelection();
-       update(text, BufferView::SELECT|BufferView::FITCUR);
+       update(text, BufferView::SELECT);
 }
 
 
@@ -658,24 +642,27 @@ void BufferView::replaceWord(string const & replacestring)
                return;
 
        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->setSelectionRange(this, replacestring.length());
+       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
 
@@ -690,8 +677,9 @@ bool BufferView::lockInset(UpdatableInset * inset)
        if (!theLockingInset()) {
                // first check if it's the inset under the cursor we want lock
                // should be most of the time
-               char const c = text->cursor.par()->getChar(text->cursor.pos());
-               if (c == Paragraph::META_INSET) {
+               if (text->cursor.pos() < text->cursor.par()->size()
+                   && text->cursor.par()->getChar(text->cursor.pos()) ==
+                   Paragraph::META_INSET) {
                        Inset * in = text->cursor.par()->getInset(text->cursor.pos());
                        if (inset == in) {
                                theLockingInset(inset);
@@ -707,12 +695,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);
                                }
@@ -724,44 +712,6 @@ bool BufferView::lockInset(UpdatableInset * inset)
 }
 
 
-void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
-{
-       if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
-               LyXCursor cursor = text->cursor;
-               Inset * locking_inset = theLockingInset()->getLockingInset();
-
-               if ((cursor.pos() - 1 >= 0) &&
-                   cursor.par()->isInset(cursor.pos() - 1) &&
-                   (cursor.par()->getInset(cursor.pos() - 1) ==
-                    locking_inset))
-                       text->setCursor(this, cursor,
-                                       cursor.par(), cursor.pos() - 1);
-               LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
-               LyXText * txt = getLyXText();
-               if (locking_inset->isTextInset() &&
-                   locking_inset->lyxCode() != Inset::ERT_CODE &&
-                   (txt->real_current_font.language() !=
-                    buffer()->params.language
-                    || txt->real_current_font.isVisibleRightToLeft()
-                    != buffer()->params.language->RightToLeft()))
-                       shape = (txt->real_current_font.isVisibleRightToLeft())
-                               ? LyXScreen::REVERSED_L_SHAPE
-                               : LyXScreen::L_SHAPE;
-               y += cursor.iy() + theLockingInset()->insetInInsetY();
-               screen().showManualCursor(text, x, y, asc, desc,
-                                                 shape);
-       }
-}
-
-
-void BufferView::hideLockedInsetCursor()
-{
-       if (theLockingInset() && available()) {
-               screen().hideCursor();
-       }
-}
-
-
 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
 {
        if (theLockingInset() && available()) {
@@ -775,6 +725,12 @@ bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
 }
 
 
+void BufferView::hideCursor()
+{
+       screen().hideCursor();
+}
+
+
 int BufferView::unlockInset(UpdatableInset * inset)
 {
        if (!inset)
@@ -785,13 +741,13 @@ 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;
@@ -808,13 +764,13 @@ void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
                kind = Undo::FINISH;
        setUndo(this, kind,
                text->cursor.par(),
-               text->cursor.par()->next());
+               boost::next(text->cursor.par()));
 }
 
 
-void BufferView::updateInset(Inset * inset, bool mark_dirty)
+void BufferView::updateInset(Inset * inset)
 {
-       pimpl_->updateInset(inset, mark_dirty);
+       pimpl_->updateInset(inset);
 }
 
 
@@ -850,14 +806,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->cursor.par()->next());
-                               text->fullRebreak(this);
+                               text->setCursorIntern(par, 0);
+                               text->redoParagraphs(text->cursor,
+                                                    boost::next(text->cursor.par()));
+                               text->fullRebreak();
                        }
                }
        }
-       text->setCursorIntern(this, cursor.par(), cursor.pos());
+       text->setCursorIntern(cursor.par(), cursor.pos());
        return need_update;
 }
 
@@ -943,8 +899,9 @@ Encoding const * BufferView::getEncoding() const
        if (!t)
                return 0;
 
-       LyXCursor const & c= t->cursor;
-       LyXFont const font = c.par()->getFont(buffer()->params, c.pos());
+       LyXCursor const & c = t->cursor;
+       LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
+                                             outerFont(c.par(), t->ownerParagraphs()));
        return font.language()->encoding();
 }