[LyX/2.3.x] Provide proper fallback if a bibliography processor is not found

Juergen Spitzmueller spitz at lyx.org
Mon Oct 18 06:21:52 UTC 2021


commit c7d29be153debac82e3d2e8865fcc849f0a5f40d
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Sun Oct 17 11:29:18 2021 +0200

    Provide proper fallback if a bibliography processor is not found
    
    Check for appropriate fallbacks and warn user if the requested
    bibliography processor is unavailable.
    
    (cherry picked from commit f3500725655c2a219d86f5f341195b4ae365ab4c)
---
 src/BufferParams.cpp |   66 +++++++++++++++++++++++++++++++++++++++++++++++--
 src/BufferParams.h   |    5 +++-
 src/Converter.cpp    |    2 +-
 3 files changed, 68 insertions(+), 5 deletions(-)

diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp
index 11a7842..51aab82 100644
--- a/src/BufferParams.cpp
+++ b/src/BufferParams.cpp
@@ -3565,11 +3565,71 @@ vector<CitationStyle> BufferParams::citeStyles() const
 }
 
 
-string const BufferParams::bibtexCommand() const
+string const BufferParams::getBibtexCommand(string const cmd, bool const warn) const
+{
+	// split from options
+	string command_in;
+	split(cmd, command_in, ' ');
+
+	// Look if the requested command is available. If so, use that.
+	for (auto const & alts : lyxrc.bibtex_alternatives) {
+		string command_prov;
+		split(alts, command_prov, ' ');
+		if (command_in == command_prov)
+			return cmd;
+	}
+
+	// If not, find the most suitable fallback for the current cite framework,
+	// and warn. Note that we omit options in any such case.
+	string fallback;
+	if (useBiblatex()) {
+		// For Biblatex, we prefer biber (also for Japanese)
+		// and try to fall back to bibtex8
+		if (lyxrc.bibtex_alternatives.find("biber") != lyxrc.bibtex_alternatives.end())
+			fallback = "biber";
+		else if (lyxrc.bibtex_alternatives.find("bibtex8") != lyxrc.bibtex_alternatives.end())
+			fallback = "bibtex8";
+	}
+	// For classic BibTeX and as last resort for biblatex, try bibtex
+	if (fallback.empty()) {
+		if (lyxrc.bibtex_alternatives.find("bibtex") != lyxrc.bibtex_alternatives.end())
+			fallback = "bibtex";
+	}
+
+	if (!warn)
+		return fallback;
+
+	if (fallback.empty()) {
+		frontend::Alert::warning(
+			_("No bibliography processor found!"),
+			support::bformat(
+			  _("The bibliography processor requested by this document "
+			    "(%1$s) is not available and no appropriate "
+			    "alternative has been found. "
+			    "No bibliography and references will be generated.\n"
+			    "Please fix your installation!"),
+			  from_utf8(cmd)));
+	} else {
+		frontend::Alert::warning(
+			_("Requested bibliography processor not found!"),
+			support::bformat(
+			  _("The bibliography processor requested by this document "
+			    "(%1$s) is not available. "
+			    "As a fallback, '%2$s' will be used, options are omitted. "
+			    "This might result in errors or unwanted changes in "
+			    "the bibliography. Please check carefully!\n"
+			    "It is suggested to install the missing processor."),
+			  from_utf8(cmd), from_utf8(fallback)));
+	}
+	return fallback;
+}
+
+
+string const BufferParams::bibtexCommand(bool const warn) const
 {
 	// Return document-specific setting if available
 	if (bibtex_command != "default")
-		return bibtex_command;
+		return getBibtexCommand(bibtex_command, warn);
 
 	// If we have "default" in document settings, consult the prefs
 	// 1. Japanese (uses a specific processor)
@@ -3589,7 +3649,7 @@ string const BufferParams::bibtexCommand() const
 	// 2. All other languages
 	else if (lyxrc.bibtex_command != "automatic")
 		// Return the specified program, if "automatic" is not set
-		return lyxrc.bibtex_command;
+		return getBibtexCommand(lyxrc.bibtex_command, warn);
 
 	// 3. Automatic: find the most suitable for the current cite framework
 	if (useBiblatex()) {
diff --git a/src/BufferParams.h b/src/BufferParams.h
index 23cd435..87c848c 100644
--- a/src/BufferParams.h
+++ b/src/BufferParams.h
@@ -490,7 +490,7 @@ public:
 	std::vector<CitationStyle> citeStyles() const;
 
 	/// Return the actual bibtex command (lyxrc or buffer param)
-	std::string const bibtexCommand() const;
+	std::string const bibtexCommand(bool const warn = false) const;
 
 	/// Are we using biblatex?
 	bool useBiblatex() const;
@@ -589,6 +589,9 @@ private:
 	std::string biblio_style;
 	/// Split bibliography?
 	bool use_bibtopic;
+	/// Return the actual or an appropriate fallback bibtex command
+	std::string const getBibtexCommand(std::string const cmd,
+					   bool const warn) const;
 	///
 	DocumentClassPtr doc_class_;
 	///
diff --git a/src/Converter.cpp b/src/Converter.cpp
index e272d08..666e61c 100644
--- a/src/Converter.cpp
+++ b/src/Converter.cpp
@@ -467,7 +467,7 @@ bool Converters::convert(Buffer const * buffer,
 			 || suffixIs(buffer->params().bufferFormat(), "-ja"))
 			&& buffer->params().encoding().package() == Encoding::japanese;
 		runparams.use_indices = buffer->params().use_indices;
-		runparams.bibtex_command = buffer->params().bibtexCommand();
+		runparams.bibtex_command = buffer->params().bibtexCommand(true);
 		runparams.index_command = (buffer->params().index_command == "default") ?
 			string() : buffer->params().index_command;
 		runparams.document_language = buffer->params().language->babel();


More information about the lyx-cvs mailing list