From bb0432d74a7ca6b8e1c1f98d71405dc04eb6257a Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Thu, 8 Dec 2011 20:05:51 +0000 Subject: [PATCH] Beautify output of \usepckage statements (each statement is on its own line now) and move more packages to the new exclude mechanism. The remaining ones are not so easy. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@40442 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BiblioInfo.cpp | 4 ++-- src/BufferParams.cpp | 2 +- src/Encoding.cpp | 27 +++++++++++++++++++++++---- src/Encoding.h | 7 ++++--- src/mathed/MathParser.cpp | 12 ++++++++---- src/tex2lyx/Preamble.cpp | 34 ++++++++++------------------------ src/tex2lyx/math.cpp | 6 ++++++ src/tex2lyx/text.cpp | 26 ++++++++++++++++++++++---- 8 files changed, 76 insertions(+), 42 deletions(-) diff --git a/src/BiblioInfo.cpp b/src/BiblioInfo.cpp index 6b48f02636..8eede65d7b 100644 --- a/src/BiblioInfo.cpp +++ b/src/BiblioInfo.cpp @@ -175,8 +175,8 @@ docstring convertLaTeXCommands(docstring const & str) val.insert(2, from_ascii("{")); } docstring rem; - docstring const cnvtd = Encodings::fromLaTeXCommand(val, rem, - Encodings::TEXT_CMD); + docstring const cnvtd = Encodings::fromLaTeXCommand(val, + Encodings::TEXT_CMD, rem); if (!cnvtd.empty()) { // it did, so we'll take that bit and proceed with what's left ret += cnvtd; diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp index 5770d2544b..e709cc6167 100644 --- a/src/BufferParams.cpp +++ b/src/BufferParams.cpp @@ -1006,7 +1006,7 @@ void BufferParams::writeFile(ostream & os) const << "\n\\paperorientation " << string_orientation[orientation] << "\n\\suppress_date " << convert(suppress_date) << "\n\\justification " << convert(justification) - << "\n\\use_refstyle " << use_refstyle + << "\n\\use_refstyle " << use_refstyle << '\n'; if (isbackgroundcolor == true) os << "\\backgroundcolor " << lyx::X11hexname(backgroundcolor) << '\n'; diff --git a/src/Encoding.cpp b/src/Encoding.cpp index a8846527b4..c361418fc1 100644 --- a/src/Encoding.cpp +++ b/src/Encoding.cpp @@ -420,15 +420,26 @@ bool Encodings::latexMathChar(char_type c, bool mathmode, } -char_type Encodings::fromLaTeXCommand(docstring const & cmd, bool & combining) +char_type Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype, + bool & combining, set * req) { CharInfoMap::const_iterator const end = unicodesymbols.end(); CharInfoMap::const_iterator it = unicodesymbols.begin(); for (combining = false; it != end; ++it) { docstring const math = it->second.mathcommand; docstring const text = it->second.textcommand; - if (math == cmd || text == cmd) { + if ((cmdtype && MATH_CMD) && math == cmd) { combining = it->second.combining; + if (req && it->second.mathfeature && + !it->second.mathpreamble.empty()) + req->insert(it->second.mathpreamble); + return it->first; + } + if ((cmdtype & TEXT_CMD) && text == cmd) { + combining = it->second.combining; + if (req && it->second.textfeature && + !it->second.textpreamble.empty()) + req->insert(it->second.textpreamble); return it->first; } } @@ -436,8 +447,8 @@ char_type Encodings::fromLaTeXCommand(docstring const & cmd, bool & combining) } -docstring Encodings::fromLaTeXCommand(docstring const & cmd, docstring & rem, - int cmdtype) +docstring Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype, + docstring & rem, set * req) { bool const mathmode = cmdtype & MATH_CMD; bool const textmode = cmdtype & TEXT_CMD; @@ -521,6 +532,14 @@ docstring Encodings::fromLaTeXCommand(docstring const & cmd, docstring & rem, j = k - 1; i = j + 1; unicmd_size = cur_size; + if (req) { + if (math == tmp && it->second.mathfeature && + !it->second.mathpreamble.empty()) + req->insert(it->second.mathpreamble); + if (text == tmp && it->second.textfeature && + !it->second.textpreamble.empty()) + req->insert(it->second.textpreamble); + } } } if (unicmd_size) diff --git a/src/Encoding.h b/src/Encoding.h index bed6aa3810..effde73ffc 100644 --- a/src/Encoding.h +++ b/src/Encoding.h @@ -232,7 +232,8 @@ public: * Convert the LaTeX command in \p cmd to the corresponding unicode * point and set \p combining to true if it is a combining symbol */ - static char_type fromLaTeXCommand(docstring const & cmd, bool & combining); + static char_type fromLaTeXCommand(docstring const & cmd, int cmdtype, + bool & combining, std::set * req = 0); /// enum LatexCmd { /// @@ -248,8 +249,8 @@ public: * The \p cmdtype parameter can be used to limit recognized * commands to math or text mode commands only. */ - static docstring fromLaTeXCommand(docstring const & cmd, - docstring & rem, int cmdtype = MATH_CMD | TEXT_CMD); + static docstring fromLaTeXCommand(docstring const & cmd, int cmdtype, + docstring & rem, std::set * req = 0); /** * Add the preamble snippet needed for the output of \p c to * \p features. diff --git a/src/mathed/MathParser.cpp b/src/mathed/MathParser.cpp index be3df70d1e..0a0f31c1b7 100644 --- a/src/mathed/MathParser.cpp +++ b/src/mathed/MathParser.cpp @@ -1821,7 +1821,8 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags, } docstring rem; do { - cmd = Encodings::fromLaTeXCommand(cmd, rem); + cmd = Encodings::fromLaTeXCommand(cmd, + Encodings::MATH_CMD | Encodings::TEXT_CMD, rem); for (size_t i = 0; i < cmd.size(); ++i) cell->push_back(MathAtom(new InsetMathChar(cmd[i]))); if (rem.size()) { @@ -1913,8 +1914,9 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags, } } bool is_combining; - char_type c = - Encodings::fromLaTeXCommand(cmd, is_combining); + char_type c = Encodings::fromLaTeXCommand(cmd, + Encodings::MATH_CMD | Encodings::TEXT_CMD, + is_combining); if (is_combining) { if (cat == catLetter) cmd += '{'; @@ -1922,7 +1924,9 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags, ++num_tokens; if (cat == catLetter) cmd += '}'; - c = Encodings::fromLaTeXCommand(cmd, is_combining); + c = Encodings::fromLaTeXCommand(cmd, + Encodings::MATH_CMD | Encodings::TEXT_CMD, + is_combining); } if (c) { is_unicode_symbol = true; diff --git a/src/tex2lyx/Preamble.cpp b/src/tex2lyx/Preamble.cpp index fed348e8b5..f0e0f7ae9a 100644 --- a/src/tex2lyx/Preamble.cpp +++ b/src/tex2lyx/Preamble.cpp @@ -599,10 +599,10 @@ void Preamble::handle_package(Parser &p, string const & name, h_font_sc = "true"; } - if (name == "mathpazo") + else if (name == "mathpazo") h_font_roman = "palatino"; - if (name == "mathptmx") + else if (name == "mathptmx") h_font_roman = "times"; // sansserif fonts @@ -714,17 +714,8 @@ void Preamble::handle_package(Parser &p, string const & name, else if (name == "prettyref") ; // ignore this FIXME: Use the package separator mechanism instead - else if (name == "varioref") - ; // ignore this FIXME: Use the package separator mechanism instead - - else if (name == "verbatim") - ; // ignore this FIXME: Use the package separator mechanism instead - - else if (name == "textcomp") - ; // ignore this FIXME: Use the package separator mechanism instead - else if (name == "pdfpages") - ; // ignore this + ; // ignore this FIXME: Use the package separator mechanism instead else if (name == "lyxskak") { // ignore this and its options @@ -733,15 +724,16 @@ void Preamble::handle_package(Parser &p, string const & name, } else if (name == "array" || name == "booktabs" || name == "calc" || - name == "color" || name == "hhline" || name == "ifthen" || - name == "float" || name == "longtable" || name == "makeidx" || - name == "multirow" || name == "nomencl" || name == "setspace" || - name == "splitidx" || name == "subscript" || name == "ulem" || - name == "url") { + name == "color" || name == "float" || name == "hhline" || + name == "ifthen" || name == "longtable" || name == "makeidx" || + name == "multirow" || name == "nomencl" || name == "rotfloat" || + name == "splitidx" || name == "setspace" || name == "subscript" || + name == "textcomp" || name == "ulem" || name == "url" || + name == "varioref" || name == "verbatim" || name == "wrapfig") { if (!in_lyx_preamble) h_preamble << package_beg_sep << name << package_mid_sep << "\\usepackage{" - << name << '}' << package_end_sep; + << name << "}\n" << package_end_sep; } else if (name == "graphicx") @@ -750,12 +742,6 @@ void Preamble::handle_package(Parser &p, string const & name, else if (name == "geometry") handle_geometry(options); - else if (name == "rotfloat") - ; // ignore this FIXME: Use the package separator mechanism instead - - else if (name == "wrapfig") - ; // ignore this FIXME: Use the package separator mechanism instead - else if (name == "subfig") ; // ignore this FIXME: Use the package separator mechanism instead diff --git a/src/tex2lyx/math.cpp b/src/tex2lyx/math.cpp index d17d1d9017..1a8158b5a2 100644 --- a/src/tex2lyx/math.cpp +++ b/src/tex2lyx/math.cpp @@ -12,6 +12,7 @@ #include +#include "Preamble.h" #include "tex2lyx.h" #include @@ -231,6 +232,11 @@ void parse_math(Parser & p, ostream & os, unsigned flags, const mode_type mode) os << "\\\\"; } + else if (t.cs() == "vref" || t.cs() == "vpageref") { + os << t.asInput(); + preamble.registerAutomaticallyLoadedPackage("varioref"); + } + else os << t.asInput(); diff --git a/src/tex2lyx/text.cpp b/src/tex2lyx/text.cpp index ce7cf04a28..2b5d0acb0b 100644 --- a/src/tex2lyx/text.cpp +++ b/src/tex2lyx/text.cpp @@ -455,8 +455,11 @@ docstring convert_unicodesymbols(docstring s) } s = s.substr(i); docstring rem; - docstring parsed = encodings.fromLaTeXCommand(s, rem, - Encodings::TEXT_CMD); + set req; + docstring parsed = encodings.fromLaTeXCommand(s, + Encodings::TEXT_CMD, rem, &req); + for (set::const_iterator it = req.begin(); it != req.end(); it++) + preamble.registerAutomaticallyLoadedPackage(*it); os << parsed; s = rem; if (s.empty() || s[0] != '\\') @@ -1262,6 +1265,7 @@ void parse_environment(Parser & p, ostream & os, bool outer, // we must make sure that the next item gets a \begin_layout. parent_context.new_paragraph(os); p.skip_spaces(); + preamble.registerAutomaticallyLoadedPackage("rotfloat"); } else if (name == "wrapfigure" || name == "wraptable") { @@ -1294,6 +1298,7 @@ void parse_environment(Parser & p, ostream & os, bool outer, // we must make sure that the next item gets a \begin_layout. parent_context.new_paragraph(os); p.skip_spaces(); + preamble.registerAutomaticallyLoadedPackage("wrapfig"); } else if (name == "minipage") { @@ -1333,6 +1338,7 @@ void parse_environment(Parser & p, ostream & os, bool outer, end_inset(os); p.skip_spaces(); skip_braces(p); // eat {} that might by set by LyX behind comments + preamble.registerAutomaticallyLoadedPackage("verbatim"); } else if (name == "lyxgreyedout") { @@ -3019,6 +3025,9 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, << convert_command_inset_arg(p.verbatim_item()) << "\"\n"; end_inset(os); + if (t.cs() == "vref" || t.cs() == "vpageref") + preamble.registerAutomaticallyLoadedPackage("varioref"); + } else { // LyX does not support optional arguments of ref commands handle_ert(os, t.asInput() + '[' + opt + "]{" + @@ -3435,13 +3444,17 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, string command = t.asInput() + "{" + trimSpaceAndEol(p.verbatim_item()) + "}"; - docstring s = encodings.fromLaTeXCommand(from_utf8(command), rem); + set req; + docstring s = encodings.fromLaTeXCommand(from_utf8(command), + Encodings::TEXT_CMD | Encodings::MATH_CMD, rem, &req); if (!s.empty()) { if (!rem.empty()) cerr << "When parsing " << command << ", result is " << to_utf8(s) << "+" << to_utf8(rem) << endl; os << to_utf8(s); + for (set::const_iterator it = req.begin(); it != req.end(); it++) + preamble.registerAutomaticallyLoadedPackage(*it); } else // we did not find a non-ert version handle_ert(os, command, context); @@ -3575,6 +3588,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, begin_command_inset(os, "include", name); os << "preview false\n" "filename \"" << outname << "\"\n"; + if (t.cs() == "verbatiminput") + preamble.registerAutomaticallyLoadedPackage("verbatim"); } end_inset(os); } @@ -4029,8 +4044,9 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, // Only use text mode commands, since we are in text mode here, // and math commands may be invalid (bug 6797) docstring rem; + set req; docstring s = encodings.fromLaTeXCommand(from_utf8(t.asInput()), - rem, Encodings::TEXT_CMD); + Encodings::TEXT_CMD, rem, &req); if (!s.empty()) { if (!rem.empty()) cerr << "When parsing " << t.cs() @@ -4039,6 +4055,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, context.check_layout(os); os << to_utf8(s); skip_spaces_braces(p); + for (set::const_iterator it = req.begin(); it != req.end(); it++) + preamble.registerAutomaticallyLoadedPackage(*it); } //cerr << "#: " << t << " mode: " << mode << endl; // heuristic: read up to next non-nested space -- 2.39.5