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 "support/lstrings.h"
20 #include "support/tostr.h"
21 #include "support/filetools.h"
33 using std::ostringstream;
34 using std::istringstream;
38 using lyx::support::rtrim;
39 using lyx::support::suffixIs;
42 // thin wrapper around parse_text using a string
43 string parse_text(Parser & p, unsigned flags, const bool outer,
47 parse_text(p, os, flags, outer, context);
51 // parses a subdocument, usually useful in insets (whence the name)
52 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
55 Context newcontext(true, context.textclass);
56 parse_text(p, os, flags, outer, newcontext);
57 newcontext.check_end_layout(os);
61 // parses a paragraph snippet, useful for example for \emph{...}
62 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
65 Context newcontext(false, context.textclass);
66 parse_text(p, os, flags, outer, newcontext);
67 // should not be needed
68 newcontext.check_end_layout(os);
74 char const * known_latex_commands[] = { "ref", "cite", "label", "index",
75 "printindex", "pageref", "url", "vref", "vpageref", "prettyref", "eqref", 0 };
77 // LaTeX names for quotes
78 char const * known_quotes[] = { "glqq", "grqq", "quotedblbase",
79 "textquotedblleft", "quotesinglbase", "guilsinglleft", "guilsinglright", 0};
81 // the same as known_quotes with .lyx names
82 char const * known_coded_quotes[] = { "gld", "grd", "gld",
83 "grd", "gls", "fls", "frd", 0};
85 char const * known_sizes[] = { "tiny", "scriptsize", "footnotesize",
86 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
88 char const * known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
89 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
91 // splits "x=z, y=b" into a map
92 map<string, string> split_map(string const & s)
94 map<string, string> res;
97 for (size_t i = 0; i < v.size(); ++i) {
98 size_t const pos = v[i].find('=');
99 string const index = v[i].substr(0, pos);
100 string const value = v[i].substr(pos + 1, string::npos);
101 res[trim(index)] = trim(value);
106 // A simple function to translate a latex length to something lyx can
107 // understand. Not perfect, but rather best-effort.
108 string translate_len(string const & len)
110 const string::size_type i = len.find_first_not_of(" -0123456789.,");
111 //'4,5' is a valid LaTeX length number. Change it to '4.5'
112 string const length = lyx::support::subst(len, ',', '.');
114 if (i == string::npos || len[i] != '\\')
118 // We had something like \textwidth without a factor
121 istringstream iss(string(length, 0, i));
127 string const valstring = oss.str();
128 const string::size_type i2 = length.find(" ", i);
129 string const unit = string(len, i, i2 - i);
130 string const endlen = (i2 == string::npos) ? string() : string(len, i2);
131 if (unit == "\\textwidth")
132 return valstring + "text%" + endlen;
133 else if (unit == "\\columnwidth")
134 return valstring + "col%" + endlen;
135 else if (unit == "\\paperwidth")
136 return valstring + "page%" + endlen;
137 else if (unit == "\\linewidth")
138 return valstring + "line%" + endlen;
139 else if (unit == "\\paperheight")
140 return valstring + "pheight%" + endlen;
141 else if (unit == "\\textheight")
142 return valstring + "theight%" + endlen;
148 void begin_inset(ostream & os, string const & name)
150 os << "\n\\begin_inset " << name;
154 void end_inset(ostream & os)
156 os << "\n\\end_inset \n\n";
160 void skip_braces(Parser & p)
162 if (p.next_token().cat() != catBegin)
165 if (p.next_token().cat() == catEnd) {
173 void handle_ert(ostream & os, string const & s, Context const & context)
175 Context newcontext(true, context.textclass);
176 begin_inset(os, "ERT");
177 os << "\nstatus Collapsed\n";
178 newcontext.check_layout(os);
179 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
181 os << "\n\\backslash \n";
185 newcontext.check_end_layout(os);
191 isLayout(string const name) : name_(name) {}
192 bool operator()(LyXLayout_ptr const & ptr) {
193 return ptr.get() && ptr->latexname() == name_;
200 LyXLayout_ptr findLayout(LyXTextClass const & textclass,
203 LyXTextClass::const_iterator it = textclass.begin();
204 LyXTextClass::const_iterator end = textclass.end();
205 it = std::find_if(it, end, isLayout(name));
206 return (it == end) ? LyXLayout_ptr() : *it;
210 void output_command_layout(ostream & os, Parser & p, bool outer,
211 Context & parent_context,
212 LyXLayout_ptr newlayout)
214 parent_context.check_end_layout(os);
215 Context context(true, parent_context.textclass, newlayout,
216 parent_context.layout);
217 context.check_deeper(os);
218 context.check_layout(os);
219 if (context.layout->optionalargs > 0) {
220 if (p.next_token().character() == '[') {
221 p.get_token(); // eat '['
222 begin_inset(os, "OptArg\n");
223 os << "collapsed true\n";
224 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
228 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
229 context.check_end_layout(os);
230 context.check_end_deeper(os);
234 void parse_environment(Parser & p, ostream & os, bool outer,
235 Context & parent_context)
237 LyXLayout_ptr newlayout;
238 string const name = p.getArg('{', '}');
239 const bool is_starred = suffixIs(name, '*');
240 string const unstarred_name = rtrim(name, "*");
241 active_environments.push_back(name);
242 if (is_math_env(name)) {
243 parent_context.check_layout(os);
244 begin_inset(os, "Formula ");
245 os << "\\begin{" << name << "}";
246 parse_math(p, os, FLAG_END, MATH_MODE);
247 os << "\\end{" << name << "}";
251 else if (name == "tabular") {
252 parent_context.check_layout(os);
253 begin_inset(os, "Tabular ");
254 handle_tabular(p, os, parent_context);
258 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
259 parent_context.check_layout(os);
260 begin_inset(os, "Float " + unstarred_name + "\n");
261 if (p.next_token().asInput() == "[") {
262 os << "placement " << p.getArg('[', ']') << '\n';
264 os << "wide " << tostr(is_starred)
265 << "\ncollapsed false\n";
266 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
270 else if (name == "minipage") {
271 parent_context.check_layout(os);
272 string position = "1";
273 string inner_pos = "0";
274 string height = "0pt";
275 string latex_position;
276 string latex_inner_pos;
278 if (p.next_token().asInput() == "[") {
279 latex_position = p.getArg('[', ']');
280 switch(latex_position[0]) {
281 case 't': position = "0"; break;
282 case 'c': position = "1"; break;
283 case 'b': position = "2"; break;
285 cerr << "invalid position for minipage"
289 if (p.next_token().asInput() == "[") {
290 latex_height = p.getArg('[', ']');
291 height = translate_len(latex_height);
293 if (p.next_token().asInput() == "[") {
294 latex_inner_pos = p.getArg('[', ']');
295 switch(latex_inner_pos[0]) {
296 case 't': inner_pos = "0"; break;
297 case 'c': inner_pos = "1"; break;
298 case 'b': inner_pos = "2"; break;
299 case 's': inner_pos = "3"; break;
301 cerr << "invalid inner_pos for minipage"
308 string width = translate_len(p.verbatim_item());
309 if (width[0] == '\\') {
310 // lyx can't handle length variables
312 ss << "\\begin{minipage}";
313 if (latex_position.size())
314 ss << '[' << latex_position << ']';
315 if (latex_height.size())
316 ss << '[' << latex_height << ']';
317 if (latex_inner_pos.size())
318 ss << '[' << latex_inner_pos << ']';
319 ss << "{" << width << "}";
320 handle_ert(os, ss.str(), parent_context);
321 parent_context.check_end_layout(os);
322 parent_context.need_layout = true;
323 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
324 handle_ert(os, "\\end{minipage}", parent_context);
326 begin_inset(os, "Minipage\n");
327 os << "position " << position << '\n';
328 os << "inner_position " << inner_pos << '\n';
329 os << "height \"" << height << "\"\n";
330 os << "width \"" << width << "\"\n";
331 os << "collapsed false\n\n";
332 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
338 else if (name == "center") {
339 parse_text(p, os, FLAG_END, outer, parent_context);
342 // The single '=' is meant here.
343 else if ((newlayout = findLayout(parent_context.textclass, name)).get() &&
344 newlayout->isEnvironment()) {
345 Context context(true, parent_context.textclass, newlayout,
346 parent_context.layout);
347 parent_context.check_end_layout(os);
348 switch (context.layout->latextype) {
349 case LATEX_LIST_ENVIRONMENT:
350 context.extra_stuff = "\\labelwidthstring "
351 + p.verbatim_item() + '\n';
353 case LATEX_BIB_ENVIRONMENT:
354 p.verbatim_item(); // swallow next arg
359 context.check_deeper(os);
360 parse_text(p, os, FLAG_END, outer, context);
361 context.check_end_layout(os);
362 context.check_end_deeper(os);
365 else if (name == "appendix") {
366 // This is no good latex style, but it works and is used in some documents...
367 parent_context.check_end_layout(os);
368 Context context(true, parent_context.textclass, parent_context.layout,
369 parent_context.layout);
370 context.check_layout(os);
371 os << "\\start_of_appendix\n";
372 parse_text(p, os, FLAG_END, outer, context);
373 context.check_end_layout(os);
376 else if (name == "comment") {
377 parent_context.check_layout(os);
378 begin_inset(os, "Comment\n");
379 os << "collapsed false\n";
380 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
384 else if (name == "tabbing") {
385 // We need to remember that we have to handle '\=' specially
386 parent_context.check_layout(os);
387 handle_ert(os, "\\begin{" + name + "}", parent_context);
388 parse_text_snippet(p, os, FLAG_END | FLAG_TABBING, outer, parent_context);
389 handle_ert(os, "\\end{" + name + "}", parent_context);
393 parent_context.check_layout(os);
394 handle_ert(os, "\\begin{" + name + "}", parent_context);
395 parse_text_snippet(p, os, FLAG_END, outer, parent_context);
396 handle_ert(os, "\\end{" + name + "}", parent_context);
398 active_environments.pop_back();
401 } // anonymous namespace
406 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
409 LyXLayout_ptr newlayout;
410 // Store the latest bibliographystyle (needed for bibtex inset)
411 string bibliographystyle;
413 Token const & t = p.get_token();
416 cerr << "t: " << t << " flags: " << flags << "\n";
419 if (flags & FLAG_ITEM) {
420 if (t.cat() == catSpace)
424 if (t.cat() == catBegin) {
425 // skip the brace and collect everything to the next matching
427 flags |= FLAG_BRACE_LAST;
431 // handle only this single token, leave the loop if done
435 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
441 if (t.cat() == catMath) {
442 // we are inside some text mode thingy, so opening new math is allowed
443 context.check_layout(os);
444 begin_inset(os, "Formula ");
445 Token const & n = p.get_token();
446 if (n.cat() == catMath && outer) {
447 // TeX's $$...$$ syntax for displayed math
449 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
451 p.get_token(); // skip the second '$' token
453 // simple $...$ stuff
456 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
462 else if (t.cat() == catSuper || t.cat() == catSub)
463 cerr << "catcode " << t << " illegal in text mode\n";
465 // Basic support for english quotes. This should be
466 // extended to other quotes, but is not so easy (a
467 // left english quote is the same as a right german
469 else if (t.asInput() == "`"
470 && p.next_token().asInput() == "`") {
471 context.check_layout(os);
472 begin_inset(os, "Quotes ");
478 else if (t.asInput() == "'"
479 && p.next_token().asInput() == "'") {
480 context.check_layout(os);
481 begin_inset(os, "Quotes ");
489 else if (t.cat() == catLetter ||
490 t.cat() == catSpace ||
491 t.cat() == catOther ||
492 t.cat() == catAlign ||
493 t.cat() == catParameter) {
494 context.check_layout(os);
498 else if (t.cat() == catNewline) {
499 if (p.next_token().cat() == catNewline) {
500 // this should have been be done by
501 // the parser already
502 cerr << "what are we doing here?" << endl;
504 context.need_layout = true;
506 os << " "; // note the space
510 else if (t.cat() == catActive) {
511 context.check_layout(os);
512 if (t.character() == '~') {
513 if (context.layout->free_spacing)
516 os << "\\InsetSpace ~\n";
521 else if (t.cat() == catBegin) {
523 // special handling of size changes
524 context.check_layout(os);
525 bool const is_size = is_known(p.next_token().cs(), known_sizes);
526 Context newcontext(false, context.textclass);
527 // need_end_layout = false;
528 string const s = parse_text(p, FLAG_BRACE_LAST, outer, newcontext);
529 // need_end_layout = true;
530 if (s.empty() && p.next_token().character() == '`')
531 ; // ignore it in {}``
532 else if (is_size || s == "[" || s == "]" || s == "*")
535 handle_ert(os, "{", context);
537 handle_ert(os, "}", context);
541 else if (t.cat() == catEnd) {
542 if (flags & FLAG_BRACE_LAST) {
543 context.check_end_layout(os);
546 cerr << "stray '}' in text\n";
547 handle_ert(os, "}", context);
550 else if (t.cat() == catComment)
557 else if (t.cs() == "(") {
558 context.check_layout(os);
559 begin_inset(os, "Formula");
561 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
566 else if (t.cs() == "[") {
567 context.check_layout(os);
568 begin_inset(os, "Formula");
570 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
575 else if (t.cs() == "begin")
576 parse_environment(p, os, outer, context);
578 else if (t.cs() == "end") {
579 if (flags & FLAG_END) {
580 // eat environment name
581 string const name = p.getArg('{', '}');
582 if (name != active_environment())
583 cerr << "\\end{" + name + "} does not match \\begin{"
584 + active_environment() + "}\n";
587 p.error("found 'end' unexpectedly");
590 else if (t.cs() == "item") {
591 // should be done automatically by Parser::tokenize
595 if (p.next_token().character() == '[') {
596 p.get_token(); // eat '['
597 Context newcontext(false, context.textclass);
598 s = parse_text(p, FLAG_BRACK_LAST, outer, newcontext);
601 context.need_layout = true;
602 context.has_item = true;
603 context.check_layout(os);
605 if (active_environment() == "itemize") {
606 // lyx does not support \item[\mybullet] in itemize environments
607 handle_ert(os, "[", context);
609 handle_ert(os, "]", context);
610 } else if (s.size()) {
611 // The space is needed to separate the item from the rest of the sentence.
617 else if (t.cs() == "bibitem") {
618 context.need_layout = true;
619 context.has_item = true;
620 context.check_layout(os);
623 os << '{' << p.verbatim_item() << '}' << "\n";
626 else if (t.cs() == "def") {
627 context.check_layout(os);
628 string name = p.get_token().cs();
629 while (p.next_token().cat() != catBegin)
630 name += p.get_token().asString();
631 handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}', context);
634 else if (t.cs() == "par") {
636 context.check_end_layout(os);
637 context.need_layout = true;
640 else if (t.cs() == "appendix") {
641 context.check_end_layout(os);
642 Context newcontext(true, context.textclass, context.layout,
644 newcontext.check_layout(os);
645 os << "\\start_of_appendix\n";
646 parse_text(p, os, FLAG_END, outer, newcontext);
647 newcontext.check_end_layout(os);
650 // Must attempt to parse "Section*" before "Section".
651 else if ((p.next_token().asInput() == "*") &&
652 // The single '=' is meant here.
653 (newlayout = findLayout(context.textclass,
654 t.cs() + '*')).get() &&
655 newlayout->isCommand()) {
657 output_command_layout(os, p, outer, context, newlayout);
660 // The single '=' is meant here.
661 else if ((newlayout = findLayout(context.textclass, t.cs())).get() &&
662 newlayout->isCommand()) {
663 output_command_layout(os, p, outer, context, newlayout);
666 else if (t.cs() == "includegraphics") {
667 map<string, string> opts = split_map(p.getArg('[', ']'));
668 string name = p.verbatim_item();
670 context.check_layout(os);
671 begin_inset(os, "Graphics ");
672 os << "\n\tfilename " << name << '\n';
673 if (opts.find("width") != opts.end())
675 << translate_len(opts["width"]) << '\n';
676 if (opts.find("height") != opts.end())
678 << translate_len(opts["height"]) << '\n';
679 if (opts.find("scale") != opts.end()) {
680 istringstream iss(opts["scale"]);
684 os << "\tscale " << val << '\n';
686 if (opts.find("angle") != opts.end())
687 os << "\trotateAngle "
688 << opts["angle"] << '\n';
689 if (opts.find("origin") != opts.end()) {
691 string const opt = opts["origin"];
692 if (opt.find('l') != string::npos) ss << "left";
693 if (opt.find('r') != string::npos) ss << "right";
694 if (opt.find('c') != string::npos) ss << "center";
695 if (opt.find('t') != string::npos) ss << "Top";
696 if (opt.find('b') != string::npos) ss << "Bottom";
697 if (opt.find('B') != string::npos) ss << "Baseline";
699 os << "\trotateOrigin " << ss.str() << '\n';
701 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
703 if (opts.find("keepaspectratio") != opts.end())
704 os << "\tkeepAspectRatio\n";
705 if (opts.find("clip") != opts.end())
707 if (opts.find("draft") != opts.end())
709 if (opts.find("bb") != opts.end())
710 os << "\tBoundingBox "
711 << opts["bb"] << '\n';
712 int numberOfbbOptions = 0;
713 if (opts.find("bbllx") != opts.end())
715 if (opts.find("bblly") != opts.end())
717 if (opts.find("bburx") != opts.end())
719 if (opts.find("bbury") != opts.end())
721 if (numberOfbbOptions == 4)
722 os << "\tBoundingBox "
723 << opts["bbllx"] << opts["bblly"]
724 << opts["bburx"] << opts["bbury"] << '\n';
725 else if (numberOfbbOptions > 0)
726 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
727 numberOfbbOptions = 0;
728 if (opts.find("natwidth") != opts.end())
730 if (opts.find("natheight") != opts.end())
732 if (numberOfbbOptions == 2)
733 os << "\tBoundingBox 0bp 0bp "
734 << opts["natwidth"] << opts["natheight"] << '\n';
735 else if (numberOfbbOptions > 0)
736 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
737 ostringstream special;
738 if (opts.find("hiresbb") != opts.end())
739 special << "hiresbb,";
740 if (opts.find("trim") != opts.end())
742 if (opts.find("viewport") != opts.end())
743 special << "viewport=" << opts["viewport"] << ',';
744 if (opts.find("totalheight") != opts.end())
745 special << "totalheight=" << opts["totalheight"] << ',';
746 if (opts.find("type") != opts.end())
747 special << "type=" << opts["type"] << ',';
748 if (opts.find("ext") != opts.end())
749 special << "ext=" << opts["ext"] << ',';
750 if (opts.find("read") != opts.end())
751 special << "read=" << opts["read"] << ',';
752 if (opts.find("command") != opts.end())
753 special << "command=" << opts["command"] << ',';
754 string s_special = special.str();
755 if (s_special.size()) {
756 // We had special arguments. Remove the trailing ','.
757 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
759 // TODO: Handle the unknown settings better.
760 // Warn about invalid options.
761 // Check wether some option was given twice.
765 else if (t.cs() == "footnote") {
766 context.check_layout(os);
767 begin_inset(os, "Foot\n");
768 os << "collapsed true\n";
769 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
773 else if (t.cs() == "marginpar") {
774 context.check_layout(os);
775 begin_inset(os, "Marginal\n");
776 os << "collapsed true\n";
777 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
781 else if (t.cs() == "ensuremath") {
782 context.check_layout(os);
783 Context newcontext(false, context.textclass);
784 string s = parse_text(p, FLAG_ITEM, false, newcontext);
785 if (s == "±" || s == "³" || s == "²" || s == "µ")
788 handle_ert(os, "\\ensuremath{" + s + "}",
792 else if (t.cs() == "hfill") {
793 context.check_layout(os);
798 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
799 skip_braces(p); // swallow this
801 else if (t.cs() == "tableofcontents") {
802 context.check_layout(os);
803 begin_inset(os, "LatexCommand \\tableofcontents\n");
805 skip_braces(p); // swallow this
808 else if (t.cs() == "listoffigures") {
809 context.check_layout(os);
810 begin_inset(os, "FloatList figure\n");
812 skip_braces(p); // swallow this
815 else if (t.cs() == "listoftables") {
816 context.check_layout(os);
817 begin_inset(os, "FloatList table\n");
819 skip_braces(p); // swallow this
822 else if (t.cs() == "listof") {
823 string const name = p.get_token().asString();
824 if (context.textclass.floats().typeExist(name)) {
825 context.check_layout(os);
826 begin_inset(os, "FloatList ");
829 p.get_token(); // swallow second arg
831 handle_ert(os, "\\listof{" + name + "}", context);
834 else if (t.cs() == "textrm") {
835 context.check_layout(os);
836 os << "\n\\family roman \n";
837 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
838 os << "\n\\family default \n";
841 else if (t.cs() == "textsf") {
842 context.check_layout(os);
843 os << "\n\\family sans \n";
844 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
845 os << "\n\\family default \n";
848 else if (t.cs() == "textsl") {
849 context.check_layout(os);
850 os << "\n\\shape slanted \n";
851 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
852 os << "\n\\shape default \n";
855 else if (t.cs() == "texttt") {
856 context.check_layout(os);
857 os << "\n\\family typewriter \n";
858 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
859 os << "\n\\family default \n";
862 else if (t.cs() == "textit") {
863 context.check_layout(os);
864 os << "\n\\shape italic \n";
865 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
866 os << "\n\\shape default \n";
869 else if (t.cs() == "textsc") {
870 context.check_layout(os);
871 os << "\n\\noun on \n";
872 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
873 os << "\n\\noun default \n";
876 else if (t.cs() == "textbf") {
877 context.check_layout(os);
878 os << "\n\\series bold \n";
879 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
880 os << "\n\\series default \n";
883 else if (t.cs() == "underbar") {
884 context.check_layout(os);
885 os << "\n\\bar under \n";
886 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
887 os << "\n\\bar default \n";
890 else if (t.cs() == "emph" || t.cs() == "noun") {
891 context.check_layout(os);
892 os << "\n\\" << t.cs() << " on \n";
893 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
894 os << "\n\\" << t.cs() << " default \n";
897 else if (is_known(t.cs(), known_latex_commands)) {
898 context.check_layout(os);
899 begin_inset(os, "LatexCommand ");
900 os << '\\' << t.cs();
903 os << '{' << p.verbatim_item() << "}\n";
907 else if (is_known(t.cs(), known_quotes)) {
908 char const ** where = is_known(t.cs(), known_quotes);
909 begin_inset(os, "Quotes ");
910 os << known_coded_quotes[where - known_quotes];
915 else if (is_known(t.cs(), known_sizes)) {
916 char const ** where = is_known(t.cs(), known_sizes);
917 context.check_layout(os);
918 os << "\n\\size " << known_coded_sizes[where - known_sizes] << "\n";
921 else if (t.cs() == "LyX" || t.cs() == "TeX"
922 || t.cs() == "LaTeX") {
923 context.check_layout(os);
925 skip_braces(p); // eat {}
928 else if (t.cs() == "LaTeXe") {
929 context.check_layout(os);
931 skip_braces(p); // eat {}
934 else if (t.cs() == "ldots") {
935 context.check_layout(os);
937 os << "\\SpecialChar \\ldots{}\n";
940 else if (t.cs() == "lyxarrow") {
941 context.check_layout(os);
942 os << "\\SpecialChar \\menuseparator\n";
946 else if (t.cs() == "textcompwordmark") {
947 context.check_layout(os);
948 os << "\\SpecialChar \\textcompwordmark{}\n";
952 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
953 context.check_layout(os);
954 os << "\\SpecialChar \\@.\n";
958 else if (t.cs() == "-") {
959 context.check_layout(os);
960 os << "\\SpecialChar \\-\n";
963 else if (t.cs() == "textasciitilde") {
964 context.check_layout(os);
969 else if (t.cs() == "textasciicircum") {
970 context.check_layout(os);
975 else if (t.cs() == "textbackslash") {
976 context.check_layout(os);
977 os << "\n\\backslash \n";
981 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
982 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
984 context.check_layout(os);
988 else if (t.cs() == "char") {
989 context.check_layout(os);
990 if (p.next_token().character() == '`') {
992 if (p.next_token().cs() == "\"") {
997 handle_ert(os, "\\char`", context);
1000 handle_ert(os, "\\char", context);
1004 else if (t.cs() == "\"") {
1005 context.check_layout(os);
1006 string const name = p.verbatim_item();
1007 if (name == "a") os << 'ä';
1008 else if (name == "o") os << 'ö';
1009 else if (name == "u") os << 'ü';
1010 else if (name == "A") os << 'Ä';
1011 else if (name == "O") os << 'Ö';
1012 else if (name == "U") os << 'Ü';
1013 else handle_ert(os, "\"{" + name + "}", context);
1016 // Problem: \= creates a tabstop inside the tabbing environment
1017 // and else an accent. In the latter case we really would want
1018 // \={o} instead of \= o.
1019 else if (t.cs() == "H" || t.cs() == "c" || t.cs() == "^" || t.cs() == "'"
1020 || t.cs() == "~" || t.cs() == "." || (t.cs() == "=" && ! (flags & FLAG_TABBING))) {
1021 // we need the trim as the LyX parser chokes on such spaces
1022 context.check_layout(os);
1023 os << "\n\\i \\" << t.cs() << "{"
1024 << trim(parse_text(p, FLAG_ITEM, outer, context), " ") << "}\n";
1027 else if (t.cs() == "ss") {
1028 context.check_layout(os);
1032 else if (t.cs() == "i" || t.cs() == "j") {
1033 context.check_layout(os);
1034 os << "\\" << t.cs() << ' ';
1037 else if (t.cs() == "\\") {
1038 context.check_layout(os);
1039 string const next = p.next_token().asInput();
1041 handle_ert(os, "\\\\" + p.getOpt(), context);
1042 else if (next == "*") {
1044 handle_ert(os, "\\\\*" + p.getOpt(), context);
1047 os << "\n\\newline \n";
1051 else if (t.cs() == "input" || t.cs() == "include"
1052 || t.cs() == "verbatiminput") {
1053 string name = '\\' + t.cs();
1054 if (t.cs() == "verbatiminput"
1055 && p.next_token().asInput() == "*")
1056 name += p.get_token().asInput();
1057 context.check_layout(os);
1058 begin_inset(os, "Include ");
1059 string filename(p.getArg('{', '}'));
1060 string lyxname(lyx::support::ChangeExtension(filename, ".lyx"));
1061 if (tex2lyx(filename, lyxname)) {
1062 os << name << '{' << lyxname << "}\n";
1064 os << name << '{' << filename << "}\n";
1066 os << "preview false\n";
1070 else if (t.cs() == "fancyhead") {
1071 context.check_layout(os);
1073 ss << "\\fancyhead";
1075 ss << '{' << p.verbatim_item() << "}\n";
1076 handle_ert(os, ss.str(), context);
1079 else if (t.cs() == "bibliographystyle") {
1080 // store new bibliographystyle
1081 bibliographystyle = p.verbatim_item();
1082 // output new bibliographystyle.
1083 // This is only necessary if used in some other macro than \bibliography.
1084 handle_ert(os, "\\bibliographystyle{" + bibliographystyle + "}", context);
1087 else if (t.cs() == "bibliography") {
1088 context.check_layout(os);
1089 begin_inset(os, "LatexCommand ");
1091 // Do we have a bibliographystyle set?
1092 if (bibliographystyle.size()) {
1093 os << '[' << bibliographystyle << ']';
1095 os << '{' << p.verbatim_item() << "}\n";
1099 else if (t.cs() == "psfrag") {
1100 // psfrag{ps-text}[ps-pos][tex-pos]{tex-text}
1101 // TODO: Generalize this!
1102 string arguments = p.getArg('{', '}');
1104 arguments += p.getOpt();
1105 arguments += p.getOpt();
1107 handle_ert(os, "\\psfrag{" + arguments, context);
1111 //cerr << "#: " << t << " mode: " << mode << endl;
1112 // heuristic: read up to next non-nested space
1114 string s = t.asInput();
1115 string z = p.verbatim_item();
1116 while (p.good() && z != " " && z.size()) {
1117 //cerr << "read: " << z << endl;
1119 z = p.verbatim_item();
1121 cerr << "found ERT: " << s << endl;
1122 handle_ert(os, s + ' ', context);
1124 context.check_layout(os);
1125 handle_ert(os, t.asInput() + ' ', context);
1128 if (flags & FLAG_LEAVE) {
1129 flags &= ~FLAG_LEAVE;