Juergen Spitzmueller wrote:
> http://bugzilla.lyx.org/show_bug.cgi?id=961
>
> The attached patch reimplements those lfuns. It also fixes
> InsetBibtex::delDatabase, which is broken in 1.3.
>
> The only drawback is that the lfuns are not working when the cursor sits
> behind the inset. The same applies to 1.3. The reason is that
> getInsetByCode stops at the end of the document.

Attached is a third (and IMHO final) version that fixes all drawbacks and 
additionally also fixes a problem in InsetBibtex::addDatabase, which did not 
add a database when its name was a substring of another database (e.g. 
"mybib" vs. "mybib2").

OK to apply?

Jürgen
Index: BufferView_pimpl.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/BufferView_pimpl.C,v
retrieving revision 1.586
diff -u -r1.586 BufferView_pimpl.C
--- BufferView_pimpl.C	6 Jun 2005 13:11:29 -0000	1.586
+++ BufferView_pimpl.C	6 Jun 2005 13:21:07 -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;
 }
@@ -982,6 +994,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 +1226,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	6 Jun 2005 13:21:08 -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	6 Jun 2005 13:21:08 -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	6 Jun 2005 13:21:13 -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