]> git.lyx.org Git - lyx.git/blobdiff - src/support/os.cpp
Correct comment
[lyx.git] / src / support / os.cpp
index b1e4e09b8eb0d9c38e0b64be93bd35bb174c9ab8..3a5c2e59a25eaf4e04032864e5df607a0147a78c 100644 (file)
 
 #include <config.h>
 
+#ifdef _WIN32
+# define _WIN32_WINNT 0x0600
+#endif
+
 #include "support/convert.h"
 #include "support/debug.h"
 #include "support/filetools.h"
@@ -61,7 +65,7 @@ static string const python23_call(string const & binary, bool verbose = false)
        smatch sm;
        try {
                static regex const python_reg("\\((\\d*), (\\d*)\\)");
-               if (out.first < 0 || !regex_match(out.second, sm, python_reg))
+               if (!out.valid || !regex_match(out.result, sm, python_reg))
                        return string();
        } catch(regex_error const & /*e*/) {
                LYXERR0("Regex error! This should not happen.");
@@ -74,7 +78,7 @@ static string const python23_call(string const & binary, bool verbose = false)
                return string();
 
        if (verbose)
-               lyxerr << "Found Python " << out.second << "\n";
+               lyxerr << "Found Python " << out.result << "\n";
        // Add the -tt switch so that mixed tab/whitespace
        // indentation is an error
        return binary + " -tt";
@@ -89,28 +93,73 @@ static string const find_python_binary()
        // PEP 397 -- Python launcher for Windows
        // https://www.python.org/dev/peps/pep-0397/
 
-       // Check through python launcher whether python3 is
+#ifdef _WIN32
+       // Check through python launcher whether python 3 is
        // installed on computer.
        string command = python23_call("py -3");
+#else
+       // Check whether python3 in PATH is the right one.
+       string command = python23_call("python3");
+#endif // _WIN32
        if (!command.empty())
                return command;
 
+#ifndef _WIN32
+       // python3 does not exists, let us try to find python3.x in PATH
+       // the search is probably broader than required
+       // but we are trying hard to find a valid python binary
+       vector<string> const path = getEnvPath("PATH");
+       lyxerr << "Looking for python 3.x ...\n";
+       for (auto bin : path) {
+               QString const dir = toqstr(bin);
+               string const localdir = dir.toLocal8Bit().constData();
+               QDir qdir(dir);
+               qdir.setFilter(QDir::Files | QDir::Executable);
+               QStringList list = qdir.entryList(QStringList("python3*"));
+               for (auto bin2 : list) {
+                       string const binary = "\"" + addName(localdir,
+                               bin2.toLocal8Bit().constData()) + "\"";
+                       command = python23_call(binary, true);
+                       if (!command.empty())
+                               return command;
+               }
+       }
+#endif // !_WIN32
+
        // python 3 was not found let us look for python 2
+#ifdef _WIN32
        command = python23_call("py -2");
+#else
+       command = python23_call("python2");
+#endif // _WIN32
        if (!command.empty())
                return command;
 
-       // python3 does not exists, let us try to find python3.x in PATH
+#ifdef _WIN32
+       // python launcher is not installed, let cmd auto check 
+       // PATH for a python.exe
+       command = python23_call("python");
+       if (!command.empty())
+               return command;
+
+       //failed, prepare to search PATH manually
+       vector<string> const path = getEnvPath("PATH");
+       lyxerr << "Manually looking for python in PATH ...\n";
+       QString const exeName = "python*";
+#else
+       // python2 does not exists, let us try to find python2.x in PATH
        // the search is probably broader than required
        // but we are trying hard to find a valid python binary
-       vector<string> const path = getEnvPath("PATH");
-       lyxerr << "Looking for python in PATH ...\n";
+       lyxerr << "Looking for python 2.x ...\n";
+       QString const exeName = "python2*";
+#endif // _WIN32
+
        for (auto bin : path) {
                QString const dir = toqstr(bin);
                string const localdir = dir.toLocal8Bit().constData();
                QDir qdir(dir);
                qdir.setFilter(QDir::Files | QDir::Executable);
-               QStringList list = qdir.entryList(QStringList("python*"));
+               QStringList list = qdir.entryList(QStringList(exeName));
                for (auto bin2 : list) {
                        string const binary = "\"" + addName(localdir,
                                bin2.toLocal8Bit().constData()) + "\"";