]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/tex2lyx.C
fix bug 1750
[lyx.git] / src / tex2lyx / tex2lyx.C
index c14d3c098d8321bb517c9bf9dbd59bc41ec4c587..62c99b4922f3f737b040a1383d66f8121bf396ce 100644 (file)
@@ -19,6 +19,8 @@
 #include "lyxtextclass.h"
 #include "support/path_defines.h"
 #include "support/filetools.h"
+#include "support/lstrings.h"
+#include "support/lyxlib.h"
 #include "support/os.h"
 
 #include <boost/function.hpp>
@@ -45,6 +47,10 @@ using std::string;
 using std::vector;
 using std::map;
 
+using lyx::support::isStrUnsignedInt;
+using lyx::support::ltrim;
+using lyx::support::rtrim;
+using lyx::support::strToUnsignedInt;
 using lyx::support::system_lyxdir;
 using lyx::support::user_lyxdir;
 using lyx::support::IsFileReadable;
@@ -118,6 +124,34 @@ string active_environment()
 map<string, vector<ArgumentType> > known_commands;
 
 
+void add_known_command(string const & command, string const & o1,
+                       bool o2)
+{
+       // We have to handle the following cases:
+       // definition                      o1    o2    invocation result
+       // \newcommand{\foo}{bar}          ""    false \foo       bar
+       // \newcommand{\foo}[1]{bar #1}    "[1]" false \foo{x}    bar x
+       // \newcommand{\foo}[1][]{bar #1}  "[1]" true  \foo       bar 
+       // \newcommand{\foo}[1][]{bar #1}  "[1]" true  \foo[x]    bar x
+       // \newcommand{\foo}[1][x]{bar #1} "[1]" true  \foo[x]    bar x
+       unsigned int nargs = 0;
+       vector<ArgumentType> arguments;
+       string const opt1 = rtrim(ltrim(o1, "["), "]");
+       if (isStrUnsignedInt(opt1)) {
+               // The command has arguments
+               nargs = strToUnsignedInt(opt1);
+               if (nargs > 0 && o2) {
+                       // The first argument is optional
+                       arguments.push_back(optional);
+                       --nargs;
+               }
+       }
+       for (unsigned int i = 0; i < nargs; ++i)
+               arguments.push_back(required);
+       known_commands[command] = arguments;
+}
+
+
 namespace {
 
 
@@ -278,9 +312,19 @@ void easyParse(int & argc, char * argv[])
        }
 }
 
+
+// path of the parsed file
+string masterFilePath;
+
 } // anonymous namespace
 
 
+string getMasterFilePath()
+{
+       return masterFilePath;
+}
+
+
 void tex2lyx(std::istream &is, std::ostream &os)
 {
        Parser p(is);
@@ -293,7 +337,7 @@ void tex2lyx(std::istream &is, std::ostream &os)
        Context context(true, textclass);
        parse_text(p, ss, FLAG_END, true, context);
        context.check_end_layout(ss);
-       ss << "\n\\end_document\n";
+       ss << "\n\\end_body\n\\end_document\n";
        active_environments.pop_back();
        ss.seekg(0);
        os << ss.str();
@@ -336,7 +380,7 @@ int main(int argc, char * argv[])
                return 2;
        }
 
-       lyx::support::os::init(&argc, &argv);
+       lyx::support::os::init(argc, argv);
        lyx::support::setLyxPaths();
 
        string const system_syntaxfile = lyx::support::LibFileSearch("reLyX", "syntax.default");
@@ -353,6 +397,12 @@ int main(int argc, char * argv[])
                     << "\" for reading." << endl;
                return 2;
        }
+
+       if (lyx::support::AbsolutePath(argv[1]))
+               masterFilePath = lyx::support::OnlyPath(argv[1]);
+       else
+               masterFilePath = lyx::support::getcwd();
+
        ifstream is(argv[1]);
        tex2lyx(is, cout);