+ 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)
+{
+ if (!available()) {
+ value = 1;
+ return WordLangTuple();
+ }
+
+ return text->selectNextWordToSpellcheck(this, value);
+}
+
+
+void BufferView::selectLastWord()
+{
+ if (!available())
+ return;
+
+ LyXCursor cur = text->selection.cursor;
+ hideCursor();
+ beforeChange(text);
+ text->selection.cursor = cur;
+ text->selectSelectedWord(this);
+ toggleSelection(false);
+ update(text, BufferView::SELECT|BufferView::FITCUR);
+}
+
+
+void BufferView::endOfSpellCheck()
+{
+ if (!available()) return;
+
+ hideCursor();
+ beforeChange(text);
+ text->selectSelectedWord(this);
+ text->clearSelection();
+ update(text, BufferView::SELECT|BufferView::FITCUR);
+}
+
+
+void BufferView::replaceWord(string const & replacestring)
+{
+ if (!available())
+ return;
+
+ LyXText * tt = getLyXText();
+ hideCursor();
+ update(tt, BufferView::SELECT|BufferView::FITCUR);
+
+ // clear the selection (if there is any)
+ toggleSelection(false);
+ update(tt, BufferView::SELECT|BufferView::FITCUR);
+
+ // clear the selection (if there is any)
+ toggleSelection(false);
+ tt->replaceSelectionWithString(this, replacestring);
+
+ tt->setSelectionRange(this, 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);
+}
+// End of spellchecker stuff
+
+
+bool BufferView::lockInset(UpdatableInset * inset)
+{
+ if (!inset)
+ return false;
+ // don't relock if we're already locked
+ if (theLockingInset() == inset)
+ return true;
+ 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) {
+ Inset * in = text->cursor.par()->getInset(text->cursor.pos());
+ if (inset == in) {
+ theLockingInset(inset);
+ return true;
+ }
+ }
+ // Then do a deep look of the inset and lock the right one
+ int const id = inset->id();
+ ParagraphList::iterator pit = buffer()->paragraphs.begin();
+ ParagraphList::iterator pend = buffer()->paragraphs.end();
+ for (; pit != pend; ++pit) {
+ InsetList::iterator it = pit->insetlist.begin();
+ InsetList::iterator end = pit->insetlist.end();
+ for (; it != end; ++it) {
+ if (it.getInset() == inset) {
+ text->setCursorIntern(this, &*pit, it.getPos());
+ theLockingInset(inset);
+ return true;
+ }
+ if (it.getInset()->getInsetFromID(id)) {
+ text->setCursorIntern(this, &*pit, it.getPos());
+ it.getInset()->edit(this);
+ return theLockingInset()->lockInsetInInset(this, inset);
+ }
+ }
+ }
+ return false;
+ }
+ return theLockingInset()->lockInsetInInset(this, 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()) {
+ y += text->cursor.iy() + theLockingInset()->insetInInsetY();
+ if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
+ updateScrollbar();
+ return true;
+ }
+ }
+ return false;
+}
+
+
+int BufferView::unlockInset(UpdatableInset * inset)
+{
+ if (!inset)
+ return 0;
+ if (inset && theLockingInset() == inset) {
+ inset->insetUnlock(this);
+ theLockingInset(0);
+ // make sure we update the combo !
+ owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
+ // Tell the paragraph dialog that we changed paragraph
+ owner()->getDialogs().updateParagraph();
+ finishUndo();
+ return 0;
+ } else if (inset && theLockingInset() &&
+ theLockingInset()->unlockInsetInInset(this, inset)) {
+ // Tell the paragraph dialog that we changed paragraph
+ owner()->getDialogs().updateParagraph();
+ // owner inset has updated the layout combo
+ finishUndo();
+ return 0;
+ }
+ return bufferlist.unlockInset(inset);
+}
+
+
+void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
+{
+ if (!theLockingInset())
+ return; // shouldn't happen
+ if (kind == Undo::EDIT) // in this case insets would not be stored!
+ kind = Undo::FINISH;
+ setUndo(this, kind,
+ text->cursor.par(),
+ text->cursor.par()->next());
+}
+
+
+void BufferView::updateInset(Inset * inset, bool mark_dirty)
+{
+ pimpl_->updateInset(inset, mark_dirty);
+}
+
+
+bool BufferView::ChangeInsets(Inset::Code code,
+ string const & from, string const & to)
+{
+ bool need_update = false;
+ LyXCursor cursor = text->cursor;
+ LyXCursor tmpcursor = cursor;
+ cursor.par(tmpcursor.par());
+ cursor.pos(tmpcursor.pos());
+
+ ParIterator end = buffer()->par_iterator_end();
+ for (ParIterator it = buffer()->par_iterator_begin();
+ it != end; ++it) {
+ Paragraph * par = *it;
+ bool changed_inset = false;
+ for (InsetList::iterator it2 = par->insetlist.begin();
+ it2 != par->insetlist.end(); ++it2) {
+ if (it2.getInset()->lyxCode() == code) {
+ InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
+ if (inset->getContents() == from) {
+ inset->setContents(to);
+ changed_inset = true;
+ }
+ }
+ }
+ if (changed_inset) {
+ need_update = true;
+
+ // FIXME
+
+ // 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(this, cursor.par(), cursor.pos());
+ return need_update;
+}
+
+
+bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
+{
+ // Check if the label 'from' appears more than once
+ vector<string> labels = buffer()->getLabelList();
+
+ if (lyx::count(labels.begin(), labels.end(), from) > 1)
+ return false;
+
+ return ChangeInsets(Inset::REF_CODE, from, to);
+}
+
+
+bool BufferView::ChangeCitationsIfUnique(string const & from,
+ string const & to)
+{
+ typedef pair<string, string> StringPair;
+
+ vector<StringPair> keys = buffer()->getBibkeyList();
+ if (count_if(keys.begin(), keys.end(),
+ lyx::equal_1st_in_pair<StringPair>(from))
+ > 1)
+ return false;
+
+ return ChangeInsets(Inset::CITE_CODE, from, to);
+}
+
+
+UpdatableInset * BufferView::theLockingInset() const
+{
+ // If NULL is not allowed we should put an Assert here. (Lgb)
+ if (text)
+ return text->the_locking_inset;
+ return 0;
+}
+
+
+void BufferView::theLockingInset(UpdatableInset * inset)
+{
+ text->the_locking_inset = inset;
+}
+
+
+LyXText * BufferView::getLyXText() const
+{
+ if (theLockingInset()) {
+ LyXText * txt = theLockingInset()->getLyXText(this, true);
+ if (txt)
+ return txt;
+ }
+ return text;
+}
+
+
+LyXText * BufferView::getParentText(Inset * inset) const
+{
+ if (inset->owner()) {
+ LyXText * txt = inset->getLyXText(this);
+ inset = inset->owner();
+ while (inset && inset->getLyXText(this) == txt)
+ inset = inset->owner();
+ if (inset)
+ return inset->getLyXText(this);
+ }
+ return text;