From 562e961cf127e7c1838b6a40f56bc5a97011b471 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Spitzm=C3=BCller?= Date: Fri, 9 May 2008 11:13:44 +0000 Subject: [PATCH] Fix bug 4806 (patch from JMarc) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit         * Context.cpp (begin_layout): make it a (private) method, and output         the extra stuff here.         (add_par_extra_stuff): this is used for stuff that is only for next         paragraph (like extra_stuff used to be); add_extra_stuff is now for         code that is output for all paragraphs in the context.         * text.cpp: update to new Context methods. Every user of add_extra_stuff         uses add_par_extra_stuff, except alignment (which has to be repeated         to fix bug 4806). git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/branches/BRANCH_1_5_X@24698 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/tex2lyx/Context.cpp | 53 +++++++++++++++++++++++++---------------- src/tex2lyx/Context.h | 13 +++++++++- src/tex2lyx/text.cpp | 14 +++++------ 3 files changed, 51 insertions(+), 29 deletions(-) diff --git a/src/tex2lyx/Context.cpp b/src/tex2lyx/Context.cpp index 94bb4668fc..8a314431a2 100644 --- a/src/tex2lyx/Context.cpp +++ b/src/tex2lyx/Context.cpp @@ -22,19 +22,10 @@ using std::ostream; using std::endl; using std::string; +using support::contains; namespace { -void begin_layout(ostream & os, Layout_ptr layout, TeXFont const & font, - TeXFont const & normalfont) -{ - os << "\n\\begin_layout " << to_utf8(layout->name()) << "\n"; - // FIXME: This is not enough for things like - // \\Huge par1 \\par par2 - output_font_change(os, normalfont, font); -} - - void end_layout(ostream & os) { os << "\n\\end_layout\n"; @@ -103,9 +94,25 @@ Context::Context(bool need_layout_, Context::~Context() { - if (!extra_stuff.empty()) - std::cerr << "Bug: Ignoring extra stuff '" << extra_stuff - << '\'' << std::endl; + if (!par_extra_stuff.empty()) + std::cerr << "Bug: Ignoring par-level extra stuff '" + << par_extra_stuff << '\'' << endl; +} + + +void Context::begin_layout(ostream & os, Layout_ptr l) +{ + os << "\n\\begin_layout " << to_utf8(l->name()) << "\n"; + if (!extra_stuff.empty()) { + os << extra_stuff; + } + if (!par_extra_stuff.empty()) { + os << par_extra_stuff; + par_extra_stuff.erase(); + } + // FIXME: This is not enough for things like + // \\Huge par1 \\par par2 + output_font_change(os, normalfont, font); } @@ -125,7 +132,7 @@ void Context::check_layout(ostream & os) end_deeper(os); deeper_paragraph = false; } - begin_layout(os, layout, font, normalfont); + begin_layout(os, layout); has_item = false; } else { // a standard paragraph in an @@ -133,20 +140,15 @@ void Context::check_layout(ostream & os) // that this may require a begin_deeper. if (!deeper_paragraph) begin_deeper(os); - begin_layout(os, textclass.defaultLayout(), - font, normalfont); + begin_layout(os, textclass.defaultLayout()); deeper_paragraph = true; } } else { // No list-like environment - begin_layout(os, layout, font, normalfont); + begin_layout(os, layout); } need_layout = false; need_end_layout = true; - if (!extra_stuff.empty()) { - os << extra_stuff; - extra_stuff.erase(); - } os << "\n"; empty = false; } @@ -213,6 +215,13 @@ void Context::add_extra_stuff(std::string const & stuff) } +void Context::add_par_extra_stuff(string const & stuff) +{ + if (!contains(par_extra_stuff, stuff)) + par_extra_stuff += stuff; +} + + void Context::dump(ostream & os, string const & desc) const { os << "\n" << desc <<" ["; @@ -230,6 +239,8 @@ void Context::dump(ostream & os, string const & desc) const os << "new_layout_allowed "; if (!extra_stuff.empty()) os << "extrastuff=[" << extra_stuff << "] "; + if (!par_extra_stuff.empty()) + os << "parextrastuff=[" << par_extra_stuff << "] "; os << "textclass=" << textclass.name() << " layout=" << to_utf8(layout->name()) << " parent_layout=" << to_utf8(parent_layout->name()) << "] font=[" diff --git a/src/tex2lyx/Context.h b/src/tex2lyx/Context.h index e79e3ab64d..8c88e8f56e 100644 --- a/src/tex2lyx/Context.h +++ b/src/tex2lyx/Context.h @@ -111,13 +111,21 @@ public: /// Add extra stuff if not already there void add_extra_stuff(std::string const &); + /*! + * Add paragraph-level extra stuff if not already there. This + * will be reset at the next check_layout() + */ + void add_par_extra_stuff(std::string const &); + /// Do we need to output some \\begin_layout command before the /// next characters? bool need_layout; /// Do we need to output some \\end_layout command bool need_end_layout; - /// We may need to add something after this \\begin_layout command + /// We may need to add something after each \\begin_layout command std::string extra_stuff; + /// We may need to add something after this \\begin_layout command + std::string par_extra_stuff; /// If there has been an \\begin_deeper, we'll need a matching /// \\end_deeper bool need_end_deeper; @@ -149,6 +157,9 @@ public: TeXFont font; /// font attributes of normal text static TeXFont normalfont; + +private: + void begin_layout(std::ostream & os, Layout_ptr l); }; diff --git a/src/tex2lyx/text.cpp b/src/tex2lyx/text.cpp index 3123349958..9188be1f17 100644 --- a/src/tex2lyx/text.cpp +++ b/src/tex2lyx/text.cpp @@ -73,8 +73,8 @@ void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer, Context & context) { Context newcontext(context); - // Don't inherit the extra stuff - newcontext.extra_stuff.clear(); + // Don't inherit the paragraph-level extra stuff + newcontext.par_extra_stuff.clear(); parse_text(p, os, flags, outer, newcontext); // Make sure that we don't create invalid .lyx files context.need_layout = newcontext.need_layout; @@ -99,7 +99,7 @@ string parse_text_snippet(Parser & p, unsigned flags, const bool outer, newcontext.need_end_layout = false; newcontext.new_layout_allowed = false; // Avoid warning by Context::~Context() - newcontext.extra_stuff.clear(); + newcontext.par_extra_stuff.clear(); ostringstream os; parse_text_snippet(p, os, flags, outer, newcontext); return os.str(); @@ -855,8 +855,8 @@ void parse_environment(Parser & p, ostream & os, bool outer, parent_context.check_end_layout(os); switch (context.layout->latextype) { case LATEX_LIST_ENVIRONMENT: - context.extra_stuff = "\\labelwidthstring " - + p.verbatim_item() + '\n'; + context.add_par_extra_stuff("\\labelwidthstring " + + p.verbatim_item() + '\n'); p.skip_spaces(); break; case LATEX_BIB_ENVIRONMENT: @@ -1513,11 +1513,11 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, else if (t.cs() == "noindent") { p.skip_spaces(); - context.add_extra_stuff("\\noindent\n"); + context.add_par_extra_stuff("\\noindent\n"); } else if (t.cs() == "appendix") { - context.add_extra_stuff("\\start_of_appendix\n"); + context.add_par_extra_stuff("\\start_of_appendix\n"); // We need to start a new paragraph. Otherwise the // appendix in 'bla\appendix\chapter{' would start // too late.