]> git.lyx.org Git - lyx.git/blobdiff - src/support/path_defines.C.in
tostr -> convert and some bformat work
[lyx.git] / src / support / path_defines.C.in
index c55d577677a7be00a53545bf81917668b0e720b1..dfe7062efb274395531da49a61478c172be4b5a8 100644 (file)
@@ -6,7 +6,7 @@
  *
  * \author Angus Leeming
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  *
  * Warning! This file is autogenerated from path_defines.C.in.
  * All changes to this file will be lost.
 #include "filetools.h"
 #include "lstrings.h"
 #include "os.h"
+#include "lyxlib.h"
 
+using std::string;
 using std::endl;
 
 
-string build_lyxdir;
-string system_lyxdir;
-string user_lyxdir;
+namespace {
 
+// Store for the path to the lyx support files we're actually going to use.
+string system_lyxdir_;
 
-namespace {
+// Store for the path to the autogenerated files when running in place
+string build_lyxdir_;
+
+// Store for the path to the user-level support files.
+string user_lyxdir_;
+
+// Store for the path to the locale directory.
+string localedir_;
 
 /* The absolute path to the system-level lyx support files.
- * (Compile-time value.)
+ * (Make-time value.)
  */
 string const & lyx_dir()
 {
@@ -43,11 +52,17 @@ string const & lyx_dir()
        return ld;
 }
 
+} // namespace anon
+
+
+namespace lyx {
+namespace support {
+
 
 /* The absolute path to the top of the lyx build tree.
- * (Compile-time value.)
+ * (Make-time value.)
  */
-string const & lyx_top_srcdir()
+string const & top_srcdir()
 {
        static string const lts = "%TOP_SRCDIR%";
        return lts;
@@ -55,19 +70,60 @@ string const & lyx_top_srcdir()
 
 
 /* The absolute path to the system-level lyx locale directory.
- * (Compile-time value.)
+ * (Make-time value.)
  */
 string const & lyx_localedir()
 {
        static string const ll = "%LOCALEDIR%";
-       return ll;
+       if (localedir_.empty())
+               return ll;
+       else
+               return localedir_;
 }
 
-} // namespace anon
+
+string const & build_dir()
+{
+       static string const bl = "%BUILDDIR%";
+       return bl;
+}
 
 
-namespace lyx {
-namespace support {
+string const & build_lyxdir()
+{
+       return build_lyxdir_;
+}
+
+
+void build_lyxdir(string const & sld)
+{
+       build_lyxdir_ = sld;
+}
+
+
+string const & system_lyxdir()
+{
+       return system_lyxdir_;
+}
+
+
+void system_lyxdir(string const & sld)
+{
+       system_lyxdir_ = sld;
+}
+
+
+string const & user_lyxdir()
+{
+       return user_lyxdir_;
+}
+
+
+void user_lyxdir(string const & uld)
+{
+       user_lyxdir_ = uld;
+}
+
 
 bool setLyxPaths()
 {
@@ -88,6 +144,26 @@ bool setLyxPaths()
        lyxerr[Debug::INIT] << "Name of binary: " << binname << endl;
        lyxerr[Debug::INIT] << "Path of binary: " << binpath << endl;
 
+
+       string searchpath;
+
+       //
+       // Determine whether we are running in place and set
+       // build_lyxdir accordingly
+       //
+
+       string const buildlyxdir = MakeAbsPath("../lib", binpath);
+       if (!FileSearch(buildlyxdir, "lyxrc.defaults").empty()) {
+               searchpath += AddPath(top_srcdir(), "lib") + ';';
+               build_lyxdir(buildlyxdir);
+               lyxerr[Debug::INIT] << "Checking whether LyX is run in "
+                       "place... yes" << endl;
+       } else {
+               lyxerr[Debug::INIT]
+                       << "Checking whether LyX is run in place... no"
+                       << endl;
+       }
+
        //
        // Determine system directory.
        //
@@ -96,20 +172,20 @@ bool setLyxPaths()
        // 1) -sysdir command line parameter
        // 2) LYX_DIR_14x environment variable
        // 3) Maybe <path of binary>/TOP_SRCDIR/lib
-       // 4) <path of binary>/../share/<name of binary>/
-       // 4a) repeat 4 after following the Symlink if <path of
+       // 4) <path of binary>/../Resources/<name of binary>/  [for LyX/Mac]
+       // 5) <path of binary>/../share/<name of binary>/
+       // 5a) repeat 4 after following the Symlink if <path of
        //     binary> is a symbolic link.
-       // 5) hardcoded lyx_dir
+       // 6) hardcoded lyx_dir
        // The directory is checked for the presence of the file
        // "chkconfig.ltx", and if that is present, the directory
        // is accepted as the system directory.
        // If no chkconfig.ltx file is found, a warning is given,
        // and the hardcoded lyx_dir is used.
 
-       // If we had a command line switch, system_lyxdir is already set
-       string searchpath;
-       if (!system_lyxdir.empty())
-               searchpath = MakeAbsPath(system_lyxdir) + ';';
+       // If we had a command line switch, system_lyxdir_ is already set
+       if (!system_lyxdir_.empty())
+               searchpath = MakeAbsPath(system_lyxdir_) + ';';
 
        string const lyxdir = GetEnvPath("LYX_DIR_14x");
 
@@ -118,6 +194,10 @@ bool setLyxPaths()
                searchpath += lyxdir + ';';
        }
 
+       // Path of binary/../Resources/
+       searchpath += NormalizePath(AddPath(binpath, "../Resources/") +
+                                   OnlyFilename(binname)) + ';';
+
        string fullbinpath = binpath;
        FileInfo file(fullbinname, true);
        if (file.isLink()) {
@@ -137,8 +217,18 @@ bool setLyxPaths()
        bool followlink;
        do {
                // Path of binary/../share/name of binary/
-               searchpath += NormalizePath(AddPath(binpath, "../share/") +
-                     OnlyFilename(binname)) + ';';
+               string const exe_name = OnlyFilename(binname);
+#ifdef _WIN32
+               string const lyx_system_dir_name =
+                       suffixIs(exe_name, ".exe") ?
+                               ChangeExtension(exe_name, "") :
+                               exe_name;
+#else
+               string const lyx_system_dir_name = exe_name;
+#endif
+               string const shared_dir_name =
+                       NormalizePath(AddPath(binpath, "../share/"));
+               searchpath += shared_dir_name + lyx_system_dir_name + ';';
 
                // Follow Symlinks
                FileInfo file(fullbinpath, true);
@@ -158,7 +248,7 @@ bool setLyxPaths()
        } while (followlink);
 
        // <absolute top srcdir>/lib
-       searchpath += AddPath(lyx_top_srcdir(), "lib") + ';';
+       searchpath += AddPath(top_srcdir(), "lib") + ';';
        // Hardcoded dir
        searchpath += lyx_dir();
 
@@ -167,26 +257,26 @@ bool setLyxPaths()
 
        string const filename = "chkconfig.ltx";
        string const temp = FileOpenSearch(searchpath, filename, string());
-       system_lyxdir = OnlyPath(temp);
+       system_lyxdir_ = OnlyPath(temp);
 
        // Reduce "path/../path" stuff out of system directory
-       system_lyxdir = NormalizePath(system_lyxdir);
+       system_lyxdir_ = NormalizePath(system_lyxdir_);
 
        bool path_shown = false;
 
        // Warn if environment variable is set, but unusable
        if (!lyxdir.empty()) {
-               if (system_lyxdir != NormalizePath(lyxdir)) {
+               if (system_lyxdir_ != NormalizePath(lyxdir)) {
                        lyxerr <<_("LYX_DIR_14x environment variable no good.")
                               << '\n'
                               << _("System directory set to: ")
-                              << system_lyxdir << endl;
+                              << system_lyxdir_ << endl;
                        path_shown = true;
                }
        }
 
        // Warn the user if we couldn't find "chkconfig.ltx"
-       if (system_lyxdir == "./") {
+       if (system_lyxdir_ == "./") {
                lyxerr <<_("LyX Warning! Couldn't determine system directory. ")
                       <<_("Try the '-sysdir' command line parameter or ")
                       <<_("set the environment variable LYX_DIR_14x to the "
@@ -204,13 +294,58 @@ bool setLyxPaths()
                } else {
                        lyxerr << _("Expect problems.") << endl;
                }
-               system_lyxdir = lyx_dir();
+               system_lyxdir_ = lyx_dir();
                path_shown = true;
        }
 
        if (!path_shown)
                lyxerr[Debug::INIT] << "System directory: '"
-                                   << system_lyxdir << '\'' << endl;
+                                   << system_lyxdir_ << '\'' << endl;
+
+       // This is true when we are running from a Mac OS X bundle
+       // (for LyX/Mac)
+       bool const inOSXBundle =
+               system_lyxdir_ == NormalizePath(AddPath(binpath, "../Resources/")
+                                               + OnlyFilename(binname));
+       if (inOSXBundle)
+               lyxerr[Debug::INIT] << "Running from LyX/Mac bundle."
+                                   << endl;
+
+       //
+       // Set PATH for LyX/Mac
+       // LyX/Mac is a relocatable application bundle; here we add to
+       // the PATH so it can find binaries like reLyX inside its own
+       // application bundle, and also append PATH elements that it
+       // needs to run latex, previewers, etc.
+       //
+
+       if (inOSXBundle) {
+               const string newpath = GetEnv("PATH") + ":" + binpath
+                       + ":/sw/bin:/usr/local/bin"
+                       + ":/usr/local/teTeX/bin/powerpc-apple-darwin-current";
+               lyx::support::putenv("PATH", newpath);
+               lyxerr[Debug::INIT] << "LyX/Mac: setting PATH to: "
+                                   << GetEnv("PATH") << endl;
+       }
+
+
+       //
+       // Determine locale directory
+       //
+       if (!GetEnv("LYX_LOCALEDIR").empty()) {
+               localedir_ = GetEnv("LYX_LOCALEDIR");
+               lyxerr[Debug::INIT] << "LYX_LOCALEDIR=" << localedir_ << endl;
+       } else if (inOSXBundle) {
+               string const maybe_localedir =
+                       NormalizePath(AddPath(system_lyxdir_, "../locale"));
+               FileInfo fi(maybe_localedir);
+               if (fi.isOK() && fi.isDir()) {
+                       lyxerr[Debug::INIT]
+                               << "LyX/Mac: setting locale directory to "
+                               << maybe_localedir << endl;
+                       localedir_ = maybe_localedir;
+               }
+       }
 
        //
        // Determine user lyx-dir
@@ -221,22 +356,26 @@ bool setLyxPaths()
        // 2) LYX_USERDIR_14x environment variable
        // 3) $HOME/.<name of binary>
 
-       // If we had a command line switch, user_lyxdir is already set
+       // If we had a command line switch, user_lyxdir_ is already set
        bool explicit_userdir = true;
-       if (user_lyxdir.empty()) {
+       if (user_lyxdir_.empty()) {
 
                // LYX_USERDIR_14x environment variable
-               user_lyxdir = GetEnvPath("LYX_USERDIR_14x");
+               user_lyxdir_ = GetEnvPath("LYX_USERDIR_14x");
 
                // default behaviour
-               if (user_lyxdir.empty())
-                       user_lyxdir = AddPath(GetEnvPath("HOME"),
-                                             string(".") + PACKAGE);
-                       explicit_userdir = false;
+               if (user_lyxdir_.empty())
+                       if (inOSXBundle)
+                               user_lyxdir_ = AddPath(os::homepath(),
+                                                      "Library/Preferences/LyX");
+                       else
+                               user_lyxdir_ = AddPath(os::homepath(),
+                                                      string(".") + PACKAGE);
+               explicit_userdir = false;
        }
 
        lyxerr[Debug::INIT] << "User LyX directory: '"
-                           <<  user_lyxdir << '\'' << endl;
+                           <<  user_lyxdir_ << '\'' << endl;
        return explicit_userdir;
 }