]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/text.C
* support/qstring_helpers.h: erase ucs4_to_qstring() method.
[lyx.git] / src / tex2lyx / text.C
index bbcfa593f087dbceb84730bfc1deca038f779ded..d28fdea83bc4c58cec6b3edf4510df10cff40df2 100644 (file)
 #include <sstream>
 #include <vector>
 
-using lyx::support::changeExtension;
-using lyx::support::makeAbsPath;
-using lyx::support::makeRelPath;
-using lyx::support::rtrim;
-using lyx::support::suffixIs;
-using lyx::support::contains;
-using lyx::support::subst;
+
+namespace lyx {
+
+using support::addExtension;
+using support::changeExtension;
+using support::FileName;
+using support::makeAbsPath;
+using support::makeRelPath;
+using support::rtrim;
+using support::suffixIs;
+using support::contains;
+using support::subst;
 
 using std::cerr;
 using std::endl;
@@ -120,8 +125,9 @@ char const * const known_natbib_commands[] = { "cite", "citet", "citep",
  * No starred form other than "cite*" known.
  */
 char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
-"citealt", "citealp", "citeauthor", "citeyear", "citeyearpar", "fullcite",
+"citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
 // jurabib commands not (yet) supported by LyX:
+// "fullcite",
 // "footcite", "footcitet", "footcitep", "footcitealt", "footcitealp",
 // "footciteauthor", "footciteyear", "footciteyearpar",
 "citefield", "citetitle", "cite*", 0 };
@@ -132,7 +138,7 @@ char const * const known_quotes[] = { "glqq", "grqq", "quotedblbase",
 
 /// the same as known_quotes with .lyx names
 char const * const known_coded_quotes[] = { "gld", "grd", "gld",
-"grd", "gls", "fls", "frd", 0};
+"grd", "gls", "fls", "frs", 0};
 
 /// LaTeX names for font sizes
 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
@@ -198,6 +204,15 @@ char const * const known_pdftex_graphics_formats[] = {"png", "pdf", "jpg",
  */
 char const * const known_tex_extensions[] = {"tex", 0};
 
+/// spaces known by InsetSpace
+char const * const known_spaces[] = { " ", "space", ",", "thinspace", "quad",
+"qquad", "enspace", "enskip", "negthinspace", 0};
+
+/// the same as known_spaces with .lyx names
+char const * const known_coded_spaces[] = { "space{}", "space{}",
+"thinspace{}", "thinspace{}", "quad{}", "qquad{}", "enspace{}", "enskip{}",
+"negthinspace{}", 0};
+
 
 /// splits "x=z, y=b" into a map
 map<string, string> split_map(string const & s)
@@ -340,11 +355,11 @@ void translate_box_len(string const & length, string & value, string & unit, str
 string find_file(string const & name, string const & path,
                 char const * const * extensions)
 {
+       // FIXME UNICODE encoding of name and path may be wrong (makeAbsPath
+       // expects utf8)
        for (char const * const * what = extensions; *what; ++what) {
-               // We don't use ChangeExtension() because it does the wrong
-               // thing if name contains a dot.
-               string const trial = name + '.' + (*what);
-               if (fs::exists(makeAbsPath(trial, path)))
+               string const trial = addExtension(name, *what);
+               if (fs::exists(makeAbsPath(trial, path).toFilesystemEncoding()))
                        return trial;
        }
        return string();
@@ -1007,7 +1022,9 @@ void fix_relative_filename(string & name)
 {
        if (lyx::support::absolutePath(name))
                return;
-       name = makeRelPath(makeAbsPath(name, getMasterFilePath()),
+       // FIXME UNICODE encoding of name may be wrong (makeAbsPath expects
+       // utf8)
+       name = makeRelPath(makeAbsPath(name, getMasterFilePath()).absFilename(),
                           getParentFilePath());
 }
 
@@ -1038,7 +1055,13 @@ void parse_noweb(Parser & p, ostream & os, Context & context)
                return;
        }
 
-       context.check_end_layout(os);
+       // We use new_paragraph instead of check_end_layout because the stuff
+       // following the noweb chunk needs to start with a \begin_layout.
+       // This may create a new paragraph even if there was none in the
+       // noweb file, but the alternative is an invalid LyX file. Since
+       // noweb code chunks are implemented with a layout style in LyX they
+       // always must be in an own paragraph.
+       context.new_paragraph(os);
        Context newcontext(true, context.textclass, context.textclass["Scrap"]);
        newcontext.check_layout(os);
        os << name;
@@ -1466,7 +1489,9 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        string const path = getMasterFilePath();
                        // We want to preserve relative / absolute filenames,
                        // therefore path is only used for testing
-                       if (!fs::exists(makeAbsPath(name, path))) {
+                       // FIXME UNICODE encoding of name and path may be
+                       // wrong (makeAbsPath expects utf8)
+                       if (!fs::exists(makeAbsPath(name, path).toFilesystemEncoding())) {
                                // The file extension is probably missing.
                                // Now try to find it out.
                                string const dvips_name =
@@ -1496,7 +1521,9 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                        name = pdftex_name;
                        }
 
-                       if (fs::exists(makeAbsPath(name, path)))
+                       // FIXME UNICODE encoding of name and path may be
+                       // wrong (makeAbsPath expects utf8)
+                       if (fs::exists(makeAbsPath(name, path).toFilesystemEncoding()))
                                fix_relative_filename(name);
                        else
                                cerr << "Warning: Could not find graphics file '"
@@ -2073,9 +2100,16 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                         || t.cs() == "'" || t.cs() == "`"
                         || t.cs() == "~" || t.cs() == "." || t.cs() == "=") {
                        // we need the trim as the LyX parser chokes on such spaces
+                       // The argument of InsetLatexAccent is parsed as a
+                       // subset of LaTeX, so don't parse anything here,
+                       // but use the raw argument.
+                       // Otherwise we would convert \~{\i} wrongly.
+                       // This will of course not translate \~{\ss} to \~{ß},
+                       // but that does at least compile and does only look
+                       // strange on screen.
                        context.check_layout(os);
-                       os << "\n\\i \\" << t.cs() << "{"
-                          << trim(parse_text_snippet(p, FLAG_ITEM, outer, context), " ")
+                       os << "\\i \\" << t.cs() << "{"
+                          << trim(p.verbatim_item(), " ")
                           << "}\n";
                }
 
@@ -2085,9 +2119,10 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        skip_braces(p); // eat {}
                }
 
-               else if (t.cs() == "i" || t.cs() == "j") {
+               else if (t.cs() == "i" || t.cs() == "j" || t.cs() == "l" ||
+                        t.cs() == "L") {
                        context.check_layout(os);
-                       os << "\\" << t.cs() << ' ';
+                       os << "\\i \\" << t.cs() << "{}\n";
                        skip_braces(p); // eat {}
                }
 
@@ -2117,8 +2152,10 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        string const path = getMasterFilePath();
                        // We want to preserve relative / absolute filenames,
                        // therefore path is only used for testing
+                       // FIXME UNICODE encoding of filename and path may be
+                       // wrong (makeAbsPath expects utf8)
                        if (t.cs() == "include" &&
-                           !fs::exists(makeAbsPath(filename, path))) {
+                           !fs::exists(makeAbsPath(filename, path).toFilesystemEncoding())) {
                                // The file extension is probably missing.
                                // Now try to find it out.
                                string const tex_name =
@@ -2127,16 +2164,18 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                if (!tex_name.empty())
                                        filename = tex_name;
                        }
-                       if (fs::exists(makeAbsPath(filename, path))) {
+                       // FIXME UNICODE encoding of filename and path may be
+                       // wrong (makeAbsPath expects utf8)
+                       if (fs::exists(makeAbsPath(filename, path).toFilesystemEncoding())) {
                                string const abstexname =
-                                       makeAbsPath(filename, path);
+                                       makeAbsPath(filename, path).absFilename();
                                string const abslyxname =
                                        changeExtension(abstexname, ".lyx");
                                fix_relative_filename(filename);
                                string const lyxname =
                                        changeExtension(filename, ".lyx");
                                if (t.cs() != "verbatiminput" &&
-                                   tex2lyx(abstexname, abslyxname)) {
+                                   tex2lyx(abstexname, FileName(abslyxname))) {
                                        os << name << '{' << lyxname << "}\n";
                                } else {
                                        os << name << '{' << filename << "}\n";
@@ -2184,10 +2223,31 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        skip_braces(p);
                }
 
-               else if (t.cs() == "newpage") {
+               else if (is_known(t.cs(), known_spaces)) {
+                       char const * const * where = is_known(t.cs(), known_spaces);
                        context.check_layout(os);
-                       // FIXME: what about \\clearpage and \\pagebreak?
-                       os << "\n\\newpage\n";
+                       begin_inset(os, "InsetSpace ");
+                       os << '\\' << known_coded_spaces[where - known_spaces]
+                          << '\n';
+                       // LaTeX swallows whitespace after all spaces except
+                       // "\\,". We have to do that here, too, because LyX
+                       // adds "{}" which would make the spaces significant.
+                       if (t.cs() !=  ",")
+                               eat_whitespace(p, os, context, false);
+                       // LyX adds "{}" after all spaces except "\\ " and
+                       // "\\,", so we have to remove "{}".
+                       // "\\,{}" is equivalent to "\\," in LaTeX, so we
+                       // remove the braces after "\\,", too.
+                       if (t.cs() != " ")
+                               skip_braces(p);
+               }
+
+               else if (t.cs() == "newpage" ||
+                        t.cs() == "clearpage" ||
+                        t.cs() == "cleardoublepage") {
+                       context.check_layout(os);
+                       // FIXME: what about \\pagebreak?
+                       os << "\n\\" << t.cs() << "\n";
                        skip_braces(p); // eat {}
                }
 
@@ -2324,3 +2384,6 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
 }
 
 // }])
+
+
+} // namespace lyx