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/FileInfo.h"
21 #include "support/lstrings.h"
22 #include "support/convert.h"
23 #include "support/filetools.h"
25 #include <boost/tuple/tuple.hpp>
32 using lyx::support::FileInfo;
33 using lyx::support::MakeAbsPath;
34 using lyx::support::rtrim;
35 using lyx::support::suffixIs;
36 using lyx::support::contains;
37 using lyx::support::subst;
44 using std::ostringstream;
45 using std::istringstream;
50 /// thin wrapper around parse_text using a string
51 string parse_text(Parser & p, unsigned flags, const bool outer,
55 parse_text(p, os, flags, outer, context);
60 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
63 Context newcontext(true, context.textclass);
64 newcontext.font = context.font;
65 parse_text(p, os, flags, outer, newcontext);
66 newcontext.check_end_layout(os);
70 /// parses a paragraph snippet, useful for example for \emph{...}
71 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
74 Context newcontext(false, context.textclass);
75 newcontext.font = context.font;
76 parse_text(p, os, flags, outer, newcontext);
77 // should not be needed
78 newcontext.check_end_layout(os);
84 char const * const known_latex_commands[] = { "ref", "cite", "label", "index",
85 "printindex", "pageref", "url", "vref", "vpageref", "prettyref", "eqref", 0 };
89 * We can't put these into known_latex_commands because the argument order
90 * is reversed in lyx if there are 2 arguments.
91 * The starred forms are also known.
93 char const * const known_natbib_commands[] = { "cite", "citet", "citep",
94 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
95 "citefullauthor", "Citet", "Citep", "Citealt", "Citealp", "Citeauthor", 0 };
99 * We can't put these into known_latex_commands because the argument order
100 * is reversed in lyx if there are 2 arguments.
101 * No starred form other than "cite*" known.
103 char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
104 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar", "fullcite",
105 // jurabib commands not (yet) supported by LyX:
106 // "footcite", "footcitet", "footcitep", "footcitealt", "footcitealp",
107 // "footciteauthor", "footciteyear", "footciteyearpar",
108 "citefield", "citetitle", "cite*", 0 };
110 /// LaTeX names for quotes
111 char const * const known_quotes[] = { "glqq", "grqq", "quotedblbase",
112 "textquotedblleft", "quotesinglbase", "guilsinglleft", "guilsinglright", 0};
114 /// the same as known_quotes with .lyx names
115 char const * const known_coded_quotes[] = { "gld", "grd", "gld",
116 "grd", "gls", "fls", "frd", 0};
118 /// LaTeX names for font sizes
119 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
120 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
122 /// the same as known_sizes with .lyx names
123 char const * const known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
124 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
126 /// LaTeX 2.09 names for font families
127 char const * const known_old_font_families[] = { "rm", "sf", "tt", 0};
129 /// LaTeX names for font families
130 char const * const known_font_families[] = { "rmfamily", "sffamily",
133 /// the same as known_old_font_families and known_font_families with .lyx names
134 char const * const known_coded_font_families[] = { "roman", "sans",
137 /// LaTeX 2.09 names for font series
138 char const * const known_old_font_series[] = { "bf", 0};
140 /// LaTeX names for font series
141 char const * const known_font_series[] = { "bfseries", "mdseries", 0};
143 /// the same as known_old_font_series and known_font_series with .lyx names
144 char const * const known_coded_font_series[] = { "bold", "medium", 0};
146 /// LaTeX 2.09 names for font shapes
147 char const * const known_old_font_shapes[] = { "it", "sl", "sc", 0};
149 /// LaTeX names for font shapes
150 char const * const known_font_shapes[] = { "itshape", "slshape", "scshape",
153 /// the same as known_old_font_shapes and known_font_shapes with .lyx names
154 char const * const known_coded_font_shapes[] = { "italic", "slanted",
155 "smallcaps", "up", 0};
158 * Graphics file extensions known by the dvips driver of the graphics package.
159 * These extensions are used to complete the filename of an included
160 * graphics file if it does not contain an extension.
161 * The order must be the same that latex uses to find a file, because we
162 * will use the first extension that matches.
163 * This is only an approximation for the common cases. If we would want to
164 * do it right in all cases, we would need to know which graphics driver is
165 * used and know the extensions of every driver of the graphics package.
167 char const * const known_dvips_graphics_formats[] = {"eps", "ps", "eps.gz",
168 "ps.gz", "eps.Z", "ps.Z", 0};
171 * Graphics file extensions known by the pdftex driver of the graphics package.
172 * \see known_dvips_graphics_formats
174 char const * const known_pdftex_graphics_formats[] = {"png", "pdf", "jpg",
178 /// splits "x=z, y=b" into a map
179 map<string, string> split_map(string const & s)
181 map<string, string> res;
184 for (size_t i = 0; i < v.size(); ++i) {
185 size_t const pos = v[i].find('=');
186 string const index = v[i].substr(0, pos);
187 string const value = v[i].substr(pos + 1, string::npos);
188 res[trim(index)] = trim(value);
195 * Split a LaTeX length into value and unit.
196 * The latter can be a real unit like "pt", or a latex length variable
197 * like "\textwidth". The unit may contain additional stuff like glue
198 * lengths, but we don't care, because such lengths are ERT anyway.
199 * \return true if \param value and \param unit are valid.
201 bool splitLatexLength(string const & len, string & value, string & unit)
205 const string::size_type i = len.find_first_not_of(" -+0123456789.,");
206 //'4,5' is a valid LaTeX length number. Change it to '4.5'
207 string const length = subst(len, ',', '.');
208 if (i == string::npos)
211 if (len[0] == '\\') {
212 // We had something like \textwidth without a factor
218 value = trim(string(length, 0, i));
222 // 'cM' is a valid LaTeX length unit. Change it to 'cm'
223 if (contains(len, '\\'))
224 unit = trim(string(len, i));
226 unit = lyx::support::lowercase(trim(string(len, i)));
231 /// A simple function to translate a latex length to something lyx can
232 /// understand. Not perfect, but rather best-effort.
233 bool translate_len(string const & length, string & valstring, string & unit)
235 if (!splitLatexLength(length, valstring, unit))
237 // LyX uses percent values
239 istringstream iss(valstring);
244 string const percentval = oss.str();
246 if (unit.empty() || unit[0] != '\\')
248 string::size_type const i = unit.find(' ');
249 string const endlen = (i == string::npos) ? string() : string(unit, i);
250 if (unit == "\\textwidth") {
251 valstring = percentval;
252 unit = "text%" + endlen;
253 } else if (unit == "\\columnwidth") {
254 valstring = percentval;
255 unit = "col%" + endlen;
256 } else if (unit == "\\paperwidth") {
257 valstring = percentval;
258 unit = "page%" + endlen;
259 } else if (unit == "\\linewidth") {
260 valstring = percentval;
261 unit = "line%" + endlen;
262 } else if (unit == "\\paperheight") {
263 valstring = percentval;
264 unit = "pheight%" + endlen;
265 } else if (unit == "\\textheight") {
266 valstring = percentval;
267 unit = "theight%" + endlen;
273 string translate_len(string const & length)
277 if (translate_len(length, value, unit))
279 // If the input is invalid, return what we have.
285 * Translates a LaTeX length into \param value, \param unit and
286 * \param special parts suitable for a box inset.
287 * The difference from translate_len() is that a box inset knows about
288 * some special "units" that are stored in \param special.
290 void translate_box_len(string const & length, string & value, string & unit, string & special)
292 if (translate_len(length, value, unit)) {
293 if (unit == "\\height" || unit == "\\depth" ||
294 unit == "\\totalheight" || unit == "\\width") {
295 special = unit.substr(1);
296 // The unit is not used, but LyX requires a dummy setting
309 * Find a file with basename \p name in path \p path and an extension
312 string find_file(string const & name, string const & path,
313 char const * const * extensions)
315 for (char const * const * what = extensions; *what; ++what) {
316 // We don't use ChangeExtension() because it does the wrong
317 // thing if name contains a dot.
318 string const trial = name + '.' + (*what);
319 if (FileInfo(MakeAbsPath(trial, path)).exist())
326 void begin_inset(ostream & os, string const & name)
328 os << "\n\\begin_inset " << name;
332 void end_inset(ostream & os)
334 os << "\n\\end_inset\n\n";
338 void skip_braces(Parser & p)
340 if (p.next_token().cat() != catBegin)
343 if (p.next_token().cat() == catEnd) {
351 void handle_ert(ostream & os, string const & s, Context & context, bool check_layout = true)
354 // We must have a valid layout before outputting the ERT inset.
355 context.check_layout(os);
357 Context newcontext(true, context.textclass);
358 begin_inset(os, "ERT");
359 os << "\nstatus collapsed\n";
360 newcontext.check_layout(os);
361 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
363 os << "\n\\backslash\n";
364 else if (*it == '\n')
365 os << "\n\\newline\n";
369 newcontext.check_end_layout(os);
374 void handle_comment(ostream & os, string const & s, Context & context)
376 // TODO: Handle this better
377 Context newcontext(true, context.textclass);
378 begin_inset(os, "ERT");
379 os << "\nstatus collapsed\n";
380 newcontext.check_layout(os);
381 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
383 os << "\n\\backslash\n";
387 // make sure that our comment is the last thing on the line
389 newcontext.check_end_layout(os);
394 class isLayout : public std::unary_function<LyXLayout_ptr, bool> {
396 isLayout(string const name) : name_(name) {}
397 bool operator()(LyXLayout_ptr const & ptr) const {
398 return ptr->latexname() == name_;
405 LyXLayout_ptr findLayout(LyXTextClass const & textclass,
408 LyXTextClass::const_iterator beg = textclass.begin();
409 LyXTextClass::const_iterator end = textclass.end();
411 LyXTextClass::const_iterator
412 it = std::find_if(beg, end, isLayout(name));
414 return (it == end) ? LyXLayout_ptr() : *it;
418 void eat_whitespace(Parser &, ostream &, Context &, bool);
421 void output_command_layout(ostream & os, Parser & p, bool outer,
422 Context & parent_context,
423 LyXLayout_ptr newlayout)
425 parent_context.check_end_layout(os);
426 Context context(true, parent_context.textclass, newlayout,
427 parent_context.layout, parent_context.font);
428 if (parent_context.deeper_paragraph) {
429 // We are beginning a nested environment after a
430 // deeper paragraph inside the outer list environment.
431 // Therefore we don't need to output a "begin deeper".
432 context.need_end_deeper = true;
434 context.check_deeper(os);
435 context.check_layout(os);
436 if (context.layout->optionalargs > 0) {
437 eat_whitespace(p, os, context, false);
438 if (p.next_token().character() == '[') {
439 p.get_token(); // eat '['
440 begin_inset(os, "OptArg\n");
441 os << "status collapsed\n\n";
442 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
444 eat_whitespace(p, os, context, false);
447 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
448 context.check_end_layout(os);
449 if (parent_context.deeper_paragraph) {
450 // We must suppress the "end deeper" because we
451 // suppressed the "begin deeper" above.
452 context.need_end_deeper = false;
454 context.check_end_deeper(os);
455 // We don't need really a new paragraph, but
456 // we must make sure that the next item gets a \begin_layout.
457 parent_context.new_paragraph(os);
462 * Output a space if necessary.
463 * This function gets called for every whitespace token.
465 * We have three cases here:
466 * 1. A space must be suppressed. Example: The lyxcode case below
467 * 2. A space may be suppressed. Example: Spaces before "\par"
468 * 3. A space must not be suppressed. Example: A space between two words
470 * We currently handle only 1. and 3 and from 2. only the case of
471 * spaces before newlines as a side effect.
473 * 2. could be used to suppress as many spaces as possible. This has two effects:
474 * - Reimporting LyX generated LaTeX files changes almost no whitespace
475 * - Superflous whitespace from non LyX generated LaTeX files is removed.
476 * The drawback is that the logic inside the function becomes
477 * complicated, and that is the reason why it is not implemented.
479 void check_space(Parser const & p, ostream & os, Context & context)
481 Token const next = p.next_token();
482 Token const curr = p.curr_token();
483 // A space before a single newline and vice versa must be ignored
484 // LyX emits a newline before \end{lyxcode}.
485 // This newline must be ignored,
486 // otherwise LyX will add an additional protected space.
487 if (next.cat() == catSpace ||
488 next.cat() == catNewline ||
489 (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
492 context.check_layout(os);
498 * Check whether \p command is a known command. If yes,
499 * handle the command with all arguments.
500 * \return true if the command was parsed, false otherwise.
502 bool parse_command(string const & command, Parser & p, ostream & os,
503 bool outer, Context & context)
505 if (known_commands.find(command) != known_commands.end()) {
506 vector<ArgumentType> const & template_arguments = known_commands[command];
507 string ert = command;
508 size_t no_arguments = template_arguments.size();
509 for (size_t i = 0; i < no_arguments; ++i) {
510 switch (template_arguments[i]) {
512 // This argument contains regular LaTeX
513 handle_ert(os, ert + '{', context);
514 parse_text(p, os, FLAG_ITEM, outer, context);
518 // This argument may contain special characters
519 ert += '{' + p.verbatim_item() + '}';
526 handle_ert(os, ert, context);
533 /// Parses a minipage or parbox
534 void parse_box(Parser & p, ostream & os, unsigned flags, bool outer,
535 Context & parent_context, bool use_parbox)
539 string height_value = "0";
540 string height_unit = "pt";
541 string height_special = "none";
543 if (p.next_token().asInput() == "[") {
544 position = p.getArg('[', ']');
545 if (position != "t" && position != "c" && position != "b") {
547 cerr << "invalid position for minipage/parbox" << endl;
549 if (p.next_token().asInput() == "[") {
550 latex_height = p.getArg('[', ']');
551 translate_box_len(latex_height, height_value, height_unit, height_special);
553 if (p.next_token().asInput() == "[") {
554 inner_pos = p.getArg('[', ']');
555 if (inner_pos != "c" && inner_pos != "t" &&
556 inner_pos != "b" && inner_pos != "s") {
557 inner_pos = position;
558 cerr << "invalid inner_pos for minipage/parbox"
566 string const latex_width = p.verbatim_item();
567 translate_len(latex_width, width_value, width_unit);
568 if (contains(width_unit, '\\') || contains(height_unit, '\\')) {
569 // LyX can't handle length variables
574 ss << "\\begin{minipage}";
575 if (!position.empty())
576 ss << '[' << position << ']';
577 if (!latex_height.empty())
578 ss << '[' << latex_height << ']';
579 if (!inner_pos.empty())
580 ss << '[' << inner_pos << ']';
581 ss << "{" << latex_width << "}";
584 handle_ert(os, ss.str(), parent_context);
585 parent_context.new_paragraph(os);
586 parse_text_in_inset(p, os, flags, outer, parent_context);
588 handle_ert(os, "}", parent_context);
590 handle_ert(os, "\\end{minipage}", parent_context);
592 // LyX does not like empty positions, so we have
593 // to set them to the LaTeX default values here.
594 if (position.empty())
596 if (inner_pos.empty())
597 inner_pos = position;
598 parent_context.check_layout(os);
599 begin_inset(os, "Box Frameless\n");
600 os << "position \"" << position << "\"\n";
601 os << "hor_pos \"c\"\n";
602 os << "has_inner_box 1\n";
603 os << "inner_pos \"" << inner_pos << "\"\n";
604 os << "use_parbox " << use_parbox << "\n";
605 os << "width \"" << width_value << width_unit << "\"\n";
606 os << "special \"none\"\n";
607 os << "height \"" << height_value << height_unit << "\"\n";
608 os << "height_special \"" << height_special << "\"\n";
609 os << "status open\n\n";
610 parse_text_in_inset(p, os, flags, outer, parent_context);
612 #ifdef PRESERVE_LAYOUT
613 // lyx puts a % after the end of the minipage
614 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
616 //handle_comment(os, "%dummy", parent_context);
619 parent_context.new_paragraph(os);
621 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
622 //handle_comment(os, "%dummy", parent_context);
625 // We add a protected space if something real follows
626 if (p.good() && p.next_token().cat() != catComment) {
627 os << "\\InsetSpace ~\n";
635 void parse_environment(Parser & p, ostream & os, bool outer,
636 Context & parent_context)
638 LyXLayout_ptr newlayout;
639 string const name = p.getArg('{', '}');
640 const bool is_starred = suffixIs(name, '*');
641 string const unstarred_name = rtrim(name, "*");
642 eat_whitespace(p, os, parent_context, false);
643 active_environments.push_back(name);
645 if (is_math_env(name)) {
646 parent_context.check_layout(os);
647 begin_inset(os, "Formula ");
648 os << "\\begin{" << name << "}";
649 parse_math(p, os, FLAG_END, MATH_MODE);
650 os << "\\end{" << name << "}";
654 else if (name == "tabular" || name == "longtable") {
655 parent_context.check_layout(os);
656 begin_inset(os, "Tabular ");
657 handle_tabular(p, os, name == "longtable", parent_context);
661 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
662 parent_context.check_layout(os);
663 begin_inset(os, "Float " + unstarred_name + "\n");
664 if (p.next_token().asInput() == "[") {
665 os << "placement " << p.getArg('[', ']') << '\n';
667 os << "wide " << convert<string>(is_starred)
668 << "\nsideways false"
669 << "\nstatus open\n\n";
670 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
672 // We don't need really a new paragraph, but
673 // we must make sure that the next item gets a \begin_layout.
674 parent_context.new_paragraph(os);
677 else if (name == "minipage")
678 parse_box(p, os, FLAG_END, outer, parent_context, false);
680 // Alignment settings
681 else if (name == "center" || name == "flushleft" || name == "flushright" ||
682 name == "centering" || name == "raggedright" || name == "raggedleft") {
683 // We must begin a new paragraph if not already done
684 if (! parent_context.atParagraphStart()) {
685 parent_context.check_end_layout(os);
686 parent_context.new_paragraph(os);
688 if (name == "flushleft" || name == "raggedright")
689 parent_context.add_extra_stuff("\\align left ");
690 else if (name == "flushright" || name == "raggedleft")
691 parent_context.add_extra_stuff("\\align right ");
693 parent_context.add_extra_stuff("\\align center ");
694 parse_text(p, os, FLAG_END, outer, parent_context);
695 // Just in case the environment is empty ..
696 parent_context.extra_stuff.erase();
697 // We must begin a new paragraph to reset the alignment
698 parent_context.new_paragraph(os);
701 // The single '=' is meant here.
702 else if ((newlayout = findLayout(parent_context.textclass, name)).get() &&
703 newlayout->isEnvironment()) {
704 Context context(true, parent_context.textclass, newlayout,
705 parent_context.layout, parent_context.font);
706 if (parent_context.deeper_paragraph) {
707 // We are beginning a nested environment after a
708 // deeper paragraph inside the outer list environment.
709 // Therefore we don't need to output a "begin deeper".
710 context.need_end_deeper = true;
712 parent_context.check_end_layout(os);
713 switch (context.layout->latextype) {
714 case LATEX_LIST_ENVIRONMENT:
715 context.extra_stuff = "\\labelwidthstring "
716 + p.verbatim_item() + '\n';
719 case LATEX_BIB_ENVIRONMENT:
720 p.verbatim_item(); // swallow next arg
726 context.check_deeper(os);
727 parse_text(p, os, FLAG_END, outer, context);
728 context.check_end_layout(os);
729 if (parent_context.deeper_paragraph) {
730 // We must suppress the "end deeper" because we
731 // suppressed the "begin deeper" above.
732 context.need_end_deeper = false;
734 context.check_end_deeper(os);
735 parent_context.new_paragraph(os);
738 else if (name == "appendix") {
739 // This is no good latex style, but it works and is used in some documents...
740 parent_context.check_end_layout(os);
741 Context context(true, parent_context.textclass, parent_context.layout,
742 parent_context.layout, parent_context.font);
743 context.check_layout(os);
744 os << "\\start_of_appendix\n";
745 parse_text(p, os, FLAG_END, outer, context);
746 context.check_end_layout(os);
749 else if (name == "comment") {
750 parent_context.check_layout(os);
751 begin_inset(os, "Note Comment\n");
752 os << "status open\n";
753 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
757 else if (name == "lyxgreyedout") {
758 parent_context.check_layout(os);
759 begin_inset(os, "Note Greyedout\n");
760 os << "status open\n";
761 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
765 else if (name == "tabbing") {
766 // We need to remember that we have to handle '\=' specially
767 handle_ert(os, "\\begin{" + name + "}", parent_context);
768 parse_text_snippet(p, os, FLAG_END | FLAG_TABBING, outer, parent_context);
769 handle_ert(os, "\\end{" + name + "}", parent_context);
773 handle_ert(os, "\\begin{" + name + "}", parent_context);
774 parse_text_snippet(p, os, FLAG_END, outer, parent_context);
775 handle_ert(os, "\\end{" + name + "}", parent_context);
778 active_environments.pop_back();
783 /// parses a comment and outputs it to \p os.
784 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
786 BOOST_ASSERT(t.cat() == catComment);
787 context.check_layout(os);
788 if (!t.cs().empty()) {
789 handle_comment(os, '%' + t.cs(), context);
790 if (p.next_token().cat() == catNewline) {
791 // A newline after a comment line starts a new
793 if(!context.atParagraphStart()) {
794 // Only start a new paragraph if not already
795 // done (we might get called recursively)
796 context.new_paragraph(os);
798 eat_whitespace(p, os, context, true);
808 * Reads spaces and comments until the first non-space, non-comment token.
809 * New paragraphs (double newlines or \\par) are handled like simple spaces
810 * if \p eatParagraph is true.
811 * Spaces are skipped, but comments are written to \p os.
813 void eat_whitespace(Parser & p, ostream & os, Context & context,
817 Token const & t = p.get_token();
818 if (t.cat() == catComment)
819 parse_comment(p, os, t, context);
820 else if ((! eatParagraph && p.isParagraph()) ||
821 (t.cat() != catSpace && t.cat() != catNewline)) {
830 * Set a font attribute, parse text and reset the font attribute.
831 * \param attribute Attribute name (e.g. \\family, \\shape etc.)
832 * \param currentvalue Current value of the attribute. Is set to the new
833 * value during parsing.
834 * \param newvalue New value of the attribute
836 void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
837 Context & context, string const & attribute,
838 string & currentvalue, string const & newvalue)
840 context.check_layout(os);
841 string oldvalue = currentvalue;
842 currentvalue = newvalue;
843 os << '\n' << attribute << ' ' << newvalue << "\n";
844 parse_text_snippet(p, os, flags, outer, context);
845 currentvalue = oldvalue;
846 os << '\n' << attribute << ' ' << oldvalue << "\n";
850 /// get the arguments of a natbib or jurabib citation command
851 std::pair<string, string> getCiteArguments(Parser & p, bool natbibOrder)
853 // We need to distinguish "" and "[]", so we can't use p.getOpt().
855 // text before the citation
857 // text after the citation
858 string after = p.getFullOpt();
860 if (!after.empty()) {
861 before = p.getFullOpt();
862 if (natbibOrder && !before.empty())
863 std::swap(before, after);
865 return std::make_pair(before, after);
868 } // anonymous namespace
871 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
874 LyXLayout_ptr newlayout;
875 // Store the latest bibliographystyle (needed for bibtex inset)
876 string bibliographystyle;
877 bool const use_natbib = used_packages.find("natbib") != used_packages.end();
878 bool const use_jurabib = used_packages.find("jurabib") != used_packages.end();
880 Token const & t = p.get_token();
883 cerr << "t: " << t << " flags: " << flags << "\n";
886 if (flags & FLAG_ITEM) {
887 if (t.cat() == catSpace)
891 if (t.cat() == catBegin) {
892 // skip the brace and collect everything to the next matching
894 flags |= FLAG_BRACE_LAST;
898 // handle only this single token, leave the loop if done
902 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
908 if (t.cat() == catMath) {
909 // we are inside some text mode thingy, so opening new math is allowed
910 context.check_layout(os);
911 begin_inset(os, "Formula ");
912 Token const & n = p.get_token();
913 if (n.cat() == catMath && outer) {
914 // TeX's $$...$$ syntax for displayed math
916 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
918 p.get_token(); // skip the second '$' token
920 // simple $...$ stuff
923 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
929 else if (t.cat() == catSuper || t.cat() == catSub)
930 cerr << "catcode " << t << " illegal in text mode\n";
932 // Basic support for english quotes. This should be
933 // extended to other quotes, but is not so easy (a
934 // left english quote is the same as a right german
936 else if (t.asInput() == "`"
937 && p.next_token().asInput() == "`") {
938 context.check_layout(os);
939 begin_inset(os, "Quotes ");
945 else if (t.asInput() == "'"
946 && p.next_token().asInput() == "'") {
947 context.check_layout(os);
948 begin_inset(os, "Quotes ");
955 else if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph()))
956 check_space(p, os, context);
958 else if (t.cat() == catLetter ||
959 t.cat() == catOther ||
960 t.cat() == catAlign ||
961 t.cat() == catParameter) {
962 context.check_layout(os);
966 else if (p.isParagraph()) {
967 context.new_paragraph(os);
968 eat_whitespace(p, os, context, true);
971 else if (t.cat() == catActive) {
972 context.check_layout(os);
973 if (t.character() == '~') {
974 if (context.layout->free_spacing)
977 os << "\\InsetSpace ~\n";
982 else if (t.cat() == catBegin) {
983 context.check_layout(os);
984 // special handling of font attribute changes
985 Token const prev = p.prev_token();
986 Token const next = p.next_token();
987 Font const oldFont = context.font;
988 string const s = parse_text(p, FLAG_BRACE_LAST, outer,
990 context.font = oldFont;
991 if (s.empty() && (p.next_token().character() == '`' ||
992 (prev.character() == '-' &&
993 p.next_token().character() == '-')))
994 ; // ignore it in {}`` or -{}-
995 else if (s == "[" || s == "]" || s == "*")
997 else if (is_known(next.cs(), known_sizes))
998 // s will change the size, so we must reset
1000 os << s << "\n\\size " << context.font.size
1002 else if (is_known(next.cs(), known_font_families))
1003 // s will change the font family, so we must
1005 os << s << "\n\\family "
1006 << context.font.family << "\n";
1007 else if (is_known(next.cs(), known_font_series))
1008 // s will change the font series, so we must
1010 os << s << "\n\\series "
1011 << context.font.series << "\n";
1012 else if (is_known(next.cs(), known_font_shapes))
1013 // s will change the font shape, so we must
1015 os << s << "\n\\shape "
1016 << context.font.shape << "\n";
1017 else if (is_known(next.cs(), known_old_font_families) ||
1018 is_known(next.cs(), known_old_font_series) ||
1019 is_known(next.cs(), known_old_font_shapes))
1020 // s will change the font family, series
1021 // and shape, so we must reset it here
1023 << "\n\\family " << context.font.family
1024 << "\n\\series " << context.font.series
1025 << "\n\\shape " << context.font.shape
1028 handle_ert(os, "{", context, false);
1029 // s will end the current layout and begin a
1030 // new one if necessary
1032 handle_ert(os, "}", context);
1036 else if (t.cat() == catEnd) {
1037 if (flags & FLAG_BRACE_LAST) {
1040 cerr << "stray '}' in text\n";
1041 handle_ert(os, "}", context);
1044 else if (t.cat() == catComment)
1045 parse_comment(p, os, t, context);
1048 // control sequences
1051 else if (t.cs() == "(") {
1052 context.check_layout(os);
1053 begin_inset(os, "Formula");
1055 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
1060 else if (t.cs() == "[") {
1061 context.check_layout(os);
1062 begin_inset(os, "Formula");
1064 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
1069 else if (t.cs() == "begin")
1070 parse_environment(p, os, outer, context);
1072 else if (t.cs() == "end") {
1073 if (flags & FLAG_END) {
1074 // eat environment name
1075 string const name = p.getArg('{', '}');
1076 if (name != active_environment())
1077 cerr << "\\end{" + name + "} does not match \\begin{"
1078 + active_environment() + "}\n";
1081 p.error("found 'end' unexpectedly");
1084 else if (t.cs() == "item") {
1087 bool optarg = false;
1088 if (p.next_token().character() == '[') {
1089 p.get_token(); // eat '['
1090 Context newcontext(false, context.textclass);
1091 newcontext.font = context.font;
1092 s = parse_text(p, FLAG_BRACK_LAST, outer, newcontext);
1096 context.check_layout(os);
1098 if (context.layout->labeltype != LABEL_MANUAL) {
1099 // lyx does not support \item[\mybullet]
1100 // in itemize environments
1101 handle_ert(os, "[", context);
1103 handle_ert(os, "]", context);
1104 } else if (!s.empty()) {
1105 // The space is needed to separate the
1106 // item from the rest of the sentence.
1108 eat_whitespace(p, os, context, false);
1113 else if (t.cs() == "bibitem") {
1115 context.check_layout(os);
1118 os << '{' << p.verbatim_item() << '}' << "\n";
1121 else if (t.cs() == "def") {
1122 context.check_layout(os);
1123 eat_whitespace(p, os, context, false);
1124 string name = p.get_token().cs();
1125 while (p.next_token().cat() != catBegin)
1126 name += p.get_token().asString();
1127 handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}', context);
1130 else if (t.cs() == "noindent") {
1132 context.add_extra_stuff("\\noindent ");
1135 else if (t.cs() == "appendix") {
1137 context.add_extra_stuff("\\start_of_appendix ");
1140 // Must attempt to parse "Section*" before "Section".
1141 else if ((p.next_token().asInput() == "*") &&
1142 // The single '=' is meant here.
1143 (newlayout = findLayout(context.textclass,
1144 t.cs() + '*')).get() &&
1145 newlayout->isCommand()) {
1147 output_command_layout(os, p, outer, context, newlayout);
1151 // The single '=' is meant here.
1152 else if ((newlayout = findLayout(context.textclass, t.cs())).get() &&
1153 newlayout->isCommand()) {
1154 output_command_layout(os, p, outer, context, newlayout);
1158 else if (t.cs() == "includegraphics") {
1159 map<string, string> opts = split_map(p.getArg('[', ']'));
1160 string name = subst(p.verbatim_item(), "\\lyxdot ", ".");
1162 string const path = getMasterFilePath();
1163 // We want to preserve relative / absolute filenames,
1164 // therefore path is only used for testing
1165 if (!FileInfo(MakeAbsPath(name, path)).exist()) {
1166 // The file extension is probably missing.
1167 // Now try to find it out.
1168 string const dvips_name =
1169 find_file(name, path,
1170 known_dvips_graphics_formats);
1171 string const pdftex_name =
1172 find_file(name, path,
1173 known_pdftex_graphics_formats);
1174 if (!dvips_name.empty()) {
1175 if (!pdftex_name.empty()) {
1176 cerr << "This file contains the "
1178 "\"\\includegraphics{"
1180 "However, files\n\""
1181 << dvips_name << "\" and\n\""
1182 << pdftex_name << "\"\n"
1183 "both exist, so I had to make a "
1184 "choice and took the first one.\n"
1185 "Please move the unwanted one "
1186 "someplace else and try again\n"
1187 "if my choice was wrong."
1191 } else if (!pdftex_name.empty())
1194 if (!FileInfo(MakeAbsPath(name, path)).exist())
1195 cerr << "Warning: Could not find graphics file '"
1196 << name << "'." << endl;
1199 context.check_layout(os);
1200 begin_inset(os, "Graphics ");
1201 os << "\n\tfilename " << name << '\n';
1202 if (opts.find("width") != opts.end())
1204 << translate_len(opts["width"]) << '\n';
1205 if (opts.find("height") != opts.end())
1207 << translate_len(opts["height"]) << '\n';
1208 if (opts.find("scale") != opts.end()) {
1209 istringstream iss(opts["scale"]);
1213 os << "\tscale " << val << '\n';
1215 if (opts.find("angle") != opts.end())
1216 os << "\trotateAngle "
1217 << opts["angle"] << '\n';
1218 if (opts.find("origin") != opts.end()) {
1220 string const opt = opts["origin"];
1221 if (opt.find('l') != string::npos) ss << "left";
1222 if (opt.find('r') != string::npos) ss << "right";
1223 if (opt.find('c') != string::npos) ss << "center";
1224 if (opt.find('t') != string::npos) ss << "Top";
1225 if (opt.find('b') != string::npos) ss << "Bottom";
1226 if (opt.find('B') != string::npos) ss << "Baseline";
1227 if (!ss.str().empty())
1228 os << "\trotateOrigin " << ss.str() << '\n';
1230 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
1232 if (opts.find("keepaspectratio") != opts.end())
1233 os << "\tkeepAspectRatio\n";
1234 if (opts.find("clip") != opts.end())
1236 if (opts.find("draft") != opts.end())
1238 if (opts.find("bb") != opts.end())
1239 os << "\tBoundingBox "
1240 << opts["bb"] << '\n';
1241 int numberOfbbOptions = 0;
1242 if (opts.find("bbllx") != opts.end())
1243 numberOfbbOptions++;
1244 if (opts.find("bblly") != opts.end())
1245 numberOfbbOptions++;
1246 if (opts.find("bburx") != opts.end())
1247 numberOfbbOptions++;
1248 if (opts.find("bbury") != opts.end())
1249 numberOfbbOptions++;
1250 if (numberOfbbOptions == 4)
1251 os << "\tBoundingBox "
1252 << opts["bbllx"] << opts["bblly"]
1253 << opts["bburx"] << opts["bbury"] << '\n';
1254 else if (numberOfbbOptions > 0)
1255 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1256 numberOfbbOptions = 0;
1257 if (opts.find("natwidth") != opts.end())
1258 numberOfbbOptions++;
1259 if (opts.find("natheight") != opts.end())
1260 numberOfbbOptions++;
1261 if (numberOfbbOptions == 2)
1262 os << "\tBoundingBox 0bp 0bp "
1263 << opts["natwidth"] << opts["natheight"] << '\n';
1264 else if (numberOfbbOptions > 0)
1265 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1266 ostringstream special;
1267 if (opts.find("hiresbb") != opts.end())
1268 special << "hiresbb,";
1269 if (opts.find("trim") != opts.end())
1271 if (opts.find("viewport") != opts.end())
1272 special << "viewport=" << opts["viewport"] << ',';
1273 if (opts.find("totalheight") != opts.end())
1274 special << "totalheight=" << opts["totalheight"] << ',';
1275 if (opts.find("type") != opts.end())
1276 special << "type=" << opts["type"] << ',';
1277 if (opts.find("ext") != opts.end())
1278 special << "ext=" << opts["ext"] << ',';
1279 if (opts.find("read") != opts.end())
1280 special << "read=" << opts["read"] << ',';
1281 if (opts.find("command") != opts.end())
1282 special << "command=" << opts["command"] << ',';
1283 string s_special = special.str();
1284 if (!s_special.empty()) {
1285 // We had special arguments. Remove the trailing ','.
1286 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
1288 // TODO: Handle the unknown settings better.
1289 // Warn about invalid options.
1290 // Check whether some option was given twice.
1294 else if (t.cs() == "footnote") {
1296 context.check_layout(os);
1297 begin_inset(os, "Foot\n");
1298 os << "status collapsed\n\n";
1299 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1303 else if (t.cs() == "marginpar") {
1305 context.check_layout(os);
1306 begin_inset(os, "Marginal\n");
1307 os << "status collapsed\n\n";
1308 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1312 else if (t.cs() == "ensuremath") {
1314 context.check_layout(os);
1315 Context newcontext(false, context.textclass);
1316 newcontext.font = context.font;
1317 string s = parse_text(p, FLAG_ITEM, false, newcontext);
1318 if (s == "±" || s == "³" || s == "²" || s == "µ")
1321 handle_ert(os, "\\ensuremath{" + s + "}",
1325 else if (t.cs() == "hfill") {
1326 context.check_layout(os);
1327 os << "\n\\hfill\n";
1332 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
1334 skip_braces(p); // swallow this
1337 else if (t.cs() == "tableofcontents") {
1339 context.check_layout(os);
1340 begin_inset(os, "LatexCommand \\tableofcontents\n");
1342 skip_braces(p); // swallow this
1345 else if (t.cs() == "listoffigures") {
1347 context.check_layout(os);
1348 begin_inset(os, "FloatList figure\n");
1350 skip_braces(p); // swallow this
1353 else if (t.cs() == "listoftables") {
1355 context.check_layout(os);
1356 begin_inset(os, "FloatList table\n");
1358 skip_braces(p); // swallow this
1361 else if (t.cs() == "listof") {
1362 p.skip_spaces(true);
1363 string const name = p.get_token().asString();
1364 if (context.textclass.floats().typeExist(name)) {
1365 context.check_layout(os);
1366 begin_inset(os, "FloatList ");
1369 p.get_token(); // swallow second arg
1371 handle_ert(os, "\\listof{" + name + "}", context);
1374 else if (t.cs() == "textrm")
1375 parse_text_attributes(p, os, FLAG_ITEM, outer,
1376 context, "\\family",
1377 context.font.family, "roman");
1379 else if (t.cs() == "textsf")
1380 parse_text_attributes(p, os, FLAG_ITEM, outer,
1381 context, "\\family",
1382 context.font.family, "sans");
1384 else if (t.cs() == "texttt")
1385 parse_text_attributes(p, os, FLAG_ITEM, outer,
1386 context, "\\family",
1387 context.font.family, "typewriter");
1389 else if (t.cs() == "textmd")
1390 parse_text_attributes(p, os, FLAG_ITEM, outer,
1391 context, "\\series",
1392 context.font.series, "medium");
1394 else if (t.cs() == "textbf")
1395 parse_text_attributes(p, os, FLAG_ITEM, outer,
1396 context, "\\series",
1397 context.font.series, "bold");
1399 else if (t.cs() == "textup")
1400 parse_text_attributes(p, os, FLAG_ITEM, outer,
1402 context.font.shape, "up");
1404 else if (t.cs() == "textit")
1405 parse_text_attributes(p, os, FLAG_ITEM, outer,
1407 context.font.shape, "italic");
1409 else if (t.cs() == "textsl")
1410 parse_text_attributes(p, os, FLAG_ITEM, outer,
1412 context.font.shape, "slanted");
1414 else if (t.cs() == "textsc")
1415 parse_text_attributes(p, os, FLAG_ITEM, outer,
1417 context.font.shape, "smallcaps");
1419 else if (t.cs() == "textnormal" || t.cs() == "normalfont") {
1420 context.check_layout(os);
1421 Font oldFont = context.font;
1422 context.font.init();
1423 context.font.size = oldFont.size;
1424 os << "\n\\family " << context.font.family << "\n";
1425 os << "\n\\series " << context.font.series << "\n";
1426 os << "\n\\shape " << context.font.shape << "\n";
1427 if (t.cs() == "textnormal") {
1428 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1429 context.font = oldFont;
1430 os << "\n\\shape " << oldFont.shape << "\n";
1431 os << "\n\\series " << oldFont.series << "\n";
1432 os << "\n\\family " << oldFont.family << "\n";
1434 eat_whitespace(p, os, context, false);
1437 else if (t.cs() == "underbar") {
1438 // Do NOT handle \underline.
1439 // \underbar cuts through y, g, q, p etc.,
1440 // \underline does not.
1441 context.check_layout(os);
1442 os << "\n\\bar under\n";
1443 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1444 os << "\n\\bar default\n";
1447 else if (t.cs() == "emph" || t.cs() == "noun") {
1448 context.check_layout(os);
1449 os << "\n\\" << t.cs() << " on\n";
1450 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1451 os << "\n\\" << t.cs() << " default\n";
1454 else if (use_natbib &&
1455 is_known(t.cs(), known_natbib_commands) &&
1456 ((t.cs() != "citefullauthor" &&
1457 t.cs() != "citeyear" &&
1458 t.cs() != "citeyearpar") ||
1459 p.next_token().asInput() != "*")) {
1460 context.check_layout(os);
1462 // \citet[before][after]{a} \citet[after][before]{a}
1463 // \citet[before][]{a} \citet[][before]{a}
1464 // \citet[after]{a} \citet[after]{a}
1465 // \citet{a} \citet{a}
1466 string command = '\\' + t.cs();
1467 if (p.next_token().asInput() == "*") {
1471 if (command == "\\citefullauthor")
1472 // alternative name for "\\citeauthor*"
1473 command = "\\citeauthor*";
1475 // text before the citation
1477 // text after the citation
1480 boost::tie(before, after) = getCiteArguments(p, true);
1481 if (command == "\\cite") {
1482 // \cite without optional argument means
1483 // \citet, \cite with at least one optional
1484 // argument means \citep.
1485 if (before.empty() && after.empty())
1486 command = "\\citet";
1488 command = "\\citep";
1490 if (before.empty() && after == "[]")
1491 // avoid \citet[]{a}
1493 else if (before == "[]" && after == "[]") {
1494 // avoid \citet[][]{a}
1498 begin_inset(os, "LatexCommand ");
1499 os << command << after << before
1500 << '{' << p.verbatim_item() << "}\n";
1504 else if (use_jurabib &&
1505 is_known(t.cs(), known_jurabib_commands)) {
1506 context.check_layout(os);
1507 string const command = '\\' + t.cs();
1508 char argumentOrder = '\0';
1509 vector<string> const & options = used_packages["jurabib"];
1510 if (std::find(options.begin(), options.end(),
1511 "natbiborder") != options.end())
1512 argumentOrder = 'n';
1513 else if (std::find(options.begin(), options.end(),
1514 "jurabiborder") != options.end())
1515 argumentOrder = 'j';
1517 // text before the citation
1519 // text after the citation
1522 boost::tie(before, after) =
1523 getCiteArguments(p, argumentOrder != 'j');
1524 string const citation = p.verbatim_item();
1525 if (!before.empty() && argumentOrder == '\0') {
1526 cerr << "Warning: Assuming argument order "
1527 << "of jurabib version 0.6 for\n'"
1528 << command << before << after << '{'
1529 << citation << "}'.\n"
1530 << "Add 'jurabiborder' to the jurabib "
1531 << "package options if you used an\n"
1532 << "earlier jurabib version." << endl;
1534 begin_inset(os, "LatexCommand ");
1535 os << command << after << before
1536 << '{' << citation << "}\n";
1540 else if (is_known(t.cs(), known_latex_commands)) {
1541 // This needs to be after the check for natbib and
1542 // jurabib commands, because "cite" has different
1543 // arguments with natbib and jurabib.
1544 context.check_layout(os);
1545 begin_inset(os, "LatexCommand ");
1546 os << '\\' << t.cs();
1547 // lyx cannot handle newlines in a latex command
1548 // FIXME: Move the substitution into parser::getOpt()?
1549 os << subst(p.getOpt(), "\n", " ");
1550 os << subst(p.getOpt(), "\n", " ");
1551 os << '{' << subst(p.verbatim_item(), "\n", " ") << "}\n";
1555 else if (is_known(t.cs(), known_quotes)) {
1556 char const * const * where = is_known(t.cs(), known_quotes);
1557 context.check_layout(os);
1558 begin_inset(os, "Quotes ");
1559 os << known_coded_quotes[where - known_quotes];
1561 // LyX adds {} after the quote, so we have to eat
1562 // spaces here if there are any before a possible
1564 eat_whitespace(p, os, context, false);
1568 else if (is_known(t.cs(), known_sizes)) {
1569 char const * const * where = is_known(t.cs(), known_sizes);
1570 context.check_layout(os);
1571 context.font.size = known_coded_sizes[where - known_sizes];
1572 os << "\n\\size " << context.font.size << '\n';
1573 eat_whitespace(p, os, context, false);
1576 else if (is_known(t.cs(), known_font_families)) {
1577 char const * const * where =
1578 is_known(t.cs(), known_font_families);
1579 context.check_layout(os);
1580 context.font.family =
1581 known_coded_font_families[where - known_font_families];
1582 os << "\n\\family " << context.font.family << '\n';
1583 eat_whitespace(p, os, context, false);
1586 else if (is_known(t.cs(), known_font_series)) {
1587 char const * const * where =
1588 is_known(t.cs(), known_font_series);
1589 context.check_layout(os);
1590 context.font.series =
1591 known_coded_font_series[where - known_font_series];
1592 os << "\n\\series " << context.font.series << '\n';
1593 eat_whitespace(p, os, context, false);
1596 else if (is_known(t.cs(), known_font_shapes)) {
1597 char const * const * where =
1598 is_known(t.cs(), known_font_shapes);
1599 context.check_layout(os);
1600 context.font.shape =
1601 known_coded_font_shapes[where - known_font_shapes];
1602 os << "\n\\shape " << context.font.shape << '\n';
1603 eat_whitespace(p, os, context, false);
1605 else if (is_known(t.cs(), known_old_font_families)) {
1606 char const * const * where =
1607 is_known(t.cs(), known_old_font_families);
1608 context.check_layout(os);
1609 string oldsize = context.font.size;
1610 context.font.init();
1611 context.font.size = oldsize;
1612 context.font.family =
1613 known_coded_font_families[where - known_old_font_families];
1614 os << "\n\\family " << context.font.family << "\n"
1615 << "\\series " << context.font.series << "\n"
1616 << "\\shape " << context.font.shape << "\n";
1617 eat_whitespace(p, os, context, false);
1620 else if (is_known(t.cs(), known_old_font_series)) {
1621 char const * const * where =
1622 is_known(t.cs(), known_old_font_series);
1623 context.check_layout(os);
1624 string oldsize = context.font.size;
1625 context.font.init();
1626 context.font.size = oldsize;
1627 context.font.series =
1628 known_coded_font_series[where - known_old_font_series];
1629 os << "\n\\family " << context.font.family << "\n"
1630 << "\\series " << context.font.series << "\n"
1631 << "\\shape " << context.font.shape << "\n";
1632 eat_whitespace(p, os, context, false);
1635 else if (is_known(t.cs(), known_old_font_shapes)) {
1636 char const * const * where =
1637 is_known(t.cs(), known_old_font_shapes);
1638 context.check_layout(os);
1639 string oldsize = context.font.size;
1640 context.font.init();
1641 context.font.size = oldsize;
1642 context.font.shape =
1643 known_coded_font_shapes[where - known_old_font_shapes];
1644 os << "\n\\family " << context.font.family << "\n"
1645 << "\\series " << context.font.series << "\n"
1646 << "\\shape " << context.font.shape << "\n";
1647 eat_whitespace(p, os, context, false);
1650 else if (t.cs() == "LyX" || t.cs() == "TeX"
1651 || t.cs() == "LaTeX") {
1652 context.check_layout(os);
1654 skip_braces(p); // eat {}
1657 else if (t.cs() == "LaTeXe") {
1658 context.check_layout(os);
1660 skip_braces(p); // eat {}
1663 else if (t.cs() == "ldots") {
1664 context.check_layout(os);
1666 os << "\\SpecialChar \\ldots{}\n";
1669 else if (t.cs() == "lyxarrow") {
1670 context.check_layout(os);
1671 os << "\\SpecialChar \\menuseparator\n";
1675 else if (t.cs() == "textcompwordmark") {
1676 context.check_layout(os);
1677 os << "\\SpecialChar \\textcompwordmark{}\n";
1681 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
1682 context.check_layout(os);
1683 os << "\\SpecialChar \\@.\n";
1687 else if (t.cs() == "-") {
1688 context.check_layout(os);
1689 os << "\\SpecialChar \\-\n";
1692 else if (t.cs() == "textasciitilde") {
1693 context.check_layout(os);
1698 else if (t.cs() == "textasciicircum") {
1699 context.check_layout(os);
1704 else if (t.cs() == "textbackslash") {
1705 context.check_layout(os);
1706 os << "\n\\backslash\n";
1710 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
1711 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
1713 context.check_layout(os);
1717 else if (t.cs() == "char") {
1718 context.check_layout(os);
1719 if (p.next_token().character() == '`') {
1721 if (p.next_token().cs() == "\"") {
1726 handle_ert(os, "\\char`", context);
1729 handle_ert(os, "\\char", context);
1733 else if (t.cs() == "\"") {
1734 context.check_layout(os);
1735 string const name = p.verbatim_item();
1736 if (name == "a") os << 'ä';
1737 else if (name == "o") os << 'ö';
1738 else if (name == "u") os << 'ü';
1739 else if (name == "A") os << 'Ä';
1740 else if (name == "O") os << 'Ö';
1741 else if (name == "U") os << 'Ü';
1742 else handle_ert(os, "\"{" + name + "}", context);
1745 // Problem: \= creates a tabstop inside the tabbing environment
1746 // and else an accent. In the latter case we really would want
1747 // \={o} instead of \= o.
1748 else if (t.cs() == "=" && (flags & FLAG_TABBING))
1749 handle_ert(os, t.asInput(), context);
1751 else if (t.cs() == "H" || t.cs() == "c" || t.cs() == "^" || t.cs() == "'"
1752 || t.cs() == "~" || t.cs() == "." || t.cs() == "=") {
1753 // we need the trim as the LyX parser chokes on such spaces
1754 context.check_layout(os);
1755 os << "\n\\i \\" << t.cs() << "{"
1756 << trim(parse_text(p, FLAG_ITEM, outer, context), " ") << "}\n";
1759 else if (t.cs() == "ss") {
1760 context.check_layout(os);
1764 else if (t.cs() == "i" || t.cs() == "j") {
1765 context.check_layout(os);
1766 os << "\\" << t.cs() << ' ';
1769 else if (t.cs() == "\\") {
1770 context.check_layout(os);
1771 string const next = p.next_token().asInput();
1773 handle_ert(os, "\\\\" + p.getOpt(), context);
1774 else if (next == "*") {
1776 handle_ert(os, "\\\\*" + p.getOpt(), context);
1779 os << "\n\\newline\n";
1783 else if (t.cs() == "input" || t.cs() == "include"
1784 || t.cs() == "verbatiminput") {
1785 string name = '\\' + t.cs();
1786 if (t.cs() == "verbatiminput"
1787 && p.next_token().asInput() == "*")
1788 name += p.get_token().asInput();
1789 context.check_layout(os);
1790 begin_inset(os, "Include ");
1791 string filename(p.getArg('{', '}'));
1792 string lyxname(lyx::support::ChangeExtension(filename, ".lyx"));
1793 if (tex2lyx(filename, lyxname)) {
1794 os << name << '{' << lyxname << "}\n";
1796 os << name << '{' << filename << "}\n";
1798 os << "preview false\n";
1802 else if (t.cs() == "fancyhead") {
1803 context.check_layout(os);
1805 ss << "\\fancyhead";
1807 ss << '{' << p.verbatim_item() << "}\n";
1808 handle_ert(os, ss.str(), context);
1811 else if (t.cs() == "bibliographystyle") {
1812 // store new bibliographystyle
1813 bibliographystyle = p.verbatim_item();
1814 // output new bibliographystyle.
1815 // This is only necessary if used in some other macro than \bibliography.
1816 handle_ert(os, "\\bibliographystyle{" + bibliographystyle + "}", context);
1819 else if (t.cs() == "bibliography") {
1820 context.check_layout(os);
1821 begin_inset(os, "LatexCommand ");
1823 // Do we have a bibliographystyle set?
1824 if (!bibliographystyle.empty()) {
1825 os << '[' << bibliographystyle << ']';
1827 os << '{' << p.verbatim_item() << "}\n";
1831 else if (t.cs() == "parbox")
1832 parse_box(p, os, FLAG_ITEM, outer, context, true);
1834 else if (t.cs() == "smallskip" ||
1835 t.cs() == "medskip" ||
1836 t.cs() == "bigskip" ||
1837 t.cs() == "vfill") {
1838 context.check_layout(os);
1839 begin_inset(os, "VSpace ");
1845 else if (t.cs() == "newcommand" ||
1846 t.cs() == "providecommand" ||
1847 t.cs() == "renewcommand") {
1848 // these could be handled by parse_command(), but
1849 // we need to call add_known_command() here.
1850 string name = t.asInput();
1851 if (p.next_token().asInput() == "*") {
1852 // Starred form. Eat '*'
1856 string const command = p.verbatim_item();
1857 string const opt1 = p.getOpt();
1858 string const opt2 = p.getFullOpt();
1859 add_known_command(command, opt1, !opt2.empty());
1860 string const ert = name + '{' + command + '}' +
1862 '{' + p.verbatim_item() + '}';
1863 handle_ert(os, ert, context);
1866 else if (t.cs() == "vspace") {
1867 bool starred = false;
1868 if (p.next_token().asInput() == "*") {
1872 string const length = p.verbatim_item();
1875 bool valid = splitLatexLength(length, valstring, unit);
1876 bool known_vspace = false;
1877 bool known_unit = false;
1880 istringstream iss(valstring);
1883 if (unit == "\\smallskipamount") {
1885 known_vspace = true;
1886 } else if (unit == "\\medskipamount") {
1888 known_vspace = true;
1889 } else if (unit == "\\bigskipamount") {
1891 known_vspace = true;
1892 } else if (unit == "\\fill") {
1894 known_vspace = true;
1897 if (!known_vspace) {
1898 switch (unitFromString(unit)) {
1919 if (known_unit || known_vspace) {
1920 // Literal length or known variable
1921 context.check_layout(os);
1922 begin_inset(os, "VSpace ");
1930 // LyX can't handle other length variables in Inset VSpace
1931 string name = t.asInput();
1936 handle_ert(os, name + '{' + unit + '}', context);
1937 else if (value == -1.0)
1938 handle_ert(os, name + "{-" + unit + '}', context);
1940 handle_ert(os, name + '{' + valstring + unit + '}', context);
1942 handle_ert(os, name + '{' + length + '}', context);
1947 //cerr << "#: " << t << " mode: " << mode << endl;
1948 // heuristic: read up to next non-nested space
1950 string s = t.asInput();
1951 string z = p.verbatim_item();
1952 while (p.good() && z != " " && z.size()) {
1953 //cerr << "read: " << z << endl;
1955 z = p.verbatim_item();
1957 cerr << "found ERT: " << s << endl;
1958 handle_ert(os, s + ' ', context);
1960 string name = t.asInput();
1961 if (p.next_token().asInput() == "*") {
1962 // Starred commands like \vspace*{}
1963 p.get_token(); // Eat '*'
1966 if (! parse_command(name, p, os, outer, context))
1967 handle_ert(os, name, context);
1970 if (flags & FLAG_LEAVE) {
1971 flags &= ~FLAG_LEAVE;