Hi, Richard,

I think most of your problems can be solved in a less-radical way, by
saving relative path, or something like that, so that you can recover
user input.

The attached patch tries to do exactly this. It adds meta_ to
EmbeddedFile and allow InsetBibtex to add 'biblio' to an EmbeddedFile.
params are created from this meta info so minimal changes are needed
to the current code base.

Latex output etc does not work yet due to my lack of time, but with
this meta_ information, I am sure you can quickly figure them out.

Cheers,
Bo
Index: src/insets/InsetBibtex.cpp
===================================================================
--- src/insets/InsetBibtex.cpp	(revision 24012)
+++ src/insets/InsetBibtex.cpp	(working copy)
@@ -705,9 +705,11 @@
 
 
 
-bool InsetBibtex::addDatabase(string const & db)
+bool InsetBibtex::addDatabase(docstring const & db)
 {
-	EmbeddedFile file(changeExtension(db, "bib"), buffer().filePath());
+	FileName const texPath = getBibTeXPath(db, buffer());
+	EmbeddedFile file(texPath.absFilename(), buffer().filePath());
+	file.setMetaInfo(db);
 	
 	// only compare filename
 	EmbeddedFileList::iterator it = bibfiles_.begin();
@@ -722,9 +724,10 @@
 }
 
 
-bool InsetBibtex::delDatabase(string const & db)
+bool InsetBibtex::delDatabase(docstring const & db)
 {
-	EmbeddedFile file(changeExtension(db, "bib"), buffer().filePath());
+	FileName const texPath = getBibTeXPath(db, buffer());
+	EmbeddedFile file(texPath.absFilename(), buffer().filePath());
 	
 	// only compare filename
 	EmbeddedFileList::iterator it = bibfiles_.begin();
@@ -764,10 +767,15 @@
 	embedStatus = split(embedStatus, emb, ',');
 	
 	while (!tmp.empty()) {
-		EmbeddedFile file(changeExtension(tmp, "bib"), buffer().filePath());
+		docstring bibname = from_utf8(tmp);
+		FileName const texPath = getBibTeXPath(bibname, buffer());
+		EmbeddedFile file(texPath.absFilename(), buffer().filePath());
+		file.setMetaInfo(bibname);
 		
 		file.setInzipName(emb);
 		file.setEmbed(!emb.empty());
+		// enable but not copy files because we are sure these
+		// files exist.
 		file.enable(buffer().embedded(), &buffer(), false);
 		bibfiles_.push_back(file);
 		// Get next file name
@@ -792,7 +800,7 @@
 			embed += ',';
 		} else
 			first = false;
-		bibfiles += from_utf8(it->outputFilename(buffer().filePath()));
+		bibfiles += it->metaInfo();
 		if (it->embedded())
 			embed += from_utf8(it->inzipName());
 	}
@@ -801,6 +809,19 @@
 }
 
 
+// look up the path to the file using TeX
+FileName InsetBibtex::getBibTeXPath(docstring const & filename, Buffer const & buf)
+{
+	string texfile = changeExtension(to_utf8(filename), "bib");
+	// note that, if the filename can be found directly from the path, 
+	// findtexfile will just return a FileName object for that path.
+	FileName file(findtexfile(texfile, "bib"));
+	if (file.empty())
+		file = FileName(makeAbsPath(texfile, buf.filePath()));
+	return file;
+}
+
+
 void InsetBibtex::registerEmbeddedFiles(EmbeddedFileList & files) const
 {
 	if (bibfiles_.empty())
Index: src/insets/InsetBibtex.h
===================================================================
--- src/insets/InsetBibtex.h	(revision 24012)
+++ src/insets/InsetBibtex.h	(working copy)
@@ -43,9 +43,9 @@
 	///
 	EmbeddedFileList embeddedFiles() const;
 	///
-	bool addDatabase(std::string const &);
+	bool addDatabase(docstring const &);
 	///
-	bool delDatabase(std::string const &);
+	bool delDatabase(docstring const &);
 	///
 	void validate(LaTeXFeatures &) const;
 	///
@@ -63,6 +63,10 @@
 	void createBibFiles(docstring const & bibfiles, docstring const & embed) const;
 	/// update bibfiles and embed from bibfiles_
 	void updateParam();
+	///
+	static support::FileName 
+		getBibTeXPath(docstring const & filename, Buffer const & buf);
+	
 private:
 	///
 	void registerEmbeddedFiles(EmbeddedFileList &) const;
Index: src/BufferView.cpp
===================================================================
--- src/BufferView.cpp	(revision 24012)
+++ src/BufferView.cpp	(working copy)
@@ -1248,7 +1248,7 @@
 		InsetBibtex * inset = getInsetByCode<InsetBibtex>(tmpcur,
 						BIBTEX_CODE);
 		if (inset) {
-			if (inset->addDatabase(to_utf8(cmd.argument())))
+			if (inset->addDatabase(cmd.argument()))
 				buffer_.updateBibfilesCache();
 		}
 		break;
@@ -1260,7 +1260,7 @@
 		InsetBibtex * inset = getInsetByCode<InsetBibtex>(tmpcur,
 						BIBTEX_CODE);
 		if (inset) {
-			if (inset->delDatabase(to_utf8(cmd.argument())))
+			if (inset->delDatabase(cmd.argument()))
 				buffer_.updateBibfilesCache();
 		}
 		break;
Index: src/EmbeddedFiles.cpp
===================================================================
--- src/EmbeddedFiles.cpp	(revision 24012)
+++ src/EmbeddedFiles.cpp	(working copy)
@@ -48,7 +48,7 @@
 namespace Alert = frontend::Alert;
 
 EmbeddedFile::EmbeddedFile(string const & file, std::string const & buffer_path)
-	: DocFileName("", false), embedded_(false), inset_list_()
+	: DocFileName("", false), embedded_(false), inset_list_(), meta_()
 {
 	set(file, buffer_path);
 }
Index: src/frontends/qt4/GuiBibtex.cpp
===================================================================
--- src/frontends/qt4/GuiBibtex.cpp	(revision 24012)
+++ src/frontends/qt4/GuiBibtex.cpp	(working copy)
@@ -25,6 +25,8 @@
 
 #include "ButtonPolicy.h"
 
+#include "insets/InsetBibtex.h"
+
 #include "frontends/alert.h"
 
 #include "support/debug.h"
@@ -370,8 +372,9 @@
 		QString filename = databaseLW->item(i)->text();
 		dbs += qstring_to_ucs4(filename);
 		try {
-			EmbeddedFile file(fromqstr(changeExtension(filename, "bib")),
-				buf.filePath());
+			docstring bibfiled = qstring_to_ucs4(filename);
+			FileName bibfilepath = InsetBibtex::getBibTeXPath(bibfiled, buf);
+			EmbeddedFile file(bibfilepath.absFilename(), buf.filePath());
 			file.setEmbed(databaseLW->item(i)->checkState() == Qt::Checked);
 			// move file around if needed, an exception may be raised.
 			file.enable(buf.embedded(), &buf, true);
Index: src/EmbeddedFiles.h
===================================================================
--- src/EmbeddedFiles.h	(revision 24012)
+++ src/EmbeddedFiles.h	(working copy)
@@ -124,6 +124,11 @@
 	/// filename in the zip file, which is related to buffer temp directory.
 	std::string inzipName() const { return inzip_name_; }
 
+	/// set some meta information to this EmbeddedFile
+	void setMetaInfo(docstring const & meta) { meta_ = meta; }
+	/// get meta info
+	docstring metaInfo() const { return meta_; }
+	
 	/// embedded file, equals to temppath()/inzipName()
 	std::string embeddedFile() const;
 	/// embeddedFile() or absFilename() depending on embedding status
@@ -190,6 +195,10 @@
 	/// be retrived from a buffer, but a buffer is not always available when
 	/// an EmbeddedFile is used.
 	std::string temp_path_;
+	///
+	/// meta information that is used by various insets to store
+	/// auxillary information.
+	docstring meta_;
 };
 
 

Reply via email to