+ // 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);
+}
+
+
+/*!
+ * Output a space if necessary.
+ * This function gets called for every whitespace token.
+ *
+ * We have three cases here:
+ * 1. A space must be suppressed. Example: The lyxcode case below
+ * 2. A space may be suppressed. Example: Spaces before "\par"
+ * 3. A space must not be suppressed. Example: A space between two words
+ *
+ * We currently handle only 1. and 3 and from 2. only the case of
+ * spaces before newlines as a side effect.
+ *
+ * 2. could be used to suppress as many spaces as possible. This has two effects:
+ * - Reimporting LyX generated LaTeX files changes almost no whitespace
+ * - Superflous whitespace from non LyX generated LaTeX files is removed.
+ * The drawback is that the logic inside the function becomes
+ * complicated, and that is the reason why it is not implemented.
+ */
+void check_space(Parser const & p, ostream & os, Context & context)
+{
+ Token const next = p.next_token();
+ Token const curr = p.curr_token();
+ // A space before a single newline and vice versa must be ignored
+ // LyX emits a newline before \end{lyxcode}.
+ // This newline must be ignored,
+ // otherwise LyX will add an additional protected space.
+ if (next.cat() == catSpace ||
+ next.cat() == catNewline ||
+ (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
+ return;
+ }
+ context.check_layout(os);
+ os << ' ';
+}
+
+
+/*!
+ * 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
+ }