+/*!
+ * Check wether \param command is a known command. If yes,
+ * handle the command with all arguments.
+ * \return true if the command was parsed, false otherwise.
+ */
+bool parse_command(string const & command, Parser & p, ostream & os,
+ bool outer, Context & context)
+{
+ if (known_commands.find(command) != known_commands.end()) {
+ vector<ArgumentType> const & template_arguments = known_commands[command];
+ string ert = command;
+ size_t no_arguments = template_arguments.size();
+ for (size_t i = 0; i < no_arguments; ++i) {
+ switch (template_arguments[i]) {
+ case required:
+ // This argument contains regular LaTeX
+ handle_ert(os, ert + '{', context);
+ parse_text(p, os, FLAG_ITEM, outer, context);
+ ert = "}";
+ break;
+ case verbatim:
+ // This argument may contain special characters
+ ert += '{' + p.verbatim_item() + '}';
+ break;
+ case optional:
+ ert += p.getOpt();
+ break;
+ }
+ }
+ handle_ert(os, ert, context);
+ return true;
+ }
+ return false;
+}
+
+
+/// Parses a minipage or parbox
+void parse_box(Parser & p, ostream & os, unsigned flags, bool outer,
+ Context & parent_context, bool use_parbox)
+{
+ string position;
+ string inner_pos;
+ string height_value = "0";
+ string height_unit = "pt";
+ string height_special = "none";
+ string latex_height;
+ if (p.next_token().asInput() == "[") {
+ position = p.getArg('[', ']');
+ if (position != "t" && position != "c" && position != "b") {
+ position = "c";
+ cerr << "invalid position for minipage/parbox" << endl;
+ }
+ if (p.next_token().asInput() == "[") {
+ latex_height = p.getArg('[', ']');
+ translate_box_len(latex_height, height_value, height_unit, height_special);
+
+ if (p.next_token().asInput() == "[") {
+ inner_pos = p.getArg('[', ']');
+ if (inner_pos != "c" && inner_pos != "t" &&
+ inner_pos != "b" && inner_pos != "s") {
+ inner_pos = position;
+ cerr << "invalid inner_pos for minipage/parbox"
+ << endl;
+ }
+ }
+ }
+ }
+ string width_value;
+ string width_unit;
+ string const latex_width = p.verbatim_item();
+ translate_len(latex_width, width_value, width_unit);
+ if (contains(width_unit, "\\") || contains(height_unit, "\\")) {
+ // LyX can't handle length variables
+ ostringstream ss;
+ if (use_parbox)
+ ss << "\\parbox";
+ else
+ ss << "\\begin{minipage}";
+ if (!position.empty())
+ ss << '[' << position << ']';
+ if (!latex_height.empty())
+ ss << '[' << latex_height << ']';
+ if (!inner_pos.empty())
+ ss << '[' << inner_pos << ']';
+ ss << "{" << latex_width << "}";
+ if (use_parbox)
+ ss << '{';
+ handle_ert(os, ss.str(), parent_context);
+ parent_context.new_paragraph(os);
+ parse_text_in_inset(p, os, flags, outer, parent_context);
+ if (use_parbox)
+ handle_ert(os, "}", parent_context);
+ else
+ handle_ert(os, "\\end{minipage}", parent_context);
+ } else {
+ // LyX does not like empty positions, so we have
+ // to set them to the LaTeX default values here.
+ if (position.empty())
+ position = "c";
+ if (inner_pos.empty())
+ inner_pos = position;
+ parent_context.check_layout(os);
+ begin_inset(os, "Box Frameless\n");
+ os << "position \"" << position << "\"\n";
+ os << "hor_pos \"c\"\n";
+ os << "has_inner_box 1\n";
+ os << "inner_pos \"" << inner_pos << "\"\n";
+ os << "use_parbox " << use_parbox << "\n";
+ os << "width \"" << width_value << width_unit << "\"\n";
+ os << "special \"none\"\n";
+ os << "height \"" << height_value << height_unit << "\"\n";
+ os << "height_special \"" << height_special << "\"\n";
+ os << "status open\n\n";
+ parse_text_in_inset(p, os, flags, outer, parent_context);
+ end_inset(os);
+#ifdef PRESERVE_LAYOUT
+ // lyx puts a % after the end of the minipage
+ if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
+ // new paragraph
+ //handle_comment(os, "%dummy", parent_context);
+ p.get_token();
+ p.skip_spaces();
+ parent_context.new_paragraph(os);
+ }
+ else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
+ //handle_comment(os, "%dummy", parent_context);
+ p.get_token();
+ p.skip_spaces();
+ // We add a protected space if something real follows
+ if (p.good() && p.next_token().cat() != catComment) {
+ os << "\\InsetSpace ~\n";
+ }
+ }
+#endif
+ }
+}
+
+
+void parse_environment(Parser & p, ostream & os, bool outer,
+ Context & parent_context)
+{
+ LyXLayout_ptr newlayout;
+ string const name = p.getArg('{', '}');
+ const bool is_starred = suffixIs(name, '*');
+ string const unstarred_name = rtrim(name, "*");
+ active_environments.push_back(name);
+ p.skip_spaces();
+
+ if (is_math_env(name)) {
+ parent_context.check_layout(os);
+ begin_inset(os, "Formula ");
+ os << "\\begin{" << name << "}";
+ parse_math(p, os, FLAG_END, MATH_MODE);
+ os << "\\end{" << name << "}";
+ end_inset(os);
+ }
+
+ else if (name == "tabular") {
+ parent_context.check_layout(os);
+ begin_inset(os, "Tabular ");
+ handle_tabular(p, os, parent_context);
+ end_inset(os);
+ }
+
+ else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
+ parent_context.check_layout(os);
+ begin_inset(os, "Float " + unstarred_name + "\n");
+ if (p.next_token().asInput() == "[") {
+ os << "placement " << p.getArg('[', ']') << '\n';
+ }
+ os << "wide " << tostr(is_starred)
+ << "\nstatus open\n\n";
+ parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
+ end_inset(os);
+ // We don't need really a new paragraph, but
+ // we must make sure that the next item gets a \begin_layout.
+ parent_context.new_paragraph(os);
+ }
+
+ else if (name == "minipage")
+ parse_box(p, os, FLAG_END, outer, parent_context, false);
+
+ // Alignment settings
+ else if (name == "center" || name == "flushleft" || name == "flushright" ||
+ name == "centering" || name == "raggedright" || name == "raggedleft") {
+ // We must begin a new paragraph if not already done
+ if (! parent_context.atParagraphStart()) {
+ parent_context.check_end_layout(os);
+ parent_context.new_paragraph(os);
+ }
+ if (name == "flushleft" || name == "raggedright")
+ parent_context.add_extra_stuff("\\align left ");
+ else if (name == "flushright" || name == "raggedleft")
+ parent_context.add_extra_stuff("\\align right ");
+ else
+ parent_context.add_extra_stuff("\\align center ");
+ parse_text(p, os, FLAG_END, outer, parent_context);
+ // Just in case the environment is empty ..
+ parent_context.extra_stuff.erase();
+ // We must begin a new paragraph to reset the alignment
+ parent_context.new_paragraph(os);
+ }
+
+ // The single '=' is meant here.
+ else if ((newlayout = findLayout(parent_context.textclass, name)).get() &&
+ newlayout->isEnvironment()) {
+ Context context(true, parent_context.textclass, newlayout,
+ parent_context.layout);
+ parent_context.check_end_layout(os);
+ switch (context.layout->latextype) {
+ case LATEX_LIST_ENVIRONMENT:
+ context.extra_stuff = "\\labelwidthstring "
+ + p.verbatim_item() + '\n';
+ p.skip_spaces();
+ break;
+ case LATEX_BIB_ENVIRONMENT:
+ p.verbatim_item(); // swallow next arg
+ p.skip_spaces();
+ break;
+ default:
+ break;
+ }
+ context.check_deeper(os);
+ parse_text(p, os, FLAG_END, outer, context);
+ context.check_end_layout(os);
+ context.check_end_deeper(os);
+ parent_context.new_paragraph(os);
+ }
+
+ else if (name == "appendix") {
+ // This is no good latex style, but it works and is used in some documents...
+ parent_context.check_end_layout(os);
+ Context context(true, parent_context.textclass, parent_context.layout,
+ parent_context.layout);
+ context.check_layout(os);
+ os << "\\start_of_appendix\n";
+ parse_text(p, os, FLAG_END, outer, context);
+ context.check_end_layout(os);
+ }
+
+ else if (name == "comment") {
+ parent_context.check_layout(os);
+ begin_inset(os, "Note Comment\n");
+ os << "status open\n";
+ parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
+ end_inset(os);
+ }
+
+ else if (name == "lyxgreyedout") {
+ parent_context.check_layout(os);
+ begin_inset(os, "Note Greyedout\n");
+ os << "status open\n";
+ parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
+ end_inset(os);
+ }
+
+ else if (name == "tabbing") {
+ // We need to remember that we have to handle '\=' specially
+ handle_ert(os, "\\begin{" + name + "}", parent_context);
+ parse_text_snippet(p, os, FLAG_END | FLAG_TABBING, outer, parent_context);
+ handle_ert(os, "\\end{" + name + "}", parent_context);
+ }
+
+ else {
+ handle_ert(os, "\\begin{" + name + "}", parent_context);
+ parse_text_snippet(p, os, FLAG_END, outer, parent_context);
+ handle_ert(os, "\\end{" + name + "}", parent_context);
+ }
+
+ active_environments.pop_back();
+ if (name != "math")
+ p.skip_spaces();
+}
+