From 8b2ea705df7568412180d8c8eca6257b356ec0a7 Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Sun, 2 Jan 2011 18:16:23 +0000 Subject: [PATCH] Fix wrong setting of bibinset options if \cite{*} was found. Improve heuristic for outputting \bibliographystyle: Now it is suppressed if \bibliography follows immediately, since LyX adds it automatically in that case. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@37066 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/tex2lyx/Parser.cpp | 13 +++++++++++++ src/tex2lyx/Parser.h | 10 +++++++++- src/tex2lyx/text.cpp | 29 +++++++++++++++++++++++++---- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/tex2lyx/Parser.cpp b/src/tex2lyx/Parser.cpp index 1242b67ff2..28e5b724c6 100644 --- a/src/tex2lyx/Parser.cpp +++ b/src/tex2lyx/Parser.cpp @@ -276,6 +276,19 @@ void Parser::putback() } +void Parser::pushPosition() +{ + positions_.push_back(pos_); +} + + +void Parser::popPosition() +{ + pos_ = positions_.back(); + positions_.pop_back(); +} + + bool Parser::good() { if (pos_ < tokens_.size()) diff --git a/src/tex2lyx/Parser.h b/src/tex2lyx/Parser.h index 51c063ebf7..45d6c67ce8 100644 --- a/src/tex2lyx/Parser.h +++ b/src/tex2lyx/Parser.h @@ -110,7 +110,9 @@ std::ostream & operator<<(std::ostream & os, Token const & t); */ class Parser { - + /// noncopyable + Parser(Parser const & p); + Parser & operator=(Parser const & p); public: /// Parser(idocstream & is); @@ -128,6 +130,10 @@ public: int lineno() const { return lineno_; } /// void putback(); + /// store current position + void pushPosition(); + /// restore previous position + void popPosition(); /// dump contents to screen void dump() const; @@ -229,6 +235,8 @@ private: /// unsigned pos_; /// + std::vector positions_; + /// idocstringstream * iss_; /// idocstream & is_; diff --git a/src/tex2lyx/text.cpp b/src/tex2lyx/text.cpp index 4543c488ab..341bfd3a7c 100644 --- a/src/tex2lyx/text.cpp +++ b/src/tex2lyx/text.cpp @@ -2272,7 +2272,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, os << "after " << '"' << after << '"' << "\n"; os << "key " << '"' << key << '"' << "\n"; end_inset(os); - } else + } else if (t.cs() == "nocite") btprint = key; } @@ -2722,9 +2722,30 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, else if (t.cs() == "bibliographystyle") { // store new bibliographystyle bibliographystyle = p.verbatim_item(); - // output new bibliographystyle. - // This is only necessary if used in some other macro than \bibliography. - handle_ert(os, "\\bibliographystyle{" + bibliographystyle + "}", context); + // If any other command than \bibliography and + // \nocite{*} follows, we need to output the style + // (because it might be used by that command). + // Otherwise, it will automatically be output by LyX. + p.pushPosition(); + bool output = true; + for (Token t2 = p.get_token(); p.good(); t2 = p.get_token()) { + if (t2.cat() == catBegin) + break; + if (t2.cat() != catEscape) + continue; + if (t2.cs() == "nocite") { + if (p.getArg('{', '}') == "*") + continue; + } else if (t2.cs() == "bibliography") + output = false; + break; + } + p.popPosition(); + if (output) { + handle_ert(os, + "\\bibliographystyle{" + bibliographystyle + '}', + context); + } } else if (t.cs() == "bibliography") { -- 2.39.2