]> git.lyx.org Git - lyx.git/blobdiff - src/bufferview_funcs.C
fix crash when collapsing ert with cursor inside
[lyx.git] / src / bufferview_funcs.C
index 74688ae6b43e7459a1a77b96ba84d6f795005fa5..170ed0b80e0fb8cfbae8a527cdf52d48929b9940 100644 (file)
@@ -33,6 +33,7 @@
 #include "frontends/Alert.h"
 #include "frontends/LyXView.h"
 
+#include "insets/insetcommand.h"
 #include "insets/insettext.h"
 
 #include "support/convert.h"
@@ -44,6 +45,7 @@ using lyx::support::bformat;
 using std::istringstream;
 using std::ostringstream;
 using std::string;
+using std::vector;
 
 
 namespace bv_funcs {
@@ -209,5 +211,66 @@ CurStatus status(BufferView const * bv, DocIterator const & dit)
                return CUR_BELOW;
 }
 
+namespace {
+
+bool gotoNextInset(LCursor & cur,
+                  vector<InsetBase_code> const & codes,
+                  string const & contents)
+{
+       LCursor tmpcur = cur;
+
+       while (tmpcur) {
+               InsetBase const * inset = tmpcur.nextInset();
+               if (inset
+                   && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
+                   && (contents.empty() ||
+                       static_cast<InsetCommand const *>(inset)->getContents() == contents)) {
+                       cur = tmpcur;
+                       return true;
+               }
+               tmpcur.forwardInset();
+       }
+
+       return false;
+}
+
+}
+
+
+void gotoInset(BufferView * bv, vector<InsetBase_code> const & codes,
+              bool same_content)
+{
+       string contents;
+       LCursor tmpcur = bv->cursor();
+       tmpcur.forwardInset();
+
+       if (same_content) {
+               InsetBase const * inset = tmpcur.nextInset();
+               if (inset
+                   && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
+                       contents = static_cast<InsetCommand const *>(inset)->getContents();
+               }
+       }
+
+       if (!gotoNextInset(tmpcur, codes, contents)) {
+               if (tmpcur != doc_iterator_begin(tmpcur.inset())) {
+                       tmpcur.reset(tmpcur.bottom().inset());
+                       if (!gotoNextInset(tmpcur, codes, contents))
+                               bv->cursor().message(_("No more insets"));
+               } else {
+                       bv->cursor().message(_("No more insets"));
+               }
+       }
+
+       tmpcur.clearSelection();
+       bv->setCursor(tmpcur);
+}
+
+
+void gotoInset(BufferView * bv, InsetBase_code code, bool same_content)
+{
+       gotoInset(bv, vector<InsetBase_code>(1, code), same_content);
+}
+
 
 } // namespace bv_funcs