3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jean-Marc Lasgouttes
9 * Full author contact details are available in file CREDITS.
18 #include "FloatList.h"
19 #include "lengthcommon.h"
20 #include "support/lstrings.h"
21 #include "support/convert.h"
22 #include "support/filetools.h"
24 #include <boost/filesystem/operations.hpp>
25 #include <boost/tuple/tuple.hpp>
32 using lyx::support::MakeAbsPath;
33 using lyx::support::rtrim;
34 using lyx::support::suffixIs;
35 using lyx::support::contains;
36 using lyx::support::subst;
43 using std::ostringstream;
44 using std::istringstream;
48 namespace fs = boost::filesystem;
51 /// thin wrapper around parse_text using a string
52 string parse_text(Parser & p, unsigned flags, const bool outer,
56 parse_text(p, os, flags, outer, context);
61 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
64 Context newcontext(true, context.textclass);
65 newcontext.font = context.font;
66 parse_text(p, os, flags, outer, newcontext);
67 newcontext.check_end_layout(os);
71 /// parses a paragraph snippet, useful for example for \emph{...}
72 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
75 Context newcontext(false, context.textclass);
76 newcontext.font = context.font;
77 parse_text(p, os, flags, outer, newcontext);
78 // should not be needed
79 newcontext.check_end_layout(os);
85 char const * const known_latex_commands[] = { "ref", "cite", "label", "index",
86 "printindex", "pageref", "url", "vref", "vpageref", "prettyref", "eqref", 0 };
90 * We can't put these into known_latex_commands because the argument order
91 * is reversed in lyx if there are 2 arguments.
92 * The starred forms are also known.
94 char const * const known_natbib_commands[] = { "cite", "citet", "citep",
95 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
96 "citefullauthor", "Citet", "Citep", "Citealt", "Citealp", "Citeauthor", 0 };
100 * We can't put these into known_latex_commands because the argument order
101 * is reversed in lyx if there are 2 arguments.
102 * No starred form other than "cite*" known.
104 char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
105 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar", "fullcite",
106 // jurabib commands not (yet) supported by LyX:
107 // "footcite", "footcitet", "footcitep", "footcitealt", "footcitealp",
108 // "footciteauthor", "footciteyear", "footciteyearpar",
109 "citefield", "citetitle", "cite*", 0 };
111 /// LaTeX names for quotes
112 char const * const known_quotes[] = { "glqq", "grqq", "quotedblbase",
113 "textquotedblleft", "quotesinglbase", "guilsinglleft", "guilsinglright", 0};
115 /// the same as known_quotes with .lyx names
116 char const * const known_coded_quotes[] = { "gld", "grd", "gld",
117 "grd", "gls", "fls", "frd", 0};
119 /// LaTeX names for font sizes
120 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
121 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
123 /// the same as known_sizes with .lyx names
124 char const * const known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
125 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
127 /// LaTeX 2.09 names for font families
128 char const * const known_old_font_families[] = { "rm", "sf", "tt", 0};
130 /// LaTeX names for font families
131 char const * const known_font_families[] = { "rmfamily", "sffamily",
134 /// the same as known_old_font_families and known_font_families with .lyx names
135 char const * const known_coded_font_families[] = { "roman", "sans",
138 /// LaTeX 2.09 names for font series
139 char const * const known_old_font_series[] = { "bf", 0};
141 /// LaTeX names for font series
142 char const * const known_font_series[] = { "bfseries", "mdseries", 0};
144 /// the same as known_old_font_series and known_font_series with .lyx names
145 char const * const known_coded_font_series[] = { "bold", "medium", 0};
147 /// LaTeX 2.09 names for font shapes
148 char const * const known_old_font_shapes[] = { "it", "sl", "sc", 0};
150 /// LaTeX names for font shapes
151 char const * const known_font_shapes[] = { "itshape", "slshape", "scshape",
154 /// the same as known_old_font_shapes and known_font_shapes with .lyx names
155 char const * const known_coded_font_shapes[] = { "italic", "slanted",
156 "smallcaps", "up", 0};
159 * Graphics file extensions known by the dvips driver of the graphics package.
160 * These extensions are used to complete the filename of an included
161 * graphics file if it does not contain an extension.
162 * The order must be the same that latex uses to find a file, because we
163 * will use the first extension that matches.
164 * This is only an approximation for the common cases. If we would want to
165 * do it right in all cases, we would need to know which graphics driver is
166 * used and know the extensions of every driver of the graphics package.
168 char const * const known_dvips_graphics_formats[] = {"eps", "ps", "eps.gz",
169 "ps.gz", "eps.Z", "ps.Z", 0};
172 * Graphics file extensions known by the pdftex driver of the graphics package.
173 * \see known_dvips_graphics_formats
175 char const * const known_pdftex_graphics_formats[] = {"png", "pdf", "jpg",
179 /// splits "x=z, y=b" into a map
180 map<string, string> split_map(string const & s)
182 map<string, string> res;
185 for (size_t i = 0; i < v.size(); ++i) {
186 size_t const pos = v[i].find('=');
187 string const index = v[i].substr(0, pos);
188 string const value = v[i].substr(pos + 1, string::npos);
189 res[trim(index)] = trim(value);
196 * Split a LaTeX length into value and unit.
197 * The latter can be a real unit like "pt", or a latex length variable
198 * like "\textwidth". The unit may contain additional stuff like glue
199 * lengths, but we don't care, because such lengths are ERT anyway.
200 * \returns true if \p value and \p unit are valid.
202 bool splitLatexLength(string const & len, string & value, string & unit)
206 const string::size_type i = len.find_first_not_of(" -+0123456789.,");
207 //'4,5' is a valid LaTeX length number. Change it to '4.5'
208 string const length = subst(len, ',', '.');
209 if (i == string::npos)
212 if (len[0] == '\\') {
213 // We had something like \textwidth without a factor
219 value = trim(string(length, 0, i));
223 // 'cM' is a valid LaTeX length unit. Change it to 'cm'
224 if (contains(len, '\\'))
225 unit = trim(string(len, i));
227 unit = lyx::support::lowercase(trim(string(len, i)));
232 /// A simple function to translate a latex length to something lyx can
233 /// understand. Not perfect, but rather best-effort.
234 bool translate_len(string const & length, string & valstring, string & unit)
236 if (!splitLatexLength(length, valstring, unit))
238 // LyX uses percent values
240 istringstream iss(valstring);
245 string const percentval = oss.str();
247 if (unit.empty() || unit[0] != '\\')
249 string::size_type const i = unit.find(' ');
250 string const endlen = (i == string::npos) ? string() : string(unit, i);
251 if (unit == "\\textwidth") {
252 valstring = percentval;
253 unit = "text%" + endlen;
254 } else if (unit == "\\columnwidth") {
255 valstring = percentval;
256 unit = "col%" + endlen;
257 } else if (unit == "\\paperwidth") {
258 valstring = percentval;
259 unit = "page%" + endlen;
260 } else if (unit == "\\linewidth") {
261 valstring = percentval;
262 unit = "line%" + endlen;
263 } else if (unit == "\\paperheight") {
264 valstring = percentval;
265 unit = "pheight%" + endlen;
266 } else if (unit == "\\textheight") {
267 valstring = percentval;
268 unit = "theight%" + endlen;
274 string translate_len(string const & length)
278 if (translate_len(length, value, unit))
280 // If the input is invalid, return what we have.
286 * Translates a LaTeX length into \p value, \p unit and
287 * \p special parts suitable for a box inset.
288 * The difference from translate_len() is that a box inset knows about
289 * some special "units" that are stored in \p special.
291 void translate_box_len(string const & length, string & value, string & unit, string & special)
293 if (translate_len(length, value, unit)) {
294 if (unit == "\\height" || unit == "\\depth" ||
295 unit == "\\totalheight" || unit == "\\width") {
296 special = unit.substr(1);
297 // The unit is not used, but LyX requires a dummy setting
310 * Find a file with basename \p name in path \p path and an extension
313 string find_file(string const & name, string const & path,
314 char const * const * extensions)
316 for (char const * const * what = extensions; *what; ++what) {
317 // We don't use ChangeExtension() because it does the wrong
318 // thing if name contains a dot.
319 string const trial = name + '.' + (*what);
320 if (fs::exists(MakeAbsPath(trial, path)))
327 void begin_inset(ostream & os, string const & name)
329 os << "\n\\begin_inset " << name;
333 void end_inset(ostream & os)
335 os << "\n\\end_inset\n\n";
339 void skip_braces(Parser & p)
341 if (p.next_token().cat() != catBegin)
344 if (p.next_token().cat() == catEnd) {
352 void handle_ert(ostream & os, string const & s, Context & context, bool check_layout = true)
355 // We must have a valid layout before outputting the ERT inset.
356 context.check_layout(os);
358 Context newcontext(true, context.textclass);
359 begin_inset(os, "ERT");
360 os << "\nstatus collapsed\n";
361 newcontext.check_layout(os);
362 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
364 os << "\n\\backslash\n";
365 else if (*it == '\n') {
366 newcontext.new_paragraph(os);
367 newcontext.check_layout(os);
371 newcontext.check_end_layout(os);
376 void handle_comment(ostream & os, string const & s, Context & context)
378 // TODO: Handle this better
379 Context newcontext(true, context.textclass);
380 begin_inset(os, "ERT");
381 os << "\nstatus collapsed\n";
382 newcontext.check_layout(os);
383 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
385 os << "\n\\backslash\n";
389 // make sure that our comment is the last thing on the line
390 newcontext.new_paragraph(os);
391 newcontext.check_layout(os);
392 newcontext.check_end_layout(os);
397 class isLayout : public std::unary_function<LyXLayout_ptr, bool> {
399 isLayout(string const name) : name_(name) {}
400 bool operator()(LyXLayout_ptr const & ptr) const {
401 return ptr->latexname() == name_;
408 LyXLayout_ptr findLayout(LyXTextClass const & textclass,
411 LyXTextClass::const_iterator beg = textclass.begin();
412 LyXTextClass::const_iterator end = textclass.end();
414 LyXTextClass::const_iterator
415 it = std::find_if(beg, end, isLayout(name));
417 return (it == end) ? LyXLayout_ptr() : *it;
421 void eat_whitespace(Parser &, ostream &, Context &, bool);
424 void output_command_layout(ostream & os, Parser & p, bool outer,
425 Context & parent_context,
426 LyXLayout_ptr newlayout)
428 parent_context.check_end_layout(os);
429 Context context(true, parent_context.textclass, newlayout,
430 parent_context.layout, parent_context.font);
431 if (parent_context.deeper_paragraph) {
432 // We are beginning a nested environment after a
433 // deeper paragraph inside the outer list environment.
434 // Therefore we don't need to output a "begin deeper".
435 context.need_end_deeper = true;
437 context.check_deeper(os);
438 context.check_layout(os);
439 if (context.layout->optionalargs > 0) {
440 eat_whitespace(p, os, context, false);
441 if (p.next_token().character() == '[') {
442 p.get_token(); // eat '['
443 begin_inset(os, "OptArg\n");
444 os << "status collapsed\n\n";
445 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
447 eat_whitespace(p, os, context, false);
450 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
451 context.check_end_layout(os);
452 if (parent_context.deeper_paragraph) {
453 // We must suppress the "end deeper" because we
454 // suppressed the "begin deeper" above.
455 context.need_end_deeper = false;
457 context.check_end_deeper(os);
458 // We don't need really a new paragraph, but
459 // we must make sure that the next item gets a \begin_layout.
460 parent_context.new_paragraph(os);
465 * Output a space if necessary.
466 * This function gets called for every whitespace token.
468 * We have three cases here:
469 * 1. A space must be suppressed. Example: The lyxcode case below
470 * 2. A space may be suppressed. Example: Spaces before "\par"
471 * 3. A space must not be suppressed. Example: A space between two words
473 * We currently handle only 1. and 3 and from 2. only the case of
474 * spaces before newlines as a side effect.
476 * 2. could be used to suppress as many spaces as possible. This has two effects:
477 * - Reimporting LyX generated LaTeX files changes almost no whitespace
478 * - Superflous whitespace from non LyX generated LaTeX files is removed.
479 * The drawback is that the logic inside the function becomes
480 * complicated, and that is the reason why it is not implemented.
482 void check_space(Parser const & p, ostream & os, Context & context)
484 Token const next = p.next_token();
485 Token const curr = p.curr_token();
486 // A space before a single newline and vice versa must be ignored
487 // LyX emits a newline before \end{lyxcode}.
488 // This newline must be ignored,
489 // otherwise LyX will add an additional protected space.
490 if (next.cat() == catSpace ||
491 next.cat() == catNewline ||
492 (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
495 context.check_layout(os);
501 * Check whether \p command is a known command. If yes,
502 * handle the command with all arguments.
503 * \return true if the command was parsed, false otherwise.
505 bool parse_command(string const & command, Parser & p, ostream & os,
506 bool outer, Context & context)
508 if (known_commands.find(command) != known_commands.end()) {
509 vector<ArgumentType> const & template_arguments = known_commands[command];
510 string ert = command;
511 size_t no_arguments = template_arguments.size();
512 for (size_t i = 0; i < no_arguments; ++i) {
513 switch (template_arguments[i]) {
515 // This argument contains regular LaTeX
516 handle_ert(os, ert + '{', context);
517 parse_text(p, os, FLAG_ITEM, outer, context);
521 // This argument may contain special characters
522 ert += '{' + p.verbatim_item() + '}';
529 handle_ert(os, ert, context);
536 /// Parses a minipage or parbox
537 void parse_box(Parser & p, ostream & os, unsigned flags, bool outer,
538 Context & parent_context, bool use_parbox)
542 string height_value = "0";
543 string height_unit = "pt";
544 string height_special = "none";
546 if (p.next_token().asInput() == "[") {
547 position = p.getArg('[', ']');
548 if (position != "t" && position != "c" && position != "b") {
550 cerr << "invalid position for minipage/parbox" << endl;
552 if (p.next_token().asInput() == "[") {
553 latex_height = p.getArg('[', ']');
554 translate_box_len(latex_height, height_value, height_unit, height_special);
556 if (p.next_token().asInput() == "[") {
557 inner_pos = p.getArg('[', ']');
558 if (inner_pos != "c" && inner_pos != "t" &&
559 inner_pos != "b" && inner_pos != "s") {
560 inner_pos = position;
561 cerr << "invalid inner_pos for minipage/parbox"
569 string const latex_width = p.verbatim_item();
570 translate_len(latex_width, width_value, width_unit);
571 if (contains(width_unit, '\\') || contains(height_unit, '\\')) {
572 // LyX can't handle length variables
577 ss << "\\begin{minipage}";
578 if (!position.empty())
579 ss << '[' << position << ']';
580 if (!latex_height.empty())
581 ss << '[' << latex_height << ']';
582 if (!inner_pos.empty())
583 ss << '[' << inner_pos << ']';
584 ss << "{" << latex_width << "}";
587 handle_ert(os, ss.str(), parent_context);
588 parent_context.new_paragraph(os);
589 parse_text_in_inset(p, os, flags, outer, parent_context);
591 handle_ert(os, "}", parent_context);
593 handle_ert(os, "\\end{minipage}", parent_context);
595 // LyX does not like empty positions, so we have
596 // to set them to the LaTeX default values here.
597 if (position.empty())
599 if (inner_pos.empty())
600 inner_pos = position;
601 parent_context.check_layout(os);
602 begin_inset(os, "Box Frameless\n");
603 os << "position \"" << position << "\"\n";
604 os << "hor_pos \"c\"\n";
605 os << "has_inner_box 1\n";
606 os << "inner_pos \"" << inner_pos << "\"\n";
607 os << "use_parbox " << use_parbox << "\n";
608 os << "width \"" << width_value << width_unit << "\"\n";
609 os << "special \"none\"\n";
610 os << "height \"" << height_value << height_unit << "\"\n";
611 os << "height_special \"" << height_special << "\"\n";
612 os << "status open\n\n";
613 parse_text_in_inset(p, os, flags, outer, parent_context);
615 #ifdef PRESERVE_LAYOUT
616 // lyx puts a % after the end of the minipage
617 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
619 //handle_comment(os, "%dummy", parent_context);
622 parent_context.new_paragraph(os);
624 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
625 //handle_comment(os, "%dummy", parent_context);
628 // We add a protected space if something real follows
629 if (p.good() && p.next_token().cat() != catComment) {
630 os << "\\InsetSpace ~\n";
638 void parse_environment(Parser & p, ostream & os, bool outer,
639 Context & parent_context)
641 LyXLayout_ptr newlayout;
642 string const name = p.getArg('{', '}');
643 const bool is_starred = suffixIs(name, '*');
644 string const unstarred_name = rtrim(name, "*");
645 eat_whitespace(p, os, parent_context, false);
646 active_environments.push_back(name);
648 if (is_math_env(name)) {
649 parent_context.check_layout(os);
650 begin_inset(os, "Formula ");
651 os << "\\begin{" << name << "}";
652 parse_math(p, os, FLAG_END, MATH_MODE);
653 os << "\\end{" << name << "}";
657 else if (name == "tabular" || name == "longtable") {
658 parent_context.check_layout(os);
659 begin_inset(os, "Tabular ");
660 handle_tabular(p, os, name == "longtable", parent_context);
664 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
665 parent_context.check_layout(os);
666 begin_inset(os, "Float " + unstarred_name + "\n");
667 if (p.next_token().asInput() == "[") {
668 os << "placement " << p.getArg('[', ']') << '\n';
670 os << "wide " << convert<string>(is_starred)
671 << "\nsideways false"
672 << "\nstatus open\n\n";
673 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
675 // We don't need really a new paragraph, but
676 // we must make sure that the next item gets a \begin_layout.
677 parent_context.new_paragraph(os);
680 else if (name == "minipage")
681 parse_box(p, os, FLAG_END, outer, parent_context, false);
683 // Alignment settings
684 else if (name == "center" || name == "flushleft" || name == "flushright" ||
685 name == "centering" || name == "raggedright" || name == "raggedleft") {
686 // We must begin a new paragraph if not already done
687 if (! parent_context.atParagraphStart()) {
688 parent_context.check_end_layout(os);
689 parent_context.new_paragraph(os);
691 if (name == "flushleft" || name == "raggedright")
692 parent_context.add_extra_stuff("\\align left\n");
693 else if (name == "flushright" || name == "raggedleft")
694 parent_context.add_extra_stuff("\\align right\n");
696 parent_context.add_extra_stuff("\\align center\n");
697 parse_text(p, os, FLAG_END, outer, parent_context);
698 // Just in case the environment is empty ..
699 parent_context.extra_stuff.erase();
700 // We must begin a new paragraph to reset the alignment
701 parent_context.new_paragraph(os);
704 // The single '=' is meant here.
705 else if ((newlayout = findLayout(parent_context.textclass, name)).get() &&
706 newlayout->isEnvironment()) {
707 Context context(true, parent_context.textclass, newlayout,
708 parent_context.layout, parent_context.font);
709 if (parent_context.deeper_paragraph) {
710 // We are beginning a nested environment after a
711 // deeper paragraph inside the outer list environment.
712 // Therefore we don't need to output a "begin deeper".
713 context.need_end_deeper = true;
715 parent_context.check_end_layout(os);
716 switch (context.layout->latextype) {
717 case LATEX_LIST_ENVIRONMENT:
718 context.extra_stuff = "\\labelwidthstring "
719 + p.verbatim_item() + '\n';
722 case LATEX_BIB_ENVIRONMENT:
723 p.verbatim_item(); // swallow next arg
729 context.check_deeper(os);
730 parse_text(p, os, FLAG_END, outer, context);
731 context.check_end_layout(os);
732 if (parent_context.deeper_paragraph) {
733 // We must suppress the "end deeper" because we
734 // suppressed the "begin deeper" above.
735 context.need_end_deeper = false;
737 context.check_end_deeper(os);
738 parent_context.new_paragraph(os);
741 else if (name == "appendix") {
742 // This is no good latex style, but it works and is used in some documents...
743 parent_context.check_end_layout(os);
744 Context context(true, parent_context.textclass, parent_context.layout,
745 parent_context.layout, parent_context.font);
746 context.check_layout(os);
747 os << "\\start_of_appendix\n";
748 parse_text(p, os, FLAG_END, outer, context);
749 context.check_end_layout(os);
752 else if (name == "comment") {
753 parent_context.check_layout(os);
754 begin_inset(os, "Note Comment\n");
755 os << "status open\n";
756 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
760 else if (name == "lyxgreyedout") {
761 parent_context.check_layout(os);
762 begin_inset(os, "Note Greyedout\n");
763 os << "status open\n";
764 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
768 else if (name == "tabbing") {
769 // We need to remember that we have to handle '\=' specially
770 handle_ert(os, "\\begin{" + name + "}", parent_context);
771 parse_text_snippet(p, os, FLAG_END | FLAG_TABBING, outer, parent_context);
772 handle_ert(os, "\\end{" + name + "}", parent_context);
776 handle_ert(os, "\\begin{" + name + "}", parent_context);
777 parse_text_snippet(p, os, FLAG_END, outer, parent_context);
778 handle_ert(os, "\\end{" + name + "}", parent_context);
781 active_environments.pop_back();
786 /// parses a comment and outputs it to \p os.
787 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
789 BOOST_ASSERT(t.cat() == catComment);
790 context.check_layout(os);
791 if (!t.cs().empty()) {
792 handle_comment(os, '%' + t.cs(), context);
793 if (p.next_token().cat() == catNewline) {
794 // A newline after a comment line starts a new
796 if(!context.atParagraphStart()) {
797 // Only start a new paragraph if not already
798 // done (we might get called recursively)
799 context.new_paragraph(os);
801 eat_whitespace(p, os, context, true);
811 * Reads spaces and comments until the first non-space, non-comment token.
812 * New paragraphs (double newlines or \\par) are handled like simple spaces
813 * if \p eatParagraph is true.
814 * Spaces are skipped, but comments are written to \p os.
816 void eat_whitespace(Parser & p, ostream & os, Context & context,
820 Token const & t = p.get_token();
821 if (t.cat() == catComment)
822 parse_comment(p, os, t, context);
823 else if ((! eatParagraph && p.isParagraph()) ||
824 (t.cat() != catSpace && t.cat() != catNewline)) {
833 * Set a font attribute, parse text and reset the font attribute.
834 * \param attribute Attribute name (e.g. \\family, \\shape etc.)
835 * \param currentvalue Current value of the attribute. Is set to the new
836 * value during parsing.
837 * \param newvalue New value of the attribute
839 void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
840 Context & context, string const & attribute,
841 string & currentvalue, string const & newvalue)
843 context.check_layout(os);
844 string oldvalue = currentvalue;
845 currentvalue = newvalue;
846 os << '\n' << attribute << ' ' << newvalue << "\n";
847 parse_text_snippet(p, os, flags, outer, context);
848 currentvalue = oldvalue;
849 os << '\n' << attribute << ' ' << oldvalue << "\n";
853 /// get the arguments of a natbib or jurabib citation command
854 std::pair<string, string> getCiteArguments(Parser & p, bool natbibOrder)
856 // We need to distinguish "" and "[]", so we can't use p.getOpt().
858 // text before the citation
860 // text after the citation
861 string after = p.getFullOpt();
863 if (!after.empty()) {
864 before = p.getFullOpt();
865 if (natbibOrder && !before.empty())
866 std::swap(before, after);
868 return std::make_pair(before, after);
871 } // anonymous namespace
874 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
877 LyXLayout_ptr newlayout;
878 // Store the latest bibliographystyle (needed for bibtex inset)
879 string bibliographystyle;
880 bool const use_natbib = used_packages.find("natbib") != used_packages.end();
881 bool const use_jurabib = used_packages.find("jurabib") != used_packages.end();
883 Token const & t = p.get_token();
886 cerr << "t: " << t << " flags: " << flags << "\n";
889 if (flags & FLAG_ITEM) {
890 if (t.cat() == catSpace)
894 if (t.cat() == catBegin) {
895 // skip the brace and collect everything to the next matching
897 flags |= FLAG_BRACE_LAST;
901 // handle only this single token, leave the loop if done
905 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
911 if (t.cat() == catMath) {
912 // we are inside some text mode thingy, so opening new math is allowed
913 context.check_layout(os);
914 begin_inset(os, "Formula ");
915 Token const & n = p.get_token();
916 if (n.cat() == catMath && outer) {
917 // TeX's $$...$$ syntax for displayed math
919 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
921 p.get_token(); // skip the second '$' token
923 // simple $...$ stuff
926 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
932 else if (t.cat() == catSuper || t.cat() == catSub)
933 cerr << "catcode " << t << " illegal in text mode\n";
935 // Basic support for english quotes. This should be
936 // extended to other quotes, but is not so easy (a
937 // left english quote is the same as a right german
939 else if (t.asInput() == "`"
940 && p.next_token().asInput() == "`") {
941 context.check_layout(os);
942 begin_inset(os, "Quotes ");
948 else if (t.asInput() == "'"
949 && p.next_token().asInput() == "'") {
950 context.check_layout(os);
951 begin_inset(os, "Quotes ");
958 else if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph()))
959 check_space(p, os, context);
961 else if (t.cat() == catLetter ||
962 t.cat() == catOther ||
963 t.cat() == catAlign ||
964 t.cat() == catParameter) {
965 context.check_layout(os);
969 else if (p.isParagraph()) {
970 context.new_paragraph(os);
971 eat_whitespace(p, os, context, true);
974 else if (t.cat() == catActive) {
975 context.check_layout(os);
976 if (t.character() == '~') {
977 if (context.layout->free_spacing)
980 os << "\\InsetSpace ~\n";
985 else if (t.cat() == catBegin) {
986 context.check_layout(os);
987 // special handling of font attribute changes
988 Token const prev = p.prev_token();
989 Token const next = p.next_token();
990 Font const oldFont = context.font;
991 string const s = parse_text(p, FLAG_BRACE_LAST, outer,
993 context.font = oldFont;
994 if (s.empty() && (p.next_token().character() == '`' ||
995 (prev.character() == '-' &&
996 p.next_token().character() == '-')))
997 ; // ignore it in {}`` or -{}-
998 else if (s == "[" || s == "]" || s == "*")
1000 else if (is_known(next.cs(), known_sizes)) {
1001 // s will change the size, so we must reset
1004 if (!context.atParagraphStart())
1006 << context.font.size << "\n";
1007 } else if (is_known(next.cs(), known_font_families)) {
1008 // s will change the font family, so we must
1011 if (!context.atParagraphStart())
1013 << context.font.family << "\n";
1014 } else if (is_known(next.cs(), known_font_series)) {
1015 // s will change the font series, so we must
1018 if (!context.atParagraphStart())
1020 << context.font.series << "\n";
1021 } else if (is_known(next.cs(), known_font_shapes)) {
1022 // s will change the font shape, so we must
1025 if (!context.atParagraphStart())
1027 << context.font.shape << "\n";
1028 } else if (is_known(next.cs(), known_old_font_families) ||
1029 is_known(next.cs(), known_old_font_series) ||
1030 is_known(next.cs(), known_old_font_shapes)) {
1031 // s will change the font family, series
1032 // and shape, so we must reset it here
1034 if (!context.atParagraphStart())
1036 << context.font.family
1038 << context.font.series
1040 << context.font.shape << "\n";
1042 handle_ert(os, "{", context, false);
1043 // s will end the current layout and begin a
1044 // new one if necessary
1046 handle_ert(os, "}", context);
1050 else if (t.cat() == catEnd) {
1051 if (flags & FLAG_BRACE_LAST) {
1054 cerr << "stray '}' in text\n";
1055 handle_ert(os, "}", context);
1058 else if (t.cat() == catComment)
1059 parse_comment(p, os, t, context);
1062 // control sequences
1065 else if (t.cs() == "(") {
1066 context.check_layout(os);
1067 begin_inset(os, "Formula");
1069 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
1074 else if (t.cs() == "[") {
1075 context.check_layout(os);
1076 begin_inset(os, "Formula");
1078 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
1083 else if (t.cs() == "begin")
1084 parse_environment(p, os, outer, context);
1086 else if (t.cs() == "end") {
1087 if (flags & FLAG_END) {
1088 // eat environment name
1089 string const name = p.getArg('{', '}');
1090 if (name != active_environment())
1091 cerr << "\\end{" + name + "} does not match \\begin{"
1092 + active_environment() + "}\n";
1095 p.error("found 'end' unexpectedly");
1098 else if (t.cs() == "item") {
1101 bool optarg = false;
1102 if (p.next_token().character() == '[') {
1103 p.get_token(); // eat '['
1104 Context newcontext(false, context.textclass);
1105 newcontext.font = context.font;
1106 s = parse_text(p, FLAG_BRACK_LAST, outer, newcontext);
1110 context.check_layout(os);
1112 if (context.layout->labeltype != LABEL_MANUAL) {
1113 // lyx does not support \item[\mybullet]
1114 // in itemize environments
1115 handle_ert(os, "[", context);
1117 handle_ert(os, "]", context);
1118 } else if (!s.empty()) {
1119 // The space is needed to separate the
1120 // item from the rest of the sentence.
1122 eat_whitespace(p, os, context, false);
1127 else if (t.cs() == "bibitem") {
1129 context.check_layout(os);
1132 os << '{' << p.verbatim_item() << '}' << "\n";
1135 else if (t.cs() == "def") {
1136 context.check_layout(os);
1137 eat_whitespace(p, os, context, false);
1138 string name = p.get_token().cs();
1139 while (p.next_token().cat() != catBegin)
1140 name += p.get_token().asString();
1141 handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}', context);
1144 else if (t.cs() == "noindent") {
1146 context.add_extra_stuff("\\noindent\n");
1149 else if (t.cs() == "appendix") {
1151 context.add_extra_stuff("\\start_of_appendix\n");
1154 // Must attempt to parse "Section*" before "Section".
1155 else if ((p.next_token().asInput() == "*") &&
1156 // The single '=' is meant here.
1157 (newlayout = findLayout(context.textclass,
1158 t.cs() + '*')).get() &&
1159 newlayout->isCommand()) {
1161 output_command_layout(os, p, outer, context, newlayout);
1165 // The single '=' is meant here.
1166 else if ((newlayout = findLayout(context.textclass, t.cs())).get() &&
1167 newlayout->isCommand()) {
1168 output_command_layout(os, p, outer, context, newlayout);
1172 else if (t.cs() == "includegraphics") {
1173 bool const clip = p.next_token().asInput() == "*";
1176 map<string, string> opts = split_map(p.getArg('[', ']'));
1178 opts["clip"] = string();
1179 string name = subst(p.verbatim_item(), "\\lyxdot ", ".");
1181 string const path = getMasterFilePath();
1182 // We want to preserve relative / absolute filenames,
1183 // therefore path is only used for testing
1184 if (!fs::exists(MakeAbsPath(name, path))) {
1185 // The file extension is probably missing.
1186 // Now try to find it out.
1187 string const dvips_name =
1188 find_file(name, path,
1189 known_dvips_graphics_formats);
1190 string const pdftex_name =
1191 find_file(name, path,
1192 known_pdftex_graphics_formats);
1193 if (!dvips_name.empty()) {
1194 if (!pdftex_name.empty()) {
1195 cerr << "This file contains the "
1197 "\"\\includegraphics{"
1199 "However, files\n\""
1200 << dvips_name << "\" and\n\""
1201 << pdftex_name << "\"\n"
1202 "both exist, so I had to make a "
1203 "choice and took the first one.\n"
1204 "Please move the unwanted one "
1205 "someplace else and try again\n"
1206 "if my choice was wrong."
1210 } else if (!pdftex_name.empty())
1213 if (!fs::exists(MakeAbsPath(name, path)))
1214 cerr << "Warning: Could not find graphics file '"
1215 << name << "'." << endl;
1218 context.check_layout(os);
1219 begin_inset(os, "Graphics ");
1220 os << "\n\tfilename " << name << '\n';
1221 if (opts.find("width") != opts.end())
1223 << translate_len(opts["width"]) << '\n';
1224 if (opts.find("height") != opts.end())
1226 << translate_len(opts["height"]) << '\n';
1227 if (opts.find("scale") != opts.end()) {
1228 istringstream iss(opts["scale"]);
1232 os << "\tscale " << val << '\n';
1234 if (opts.find("angle") != opts.end())
1235 os << "\trotateAngle "
1236 << opts["angle"] << '\n';
1237 if (opts.find("origin") != opts.end()) {
1239 string const opt = opts["origin"];
1240 if (opt.find('l') != string::npos) ss << "left";
1241 if (opt.find('r') != string::npos) ss << "right";
1242 if (opt.find('c') != string::npos) ss << "center";
1243 if (opt.find('t') != string::npos) ss << "Top";
1244 if (opt.find('b') != string::npos) ss << "Bottom";
1245 if (opt.find('B') != string::npos) ss << "Baseline";
1246 if (!ss.str().empty())
1247 os << "\trotateOrigin " << ss.str() << '\n';
1249 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
1251 if (opts.find("keepaspectratio") != opts.end())
1252 os << "\tkeepAspectRatio\n";
1253 if (opts.find("clip") != opts.end())
1255 if (opts.find("draft") != opts.end())
1257 if (opts.find("bb") != opts.end())
1258 os << "\tBoundingBox "
1259 << opts["bb"] << '\n';
1260 int numberOfbbOptions = 0;
1261 if (opts.find("bbllx") != opts.end())
1262 numberOfbbOptions++;
1263 if (opts.find("bblly") != opts.end())
1264 numberOfbbOptions++;
1265 if (opts.find("bburx") != opts.end())
1266 numberOfbbOptions++;
1267 if (opts.find("bbury") != opts.end())
1268 numberOfbbOptions++;
1269 if (numberOfbbOptions == 4)
1270 os << "\tBoundingBox "
1271 << opts["bbllx"] << opts["bblly"]
1272 << opts["bburx"] << opts["bbury"] << '\n';
1273 else if (numberOfbbOptions > 0)
1274 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1275 numberOfbbOptions = 0;
1276 if (opts.find("natwidth") != opts.end())
1277 numberOfbbOptions++;
1278 if (opts.find("natheight") != opts.end())
1279 numberOfbbOptions++;
1280 if (numberOfbbOptions == 2)
1281 os << "\tBoundingBox 0bp 0bp "
1282 << opts["natwidth"] << opts["natheight"] << '\n';
1283 else if (numberOfbbOptions > 0)
1284 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1285 ostringstream special;
1286 if (opts.find("hiresbb") != opts.end())
1287 special << "hiresbb,";
1288 if (opts.find("trim") != opts.end())
1290 if (opts.find("viewport") != opts.end())
1291 special << "viewport=" << opts["viewport"] << ',';
1292 if (opts.find("totalheight") != opts.end())
1293 special << "totalheight=" << opts["totalheight"] << ',';
1294 if (opts.find("type") != opts.end())
1295 special << "type=" << opts["type"] << ',';
1296 if (opts.find("ext") != opts.end())
1297 special << "ext=" << opts["ext"] << ',';
1298 if (opts.find("read") != opts.end())
1299 special << "read=" << opts["read"] << ',';
1300 if (opts.find("command") != opts.end())
1301 special << "command=" << opts["command"] << ',';
1302 string s_special = special.str();
1303 if (!s_special.empty()) {
1304 // We had special arguments. Remove the trailing ','.
1305 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
1307 // TODO: Handle the unknown settings better.
1308 // Warn about invalid options.
1309 // Check whether some option was given twice.
1313 else if (t.cs() == "footnote") {
1315 context.check_layout(os);
1316 begin_inset(os, "Foot\n");
1317 os << "status collapsed\n\n";
1318 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1322 else if (t.cs() == "marginpar") {
1324 context.check_layout(os);
1325 begin_inset(os, "Marginal\n");
1326 os << "status collapsed\n\n";
1327 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1331 else if (t.cs() == "ensuremath") {
1333 context.check_layout(os);
1334 Context newcontext(false, context.textclass);
1335 newcontext.font = context.font;
1336 string s = parse_text(p, FLAG_ITEM, false, newcontext);
1337 if (s == "±" || s == "³" || s == "²" || s == "µ")
1340 handle_ert(os, "\\ensuremath{" + s + "}",
1344 else if (t.cs() == "hfill") {
1345 context.check_layout(os);
1346 os << "\n\\hfill\n";
1351 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
1352 // FIXME: Somehow prevent title layouts if
1353 // "maketitle" was not found
1355 skip_braces(p); // swallow this
1358 else if (t.cs() == "tableofcontents") {
1360 context.check_layout(os);
1361 begin_inset(os, "LatexCommand \\tableofcontents\n");
1363 skip_braces(p); // swallow this
1366 else if (t.cs() == "listoffigures") {
1368 context.check_layout(os);
1369 begin_inset(os, "FloatList figure\n");
1371 skip_braces(p); // swallow this
1374 else if (t.cs() == "listoftables") {
1376 context.check_layout(os);
1377 begin_inset(os, "FloatList table\n");
1379 skip_braces(p); // swallow this
1382 else if (t.cs() == "listof") {
1383 p.skip_spaces(true);
1384 string const name = p.get_token().asString();
1385 if (context.textclass.floats().typeExist(name)) {
1386 context.check_layout(os);
1387 begin_inset(os, "FloatList ");
1390 p.get_token(); // swallow second arg
1392 handle_ert(os, "\\listof{" + name + "}", context);
1395 else if (t.cs() == "textrm")
1396 parse_text_attributes(p, os, FLAG_ITEM, outer,
1397 context, "\\family",
1398 context.font.family, "roman");
1400 else if (t.cs() == "textsf")
1401 parse_text_attributes(p, os, FLAG_ITEM, outer,
1402 context, "\\family",
1403 context.font.family, "sans");
1405 else if (t.cs() == "texttt")
1406 parse_text_attributes(p, os, FLAG_ITEM, outer,
1407 context, "\\family",
1408 context.font.family, "typewriter");
1410 else if (t.cs() == "textmd")
1411 parse_text_attributes(p, os, FLAG_ITEM, outer,
1412 context, "\\series",
1413 context.font.series, "medium");
1415 else if (t.cs() == "textbf")
1416 parse_text_attributes(p, os, FLAG_ITEM, outer,
1417 context, "\\series",
1418 context.font.series, "bold");
1420 else if (t.cs() == "textup")
1421 parse_text_attributes(p, os, FLAG_ITEM, outer,
1423 context.font.shape, "up");
1425 else if (t.cs() == "textit")
1426 parse_text_attributes(p, os, FLAG_ITEM, outer,
1428 context.font.shape, "italic");
1430 else if (t.cs() == "textsl")
1431 parse_text_attributes(p, os, FLAG_ITEM, outer,
1433 context.font.shape, "slanted");
1435 else if (t.cs() == "textsc")
1436 parse_text_attributes(p, os, FLAG_ITEM, outer,
1438 context.font.shape, "smallcaps");
1440 else if (t.cs() == "textnormal" || t.cs() == "normalfont") {
1441 context.check_layout(os);
1442 Font oldFont = context.font;
1443 context.font.init();
1444 context.font.size = oldFont.size;
1445 os << "\n\\family " << context.font.family << "\n";
1446 os << "\n\\series " << context.font.series << "\n";
1447 os << "\n\\shape " << context.font.shape << "\n";
1448 if (t.cs() == "textnormal") {
1449 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1450 context.font = oldFont;
1451 os << "\n\\shape " << oldFont.shape << "\n";
1452 os << "\n\\series " << oldFont.series << "\n";
1453 os << "\n\\family " << oldFont.family << "\n";
1455 eat_whitespace(p, os, context, false);
1458 else if (t.cs() == "underbar") {
1459 // Do NOT handle \underline.
1460 // \underbar cuts through y, g, q, p etc.,
1461 // \underline does not.
1462 context.check_layout(os);
1463 os << "\n\\bar under\n";
1464 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1465 os << "\n\\bar default\n";
1468 else if (t.cs() == "emph" || t.cs() == "noun") {
1469 context.check_layout(os);
1470 os << "\n\\" << t.cs() << " on\n";
1471 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1472 os << "\n\\" << t.cs() << " default\n";
1475 else if (use_natbib &&
1476 is_known(t.cs(), known_natbib_commands) &&
1477 ((t.cs() != "citefullauthor" &&
1478 t.cs() != "citeyear" &&
1479 t.cs() != "citeyearpar") ||
1480 p.next_token().asInput() != "*")) {
1481 context.check_layout(os);
1483 // \citet[before][after]{a} \citet[after][before]{a}
1484 // \citet[before][]{a} \citet[][before]{a}
1485 // \citet[after]{a} \citet[after]{a}
1486 // \citet{a} \citet{a}
1487 string command = '\\' + t.cs();
1488 if (p.next_token().asInput() == "*") {
1492 if (command == "\\citefullauthor")
1493 // alternative name for "\\citeauthor*"
1494 command = "\\citeauthor*";
1496 // text before the citation
1498 // text after the citation
1501 boost::tie(before, after) = getCiteArguments(p, true);
1502 if (command == "\\cite") {
1503 // \cite without optional argument means
1504 // \citet, \cite with at least one optional
1505 // argument means \citep.
1506 if (before.empty() && after.empty())
1507 command = "\\citet";
1509 command = "\\citep";
1511 if (before.empty() && after == "[]")
1512 // avoid \citet[]{a}
1514 else if (before == "[]" && after == "[]") {
1515 // avoid \citet[][]{a}
1519 begin_inset(os, "LatexCommand ");
1520 os << command << after << before
1521 << '{' << p.verbatim_item() << "}\n";
1525 else if (use_jurabib &&
1526 is_known(t.cs(), known_jurabib_commands)) {
1527 context.check_layout(os);
1528 string const command = '\\' + t.cs();
1529 char argumentOrder = '\0';
1530 vector<string> const & options = used_packages["jurabib"];
1531 if (std::find(options.begin(), options.end(),
1532 "natbiborder") != options.end())
1533 argumentOrder = 'n';
1534 else if (std::find(options.begin(), options.end(),
1535 "jurabiborder") != options.end())
1536 argumentOrder = 'j';
1538 // text before the citation
1540 // text after the citation
1543 boost::tie(before, after) =
1544 getCiteArguments(p, argumentOrder != 'j');
1545 string const citation = p.verbatim_item();
1546 if (!before.empty() && argumentOrder == '\0') {
1547 cerr << "Warning: Assuming argument order "
1548 "of jurabib version 0.6 for\n'"
1549 << command << before << after << '{'
1550 << citation << "}'.\n"
1551 "Add 'jurabiborder' to the jurabib "
1552 "package options if you used an\n"
1553 "earlier jurabib version." << endl;
1555 begin_inset(os, "LatexCommand ");
1556 os << command << after << before
1557 << '{' << citation << "}\n";
1561 else if (is_known(t.cs(), known_latex_commands)) {
1562 // This needs to be after the check for natbib and
1563 // jurabib commands, because "cite" has different
1564 // arguments with natbib and jurabib.
1565 context.check_layout(os);
1566 begin_inset(os, "LatexCommand ");
1567 os << '\\' << t.cs();
1568 // lyx cannot handle newlines in a latex command
1569 // FIXME: Move the substitution into parser::getOpt()?
1570 os << subst(p.getOpt(), "\n", " ");
1571 os << subst(p.getOpt(), "\n", " ");
1572 os << '{' << subst(p.verbatim_item(), "\n", " ") << "}\n";
1576 else if (is_known(t.cs(), known_quotes)) {
1577 char const * const * where = is_known(t.cs(), known_quotes);
1578 context.check_layout(os);
1579 begin_inset(os, "Quotes ");
1580 os << known_coded_quotes[where - known_quotes];
1582 // LyX adds {} after the quote, so we have to eat
1583 // spaces here if there are any before a possible
1585 eat_whitespace(p, os, context, false);
1589 else if (is_known(t.cs(), known_sizes)) {
1590 char const * const * where = is_known(t.cs(), known_sizes);
1591 context.check_layout(os);
1592 context.font.size = known_coded_sizes[where - known_sizes];
1593 os << "\n\\size " << context.font.size << '\n';
1594 eat_whitespace(p, os, context, false);
1597 else if (is_known(t.cs(), known_font_families)) {
1598 char const * const * where =
1599 is_known(t.cs(), known_font_families);
1600 context.check_layout(os);
1601 context.font.family =
1602 known_coded_font_families[where - known_font_families];
1603 os << "\n\\family " << context.font.family << '\n';
1604 eat_whitespace(p, os, context, false);
1607 else if (is_known(t.cs(), known_font_series)) {
1608 char const * const * where =
1609 is_known(t.cs(), known_font_series);
1610 context.check_layout(os);
1611 context.font.series =
1612 known_coded_font_series[where - known_font_series];
1613 os << "\n\\series " << context.font.series << '\n';
1614 eat_whitespace(p, os, context, false);
1617 else if (is_known(t.cs(), known_font_shapes)) {
1618 char const * const * where =
1619 is_known(t.cs(), known_font_shapes);
1620 context.check_layout(os);
1621 context.font.shape =
1622 known_coded_font_shapes[where - known_font_shapes];
1623 os << "\n\\shape " << context.font.shape << '\n';
1624 eat_whitespace(p, os, context, false);
1626 else if (is_known(t.cs(), known_old_font_families)) {
1627 char const * const * where =
1628 is_known(t.cs(), known_old_font_families);
1629 context.check_layout(os);
1630 string oldsize = context.font.size;
1631 context.font.init();
1632 context.font.size = oldsize;
1633 context.font.family =
1634 known_coded_font_families[where - known_old_font_families];
1635 os << "\n\\family " << context.font.family << "\n"
1636 << "\\series " << context.font.series << "\n"
1637 << "\\shape " << context.font.shape << "\n";
1638 eat_whitespace(p, os, context, false);
1641 else if (is_known(t.cs(), known_old_font_series)) {
1642 char const * const * where =
1643 is_known(t.cs(), known_old_font_series);
1644 context.check_layout(os);
1645 string oldsize = context.font.size;
1646 context.font.init();
1647 context.font.size = oldsize;
1648 context.font.series =
1649 known_coded_font_series[where - known_old_font_series];
1650 os << "\n\\family " << context.font.family << "\n"
1651 << "\\series " << context.font.series << "\n"
1652 << "\\shape " << context.font.shape << "\n";
1653 eat_whitespace(p, os, context, false);
1656 else if (is_known(t.cs(), known_old_font_shapes)) {
1657 char const * const * where =
1658 is_known(t.cs(), known_old_font_shapes);
1659 context.check_layout(os);
1660 string oldsize = context.font.size;
1661 context.font.init();
1662 context.font.size = oldsize;
1663 context.font.shape =
1664 known_coded_font_shapes[where - known_old_font_shapes];
1665 os << "\n\\family " << context.font.family << "\n"
1666 << "\\series " << context.font.series << "\n"
1667 << "\\shape " << context.font.shape << "\n";
1668 eat_whitespace(p, os, context, false);
1671 else if (t.cs() == "LyX" || t.cs() == "TeX"
1672 || t.cs() == "LaTeX") {
1673 context.check_layout(os);
1675 skip_braces(p); // eat {}
1678 else if (t.cs() == "LaTeXe") {
1679 context.check_layout(os);
1681 skip_braces(p); // eat {}
1684 else if (t.cs() == "ldots") {
1685 context.check_layout(os);
1687 os << "\\SpecialChar \\ldots{}\n";
1690 else if (t.cs() == "lyxarrow") {
1691 context.check_layout(os);
1692 os << "\\SpecialChar \\menuseparator\n";
1696 else if (t.cs() == "textcompwordmark") {
1697 context.check_layout(os);
1698 os << "\\SpecialChar \\textcompwordmark{}\n";
1702 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
1703 context.check_layout(os);
1704 os << "\\SpecialChar \\@.\n";
1708 else if (t.cs() == "-") {
1709 context.check_layout(os);
1710 os << "\\SpecialChar \\-\n";
1713 else if (t.cs() == "textasciitilde") {
1714 context.check_layout(os);
1719 else if (t.cs() == "textasciicircum") {
1720 context.check_layout(os);
1725 else if (t.cs() == "textbackslash") {
1726 context.check_layout(os);
1727 os << "\n\\backslash\n";
1731 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
1732 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
1734 context.check_layout(os);
1738 else if (t.cs() == "char") {
1739 context.check_layout(os);
1740 if (p.next_token().character() == '`') {
1742 if (p.next_token().cs() == "\"") {
1747 handle_ert(os, "\\char`", context);
1750 handle_ert(os, "\\char", context);
1754 else if (t.cs() == "\"") {
1755 context.check_layout(os);
1756 string const name = p.verbatim_item();
1757 if (name == "a") os << 'ä';
1758 else if (name == "o") os << 'ö';
1759 else if (name == "u") os << 'ü';
1760 else if (name == "A") os << 'Ä';
1761 else if (name == "O") os << 'Ö';
1762 else if (name == "U") os << 'Ü';
1763 else handle_ert(os, "\"{" + name + "}", context);
1766 // Problem: \= creates a tabstop inside the tabbing environment
1767 // and else an accent. In the latter case we really would want
1768 // \={o} instead of \= o.
1769 else if (t.cs() == "=" && (flags & FLAG_TABBING))
1770 handle_ert(os, t.asInput(), context);
1772 else if (t.cs() == "H" || t.cs() == "c" || t.cs() == "^" || t.cs() == "'"
1773 || t.cs() == "~" || t.cs() == "." || t.cs() == "=") {
1774 // we need the trim as the LyX parser chokes on such spaces
1775 context.check_layout(os);
1776 os << "\n\\i \\" << t.cs() << "{"
1777 << trim(parse_text(p, FLAG_ITEM, outer, context), " ") << "}\n";
1780 else if (t.cs() == "ss") {
1781 context.check_layout(os);
1785 else if (t.cs() == "i" || t.cs() == "j") {
1786 context.check_layout(os);
1787 os << "\\" << t.cs() << ' ';
1790 else if (t.cs() == "\\") {
1791 context.check_layout(os);
1792 string const next = p.next_token().asInput();
1794 handle_ert(os, "\\\\" + p.getOpt(), context);
1795 else if (next == "*") {
1797 handle_ert(os, "\\\\*" + p.getOpt(), context);
1800 os << "\n\\newline\n";
1804 else if (t.cs() == "input" || t.cs() == "include"
1805 || t.cs() == "verbatiminput") {
1806 string name = '\\' + t.cs();
1807 if (t.cs() == "verbatiminput"
1808 && p.next_token().asInput() == "*")
1809 name += p.get_token().asInput();
1810 context.check_layout(os);
1811 begin_inset(os, "Include ");
1812 string filename(p.getArg('{', '}'));
1813 string lyxname(lyx::support::ChangeExtension(filename, ".lyx"));
1814 if (tex2lyx(filename, lyxname)) {
1815 os << name << '{' << lyxname << "}\n";
1817 os << name << '{' << filename << "}\n";
1819 os << "preview false\n";
1823 else if (t.cs() == "fancyhead") {
1824 context.check_layout(os);
1826 ss << "\\fancyhead";
1828 ss << '{' << p.verbatim_item() << "}\n";
1829 handle_ert(os, ss.str(), context);
1832 else if (t.cs() == "bibliographystyle") {
1833 // store new bibliographystyle
1834 bibliographystyle = p.verbatim_item();
1835 // output new bibliographystyle.
1836 // This is only necessary if used in some other macro than \bibliography.
1837 handle_ert(os, "\\bibliographystyle{" + bibliographystyle + "}", context);
1840 else if (t.cs() == "bibliography") {
1841 context.check_layout(os);
1842 begin_inset(os, "LatexCommand ");
1844 // Do we have a bibliographystyle set?
1845 if (!bibliographystyle.empty()) {
1846 os << '[' << bibliographystyle << ']';
1848 os << '{' << p.verbatim_item() << "}\n";
1852 else if (t.cs() == "parbox")
1853 parse_box(p, os, FLAG_ITEM, outer, context, true);
1855 else if (t.cs() == "smallskip" ||
1856 t.cs() == "medskip" ||
1857 t.cs() == "bigskip" ||
1858 t.cs() == "vfill") {
1859 context.check_layout(os);
1860 begin_inset(os, "VSpace ");
1866 else if (t.cs() == "newcommand" ||
1867 t.cs() == "providecommand" ||
1868 t.cs() == "renewcommand") {
1869 // these could be handled by parse_command(), but
1870 // we need to call add_known_command() here.
1871 string name = t.asInput();
1872 if (p.next_token().asInput() == "*") {
1873 // Starred form. Eat '*'
1877 string const command = p.verbatim_item();
1878 string const opt1 = p.getOpt();
1879 string const opt2 = p.getFullOpt();
1880 add_known_command(command, opt1, !opt2.empty());
1881 string const ert = name + '{' + command + '}' +
1883 '{' + p.verbatim_item() + '}';
1884 handle_ert(os, ert, context);
1887 else if (t.cs() == "vspace") {
1888 bool starred = false;
1889 if (p.next_token().asInput() == "*") {
1893 string const length = p.verbatim_item();
1896 bool valid = splitLatexLength(length, valstring, unit);
1897 bool known_vspace = false;
1898 bool known_unit = false;
1901 istringstream iss(valstring);
1904 if (unit == "\\smallskipamount") {
1906 known_vspace = true;
1907 } else if (unit == "\\medskipamount") {
1909 known_vspace = true;
1910 } else if (unit == "\\bigskipamount") {
1912 known_vspace = true;
1913 } else if (unit == "\\fill") {
1915 known_vspace = true;
1918 if (!known_vspace) {
1919 switch (unitFromString(unit)) {
1940 if (known_unit || known_vspace) {
1941 // Literal length or known variable
1942 context.check_layout(os);
1943 begin_inset(os, "VSpace ");
1951 // LyX can't handle other length variables in Inset VSpace
1952 string name = t.asInput();
1957 handle_ert(os, name + '{' + unit + '}', context);
1958 else if (value == -1.0)
1959 handle_ert(os, name + "{-" + unit + '}', context);
1961 handle_ert(os, name + '{' + valstring + unit + '}', context);
1963 handle_ert(os, name + '{' + length + '}', context);
1968 //cerr << "#: " << t << " mode: " << mode << endl;
1969 // heuristic: read up to next non-nested space
1971 string s = t.asInput();
1972 string z = p.verbatim_item();
1973 while (p.good() && z != " " && z.size()) {
1974 //cerr << "read: " << z << endl;
1976 z = p.verbatim_item();
1978 cerr << "found ERT: " << s << endl;
1979 handle_ert(os, s + ' ', context);
1981 string name = t.asInput();
1982 if (p.next_token().asInput() == "*") {
1983 // Starred commands like \vspace*{}
1984 p.get_token(); // Eat '*'
1987 if (! parse_command(name, p, os, outer, context))
1988 handle_ert(os, name, context);
1991 if (flags & FLAG_LEAVE) {
1992 flags &= ~FLAG_LEAVE;