Juergen Spitzmueller wrote:
> /// Get next inset of this class from current cursor position
> template <class T>
> T * getInsetByCode(LCursor & cur, InsetBase::Code code)
> {
>         T * inset = 0;
>         DocIterator it = cur;
>         if (it.nextInset() &&
>             it.nextInset()->lyxCode() == code) {
>                 inset = static_cast<T*>(it.nextInset());
>         }
>         return inset;
> }

Actually, this thing is so stupid that it stumbles over the next inset, no 
matter what kind. The attached patch fixes this and also starts from the 
beginning.

What do you think?

Jürgen
Index: BufferView_pimpl.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/BufferView_pimpl.C,v
retrieving revision 1.584
diff -u -r1.584 BufferView_pimpl.C
--- BufferView_pimpl.C	31 May 2005 14:40:23 -0000	1.584
+++ BufferView_pimpl.C	3 Jun 2005 18:03:15 -0000
@@ -51,6 +51,7 @@
 #include "undo.h"
 #include "vspace.h"
 
+#include "insets/insetbibtex.h"
 #include "insets/insetref.h"
 #include "insets/insettext.h"
 
@@ -128,9 +129,20 @@
 {
 	T * inset = 0;
 	DocIterator it = cur;
-	if (it.nextInset() &&
-	    it.nextInset()->lyxCode() == code) {
-		inset = static_cast<T*>(it.nextInset());
+	T * first_inset = static_cast<T*>(it.nextInset());
+	while (it) {
+		if (!it.nextInset()) {
+			// try from the beginning, but break after one loop
+			it.pit() = 0;
+			it.pos() = 0;
+			if (!it.nextInset() || it.nextInset() == first_inset)
+				break;
+		}
+		if (it.nextInset()->lyxCode() == code) {
+			inset = static_cast<T*>(it.nextInset());
+			break;
+		}
+		it.forwardInset();
 	}
 	return inset;
 }
@@ -987,6 +999,8 @@
 	case LFUN_MARK_ON:
 	case LFUN_SETMARK:
 	case LFUN_CENTER:
+	case LFUN_BIBDB_ADD:
+	case LFUN_BIBDB_DEL:
 	case LFUN_WORDS_COUNT:
 		flag.enabled(true);
 		break;
@@ -1211,6 +1225,24 @@
 	case LFUN_CENTER:
 		center();
 		break;
+
+	case LFUN_BIBDB_ADD: {
+		InsetBibtex * inset =
+			getInsetByCode<InsetBibtex>(cursor_,
+							 InsetBase::BIBTEX_CODE);
+		if (inset)
+			inset->addDatabase(cmd.argument);
+		break;
+	}
+
+	case LFUN_BIBDB_DEL: {
+		InsetBibtex * inset =
+			getInsetByCode<InsetBibtex>(cursor_,
+							 InsetBase::BIBTEX_CODE);
+		if (inset)
+			inset->delDatabase(cmd.argument);
+		break;
+	}
 
 	case LFUN_WORDS_COUNT: {
 		DocIterator from, to;
Index: LyXAction.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/LyXAction.C,v
retrieving revision 1.206
diff -u -r1.206 LyXAction.C
--- LyXAction.C	8 May 2005 10:02:37 -0000	1.206
+++ LyXAction.C	3 Jun 2005 18:03:17 -0000
@@ -188,6 +188,8 @@
 		{ LFUN_INSERT_LABEL, "label-insert", Noop },
 		{ LFUN_INSET_OPTARG, "optional-insert", Noop },
 		{ LFUN_INSERT_BIBITEM, "bibitem-insert", Noop },
+		{ LFUN_BIBDB_ADD, "bibtex-database-add", Noop },
+		{ LFUN_BIBDB_DEL, "bibtex-database-del", Noop },
 		{ LFUN_INSERT_LINE, "line-insert", Noop },
 		{ LFUN_INSERT_PAGEBREAK, "pagebreak-insert", Noop },
 		{ LFUN_LANGUAGE, "language", Noop },
Index: lfuns.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/lfuns.h,v
retrieving revision 1.41
diff -u -r1.41 lfuns.h
--- lfuns.h	8 May 2005 10:02:37 -0000	1.41
+++ lfuns.h	3 Jun 2005 18:03:19 -0000
@@ -355,6 +355,8 @@
 	// 270
 	LFUN_WORDS_COUNT,
 	LFUN_OUTPUT_CHANGES,             // jspitzm 20050121
+	LFUN_BIBDB_ADD,
+	LFUN_BIBDB_DEL,
 
 	LFUN_LASTACTION                  // end of the table
 };
Index: insets/insetbibtex.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insetbibtex.C,v
retrieving revision 1.54
diff -u -r1.54 insetbibtex.C
--- insets/insetbibtex.C	17 May 2005 11:11:45 -0000	1.54
+++ insets/insetbibtex.C	3 Jun 2005 18:03:24 -0000
@@ -283,16 +283,17 @@
 
 bool InsetBibtex::delDatabase(string const & db)
 {
-	if (contains(getContents(), db)) {
+	string contents(getContents());
+	if (contains(contents, db)) {
+		int const n = tokenPos(contents, ',', db);
 		string bd = db;
-		int const n = tokenPos(getContents(), ',', bd);
 		if (n > 0) {
-			// Weird code, would someone care to explain this?(Lgb)
-			string tmp(", ");
-			tmp += bd;
-			setContents(subst(getContents(), tmp, ", "));
+			// this is not the first database
+			string tmp = ',' + bd;
+			setContents(subst(contents, tmp, ""));
 		} else if (n == 0)
-			setContents(split(getContents(), bd, ','));
+			// this is the first (or only) database
+			setContents(split(contents, bd, ','));
 		else
 			return false;
 	}

Reply via email to