+
+
+bool BufferView::ChangeInsets(Inset::Code code,
+ string const & from, string const & to)
+{
+ bool flag = false;
+ LyXParagraph * par = buffer()->paragraph;
+ LyXCursor cursor = text->cursor;
+ LyXCursor tmpcursor = cursor;
+#ifndef NEW_INSETS
+ cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
+ cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
+#else
+ cursor.par(tmpcursor.par());
+ cursor.pos(tmpcursor.pos());
+#endif
+
+ while (par) {
+ bool flag2 = false;
+ for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
+ it != par->inset_iterator_end(); ++it) {
+ if ((*it)->LyxCode() == code) {
+ InsetCommand * inset = static_cast<InsetCommand *>(*it);
+ if (inset->getContents() == from) {
+ inset->setContents(to);
+ flag2 = true;
+ }
+ }
+ }
+#ifndef NEW_INSETS
+ if (flag2) {
+ flag = true;
+ if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
+ // this is possible now, since SetCursor takes
+ // care about footnotes
+ text->SetCursorIntern(this, par, 0);
+ text->RedoParagraphs(this, text->cursor,
+ text->cursor.par()->next());
+ text->FullRebreak(this);
+ }
+ }
+ par = par->next_;
+#else
+ if (flag2) {
+ flag = true;
+ // this is possible now, since SetCursor takes
+ // care about footnotes
+ text->SetCursorIntern(this, par, 0);
+ text->RedoParagraphs(this, text->cursor,
+ text->cursor.par()->next());
+ text->FullRebreak(this);
+ }
+ par = par->next();
+#endif
+ }
+ text->SetCursorIntern(this, cursor.par(), cursor.pos());
+ return flag;
+}
+
+
+bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
+{
+ // Check if the label 'from' appears more than once
+ vector<string> labels = buffer()->getLabelList();
+ if (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)
+{
+
+ vector<pair<string,string> > keys = buffer()->getBibkeyList();
+ if (count_if(keys.begin(), keys.end(),
+ equal_1st_in_pair<string,string>(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;
+}
+
+
+Language const * BufferView::getParentLanguage(Inset * inset) const
+{
+ LyXText * text = getParentText(inset);
+ return text->cursor.par()->GetFontSettings(buffer()->params,
+ text->cursor.pos()).language();
+}