First step of moving the bibliography settings to the document settings (needed, e.g., for biblatex support). We define a default there. A BibTeX inset takes the "default" style from the buffer if it doesn't specify a style of its own. No UI yet, but it's coming. OK?

--
Julien
>From cb7225e511ea0ae8714a7bccc6e0bb94ae0698f6 Mon Sep 17 00:00:00 2001
From: Julien Rioux <jri...@lyx.org>
Date: Thu, 6 Oct 2011 17:38:05 +0200
Subject: [PATCH] Add a document-wide default bibliography style \biblio_style.

This holds the name of a BibTeX style file for now. Any BibTeX inset
can set the style to "default" to use the document-wide style.

LyX format incremented to 417.
---
 development/FORMAT         |    5 ++++
 lib/lyx2lyx/lyx_2_1.py     |   52 ++++++++++++++++++++++++++++++++++++++++---
 src/Buffer.cpp             |    2 +-
 src/BufferParams.cpp       |   17 ++++++++++++++
 src/BufferParams.h         |    7 ++++++
 src/insets/InsetBibtex.cpp |    3 ++
 6 files changed, 81 insertions(+), 5 deletions(-)

diff --git a/development/FORMAT b/development/FORMAT
index 10c1d88..577c07c 100644
--- a/development/FORMAT
+++ b/development/FORMAT
@@ -11,6 +11,11 @@ adjustments are made to tex2lyx and bugs are fixed in lyx2lyx.
 
 -----------------------
 
+2011-10-06 Julien Rioux <jri...@lyx.org>
+	* Format incremented to 417 (rXXXXX)
+	  New buffer param \biblio_style to specify a document-wide
+	  default bibliography style (BibTeX style for the moment).
+
 2011-08-29 Uwe Stöhr <uwesto...@web.de>
 	* Format incremented to 416 (r39557)
 	  support for \negmedspace and \negthinspace outside of math
diff --git a/lib/lyx2lyx/lyx_2_1.py b/lib/lyx2lyx/lyx_2_1.py
index 6c6c6fe..ead33f2 100644
--- a/lib/lyx2lyx/lyx_2_1.py
+++ b/lib/lyx2lyx/lyx_2_1.py
@@ -25,7 +25,8 @@ import sys, os
 
 # Uncomment only what you need to import, please.
 
-from parser_tools import find_token, find_end_of_inset, get_value
+from parser_tools import find_token, find_end_of_inset, get_value, \
+    get_quoted_value
 
 #from parser_tools import find_token, find_end_of, find_tokens, \
   #find_token_exact, find_end_of_inset, find_end_of_layout, \
@@ -162,17 +163,60 @@ def revert_math_spaces(document):
       i = i + 1
 
 
+def convert_biblio_style(document):
+    "Add a sensible default for \\biblio_style based on the citation engine."
+    i = find_token(document.header, "\\cite_engine", 0)
+    if i != -1:
+        engine = get_value(document.header, "\\cite_engine", i).split("_")[0]
+        style = {"basic": "plain", "natbib": "plainnat", "jurabib": "jurabib"}
+        document.header.insert(i + 1, "\\biblio_style " + style[engine])
+
+
+def revert_biblio_style(document):
+    "BibTeX insets with default option use the style defined by \\biblio_style."
+    i = find_token(document.header, "\\biblio_style" , 0)
+    if i == -1:
+        document.warning("No \\biblio_style line. Nothing to do.")
+        return
+
+    default_style = get_value(document.header, "\\biblio_style", i)
+    del document.header[i]
+
+    # We are looking for bibtex insets having the default option
+    i = 0
+    while True:
+        i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
+        if i == -1:
+            return
+        j = find_end_of_inset(document.body, i)
+        if j == -1:
+            document.warning("Malformed LyX document: Can't find end of bibtex inset at line " + str(i))
+            i += 1
+            return
+        k = find_token(document.body, "options", i, j)
+        if k != -1:
+            options = get_quoted_value(document.body, "options", k)
+            if options.split(",")[0] == "default":
+                document.body[k] = 'options "%s"' \
+                    % options.replace("default", default_style) + '"'
+        i = j
+
+
 ##
 # Conversion hub
 #
 
 supported_versions = ["2.1.0","2.1"]
-convert = [[414, []],
+convert = [
+           [414, []],
            [415, [convert_undertilde]],
-           [416, []]
+           [416, []],
+           [417, [convert_biblio_style]],
           ]
 
-revert =  [[415, [revert_negative_space,revert_math_spaces]],
+revert =  [
+           [416, [revert_biblio_style]],
+           [415, [revert_negative_space,revert_math_spaces]],
            [414, [revert_undertilde]],
            [413, [revert_visible_space]]
           ]
diff --git a/src/Buffer.cpp b/src/Buffer.cpp
index 6aedab2..1b892e5 100644
--- a/src/Buffer.cpp
+++ b/src/Buffer.cpp
@@ -128,7 +128,7 @@ namespace {
 
 // Do not remove the comment below, so we get merge conflict in
 // independent branches. Instead add your own.
-int const LYX_FORMAT = 416; //uwestoehr : support for horizontal spaces (bug 7728)
+int const LYX_FORMAT = 417; //jrioux : document-wide BibTeX style via \biblio_style
 
 typedef map<string, bool> DepClean;
 typedef map<docstring, pair<InsetLabel const *, Buffer::References> > RefCache;
diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp
index 4f1edc1..e0f66a0 100644
--- a/src/BufferParams.cpp
+++ b/src/BufferParams.cpp
@@ -367,6 +367,7 @@ BufferParams::BufferParams()
 	use_mathdots = package_auto;
 	use_undertilde = package_auto;
 	cite_engine_ = ENGINE_BASIC;
+	biblio_style_ = "plain";
 	use_bibtopic = false;
 	use_indices = false;
 	trackChanges = false;
@@ -695,6 +696,9 @@ string BufferParams::readToken(Lexer & lex, string const & token,
 		string engine;
 		lex >> engine;
 		cite_engine_ = citeenginetranslator().find(engine);
+	} else if (token == "\\biblio_style") {
+		lex.eatLine();
+		biblio_style_ = lex.getString();
 	} else if (token == "\\use_bibtopic") {
 		lex >> use_bibtopic;
 	} else if (token == "\\use_indices") {
@@ -998,6 +1002,7 @@ void BufferParams::writeFile(ostream & os) const
 	   << "\n\\use_mathdots " << use_mathdots
 	   << "\n\\use_undertilde " << use_undertilde
 	   << "\n\\cite_engine " << citeenginetranslator().find(cite_engine_)
+	   << "\n\\biblio_style " << biblio_style_
 	   << "\n\\use_bibtopic " << convert<string>(use_bibtopic)
 	   << "\n\\use_indices " << convert<string>(use_indices)
 	   << "\n\\paperorientation " << string_orientation[orientation]
@@ -2954,4 +2959,16 @@ void BufferParams::setCiteEngine(CiteEngine cite_engine)
 	cite_engine_ = cite_engine;
 }
 
+
+string BufferParams::biblioStyle() const
+{
+	return biblio_style_;
+}
+
+
+void BufferParams::setBiblioStyle(string biblio_style)
+{
+	biblio_style_ = biblio_style;
+}
+
 } // namespace lyx
diff --git a/src/BufferParams.h b/src/BufferParams.h
index a141ded..f5ddf6f 100644
--- a/src/BufferParams.h
+++ b/src/BufferParams.h
@@ -411,6 +411,11 @@ public:
 	///
 	void setCiteEngine(CiteEngine const);
 
+	/// default BibTeX style file
+	std::string biblioStyle() const;
+	///
+	void setBiblioStyle(std::string const);
+
 	/// options for pdf output
 	PDFOptions & pdfoptions();
 	PDFOptions const & pdfoptions() const;
@@ -471,6 +476,8 @@ private:
 	mutable DefaultFlavorCache default_flavors_;
 	/// for use with natbib
 	CiteEngine cite_engine_;
+	/// the default BibTeX style
+	std::string biblio_style_;
 	///
 	DocumentClass * doc_class_;
 	/// 
diff --git a/src/insets/InsetBibtex.cpp b/src/insets/InsetBibtex.cpp
index 329b329..2b7a3e5 100644
--- a/src/insets/InsetBibtex.cpp
+++ b/src/insets/InsetBibtex.cpp
@@ -332,6 +332,9 @@ void InsetBibtex::latex(otexstream & os, OutputParams const & runparams) const
 			style = split(style, bibtotoc, ',');
 	}
 
+	if (style == "default")
+		style = buffer().params().biblioStyle();
+
 	if (!style.empty()) {
 		string base = normalizeName(buffer(), runparams, style, ".bst");
 		FileName const try_in_file = 
-- 
1.7.3.4

Reply via email to