Jean-Marc Lasgouttes wrote:
> My point is that the behaviour of getInsetByCode is intended and
> should be left alone.

OK, then the comment to the function is at least misleading. How about the 
following (I know that it is not exactly what you are looking for, but I do 
not see the need in changing gotoInset now).

Jürgen
Index: BufferView_pimpl.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/BufferView_pimpl.C,v
retrieving revision 1.587
diff -u -r1.587 BufferView_pimpl.C
--- BufferView_pimpl.C	9 Jun 2005 09:58:03 -0000	1.587
+++ BufferView_pimpl.C	10 Jun 2005 08:36:21 -0000
@@ -51,6 +51,7 @@
 #include "undo.h"
 #include "vspace.h"
 
+#include "insets/insetbibtex.h"
 #include "insets/insetref.h"
 #include "insets/insettext.h"
 
@@ -122,7 +123,7 @@
 boost::signals::connection lostcon;
 
 
-/// Get next inset of this class from current cursor position
+/// Return an inset of this class if it exists at the current cursor position
 template <class T>
 T * getInsetByCode(LCursor & cur, InsetBase::Code code)
 {
@@ -135,6 +136,31 @@
 	return inset;
 }
 
+
+/// Get next inset of this class from current cursor position
+template <class T>
+T * findInset(LCursor & cur, InsetBase::Code code)
+{
+	T * inset = 0;
+	DocIterator it = cur;
+	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;
+}
+
 } // anon namespace
 
 
@@ -982,6 +1008,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;
@@ -1212,6 +1240,22 @@
 	case LFUN_CENTER:
 		center();
 		break;
+
+	case LFUN_BIBDB_ADD: {
+		InsetBibtex * inset =
+			findInset<InsetBibtex>(cursor_, InsetBase::BIBTEX_CODE);
+		if (inset)
+			inset->addDatabase(cmd.argument);
+		break;
+	}
+
+	case LFUN_BIBDB_DEL: {
+		InsetBibtex * inset =
+			findInset<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	10 Jun 2005 08:36:22 -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	10 Jun 2005 08:36:24 -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	10 Jun 2005 08:36:37 -0000
@@ -271,7 +271,7 @@
 bool InsetBibtex::addDatabase(string const & db)
 {
 	string contents(getContents());
-	if (!contains(contents, db)) {
+	if (tokenPos(contents, ',', db) == -1) {
 		if (!contents.empty())
 			contents += ',';
 		setContents(contents + db);
@@ -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