1 /**
2 * \file tex2lyx/text.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
5 *
6 * \author André Pönitz
7 * \author Jean-Marc Lasgouttes
8 * \author Uwe Stöhr
9 *
10 * Full author contact details are available in file CREDITS.
11 */
12
13 // {[(
14
15 #include <config.h>
16
17 #include "tex2lyx.h"
18
19 #include "Context.h"
20 #include "Encoding.h"
21 #include "FloatList.h"
22 #include "LaTeXPackages.h"
23 #include "Layout.h"
24 #include "Length.h"
25 #include "Preamble.h"
26
27 #include "insets/ExternalTemplate.h"
28
29 #include "support/lassert.h"
30 #include "support/convert.h"
31 #include "support/FileName.h"
32 #include "support/filetools.h"
33 #include "support/lstrings.h"
34 #include "support/lyxtime.h"
35
36 #include <algorithm>
37 #include <iostream>
38 #include <map>
39 #include <sstream>
40 #include <vector>
41
42 using namespace std;
43 using namespace lyx::support;
44
45 namespace lyx {
46
47
48 namespace {
49
50 void output_arguments(ostream &, Parser &, bool, bool, string, Context &,
51 Layout::LaTeXArgMap const &);
52
53 }
54
55
parse_text_in_inset(Parser & p,ostream & os,unsigned flags,bool outer,Context const & context,InsetLayout const * layout,string const rdelim)56 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
57 Context const & context, InsetLayout const * layout,
58 string const rdelim)
59 {
60 bool const forcePlainLayout =
61 layout ? layout->forcePlainLayout() : false;
62 Context newcontext(true, context.textclass);
63 if (forcePlainLayout)
64 newcontext.layout = &context.textclass.plainLayout();
65 else
66 newcontext.font = context.font;
67 if (layout)
68 output_arguments(os, p, outer, false, string(), newcontext,
69 layout->latexargs());
70 // If we have a latex param, we eat it here.
71 if (!context.latexparam.empty()) {
72 ostringstream oss;
73 Context dummy(true, context.textclass);
74 parse_text(p, oss, FLAG_RDELIM, outer, dummy,
75 string(1, context.latexparam.back()));
76 }
77 parse_text(p, os, flags, outer, newcontext, rdelim);
78 if (layout)
79 output_arguments(os, p, outer, false, "post", newcontext,
80 layout->postcommandargs());
81 newcontext.check_end_layout(os);
82 }
83
84
85 namespace {
86
parse_text_in_inset(Parser & p,ostream & os,unsigned flags,bool outer,Context const & context,string const & name,string const rdelim=string ())87 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
88 Context const & context, string const & name,
89 string const rdelim = string())
90 {
91 InsetLayout const * layout = 0;
92 DocumentClass::InsetLayouts::const_iterator it =
93 context.textclass.insetLayouts().find(from_ascii(name));
94 if (it != context.textclass.insetLayouts().end())
95 layout = &(it->second);
96 parse_text_in_inset(p, os, flags, outer, context, layout, rdelim);
97 }
98
99 /// parses a paragraph snippet, useful for example for \\emph{...}
parse_text_snippet(Parser & p,ostream & os,unsigned flags,bool outer,Context & context)100 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
101 Context & context)
102 {
103 Context newcontext(context);
104 // Don't inherit the paragraph-level extra stuff
105 newcontext.par_extra_stuff.clear();
106 parse_text(p, os, flags, outer, newcontext);
107 // Make sure that we don't create invalid .lyx files
108 context.need_layout = newcontext.need_layout;
109 context.need_end_layout = newcontext.need_end_layout;
110 }
111
112
113 /*!
114 * Thin wrapper around parse_text_snippet() using a string.
115 *
116 * We completely ignore \c context.need_layout and \c context.need_end_layout,
117 * because our return value is not used directly (otherwise the stream version
118 * of parse_text_snippet() could be used). That means that the caller needs
119 * to do layout management manually.
120 * This is intended to parse text that does not create any layout changes.
121 */
parse_text_snippet(Parser & p,unsigned flags,const bool outer,Context & context)122 string parse_text_snippet(Parser & p, unsigned flags, const bool outer,
123 Context & context)
124 {
125 Context newcontext(context);
126 newcontext.need_layout = false;
127 newcontext.need_end_layout = false;
128 newcontext.new_layout_allowed = false;
129 // Avoid warning by Context::~Context()
130 newcontext.par_extra_stuff.clear();
131 ostringstream os;
132 parse_text_snippet(p, os, flags, outer, newcontext);
133 return os.str();
134 }
135
136 string fboxrule = "";
137 string fboxsep = "";
138 string shadow_size = "";
139
140 char const * const known_ref_commands[] = { "ref", "pageref", "vref",
141 "vpageref", "prettyref", "nameref", "eqref", 0 };
142
143 char const * const known_coded_ref_commands[] = { "ref", "pageref", "vref",
144 "vpageref", "formatted", "nameref", "eqref", 0 };
145
146 char const * const known_refstyle_commands[] = { "algref", "chapref", "corref",
147 "eqref", "enuref", "figref", "fnref", "lemref", "parref", "partref", "propref",
148 "secref", "subsecref", "tabref", "thmref", 0 };
149
150 char const * const known_refstyle_prefixes[] = { "alg", "chap", "cor",
151 "eq", "enu", "fig", "fn", "lem", "par", "part", "prop",
152 "sec", "subsec", "tab", "thm", 0 };
153
154
155 /**
156 * supported CJK encodings
157 * JIS does not work with LyX's encoding conversion
158 */
159 const char * const supported_CJK_encodings[] = {
160 "EUC-JP", "KS", "GB", "UTF8",
161 "Bg5", /*"JIS",*/ "SJIS", 0};
162
163 /**
164 * the same as supported_CJK_encodings with their corresponding LyX language name
165 * FIXME: The mapping "UTF8" => "chinese-traditional" is only correct for files
166 * created by LyX.
167 * NOTE: "Bg5", "JIS" and "SJIS" are not supported by LyX, on re-export the
168 * encodings "UTF8", "EUC-JP" and "EUC-JP" will be used.
169 * please keep this in sync with supported_CJK_encodings line by line!
170 */
171 const char * const supported_CJK_languages[] = {
172 "japanese-cjk", "korean", "chinese-simplified", "chinese-traditional",
173 "chinese-traditional", /*"japanese-cjk",*/ "japanese-cjk", 0};
174
175 /*!
176 * natbib commands.
177 * The starred forms are also known except for "citefullauthor",
178 * "citeyear" and "citeyearpar".
179 */
180 char const * const known_natbib_commands[] = { "cite", "citet", "citep",
181 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
182 "citefullauthor", "Citet", "Citep", "Citealt", "Citealp", "Citeauthor", 0 };
183
184 /*!
185 * jurabib commands.
186 * No starred form other than "cite*" known.
187 */
188 char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
189 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
190 // jurabib commands not (yet) supported by LyX:
191 // "fullcite",
192 // "footcite", "footcitet", "footcitep", "footcitealt", "footcitealp",
193 // "footciteauthor", "footciteyear", "footciteyearpar",
194 "citefield", "citetitle", 0 };
195
196 /*!
197 * biblatex commands.
198 * Known starred forms: \cite*, \citeauthor*, \Citeauthor*, \parencite*, \citetitle*.
199 */
200 char const * const known_biblatex_commands[] = { "cite", "Cite", "textcite", "Textcite",
201 "parencite", "Parencite", "citeauthor", "Citeauthor", "citeyear", "smartcite", "Smartcite",
202 "footcite", "Footcite", "autocite", "Autocite", "citetitle", "fullcite", "footfullcite",
203 "supercite", "cites", "Cites", "textcites", "Textcites", "parencites", "Parencites",
204 "smartcites", "Smartcites", "autocites", "Autocites", 0 };
205
206 // Whether we need to insert a bibtex inset in a comment
207 bool need_commentbib = false;
208
209 /// LaTeX names for quotes
210 char const * const known_quotes[] = { "dq", "guillemotleft", "flqq", "og",
211 "guillemotright", "frqq", "fg", "glq", "glqq", "textquoteleft", "grq", "grqq",
212 "quotedblbase", "textquotedblleft", "quotesinglbase", "textquoteright", "flq",
213 "guilsinglleft", "frq", "guilsinglright", "textquotedblright", "textquotesingle",
214 "textquotedbl", 0};
215
216 /// the same as known_quotes with .lyx names
217 char const * const known_coded_quotes[] = { "qrd", "ard", "ard", "ard",
218 "ald", "ald", "ald", "gls", "gld", "els", "els", "eld",
219 "gld", "eld", "gls", "ers", "ars",
220 "ars", "als", "als", "erd", "qrs", "qrd", 0};
221
222 /// LaTeX names for font sizes
223 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
224 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
225
226 /// the same as known_sizes with .lyx names
227 char const * const known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
228 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
229
230 /// LaTeX 2.09 names for font families
231 char const * const known_old_font_families[] = { "rm", "sf", "tt", 0};
232
233 /// LaTeX names for font families
234 char const * const known_font_families[] = { "rmfamily", "sffamily",
235 "ttfamily", 0};
236
237 /// LaTeX names for font family changing commands
238 char const * const known_text_font_families[] = { "textrm", "textsf",
239 "texttt", 0};
240
241 /// The same as known_old_font_families, known_font_families and
242 /// known_text_font_families with .lyx names
243 char const * const known_coded_font_families[] = { "roman", "sans",
244 "typewriter", 0};
245
246 /// LaTeX 2.09 names for font series
247 char const * const known_old_font_series[] = { "bf", 0};
248
249 /// LaTeX names for font series
250 char const * const known_font_series[] = { "bfseries", "mdseries", 0};
251
252 /// LaTeX names for font series changing commands
253 char const * const known_text_font_series[] = { "textbf", "textmd", 0};
254
255 /// The same as known_old_font_series, known_font_series and
256 /// known_text_font_series with .lyx names
257 char const * const known_coded_font_series[] = { "bold", "medium", 0};
258
259 /// LaTeX 2.09 names for font shapes
260 char const * const known_old_font_shapes[] = { "it", "sl", "sc", 0};
261
262 /// LaTeX names for font shapes
263 char const * const known_font_shapes[] = { "itshape", "slshape", "scshape",
264 "upshape", 0};
265
266 /// LaTeX names for font shape changing commands
267 char const * const known_text_font_shapes[] = { "textit", "textsl", "textsc",
268 "textup", 0};
269
270 /// The same as known_old_font_shapes, known_font_shapes and
271 /// known_text_font_shapes with .lyx names
272 char const * const known_coded_font_shapes[] = { "italic", "slanted",
273 "smallcaps", "up", 0};
274
275 /// Known special characters which need skip_spaces_braces() afterwards
276 char const * const known_special_chars[] = {"ldots",
277 "lyxarrow", "textcompwordmark",
278 "slash", "textasciitilde", "textasciicircum", "textbackslash",
279 "LyX", "TeX", "LaTeXe",
280 "LaTeX", 0};
281
282 /// special characters from known_special_chars which may have a \\protect before
283 char const * const known_special_protect_chars[] = {"LyX", "TeX",
284 "LaTeXe", "LaTeX", 0};
285
286 /// the same as known_special_chars with .lyx names
287 char const * const known_coded_special_chars[] = {"\\SpecialChar ldots\n",
288 "\\SpecialChar menuseparator\n", "\\SpecialChar ligaturebreak\n",
289 "\\SpecialChar breakableslash\n", "~", "^", "\n\\backslash\n",
290 "\\SpecialChar LyX\n", "\\SpecialChar TeX\n", "\\SpecialChar LaTeX2e\n",
291 "\\SpecialChar LaTeX\n", 0};
292
293 /*!
294 * Graphics file extensions known by the dvips driver of the graphics package.
295 * These extensions are used to complete the filename of an included
296 * graphics file if it does not contain an extension.
297 * The order must be the same that latex uses to find a file, because we
298 * will use the first extension that matches.
299 * This is only an approximation for the common cases. If we would want to
300 * do it right in all cases, we would need to know which graphics driver is
301 * used and know the extensions of every driver of the graphics package.
302 */
303 char const * const known_dvips_graphics_formats[] = {"eps", "ps", "eps.gz",
304 "ps.gz", "eps.Z", "ps.Z", 0};
305
306 /*!
307 * Graphics file extensions known by the pdftex driver of the graphics package.
308 * \sa known_dvips_graphics_formats
309 */
310 char const * const known_pdftex_graphics_formats[] = {"png", "pdf", "jpg",
311 "mps", "tif", 0};
312
313 /*!
314 * Known file extensions for TeX files as used by \\include.
315 */
316 char const * const known_tex_extensions[] = {"tex", 0};
317
318 /// spaces known by InsetSpace
319 char const * const known_spaces[] = { " ", "space", ",",
320 "thinspace", "quad", "qquad", "enspace", "enskip",
321 "negthinspace", "negmedspace", "negthickspace", "textvisiblespace",
322 "hfill", "dotfill", "hrulefill", "leftarrowfill", "rightarrowfill",
323 "upbracefill", "downbracefill", 0};
324
325 /// the same as known_spaces with .lyx names
326 char const * const known_coded_spaces[] = { "space{}", "space{}",
327 "thinspace{}", "thinspace{}", "quad{}", "qquad{}", "enspace{}", "enskip{}",
328 "negthinspace{}", "negmedspace{}", "negthickspace{}", "textvisiblespace{}",
329 "hfill{}", "dotfill{}", "hrulefill{}", "leftarrowfill{}", "rightarrowfill{}",
330 "upbracefill{}", "downbracefill{}", 0};
331
332 /// known TIPA combining diacritical marks
333 char const * const known_tipa_marks[] = {"textsubwedge", "textsubumlaut",
334 "textsubtilde", "textseagull", "textsubbridge", "textinvsubbridge",
335 "textsubsquare", "textsubrhalfring", "textsublhalfring", "textsubplus",
336 "textovercross", "textsubarch", "textsuperimposetilde", "textraising",
337 "textlowering", "textadvancing", "textretracting", "textdoublegrave",
338 "texthighrise", "textlowrise", "textrisefall", "textsyllabic",
339 "textsubring", "textsubbar", 0};
340
341 /// TIPA tones that need special handling
342 char const * const known_tones[] = {"15", "51", "45", "12", "454", 0};
343
344 // string to store the float type to be able to determine the type of subfloats
345 string float_type = "";
346
347 // string to store the float status of minted listings
348 string minted_float = "";
349
350 // whether a caption has been parsed for a floating minted listing
351 bool minted_float_has_caption = false;
352
353 // The caption for non-floating minted listings
354 string minted_nonfloat_caption = "";
355
356 // Characters that have to be escaped by \\ in LaTeX
357 char const * const known_escaped_chars[] = {
358 "&", "_", "$", "%", "#", "^", "{", "}", 0};
359
360
361 /// splits "x=z, y=b" into a map and an ordered keyword vector
split_map(string const & s,map<string,string> & res,vector<string> & keys)362 void split_map(string const & s, map<string, string> & res, vector<string> & keys)
363 {
364 vector<string> v;
365 split(s, v);
366 res.clear();
367 keys.resize(v.size());
368 for (size_t i = 0; i < v.size(); ++i) {
369 size_t const pos = v[i].find('=');
370 string const index = trimSpaceAndEol(v[i].substr(0, pos));
371 string const value = trimSpaceAndEol(v[i].substr(pos + 1, string::npos));
372 res[index] = value;
373 keys[i] = index;
374 }
375 }
376
377
378 /*!
379 * Split a LaTeX length into value and unit.
380 * The latter can be a real unit like "pt", or a latex length variable
381 * like "\textwidth". The unit may contain additional stuff like glue
382 * lengths, but we don't care, because such lengths are ERT anyway.
383 * \returns true if \p value and \p unit are valid.
384 */
splitLatexLength(string const & len,string & value,string & unit)385 bool splitLatexLength(string const & len, string & value, string & unit)
386 {
387 if (len.empty())
388 return false;
389 const string::size_type i = len.find_first_not_of(" -+0123456789.,");
390 //'4,5' is a valid LaTeX length number. Change it to '4.5'
391 string const length = subst(len, ',', '.');
392 if (i == string::npos)
393 return false;
394 if (i == 0) {
395 if (len[0] == '\\') {
396 // We had something like \textwidth without a factor
397 value = "1.0";
398 } else {
399 return false;
400 }
401 } else {
402 value = trimSpaceAndEol(string(length, 0, i));
403 }
404 if (value == "-")
405 value = "-1.0";
406 // 'cM' is a valid LaTeX length unit. Change it to 'cm'
407 if (contains(len, '\\'))
408 unit = trimSpaceAndEol(string(len, i));
409 else
410 unit = ascii_lowercase(trimSpaceAndEol(string(len, i)));
411 return true;
412 }
413
414
415 /// A simple function to translate a latex length to something LyX can
416 /// understand. Not perfect, but rather best-effort.
translate_len(string const & length,string & valstring,string & unit)417 bool translate_len(string const & length, string & valstring, string & unit)
418 {
419 if (!splitLatexLength(length, valstring, unit))
420 return false;
421 // LyX uses percent values
422 double value;
423 istringstream iss(valstring);
424 iss >> value;
425 value *= 100;
426 ostringstream oss;
427 oss << value;
428 string const percentval = oss.str();
429 // a normal length
430 if (unit.empty() || unit[0] != '\\')
431 return true;
432 string::size_type const i = unit.find(' ');
433 string const endlen = (i == string::npos) ? string() : string(unit, i);
434 if (unit == "\\textwidth") {
435 valstring = percentval;
436 unit = "text%" + endlen;
437 } else if (unit == "\\columnwidth") {
438 valstring = percentval;
439 unit = "col%" + endlen;
440 } else if (unit == "\\paperwidth") {
441 valstring = percentval;
442 unit = "page%" + endlen;
443 } else if (unit == "\\linewidth") {
444 valstring = percentval;
445 unit = "line%" + endlen;
446 } else if (unit == "\\paperheight") {
447 valstring = percentval;
448 unit = "pheight%" + endlen;
449 } else if (unit == "\\textheight") {
450 valstring = percentval;
451 unit = "theight%" + endlen;
452 } else if (unit == "\\baselineskip") {
453 valstring = percentval;
454 unit = "baselineskip%" + endlen;
455 }
456 return true;
457 }
458
459
460 /// If we have ambiguous quotation marks, make a smart guess
461 /// based on main quote style
guessQuoteStyle(string in,bool const opening)462 string guessQuoteStyle(string in, bool const opening)
463 {
464 string res = in;
465 if (prefixIs(in, "qr")) {// straight quote
466 if (!opening)
467 res = subst(res, "r", "l");
468 } else if (in == "eld") {// ``
469 if (preamble.quotesStyle() == "german")
470 res = "grd";
471 else if (preamble.quotesStyle() == "british")
472 res = "bls";
473 else if (preamble.quotesStyle() == "french")
474 res = "fls";
475 else if (preamble.quotesStyle() == "russian")
476 res = "rrs";
477 } else if (in == "erd") {// ''
478 if (preamble.quotesStyle() == "polish")
479 res = "prd";
480 else if (preamble.quotesStyle() == "british")
481 res = "brs";
482 else if (preamble.quotesStyle() == "french")
483 res = "frs";
484 else if (preamble.quotesStyle() == "swedish")
485 res = opening ? "sld" : "srd";
486 } else if (in == "els") {// `
487 if (preamble.quotesStyle() == "german")
488 res = "grs";
489 else if (preamble.quotesStyle() == "british")
490 res = "bld";
491 } else if (in == "ers") {// '
492 if (preamble.quotesStyle() == "polish")
493 res = "prs";
494 else if (preamble.quotesStyle() == "british")
495 res = "brd";
496 else if (preamble.quotesStyle() == "swedish")
497 res = opening ? "sls" : "srs";
498 } else if (in == "ard") {// >>
499 if (preamble.quotesStyle() == "swiss")
500 res = "cld";
501 else if (preamble.quotesStyle() == "french")
502 res = "fld";
503 else if (preamble.quotesStyle() == "russian")
504 res = "rld";
505 } else if (in == "ald") {// <<
506 if (preamble.quotesStyle() == "swiss")
507 res = "crd";
508 else if (preamble.quotesStyle() == "french")
509 res = "frd";
510 else if (preamble.quotesStyle() == "russian")
511 res = "rrd";
512 } else if (in == "ars") {// >
513 if (preamble.quotesStyle() == "swiss")
514 res = "cls";
515 } else if (in == "als") {// <
516 if (preamble.quotesStyle() == "swiss")
517 res = "crs";
518 } else if (in == "gld") {// ,,
519 if (preamble.quotesStyle() == "polish")
520 res = "pld";
521 else if (preamble.quotesStyle() == "russian")
522 res = "rls";
523 } else if (in == "gls") {// ,
524 if (preamble.quotesStyle() == "polish")
525 res = "pls";
526 }
527 return res;
528 }
529
530
fromPolyglossiaEnvironment(string const s)531 string const fromPolyglossiaEnvironment(string const s)
532 {
533 // Since \arabic is taken by the LaTeX kernel,
534 // the Arabic polyglossia environment is upcased
535 if (s == "Arabic")
536 return "arabic";
537 else
538 return s;
539 }
540
541
uncapitalize(string const s)542 string uncapitalize(string const s)
543 {
544 docstring in = from_ascii(s);
545 char_type t = lowercase(s[0]);
546 in[0] = t;
547 return to_ascii(in);
548 }
549
550
isCapitalized(string const s)551 bool isCapitalized(string const s)
552 {
553 docstring in = from_ascii(s);
554 char_type t = uppercase(s[0]);
555 in[0] = t;
556 return to_ascii(in) == s;
557 }
558
559
560 } // namespace
561
562
translate_len(string const & length)563 string translate_len(string const & length)
564 {
565 string unit;
566 string value;
567 if (translate_len(length, value, unit))
568 return value + unit;
569 // If the input is invalid, return what we have.
570 return length;
571 }
572
573
574 namespace {
575
576 /*!
577 * Translates a LaTeX length into \p value, \p unit and
578 * \p special parts suitable for a box inset.
579 * The difference from translate_len() is that a box inset knows about
580 * some special "units" that are stored in \p special.
581 */
translate_box_len(string const & length,string & value,string & unit,string & special)582 void translate_box_len(string const & length, string & value, string & unit, string & special)
583 {
584 if (translate_len(length, value, unit)) {
585 if (unit == "\\height" || unit == "\\depth" ||
586 unit == "\\totalheight" || unit == "\\width") {
587 special = unit.substr(1);
588 // The unit is not used, but LyX requires a dummy setting
589 unit = "in";
590 } else
591 special = "none";
592 } else {
593 value.clear();
594 unit = length;
595 special = "none";
596 }
597 }
598
599
begin_inset(ostream & os,string const & name)600 void begin_inset(ostream & os, string const & name)
601 {
602 os << "\n\\begin_inset " << name;
603 }
604
605
begin_command_inset(ostream & os,string const & name,string const & latexname)606 void begin_command_inset(ostream & os, string const & name,
607 string const & latexname)
608 {
609 begin_inset(os, "CommandInset ");
610 os << name << "\nLatexCommand " << latexname << '\n';
611 }
612
613
end_inset(ostream & os)614 void end_inset(ostream & os)
615 {
616 os << "\n\\end_inset\n\n";
617 }
618
619
skip_braces(Parser & p)620 bool skip_braces(Parser & p)
621 {
622 if (p.next_token().cat() != catBegin)
623 return false;
624 p.get_token();
625 if (p.next_token().cat() == catEnd) {
626 p.get_token();
627 return true;
628 }
629 p.putback();
630 return false;
631 }
632
633
634 /// replace LaTeX commands in \p s from the unicodesymbols file with their
635 /// unicode points
convert_unicodesymbols(docstring s)636 pair<bool, docstring> convert_unicodesymbols(docstring s)
637 {
638 bool res = true;
639 odocstringstream os;
640 for (size_t i = 0; i < s.size();) {
641 if (s[i] != '\\') {
642 os.put(s[i++]);
643 continue;
644 }
645 s = s.substr(i);
646 bool termination;
647 docstring rem;
648 set<string> req;
649 docstring parsed = normalize_c(encodings.fromLaTeXCommand(s,
650 Encodings::TEXT_CMD, termination, rem, &req));
651 set<string>::const_iterator it = req.begin();
652 set<string>::const_iterator en = req.end();
653 for (; it != en; ++it)
654 preamble.registerAutomaticallyLoadedPackage(*it);
655 os << parsed;
656 s = rem;
657 if (s.empty() || s[0] != '\\')
658 i = 0;
659 else {
660 res = false;
661 for (auto const & c : known_escaped_chars)
662 if (c != 0 && prefixIs(s, from_ascii("\\") + c))
663 res = true;
664 i = 1;
665 }
666 }
667 return make_pair(res, os.str());
668 }
669
670
671 /// try to convert \p s to a valid InsetCommand argument
672 /// return whether this succeeded. If not, these command insets
673 /// get the "literate" flag.
convert_latexed_command_inset_arg(string s)674 pair<bool, string> convert_latexed_command_inset_arg(string s)
675 {
676 bool success = false;
677 if (isAscii(s)) {
678 // since we don't know the input encoding we can't use from_utf8
679 pair<bool, docstring> res = convert_unicodesymbols(from_ascii(s));
680 success = res.first;
681 s = to_utf8(res.second);
682 }
683 // LyX cannot handle newlines in a latex command
684 return make_pair(success, subst(s, "\n", " "));
685 }
686
687 /// try to convert \p s to a valid InsetCommand argument
688 /// without trying to recode macros.
convert_literate_command_inset_arg(string s)689 string convert_literate_command_inset_arg(string s)
690 {
691 // LyX cannot handle newlines in a latex command
692 return subst(s, "\n", " ");
693 }
694
output_ert(ostream & os,string const & s,Context & context)695 void output_ert(ostream & os, string const & s, Context & context)
696 {
697 context.check_layout(os);
698 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
699 if (*it == '\\')
700 os << "\n\\backslash\n";
701 else if (*it == '\n') {
702 context.new_paragraph(os);
703 context.check_layout(os);
704 } else
705 os << *it;
706 }
707 context.check_end_layout(os);
708 }
709
710
output_ert_inset(ostream & os,string const & s,Context & context)711 void output_ert_inset(ostream & os, string const & s, Context & context)
712 {
713 // We must have a valid layout before outputting the ERT inset.
714 context.check_layout(os);
715 Context newcontext(true, context.textclass);
716 InsetLayout const & layout = context.textclass.insetLayout(from_ascii("ERT"));
717 if (layout.forcePlainLayout())
718 newcontext.layout = &context.textclass.plainLayout();
719 begin_inset(os, "ERT");
720 os << "\nstatus collapsed\n";
721 output_ert(os, s, newcontext);
722 end_inset(os);
723 }
724
725
output_comment(Parser & p,ostream & os,string const & s,Context & context)726 void output_comment(Parser & p, ostream & os, string const & s,
727 Context & context)
728 {
729 if (p.next_token().cat() == catNewline)
730 output_ert_inset(os, '%' + s, context);
731 else
732 output_ert_inset(os, '%' + s + '\n', context);
733 }
734
735
findLayout(TextClass const & textclass,string const & name,bool command,string const & latexparam=string ())736 Layout const * findLayout(TextClass const & textclass, string const & name, bool command,
737 string const & latexparam = string())
738 {
739 Layout const * layout = findLayoutWithoutModule(textclass, name, command, latexparam);
740 if (layout)
741 return layout;
742 if (checkModule(name, command))
743 return findLayoutWithoutModule(textclass, name, command, latexparam);
744 return layout;
745 }
746
747
findInsetLayout(TextClass const & textclass,string const & name,bool command,string const & latexparam=string ())748 InsetLayout const * findInsetLayout(TextClass const & textclass, string const & name, bool command,
749 string const & latexparam = string())
750 {
751 InsetLayout const * insetlayout =
752 findInsetLayoutWithoutModule(textclass, name, command, latexparam);
753 if (insetlayout)
754 return insetlayout;
755 if (checkModule(name, command))
756 return findInsetLayoutWithoutModule(textclass, name, command, latexparam);
757 return insetlayout;
758 }
759
760
761 void eat_whitespace(Parser &, ostream &, Context &, bool);
762
763
764 /*!
765 * Skips whitespace and braces.
766 * This should be called after a command has been parsed that is not put into
767 * ERT, and where LyX adds "{}" if needed.
768 */
skip_spaces_braces(Parser & p,bool keepws=false)769 void skip_spaces_braces(Parser & p, bool keepws = false)
770 {
771 /* The following four examples produce the same typeset output and
772 should be handled by this function:
773 - abc \j{} xyz
774 - abc \j {} xyz
775 - abc \j
776 {} xyz
777 - abc \j %comment
778 {} xyz
779 */
780 // Unfortunately we need to skip comments, too.
781 // We can't use eat_whitespace since writing them after the {}
782 // results in different output in some cases.
783 bool const skipped_spaces = p.skip_spaces(true);
784 bool const skipped_braces = skip_braces(p);
785 if (keepws && skipped_spaces && !skipped_braces)
786 // put back the space (it is better handled by check_space)
787 p.unskip_spaces(true);
788 }
789
790
output_arguments(ostream & os,Parser & p,bool outer,bool need_layout,string const prefix,Context & context,Layout::LaTeXArgMap const & latexargs)791 void output_arguments(ostream & os, Parser & p, bool outer, bool need_layout, string const prefix,
792 Context & context, Layout::LaTeXArgMap const & latexargs)
793 {
794 if (context.layout->latextype != LATEX_ITEM_ENVIRONMENT || !prefix.empty()) {
795 if (need_layout) {
796 context.check_layout(os);
797 need_layout = false;
798 } else
799 need_layout = true;
800 }
801 int i = 0;
802 Layout::LaTeXArgMap::const_iterator lait = latexargs.begin();
803 Layout::LaTeXArgMap::const_iterator const laend = latexargs.end();
804 for (; lait != laend; ++lait) {
805 ++i;
806 eat_whitespace(p, os, context, false);
807 if (lait->second.mandatory) {
808 if (p.next_token().cat() != catBegin)
809 break;
810 string ldelim = to_utf8(lait->second.ldelim);
811 string rdelim = to_utf8(lait->second.rdelim);
812 if (ldelim.empty())
813 ldelim = "{";
814 if (rdelim.empty())
815 rdelim = "}";
816 p.get_token(); // eat ldelim
817 if (ldelim.size() > 1)
818 p.get_token(); // eat ldelim
819 if (need_layout) {
820 context.check_layout(os);
821 need_layout = false;
822 }
823 begin_inset(os, "Argument ");
824 if (!prefix.empty())
825 os << prefix << ':';
826 os << i << "\nstatus collapsed\n\n";
827 parse_text_in_inset(p, os, FLAG_RDELIM, outer, context, 0, rdelim);
828 end_inset(os);
829 } else {
830 string ldelim = to_utf8(lait->second.ldelim);
831 string rdelim = to_utf8(lait->second.rdelim);
832 if (ldelim.empty())
833 ldelim = "[";
834 if (rdelim.empty())
835 rdelim = "]";
836 string tok = p.next_token().asInput();
837 // we only support delimiters with max 2 chars for now.
838 if (ldelim.size() > 1)
839 tok += p.next_next_token().asInput();
840 if (p.next_token().cat() == catEscape || tok != ldelim)
841 continue;
842 p.get_token(); // eat ldelim
843 if (ldelim.size() > 1)
844 p.get_token(); // eat ldelim
845 if (need_layout) {
846 context.check_layout(os);
847 need_layout = false;
848 }
849 begin_inset(os, "Argument ");
850 if (!prefix.empty())
851 os << prefix << ':';
852 os << i << "\nstatus collapsed\n\n";
853 parse_text_in_inset(p, os, FLAG_RDELIM, outer, context, 0, rdelim);
854 end_inset(os);
855 }
856 eat_whitespace(p, os, context, false);
857 }
858 }
859
860
output_command_layout(ostream & os,Parser & p,bool outer,Context & parent_context,Layout const * newlayout)861 void output_command_layout(ostream & os, Parser & p, bool outer,
862 Context & parent_context,
863 Layout const * newlayout)
864 {
865 TeXFont const oldFont = parent_context.font;
866 // save the current font size
867 string const size = oldFont.size;
868 // reset the font size to default, because the font size switches
869 // don't affect section headings and the like
870 parent_context.font.size = Context::normalfont.size;
871 // we only need to write the font change if we have an open layout
872 if (!parent_context.atParagraphStart())
873 output_font_change(os, oldFont, parent_context.font);
874 parent_context.check_end_layout(os);
875 Context context(true, parent_context.textclass, newlayout,
876 parent_context.layout, parent_context.font);
877 if (parent_context.deeper_paragraph) {
878 // We are beginning a nested environment after a
879 // deeper paragraph inside the outer list environment.
880 // Therefore we don't need to output a "begin deeper".
881 context.need_end_deeper = true;
882 }
883 context.check_deeper(os);
884 output_arguments(os, p, outer, true, string(), context,
885 context.layout->latexargs());
886 // If we have a latex param, we eat it here.
887 if (!parent_context.latexparam.empty()) {
888 ostringstream oss;
889 Context dummy(true, parent_context.textclass);
890 parse_text(p, oss, FLAG_RDELIM, outer, dummy,
891 string(1, parent_context.latexparam.back()));
892 }
893 parse_text(p, os, FLAG_ITEM, outer, context);
894 output_arguments(os, p, outer, false, "post", context,
895 context.layout->postcommandargs());
896 context.check_end_layout(os);
897 if (parent_context.deeper_paragraph) {
898 // We must suppress the "end deeper" because we
899 // suppressed the "begin deeper" above.
900 context.need_end_deeper = false;
901 }
902 context.check_end_deeper(os);
903 // We don't need really a new paragraph, but
904 // we must make sure that the next item gets a \begin_layout.
905 parent_context.new_paragraph(os);
906 // Set the font size to the original value. No need to output it here
907 // (Context::begin_layout() will do that if needed)
908 parent_context.font.size = size;
909 }
910
911
912 /*!
913 * Output a space if necessary.
914 * This function gets called for every whitespace token.
915 *
916 * We have three cases here:
917 * 1. A space must be suppressed. Example: The lyxcode case below
918 * 2. A space may be suppressed. Example: Spaces before "\par"
919 * 3. A space must not be suppressed. Example: A space between two words
920 *
921 * We currently handle only 1. and 3 and from 2. only the case of
922 * spaces before newlines as a side effect.
923 *
924 * 2. could be used to suppress as many spaces as possible. This has two effects:
925 * - Reimporting LyX generated LaTeX files changes almost no whitespace
926 * - Superflous whitespace from non LyX generated LaTeX files is removed.
927 * The drawback is that the logic inside the function becomes
928 * complicated, and that is the reason why it is not implemented.
929 */
check_space(Parser & p,ostream & os,Context & context)930 void check_space(Parser & p, ostream & os, Context & context)
931 {
932 Token const next = p.next_token();
933 Token const curr = p.curr_token();
934 // A space before a single newline and vice versa must be ignored
935 // LyX emits a newline before \end{lyxcode}.
936 // This newline must be ignored,
937 // otherwise LyX will add an additional protected space.
938 if (next.cat() == catSpace ||
939 next.cat() == catNewline ||
940 (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
941 return;
942 }
943 context.check_layout(os);
944 os << ' ';
945 }
946
947
948 /*!
949 * Parse all arguments of \p command
950 */
parse_arguments(string const & command,vector<ArgumentType> const & template_arguments,Parser & p,ostream & os,bool outer,Context & context)951 void parse_arguments(string const & command,
952 vector<ArgumentType> const & template_arguments,
953 Parser & p, ostream & os, bool outer, Context & context)
954 {
955 string ert = command;
956 size_t no_arguments = template_arguments.size();
957 for (size_t i = 0; i < no_arguments; ++i) {
958 switch (template_arguments[i]) {
959 case required:
960 case req_group:
961 // This argument contains regular LaTeX
962 output_ert_inset(os, ert + '{', context);
963 eat_whitespace(p, os, context, false);
964 if (template_arguments[i] == required)
965 parse_text(p, os, FLAG_ITEM, outer, context);
966 else
967 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
968 ert = "}";
969 break;
970 case item:
971 // This argument consists only of a single item.
972 // The presence of '{' or not must be preserved.
973 p.skip_spaces();
974 if (p.next_token().cat() == catBegin)
975 ert += '{' + p.verbatim_item() + '}';
976 else
977 ert += p.verbatim_item();
978 break;
979 case displaymath:
980 case verbatim:
981 // This argument may contain special characters
982 ert += '{' + p.verbatim_item() + '}';
983 break;
984 case optional:
985 case opt_group:
986 // true because we must not eat whitespace
987 // if an optional arg follows we must not strip the
988 // brackets from this one
989 if (i < no_arguments - 1 &&
990 template_arguments[i+1] == optional)
991 ert += p.getFullOpt(true);
992 else
993 ert += p.getOpt(true);
994 break;
995 }
996 }
997 output_ert_inset(os, ert, context);
998 }
999
1000
1001 /*!
1002 * Check whether \p command is a known command. If yes,
1003 * handle the command with all arguments.
1004 * \return true if the command was parsed, false otherwise.
1005 */
parse_command(string const & command,Parser & p,ostream & os,bool outer,Context & context)1006 bool parse_command(string const & command, Parser & p, ostream & os,
1007 bool outer, Context & context)
1008 {
1009 if (known_commands.find(command) != known_commands.end()) {
1010 parse_arguments(command, known_commands[command], p, os,
1011 outer, context);
1012 return true;
1013 }
1014 return false;
1015 }
1016
1017
1018 /// Parses a minipage or parbox
parse_box(Parser & p,ostream & os,unsigned outer_flags,unsigned inner_flags,bool outer,Context & parent_context,string const & outer_type,string const & special,string inner_type,string const & frame_color,string const & background_color)1019 void parse_box(Parser & p, ostream & os, unsigned outer_flags,
1020 unsigned inner_flags, bool outer, Context & parent_context,
1021 string const & outer_type, string const & special,
1022 string inner_type, string const & frame_color,
1023 string const & background_color)
1024 {
1025 string position;
1026 string inner_pos;
1027 string hor_pos = "l";
1028 // We need to set the height to the LaTeX default of 1\\totalheight
1029 // for the case when no height argument is given
1030 string height_value = "1";
1031 string height_unit = "in";
1032 string height_special = "totalheight";
1033 string latex_height;
1034 string width_value;
1035 string width_unit;
1036 string latex_width;
1037 string width_special = "none";
1038 string thickness = "0.4pt";
1039 if (!fboxrule.empty())
1040 thickness = fboxrule;
1041 else
1042 thickness = "0.4pt";
1043 string separation;
1044 if (!fboxsep.empty())
1045 separation = fboxsep;
1046 else
1047 separation = "3pt";
1048 string shadowsize;
1049 if (!shadow_size.empty())
1050 shadowsize = shadow_size;
1051 else
1052 shadowsize = "4pt";
1053 string framecolor = "black";
1054 string backgroundcolor = "none";
1055 if (!frame_color.empty())
1056 framecolor = frame_color;
1057 if (!background_color.empty())
1058 backgroundcolor = background_color;
1059 // if there is a color box around the \begin statements have not yet been parsed
1060 // so do this now
1061 if (!frame_color.empty() || !background_color.empty()) {
1062 eat_whitespace(p, os, parent_context, false);
1063 p.get_token().asInput(); // the '{'
1064 // parse minipage
1065 if (p.next_token().asInput() == "\\begin") {
1066 p.get_token().asInput();
1067 p.getArg('{', '}');
1068 inner_type = "minipage";
1069 inner_flags = FLAG_END;
1070 active_environments.push_back("minipage");
1071 }
1072 // parse parbox
1073 else if (p.next_token().asInput() == "\\parbox") {
1074 p.get_token().asInput();
1075 inner_type = "parbox";
1076 inner_flags = FLAG_ITEM;
1077 }
1078 // parse makebox
1079 else if (p.next_token().asInput() == "\\makebox") {
1080 p.get_token().asInput();
1081 inner_type = "makebox";
1082 inner_flags = FLAG_ITEM;
1083 }
1084 // in case there is just \colorbox{color}{text}
1085 else {
1086 latex_width = "";
1087 inner_type = "makebox";
1088 inner_flags = FLAG_BRACE_LAST;
1089 position = "t";
1090 inner_pos = "t";
1091 }
1092 }
1093 if (!p.hasOpt() && (inner_type == "makebox" || outer_type == "mbox"))
1094 hor_pos = "c";
1095 if (!inner_type.empty() && p.hasOpt()) {
1096 if (inner_type != "makebox")
1097 position = p.getArg('[', ']');
1098 else {
1099 latex_width = p.getArg('[', ']');
1100 translate_box_len(latex_width, width_value, width_unit, width_special);
1101 position = "t";
1102 }
1103 if (position != "t" && position != "c" && position != "b") {
1104 cerr << "invalid position " << position << " for "
1105 << inner_type << endl;
1106 position = "c";
1107 }
1108 if (p.hasOpt()) {
1109 if (inner_type != "makebox") {
1110 latex_height = p.getArg('[', ']');
1111 translate_box_len(latex_height, height_value, height_unit, height_special);
1112 } else {
1113 string const opt = p.getArg('[', ']');
1114 if (!opt.empty()) {
1115 hor_pos = opt;
1116 if (hor_pos != "l" && hor_pos != "c" &&
1117 hor_pos != "r" && hor_pos != "s") {
1118 cerr << "invalid hor_pos " << hor_pos
1119 << " for " << inner_type << endl;
1120 hor_pos = "c";
1121 }
1122 }
1123 }
1124
1125 if (p.hasOpt()) {
1126 inner_pos = p.getArg('[', ']');
1127 if (inner_pos != "c" && inner_pos != "t" &&
1128 inner_pos != "b" && inner_pos != "s") {
1129 cerr << "invalid inner_pos "
1130 << inner_pos << " for "
1131 << inner_type << endl;
1132 inner_pos = position;
1133 }
1134 }
1135 } else {
1136 if (inner_type == "makebox")
1137 hor_pos = "c";
1138 }
1139 }
1140 if (inner_type.empty()) {
1141 if (special.empty() && outer_type != "framebox")
1142 latex_width = "1\\columnwidth";
1143 else {
1144 Parser p2(special);
1145 latex_width = p2.getArg('[', ']');
1146 string const opt = p2.getArg('[', ']');
1147 if (!opt.empty()) {
1148 hor_pos = opt;
1149 if (hor_pos != "l" && hor_pos != "c" &&
1150 hor_pos != "r" && hor_pos != "s") {
1151 cerr << "invalid hor_pos " << hor_pos
1152 << " for " << outer_type << endl;
1153 hor_pos = "c";
1154 }
1155 } else {
1156 if (outer_type == "framebox")
1157 hor_pos = "c";
1158 }
1159 }
1160 } else if (inner_type != "makebox")
1161 latex_width = p.verbatim_item();
1162 // if e.g. only \ovalbox{content} was used, set the width to 1\columnwidth
1163 // as this is LyX's standard for such cases (except for makebox)
1164 // \framebox is more special and handled below
1165 if (latex_width.empty() && inner_type != "makebox"
1166 && outer_type != "framebox")
1167 latex_width = "1\\columnwidth";
1168
1169 translate_len(latex_width, width_value, width_unit);
1170
1171 bool shadedparbox = false;
1172 if (inner_type == "shaded") {
1173 eat_whitespace(p, os, parent_context, false);
1174 if (outer_type == "parbox") {
1175 // Eat '{'
1176 if (p.next_token().cat() == catBegin)
1177 p.get_token();
1178 eat_whitespace(p, os, parent_context, false);
1179 shadedparbox = true;
1180 }
1181 p.get_token();
1182 p.getArg('{', '}');
1183 }
1184 // If we already read the inner box we have to push the inner env
1185 if (!outer_type.empty() && !inner_type.empty() &&
1186 (inner_flags & FLAG_END))
1187 active_environments.push_back(inner_type);
1188 bool use_ert = false;
1189 if (!outer_type.empty() && !inner_type.empty()) {
1190 // Look whether there is some content after the end of the
1191 // inner box, but before the end of the outer box.
1192 // If yes, we need to output ERT.
1193 p.pushPosition();
1194 if (inner_flags & FLAG_END)
1195 p.ertEnvironment(inner_type);
1196 else
1197 p.verbatim_item();
1198 p.skip_spaces(true);
1199 bool const outer_env(outer_type == "framed" || outer_type == "minipage");
1200 if ((outer_env && p.next_token().asInput() != "\\end") ||
1201 (!outer_env && p.next_token().cat() != catEnd)) {
1202 // something is between the end of the inner box and
1203 // the end of the outer box, so we need to use ERT.
1204 use_ert = true;
1205 }
1206 p.popPosition();
1207 }
1208
1209 if (use_ert) {
1210 ostringstream ss;
1211 if (!outer_type.empty()) {
1212 if (outer_flags & FLAG_END)
1213 ss << "\\begin{" << outer_type << '}';
1214 else {
1215 ss << '\\' << outer_type << '{';
1216 if (!special.empty())
1217 ss << special;
1218 }
1219 }
1220 if (!inner_type.empty()) {
1221 if (inner_type != "shaded") {
1222 if (inner_flags & FLAG_END)
1223 ss << "\\begin{" << inner_type << '}';
1224 else
1225 ss << '\\' << inner_type;
1226 }
1227 if (!position.empty())
1228 ss << '[' << position << ']';
1229 if (!latex_height.empty())
1230 ss << '[' << latex_height << ']';
1231 if (!inner_pos.empty())
1232 ss << '[' << inner_pos << ']';
1233 ss << '{' << latex_width << '}';
1234 if (!(inner_flags & FLAG_END))
1235 ss << '{';
1236 }
1237 if (inner_type == "shaded")
1238 ss << "\\begin{shaded}";
1239 output_ert_inset(os, ss.str(), parent_context);
1240 if (!inner_type.empty()) {
1241 parse_text(p, os, inner_flags, outer, parent_context);
1242 if (inner_flags & FLAG_END)
1243 output_ert_inset(os, "\\end{" + inner_type + '}',
1244 parent_context);
1245 else
1246 output_ert_inset(os, "}", parent_context);
1247 }
1248 if (!outer_type.empty()) {
1249 // If we already read the inner box we have to pop
1250 // the inner env
1251 if (!inner_type.empty() && (inner_flags & FLAG_END))
1252 active_environments.pop_back();
1253
1254 // Ensure that the end of the outer box is parsed correctly:
1255 // The opening brace has been eaten by parse_outer_box()
1256 if (!outer_type.empty() && (outer_flags & FLAG_ITEM)) {
1257 outer_flags &= ~FLAG_ITEM;
1258 outer_flags |= FLAG_BRACE_LAST;
1259 }
1260 parse_text(p, os, outer_flags, outer, parent_context);
1261 if (outer_flags & FLAG_END)
1262 output_ert_inset(os, "\\end{" + outer_type + '}',
1263 parent_context);
1264 else
1265 output_ert_inset(os, "}", parent_context);
1266 }
1267 } else {
1268 // LyX does not like empty positions, so we have
1269 // to set them to the LaTeX default values here.
1270 if (position.empty())
1271 position = "c";
1272 if (inner_pos.empty())
1273 inner_pos = position;
1274 parent_context.check_layout(os);
1275 begin_inset(os, "Box ");
1276 if (outer_type == "framed")
1277 os << "Framed\n";
1278 else if (outer_type == "framebox" || outer_type == "fbox" || !frame_color.empty())
1279 os << "Boxed\n";
1280 else if (outer_type == "shadowbox")
1281 os << "Shadowbox\n";
1282 else if ((outer_type == "shaded" && inner_type.empty()) ||
1283 (outer_type == "minipage" && inner_type == "shaded") ||
1284 (outer_type == "parbox" && inner_type == "shaded")) {
1285 os << "Shaded\n";
1286 preamble.registerAutomaticallyLoadedPackage("color");
1287 } else if (outer_type == "doublebox")
1288 os << "Doublebox\n";
1289 else if (outer_type.empty() || outer_type == "mbox")
1290 os << "Frameless\n";
1291 else
1292 os << outer_type << '\n';
1293 os << "position \"" << position << "\"\n";
1294 os << "hor_pos \"" << hor_pos << "\"\n";
1295 if (outer_type == "mbox")
1296 os << "has_inner_box 1\n";
1297 else if (!frame_color.empty() && inner_type == "makebox")
1298 os << "has_inner_box 0\n";
1299 else
1300 os << "has_inner_box " << !inner_type.empty() << "\n";
1301 os << "inner_pos \"" << inner_pos << "\"\n";
1302 os << "use_parbox " << (inner_type == "parbox" || shadedparbox)
1303 << '\n';
1304 if (outer_type == "mbox")
1305 os << "use_makebox 1\n";
1306 else if (!frame_color.empty())
1307 os << "use_makebox 0\n";
1308 else
1309 os << "use_makebox " << (inner_type == "makebox") << '\n';
1310 if (outer_type == "mbox" || (outer_type == "fbox" && inner_type.empty()))
1311 os << "width \"\"\n";
1312 // for values like "1.5\width" LyX uses "1.5in" as width ad sets "width" as sepecial
1313 else if (contains(width_unit, '\\'))
1314 os << "width \"" << width_value << "in" << "\"\n";
1315 else
1316 os << "width \"" << width_value << width_unit << "\"\n";
1317 if (contains(width_unit, '\\')) {
1318 width_unit.erase (0,1); // remove the leading '\'
1319 os << "special \"" << width_unit << "\"\n";
1320 } else
1321 os << "special \"" << width_special << "\"\n";
1322 if (contains(height_unit, '\\'))
1323 os << "height \"" << height_value << "in" << "\"\n";
1324 else
1325 os << "height \"" << height_value << height_unit << "\"\n";
1326 os << "height_special \"" << height_special << "\"\n";
1327 os << "thickness \"" << thickness << "\"\n";
1328 os << "separation \"" << separation << "\"\n";
1329 os << "shadowsize \"" << shadowsize << "\"\n";
1330 os << "framecolor \"" << framecolor << "\"\n";
1331 os << "backgroundcolor \"" << backgroundcolor << "\"\n";
1332 os << "status open\n\n";
1333
1334 // Unfortunately we can't use parse_text_in_inset:
1335 // InsetBox::forcePlainLayout() is hard coded and does not
1336 // use the inset layout. Apart from that do we call parse_text
1337 // up to two times, but need only one check_end_layout.
1338 bool const forcePlainLayout =
1339 (!inner_type.empty() || inner_type == "makebox") &&
1340 outer_type != "shaded" && outer_type != "framed";
1341 Context context(true, parent_context.textclass);
1342 if (forcePlainLayout)
1343 context.layout = &context.textclass.plainLayout();
1344 else
1345 context.font = parent_context.font;
1346
1347 // If we have no inner box the contents will be read with the outer box
1348 if (!inner_type.empty())
1349 parse_text(p, os, inner_flags, outer, context);
1350
1351 // Ensure that the end of the outer box is parsed correctly:
1352 // The opening brace has been eaten by parse_outer_box()
1353 if (!outer_type.empty() && (outer_flags & FLAG_ITEM)) {
1354 outer_flags &= ~FLAG_ITEM;
1355 outer_flags |= FLAG_BRACE_LAST;
1356 }
1357
1358 // Find end of outer box, output contents if inner_type is
1359 // empty and output possible comments
1360 if (!outer_type.empty()) {
1361 // If we already read the inner box we have to pop
1362 // the inner env
1363 if (!inner_type.empty() && (inner_flags & FLAG_END))
1364 active_environments.pop_back();
1365 // This does not output anything but comments if
1366 // inner_type is not empty (see use_ert)
1367 parse_text(p, os, outer_flags, outer, context);
1368 }
1369
1370 context.check_end_layout(os);
1371 end_inset(os);
1372 #ifdef PRESERVE_LAYOUT
1373 // LyX puts a % after the end of the minipage
1374 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
1375 // new paragraph
1376 //output_comment(p, os, "dummy", parent_context);
1377 p.get_token();
1378 p.skip_spaces();
1379 parent_context.new_paragraph(os);
1380 }
1381 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
1382 //output_comment(p, os, "dummy", parent_context);
1383 p.get_token();
1384 p.skip_spaces();
1385 // We add a protected space if something real follows
1386 if (p.good() && p.next_token().cat() != catComment) {
1387 begin_inset(os, "space ~\n");
1388 end_inset(os);
1389 }
1390 }
1391 #endif
1392 }
1393 if (inner_type == "minipage" && (!frame_color.empty() || !background_color.empty()))
1394 active_environments.pop_back();
1395 if (inner_flags != FLAG_BRACE_LAST && (!frame_color.empty() || !background_color.empty())) {
1396 // in this case we have to eat the the closing brace of the color box
1397 p.get_token().asInput(); // the '}'
1398 }
1399 if (p.next_token().asInput() == "}") {
1400 // in this case we assume that the closing brace is from the box settings
1401 // therefore reset these values for the next box
1402 fboxrule = "";
1403 fboxsep = "";
1404 shadow_size = "";
1405 }
1406
1407 // all boxes except of Frameless and Shaded require calc
1408 if (!(outer_type.empty() || outer_type == "mbox") &&
1409 !((outer_type == "shaded" && inner_type.empty()) ||
1410 (outer_type == "minipage" && inner_type == "shaded") ||
1411 (outer_type == "parbox" && inner_type == "shaded")))
1412 preamble.registerAutomaticallyLoadedPackage("calc");
1413 }
1414
1415
parse_outer_box(Parser & p,ostream & os,unsigned flags,bool outer,Context & parent_context,string const & outer_type,string const & special)1416 void parse_outer_box(Parser & p, ostream & os, unsigned flags, bool outer,
1417 Context & parent_context, string const & outer_type,
1418 string const & special)
1419 {
1420 eat_whitespace(p, os, parent_context, false);
1421 if (flags & FLAG_ITEM) {
1422 // Eat '{'
1423 if (p.next_token().cat() == catBegin)
1424 p.get_token();
1425 else
1426 cerr << "Warning: Ignoring missing '{' after \\"
1427 << outer_type << '.' << endl;
1428 eat_whitespace(p, os, parent_context, false);
1429 }
1430 string inner;
1431 unsigned int inner_flags = 0;
1432 p.pushPosition();
1433 if (outer_type == "minipage" || outer_type == "parbox") {
1434 p.skip_spaces(true);
1435 while (p.hasOpt()) {
1436 p.getArg('[', ']');
1437 p.skip_spaces(true);
1438 }
1439 p.getArg('{', '}');
1440 p.skip_spaces(true);
1441 if (outer_type == "parbox") {
1442 // Eat '{'
1443 if (p.next_token().cat() == catBegin)
1444 p.get_token();
1445 p.skip_spaces(true);
1446 }
1447 }
1448 if (outer_type == "shaded" || outer_type == "mbox") {
1449 // These boxes never have an inner box
1450 ;
1451 } else if (p.next_token().asInput() == "\\parbox") {
1452 inner = p.get_token().cs();
1453 inner_flags = FLAG_ITEM;
1454 } else if (p.next_token().asInput() == "\\begin") {
1455 // Is this a minipage or shaded box?
1456 p.pushPosition();
1457 p.get_token();
1458 inner = p.getArg('{', '}');
1459 p.popPosition();
1460 if (inner == "minipage" || inner == "shaded")
1461 inner_flags = FLAG_END;
1462 else
1463 inner = "";
1464 }
1465 p.popPosition();
1466 if (inner_flags == FLAG_END) {
1467 if (inner != "shaded")
1468 {
1469 p.get_token();
1470 p.getArg('{', '}');
1471 eat_whitespace(p, os, parent_context, false);
1472 }
1473 parse_box(p, os, flags, FLAG_END, outer, parent_context,
1474 outer_type, special, inner, "", "");
1475 } else {
1476 if (inner_flags == FLAG_ITEM) {
1477 p.get_token();
1478 eat_whitespace(p, os, parent_context, false);
1479 }
1480 parse_box(p, os, flags, inner_flags, outer, parent_context,
1481 outer_type, special, inner, "", "");
1482 }
1483 }
1484
1485
parse_listings(Parser & p,ostream & os,Context & parent_context,bool in_line,bool use_minted)1486 void parse_listings(Parser & p, ostream & os, Context & parent_context,
1487 bool in_line, bool use_minted)
1488 {
1489 parent_context.check_layout(os);
1490 begin_inset(os, "listings\n");
1491 string arg = p.hasOpt() ? subst(p.verbatimOption(), "\n", "") : string();
1492 size_t i;
1493 while ((i = arg.find(", ")) != string::npos
1494 || (i = arg.find(",\t")) != string::npos)
1495 arg.erase(i + 1, 1);
1496
1497 if (use_minted) {
1498 string const language = p.getArg('{', '}');
1499 p.skip_spaces(true);
1500 arg += string(arg.empty() ? "" : ",") + "language=" + language;
1501 if (!minted_float.empty()) {
1502 arg += string(arg.empty() ? "" : ",") + minted_float;
1503 minted_nonfloat_caption.clear();
1504 }
1505 }
1506 if (!arg.empty()) {
1507 os << "lstparams " << '"' << arg << '"' << '\n';
1508 if (arg.find("\\color") != string::npos)
1509 preamble.registerAutomaticallyLoadedPackage("color");
1510 }
1511 if (in_line)
1512 os << "inline true\n";
1513 else
1514 os << "inline false\n";
1515 os << "status open\n";
1516 Context context(true, parent_context.textclass);
1517 context.layout = &parent_context.textclass.plainLayout();
1518 if (use_minted && prefixIs(minted_nonfloat_caption, "[t]")) {
1519 minted_nonfloat_caption.erase(0,3);
1520 os << "\n\\begin_layout Plain Layout\n";
1521 begin_inset(os, "Caption Standard\n");
1522 Context newcontext(true, context.textclass,
1523 context.layout, 0, context.font);
1524 newcontext.check_layout(os);
1525 os << minted_nonfloat_caption << "\n";
1526 newcontext.check_end_layout(os);
1527 end_inset(os);
1528 os << "\n\\end_layout\n";
1529 minted_nonfloat_caption.clear();
1530 }
1531 string s;
1532 if (in_line) {
1533 // set catcodes to verbatim early, just in case.
1534 p.setCatcodes(VERBATIM_CATCODES);
1535 string delim = p.get_token().asInput();
1536 //FIXME: handler error condition
1537 s = p.verbatimStuff(delim).second;
1538 // context.new_paragraph(os);
1539 } else if (use_minted) {
1540 s = p.verbatimEnvironment("minted");
1541 } else {
1542 s = p.verbatimEnvironment("lstlisting");
1543 }
1544 output_ert(os, s, context);
1545 if (use_minted && prefixIs(minted_nonfloat_caption, "[b]")) {
1546 minted_nonfloat_caption.erase(0,3);
1547 os << "\n\\begin_layout Plain Layout\n";
1548 begin_inset(os, "Caption Standard\n");
1549 Context newcontext(true, context.textclass,
1550 context.layout, 0, context.font);
1551 newcontext.check_layout(os);
1552 os << minted_nonfloat_caption << "\n";
1553 newcontext.check_end_layout(os);
1554 end_inset(os);
1555 os << "\n\\end_layout\n";
1556 minted_nonfloat_caption.clear();
1557 }
1558 // Don't close the inset here for floating minted listings.
1559 // It will be closed at the end of the listing environment.
1560 if (!use_minted || minted_float.empty())
1561 end_inset(os);
1562 else {
1563 eat_whitespace(p, os, parent_context, true);
1564 Token t = p.get_token();
1565 if (t.asInput() != "\\end") {
1566 // If anything follows, collect it into a caption.
1567 minted_float_has_caption = true;
1568 os << "\n\\begin_layout Plain Layout\n"; // outer layout
1569 begin_inset(os, "Caption Standard\n");
1570 os << "\n\\begin_layout Plain Layout\n"; // inner layout
1571 }
1572 p.putback();
1573 }
1574 }
1575
1576
1577 /// parse an unknown environment
parse_unknown_environment(Parser & p,string const & name,ostream & os,unsigned flags,bool outer,Context & parent_context)1578 void parse_unknown_environment(Parser & p, string const & name, ostream & os,
1579 unsigned flags, bool outer,
1580 Context & parent_context)
1581 {
1582 if (name == "tabbing")
1583 // We need to remember that we have to handle '\=' specially
1584 flags |= FLAG_TABBING;
1585
1586 // We need to translate font changes and paragraphs inside the
1587 // environment to ERT if we have a non standard font.
1588 // Otherwise things like
1589 // \large\begin{foo}\huge bar\end{foo}
1590 // will not work.
1591 bool const specialfont =
1592 (parent_context.font != parent_context.normalfont);
1593 bool const new_layout_allowed = parent_context.new_layout_allowed;
1594 if (specialfont)
1595 parent_context.new_layout_allowed = false;
1596 output_ert_inset(os, "\\begin{" + name + "}", parent_context);
1597 // Try to handle options: Look if we have an optional arguments,
1598 // and if so, put the brackets in ERT.
1599 while (p.hasOpt()) {
1600 p.get_token(); // eat '['
1601 output_ert_inset(os, "[", parent_context);
1602 os << parse_text_snippet(p, FLAG_BRACK_LAST, outer, parent_context);
1603 output_ert_inset(os, "]", parent_context);
1604 }
1605 parse_text_snippet(p, os, flags, outer, parent_context);
1606 output_ert_inset(os, "\\end{" + name + "}", parent_context);
1607 if (specialfont)
1608 parent_context.new_layout_allowed = new_layout_allowed;
1609 }
1610
1611
parse_environment(Parser & p,ostream & os,bool outer,string & last_env,Context & parent_context)1612 void parse_environment(Parser & p, ostream & os, bool outer,
1613 string & last_env, Context & parent_context)
1614 {
1615 Layout const * newlayout;
1616 InsetLayout const * newinsetlayout = 0;
1617 string const name = p.getArg('{', '}');
1618 const bool is_starred = suffixIs(name, '*');
1619 string const unstarred_name = rtrim(name, "*");
1620 active_environments.push_back(name);
1621
1622 if (is_math_env(name)) {
1623 parent_context.check_layout(os);
1624 begin_inset(os, "Formula ");
1625 os << "\\begin{" << name << "}";
1626 parse_math(p, os, FLAG_END, MATH_MODE);
1627 os << "\\end{" << name << "}";
1628 end_inset(os);
1629 if (is_display_math_env(name)) {
1630 // Prevent the conversion of a line break to a space
1631 // (bug 7668). This does not change the output, but
1632 // looks ugly in LyX.
1633 eat_whitespace(p, os, parent_context, false);
1634 }
1635 }
1636
1637 // We need to use fromPolyglossiaEnvironment die to Arabic > arabic
1638 else if (is_known(fromPolyglossiaEnvironment(name), preamble.polyglossia_languages)) {
1639 // We must begin a new paragraph if not already done
1640 if (! parent_context.atParagraphStart()) {
1641 parent_context.check_end_layout(os);
1642 parent_context.new_paragraph(os);
1643 }
1644 // save the language in the context so that it is
1645 // handled by parse_text
1646 parent_context.font.language =
1647 preamble.polyglossia2lyx(fromPolyglossiaEnvironment(name));
1648 parse_text(p, os, FLAG_END, outer, parent_context);
1649 // Just in case the environment is empty
1650 parent_context.extra_stuff.erase();
1651 // We must begin a new paragraph to reset the language
1652 parent_context.new_paragraph(os);
1653 p.skip_spaces();
1654 }
1655
1656 else if (unstarred_name == "tabular" || name == "longtable") {
1657 eat_whitespace(p, os, parent_context, false);
1658 string width = "0pt";
1659 string halign;
1660 if (name == "longtable" && p.hasOpt()) {
1661 string const opt = p.getArg('[', ']');
1662 if (opt == "c")
1663 halign = "center";
1664 else if (opt == "l")
1665 halign = "left";
1666 else if (opt == "r")
1667 halign = "right";
1668 }
1669 if (name == "tabular*") {
1670 width = lyx::translate_len(p.getArg('{', '}'));
1671 eat_whitespace(p, os, parent_context, false);
1672 }
1673 parent_context.check_layout(os);
1674 begin_inset(os, "Tabular ");
1675 handle_tabular(p, os, name, width, halign, parent_context);
1676 end_inset(os);
1677 p.skip_spaces();
1678 }
1679
1680 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
1681 eat_whitespace(p, os, parent_context, false);
1682 string const opt = p.hasOpt() ? p.getArg('[', ']') : string();
1683 eat_whitespace(p, os, parent_context, false);
1684 parent_context.check_layout(os);
1685 begin_inset(os, "Float " + unstarred_name + "\n");
1686 // store the float type for subfloats
1687 // subfloats only work with figures and tables
1688 if (unstarred_name == "figure")
1689 float_type = unstarred_name;
1690 else if (unstarred_name == "table")
1691 float_type = unstarred_name;
1692 else
1693 float_type = "";
1694 if (!opt.empty())
1695 os << "placement " << opt << '\n';
1696 if (contains(opt, "H"))
1697 preamble.registerAutomaticallyLoadedPackage("float");
1698 else {
1699 Floating const & fl = parent_context.textclass.floats()
1700 .getType(unstarred_name);
1701 if (!fl.floattype().empty() && fl.usesFloatPkg())
1702 preamble.registerAutomaticallyLoadedPackage("float");
1703 }
1704
1705 os << "wide " << convert<string>(is_starred)
1706 << "\nsideways false"
1707 << "\nstatus open\n\n";
1708 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1709 end_inset(os);
1710 // We don't need really a new paragraph, but
1711 // we must make sure that the next item gets a \begin_layout.
1712 parent_context.new_paragraph(os);
1713 p.skip_spaces();
1714 // the float is parsed thus delete the type
1715 float_type = "";
1716 }
1717
1718 else if (unstarred_name == "sidewaysfigure"
1719 || unstarred_name == "sidewaystable"
1720 || unstarred_name == "sidewaysalgorithm") {
1721 string const opt = p.hasOpt() ? p.getArg('[', ']') : string();
1722 eat_whitespace(p, os, parent_context, false);
1723 parent_context.check_layout(os);
1724 if (unstarred_name == "sidewaysfigure")
1725 begin_inset(os, "Float figure\n");
1726 else if (unstarred_name == "sidewaystable")
1727 begin_inset(os, "Float table\n");
1728 else if (unstarred_name == "sidewaysalgorithm")
1729 begin_inset(os, "Float algorithm\n");
1730 if (!opt.empty())
1731 os << "placement " << opt << '\n';
1732 if (contains(opt, "H"))
1733 preamble.registerAutomaticallyLoadedPackage("float");
1734 os << "wide " << convert<string>(is_starred)
1735 << "\nsideways true"
1736 << "\nstatus open\n\n";
1737 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1738 end_inset(os);
1739 // We don't need really a new paragraph, but
1740 // we must make sure that the next item gets a \begin_layout.
1741 parent_context.new_paragraph(os);
1742 p.skip_spaces();
1743 preamble.registerAutomaticallyLoadedPackage("rotfloat");
1744 }
1745
1746 else if (name == "wrapfigure" || name == "wraptable") {
1747 // syntax is \begin{wrapfigure}[lines]{placement}[overhang]{width}
1748 eat_whitespace(p, os, parent_context, false);
1749 parent_context.check_layout(os);
1750 // default values
1751 string lines = "0";
1752 string overhang = "0col%";
1753 // parse
1754 if (p.hasOpt())
1755 lines = p.getArg('[', ']');
1756 string const placement = p.getArg('{', '}');
1757 if (p.hasOpt())
1758 overhang = p.getArg('[', ']');
1759 string const width = p.getArg('{', '}');
1760 // write
1761 if (name == "wrapfigure")
1762 begin_inset(os, "Wrap figure\n");
1763 else
1764 begin_inset(os, "Wrap table\n");
1765 os << "lines " << lines
1766 << "\nplacement " << placement
1767 << "\noverhang " << lyx::translate_len(overhang)
1768 << "\nwidth " << lyx::translate_len(width)
1769 << "\nstatus open\n\n";
1770 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1771 end_inset(os);
1772 // We don't need really a new paragraph, but
1773 // we must make sure that the next item gets a \begin_layout.
1774 parent_context.new_paragraph(os);
1775 p.skip_spaces();
1776 preamble.registerAutomaticallyLoadedPackage("wrapfig");
1777 }
1778
1779 else if (name == "minipage") {
1780 eat_whitespace(p, os, parent_context, false);
1781 // Test whether this is an outer box of a shaded box
1782 p.pushPosition();
1783 // swallow arguments
1784 while (p.hasOpt()) {
1785 p.getArg('[', ']');
1786 p.skip_spaces(true);
1787 }
1788 p.getArg('{', '}');
1789 p.skip_spaces(true);
1790 Token t = p.get_token();
1791 bool shaded = false;
1792 if (t.asInput() == "\\begin") {
1793 p.skip_spaces(true);
1794 if (p.getArg('{', '}') == "shaded")
1795 shaded = true;
1796 }
1797 p.popPosition();
1798 if (shaded)
1799 parse_outer_box(p, os, FLAG_END, outer,
1800 parent_context, name, "shaded");
1801 else
1802 parse_box(p, os, 0, FLAG_END, outer, parent_context,
1803 "", "", name, "", "");
1804 p.skip_spaces();
1805 }
1806
1807 else if (name == "comment") {
1808 eat_whitespace(p, os, parent_context, false);
1809 parent_context.check_layout(os);
1810 begin_inset(os, "Note Comment\n");
1811 os << "status open\n";
1812 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1813 end_inset(os);
1814 p.skip_spaces();
1815 skip_braces(p); // eat {} that might by set by LyX behind comments
1816 preamble.registerAutomaticallyLoadedPackage("verbatim");
1817 }
1818
1819 else if (unstarred_name == "verbatim") {
1820 // FIXME: this should go in the generic code that
1821 // handles environments defined in layout file that
1822 // have "PassThru 1". However, the code over there is
1823 // already too complicated for my taste.
1824 string const ascii_name =
1825 (name == "verbatim*") ? "Verbatim*" : "Verbatim";
1826 parent_context.new_paragraph(os);
1827 Context context(true, parent_context.textclass,
1828 &parent_context.textclass[from_ascii(ascii_name)]);
1829 string s = p.verbatimEnvironment(name);
1830 output_ert(os, s, context);
1831 p.skip_spaces();
1832 }
1833
1834 else if (name == "IPA") {
1835 eat_whitespace(p, os, parent_context, false);
1836 parent_context.check_layout(os);
1837 begin_inset(os, "IPA\n");
1838 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1839 end_inset(os);
1840 p.skip_spaces();
1841 preamble.registerAutomaticallyLoadedPackage("tipa");
1842 preamble.registerAutomaticallyLoadedPackage("tipx");
1843 }
1844
1845 else if (name == parent_context.textclass.titlename()
1846 && parent_context.textclass.titletype() == TITLE_ENVIRONMENT) {
1847 parse_text(p, os, FLAG_END, outer, parent_context);
1848 // Just in case the environment is empty
1849 parent_context.extra_stuff.erase();
1850 // We must begin a new paragraph
1851 parent_context.new_paragraph(os);
1852 p.skip_spaces();
1853 }
1854
1855 else if (name == "CJK") {
1856 // the scheme is \begin{CJK}{encoding}{mapping}text\end{CJK}
1857 // It is impossible to decide if a CJK environment was in its own paragraph or within
1858 // a line. We therefore always assume a paragraph since the latter is a rare case.
1859 eat_whitespace(p, os, parent_context, false);
1860 parent_context.check_end_layout(os);
1861 // store the encoding to be able to reset it
1862 string const encoding_old = p.getEncoding();
1863 string const encoding = p.getArg('{', '}');
1864 // FIXME: For some reason JIS does not work. Although the text
1865 // in tests/CJK.tex is identical with the SJIS version if you
1866 // convert both snippets using the recode command line utility,
1867 // the resulting .lyx file contains some extra characters if
1868 // you set buggy_encoding to false for JIS.
1869 bool const buggy_encoding = encoding == "JIS";
1870 if (!buggy_encoding)
1871 p.setEncoding(encoding, Encoding::CJK);
1872 else {
1873 // FIXME: This will read garbage, since the data is not encoded in utf8.
1874 p.setEncoding("UTF-8");
1875 }
1876 // LyX only supports the same mapping for all CJK
1877 // environments, so we might need to output everything as ERT
1878 string const mapping = trim(p.getArg('{', '}'));
1879 char const * const * const where =
1880 is_known(encoding, supported_CJK_encodings);
1881 if (!buggy_encoding && !preamble.fontCJKSet())
1882 preamble.fontCJK(mapping);
1883 bool knownMapping = mapping == preamble.fontCJK();
1884 if (buggy_encoding || !knownMapping || !where) {
1885 parent_context.check_layout(os);
1886 output_ert_inset(os, "\\begin{" + name + "}{" + encoding + "}{" + mapping + "}",
1887 parent_context);
1888 // we must parse the content as verbatim because e.g. JIS can contain
1889 // normally invalid characters
1890 // FIXME: This works only for the most simple cases.
1891 // Since TeX control characters are not parsed,
1892 // things like comments are completely wrong.
1893 string const s = p.plainEnvironment("CJK");
1894 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
1895 string snip;
1896 snip += *it;
1897 if (snip == "\\" || is_known(snip, known_escaped_chars))
1898 output_ert_inset(os, snip, parent_context);
1899 else if (*it == '\n' && it + 1 != et && s.begin() + 1 != it)
1900 os << "\n ";
1901 else
1902 os << *it;
1903 }
1904 output_ert_inset(os, "\\end{" + name + "}",
1905 parent_context);
1906 } else {
1907 string const lang =
1908 supported_CJK_languages[where - supported_CJK_encodings];
1909 // store the language because we must reset it at the end
1910 string const lang_old = parent_context.font.language;
1911 parent_context.font.language = lang;
1912 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1913 parent_context.font.language = lang_old;
1914 parent_context.new_paragraph(os);
1915 }
1916 p.setEncoding(encoding_old);
1917 p.skip_spaces();
1918 }
1919
1920 else if (name == "lyxgreyedout") {
1921 eat_whitespace(p, os, parent_context, false);
1922 parent_context.check_layout(os);
1923 begin_inset(os, "Note Greyedout\n");
1924 os << "status open\n";
1925 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1926 end_inset(os);
1927 p.skip_spaces();
1928 if (!preamble.notefontcolor().empty())
1929 preamble.registerAutomaticallyLoadedPackage("color");
1930 }
1931
1932 else if (name == "btSect") {
1933 eat_whitespace(p, os, parent_context, false);
1934 parent_context.check_layout(os);
1935 begin_command_inset(os, "bibtex", "bibtex");
1936 string bibstyle = "plain";
1937 if (p.hasOpt()) {
1938 bibstyle = p.getArg('[', ']');
1939 p.skip_spaces(true);
1940 }
1941 string const bibfile = p.getArg('{', '}');
1942 eat_whitespace(p, os, parent_context, false);
1943 Token t = p.get_token();
1944 if (t.asInput() == "\\btPrintCited") {
1945 p.skip_spaces(true);
1946 os << "btprint " << '"' << "btPrintCited" << '"' << "\n";
1947 }
1948 if (t.asInput() == "\\btPrintNotCited") {
1949 p.skip_spaces(true);
1950 os << "btprint " << '"' << "btPrintNotCited" << '"' << "\n";
1951 }
1952 if (t.asInput() == "\\btPrintAll") {
1953 p.skip_spaces(true);
1954 os << "btprint " << '"' << "btPrintAll" << '"' << "\n";
1955 }
1956 os << "bibfiles " << '"' << bibfile << "\"\n"
1957 << "options " << '"' << bibstyle << "\"\n";
1958 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1959 end_inset(os);
1960 p.skip_spaces();
1961 }
1962
1963 else if (name == "btUnit") {
1964 string const nt = p.next_next_token().cs();
1965 // Do not attempt to overwrite a former diverging multibib.
1966 // Those are output as ERT instead.
1967 if ((nt == "part" || nt == "chapter"
1968 || nt == "section" || nt == "subsection")
1969 && (preamble.multibib().empty() || preamble.multibib() == nt)) {
1970 parse_text(p, os, FLAG_END, outer, parent_context);
1971 preamble.multibib(nt);
1972 } else
1973 parse_unknown_environment(p, name, os, FLAG_END, outer,
1974 parent_context);
1975 }
1976
1977 else if (name == "framed" || name == "shaded") {
1978 eat_whitespace(p, os, parent_context, false);
1979 parse_outer_box(p, os, FLAG_END, outer, parent_context, name, "");
1980 p.skip_spaces();
1981 preamble.registerAutomaticallyLoadedPackage("framed");
1982 }
1983
1984 else if (name == "listing") {
1985 minted_float = "float";
1986 eat_whitespace(p, os, parent_context, false);
1987 string const opt = p.hasOpt() ? p.getArg('[', ']') : string();
1988 if (!opt.empty())
1989 minted_float += "=" + opt;
1990 // If something precedes \begin{minted}, we output it at the end
1991 // as a caption, in order to keep it inside the listings inset.
1992 eat_whitespace(p, os, parent_context, true);
1993 p.pushPosition();
1994 Token const & t = p.get_token();
1995 p.skip_spaces(true);
1996 string const envname = p.next_token().cat() == catBegin
1997 ? p.getArg('{', '}') : string();
1998 bool prologue = t.asInput() != "\\begin" || envname != "minted";
1999 p.popPosition();
2000 minted_float_has_caption = false;
2001 string content = parse_text_snippet(p, FLAG_END, outer,
2002 parent_context);
2003 size_t i = content.find("\\begin_inset listings");
2004 bool minted_env = i != string::npos;
2005 string caption;
2006 if (prologue) {
2007 caption = content.substr(0, i);
2008 content.erase(0, i);
2009 }
2010 parent_context.check_layout(os);
2011 if (minted_env && minted_float_has_caption) {
2012 eat_whitespace(p, os, parent_context, true);
2013 os << content << "\n";
2014 if (!caption.empty())
2015 os << caption << "\n";
2016 os << "\n\\end_layout\n"; // close inner layout
2017 end_inset(os); // close caption inset
2018 os << "\n\\end_layout\n"; // close outer layout
2019 } else if (!caption.empty()) {
2020 if (!minted_env) {
2021 begin_inset(os, "listings\n");
2022 os << "lstparams " << '"' << minted_float << '"' << '\n';
2023 os << "inline false\n";
2024 os << "status collapsed\n";
2025 }
2026 os << "\n\\begin_layout Plain Layout\n";
2027 begin_inset(os, "Caption Standard\n");
2028 Context newcontext(true, parent_context.textclass,
2029 0, 0, parent_context.font);
2030 newcontext.check_layout(os);
2031 os << caption << "\n";
2032 newcontext.check_end_layout(os);
2033 end_inset(os);
2034 os << "\n\\end_layout\n";
2035 } else if (content.empty()) {
2036 begin_inset(os, "listings\n");
2037 os << "lstparams " << '"' << minted_float << '"' << '\n';
2038 os << "inline false\n";
2039 os << "status collapsed\n";
2040 } else {
2041 os << content << "\n";
2042 }
2043 end_inset(os); // close listings inset
2044 parent_context.check_end_layout(os);
2045 parent_context.new_paragraph(os);
2046 p.skip_spaces();
2047 minted_float.clear();
2048 minted_float_has_caption = false;
2049 }
2050
2051 else if (name == "lstlisting" || name == "minted") {
2052 bool use_minted = name == "minted";
2053 eat_whitespace(p, os, parent_context, false);
2054 if (use_minted && minted_float.empty()) {
2055 // look ahead for a bottom caption
2056 p.pushPosition();
2057 bool found_end_minted = false;
2058 while (!found_end_minted && p.good()) {
2059 Token const & t = p.get_token();
2060 p.skip_spaces();
2061 string const envname =
2062 p.next_token().cat() == catBegin
2063 ? p.getArg('{', '}') : string();
2064 found_end_minted = t.asInput() == "\\end"
2065 && envname == "minted";
2066 }
2067 eat_whitespace(p, os, parent_context, true);
2068 Token const & t = p.get_token();
2069 p.skip_spaces(true);
2070 if (t.asInput() == "\\lyxmintcaption") {
2071 string const pos = p.getArg('[', ']');
2072 if (pos == "b") {
2073 string const caption =
2074 parse_text_snippet(p, FLAG_ITEM,
2075 false, parent_context);
2076 minted_nonfloat_caption = "[b]" + caption;
2077 eat_whitespace(p, os, parent_context, true);
2078 }
2079 }
2080 p.popPosition();
2081 }
2082 parse_listings(p, os, parent_context, false, use_minted);
2083 p.skip_spaces();
2084 }
2085
2086 else if (!parent_context.new_layout_allowed)
2087 parse_unknown_environment(p, name, os, FLAG_END, outer,
2088 parent_context);
2089
2090 // Alignment and spacing settings
2091 // FIXME (bug xxxx): These settings can span multiple paragraphs and
2092 // therefore are totally broken!
2093 // Note that \centering, \raggedright, and \raggedleft cannot be handled, as
2094 // they are commands not environments. They are furthermore switches that
2095 // can be ended by another switches, but also by commands like \footnote or
2096 // \parbox. So the only safe way is to leave them untouched.
2097 // However, we support the pseudo-environments
2098 // \begin{centering} ... \end{centering}
2099 // \begin{raggedright} ... \end{raggedright}
2100 // \begin{raggedleft} ... \end{raggedleft}
2101 // since they are used by LyX in floats (for spacing reasons)
2102 else if (name == "center" || name == "centering" ||
2103 name == "flushleft" || name == "raggedright" ||
2104 name == "flushright" || name == "raggedleft" ||
2105 name == "singlespace" || name == "onehalfspace" ||
2106 name == "doublespace" || name == "spacing") {
2107 eat_whitespace(p, os, parent_context, false);
2108 // We must begin a new paragraph if not already done
2109 if (! parent_context.atParagraphStart()) {
2110 parent_context.check_end_layout(os);
2111 parent_context.new_paragraph(os);
2112 }
2113 if (name == "flushleft" || name == "raggedright")
2114 parent_context.add_extra_stuff("\\align left\n");
2115 else if (name == "flushright" || name == "raggedleft")
2116 parent_context.add_extra_stuff("\\align right\n");
2117 else if (name == "center" || name == "centering")
2118 parent_context.add_extra_stuff("\\align center\n");
2119 else if (name == "singlespace")
2120 parent_context.add_extra_stuff("\\paragraph_spacing single\n");
2121 else if (name == "onehalfspace") {
2122 parent_context.add_extra_stuff("\\paragraph_spacing onehalf\n");
2123 preamble.registerAutomaticallyLoadedPackage("setspace");
2124 } else if (name == "doublespace") {
2125 parent_context.add_extra_stuff("\\paragraph_spacing double\n");
2126 preamble.registerAutomaticallyLoadedPackage("setspace");
2127 } else if (name == "spacing") {
2128 parent_context.add_extra_stuff("\\paragraph_spacing other " + p.verbatim_item() + "\n");
2129 preamble.registerAutomaticallyLoadedPackage("setspace");
2130 }
2131 parse_text(p, os, FLAG_END, outer, parent_context);
2132 // Just in case the environment is empty
2133 parent_context.extra_stuff.erase();
2134 // We must begin a new paragraph to reset the alignment
2135 parent_context.new_paragraph(os);
2136 p.skip_spaces();
2137 }
2138
2139 // The single '=' is meant here.
2140 else if ((newlayout = findLayout(parent_context.textclass, name, false))) {
2141 eat_whitespace(p, os, parent_context, false);
2142 Context context(true, parent_context.textclass, newlayout,
2143 parent_context.layout, parent_context.font);
2144 if (parent_context.deeper_paragraph) {
2145 // We are beginning a nested environment after a
2146 // deeper paragraph inside the outer list environment.
2147 // Therefore we don't need to output a "begin deeper".
2148 context.need_end_deeper = true;
2149 }
2150 parent_context.check_end_layout(os);
2151 if (last_env == name) {
2152 // we need to output a separator since LyX would export
2153 // the two environments as one otherwise (bug 5716)
2154 TeX2LyXDocClass const & textclass(parent_context.textclass);
2155 Context newcontext(true, textclass,
2156 &(textclass.defaultLayout()));
2157 newcontext.check_layout(os);
2158 begin_inset(os, "Separator plain\n");
2159 end_inset(os);
2160 newcontext.check_end_layout(os);
2161 }
2162 switch (context.layout->latextype) {
2163 case LATEX_LIST_ENVIRONMENT:
2164 context.add_par_extra_stuff("\\labelwidthstring "
2165 + p.verbatim_item() + '\n');
2166 p.skip_spaces();
2167 break;
2168 case LATEX_BIB_ENVIRONMENT:
2169 p.verbatim_item(); // swallow next arg
2170 p.skip_spaces();
2171 break;
2172 default:
2173 break;
2174 }
2175 context.check_deeper(os);
2176 if (newlayout->keepempty) {
2177 // We need to start a new paragraph
2178 // even if it is empty.
2179 context.new_paragraph(os);
2180 context.check_layout(os);
2181 }
2182 // handle known optional and required arguments
2183 if (context.layout->latextype == LATEX_ENVIRONMENT)
2184 output_arguments(os, p, outer, false, string(), context,
2185 context.layout->latexargs());
2186 else if (context.layout->latextype == LATEX_ITEM_ENVIRONMENT) {
2187 ostringstream oss;
2188 output_arguments(oss, p, outer, false, string(), context,
2189 context.layout->latexargs());
2190 context.list_extra_stuff = oss.str();
2191 }
2192 parse_text(p, os, FLAG_END, outer, context);
2193 if (context.layout->latextype == LATEX_ENVIRONMENT)
2194 output_arguments(os, p, outer, false, "post", context,
2195 context.layout->postcommandargs());
2196 context.check_end_layout(os);
2197 if (parent_context.deeper_paragraph) {
2198 // We must suppress the "end deeper" because we
2199 // suppressed the "begin deeper" above.
2200 context.need_end_deeper = false;
2201 }
2202 context.check_end_deeper(os);
2203 parent_context.new_paragraph(os);
2204 p.skip_spaces();
2205 if (!preamble.titleLayoutFound())
2206 preamble.titleLayoutFound(newlayout->intitle);
2207 set<string> const & req = newlayout->requires();
2208 set<string>::const_iterator it = req.begin();
2209 set<string>::const_iterator en = req.end();
2210 for (; it != en; ++it)
2211 preamble.registerAutomaticallyLoadedPackage(*it);
2212 }
2213
2214 // The single '=' is meant here.
2215 else if ((newinsetlayout = findInsetLayout(parent_context.textclass, name, false))) {
2216 eat_whitespace(p, os, parent_context, false);
2217 parent_context.check_layout(os);
2218 begin_inset(os, "Flex ");
2219 os << to_utf8(newinsetlayout->name()) << '\n'
2220 << "status collapsed\n";
2221 if (newinsetlayout->isPassThru()) {
2222 string const arg = p.verbatimEnvironment(name);
2223 Context context(true, parent_context.textclass,
2224 &parent_context.textclass.plainLayout(),
2225 parent_context.layout);
2226 output_ert(os, arg, parent_context);
2227 } else
2228 parse_text_in_inset(p, os, FLAG_END, false, parent_context, newinsetlayout);
2229 end_inset(os);
2230 }
2231
2232 else if (name == "appendix") {
2233 // This is no good latex style, but it works and is used in some documents...
2234 eat_whitespace(p, os, parent_context, false);
2235 parent_context.check_end_layout(os);
2236 Context context(true, parent_context.textclass, parent_context.layout,
2237 parent_context.layout, parent_context.font);
2238 context.check_layout(os);
2239 os << "\\start_of_appendix\n";
2240 parse_text(p, os, FLAG_END, outer, context);
2241 context.check_end_layout(os);
2242 p.skip_spaces();
2243 }
2244
2245 else if (known_environments.find(name) != known_environments.end()) {
2246 vector<ArgumentType> arguments = known_environments[name];
2247 // The last "argument" denotes wether we may translate the
2248 // environment contents to LyX
2249 // The default required if no argument is given makes us
2250 // compatible with the reLyXre environment.
2251 ArgumentType contents = arguments.empty() ?
2252 required :
2253 arguments.back();
2254 if (!arguments.empty())
2255 arguments.pop_back();
2256 // See comment in parse_unknown_environment()
2257 bool const specialfont =
2258 (parent_context.font != parent_context.normalfont);
2259 bool const new_layout_allowed =
2260 parent_context.new_layout_allowed;
2261 if (specialfont)
2262 parent_context.new_layout_allowed = false;
2263 parse_arguments("\\begin{" + name + "}", arguments, p, os,
2264 outer, parent_context);
2265 if (contents == verbatim)
2266 output_ert_inset(os, p.ertEnvironment(name),
2267 parent_context);
2268 else
2269 parse_text_snippet(p, os, FLAG_END, outer,
2270 parent_context);
2271 output_ert_inset(os, "\\end{" + name + "}", parent_context);
2272 if (specialfont)
2273 parent_context.new_layout_allowed = new_layout_allowed;
2274 }
2275
2276 else
2277 parse_unknown_environment(p, name, os, FLAG_END, outer,
2278 parent_context);
2279
2280 last_env = name;
2281 active_environments.pop_back();
2282 }
2283
2284
2285 /// parses a comment and outputs it to \p os.
parse_comment(Parser & p,ostream & os,Token const & t,Context & context)2286 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
2287 {
2288 LASSERT(t.cat() == catComment, return);
2289 if (!t.cs().empty()) {
2290 context.check_layout(os);
2291 output_comment(p, os, t.cs(), context);
2292 if (p.next_token().cat() == catNewline) {
2293 // A newline after a comment line starts a new
2294 // paragraph
2295 if (context.new_layout_allowed) {
2296 if(!context.atParagraphStart())
2297 // Only start a new paragraph if not already
2298 // done (we might get called recursively)
2299 context.new_paragraph(os);
2300 } else
2301 output_ert_inset(os, "\n", context);
2302 eat_whitespace(p, os, context, true);
2303 }
2304 } else {
2305 // "%\n" combination
2306 p.skip_spaces();
2307 }
2308 }
2309
2310
2311 /*!
2312 * Reads spaces and comments until the first non-space, non-comment token.
2313 * New paragraphs (double newlines or \\par) are handled like simple spaces
2314 * if \p eatParagraph is true.
2315 * Spaces are skipped, but comments are written to \p os.
2316 */
eat_whitespace(Parser & p,ostream & os,Context & context,bool eatParagraph)2317 void eat_whitespace(Parser & p, ostream & os, Context & context,
2318 bool eatParagraph)
2319 {
2320 while (p.good()) {
2321 Token const & t = p.get_token();
2322 if (t.cat() == catComment)
2323 parse_comment(p, os, t, context);
2324 else if ((! eatParagraph && p.isParagraph()) ||
2325 (t.cat() != catSpace && t.cat() != catNewline)) {
2326 p.putback();
2327 return;
2328 }
2329 }
2330 }
2331
2332
2333 /*!
2334 * Set a font attribute, parse text and reset the font attribute.
2335 * \param attribute Attribute name (e.g. \\family, \\shape etc.)
2336 * \param currentvalue Current value of the attribute. Is set to the new
2337 * value during parsing.
2338 * \param newvalue New value of the attribute
2339 */
parse_text_attributes(Parser & p,ostream & os,unsigned flags,bool outer,Context & context,string const & attribute,string & currentvalue,string const & newvalue)2340 void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
2341 Context & context, string const & attribute,
2342 string & currentvalue, string const & newvalue)
2343 {
2344 context.check_layout(os);
2345 string const oldvalue = currentvalue;
2346 currentvalue = newvalue;
2347 os << '\n' << attribute << ' ' << newvalue << "\n";
2348 parse_text_snippet(p, os, flags, outer, context);
2349 context.check_layout(os);
2350 os << '\n' << attribute << ' ' << oldvalue << "\n";
2351 currentvalue = oldvalue;
2352 }
2353
2354
2355 /// get the arguments of a natbib or jurabib citation command
get_cite_arguments(Parser & p,bool natbibOrder,string & before,string & after,bool const qualified=false)2356 void get_cite_arguments(Parser & p, bool natbibOrder,
2357 string & before, string & after, bool const qualified = false)
2358 {
2359 // We need to distinguish "" and "[]", so we can't use p.getOpt().
2360
2361 // text before the citation
2362 before.clear();
2363 // text after the citation
2364 after = qualified ? p.getFullOpt(false, '(', ')') : p.getFullOpt();
2365
2366 if (!after.empty()) {
2367 before = qualified ? p.getFullOpt(false, '(', ')') : p.getFullOpt();
2368 if (natbibOrder && !before.empty())
2369 swap(before, after);
2370 }
2371 }
2372
2373
copy_file(FileName const & src,string dstname)2374 void copy_file(FileName const & src, string dstname)
2375 {
2376 if (!copyFiles())
2377 return;
2378 string const absParent = getParentFilePath(false);
2379 FileName dst;
2380 if (FileName::isAbsolute(dstname))
2381 dst = FileName(dstname);
2382 else
2383 dst = makeAbsPath(dstname, absParent);
2384 FileName const srcpath = src.onlyPath();
2385 FileName const dstpath = dst.onlyPath();
2386 if (equivalent(srcpath, dstpath))
2387 return;
2388 if (!dstpath.isDirectory()) {
2389 if (!dstpath.createPath()) {
2390 cerr << "Warning: Could not create directory for file `"
2391 << dst.absFileName() << "´." << endl;
2392 return;
2393 }
2394 }
2395 if (dst.isReadableFile()) {
2396 if (overwriteFiles())
2397 cerr << "Warning: Overwriting existing file `"
2398 << dst.absFileName() << "´." << endl;
2399 else {
2400 cerr << "Warning: Not overwriting existing file `"
2401 << dst.absFileName() << "´." << endl;
2402 return;
2403 }
2404 }
2405 if (!src.copyTo(dst))
2406 cerr << "Warning: Could not copy file `" << src.absFileName()
2407 << "´ to `" << dst.absFileName() << "´." << endl;
2408 }
2409
2410
2411 /// Parse a literate Chunk section. The initial "<<" is already parsed.
parse_chunk(Parser & p,ostream & os,Context & context)2412 bool parse_chunk(Parser & p, ostream & os, Context & context)
2413 {
2414 // check whether a chunk is possible here.
2415 if (!context.textclass.hasInsetLayout(from_ascii("Flex:Chunk"))) {
2416 return false;
2417 }
2418
2419 p.pushPosition();
2420
2421 // read the parameters
2422 Parser::Arg const params = p.verbatimStuff(">>=\n", false);
2423 if (!params.first) {
2424 p.popPosition();
2425 return false;
2426 }
2427
2428 Parser::Arg const code = p.verbatimStuff("\n@");
2429 if (!code.first) {
2430 p.popPosition();
2431 return false;
2432 }
2433 string const post_chunk = p.verbatimStuff("\n").second + '\n';
2434 if (post_chunk[0] != ' ' && post_chunk[0] != '\n') {
2435 p.popPosition();
2436 return false;
2437 }
2438 // The last newline read is important for paragraph handling
2439 p.putback();
2440 p.deparse();
2441
2442 //cerr << "params=[" << params.second << "], code=[" << code.second << "]" <<endl;
2443 // We must have a valid layout before outputting the Chunk inset.
2444 context.check_layout(os);
2445 Context chunkcontext(true, context.textclass);
2446 chunkcontext.layout = &context.textclass.plainLayout();
2447 begin_inset(os, "Flex Chunk");
2448 os << "\nstatus open\n";
2449 if (!params.second.empty()) {
2450 chunkcontext.check_layout(os);
2451 Context paramscontext(true, context.textclass);
2452 paramscontext.layout = &context.textclass.plainLayout();
2453 begin_inset(os, "Argument 1");
2454 os << "\nstatus open\n";
2455 output_ert(os, params.second, paramscontext);
2456 end_inset(os);
2457 }
2458 output_ert(os, code.second, chunkcontext);
2459 end_inset(os);
2460
2461 p.dropPosition();
2462 return true;
2463 }
2464
2465
2466 /// detects \\def, \\long\\def and \\global\\long\\def with ws and comments
is_macro(Parser & p)2467 bool is_macro(Parser & p)
2468 {
2469 Token first = p.curr_token();
2470 if (first.cat() != catEscape || !p.good())
2471 return false;
2472 if (first.cs() == "def")
2473 return true;
2474 if (first.cs() != "global" && first.cs() != "long")
2475 return false;
2476 Token second = p.get_token();
2477 int pos = 1;
2478 while (p.good() && !p.isParagraph() && (second.cat() == catSpace ||
2479 second.cat() == catNewline || second.cat() == catComment)) {
2480 second = p.get_token();
2481 pos++;
2482 }
2483 bool secondvalid = second.cat() == catEscape;
2484 Token third;
2485 bool thirdvalid = false;
2486 if (p.good() && first.cs() == "global" && secondvalid &&
2487 second.cs() == "long") {
2488 third = p.get_token();
2489 pos++;
2490 while (p.good() && !p.isParagraph() &&
2491 (third.cat() == catSpace ||
2492 third.cat() == catNewline ||
2493 third.cat() == catComment)) {
2494 third = p.get_token();
2495 pos++;
2496 }
2497 thirdvalid = third.cat() == catEscape;
2498 }
2499 for (int i = 0; i < pos; ++i)
2500 p.putback();
2501 if (!secondvalid)
2502 return false;
2503 if (!thirdvalid)
2504 return (first.cs() == "global" || first.cs() == "long") &&
2505 second.cs() == "def";
2506 return first.cs() == "global" && second.cs() == "long" &&
2507 third.cs() == "def";
2508 }
2509
2510
2511 /// Parse a macro definition (assumes that is_macro() returned true)
parse_macro(Parser & p,ostream & os,Context & context)2512 void parse_macro(Parser & p, ostream & os, Context & context)
2513 {
2514 context.check_layout(os);
2515 Token first = p.curr_token();
2516 Token second;
2517 Token third;
2518 string command = first.asInput();
2519 if (first.cs() != "def") {
2520 p.get_token();
2521 eat_whitespace(p, os, context, false);
2522 second = p.curr_token();
2523 command += second.asInput();
2524 if (second.cs() != "def") {
2525 p.get_token();
2526 eat_whitespace(p, os, context, false);
2527 third = p.curr_token();
2528 command += third.asInput();
2529 }
2530 }
2531 eat_whitespace(p, os, context, false);
2532 string const name = p.get_token().cs();
2533 eat_whitespace(p, os, context, false);
2534
2535 // parameter text
2536 bool simple = true;
2537 string paramtext;
2538 int arity = 0;
2539 while (p.next_token().cat() != catBegin) {
2540 if (p.next_token().cat() == catParameter) {
2541 // # found
2542 p.get_token();
2543 paramtext += "#";
2544
2545 // followed by number?
2546 if (p.next_token().cat() == catOther) {
2547 string s = p.get_token().asInput();
2548 paramtext += s;
2549 // number = current arity + 1?
2550 if (s.size() == 1 && s[0] == arity + '0' + 1)
2551 ++arity;
2552 else
2553 simple = false;
2554 } else
2555 paramtext += p.get_token().cs();
2556 } else {
2557 paramtext += p.get_token().cs();
2558 simple = false;
2559 }
2560 }
2561
2562 // only output simple (i.e. compatible) macro as FormulaMacros
2563 string ert = '\\' + name + ' ' + paramtext + '{' + p.verbatim_item() + '}';
2564 if (simple) {
2565 context.check_layout(os);
2566 begin_inset(os, "FormulaMacro");
2567 os << "\n\\def" << ert;
2568 end_inset(os);
2569 } else
2570 output_ert_inset(os, command + ert, context);
2571 }
2572
2573
registerExternalTemplatePackages(string const & name)2574 void registerExternalTemplatePackages(string const & name)
2575 {
2576 external::TemplateManager const & etm = external::TemplateManager::get();
2577 external::Template const * const et = etm.getTemplateByName(name);
2578 if (!et)
2579 return;
2580 external::Template::Formats::const_iterator cit = et->formats.end();
2581 if (pdflatex)
2582 cit = et->formats.find("PDFLaTeX");
2583 if (cit == et->formats.end())
2584 // If the template has not specified a PDFLaTeX output,
2585 // we try the LaTeX format.
2586 cit = et->formats.find("LaTeX");
2587 if (cit == et->formats.end())
2588 return;
2589 vector<string>::const_iterator qit = cit->second.requirements.begin();
2590 vector<string>::const_iterator qend = cit->second.requirements.end();
2591 for (; qit != qend; ++qit)
2592 preamble.registerAutomaticallyLoadedPackage(*qit);
2593 }
2594
2595 } // anonymous namespace
2596
2597
2598 /*!
2599 * Find a file with basename \p name in path \p path and an extension
2600 * in \p extensions.
2601 */
find_file(string const & name,string const & path,char const * const * extensions)2602 string find_file(string const & name, string const & path,
2603 char const * const * extensions)
2604 {
2605 for (char const * const * what = extensions; *what; ++what) {
2606 string const trial = addExtension(name, *what);
2607 if (makeAbsPath(trial, path).exists())
2608 return trial;
2609 }
2610 return string();
2611 }
2612
2613
2614 /// Convert filenames with TeX macros and/or quotes to something LyX
2615 /// can understand
normalize_filename(string const & name)2616 string const normalize_filename(string const & name)
2617 {
2618 Parser p(name);
2619 ostringstream os;
2620 while (p.good()) {
2621 Token const & t = p.get_token();
2622 if (t.cat() != catEscape)
2623 os << t.asInput();
2624 else if (t.cs() == "lyxdot") {
2625 // This is used by LyX for simple dots in relative
2626 // names
2627 os << '.';
2628 p.skip_spaces();
2629 } else if (t.cs() == "space") {
2630 os << ' ';
2631 p.skip_spaces();
2632 } else if (t.cs() == "string") {
2633 // Convert \string" to " and \string~ to ~
2634 Token const & n = p.next_token();
2635 if (n.asInput() != "\"" && n.asInput() != "~")
2636 os << t.asInput();
2637 } else
2638 os << t.asInput();
2639 }
2640 // Strip quotes. This is a bit complicated (see latex_path()).
2641 string full = os.str();
2642 if (!full.empty() && full[0] == '"') {
2643 string base = removeExtension(full);
2644 string ext = getExtension(full);
2645 if (!base.empty() && base[base.length()-1] == '"')
2646 // "a b"
2647 // "a b".tex
2648 return addExtension(trim(base, "\""), ext);
2649 if (full[full.length()-1] == '"')
2650 // "a b.c"
2651 // "a b.c".tex
2652 return trim(full, "\"");
2653 }
2654 return full;
2655 }
2656
2657
2658 /// Convert \p name from TeX convention (relative to master file) to LyX
2659 /// convention (relative to .lyx file) if it is relative
fix_child_filename(string & name)2660 void fix_child_filename(string & name)
2661 {
2662 string const absMasterTeX = getMasterFilePath(true);
2663 bool const isabs = FileName::isAbsolute(name);
2664 // convert from "relative to .tex master" to absolute original path
2665 if (!isabs)
2666 name = makeAbsPath(name, absMasterTeX).absFileName();
2667 bool copyfile = copyFiles();
2668 string const absParentLyX = getParentFilePath(false);
2669 string abs = name;
2670 if (copyfile) {
2671 // convert from absolute original path to "relative to master file"
2672 string const rel = to_utf8(makeRelPath(from_utf8(name),
2673 from_utf8(absMasterTeX)));
2674 // re-interpret "relative to .tex file" as "relative to .lyx file"
2675 // (is different if the master .lyx file resides in a
2676 // different path than the master .tex file)
2677 string const absMasterLyX = getMasterFilePath(false);
2678 abs = makeAbsPath(rel, absMasterLyX).absFileName();
2679 // Do not copy if the new path is impossible to create. Example:
2680 // absMasterTeX = "/foo/bar/"
2681 // absMasterLyX = "/bar/"
2682 // name = "/baz.eps" => new absolute name would be "/../baz.eps"
2683 if (contains(name, "/../"))
2684 copyfile = false;
2685 }
2686 if (copyfile) {
2687 if (isabs)
2688 name = abs;
2689 else {
2690 // convert from absolute original path to
2691 // "relative to .lyx file"
2692 name = to_utf8(makeRelPath(from_utf8(abs),
2693 from_utf8(absParentLyX)));
2694 }
2695 }
2696 else if (!isabs) {
2697 // convert from absolute original path to "relative to .lyx file"
2698 name = to_utf8(makeRelPath(from_utf8(name),
2699 from_utf8(absParentLyX)));
2700 }
2701 }
2702
2703
parse_text(Parser & p,ostream & os,unsigned flags,bool outer,Context & context,string const rdelim)2704 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
2705 Context & context, string const rdelim)
2706 {
2707 Layout const * newlayout = 0;
2708 InsetLayout const * newinsetlayout = 0;
2709 char const * const * where = 0;
2710 // Store the latest bibliographystyle, addcontentslineContent and
2711 // nocite{*} option (needed for bibtex inset)
2712 string btprint;
2713 string contentslineContent;
2714 // Some classes provide a \bibliographystyle, so do not output
2715 // any if none is explicitly set.
2716 string bibliographystyle;
2717 bool const use_natbib = isProvided("natbib");
2718 bool const use_jurabib = isProvided("jurabib");
2719 bool const use_biblatex = isProvided("biblatex")
2720 && preamble.citeEngine() != "biblatex-natbib";
2721 bool const use_biblatex_natbib = isProvided("biblatex-natbib")
2722 || (isProvided("biblatex") && preamble.citeEngine() == "biblatex-natbib");
2723 need_commentbib = use_biblatex || use_biblatex_natbib;
2724 string last_env;
2725
2726 // it is impossible to determine the correct encoding for non-CJK Japanese.
2727 // Therefore write a note at the beginning of the document
2728 if (is_nonCJKJapanese) {
2729 context.check_layout(os);
2730 begin_inset(os, "Note Note\n");
2731 os << "status open\n\\begin_layout Plain Layout\n"
2732 << "\\series bold\n"
2733 << "Important information:\n"
2734 << "\\end_layout\n\n"
2735 << "\\begin_layout Plain Layout\n"
2736 << "The original LaTeX source for this document is in Japanese (pLaTeX).\n"
2737 << " It was therefore impossible for tex2lyx to determine the correct encoding.\n"
2738 << " The iconv encoding " << p.getEncoding() << " was used.\n"
2739 << " If this is incorrect, you must run the tex2lyx program on the command line\n"
2740 << " and specify the encoding using the -e command-line switch.\n"
2741 << " In addition, you might want to double check that the desired output encoding\n"
2742 << " is correctly selected in Document > Settings > Language.\n"
2743 << "\\end_layout\n";
2744 end_inset(os);
2745 is_nonCJKJapanese = false;
2746 }
2747
2748 bool have_cycled = false;
2749 while (p.good()) {
2750 // Leave here only after at least one cycle
2751 if (have_cycled && flags & FLAG_LEAVE) {
2752 flags &= ~FLAG_LEAVE;
2753 break;
2754 }
2755
2756 Token const & t = p.get_token();
2757 #ifdef FILEDEBUG
2758 debugToken(cerr, t, flags);
2759 #endif
2760
2761 if (flags & FLAG_ITEM) {
2762 if (t.cat() == catSpace)
2763 continue;
2764
2765 flags &= ~FLAG_ITEM;
2766 if (t.cat() == catBegin) {
2767 // skip the brace and collect everything to the next matching
2768 // closing brace
2769 flags |= FLAG_BRACE_LAST;
2770 continue;
2771 }
2772
2773 // handle only this single token, leave the loop if done
2774 flags |= FLAG_LEAVE;
2775 }
2776
2777 if (t.cat() != catEscape && t.character() == ']' &&
2778 (flags & FLAG_BRACK_LAST))
2779 return;
2780 if (t.cat() == catEnd && (flags & FLAG_BRACE_LAST))
2781 return;
2782 string tok = t.asInput();
2783 // we only support delimiters with max 2 chars for now.
2784 if (rdelim.size() > 1)
2785 tok += p.next_token().asInput();
2786 if (t.cat() != catEscape && !rdelim.empty()
2787 && tok == rdelim && (flags & FLAG_RDELIM)) {
2788 if (rdelim.size() > 1)
2789 p.get_token(); // eat rdelim
2790 return;
2791 }
2792
2793 // If there is anything between \end{env} and \begin{env} we
2794 // don't need to output a separator.
2795 if (t.cat() != catSpace && t.cat() != catNewline &&
2796 t.asInput() != "\\begin")
2797 last_env = "";
2798
2799 //
2800 // cat codes
2801 //
2802 have_cycled = true;
2803 bool const starred = p.next_token().asInput() == "*";
2804 string const starredname(starred ? (t.cs() + '*') : t.cs());
2805 if (t.cat() == catMath) {
2806 // we are inside some text mode thingy, so opening new math is allowed
2807 context.check_layout(os);
2808 begin_inset(os, "Formula ");
2809 Token const & n = p.get_token();
2810 bool const display(n.cat() == catMath && outer);
2811 if (display) {
2812 // TeX's $$...$$ syntax for displayed math
2813 os << "\\[";
2814 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
2815 os << "\\]";
2816 p.get_token(); // skip the second '$' token
2817 } else {
2818 // simple $...$ stuff
2819 p.putback();
2820 os << '$';
2821 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
2822 os << '$';
2823 }
2824 end_inset(os);
2825 if (display) {
2826 // Prevent the conversion of a line break to a
2827 // space (bug 7668). This does not change the
2828 // output, but looks ugly in LyX.
2829 eat_whitespace(p, os, context, false);
2830 }
2831 continue;
2832 }
2833
2834 if (t.cat() == catSuper || t.cat() == catSub) {
2835 cerr << "catcode " << t << " illegal in text mode\n";
2836 continue;
2837 }
2838
2839 // Basic support for quotes. We try to disambiguate
2840 // quotes from the context (e.g., a left english quote is
2841 // the same as a right german quote...).
2842 // Try to make a smart guess about the side
2843 Token const prev = p.prev_token();
2844 bool const opening = (prev.cat() != catSpace && prev.character() != 0
2845 && prev.character() != '\n' && prev.character() != '~');
2846 if (t.asInput() == "`" && p.next_token().asInput() == "`") {
2847 context.check_layout(os);
2848 begin_inset(os, "Quotes ");
2849 os << guessQuoteStyle("eld", opening);
2850 end_inset(os);
2851 p.get_token();
2852 skip_braces(p);
2853 continue;
2854 }
2855 if (t.asInput() == "'" && p.next_token().asInput() == "'") {
2856 context.check_layout(os);
2857 begin_inset(os, "Quotes ");
2858 os << guessQuoteStyle("erd", opening);
2859 end_inset(os);
2860 p.get_token();
2861 skip_braces(p);
2862 continue;
2863 }
2864
2865 if (t.asInput() == ">" && p.next_token().asInput() == ">") {
2866 context.check_layout(os);
2867 begin_inset(os, "Quotes ");
2868 os << guessQuoteStyle("ald", opening);
2869 end_inset(os);
2870 p.get_token();
2871 skip_braces(p);
2872 continue;
2873 }
2874
2875 if (t.asInput() == "<"
2876 && p.next_token().asInput() == "<") {
2877 bool has_chunk = false;
2878 if (noweb_mode) {
2879 p.pushPosition();
2880 p.get_token();
2881 has_chunk = parse_chunk(p, os, context);
2882 if (!has_chunk)
2883 p.popPosition();
2884 }
2885
2886 if (!has_chunk) {
2887 context.check_layout(os);
2888 begin_inset(os, "Quotes ");
2889 os << guessQuoteStyle("ard", opening);
2890 end_inset(os);
2891 p.get_token();
2892 skip_braces(p);
2893 }
2894 continue;
2895 }
2896
2897 if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph())) {
2898 check_space(p, os, context);
2899 continue;
2900 }
2901
2902 // babel shorthands (also used by polyglossia)
2903 // Since these can have different meanings for different languages
2904 // we import them as ERT (but they must be put in ERT to get output
2905 // verbatim).
2906 if (t.asInput() == "\"") {
2907 string s = "\"";
2908 // These are known pairs. We put them together in
2909 // one ERT inset. In other cases (such as "a), only
2910 // the quotation mark is ERTed.
2911 if (p.next_token().asInput() == "\""
2912 || p.next_token().asInput() == "|"
2913 || p.next_token().asInput() == "-"
2914 || p.next_token().asInput() == "~"
2915 || p.next_token().asInput() == "="
2916 || p.next_token().asInput() == "/"
2917 || p.next_token().asInput() == "~"
2918 || p.next_token().asInput() == "'"
2919 || p.next_token().asInput() == "`"
2920 || p.next_token().asInput() == "<"
2921 || p.next_token().asInput() == ">") {
2922 s += p.next_token().asInput();
2923 p.get_token();
2924 }
2925 output_ert_inset(os, s, context);
2926 continue;
2927 }
2928
2929 if (t.character() == '[' && noweb_mode &&
2930 p.next_token().character() == '[') {
2931 // These can contain underscores
2932 p.putback();
2933 string const s = p.getFullOpt() + ']';
2934 if (p.next_token().character() == ']')
2935 p.get_token();
2936 else
2937 cerr << "Warning: Inserting missing ']' in '"
2938 << s << "'." << endl;
2939 output_ert_inset(os, s, context);
2940 continue;
2941 }
2942
2943 if (t.cat() == catLetter) {
2944 context.check_layout(os);
2945 os << t.cs();
2946 continue;
2947 }
2948
2949 if (t.cat() == catOther ||
2950 t.cat() == catAlign ||
2951 t.cat() == catParameter) {
2952 context.check_layout(os);
2953 if (t.asInput() == "-" && p.next_token().asInput() == "-" &&
2954 context.merging_hyphens_allowed &&
2955 context.font.family != "ttfamily" &&
2956 !context.layout->pass_thru) {
2957 if (p.next_next_token().asInput() == "-") {
2958 // --- is emdash
2959 os << to_utf8(docstring(1, 0x2014));
2960 p.get_token();
2961 } else
2962 // -- is endash
2963 os << to_utf8(docstring(1, 0x2013));
2964 p.get_token();
2965 } else
2966 // This translates "&" to "\\&" which may be wrong...
2967 os << t.cs();
2968 continue;
2969 }
2970
2971 if (p.isParagraph()) {
2972 // In minted floating listings we will collect
2973 // everything into the caption, where multiple
2974 // paragraphs are forbidden.
2975 if (minted_float.empty()) {
2976 if (context.new_layout_allowed)
2977 context.new_paragraph(os);
2978 else
2979 output_ert_inset(os, "\\par ", context);
2980 } else
2981 os << ' ';
2982 eat_whitespace(p, os, context, true);
2983 continue;
2984 }
2985
2986 if (t.cat() == catActive) {
2987 context.check_layout(os);
2988 if (t.character() == '~') {
2989 if (context.layout->free_spacing)
2990 os << ' ';
2991 else {
2992 begin_inset(os, "space ~\n");
2993 end_inset(os);
2994 }
2995 } else
2996 os << t.cs();
2997 continue;
2998 }
2999
3000 if (t.cat() == catBegin) {
3001 Token const next = p.next_token();
3002 Token const end = p.next_next_token();
3003 if (next.cat() == catEnd) {
3004 // {}
3005 Token const prev = p.prev_token();
3006 p.get_token();
3007 if (p.next_token().character() == '`')
3008 ; // ignore it in {}``
3009 else
3010 output_ert_inset(os, "{}", context);
3011 } else if (next.cat() == catEscape &&
3012 is_known(next.cs(), known_quotes) &&
3013 end.cat() == catEnd) {
3014 // Something like {\textquoteright} (e.g.
3015 // from writer2latex). We may skip the
3016 // braces here for better readability.
3017 parse_text_snippet(p, os, FLAG_BRACE_LAST,
3018 outer, context);
3019 } else if (p.next_token().asInput() == "\\ascii") {
3020 // handle the \ascii characters
3021 // (the case without braces is handled later)
3022 // the code is "{\ascii\xxx}"
3023 p.get_token(); // eat \ascii
3024 string name2 = p.get_token().asInput();
3025 p.get_token(); // eat the final '}'
3026 string const name = "{\\ascii" + name2 + "}";
3027 bool termination;
3028 docstring rem;
3029 set<string> req;
3030 // get the character from unicodesymbols
3031 docstring s = encodings.fromLaTeXCommand(from_utf8(name),
3032 Encodings::TEXT_CMD, termination, rem, &req);
3033 if (!s.empty()) {
3034 context.check_layout(os);
3035 os << to_utf8(s);
3036 if (!rem.empty())
3037 output_ert_inset(os,
3038 to_utf8(rem), context);
3039 for (set<string>::const_iterator it = req.begin();
3040 it != req.end(); ++it)
3041 preamble.registerAutomaticallyLoadedPackage(*it);
3042 } else
3043 // we did not find a non-ert version
3044 output_ert_inset(os, name, context);
3045 } else {
3046 context.check_layout(os);
3047 // special handling of font attribute changes
3048 Token const prev = p.prev_token();
3049 TeXFont const oldFont = context.font;
3050 if (next.character() == '[' ||
3051 next.character() == ']' ||
3052 next.character() == '*') {
3053 p.get_token();
3054 if (p.next_token().cat() == catEnd) {
3055 os << next.cs();
3056 p.get_token();
3057 } else {
3058 p.putback();
3059 output_ert_inset(os, "{", context);
3060 parse_text_snippet(p, os,
3061 FLAG_BRACE_LAST,
3062 outer, context);
3063 output_ert_inset(os, "}", context);
3064 }
3065 } else if (! context.new_layout_allowed) {
3066 output_ert_inset(os, "{", context);
3067 parse_text_snippet(p, os, FLAG_BRACE_LAST,
3068 outer, context);
3069 output_ert_inset(os, "}", context);
3070 } else if (is_known(next.cs(), known_sizes)) {
3071 // next will change the size, so we must
3072 // reset it here
3073 parse_text_snippet(p, os, FLAG_BRACE_LAST,
3074 outer, context);
3075 if (!context.atParagraphStart())
3076 os << "\n\\size "
3077 << context.font.size << "\n";
3078 } else if (is_known(next.cs(), known_font_families)) {
3079 // next will change the font family, so we
3080 // must reset it here
3081 parse_text_snippet(p, os, FLAG_BRACE_LAST,
3082 outer, context);
3083 if (!context.atParagraphStart())
3084 os << "\n\\family "
3085 << context.font.family << "\n";
3086 } else if (is_known(next.cs(), known_font_series)) {
3087 // next will change the font series, so we
3088 // must reset it here
3089 parse_text_snippet(p, os, FLAG_BRACE_LAST,
3090 outer, context);
3091 if (!context.atParagraphStart())
3092 os << "\n\\series "
3093 << context.font.series << "\n";
3094 } else if (is_known(next.cs(), known_font_shapes)) {
3095 // next will change the font shape, so we
3096 // must reset it here
3097 parse_text_snippet(p, os, FLAG_BRACE_LAST,
3098 outer, context);
3099 if (!context.atParagraphStart())
3100 os << "\n\\shape "
3101 << context.font.shape << "\n";
3102 } else if (is_known(next.cs(), known_old_font_families) ||
3103 is_known(next.cs(), known_old_font_series) ||
3104 is_known(next.cs(), known_old_font_shapes)) {
3105 // next will change the font family, series
3106 // and shape, so we must reset it here
3107 parse_text_snippet(p, os, FLAG_BRACE_LAST,
3108 outer, context);
3109 if (!context.atParagraphStart())
3110 os << "\n\\family "
3111 << context.font.family
3112 << "\n\\series "
3113 << context.font.series
3114 << "\n\\shape "
3115 << context.font.shape << "\n";
3116 } else {
3117 output_ert_inset(os, "{", context);
3118 parse_text_snippet(p, os, FLAG_BRACE_LAST,
3119 outer, context);
3120 output_ert_inset(os, "}", context);
3121 }
3122 }
3123 continue;
3124 }
3125
3126 if (t.cat() == catEnd) {
3127 if (flags & FLAG_BRACE_LAST) {
3128 return;
3129 }
3130 cerr << "stray '}' in text\n";
3131 output_ert_inset(os, "}", context);
3132 continue;
3133 }
3134
3135 if (t.cat() == catComment) {
3136 parse_comment(p, os, t, context);
3137 continue;
3138 }
3139
3140 //
3141 // control sequences
3142 //
3143
3144 if (t.cs() == "(" || t.cs() == "[") {
3145 bool const simple = t.cs() == "(";
3146 context.check_layout(os);
3147 begin_inset(os, "Formula");
3148 os << " \\" << t.cs();
3149 parse_math(p, os, simple ? FLAG_SIMPLE2 : FLAG_EQUATION, MATH_MODE);
3150 os << '\\' << (simple ? ')' : ']');
3151 end_inset(os);
3152 if (!simple) {
3153 // Prevent the conversion of a line break to a
3154 // space (bug 7668). This does not change the
3155 // output, but looks ugly in LyX.
3156 eat_whitespace(p, os, context, false);
3157 }
3158 continue;
3159 }
3160
3161 if (t.cs() == "begin") {
3162 parse_environment(p, os, outer, last_env,
3163 context);
3164 continue;
3165 }
3166
3167 if (t.cs() == "end") {
3168 if (flags & FLAG_END) {
3169 // eat environment name
3170 string const name = p.getArg('{', '}');
3171 if (name != active_environment())
3172 cerr << "\\end{" + name + "} does not match \\begin{"
3173 + active_environment() + "}\n";
3174 return;
3175 }
3176 p.error("found 'end' unexpectedly");
3177 continue;
3178 }
3179
3180 // "item" by default, but could be something else
3181 if (t.cs() == context.layout->itemcommand()) {
3182 string s;
3183 if (context.layout->labeltype == LABEL_MANUAL) {
3184 // FIXME: This swallows comments, but we cannot use
3185 // eat_whitespace() since we must not output
3186 // anything before the item.
3187 p.skip_spaces(true);
3188 s = p.verbatimOption();
3189 } else
3190 p.skip_spaces(false);
3191 context.set_item();
3192 context.check_layout(os);
3193 if (context.has_item) {
3194 // An item in an unknown list-like environment
3195 // FIXME: Do this in check_layout()!
3196 context.has_item = false;
3197 string item = "\\" + context.layout->itemcommand();
3198 if (!p.hasOpt())
3199 item += " ";
3200 output_ert_inset(os, item, context);
3201 }
3202 if (context.layout->labeltype != LABEL_MANUAL)
3203 output_arguments(os, p, outer, false, "item", context,
3204 context.layout->itemargs());
3205 if (!context.list_extra_stuff.empty()) {
3206 os << context.list_extra_stuff;
3207 context.list_extra_stuff.clear();
3208 }
3209 else if (!s.empty()) {
3210 // LyX adds braces around the argument,
3211 // so we need to remove them here.
3212 if (s.size() > 2 && s[0] == '{' &&
3213 s[s.size()-1] == '}')
3214 s = s.substr(1, s.size()-2);
3215 // If the argument contains a space we
3216 // must put it into ERT: Otherwise LyX
3217 // would misinterpret the space as
3218 // item delimiter (bug 7663)
3219 if (contains(s, ' ')) {
3220 output_ert_inset(os, s, context);
3221 } else {
3222 Parser p2(s + ']');
3223 os << parse_text_snippet(p2,
3224 FLAG_BRACK_LAST, outer, context);
3225 }
3226 // The space is needed to separate the
3227 // item from the rest of the sentence.
3228 os << ' ';
3229 eat_whitespace(p, os, context, false);
3230 }
3231 continue;
3232 }
3233
3234 if (t.cs() == "bibitem") {
3235 context.set_item();
3236 context.check_layout(os);
3237 eat_whitespace(p, os, context, false);
3238 string label = p.verbatimOption();
3239 pair<bool, string> lbl = convert_latexed_command_inset_arg(label);
3240 bool const literal = !lbl.first;
3241 label = literal ? subst(label, "\n", " ") : lbl.second;
3242 string lit = literal ? "\"true\"" : "\"false\"";
3243 string key = convert_literate_command_inset_arg(p.verbatim_item());
3244 begin_command_inset(os, "bibitem", "bibitem");
3245 os << "label \"" << label << "\"\n"
3246 << "key \"" << key << "\"\n"
3247 << "literal " << lit << "\n";
3248 end_inset(os);
3249 continue;
3250 }
3251
3252 if (is_macro(p)) {
3253 // catch the case of \def\inputGnumericTable
3254 bool macro = true;
3255 if (t.cs() == "def") {
3256 Token second = p.next_token();
3257 if (second.cs() == "inputGnumericTable") {
3258 p.pushPosition();
3259 p.get_token();
3260 skip_braces(p);
3261 Token third = p.get_token();
3262 p.popPosition();
3263 if (third.cs() == "input") {
3264 p.get_token();
3265 skip_braces(p);
3266 p.get_token();
3267 string name = normalize_filename(p.verbatim_item());
3268 string const path = getMasterFilePath(true);
3269 // We want to preserve relative / absolute filenames,
3270 // therefore path is only used for testing
3271 // The file extension is in every case ".tex".
3272 // So we need to remove this extension and check for
3273 // the original one.
3274 name = removeExtension(name);
3275 if (!makeAbsPath(name, path).exists()) {
3276 char const * const Gnumeric_formats[] = {"gnumeric",
3277 "ods", "xls", 0};
3278 string const Gnumeric_name =
3279 find_file(name, path, Gnumeric_formats);
3280 if (!Gnumeric_name.empty())
3281 name = Gnumeric_name;
3282 }
3283 FileName const absname = makeAbsPath(name, path);
3284 if (absname.exists()) {
3285 fix_child_filename(name);
3286 copy_file(absname, name);
3287 } else
3288 cerr << "Warning: Could not find file '"
3289 << name << "'." << endl;
3290 context.check_layout(os);
3291 begin_inset(os, "External\n\ttemplate ");
3292 os << "GnumericSpreadsheet\n\tfilename "
3293 << name << "\n";
3294 end_inset(os);
3295 context.check_layout(os);
3296 macro = false;
3297 // register the packages that are automatically loaded
3298 // by the Gnumeric template
3299 registerExternalTemplatePackages("GnumericSpreadsheet");
3300 }
3301 }
3302 }
3303 if (macro)
3304 parse_macro(p, os, context);
3305 continue;
3306 }
3307
3308 if (t.cs() == "noindent") {
3309 p.skip_spaces();
3310 context.add_par_extra_stuff("\\noindent\n");
3311 continue;
3312 }
3313
3314 if (t.cs() == "appendix") {
3315 context.add_par_extra_stuff("\\start_of_appendix\n");
3316 // We need to start a new paragraph. Otherwise the
3317 // appendix in 'bla\appendix\chapter{' would start
3318 // too late.
3319 context.new_paragraph(os);
3320 // We need to make sure that the paragraph is
3321 // generated even if it is empty. Otherwise the
3322 // appendix in '\par\appendix\par\chapter{' would
3323 // start too late.
3324 context.check_layout(os);
3325 // FIXME: This is a hack to prevent paragraph
3326 // deletion if it is empty. Handle this better!
3327 output_comment(p, os,
3328 "dummy comment inserted by tex2lyx to "
3329 "ensure that this paragraph is not empty",
3330 context);
3331 // Both measures above may generate an additional
3332 // empty paragraph, but that does not hurt, because
3333 // whitespace does not matter here.
3334 eat_whitespace(p, os, context, true);
3335 continue;
3336 }
3337
3338 // Must catch empty dates before findLayout is called below
3339 if (t.cs() == "date") {
3340 eat_whitespace(p, os, context, false);
3341 p.pushPosition();
3342 string const date = p.verbatim_item();
3343 p.popPosition();
3344 if (date.empty()) {
3345 preamble.suppressDate(true);
3346 p.verbatim_item();
3347 } else {
3348 preamble.suppressDate(false);
3349 if (context.new_layout_allowed &&
3350 (newlayout = findLayout(context.textclass,
3351 t.cs(), true))) {
3352 // write the layout
3353 output_command_layout(os, p, outer,
3354 context, newlayout);
3355 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
3356 if (!preamble.titleLayoutFound())
3357 preamble.titleLayoutFound(newlayout->intitle);
3358 set<string> const & req = newlayout->requires();
3359 set<string>::const_iterator it = req.begin();
3360 set<string>::const_iterator en = req.end();
3361 for (; it != en; ++it)
3362 preamble.registerAutomaticallyLoadedPackage(*it);
3363 } else
3364 output_ert_inset(os,
3365 "\\date{" + p.verbatim_item() + '}',
3366 context);
3367 }
3368 continue;
3369 }
3370
3371 // Before we look for the layout name with star and alone below, we check the layouts including
3372 // the LateXParam, which might be one or several options or a star.
3373 // The single '=' is meant here.
3374 if (context.new_layout_allowed &&
3375 (newlayout = findLayout(context.textclass, t.cs(), true, p.getCommandLatexParam()))) {
3376 // store the latexparam here. This is eaten in output_command_layout
3377 context.latexparam = newlayout->latexparam();
3378 // write the layout
3379 output_command_layout(os, p, outer, context, newlayout);
3380 context.latexparam.clear();
3381 p.skip_spaces();
3382 if (!preamble.titleLayoutFound())
3383 preamble.titleLayoutFound(newlayout->intitle);
3384 set<string> const & req = newlayout->requires();
3385 for (set<string>::const_iterator it = req.begin(); it != req.end(); ++it)
3386 preamble.registerAutomaticallyLoadedPackage(*it);
3387 continue;
3388 }
3389
3390 // Starred section headings
3391 // Must attempt to parse "Section*" before "Section".
3392 if ((p.next_token().asInput() == "*") &&
3393 context.new_layout_allowed &&
3394 (newlayout = findLayout(context.textclass, t.cs() + '*', true))) {
3395 // write the layout
3396 p.get_token();
3397 output_command_layout(os, p, outer, context, newlayout);
3398 p.skip_spaces();
3399 if (!preamble.titleLayoutFound())
3400 preamble.titleLayoutFound(newlayout->intitle);
3401 set<string> const & req = newlayout->requires();
3402 for (set<string>::const_iterator it = req.begin(); it != req.end(); ++it)
3403 preamble.registerAutomaticallyLoadedPackage(*it);
3404 continue;
3405 }
3406
3407 // Section headings and the like
3408 if (context.new_layout_allowed &&
3409 (newlayout = findLayout(context.textclass, t.cs(), true))) {
3410 // write the layout
3411 output_command_layout(os, p, outer, context, newlayout);
3412 p.skip_spaces();
3413 if (!preamble.titleLayoutFound())
3414 preamble.titleLayoutFound(newlayout->intitle);
3415 set<string> const & req = newlayout->requires();
3416 for (set<string>::const_iterator it = req.begin(); it != req.end(); ++it)
3417 preamble.registerAutomaticallyLoadedPackage(*it);
3418 continue;
3419 }
3420
3421 if (t.cs() == "subfloat") {
3422 // the syntax is \subfloat[list entry][sub caption]{content}
3423 // if it is a table of figure depends on the surrounding float
3424 p.skip_spaces();
3425 // do nothing if there is no outer float
3426 if (!float_type.empty()) {
3427 context.check_layout(os);
3428 p.skip_spaces();
3429 begin_inset(os, "Float " + float_type + "\n");
3430 os << "wide false"
3431 << "\nsideways false"
3432 << "\nstatus collapsed\n\n";
3433 // test for caption
3434 string caption;
3435 bool has_caption = false;
3436 if (p.next_token().cat() != catEscape &&
3437 p.next_token().character() == '[') {
3438 p.get_token(); // eat '['
3439 caption = parse_text_snippet(p, FLAG_BRACK_LAST, outer, context);
3440 has_caption = true;
3441 }
3442 // In case we have two optional args, the second is the caption.
3443 if (p.next_token().cat() != catEscape &&
3444 p.next_token().character() == '[') {
3445 p.get_token(); // eat '['
3446 caption = parse_text_snippet(p, FLAG_BRACK_LAST, outer, context);
3447 }
3448 // the content
3449 parse_text_in_inset(p, os, FLAG_ITEM, outer, context);
3450 // the caption comes always as the last
3451 if (has_caption) {
3452 // we must make sure that the caption gets a \begin_layout
3453 os << "\n\\begin_layout Plain Layout";
3454 p.skip_spaces();
3455 begin_inset(os, "Caption Standard\n");
3456 Context newcontext(true, context.textclass,
3457 0, 0, context.font);
3458 newcontext.check_layout(os);
3459 os << caption << "\n";
3460 newcontext.check_end_layout(os);
3461 end_inset(os);
3462 p.skip_spaces();
3463 // close the layout we opened
3464 os << "\n\\end_layout";
3465 }
3466 end_inset(os);
3467 p.skip_spaces();
3468 } else {
3469 // if the float type is not supported or there is no surrounding float
3470 // output it as ERT
3471 string opt_arg1;
3472 string opt_arg2;
3473 if (p.hasOpt()) {
3474 opt_arg1 = convert_literate_command_inset_arg(p.getFullOpt());
3475 if (p.hasOpt())
3476 opt_arg2 = convert_literate_command_inset_arg(p.getFullOpt());
3477 }
3478 output_ert_inset(os, t.asInput() + opt_arg1 + opt_arg2
3479 + "{" + p.verbatim_item() + '}', context);
3480 }
3481 continue;
3482 }
3483
3484 if (t.cs() == "xymatrix") {
3485 // we must open a new math because LyX's xy support is in math
3486 context.check_layout(os);
3487 begin_inset(os, "Formula ");
3488 os << '$';
3489 os << "\\" << t.cs() << '{';
3490 parse_math(p, os, FLAG_ITEM, MATH_MODE);
3491 os << '}' << '$';
3492 end_inset(os);
3493 preamble.registerAutomaticallyLoadedPackage("xy");
3494 continue;
3495 }
3496
3497 if (t.cs() == "includegraphics") {
3498 bool const clip = p.next_token().asInput() == "*";
3499 if (clip)
3500 p.get_token();
3501 string const arg = p.getArg('[', ']');
3502 map<string, string> opts;
3503 vector<string> keys;
3504 split_map(arg, opts, keys);
3505 if (clip)
3506 opts["clip"] = string();
3507 string name = normalize_filename(p.verbatim_item());
3508
3509 string const path = getMasterFilePath(true);
3510 // We want to preserve relative / absolute filenames,
3511 // therefore path is only used for testing
3512 if (!makeAbsPath(name, path).exists()) {
3513 // The file extension is probably missing.
3514 // Now try to find it out.
3515 string const dvips_name =
3516 find_file(name, path,
3517 known_dvips_graphics_formats);
3518 string const pdftex_name =
3519 find_file(name, path,
3520 known_pdftex_graphics_formats);
3521 if (!dvips_name.empty()) {
3522 if (!pdftex_name.empty()) {
3523 cerr << "This file contains the "
3524 "latex snippet\n"
3525 "\"\\includegraphics{"
3526 << name << "}\".\n"
3527 "However, files\n\""
3528 << dvips_name << "\" and\n\""
3529 << pdftex_name << "\"\n"
3530 "both exist, so I had to make a "
3531 "choice and took the first one.\n"
3532 "Please move the unwanted one "
3533 "someplace else and try again\n"
3534 "if my choice was wrong."
3535 << endl;
3536 }
3537 name = dvips_name;
3538 } else if (!pdftex_name.empty()) {
3539 name = pdftex_name;
3540 pdflatex = true;
3541 }
3542 }
3543
3544 FileName const absname = makeAbsPath(name, path);
3545 if (absname.exists()) {
3546 fix_child_filename(name);
3547 copy_file(absname, name);
3548 } else
3549 cerr << "Warning: Could not find graphics file '"
3550 << name << "'." << endl;
3551
3552 context.check_layout(os);
3553 begin_inset(os, "Graphics ");
3554 os << "\n\tfilename " << name << '\n';
3555 if (opts.find("width") != opts.end())
3556 os << "\twidth "
3557 << translate_len(opts["width"]) << '\n';
3558 if (opts.find("height") != opts.end())
3559 os << "\theight "
3560 << translate_len(opts["height"]) << '\n';
3561 if (opts.find("scale") != opts.end()) {
3562 istringstream iss(opts["scale"]);
3563 double val;
3564 iss >> val;
3565 val = val*100;
3566 os << "\tscale " << val << '\n';
3567 }
3568 if (opts.find("angle") != opts.end()) {
3569 os << "\trotateAngle "
3570 << opts["angle"] << '\n';
3571 vector<string>::const_iterator a =
3572 find(keys.begin(), keys.end(), "angle");
3573 vector<string>::const_iterator s =
3574 find(keys.begin(), keys.end(), "width");
3575 if (s == keys.end())
3576 s = find(keys.begin(), keys.end(), "height");
3577 if (s == keys.end())
3578 s = find(keys.begin(), keys.end(), "scale");
3579 if (s != keys.end() && distance(s, a) > 0)
3580 os << "\tscaleBeforeRotation\n";
3581 }
3582 if (opts.find("origin") != opts.end()) {
3583 ostringstream ss;
3584 string const opt = opts["origin"];
3585 if (opt.find('l') != string::npos) ss << "left";
3586 if (opt.find('r') != string::npos) ss << "right";
3587 if (opt.find('c') != string::npos) ss << "center";
3588 if (opt.find('t') != string::npos) ss << "Top";
3589 if (opt.find('b') != string::npos) ss << "Bottom";
3590 if (opt.find('B') != string::npos) ss << "Baseline";
3591 if (!ss.str().empty())
3592 os << "\trotateOrigin " << ss.str() << '\n';
3593 else
3594 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
3595 }
3596 if (opts.find("keepaspectratio") != opts.end())
3597 os << "\tkeepAspectRatio\n";
3598 if (opts.find("clip") != opts.end())
3599 os << "\tclip\n";
3600 if (opts.find("draft") != opts.end())
3601 os << "\tdraft\n";
3602 if (opts.find("bb") != opts.end())
3603 os << "\tBoundingBox "
3604 << opts["bb"] << '\n';
3605 int numberOfbbOptions = 0;
3606 if (opts.find("bbllx") != opts.end())
3607 numberOfbbOptions++;
3608 if (opts.find("bblly") != opts.end())
3609 numberOfbbOptions++;
3610 if (opts.find("bburx") != opts.end())
3611 numberOfbbOptions++;
3612 if (opts.find("bbury") != opts.end())
3613 numberOfbbOptions++;
3614 if (numberOfbbOptions == 4)
3615 os << "\tBoundingBox "
3616 << opts["bbllx"] << " " << opts["bblly"] << " "
3617 << opts["bburx"] << " " << opts["bbury"] << '\n';
3618 else if (numberOfbbOptions > 0)
3619 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
3620 numberOfbbOptions = 0;
3621 if (opts.find("natwidth") != opts.end())
3622 numberOfbbOptions++;
3623 if (opts.find("natheight") != opts.end())
3624 numberOfbbOptions++;
3625 if (numberOfbbOptions == 2)
3626 os << "\tBoundingBox 0bp 0bp "
3627 << opts["natwidth"] << " " << opts["natheight"] << '\n';
3628 else if (numberOfbbOptions > 0)
3629 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
3630 ostringstream special;
3631 if (opts.find("hiresbb") != opts.end())
3632 special << "hiresbb,";
3633 if (opts.find("trim") != opts.end())
3634 special << "trim,";
3635 if (opts.find("viewport") != opts.end())
3636 special << "viewport=" << opts["viewport"] << ',';
3637 if (opts.find("totalheight") != opts.end())
3638 special << "totalheight=" << opts["totalheight"] << ',';
3639 if (opts.find("type") != opts.end())
3640 special << "type=" << opts["type"] << ',';
3641 if (opts.find("ext") != opts.end())
3642 special << "ext=" << opts["ext"] << ',';
3643 if (opts.find("read") != opts.end())
3644 special << "read=" << opts["read"] << ',';
3645 if (opts.find("command") != opts.end())
3646 special << "command=" << opts["command"] << ',';
3647 string s_special = special.str();
3648 if (!s_special.empty()) {
3649 // We had special arguments. Remove the trailing ','.
3650 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
3651 }
3652 // TODO: Handle the unknown settings better.
3653 // Warn about invalid options.
3654 // Check whether some option was given twice.
3655 end_inset(os);
3656 preamble.registerAutomaticallyLoadedPackage("graphicx");
3657 continue;
3658 }
3659
3660 if (t.cs() == "footnote" ||
3661 (t.cs() == "thanks" && context.layout->intitle)) {
3662 p.skip_spaces();
3663 context.check_layout(os);
3664 begin_inset(os, "Foot\n");
3665 os << "status collapsed\n\n";
3666 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
3667 end_inset(os);
3668 continue;
3669 }
3670
3671 if (t.cs() == "marginpar") {
3672 p.skip_spaces();
3673 context.check_layout(os);
3674 begin_inset(os, "Marginal\n");
3675 os << "status collapsed\n\n";
3676 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
3677 end_inset(os);
3678 continue;
3679 }
3680
3681 if (t.cs() == "lstinline" || t.cs() == "mintinline") {
3682 bool const use_minted = t.cs() == "mintinline";
3683 p.skip_spaces();
3684 parse_listings(p, os, context, true, use_minted);
3685 continue;
3686 }
3687
3688 if (t.cs() == "ensuremath") {
3689 p.skip_spaces();
3690 context.check_layout(os);
3691 string const s = p.verbatim_item();
3692 //FIXME: this never triggers in UTF8
3693 if (s == "\xb1" || s == "\xb3" || s == "\xb2" || s == "\xb5")
3694 os << s;
3695 else
3696 output_ert_inset(os, "\\ensuremath{" + s + "}",
3697 context);
3698 continue;
3699 }
3700
3701 else if (t.cs() == "makeindex"
3702 || (t.cs() == context.textclass.titlename()
3703 && context.textclass.titletype() == TITLE_COMMAND_AFTER)) {
3704 if (preamble.titleLayoutFound()) {
3705 // swallow this
3706 skip_spaces_braces(p);
3707 } else
3708 output_ert_inset(os, t.asInput(), context);
3709 continue;
3710 }
3711
3712 if (t.cs() == "tableofcontents"
3713 || t.cs() == "lstlistoflistings"
3714 || t.cs() == "listoflistings") {
3715 string name = t.cs();
3716 if (preamble.minted() && name == "listoflistings")
3717 name.insert(0, "lst");
3718 context.check_layout(os);
3719 begin_command_inset(os, "toc", name);
3720 end_inset(os);
3721 skip_spaces_braces(p);
3722 if (name == "lstlistoflistings") {
3723 if (preamble.minted())
3724 preamble.registerAutomaticallyLoadedPackage("minted");
3725 else
3726 preamble.registerAutomaticallyLoadedPackage("listings");
3727 }
3728 continue;
3729 }
3730
3731 if (t.cs() == "listoffigures" || t.cs() == "listoftables") {
3732 context.check_layout(os);
3733 if (t.cs() == "listoffigures")
3734 begin_inset(os, "FloatList figure\n");
3735 else
3736 begin_inset(os, "FloatList table\n");
3737 end_inset(os);
3738 skip_spaces_braces(p);
3739 continue;
3740 }
3741
3742 if (t.cs() == "listof") {
3743 p.skip_spaces(true);
3744 string const name = p.verbatim_item();
3745 if (context.textclass.floats().typeExist(name)) {
3746 context.check_layout(os);
3747 begin_inset(os, "FloatList ");
3748 os << name << "\n";
3749 end_inset(os);
3750 p.verbatim_item(); // swallow second arg
3751 } else
3752 output_ert_inset(os, "\\listof{" + name + "}", context);
3753 continue;
3754 }
3755
3756 if ((where = is_known(t.cs(), known_text_font_families))) {
3757 parse_text_attributes(p, os, FLAG_ITEM, outer,
3758 context, "\\family", context.font.family,
3759 known_coded_font_families[where - known_text_font_families]);
3760 continue;
3761 }
3762
3763 // beamer has a \textbf<overlay>{} inset
3764 if (!p.hasOpt("<") && (where = is_known(t.cs(), known_text_font_series))) {
3765 parse_text_attributes(p, os, FLAG_ITEM, outer,
3766 context, "\\series", context.font.series,
3767 known_coded_font_series[where - known_text_font_series]);
3768 continue;
3769 }
3770
3771 // beamer has a \textit<overlay>{} inset
3772 if (!p.hasOpt("<") && (where = is_known(t.cs(), known_text_font_shapes))) {
3773 parse_text_attributes(p, os, FLAG_ITEM, outer,
3774 context, "\\shape", context.font.shape,
3775 known_coded_font_shapes[where - known_text_font_shapes]);
3776 continue;
3777 }
3778
3779 if (t.cs() == "textnormal" || t.cs() == "normalfont") {
3780 context.check_layout(os);
3781 TeXFont oldFont = context.font;
3782 context.font.init();
3783 context.font.size = oldFont.size;
3784 os << "\n\\family " << context.font.family << "\n";
3785 os << "\n\\series " << context.font.series << "\n";
3786 os << "\n\\shape " << context.font.shape << "\n";
3787 if (t.cs() == "textnormal") {
3788 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
3789 output_font_change(os, context.font, oldFont);
3790 context.font = oldFont;
3791 } else
3792 eat_whitespace(p, os, context, false);
3793 continue;
3794 }
3795
3796 if (t.cs() == "textcolor") {
3797 // scheme is \textcolor{color name}{text}
3798 string const color = p.verbatim_item();
3799 // we support the predefined colors of the color and the xcolor package
3800 if (color == "black" || color == "blue" || color == "cyan"
3801 || color == "green" || color == "magenta" || color == "red"
3802 || color == "white" || color == "yellow") {
3803 context.check_layout(os);
3804 os << "\n\\color " << color << "\n";
3805 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
3806 context.check_layout(os);
3807 os << "\n\\color inherit\n";
3808 preamble.registerAutomaticallyLoadedPackage("color");
3809 } else if (color == "brown" || color == "darkgray" || color == "gray"
3810 || color == "lightgray" || color == "lime" || color == "olive"
3811 || color == "orange" || color == "pink" || color == "purple"
3812 || color == "teal" || color == "violet") {
3813 context.check_layout(os);
3814 os << "\n\\color " << color << "\n";
3815 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
3816 context.check_layout(os);
3817 os << "\n\\color inherit\n";
3818 preamble.registerAutomaticallyLoadedPackage("xcolor");
3819 } else
3820 // for custom defined colors
3821 output_ert_inset(os, t.asInput() + "{" + color + "}", context);
3822 continue;
3823 }
3824
3825 if (t.cs() == "underbar" || t.cs() == "uline") {
3826 // \underbar is not 100% correct (LyX outputs \uline
3827 // of ulem.sty). The difference is that \ulem allows
3828 // line breaks, and \underbar does not.
3829 // Do NOT handle \underline.
3830 // \underbar cuts through y, g, q, p etc.,
3831 // \underline does not.
3832 context.check_layout(os);
3833 os << "\n\\bar under\n";
3834 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
3835 context.check_layout(os);
3836 os << "\n\\bar default\n";
3837 preamble.registerAutomaticallyLoadedPackage("ulem");
3838 continue;
3839 }
3840
3841 if (t.cs() == "sout") {
3842 context.check_layout(os);
3843 os << "\n\\strikeout on\n";
3844 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
3845 context.check_layout(os);
3846 os << "\n\\strikeout default\n";
3847 preamble.registerAutomaticallyLoadedPackage("ulem");
3848 continue;
3849 }
3850
3851 // beamer has an \emph<overlay>{} inset
3852 if ((t.cs() == "uuline" || t.cs() == "uwave"
3853 || t.cs() == "emph" || t.cs() == "noun"
3854 || t.cs() == "xout") && !p.hasOpt("<")) {
3855 context.check_layout(os);
3856 os << "\n\\" << t.cs() << " on\n";
3857 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
3858 context.check_layout(os);
3859 os << "\n\\" << t.cs() << " default\n";
3860 if (t.cs() == "uuline" || t.cs() == "uwave" || t.cs() == "xout")
3861 preamble.registerAutomaticallyLoadedPackage("ulem");
3862 continue;
3863 }
3864
3865 if (t.cs() == "lyxadded" || t.cs() == "lyxdeleted") {
3866 context.check_layout(os);
3867 string name = p.getArg('{', '}');
3868 string localtime = p.getArg('{', '}');
3869 preamble.registerAuthor(name);
3870 Author const & author = preamble.getAuthor(name);
3871 // from_asctime_utc() will fail if LyX decides to output the
3872 // time in the text language.
3873 time_t ptime = from_asctime_utc(localtime);
3874 if (ptime == static_cast<time_t>(-1)) {
3875 cerr << "Warning: Could not parse time `" << localtime
3876 << "´ for change tracking, using current time instead.\n";
3877 ptime = current_time();
3878 }
3879 if (t.cs() == "lyxadded")
3880 os << "\n\\change_inserted ";
3881 else
3882 os << "\n\\change_deleted ";
3883 os << author.bufferId() << ' ' << ptime << '\n';
3884 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
3885 bool dvipost = LaTeXPackages::isAvailable("dvipost");
3886 bool xcolorulem = LaTeXPackages::isAvailable("ulem") &&
3887 LaTeXPackages::isAvailable("xcolor");
3888 // No need to test for luatex, since luatex comes in
3889 // two flavours (dvi and pdf), like latex, and those
3890 // are detected by pdflatex.
3891 if (pdflatex || xetex) {
3892 if (xcolorulem) {
3893 preamble.registerAutomaticallyLoadedPackage("ulem");
3894 preamble.registerAutomaticallyLoadedPackage("xcolor");
3895 preamble.registerAutomaticallyLoadedPackage("pdfcolmk");
3896 }
3897 } else {
3898 if (dvipost) {
3899 preamble.registerAutomaticallyLoadedPackage("dvipost");
3900 } else if (xcolorulem) {
3901 preamble.registerAutomaticallyLoadedPackage("ulem");
3902 preamble.registerAutomaticallyLoadedPackage("xcolor");
3903 }
3904 }
3905 continue;
3906 }
3907
3908 if (t.cs() == "textipa") {
3909 context.check_layout(os);
3910 begin_inset(os, "IPA\n");
3911 bool merging_hyphens_allowed = context.merging_hyphens_allowed;
3912 context.merging_hyphens_allowed = false;
3913 parse_text_in_inset(p, os, FLAG_ITEM, outer, context);
3914 context.merging_hyphens_allowed = merging_hyphens_allowed;
3915 end_inset(os);
3916 preamble.registerAutomaticallyLoadedPackage("tipa");
3917 preamble.registerAutomaticallyLoadedPackage("tipx");
3918 continue;
3919 }
3920
3921 if ((preamble.isPackageUsed("tipa") && t.cs() == "t" && p.next_token().asInput() == "*")
3922 || t.cs() == "texttoptiebar" || t.cs() == "textbottomtiebar") {
3923 context.check_layout(os);
3924 if (t.cs() == "t")
3925 // swallow star
3926 p.get_token();
3927 string const type = (t.cs() == "t") ? "bottomtiebar" : t.cs().substr(4);
3928 begin_inset(os, "IPADeco " + type + "\n");
3929 os << "status open\n";
3930 parse_text_in_inset(p, os, FLAG_ITEM, outer, context);
3931 end_inset(os);
3932 p.skip_spaces();
3933 continue;
3934 }
3935
3936 if (t.cs() == "textvertline") {
3937 // FIXME: This is not correct, \textvertline is higher than |
3938 os << "|";
3939 skip_braces(p);
3940 continue;
3941 }
3942
3943 if (t.cs() == "tone" ) {
3944 context.check_layout(os);
3945 // register the tone package
3946 preamble.registerAutomaticallyLoadedPackage("tone");
3947 string content = trimSpaceAndEol(p.verbatim_item());
3948 string command = t.asInput() + "{" + content + "}";
3949 // some tones can be detected by unicodesymbols, some need special code
3950 if (is_known(content, known_tones)) {
3951 os << "\\IPAChar " << command << "\n";
3952 continue;
3953 }
3954 // try to see whether the string is in unicodesymbols
3955 bool termination;
3956 docstring rem;
3957 set<string> req;
3958 docstring s = encodings.fromLaTeXCommand(from_utf8(command),
3959 Encodings::TEXT_CMD | Encodings::MATH_CMD,
3960 termination, rem, &req);
3961 if (!s.empty()) {
3962 os << to_utf8(s);
3963 if (!rem.empty())
3964 output_ert_inset(os, to_utf8(rem), context);
3965 for (set<string>::const_iterator it = req.begin();
3966 it != req.end(); ++it)
3967 preamble.registerAutomaticallyLoadedPackage(*it);
3968 } else
3969 // we did not find a non-ert version
3970 output_ert_inset(os, command, context);
3971 continue;
3972 }
3973
3974 if (t.cs() == "phantom" || t.cs() == "hphantom" ||
3975 t.cs() == "vphantom") {
3976 context.check_layout(os);
3977 if (t.cs() == "phantom")
3978 begin_inset(os, "Phantom Phantom\n");
3979 if (t.cs() == "hphantom")
3980 begin_inset(os, "Phantom HPhantom\n");
3981 if (t.cs() == "vphantom")
3982 begin_inset(os, "Phantom VPhantom\n");
3983 os << "status open\n";
3984 parse_text_in_inset(p, os, FLAG_ITEM, outer, context,
3985 "Phantom");
3986 end_inset(os);
3987 continue;
3988 }
3989
3990 if (t.cs() == "href") {
3991 context.check_layout(os);
3992 string target = convert_literate_command_inset_arg(p.verbatim_item());
3993 string name = p.verbatim_item();
3994 pair<bool, string> nm = convert_latexed_command_inset_arg(name);
3995 bool const literal = !nm.first;
3996 name = literal ? subst(name, "\n", " ") : nm.second;
3997 string lit = literal ? "\"true\"" : "\"false\"";
3998 string type;
3999 size_t i = target.find(':');
4000 if (i != string::npos) {
4001 type = target.substr(0, i + 1);
4002 if (type == "mailto:" || type == "file:")
4003 target = target.substr(i + 1);
4004 // handle the case that name is equal to target, except of "http(s)://"
4005 else if (target.substr(i + 3) == name && (type == "http:" || type == "https:"))
4006 target = name;
4007 }
4008 begin_command_inset(os, "href", "href");
4009 if (name != target)
4010 os << "name \"" << name << "\"\n";
4011 os << "target \"" << target << "\"\n";
4012 if (type == "mailto:" || type == "file:")
4013 os << "type \"" << type << "\"\n";
4014 os << "literal " << lit << "\n";
4015 end_inset(os);
4016 skip_spaces_braces(p);
4017 continue;
4018 }
4019
4020 if (t.cs() == "lyxline") {
4021 // swallow size argument (it is not used anyway)
4022 p.getArg('{', '}');
4023 if (!context.atParagraphStart()) {
4024 // so our line is in the middle of a paragraph
4025 // we need to add a new line, lest this line
4026 // follow the other content on that line and
4027 // run off the side of the page
4028 // FIXME: This may create an empty paragraph,
4029 // but without that it would not be
4030 // possible to set noindent below.
4031 // Fortunately LaTeX does not care
4032 // about the empty paragraph.
4033 context.new_paragraph(os);
4034 }
4035 if (preamble.indentParagraphs()) {
4036 // we need to unindent, lest the line be too long
4037 context.add_par_extra_stuff("\\noindent\n");
4038 }
4039 context.check_layout(os);
4040 begin_command_inset(os, "line", "rule");
4041 os << "offset \"0.5ex\"\n"
4042 "width \"100line%\"\n"
4043 "height \"1pt\"\n";
4044 end_inset(os);
4045 continue;
4046 }
4047
4048 if (t.cs() == "rule") {
4049 string const offset = (p.hasOpt() ? p.getArg('[', ']') : string());
4050 string const width = p.getArg('{', '}');
4051 string const thickness = p.getArg('{', '}');
4052 context.check_layout(os);
4053 begin_command_inset(os, "line", "rule");
4054 if (!offset.empty())
4055 os << "offset \"" << translate_len(offset) << "\"\n";
4056 os << "width \"" << translate_len(width) << "\"\n"
4057 "height \"" << translate_len(thickness) << "\"\n";
4058 end_inset(os);
4059 continue;
4060 }
4061
4062 // Handle refstyle first in order to to catch \eqref, because this
4063 // can also occur without refstyle. Only recognize these commands if
4064 // refstyle.sty was found in the preamble (otherwise \eqref
4065 // and user defined ref commands could be misdetected).
4066 // We uncapitalize the input in order to catch capitalized commands
4067 // such as \Eqref.
4068 if ((where = is_known(uncapitalize(t.cs()), known_refstyle_commands))
4069 && preamble.refstyle()) {
4070 string const cap = isCapitalized(t.cs()) ? "true" : "false";
4071 string plural = "false";
4072 // Catch the plural option [s]
4073 if (p.hasOpt()) {
4074 string const opt = p.getOpt();
4075 if (opt == "[s]")
4076 plural = "true";
4077 else {
4078 // LyX does not yet support other optional arguments of ref commands
4079 output_ert_inset(os, t.asInput() + opt + "{" +
4080 p.verbatim_item() + '}', context);
4081 continue;
4082 }
4083 }
4084 context.check_layout(os);
4085 begin_command_inset(os, "ref", "formatted");
4086 os << "reference \"";
4087 os << known_refstyle_prefixes[where - known_refstyle_commands]
4088 << ":";
4089 os << convert_literate_command_inset_arg(p.getArg('{', '}'))
4090 << "\"\n";
4091 os << "plural \"" << plural << "\"\n";
4092 os << "caps \"" << cap << "\"\n";
4093 os << "noprefix \"false\"\n";
4094 end_inset(os);
4095 preamble.registerAutomaticallyLoadedPackage("refstyle");
4096 continue;
4097 }
4098
4099 // if refstyle is used, we must not convert \prettyref to a
4100 // formatted reference, since that would result in a refstyle command.
4101 if ((where = is_known(t.cs(), known_ref_commands)) &&
4102 (t.cs() != "prettyref" || !preamble.refstyle())) {
4103 string const opt = p.getOpt();
4104 if (opt.empty()) {
4105 context.check_layout(os);
4106 begin_command_inset(os, "ref",
4107 known_coded_ref_commands[where - known_ref_commands]);
4108 os << "reference \""
4109 << convert_literate_command_inset_arg(p.verbatim_item())
4110 << "\"\n";
4111 os << "plural \"false\"\n";
4112 os << "caps \"false\"\n";
4113 os << "noprefix \"false\"\n";
4114 end_inset(os);
4115 if (t.cs() == "vref" || t.cs() == "vpageref")
4116 preamble.registerAutomaticallyLoadedPackage("varioref");
4117 else if (t.cs() == "prettyref")
4118 preamble.registerAutomaticallyLoadedPackage("prettyref");
4119 } else {
4120 // LyX does not yet support optional arguments of ref commands
4121 output_ert_inset(os, t.asInput() + opt + "{" +
4122 p.verbatim_item() + '}', context);
4123 }
4124 continue;
4125 }
4126
4127 if (use_natbib &&
4128 is_known(t.cs(), known_natbib_commands) &&
4129 ((t.cs() != "citefullauthor" &&
4130 t.cs() != "citeyear" &&
4131 t.cs() != "citeyearpar") ||
4132 p.next_token().asInput() != "*")) {
4133 context.check_layout(os);
4134 string command = t.cs();
4135 if (p.next_token().asInput() == "*") {
4136 command += '*';
4137 p.get_token();
4138 }
4139 if (command == "citefullauthor")
4140 // alternative name for "\\citeauthor*"
4141 command = "citeauthor*";
4142
4143 // text before the citation
4144 string before;
4145 // text after the citation
4146 string after;
4147 get_cite_arguments(p, true, before, after);
4148
4149 if (command == "cite") {
4150 // \cite without optional argument means
4151 // \citet, \cite with at least one optional
4152 // argument means \citep.
4153 if (before.empty() && after.empty())
4154 command = "citet";
4155 else
4156 command = "citep";
4157 }
4158 if (before.empty() && after == "[]")
4159 // avoid \citet[]{a}
4160 after.erase();
4161 else if (before == "[]" && after == "[]") {
4162 // avoid \citet[][]{a}
4163 before.erase();
4164 after.erase();
4165 }
4166 bool literal = false;
4167 pair<bool, string> aft;
4168 pair<bool, string> bef;
4169 // remove the brackets around after and before
4170 if (!after.empty()) {
4171 after.erase(0, 1);
4172 after.erase(after.length() - 1, 1);
4173 aft = convert_latexed_command_inset_arg(after);
4174 literal = !aft.first;
4175 after = literal ? subst(after, "\n", " ") : aft.second;
4176 }
4177 if (!before.empty()) {
4178 before.erase(0, 1);
4179 before.erase(before.length() - 1, 1);
4180 bef = convert_latexed_command_inset_arg(before);
4181 literal |= !bef.first;
4182 before = literal ? subst(before, "\n", " ") : bef.second;
4183 if (literal && !after.empty())
4184 after = subst(after, "\n", " ");
4185 }
4186 string lit = literal ? "\"true\"" : "\"false\"";
4187 begin_command_inset(os, "citation", command);
4188 os << "after " << '"' << after << '"' << "\n";
4189 os << "before " << '"' << before << '"' << "\n";
4190 os << "key \""
4191 << convert_literate_command_inset_arg(p.verbatim_item())
4192 << "\"\n"
4193 << "literal " << lit << "\n";
4194 end_inset(os);
4195 // Need to set the cite engine if natbib is loaded by
4196 // the document class directly
4197 if (preamble.citeEngine() == "basic")
4198 preamble.citeEngine("natbib");
4199 continue;
4200 }
4201
4202 if ((use_biblatex
4203 && is_known(t.cs(), known_biblatex_commands)
4204 && ((t.cs() == "cite"
4205 || t.cs() == "citeauthor"
4206 || t.cs() == "Citeauthor"
4207 || t.cs() == "parencite"
4208 || t.cs() == "citetitle")
4209 || p.next_token().asInput() != "*"))
4210 || (use_biblatex_natbib
4211 && (is_known(t.cs(), known_biblatex_commands)
4212 || is_known(t.cs(), known_natbib_commands))
4213 && ((t.cs() == "cite" || t.cs() == "citet" || t.cs() == "Citet"
4214 || t.cs() == "citep" || t.cs() == "Citep" || t.cs() == "citealt"
4215 || t.cs() == "Citealt" || t.cs() == "citealp" || t.cs() == "Citealp"
4216 || t.cs() == "citeauthor" || t.cs() == "Citeauthor"
4217 || t.cs() == "parencite" || t.cs() == "citetitle")
4218 || p.next_token().asInput() != "*"))){
4219 context.check_layout(os);
4220 string command = t.cs();
4221 if (p.next_token().asInput() == "*") {
4222 command += '*';
4223 p.get_token();
4224 }
4225
4226 bool const qualified = suffixIs(command, "s");
4227 if (qualified)
4228 command = rtrim(command, "s");
4229
4230 // text before the citation
4231 string before;
4232 // text after the citation
4233 string after;
4234 get_cite_arguments(p, true, before, after, qualified);
4235
4236 // These use natbib cmd names in LyX
4237 // for inter-citeengine compativility
4238 if (command == "citeyear")
4239 command = "citebyear";
4240 else if (command == "cite*")
4241 command = "citeyear";
4242 else if (command == "textcite")
4243 command = "citet";
4244 else if (command == "Textcite")
4245 command = "Citet";
4246 else if (command == "parencite")
4247 command = "citep";
4248 else if (command == "Parencite")
4249 command = "Citep";
4250 else if (command == "parencite*")
4251 command = "citeyearpar";
4252 else if (command == "smartcite")
4253 command = "footcite";
4254 else if (command == "Smartcite")
4255 command = "Footcite";
4256
4257 string const emptyarg = qualified ? "()" : "[]";
4258 if (before.empty() && after == emptyarg)
4259 // avoid \cite[]{a}
4260 after.erase();
4261 else if (before == emptyarg && after == emptyarg) {
4262 // avoid \cite[][]{a}
4263 before.erase();
4264 after.erase();
4265 }
4266 bool literal = false;
4267 pair<bool, string> aft;
4268 pair<bool, string> bef;
4269 // remove the brackets around after and before
4270 if (!after.empty()) {
4271 after.erase(0, 1);
4272 after.erase(after.length() - 1, 1);
4273 aft = convert_latexed_command_inset_arg(after);
4274 literal = !aft.first;
4275 after = literal ? subst(after, "\n", " ") : aft.second;
4276 }
4277 if (!before.empty()) {
4278 before.erase(0, 1);
4279 before.erase(before.length() - 1, 1);
4280 bef = convert_latexed_command_inset_arg(before);
4281 literal |= !bef.first;
4282 before = literal ? subst(before, "\n", " ") : bef.second;
4283 }
4284 string keys, pretextlist, posttextlist;
4285 if (qualified) {
4286 map<string, string> pres, posts, preslit, postslit;
4287 vector<string> lkeys;
4288 // text before the citation
4289 string lbefore, lbeforelit;
4290 // text after the citation
4291 string lafter, lafterlit;
4292 string lkey;
4293 pair<bool, string> laft, lbef;
4294 while (true) {
4295 get_cite_arguments(p, true, lbefore, lafter);
4296 // remove the brackets around after and before
4297 if (!lafter.empty()) {
4298 lafter.erase(0, 1);
4299 lafter.erase(lafter.length() - 1, 1);
4300 laft = convert_latexed_command_inset_arg(lafter);
4301 literal |= !laft.first;
4302 lafter = laft.second;
4303 lafterlit = subst(lbefore, "\n", " ");
4304 }
4305 if (!lbefore.empty()) {
4306 lbefore.erase(0, 1);
4307 lbefore.erase(lbefore.length() - 1, 1);
4308 lbef = convert_latexed_command_inset_arg(lbefore);
4309 literal |= !lbef.first;
4310 lbefore = lbef.second;
4311 lbeforelit = subst(lbefore, "\n", " ");
4312 }
4313 if (lbefore.empty() && lafter == "[]") {
4314 // avoid \cite[]{a}
4315 lafter.erase();
4316 lafterlit.erase();
4317 }
4318 else if (lbefore == "[]" && lafter == "[]") {
4319 // avoid \cite[][]{a}
4320 lbefore.erase();
4321 lafter.erase();
4322 lbeforelit.erase();
4323 lafterlit.erase();
4324 }
4325 lkey = p.getArg('{', '}');
4326 if (lkey.empty())
4327 break;
4328 if (!lbefore.empty()) {
4329 pres.insert(make_pair(lkey, lbefore));
4330 preslit.insert(make_pair(lkey, lbeforelit));
4331 }
4332 if (!lafter.empty()) {
4333 posts.insert(make_pair(lkey, lafter));
4334 postslit.insert(make_pair(lkey, lafterlit));
4335 }
4336 lkeys.push_back(lkey);
4337 }
4338 keys = convert_literate_command_inset_arg(getStringFromVector(lkeys));
4339 if (literal) {
4340 pres = preslit;
4341 posts = postslit;
4342 }
4343 for (auto const & ptl : pres) {
4344 if (!pretextlist.empty())
4345 pretextlist += '\t';
4346 pretextlist += ptl.first + " " + ptl.second;
4347 }
4348 for (auto const & potl : posts) {
4349 if (!posttextlist.empty())
4350 posttextlist += '\t';
4351 posttextlist += potl.first + " " + potl.second;
4352 }
4353 } else
4354 keys = convert_literate_command_inset_arg(p.verbatim_item());
4355 if (literal) {
4356 if (!after.empty())
4357 after = subst(after, "\n", " ");
4358 if (!before.empty())
4359 before = subst(after, "\n", " ");
4360 }
4361 string lit = literal ? "\"true\"" : "\"false\"";
4362 begin_command_inset(os, "citation", command);
4363 os << "after " << '"' << after << '"' << "\n";
4364 os << "before " << '"' << before << '"' << "\n";
4365 os << "key \""
4366 << keys
4367 << "\"\n";
4368 if (!pretextlist.empty())
4369 os << "pretextlist " << '"' << pretextlist << '"' << "\n";
4370 if (!posttextlist.empty())
4371 os << "posttextlist " << '"' << posttextlist << '"' << "\n";
4372 os << "literal " << lit << "\n";
4373 end_inset(os);
4374 // Need to set the cite engine if biblatex is loaded by
4375 // the document class directly
4376 if (preamble.citeEngine() == "basic")
4377 use_biblatex_natbib ?
4378 preamble.citeEngine("biblatex-natbib")
4379 : preamble.citeEngine("biblatex");
4380 continue;
4381 }
4382
4383 if (use_jurabib &&
4384 is_known(t.cs(), known_jurabib_commands) &&
4385 (t.cs() == "cite" || p.next_token().asInput() != "*")) {
4386 context.check_layout(os);
4387 string command = t.cs();
4388 if (p.next_token().asInput() == "*") {
4389 command += '*';
4390 p.get_token();
4391 }
4392 char argumentOrder = '\0';
4393 vector<string> const options =
4394 preamble.getPackageOptions("jurabib");
4395 if (find(options.begin(), options.end(),
4396 "natbiborder") != options.end())
4397 argumentOrder = 'n';
4398 else if (find(options.begin(), options.end(),
4399 "jurabiborder") != options.end())
4400 argumentOrder = 'j';
4401
4402 // text before the citation
4403 string before;
4404 // text after the citation
4405 string after;
4406 get_cite_arguments(p, argumentOrder != 'j', before, after);
4407
4408 string const citation = p.verbatim_item();
4409 if (!before.empty() && argumentOrder == '\0') {
4410 cerr << "Warning: Assuming argument order "
4411 "of jurabib version 0.6 for\n'"
4412 << command << before << after << '{'
4413 << citation << "}'.\n"
4414 "Add 'jurabiborder' to the jurabib "
4415 "package options if you used an\n"
4416 "earlier jurabib version." << endl;
4417 }
4418 bool literal = false;
4419 pair<bool, string> aft;
4420 pair<bool, string> bef;
4421 // remove the brackets around after and before
4422 if (!after.empty()) {
4423 after.erase(0, 1);
4424 after.erase(after.length() - 1, 1);
4425 aft = convert_latexed_command_inset_arg(after);
4426 literal = !aft.first;
4427 after = literal ? subst(after, "\n", " ") : aft.second;
4428 }
4429 if (!before.empty()) {
4430 before.erase(0, 1);
4431 before.erase(before.length() - 1, 1);
4432 bef = convert_latexed_command_inset_arg(before);
4433 literal |= !bef.first;
4434 before = literal ? subst(before, "\n", " ") : bef.second;
4435 if (literal && !after.empty())
4436 after = subst(after, "\n", " ");
4437 }
4438 string lit = literal ? "\"true\"" : "\"false\"";
4439 begin_command_inset(os, "citation", command);
4440 os << "after " << '"' << after << "\"\n"
4441 << "before " << '"' << before << "\"\n"
4442 << "key " << '"' << citation << "\"\n"
4443 << "literal " << lit << "\n";
4444 end_inset(os);
4445 // Need to set the cite engine if jurabib is loaded by
4446 // the document class directly
4447 if (preamble.citeEngine() == "basic")
4448 preamble.citeEngine("jurabib");
4449 continue;
4450 }
4451
4452 if (t.cs() == "cite"
4453 || t.cs() == "nocite") {
4454 context.check_layout(os);
4455 string after = p.getArg('[', ']');
4456 pair<bool, string> aft = convert_latexed_command_inset_arg(after);
4457 bool const literal = !aft.first;
4458 after = literal ? subst(after, "\n", " ") : aft.second;
4459 string lit = literal ? "\"true\"" : "\"false\"";
4460 string key = convert_literate_command_inset_arg(p.verbatim_item());
4461 // store the case that it is "\nocite{*}" to use it later for
4462 // the BibTeX inset
4463 if (key != "*") {
4464 begin_command_inset(os, "citation", t.cs());
4465 os << "after " << '"' << after << "\"\n"
4466 << "key " << '"' << key << "\"\n"
4467 << "literal " << lit << "\n";
4468 end_inset(os);
4469 } else if (t.cs() == "nocite")
4470 btprint = key;
4471 continue;
4472 }
4473
4474 if (t.cs() == "index" ||
4475 (t.cs() == "sindex" && preamble.use_indices() == "true")) {
4476 context.check_layout(os);
4477 string const arg = (t.cs() == "sindex" && p.hasOpt()) ?
4478 p.getArg('[', ']') : "";
4479 string const kind = arg.empty() ? "idx" : arg;
4480 begin_inset(os, "Index ");
4481 os << kind << "\nstatus collapsed\n";
4482 parse_text_in_inset(p, os, FLAG_ITEM, false, context, "Index");
4483 end_inset(os);
4484 if (kind != "idx")
4485 preamble.registerAutomaticallyLoadedPackage("splitidx");
4486 continue;
4487 }
4488
4489 if (t.cs() == "nomenclature") {
4490 context.check_layout(os);
4491 begin_command_inset(os, "nomenclature", "nomenclature");
4492 string prefix = convert_literate_command_inset_arg(p.getArg('[', ']'));
4493 if (!prefix.empty())
4494 os << "prefix " << '"' << prefix << '"' << "\n";
4495 string symbol = p.verbatim_item();
4496 pair<bool, string> sym = convert_latexed_command_inset_arg(symbol);
4497 bool literal = !sym.first;
4498 string description = p.verbatim_item();
4499 pair<bool, string> desc = convert_latexed_command_inset_arg(description);
4500 literal |= !desc.first;
4501 if (literal) {
4502 symbol = subst(symbol, "\n", " ");
4503 description = subst(description, "\n", " ");
4504 } else {
4505 symbol = sym.second;
4506 description = desc.second;
4507 }
4508 string lit = literal ? "\"true\"" : "\"false\"";
4509 os << "symbol " << '"' << symbol;
4510 os << "\"\ndescription \""
4511 << description << "\"\n"
4512 << "literal " << lit << "\n";
4513 end_inset(os);
4514 preamble.registerAutomaticallyLoadedPackage("nomencl");
4515 continue;
4516 }
4517
4518 if (t.cs() == "label") {
4519 context.check_layout(os);
4520 begin_command_inset(os, "label", "label");
4521 os << "name \""
4522 << convert_literate_command_inset_arg(p.verbatim_item())
4523 << "\"\n";
4524 end_inset(os);
4525 continue;
4526 }
4527
4528 if (t.cs() == "lyxmintcaption") {
4529 string const pos = p.getArg('[', ']');
4530 if (pos == "t") {
4531 string const caption =
4532 parse_text_snippet(p, FLAG_ITEM, false,
4533 context);
4534 minted_nonfloat_caption = "[t]" + caption;
4535 } else {
4536 // We already got the caption at the bottom,
4537 // so simply skip it.
4538 parse_text_snippet(p, FLAG_ITEM, false, context);
4539 }
4540 eat_whitespace(p, os, context, true);
4541 continue;
4542 }
4543
4544 if (t.cs() == "printindex" || t.cs() == "printsubindex") {
4545 context.check_layout(os);
4546 string commandname = t.cs();
4547 bool star = false;
4548 if (p.next_token().asInput() == "*") {
4549 commandname += "*";
4550 star = true;
4551 p.get_token();
4552 }
4553 begin_command_inset(os, "index_print", commandname);
4554 string const indexname = p.getArg('[', ']');
4555 if (!star) {
4556 if (indexname.empty())
4557 os << "type \"idx\"\n";
4558 else
4559 os << "type \"" << indexname << "\"\n";
4560 os << "literal \"true\"\n";
4561 }
4562 end_inset(os);
4563 skip_spaces_braces(p);
4564 preamble.registerAutomaticallyLoadedPackage("makeidx");
4565 if (preamble.use_indices() == "true")
4566 preamble.registerAutomaticallyLoadedPackage("splitidx");
4567 continue;
4568 }
4569
4570 if (t.cs() == "printnomenclature") {
4571 string width = "";
4572 string width_type = "";
4573 context.check_layout(os);
4574 begin_command_inset(os, "nomencl_print", "printnomenclature");
4575 // case of a custom width
4576 if (p.hasOpt()) {
4577 width = p.getArg('[', ']');
4578 width = translate_len(width);
4579 width_type = "custom";
4580 }
4581 // case of no custom width
4582 // the case of no custom width but the width set
4583 // via \settowidth{\nomlabelwidth}{***} cannot be supported
4584 // because the user could have set anything, not only the width
4585 // of the longest label (which would be width_type = "auto")
4586 string label = convert_literate_command_inset_arg(p.getArg('{', '}'));
4587 if (label.empty() && width_type.empty())
4588 width_type = "none";
4589 os << "set_width \"" << width_type << "\"\n";
4590 if (width_type == "custom")
4591 os << "width \"" << width << '\"';
4592 end_inset(os);
4593 skip_spaces_braces(p);
4594 preamble.registerAutomaticallyLoadedPackage("nomencl");
4595 continue;
4596 }
4597
4598 if ((t.cs() == "textsuperscript" || t.cs() == "textsubscript")) {
4599 context.check_layout(os);
4600 begin_inset(os, "script ");
4601 os << t.cs().substr(4) << '\n';
4602 newinsetlayout = findInsetLayout(context.textclass, t.cs(), true);
4603 parse_text_in_inset(p, os, FLAG_ITEM, false, context, newinsetlayout);
4604 end_inset(os);
4605 if (t.cs() == "textsubscript")
4606 preamble.registerAutomaticallyLoadedPackage("subscript");
4607 continue;
4608 }
4609
4610 if ((where = is_known(t.cs(), known_quotes))) {
4611 context.check_layout(os);
4612 begin_inset(os, "Quotes ");
4613 string quotetype = known_coded_quotes[where - known_quotes];
4614 // try to make a smart guess about the side
4615 Token const prev = p.prev_token();
4616 bool const opening = (prev.cat() != catSpace && prev.character() != 0
4617 && prev.character() != '\n' && prev.character() != '~');
4618 quotetype = guessQuoteStyle(quotetype, opening);
4619 os << quotetype;
4620 end_inset(os);
4621 // LyX adds {} after the quote, so we have to eat
4622 // spaces here if there are any before a possible
4623 // {} pair.
4624 eat_whitespace(p, os, context, false);
4625 skip_braces(p);
4626 continue;
4627 }
4628
4629 if ((where = is_known(t.cs(), known_sizes)) &&
4630 context.new_layout_allowed) {
4631 context.check_layout(os);
4632 TeXFont const oldFont = context.font;
4633 context.font.size = known_coded_sizes[where - known_sizes];
4634 output_font_change(os, oldFont, context.font);
4635 eat_whitespace(p, os, context, false);
4636 continue;
4637 }
4638
4639 if ((where = is_known(t.cs(), known_font_families)) &&
4640 context.new_layout_allowed) {
4641 context.check_layout(os);
4642 TeXFont const oldFont = context.font;
4643 context.font.family =
4644 known_coded_font_families[where - known_font_families];
4645 output_font_change(os, oldFont, context.font);
4646 eat_whitespace(p, os, context, false);
4647 continue;
4648 }
4649
4650 if ((where = is_known(t.cs(), known_font_series)) &&
4651 context.new_layout_allowed) {
4652 context.check_layout(os);
4653 TeXFont const oldFont = context.font;
4654 context.font.series =
4655 known_coded_font_series[where - known_font_series];
4656 output_font_change(os, oldFont, context.font);
4657 eat_whitespace(p, os, context, false);
4658 continue;
4659 }
4660
4661 if ((where = is_known(t.cs(), known_font_shapes)) &&
4662 context.new_layout_allowed) {
4663 context.check_layout(os);
4664 TeXFont const oldFont = context.font;
4665 context.font.shape =
4666 known_coded_font_shapes[where - known_font_shapes];
4667 output_font_change(os, oldFont, context.font);
4668 eat_whitespace(p, os, context, false);
4669 continue;
4670 }
4671 if ((where = is_known(t.cs(), known_old_font_families)) &&
4672 context.new_layout_allowed) {
4673 context.check_layout(os);
4674 TeXFont const oldFont = context.font;
4675 context.font.init();
4676 context.font.size = oldFont.size;
4677 context.font.family =
4678 known_coded_font_families[where - known_old_font_families];
4679 output_font_change(os, oldFont, context.font);
4680 eat_whitespace(p, os, context, false);
4681 continue;
4682 }
4683
4684 if ((where = is_known(t.cs(), known_old_font_series)) &&
4685 context.new_layout_allowed) {
4686 context.check_layout(os);
4687 TeXFont const oldFont = context.font;
4688 context.font.init();
4689 context.font.size = oldFont.size;
4690 context.font.series =
4691 known_coded_font_series[where - known_old_font_series];
4692 output_font_change(os, oldFont, context.font);
4693 eat_whitespace(p, os, context, false);
4694 continue;
4695 }
4696
4697 if ((where = is_known(t.cs(), known_old_font_shapes)) &&
4698 context.new_layout_allowed) {
4699 context.check_layout(os);
4700 TeXFont const oldFont = context.font;
4701 context.font.init();
4702 context.font.size = oldFont.size;
4703 context.font.shape =
4704 known_coded_font_shapes[where - known_old_font_shapes];
4705 output_font_change(os, oldFont, context.font);
4706 eat_whitespace(p, os, context, false);
4707 continue;
4708 }
4709
4710 if (t.cs() == "selectlanguage") {
4711 context.check_layout(os);
4712 // save the language for the case that a
4713 // \foreignlanguage is used
4714 context.font.language = babel2lyx(p.verbatim_item());
4715 os << "\n\\lang " << context.font.language << "\n";
4716 continue;
4717 }
4718
4719 if (t.cs() == "foreignlanguage") {
4720 string const lang = babel2lyx(p.verbatim_item());
4721 parse_text_attributes(p, os, FLAG_ITEM, outer,
4722 context, "\\lang",
4723 context.font.language, lang);
4724 continue;
4725 }
4726
4727 if (prefixIs(t.cs(), "text") && preamble.usePolyglossia()
4728 && is_known(t.cs().substr(4), preamble.polyglossia_languages)) {
4729 // scheme is \textLANGUAGE{text} where LANGUAGE is in polyglossia_languages[]
4730 string lang;
4731 // We have to output the whole command if it has an option
4732 // because LyX doesn't support this yet, see bug #8214,
4733 // only if there is a single option specifying a variant, we can handle it.
4734 if (p.hasOpt()) {
4735 string langopts = p.getOpt();
4736 // check if the option contains a variant, if yes, extract it
4737 string::size_type pos_var = langopts.find("variant");
4738 string::size_type i = langopts.find(',');
4739 string::size_type k = langopts.find('=', pos_var);
4740 if (pos_var != string::npos && i == string::npos) {
4741 string variant;
4742 variant = langopts.substr(k + 1, langopts.length() - k - 2);
4743 lang = preamble.polyglossia2lyx(variant);
4744 parse_text_attributes(p, os, FLAG_ITEM, outer,
4745 context, "\\lang",
4746 context.font.language, lang);
4747 } else
4748 output_ert_inset(os, t.asInput() + langopts, context);
4749 } else {
4750 lang = preamble.polyglossia2lyx(t.cs().substr(4, string::npos));
4751 parse_text_attributes(p, os, FLAG_ITEM, outer,
4752 context, "\\lang",
4753 context.font.language, lang);
4754 }
4755 continue;
4756 }
4757
4758 if (t.cs() == "inputencoding") {
4759 // nothing to write here
4760 string const enc = subst(p.verbatim_item(), "\n", " ");
4761 p.setEncoding(enc, Encoding::inputenc);
4762 continue;
4763 }
4764
4765 if (is_known(t.cs(), known_special_chars) ||
4766 (t.cs() == "protect" &&
4767 p.next_token().cat() == catEscape &&
4768 is_known(p.next_token().cs(), known_special_protect_chars))) {
4769 // LyX sometimes puts a \protect in front, so we have to ignore it
4770 where = is_known(
4771 t.cs() == "protect" ? p.get_token().cs() : t.cs(),
4772 known_special_chars);
4773 context.check_layout(os);
4774 os << known_coded_special_chars[where - known_special_chars];
4775 skip_spaces_braces(p);
4776 continue;
4777 }
4778
4779 if ((t.cs() == "nobreakdash" && p.next_token().asInput() == "-") ||
4780 (t.cs() == "protect" && p.next_token().asInput() == "\\nobreakdash" &&
4781 p.next_next_token().asInput() == "-") ||
4782 (t.cs() == "@" && p.next_token().asInput() == ".")) {
4783 // LyX sometimes puts a \protect in front, so we have to ignore it
4784 if (t.cs() == "protect")
4785 p.get_token();
4786 context.check_layout(os);
4787 if (t.cs() == "nobreakdash")
4788 os << "\\SpecialChar nobreakdash\n";
4789 else
4790 os << "\\SpecialChar endofsentence\n";
4791 p.get_token();
4792 continue;
4793 }
4794
4795 if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
4796 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
4797 || t.cs() == "%" || t.cs() == "-") {
4798 context.check_layout(os);
4799 if (t.cs() == "-")
4800 os << "\\SpecialChar softhyphen\n";
4801 else
4802 os << t.cs();
4803 continue;
4804 }
4805
4806 if (t.cs() == "char") {
4807 context.check_layout(os);
4808 if (p.next_token().character() == '`') {
4809 p.get_token();
4810 if (p.next_token().cs() == "\"") {
4811 p.get_token();
4812 os << '"';
4813 skip_braces(p);
4814 } else {
4815 output_ert_inset(os, "\\char`", context);
4816 }
4817 } else {
4818 output_ert_inset(os, "\\char", context);
4819 }
4820 continue;
4821 }
4822
4823 if (t.cs() == "verb") {
4824 context.check_layout(os);
4825 // set catcodes to verbatim early, just in case.
4826 p.setCatcodes(VERBATIM_CATCODES);
4827 string delim = p.get_token().asInput();
4828 Parser::Arg arg = p.verbatimStuff(delim);
4829 if (arg.first)
4830 output_ert_inset(os, "\\verb" + delim
4831 + arg.second + delim, context);
4832 else
4833 cerr << "invalid \\verb command. Skipping" << endl;
4834 continue;
4835 }
4836
4837 // Problem: \= creates a tabstop inside the tabbing environment
4838 // and else an accent. In the latter case we really would want
4839 // \={o} instead of \= o.
4840 if (t.cs() == "=" && (flags & FLAG_TABBING)) {
4841 output_ert_inset(os, t.asInput(), context);
4842 continue;
4843 }
4844
4845 if (t.cs() == "\\") {
4846 context.check_layout(os);
4847 if (p.hasOpt())
4848 output_ert_inset(os, "\\\\" + p.getOpt(), context);
4849 else if (p.next_token().asInput() == "*") {
4850 p.get_token();
4851 // getOpt() eats the following space if there
4852 // is no optional argument, but that is OK
4853 // here since it has no effect in the output.
4854 output_ert_inset(os, "\\\\*" + p.getOpt(), context);
4855 }
4856 else {
4857 begin_inset(os, "Newline newline");
4858 end_inset(os);
4859 }
4860 continue;
4861 }
4862
4863 if (t.cs() == "newline" ||
4864 (t.cs() == "linebreak" && !p.hasOpt())) {
4865 context.check_layout(os);
4866 begin_inset(os, "Newline ");
4867 os << t.cs();
4868 end_inset(os);
4869 skip_spaces_braces(p);
4870 continue;
4871 }
4872
4873 if (t.cs() == "input" || t.cs() == "include"
4874 || t.cs() == "verbatiminput"
4875 || t.cs() == "lstinputlisting"
4876 || t.cs() == "inputminted") {
4877 string name = t.cs();
4878 if (name == "verbatiminput"
4879 && p.next_token().asInput() == "*")
4880 name += p.get_token().asInput();
4881 context.check_layout(os);
4882 string lstparams;
4883 if (name == "lstinputlisting" && p.hasOpt()) {
4884 lstparams = p.getArg('[', ']');
4885 lstparams = subst(lstparams, "\n", " ");
4886 } else if (name == "inputminted") {
4887 name = "lstinputlisting";
4888 string const lang = p.getArg('{', '}');
4889 if (lang != "tex") {
4890 string cmd = "\\inputminted{" + lang + "}{";
4891 cmd += p.getArg('{', '}') + "}";
4892 output_ert_inset(os, cmd, context);
4893 continue;
4894 }
4895 if (prefixIs(minted_nonfloat_caption, "[t]")) {
4896 minted_nonfloat_caption.erase(0,3);
4897 // extract label and caption from the already produced LyX code
4898 vector<string> nfc = getVectorFromString(minted_nonfloat_caption, "\n");
4899 string const caption = nfc.front();
4900 string label;
4901 vector<string>::iterator it =
4902 find(nfc.begin(), nfc.end(), "LatexCommand label");
4903 if (it != nfc.end()) {
4904 ++it;
4905 if (it != nfc.end())
4906 label = *it;
4907 label = support::split(label, '"');
4908 label.pop_back();
4909 }
4910 minted_nonfloat_caption.clear();
4911 lstparams = "caption=" + caption;
4912 if (!label.empty())
4913 lstparams += ",label=" + label;
4914 lstparams = subst(lstparams, "\n", " ");
4915 }
4916 }
4917 string filename(normalize_filename(p.getArg('{', '}')));
4918 string const path = getMasterFilePath(true);
4919 // We want to preserve relative / absolute filenames,
4920 // therefore path is only used for testing
4921 if ((t.cs() == "include" || t.cs() == "input") &&
4922 !makeAbsPath(filename, path).exists()) {
4923 // The file extension is probably missing.
4924 // Now try to find it out.
4925 string const tex_name =
4926 find_file(filename, path,
4927 known_tex_extensions);
4928 if (!tex_name.empty())
4929 filename = tex_name;
4930 }
4931 bool external = false;
4932 string outname;
4933 if (makeAbsPath(filename, path).exists()) {
4934 string const abstexname =
4935 makeAbsPath(filename, path).absFileName();
4936 string const absfigname =
4937 changeExtension(abstexname, ".fig");
4938 fix_child_filename(filename);
4939 string const lyxname = changeExtension(filename,
4940 roundtripMode() ? ".lyx.lyx" : ".lyx");
4941 string const abslyxname = makeAbsPath(
4942 lyxname, getParentFilePath(false)).absFileName();
4943 bool xfig = false;
4944 if (!skipChildren())
4945 external = FileName(absfigname).exists();
4946 if (t.cs() == "input" && !skipChildren()) {
4947 string const ext = getExtension(abstexname);
4948
4949 // Combined PS/LaTeX:
4950 // x.eps, x.pstex_t (old xfig)
4951 // x.pstex, x.pstex_t (new xfig, e.g. 3.2.5)
4952 FileName const absepsname(
4953 changeExtension(abstexname, ".eps"));
4954 FileName const abspstexname(
4955 changeExtension(abstexname, ".pstex"));
4956 bool const xfigeps =
4957 (absepsname.exists() ||
4958 abspstexname.exists()) &&
4959 ext == "pstex_t";
4960
4961 // Combined PDF/LaTeX:
4962 // x.pdf, x.pdftex_t (old xfig)
4963 // x.pdf, x.pdf_t (new xfig, e.g. 3.2.5)
4964 FileName const abspdfname(
4965 changeExtension(abstexname, ".pdf"));
4966 bool const xfigpdf =
4967 abspdfname.exists() &&
4968 (ext == "pdftex_t" || ext == "pdf_t");
4969 if (xfigpdf)
4970 pdflatex = true;
4971
4972 // Combined PS/PDF/LaTeX:
4973 // x_pspdftex.eps, x_pspdftex.pdf, x.pspdftex
4974 string const absbase2(
4975 removeExtension(abstexname) + "_pspdftex");
4976 FileName const abseps2name(
4977 addExtension(absbase2, ".eps"));
4978 FileName const abspdf2name(
4979 addExtension(absbase2, ".pdf"));
4980 bool const xfigboth =
4981 abspdf2name.exists() &&
4982 abseps2name.exists() && ext == "pspdftex";
4983
4984 xfig = xfigpdf || xfigeps || xfigboth;
4985 external = external && xfig;
4986 }
4987 if (external) {
4988 outname = changeExtension(filename, ".fig");
4989 FileName abssrc(changeExtension(abstexname, ".fig"));
4990 copy_file(abssrc, outname);
4991 } else if (xfig) {
4992 // Don't try to convert, the result
4993 // would be full of ERT.
4994 outname = filename;
4995 FileName abssrc(abstexname);
4996 copy_file(abssrc, outname);
4997 } else if (t.cs() != "verbatiminput" &&
4998 !skipChildren() &&
4999 tex2lyx(abstexname, FileName(abslyxname),
5000 p.getEncoding())) {
5001 outname = lyxname;
5002 // no need to call copy_file
5003 // tex2lyx creates the file
5004 } else {
5005 outname = filename;
5006 FileName abssrc(abstexname);
5007 copy_file(abssrc, outname);
5008 }
5009 } else {
5010 cerr << "Warning: Could not find included file '"
5011 << filename << "'." << endl;
5012 outname = filename;
5013 }
5014 if (external) {
5015 begin_inset(os, "External\n");
5016 os << "\ttemplate XFig\n"
5017 << "\tfilename " << outname << '\n';
5018 registerExternalTemplatePackages("XFig");
5019 } else {
5020 begin_command_inset(os, "include", name);
5021 outname = subst(outname, "\"", "\\\"");
5022 os << "preview false\n"
5023 "filename \"" << outname << "\"\n";
5024 if (!lstparams.empty())
5025 os << "lstparams \"" << lstparams << "\"\n";
5026 if (t.cs() == "verbatiminput")
5027 preamble.registerAutomaticallyLoadedPackage("verbatim");
5028 }
5029 end_inset(os);
5030 continue;
5031 }
5032
5033 if (t.cs() == "bibliographystyle") {
5034 // store new bibliographystyle
5035 bibliographystyle = p.verbatim_item();
5036 // If any other command than \bibliography, \addcontentsline
5037 // and \nocite{*} follows, we need to output the style
5038 // (because it might be used by that command).
5039 // Otherwise, it will automatically be output by LyX.
5040 p.pushPosition();
5041 bool output = true;
5042 for (Token t2 = p.get_token(); p.good(); t2 = p.get_token()) {
5043 if (t2.cat() == catBegin)
5044 break;
5045 if (t2.cat() != catEscape)
5046 continue;
5047 if (t2.cs() == "nocite") {
5048 if (p.getArg('{', '}') == "*")
5049 continue;
5050 } else if (t2.cs() == "bibliography")
5051 output = false;
5052 else if (t2.cs() == "phantomsection") {
5053 output = false;
5054 continue;
5055 }
5056 else if (t2.cs() == "addcontentsline") {
5057 // get the 3 arguments of \addcontentsline
5058 p.getArg('{', '}');
5059 p.getArg('{', '}');
5060 contentslineContent = p.getArg('{', '}');
5061 // if the last argument is not \refname we must output
5062 if (contentslineContent == "\\refname")
5063 output = false;
5064 }
5065 break;
5066 }
5067 p.popPosition();
5068 if (output) {
5069 output_ert_inset(os,
5070 "\\bibliographystyle{" + bibliographystyle + '}',
5071 context);
5072 }
5073 continue;
5074 }
5075
5076 if (t.cs() == "phantomsection") {
5077 // we only support this if it occurs between
5078 // \bibliographystyle and \bibliography
5079 if (bibliographystyle.empty())
5080 output_ert_inset(os, "\\phantomsection", context);
5081 continue;
5082 }
5083
5084 if (t.cs() == "addcontentsline") {
5085 context.check_layout(os);
5086 // get the 3 arguments of \addcontentsline
5087 string const one = p.getArg('{', '}');
5088 string const two = p.getArg('{', '}');
5089 string const three = p.getArg('{', '}');
5090 // only if it is a \refname, we support if for the bibtex inset
5091 if (contentslineContent != "\\refname") {
5092 output_ert_inset(os,
5093 "\\addcontentsline{" + one + "}{" + two + "}{"+ three + '}',
5094 context);
5095 }
5096 continue;
5097 }
5098
5099 else if (t.cs() == "bibliography") {
5100 context.check_layout(os);
5101 string BibOpts;
5102 begin_command_inset(os, "bibtex", "bibtex");
5103 if (!btprint.empty()) {
5104 os << "btprint " << '"' << "btPrintAll" << '"' << "\n";
5105 // clear the string because the next BibTeX inset can be without the
5106 // \nocite{*} option
5107 btprint.clear();
5108 }
5109 os << "bibfiles " << '"' << normalize_filename(p.verbatim_item()) << '"' << "\n";
5110 // Do we have addcontentsline?
5111 if (contentslineContent == "\\refname") {
5112 BibOpts = "bibtotoc";
5113 // clear string because next BibTeX inset can be without addcontentsline
5114 contentslineContent.clear();
5115 }
5116 // Do we have a bibliographystyle set?
5117 if (!bibliographystyle.empty()) {
5118 if (BibOpts.empty())
5119 BibOpts = normalize_filename(bibliographystyle);
5120 else
5121 BibOpts = BibOpts + ',' + normalize_filename(bibliographystyle);
5122 // clear it because each bibtex entry has its style
5123 // and we need an empty string to handle \phantomsection
5124 bibliographystyle.clear();
5125 }
5126 os << "options " << '"' << BibOpts << '"' << "\n";
5127 end_inset(os);
5128 continue;
5129 }
5130
5131 if (t.cs() == "printbibliography") {
5132 context.check_layout(os);
5133 string BibOpts;
5134 string bbloptions = p.hasOpt() ? p.getArg('[', ']') : string();
5135 vector<string> opts = getVectorFromString(bbloptions);
5136 vector<string>::iterator it =
5137 find(opts.begin(), opts.end(), "heading=bibintoc");
5138 if (it != opts.end()) {
5139 opts.erase(it);
5140 BibOpts = "bibtotoc";
5141 }
5142 bbloptions = getStringFromVector(opts);
5143 begin_command_inset(os, "bibtex", "bibtex");
5144 if (!btprint.empty()) {
5145 os << "btprint " << '"' << "btPrintAll" << '"' << "\n";
5146 // clear the string because the next BibTeX inset can be without the
5147 // \nocite{*} option
5148 btprint.clear();
5149 }
5150 string bibfiles;
5151 for (auto const & bf : preamble.biblatex_bibliographies) {
5152 if (!bibfiles.empty())
5153 bibfiles += ",";
5154 bibfiles += normalize_filename(bf);
5155 }
5156 if (!bibfiles.empty())
5157 os << "bibfiles " << '"' << bibfiles << '"' << "\n";
5158 // Do we have addcontentsline?
5159 if (contentslineContent == "\\refname") {
5160 BibOpts = "bibtotoc";
5161 // clear string because next BibTeX inset can be without addcontentsline
5162 contentslineContent.clear();
5163 }
5164 os << "options " << '"' << BibOpts << '"' << "\n";
5165 if (!bbloptions.empty())
5166 os << "biblatexopts " << '"' << bbloptions << '"' << "\n";
5167 end_inset(os);
5168 need_commentbib = false;
5169 continue;
5170 }
5171
5172 if (t.cs() == "bibbysection") {
5173 context.check_layout(os);
5174 string BibOpts;
5175 string bbloptions = p.hasOpt() ? p.getArg('[', ']') : string();
5176 vector<string> opts = getVectorFromString(bbloptions);
5177 vector<string>::iterator it =
5178 find(opts.begin(), opts.end(), "heading=bibintoc");
5179 if (it != opts.end()) {
5180 opts.erase(it);
5181 BibOpts = "bibtotoc";
5182 }
5183 bbloptions = getStringFromVector(opts);
5184 begin_command_inset(os, "bibtex", "bibtex");
5185 os << "btprint " << '"' << "bibbysection" << '"' << "\n";
5186 string bibfiles;
5187 for (auto const & bf : preamble.biblatex_bibliographies) {
5188 if (!bibfiles.empty())
5189 bibfiles += ",";
5190 bibfiles += normalize_filename(bf);
5191 }
5192 if (!bibfiles.empty())
5193 os << "bibfiles " << '"' << bibfiles << '"' << "\n";
5194 os << "options " << '"' << BibOpts << '"' << "\n";
5195 if (!bbloptions.empty())
5196 os << "biblatexopts " << '"' << bbloptions << '"' << "\n";
5197 end_inset(os);
5198 need_commentbib = false;
5199 continue;
5200 }
5201
5202 if (t.cs() == "parbox") {
5203 // Test whether this is an outer box of a shaded box
5204 p.pushPosition();
5205 // swallow arguments
5206 while (p.hasOpt()) {
5207 p.getArg('[', ']');
5208 p.skip_spaces(true);
5209 }
5210 p.getArg('{', '}');
5211 p.skip_spaces(true);
5212 // eat the '{'
5213 if (p.next_token().cat() == catBegin)
5214 p.get_token();
5215 p.skip_spaces(true);
5216 Token to = p.get_token();
5217 bool shaded = false;
5218 if (to.asInput() == "\\begin") {
5219 p.skip_spaces(true);
5220 if (p.getArg('{', '}') == "shaded")
5221 shaded = true;
5222 }
5223 p.popPosition();
5224 if (shaded) {
5225 parse_outer_box(p, os, FLAG_ITEM, outer,
5226 context, "parbox", "shaded");
5227 } else
5228 parse_box(p, os, 0, FLAG_ITEM, outer, context,
5229 "", "", t.cs(), "", "");
5230 continue;
5231 }
5232
5233 if (t.cs() == "fbox" || t.cs() == "mbox" ||
5234 t.cs() == "ovalbox" || t.cs() == "Ovalbox" ||
5235 t.cs() == "shadowbox" || t.cs() == "doublebox") {
5236 parse_outer_box(p, os, FLAG_ITEM, outer, context, t.cs(), "");
5237 continue;
5238 }
5239
5240 if (t.cs() == "fcolorbox" || t.cs() == "colorbox") {
5241 string backgroundcolor;
5242 preamble.registerAutomaticallyLoadedPackage("xcolor");
5243 if (t.cs() == "fcolorbox") {
5244 string const framecolor = p.getArg('{', '}');
5245 backgroundcolor = p.getArg('{', '}');
5246 parse_box(p, os, 0, 0, outer, context, "", "", "", framecolor, backgroundcolor);
5247 } else {
5248 backgroundcolor = p.getArg('{', '}');
5249 parse_box(p, os, 0, 0, outer, context, "", "", "", "", backgroundcolor);
5250 }
5251 continue;
5252 }
5253
5254 // FIXME: due to the compiler limit of "if" nestings
5255 // the code for the alignment was put here
5256 // put them in their own if if this is fixed
5257 if (t.cs() == "fboxrule" || t.cs() == "fboxsep"
5258 || t.cs() == "shadowsize"
5259 || t.cs() == "raggedleft" || t.cs() == "centering"
5260 || t.cs() == "raggedright") {
5261 if (t.cs() == "fboxrule")
5262 fboxrule = "";
5263 if (t.cs() == "fboxsep")
5264 fboxsep = "";
5265 if (t.cs() == "shadowsize")
5266 shadow_size = "";
5267 if (t.cs() != "raggedleft" && t.cs() != "centering"
5268 && t.cs() != "raggedright") {
5269 p.skip_spaces(true);
5270 while (p.good() && p.next_token().cat() != catSpace
5271 && p.next_token().cat() != catNewline
5272 && p.next_token().cat() != catEscape) {
5273 if (t.cs() == "fboxrule")
5274 fboxrule = fboxrule + p.get_token().asInput();
5275 if (t.cs() == "fboxsep")
5276 fboxsep = fboxsep + p.get_token().asInput();
5277 if (t.cs() == "shadowsize")
5278 shadow_size = shadow_size + p.get_token().asInput();
5279 }
5280 } else {
5281 output_ert_inset(os, t.asInput(), context);
5282 }
5283 continue;
5284 }
5285
5286 //\framebox() is part of the picture environment and different from \framebox{}
5287 //\framebox{} will be parsed by parse_outer_box
5288 if (t.cs() == "framebox") {
5289 if (p.next_token().character() == '(') {
5290 //the syntax is: \framebox(x,y)[position]{content}
5291 string arg = t.asInput();
5292 arg += p.getFullParentheseArg();
5293 arg += p.getFullOpt();
5294 eat_whitespace(p, os, context, false);
5295 output_ert_inset(os, arg + '{', context);
5296 parse_text(p, os, FLAG_ITEM, outer, context);
5297 output_ert_inset(os, "}", context);
5298 } else {
5299 //the syntax is: \framebox[width][position]{content}
5300 string special = p.getFullOpt();
5301 special += p.getOpt();
5302 parse_outer_box(p, os, FLAG_ITEM, outer,
5303 context, t.cs(), special);
5304 }
5305 continue;
5306 }
5307
5308 //\makebox() is part of the picture environment and different from \makebox{}
5309 //\makebox{} will be parsed by parse_box
5310 if (t.cs() == "makebox") {
5311 if (p.next_token().character() == '(') {
5312 //the syntax is: \makebox(x,y)[position]{content}
5313 string arg = t.asInput();
5314 arg += p.getFullParentheseArg();
5315 arg += p.getFullOpt();
5316 eat_whitespace(p, os, context, false);
5317 output_ert_inset(os, arg + '{', context);
5318 parse_text(p, os, FLAG_ITEM, outer, context);
5319 output_ert_inset(os, "}", context);
5320 } else
5321 //the syntax is: \makebox[width][position]{content}
5322 parse_box(p, os, 0, FLAG_ITEM, outer, context,
5323 "", "", t.cs(), "", "");
5324 continue;
5325 }
5326
5327 if (t.cs() == "smallskip" ||
5328 t.cs() == "medskip" ||
5329 t.cs() == "bigskip" ||
5330 t.cs() == "vfill") {
5331 context.check_layout(os);
5332 begin_inset(os, "VSpace ");
5333 os << t.cs();
5334 end_inset(os);
5335 skip_spaces_braces(p);
5336 continue;
5337 }
5338
5339 if ((where = is_known(t.cs(), known_spaces))) {
5340 context.check_layout(os);
5341 begin_inset(os, "space ");
5342 os << '\\' << known_coded_spaces[where - known_spaces]
5343 << '\n';
5344 end_inset(os);
5345 // LaTeX swallows whitespace after all spaces except
5346 // "\\,". We have to do that here, too, because LyX
5347 // adds "{}" which would make the spaces significant.
5348 if (t.cs() != ",")
5349 eat_whitespace(p, os, context, false);
5350 // LyX adds "{}" after all spaces except "\\ " and
5351 // "\\,", so we have to remove "{}".
5352 // "\\,{}" is equivalent to "\\," in LaTeX, so we
5353 // remove the braces after "\\,", too.
5354 if (t.cs() != " ")
5355 skip_braces(p);
5356 continue;
5357 }
5358
5359 if (t.cs() == "newpage" ||
5360 (t.cs() == "pagebreak" && !p.hasOpt()) ||
5361 t.cs() == "clearpage" ||
5362 t.cs() == "cleardoublepage") {
5363 context.check_layout(os);
5364 begin_inset(os, "Newpage ");
5365 os << t.cs();
5366 end_inset(os);
5367 skip_spaces_braces(p);
5368 continue;
5369 }
5370
5371 if (t.cs() == "DeclareRobustCommand" ||
5372 t.cs() == "DeclareRobustCommandx" ||
5373 t.cs() == "newcommand" ||
5374 t.cs() == "newcommandx" ||
5375 t.cs() == "providecommand" ||
5376 t.cs() == "providecommandx" ||
5377 t.cs() == "renewcommand" ||
5378 t.cs() == "renewcommandx") {
5379 // DeclareRobustCommand, DeclareRobustCommandx,
5380 // providecommand and providecommandx could be handled
5381 // by parse_command(), but we need to call
5382 // add_known_command() here.
5383 string name = t.asInput();
5384 if (p.next_token().asInput() == "*") {
5385 // Starred form. Eat '*'
5386 p.get_token();
5387 name += '*';
5388 }
5389 string const command = p.verbatim_item();
5390 string const opt1 = p.getFullOpt();
5391 string const opt2 = p.getFullOpt();
5392 add_known_command(command, opt1, !opt2.empty());
5393 string const ert = name + '{' + command + '}' +
5394 opt1 + opt2 +
5395 '{' + p.verbatim_item() + '}';
5396
5397 if (t.cs() == "DeclareRobustCommand" ||
5398 t.cs() == "DeclareRobustCommandx" ||
5399 t.cs() == "providecommand" ||
5400 t.cs() == "providecommandx" ||
5401 name[name.length()-1] == '*')
5402 output_ert_inset(os, ert, context);
5403 else {
5404 context.check_layout(os);
5405 begin_inset(os, "FormulaMacro");
5406 os << "\n" << ert;
5407 end_inset(os);
5408 }
5409 continue;
5410 }
5411
5412 if (t.cs() == "let" && p.next_token().asInput() != "*") {
5413 // let could be handled by parse_command(),
5414 // but we need to call add_known_command() here.
5415 string ert = t.asInput();
5416 string name;
5417 p.skip_spaces();
5418 if (p.next_token().cat() == catBegin) {
5419 name = p.verbatim_item();
5420 ert += '{' + name + '}';
5421 } else {
5422 name = p.verbatim_item();
5423 ert += name;
5424 }
5425 string command;
5426 p.skip_spaces();
5427 if (p.next_token().cat() == catBegin) {
5428 command = p.verbatim_item();
5429 ert += '{' + command + '}';
5430 } else {
5431 command = p.verbatim_item();
5432 ert += command;
5433 }
5434 // If command is known, make name known too, to parse
5435 // its arguments correctly. For this reason we also
5436 // have commands in syntax.default that are hardcoded.
5437 CommandMap::iterator it = known_commands.find(command);
5438 if (it != known_commands.end())
5439 known_commands[t.asInput()] = it->second;
5440 output_ert_inset(os, ert, context);
5441 continue;
5442 }
5443
5444 if (t.cs() == "hspace" || t.cs() == "vspace") {
5445 if (starred)
5446 p.get_token();
5447 string name = t.asInput();
5448 string const length = p.verbatim_item();
5449 string unit;
5450 string valstring;
5451 bool valid = splitLatexLength(length, valstring, unit);
5452 bool known_hspace = false;
5453 bool known_vspace = false;
5454 bool known_unit = false;
5455 double value;
5456 if (valid) {
5457 istringstream iss(valstring);
5458 iss >> value;
5459 if (value == 1.0) {
5460 if (t.cs()[0] == 'h') {
5461 if (unit == "\\fill") {
5462 if (!starred) {
5463 unit = "";
5464 name = "\\hfill";
5465 }
5466 known_hspace = true;
5467 }
5468 } else {
5469 if (unit == "\\smallskipamount") {
5470 unit = "smallskip";
5471 known_vspace = true;
5472 } else if (unit == "\\medskipamount") {
5473 unit = "medskip";
5474 known_vspace = true;
5475 } else if (unit == "\\bigskipamount") {
5476 unit = "bigskip";
5477 known_vspace = true;
5478 } else if (unit == "\\fill") {
5479 unit = "vfill";
5480 known_vspace = true;
5481 }
5482 }
5483 }
5484 if (!known_hspace && !known_vspace) {
5485 switch (unitFromString(unit)) {
5486 case Length::SP:
5487 case Length::PT:
5488 case Length::BP:
5489 case Length::DD:
5490 case Length::MM:
5491 case Length::PC:
5492 case Length::CC:
5493 case Length::CM:
5494 case Length::IN:
5495 case Length::EX:
5496 case Length::EM:
5497 case Length::MU:
5498 known_unit = true;
5499 break;
5500 default: {
5501 //unitFromString(unit) fails for relative units like Length::PCW
5502 // therefore handle them separately
5503 if (unit == "\\paperwidth" || unit == "\\columnwidth"
5504 || unit == "\\textwidth" || unit == "\\linewidth"
5505 || unit == "\\textheight" || unit == "\\paperheight"
5506 || unit == "\\baselineskip")
5507 known_unit = true;
5508 break;
5509 }
5510 }
5511 }
5512 }
5513
5514 // check for glue lengths
5515 bool is_gluelength = false;
5516 string gluelength = length;
5517 string::size_type i = length.find(" minus");
5518 if (i == string::npos) {
5519 i = length.find(" plus");
5520 if (i != string::npos)
5521 is_gluelength = true;
5522 } else
5523 is_gluelength = true;
5524 // if yes transform "9xx minus 8yy plus 7zz"
5525 // to "9xx-8yy+7zz"
5526 if (is_gluelength) {
5527 i = gluelength.find(" minus");
5528 if (i != string::npos)
5529 gluelength.replace(i, 7, "-");
5530 i = gluelength.find(" plus");
5531 if (i != string::npos)
5532 gluelength.replace(i, 6, "+");
5533 }
5534
5535 if (t.cs()[0] == 'h' && (known_unit || known_hspace || is_gluelength)) {
5536 // Literal horizontal length or known variable
5537 context.check_layout(os);
5538 begin_inset(os, "space ");
5539 os << name;
5540 if (starred)
5541 os << '*';
5542 os << '{';
5543 if (known_hspace)
5544 os << unit;
5545 os << "}";
5546 if (known_unit && !known_hspace)
5547 os << "\n\\length " << translate_len(length);
5548 if (is_gluelength)
5549 os << "\n\\length " << gluelength;
5550 end_inset(os);
5551 } else if (known_unit || known_vspace || is_gluelength) {
5552 // Literal vertical length or known variable
5553 context.check_layout(os);
5554 begin_inset(os, "VSpace ");
5555 if (known_vspace)
5556 os << unit;
5557 if (known_unit && !known_vspace)
5558 os << translate_len(length);
5559 if (is_gluelength)
5560 os << gluelength;
5561 if (starred)
5562 os << '*';
5563 end_inset(os);
5564 } else {
5565 // LyX can't handle other length variables in Inset VSpace/space
5566 if (starred)
5567 name += '*';
5568 if (valid) {
5569 if (value == 1.0)
5570 output_ert_inset(os, name + '{' + unit + '}', context);
5571 else if (value == -1.0)
5572 output_ert_inset(os, name + "{-" + unit + '}', context);
5573 else
5574 output_ert_inset(os, name + '{' + valstring + unit + '}', context);
5575 } else
5576 output_ert_inset(os, name + '{' + length + '}', context);
5577 }
5578 continue;
5579 }
5580
5581 // Before we look for the layout name alone below, we check the layouts including the LateXParam, which
5582 // might be one or several options or a star.
5583 // The single '=' is meant here.
5584 if ((newinsetlayout = findInsetLayout(context.textclass, starredname, true, p.getCommandLatexParam()))) {
5585 if (starred)
5586 p.get_token();
5587 p.skip_spaces();
5588 context.check_layout(os);
5589 // store the latexparam here. This is eaten in parse_text_in_inset
5590 context.latexparam = newinsetlayout->latexparam();
5591 docstring name = newinsetlayout->name();
5592 bool const caption = name.find(from_ascii("Caption:")) == 0;
5593 if (caption) {
5594 // Already done for floating minted listings.
5595 if (minted_float.empty()) {
5596 begin_inset(os, "Caption ");
5597 os << to_utf8(name.substr(8)) << '\n';
5598 }
5599 } else {
5600 // FIXME: what do we do if the prefix is not Flex: ?
5601 if (prefixIs(name, from_ascii("Flex:")))
5602 name.erase(0, 5);
5603 begin_inset(os, "Flex ");
5604 os << to_utf8(name) << '\n'
5605 << "status collapsed\n";
5606 }
5607 if (!minted_float.empty()) {
5608 parse_text_snippet(p, os, FLAG_ITEM, false, context);
5609 } else if (newinsetlayout->isPassThru()) {
5610 // set catcodes to verbatim early, just in case.
5611 p.setCatcodes(VERBATIM_CATCODES);
5612 string delim = p.get_token().asInput();
5613 if (delim != "{")
5614 cerr << "Warning: bad delimiter for command " << t.asInput() << endl;
5615 //FIXME: handle error condition
5616 string const arg = p.verbatimStuff("}").second;
5617 Context newcontext(true, context.textclass);
5618 if (newinsetlayout->forcePlainLayout())
5619 newcontext.layout = &context.textclass.plainLayout();
5620 output_ert(os, arg, newcontext);
5621 } else
5622 parse_text_in_inset(p, os, FLAG_ITEM, false, context, newinsetlayout);
5623 context.latexparam.clear();
5624 if (caption)
5625 p.skip_spaces();
5626 // Minted caption insets are not closed here because
5627 // we collect everything into the caption.
5628 if (minted_float.empty())
5629 end_inset(os);
5630 continue;
5631 }
5632
5633 // The single '=' is meant here.
5634 if ((newinsetlayout = findInsetLayout(context.textclass, starredname, true))) {
5635 if (starred)
5636 p.get_token();
5637 p.skip_spaces();
5638 context.check_layout(os);
5639 docstring const name = newinsetlayout->name();
5640 bool const caption = name.find(from_ascii("Caption:")) == 0;
5641 if (caption) {
5642 // Already done for floating minted listings.
5643 if (minted_float.empty()) {
5644 begin_inset(os, "Caption ");
5645 os << to_utf8(name.substr(8)) << '\n';
5646 }
5647 } else {
5648 begin_inset(os, "Flex ");
5649 os << to_utf8(name) << '\n'
5650 << "status collapsed\n";
5651 }
5652 if (!minted_float.empty()) {
5653 parse_text_snippet(p, os, FLAG_ITEM, false, context);
5654 } else if (newinsetlayout->isPassThru()) {
5655 // set catcodes to verbatim early, just in case.
5656 p.setCatcodes(VERBATIM_CATCODES);
5657 string delim = p.get_token().asInput();
5658 if (delim != "{")
5659 cerr << "Warning: bad delimiter for command " << t.asInput() << endl;
5660 //FIXME: handle error condition
5661 string const arg = p.verbatimStuff("}").second;
5662 Context newcontext(true, context.textclass);
5663 if (newinsetlayout->forcePlainLayout())
5664 newcontext.layout = &context.textclass.plainLayout();
5665 output_ert(os, arg, newcontext);
5666 } else
5667 parse_text_in_inset(p, os, FLAG_ITEM, false, context, newinsetlayout);
5668 if (caption)
5669 p.skip_spaces();
5670 // Minted caption insets are not closed here because
5671 // we collect everything into the caption.
5672 if (minted_float.empty())
5673 end_inset(os);
5674 continue;
5675 }
5676
5677 if (t.cs() == "includepdf") {
5678 p.skip_spaces();
5679 string const arg = p.getArg('[', ']');
5680 map<string, string> opts;
5681 vector<string> keys;
5682 split_map(arg, opts, keys);
5683 string name = normalize_filename(p.verbatim_item());
5684 string const path = getMasterFilePath(true);
5685 // We want to preserve relative / absolute filenames,
5686 // therefore path is only used for testing
5687 if (!makeAbsPath(name, path).exists()) {
5688 // The file extension is probably missing.
5689 // Now try to find it out.
5690 char const * const pdfpages_format[] = {"pdf", 0};
5691 string const pdftex_name =
5692 find_file(name, path, pdfpages_format);
5693 if (!pdftex_name.empty()) {
5694 name = pdftex_name;
5695 pdflatex = true;
5696 }
5697 }
5698 FileName const absname = makeAbsPath(name, path);
5699 if (absname.exists())
5700 {
5701 fix_child_filename(name);
5702 copy_file(absname, name);
5703 } else
5704 cerr << "Warning: Could not find file '"
5705 << name << "'." << endl;
5706 // write output
5707 context.check_layout(os);
5708 begin_inset(os, "External\n\ttemplate ");
5709 os << "PDFPages\n\tfilename "
5710 << name << "\n";
5711 // parse the options
5712 if (opts.find("pages") != opts.end())
5713 os << "\textra LaTeX \"pages="
5714 << opts["pages"] << "\"\n";
5715 if (opts.find("angle") != opts.end())
5716 os << "\trotateAngle "
5717 << opts["angle"] << '\n';
5718 if (opts.find("origin") != opts.end()) {
5719 ostringstream ss;
5720 string const opt = opts["origin"];
5721 if (opt == "tl") ss << "topleft";
5722 if (opt == "bl") ss << "bottomleft";
5723 if (opt == "Bl") ss << "baselineleft";
5724 if (opt == "c") ss << "center";
5725 if (opt == "tc") ss << "topcenter";
5726 if (opt == "bc") ss << "bottomcenter";
5727 if (opt == "Bc") ss << "baselinecenter";
5728 if (opt == "tr") ss << "topright";
5729 if (opt == "br") ss << "bottomright";
5730 if (opt == "Br") ss << "baselineright";
5731 if (!ss.str().empty())
5732 os << "\trotateOrigin " << ss.str() << '\n';
5733 else
5734 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
5735 }
5736 if (opts.find("width") != opts.end())
5737 os << "\twidth "
5738 << translate_len(opts["width"]) << '\n';
5739 if (opts.find("height") != opts.end())
5740 os << "\theight "
5741 << translate_len(opts["height"]) << '\n';
5742 if (opts.find("keepaspectratio") != opts.end())
5743 os << "\tkeepAspectRatio\n";
5744 end_inset(os);
5745 context.check_layout(os);
5746 registerExternalTemplatePackages("PDFPages");
5747 continue;
5748 }
5749
5750 if (t.cs() == "loadgame") {
5751 p.skip_spaces();
5752 string name = normalize_filename(p.verbatim_item());
5753 string const path = getMasterFilePath(true);
5754 // We want to preserve relative / absolute filenames,
5755 // therefore path is only used for testing
5756 if (!makeAbsPath(name, path).exists()) {
5757 // The file extension is probably missing.
5758 // Now try to find it out.
5759 char const * const lyxskak_format[] = {"fen", 0};
5760 string const lyxskak_name =
5761 find_file(name, path, lyxskak_format);
5762 if (!lyxskak_name.empty())
5763 name = lyxskak_name;
5764 }
5765 FileName const absname = makeAbsPath(name, path);
5766 if (absname.exists())
5767 {
5768 fix_child_filename(name);
5769 copy_file(absname, name);
5770 } else
5771 cerr << "Warning: Could not find file '"
5772 << name << "'." << endl;
5773 context.check_layout(os);
5774 begin_inset(os, "External\n\ttemplate ");
5775 os << "ChessDiagram\n\tfilename "
5776 << name << "\n";
5777 end_inset(os);
5778 context.check_layout(os);
5779 // after a \loadgame follows a \showboard
5780 if (p.get_token().asInput() == "showboard")
5781 p.get_token();
5782 registerExternalTemplatePackages("ChessDiagram");
5783 continue;
5784 }
5785
5786 // try to see whether the string is in unicodesymbols
5787 // Only use text mode commands, since we are in text mode here,
5788 // and math commands may be invalid (bug 6797)
5789 string name = t.asInput();
5790 // handle the dingbats, cyrillic and greek
5791 if (name == "\\ding" || name == "\\textcyr" ||
5792 (name == "\\textgreek" && !preamble.usePolyglossia()))
5793 name = name + '{' + p.getArg('{', '}') + '}';
5794 // handle the ifsym characters
5795 else if (name == "\\textifsymbol") {
5796 string const optif = p.getFullOpt();
5797 string const argif = p.getArg('{', '}');
5798 name = name + optif + '{' + argif + '}';
5799 }
5800 // handle the \ascii characters
5801 // the case of \ascii within braces, as LyX outputs it, is already
5802 // handled for t.cat() == catBegin
5803 else if (name == "\\ascii") {
5804 // the code is "\asci\xxx"
5805 name = "{" + name + p.get_token().asInput() + "}";
5806 skip_braces(p);
5807 }
5808 // handle some TIPA special characters
5809 else if (preamble.isPackageUsed("tipa")) {
5810 if (name == "\\s") {
5811 // fromLaTeXCommand() does not yet
5812 // recognize tipa short cuts
5813 name = "\\textsyllabic";
5814 } else if (name == "\\=" &&
5815 p.next_token().asInput() == "*") {
5816 // fromLaTeXCommand() does not yet
5817 // recognize tipa short cuts
5818 p.get_token();
5819 name = "\\textsubbar";
5820 } else if (name == "\\textdoublevertline") {
5821 // FIXME: This is not correct,
5822 // \textvertline is higher than \textbardbl
5823 name = "\\textbardbl";
5824 skip_braces(p);
5825 } else if (name == "\\!" ) {
5826 if (p.next_token().asInput() == "b") {
5827 p.get_token(); // eat 'b'
5828 name = "\\texthtb";
5829 skip_braces(p);
5830 } else if (p.next_token().asInput() == "d") {
5831 p.get_token();
5832 name = "\\texthtd";
5833 skip_braces(p);
5834 } else if (p.next_token().asInput() == "g") {
5835 p.get_token();
5836 name = "\\texthtg";
5837 skip_braces(p);
5838 } else if (p.next_token().asInput() == "G") {
5839 p.get_token();
5840 name = "\\texthtscg";
5841 skip_braces(p);
5842 } else if (p.next_token().asInput() == "j") {
5843 p.get_token();
5844 name = "\\texthtbardotlessj";
5845 skip_braces(p);
5846 } else if (p.next_token().asInput() == "o") {
5847 p.get_token();
5848 name = "\\textbullseye";
5849 skip_braces(p);
5850 }
5851 } else if (name == "\\*" ) {
5852 if (p.next_token().asInput() == "k") {
5853 p.get_token();
5854 name = "\\textturnk";
5855 skip_braces(p);
5856 } else if (p.next_token().asInput() == "r") {
5857 p.get_token(); // eat 'b'
5858 name = "\\textturnr";
5859 skip_braces(p);
5860 } else if (p.next_token().asInput() == "t") {
5861 p.get_token();
5862 name = "\\textturnt";
5863 skip_braces(p);
5864 } else if (p.next_token().asInput() == "w") {
5865 p.get_token();
5866 name = "\\textturnw";
5867 skip_braces(p);
5868 }
5869 }
5870 }
5871 if ((name.size() == 2 &&
5872 contains("\"'.=^`bcdHkrtuv~", name[1]) &&
5873 p.next_token().asInput() != "*") ||
5874 is_known(name.substr(1), known_tipa_marks)) {
5875 // name is a command that corresponds to a
5876 // combining character in unicodesymbols.
5877 // Append the argument, fromLaTeXCommand()
5878 // will either convert it to a single
5879 // character or a combining sequence.
5880 name += '{' + p.verbatim_item() + '}';
5881 }
5882 // now get the character from unicodesymbols
5883 bool termination;
5884 docstring rem;
5885 set<string> req;
5886 docstring s = normalize_c(encodings.fromLaTeXCommand(from_utf8(name),
5887 Encodings::TEXT_CMD, termination, rem, &req));
5888 if (!s.empty()) {
5889 context.check_layout(os);
5890 os << to_utf8(s);
5891 if (!rem.empty())
5892 output_ert_inset(os, to_utf8(rem), context);
5893 if (termination)
5894 skip_spaces_braces(p);
5895 for (set<string>::const_iterator it = req.begin(); it != req.end(); ++it)
5896 preamble.registerAutomaticallyLoadedPackage(*it);
5897 }
5898 //cerr << "#: " << t << " mode: " << mode << endl;
5899 // heuristic: read up to next non-nested space
5900 /*
5901 string s = t.asInput();
5902 string z = p.verbatim_item();
5903 while (p.good() && z != " " && !z.empty()) {
5904 //cerr << "read: " << z << endl;
5905 s += z;
5906 z = p.verbatim_item();
5907 }
5908 cerr << "found ERT: " << s << endl;
5909 output_ert_inset(os, s + ' ', context);
5910 */
5911 else {
5912 if (t.asInput() == name &&
5913 p.next_token().asInput() == "*") {
5914 // Starred commands like \vspace*{}
5915 p.get_token(); // Eat '*'
5916 name += '*';
5917 }
5918 if (!parse_command(name, p, os, outer, context)) {
5919 output_ert_inset(os, name, context);
5920 // Try to handle options of unknown commands:
5921 // Look if we have an optional arguments,
5922 // and if so, put the brackets in ERT.
5923 while (p.hasOpt()) {
5924 p.get_token(); // eat '['
5925 output_ert_inset(os, "[", context);
5926 os << parse_text_snippet(p, FLAG_BRACK_LAST, outer, context);
5927 output_ert_inset(os, "]", context);
5928 }
5929 }
5930 }
5931 }
5932 }
5933
5934
guessLanguage(Parser & p,string const & lang)5935 string guessLanguage(Parser & p, string const & lang)
5936 {
5937 typedef std::map<std::string, size_t> LangMap;
5938 // map from language names to number of characters
5939 LangMap used;
5940 used[lang] = 0;
5941 for (char const * const * i = supported_CJK_languages; *i; i++)
5942 used[string(*i)] = 0;
5943
5944 while (p.good()) {
5945 Token const t = p.get_token();
5946 // comments are not counted for any language
5947 if (t.cat() == catComment)
5948 continue;
5949 // commands are not counted as well, but we need to detect
5950 // \begin{CJK} and switch encoding if needed
5951 if (t.cat() == catEscape) {
5952 if (t.cs() == "inputencoding") {
5953 string const enc = subst(p.verbatim_item(), "\n", " ");
5954 p.setEncoding(enc, Encoding::inputenc);
5955 continue;
5956 }
5957 if (t.cs() != "begin")
5958 continue;
5959 } else {
5960 // Non-CJK content is counted for lang.
5961 // We do not care about the real language here:
5962 // If we have more non-CJK contents than CJK contents,
5963 // we simply use the language that was specified as
5964 // babel main language.
5965 used[lang] += t.asInput().length();
5966 continue;
5967 }
5968 // Now we are starting an environment
5969 p.pushPosition();
5970 string const name = p.getArg('{', '}');
5971 if (name != "CJK") {
5972 p.popPosition();
5973 continue;
5974 }
5975 // It is a CJK environment
5976 p.popPosition();
5977 /* name = */ p.getArg('{', '}');
5978 string const encoding = p.getArg('{', '}');
5979 /* mapping = */ p.getArg('{', '}');
5980 string const encoding_old = p.getEncoding();
5981 char const * const * const where =
5982 is_known(encoding, supported_CJK_encodings);
5983 if (where)
5984 p.setEncoding(encoding, Encoding::CJK);
5985 else
5986 p.setEncoding("UTF-8");
5987 string const text = p.ertEnvironment("CJK");
5988 p.setEncoding(encoding_old);
5989 p.skip_spaces();
5990 if (!where) {
5991 // ignore contents in unknown CJK encoding
5992 continue;
5993 }
5994 // the language of the text
5995 string const cjk =
5996 supported_CJK_languages[where - supported_CJK_encodings];
5997 used[cjk] += text.length();
5998 }
5999 LangMap::const_iterator use = used.begin();
6000 for (LangMap::const_iterator it = used.begin(); it != used.end(); ++it) {
6001 if (it->second > use->second)
6002 use = it;
6003 }
6004 return use->first;
6005 }
6006
6007
check_comment_bib(ostream & os,Context & context)6008 void check_comment_bib(ostream & os, Context & context)
6009 {
6010 if (!need_commentbib)
6011 return;
6012 // We have a bibliography database, but no bibliography with biblatex
6013 // which is completely valid. Insert a bibtex inset in a note.
6014 context.check_layout(os);
6015 begin_inset(os, "Note Note\n");
6016 os << "status open\n";
6017 os << "\\begin_layout Plain Layout\n";
6018 begin_command_inset(os, "bibtex", "bibtex");
6019 string bibfiles;
6020 for (auto const & bf : preamble.biblatex_bibliographies) {
6021 if (!bibfiles.empty())
6022 bibfiles += ",";
6023 bibfiles += normalize_filename(bf);
6024 }
6025 if (!bibfiles.empty())
6026 os << "bibfiles " << '"' << bibfiles << '"' << "\n";
6027 end_inset(os);// Bibtex
6028 os << "\\end_layout\n";
6029 end_inset(os);// Note
6030 }
6031
6032 // }])
6033
6034
6035 } // namespace lyx
6036