Jean-Marc Lasgouttes wrote:
> Sorry for being so long. As I worte earlier, I would prefer to modify
> gotoNextInset (which could just use a dociterator as parameter) and
> gotoInset to fit your needs. They do what you want, but they change
> the bv cursor.

You're a very patient educator :-)

> Would the following refactorization of the code help you? Note that I
> only tried to compile it, I did not check that it works.

I have divided the functions into gotoInset and findInset (since only the 
former really "goes" to the inset). I also thought about making both 
findInset functions a bool, but this is not necessary at the moment, since 
LFUN_BIBDB_{ADD|DEL} do not need this.

The attached works using your code. What do you think?

Many thanks for your help,
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	23 Jun 2005 11:18:11 -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)
 {
@@ -982,6 +983,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 +1215,26 @@
 	case LFUN_CENTER:
 		center();
 		break;
+
+	case LFUN_BIBDB_ADD: {
+		LCursor tmpcur = cursor_;
+		bv_funcs::findInset(tmpcur, InsetBase::BIBTEX_CODE, false);
+		InsetBibtex * inset = getInsetByCode<InsetBibtex>(tmpcur,
+						InsetBase::BIBTEX_CODE);
+		if (inset)
+			inset->addDatabase(cmd.argument);
+		break;
+	}
+
+	case LFUN_BIBDB_DEL: {
+		LCursor tmpcur = cursor_;
+		bv_funcs::findInset(tmpcur, InsetBase::BIBTEX_CODE, false);
+		InsetBibtex * inset = getInsetByCode<InsetBibtex>(tmpcur,
+						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	23 Jun 2005 11:18:12 -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: bufferview_funcs.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/bufferview_funcs.C,v
retrieving revision 1.152
diff -u -r1.152 bufferview_funcs.C
--- bufferview_funcs.C	31 May 2005 10:20:42 -0000	1.152
+++ bufferview_funcs.C	23 Jun 2005 11:18:13 -0000
@@ -213,22 +213,22 @@
 
 namespace {
 
-bool gotoNextInset(LCursor & cur,
+bool gotoNextInset(DocIterator & dit,
 		   vector<InsetBase_code> const & codes,
 		   string const & contents)
 {
-	LCursor tmpcur = cur;
+	DocIterator tmpdit = dit;
 
-	while (tmpcur) {
-		InsetBase const * inset = tmpcur.nextInset();
+	while (tmpdit) {
+		InsetBase const * inset = tmpdit.nextInset();
 		if (inset
 		    && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
 		    && (contents.empty() ||
 			static_cast<InsetCommand const *>(inset)->getContents() == contents)) {
-			cur = tmpcur;
+			dit = tmpdit;
 			return true;
 		}
-		tmpcur.forwardInset();
+		tmpdit.forwardInset();
 	}
 
 	return false;
@@ -237,32 +237,49 @@
 }
 
 
-void gotoInset(BufferView * bv, vector<InsetBase_code> const & codes,
+bool findInset(DocIterator & dit, vector<InsetBase_code> const & codes,
 	       bool same_content)
 {
 	string contents;
-	LCursor tmpcur = bv->cursor();
-	tmpcur.forwardInset();
+	DocIterator tmpdit = dit;
+	tmpdit.forwardInset();
 
 	if (same_content) {
-		InsetBase const * inset = tmpcur.nextInset();
+		InsetBase const * inset = tmpdit.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 (bv->cursor() != doc_iterator_begin(bv->buffer()->inset())) {
-			tmpcur.reset(tmpcur.bottom().inset());
-			if (!gotoNextInset(tmpcur, codes, contents)) {
-				bv->cursor().message(_("No more insets"));
-				return;
+	if (!gotoNextInset(tmpdit, codes, contents)) {
+		if (dit.depth() != 1 || dit.pit() != 0 || dit.pos() != 0) {
+			tmpdit  = doc_iterator_begin(tmpdit.bottom().inset());
+			if (!gotoNextInset(tmpdit, codes, contents)) {
+				return false;
 			}
-		} else {
-			bv->cursor().message(_("No more insets"));
-			return;
-		}
+		} else
+			return false;
+	}
+	
+	dit = tmpdit;
+	return true;
+}
+
+
+void findInset(DocIterator & dit, InsetBase_code code, bool same_content)
+{
+	findInset(dit, vector<InsetBase_code>(1, code), same_content);
+}
+
+
+void gotoInset(BufferView * bv, vector<InsetBase_code> const & codes,
+	       bool same_content)
+{
+	LCursor tmpcur = bv->cursor();
+	if (!findInset(tmpcur, codes, same_content)) {
+		bv->cursor().message(_("No more insets"));
+		return;
 	}
 
 	tmpcur.clearSelection();
Index: bufferview_funcs.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/bufferview_funcs.h,v
retrieving revision 1.36
diff -u -r1.36 bufferview_funcs.h
--- bufferview_funcs.h	26 Apr 2005 11:12:09 -0000	1.36
+++ bufferview_funcs.h	23 Jun 2005 11:18:13 -0000
@@ -51,12 +51,19 @@
 
 Point coordOffset(DocIterator const & dit);
 
-// Moves cursor to the next inset with one of the given codes.
+/// Moves cursor to the next inset with one of the given codes.
 void gotoInset(BufferView * bv, std::vector<InsetBase_code> const & codes,
 	       bool same_content);
 
-// Moves cursor to the next inset with given code.
+/// Moves cursor to the next inset with given code.
 void gotoInset(BufferView * bv, InsetBase_code code, bool same_content);
+
+/// Looks for next inset with one of the the given code
+bool findInset(DocIterator & dit, std::vector<InsetBase_code> const & codes,
+	       bool same_content);
+
+/// Looks for next inset with the given code
+void findInset(DocIterator & dit, InsetBase_code code, bool same_content);
 
 
 } // namespace bv_funcs
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	23 Jun 2005 11:18:14 -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	23 Jun 2005 11:18:19 -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