]> git.lyx.org Git - lyx.git/blobdiff - src/lyx_main.C
Point fix, earlier forgotten
[lyx.git] / src / lyx_main.C
index 20ba091dd9e950026763bd8bcd5734929d76c844..dc59f8c2113b986cfe850b54a644d9166ea614cd 100644 (file)
@@ -1,9 +1,15 @@
 /**
  * \file lyx_main.C
- * Copyright 1995-2002 the LyX Team
- * Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
+ * \author Alfredo Braunstein
  * \author Lars Gullik Bjønnes
+ * \author Jean-Marc Lasgouttes
+ * \author John Levon
+ * \author André Pönitz
+ *
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
@@ -13,9 +19,9 @@
 
 #include "support/filetools.h"
 #include "support/lyxlib.h"
-#include "support/os.h"
 #include "support/FileInfo.h"
 #include "support/path.h"
+#include "support/path_defines.h"
 #include "debug.h"
 #include "gettext.h"
 #include "lyxlex.h"
@@ -59,15 +65,10 @@ using std::signal;
 using std::system;
 #endif
 
-extern void LoadLyXFile(string const &);
 extern void QuitLyX();
 
 extern LyXServer * lyxserver;
 
-string system_lyxdir;
-string build_lyxdir;
-string user_lyxdir;
-
 DebugStream lyxerr;
 
 boost::scoped_ptr<LastFiles> lastfiles;
@@ -96,18 +97,13 @@ LyX::LyX(int & argc, char * argv[])
        // we need to parse for "-dbg" and "-help"
        bool const want_gui = easyParse(argc, argv);
 
-       // Global bindings (this must be done as early as possible.) (Lgb)
-       toplevel_keymap.reset(new kb_keymap);
-       defaultKeyBindings(toplevel_keymap.get());
-
        // set the DisplayTranslator only once; should that be done here??
        // if this should not be in this file, please also remove
        // #include "graphics/GraphicsTypes.h" at the top -- Rob Lahaye.
        lyx::graphics::setDisplayTranslator();
 
-       if (want_gui) {
+       if (want_gui)
                lyx_gui::parse_init(argc, argv);
-       }
 
        // check for any spurious extra arguments
        // other than documents
@@ -124,15 +120,13 @@ LyX::LyX(int & argc, char * argv[])
        init(want_gui);
        lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
 
-       if (want_gui) {
+       if (want_gui) 
                lyx_gui::parse_lyxrc();
-       }
 
        vector<string> files;
 
-       for (int argi = argc - 1; argi >= 1; --argi) {
+       for (int argi = argc - 1; argi >= 1; --argi) 
                files.push_back(argv[argi]);
-       }
 
        if (first_start)
                files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
@@ -153,14 +147,14 @@ LyX::LyX(int & argc, char * argv[])
                        // the filename if necessary
                        string s = FileSearch(string(), *it, "lyx");
                        if (s.empty()) {
-                               last_loaded = newFile(*it, "");
+                               last_loaded = newFile(*it, string(), true);
                        } else {
-                               last_loaded = bufferlist.newBuffer(s, false);
-                               last_loaded->error.connect(boost::bind(&LyX::printError, this, _1));
-                               if (!loadLyXFile(last_loaded, s)) {
-                                       bufferlist.release(last_loaded);
-                                       last_loaded = newFile(*it, string());
-                               }
+                               Buffer * buf = bufferlist.newBuffer(s, false);
+                               buf->error.connect(boost::bind(&LyX::printError, this, _1));
+                               if (loadLyXFile(buf, s)) 
+                                       last_loaded = buf;
+                               else
+                                       bufferlist.release(buf);
                        }
                }
 
@@ -241,184 +235,7 @@ void LyX::init(bool gui)
        signal(SIGINT, error_handler);
        signal(SIGTERM, error_handler);
 
-       //
-       // Determine path of binary
-       //
-
-       string binpath = os::binpath();
-       string binname = os::binname();
-       string fullbinname = MakeAbsPath(binname, binpath);
-
-       if (binpath.empty()) {
-               lyxerr << _("Warning: could not determine path of binary.")
-                      << "\n"
-                      << _("If you have problems, try starting LyX with an absolute path.")
-                      << endl;
-       }
-       lyxerr[Debug::INIT] << "Name of binary: " << binname << endl;
-       lyxerr[Debug::INIT] << "Path of binary: " << binpath << endl;
-
-       //
-       // Determine system directory.
-       //
-
-       // Directories are searched in this order:
-       // 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
-       //     binary> is a symbolic link.
-       // 5) 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) + ';';
-
-       string const lyxdir = GetEnvPath("LYX_DIR_14x");
-
-       if (!lyxdir.empty()) {
-               lyxerr[Debug::INIT] << "LYX_DIR_14x: " << lyxdir << endl;
-               searchpath += lyxdir + ';';
-       }
-
-       string fullbinpath = binpath;
-       FileInfo file(fullbinname, true);
-       if (file.isLink()) {
-               lyxerr[Debug::INIT] << "binary is a link" << endl;
-               string link;
-               if (LyXReadLink(fullbinname, link, true)) {
-                       // Path of binary/../share/name of binary/
-                       searchpath += NormalizePath(AddPath(binpath,
-                                                           "../share/")
-                                                   + OnlyFilename(binname));
-                       searchpath += ';';
-                       fullbinpath = link;
-                       binpath = MakeAbsPath(OnlyPath(fullbinpath));
-               }
-       }
-
-       bool followlink;
-       do {
-               // Path of binary/../share/name of binary/
-               searchpath += NormalizePath(AddPath(binpath, "../share/") +
-                     OnlyFilename(binname)) + ';';
-
-               // Follow Symlinks
-               FileInfo file(fullbinpath, true);
-               followlink = file.isLink();
-               if (followlink) {
-                       lyxerr[Debug::INIT] << " directory " << fullbinpath
-                                           << " is a link" << endl;
-                       string link;
-                       if (LyXReadLink(fullbinpath, link, true)) {
-                               fullbinpath = link;
-                               binpath = MakeAbsPath(OnlyPath(fullbinpath));
-                       }
-                       else {
-                               followlink = false;
-                       }
-               }
-       } while (followlink);
-
-       // <path of binary>/TOP_SRCDIR/lib
-       build_lyxdir = MakeAbsPath("../lib", binpath);
-       if (!FileSearch(build_lyxdir, "lyxrc.defaults").empty()) {
-               searchpath += MakeAbsPath(AddPath(TOP_SRCDIR, "lib"),
-                                         binpath) + ';';
-               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;
-               build_lyxdir.erase();
-       }
-
-       // Hardcoded dir
-       searchpath += LYX_DIR;
-
-       lyxerr[Debug::INIT] << "System directory search path: "
-                           << searchpath << endl;
-
-       string const filename = "chkconfig.ltx";
-       string const temp = FileOpenSearch(searchpath, filename, string());
-       system_lyxdir = OnlyPath(temp);
-
-       // Reduce "path/../path" stuff out of system directory
-       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)) {
-                       lyxerr <<_("LYX_DIR_14x environment variable no good.")
-                              << '\n'
-                              << _("System directory set to: ")
-                              << system_lyxdir << endl;
-                       path_shown = true;
-               }
-       }
-
-       // Warn the user if we couldn't find "chkconfig.ltx"
-       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 "
-                          "LyX system directory ")
-                      << _("containing the file `chkconfig.ltx'.") << endl;
-               if (!path_shown) {
-                       FileInfo fi(LYX_DIR);
-                       if (!fi.exist()) {
-                               lyxerr << "Couldn't even find the default LYX_DIR." << endl
-                                       << "Giving up." << endl;
-                               exit(1);
-                       }
-                       lyxerr << bformat(_("Using built-in default %1$s but expect problems."),
-                               LYX_DIR) << endl;
-               } else {
-                       lyxerr << _("Expect problems.") << endl;
-               }
-               system_lyxdir = LYX_DIR;
-               path_shown = true;
-       }
-
-       if (!path_shown)
-               lyxerr[Debug::INIT] << "System directory: '"
-                                   << system_lyxdir << '\'' << endl;
-
-       //
-       // Determine user lyx-dir
-       //
-
-       // Directories are searched in this order:
-       // 1) -userdir command line parameter
-       // 2) LYX_USERDIR_14x environment variable
-       // 3) $HOME/.<name of binary>
-
-       // If we had a command line switch, user_lyxdir is already set
-       bool explicit_userdir = true;
-       if (user_lyxdir.empty()) {
-
-               // LYX_USERDIR_14x environment variable
-               user_lyxdir = GetEnvPath("LYX_USERDIR_14x");
-
-               // default behaviour
-               if (user_lyxdir.empty())
-                       user_lyxdir = AddPath(GetEnvPath("HOME"),
-                                                       string(".") + PACKAGE);
-                       explicit_userdir = false;
-       }
-
-       lyxerr[Debug::INIT] << "User LyX directory: '"
-                           <<  user_lyxdir << '\'' << endl;
+       bool const explicit_userdir = setLyxPaths();
 
        // Check that user LyX directory is ok. We don't do that if
        // running in batch mode.
@@ -432,11 +249,11 @@ void LyX::init(bool gui)
        lyx_gui::use_gui = gui;
 
        if (lyxrc.template_path.empty()) {
-               lyxrc.template_path = AddPath(system_lyxdir, "templates");
+               lyxrc.template_path = AddPath(system_lyxdir(), "templates");
        }
 
        if (lyxrc.lastfiles.empty()) {
-               lyxrc.lastfiles = AddName(user_lyxdir, "lastfiles");
+               lyxrc.lastfiles = AddName(user_lyxdir(), "lastfiles");
        }
 
        if (lyxrc.roman_font_name.empty())
@@ -470,12 +287,15 @@ void LyX::init(bool gui)
        lyxerr[Debug::INIT] << "Reading layouts..." << endl;
        LyXSetStyle();
 
-       // Ensure that we have really read a bind file, so that LyX is
-       // usable.
-       lyxrc.readBindFileIfNeeded();
+       if (gui) {
+               // Set up bindings
+               toplevel_keymap.reset(new kb_keymap);
+               defaultKeyBindings(toplevel_keymap.get());
+               toplevel_keymap->read(lyxrc.bind_file);
 
-       // Read menus
-       readUIFile(lyxrc.ui_file);
+               // Read menus
+               readUIFile(lyxrc.ui_file);
+       }
 
        if (lyxerr.debugging(Debug::LYXRC))
                lyxrc.print();
@@ -592,19 +412,19 @@ void LyX::deadKeyBindings(kb_keymap * kbmap)
 
 void LyX::queryUserLyXDir(bool explicit_userdir)
 {
-       string const configure_script = AddName(system_lyxdir, "configure");
+       string const configure_script = AddName(system_lyxdir(), "configure");
 
        // Does user directory exist?
-       FileInfo fileInfo(user_lyxdir);
+       FileInfo fileInfo(user_lyxdir());
        if (fileInfo.isOK() && fileInfo.isDir()) {
                first_start = false;
                FileInfo script(configure_script);
-               FileInfo defaults(AddName(user_lyxdir, "lyxrc.defaults"));
+               FileInfo defaults(AddName(user_lyxdir(), "lyxrc.defaults"));
                if (defaults.isOK() && script.isOK()
                    && defaults.getModificationTime() < script.getModificationTime()) {
                        lyxerr << _("LyX: reconfiguring user directory")
                               << endl;
-                       Path p(user_lyxdir);
+                       Path p(user_lyxdir());
                        ::system(configure_script.c_str());
                        lyxerr << "LyX: " << _("Done!") << endl;
                }
@@ -614,18 +434,18 @@ void LyX::queryUserLyXDir(bool explicit_userdir)
        first_start = !explicit_userdir;
 
        lyxerr << bformat(_("LyX: Creating directory %1$s"
-                                 " and running configure..."), user_lyxdir) << endl;
+                                 " and running configure..."), user_lyxdir()) << endl;
 
-       if (!createDirectory(user_lyxdir, 0755)) {
+       if (!createDirectory(user_lyxdir(), 0755)) {
                // Failed, let's use $HOME instead.
-               user_lyxdir = GetEnvPath("HOME");
+               user_lyxdir(GetEnvPath("HOME"));
                lyxerr << bformat(_("Failed. Will use %1$s instead."),
-                       user_lyxdir) << endl;
+                       user_lyxdir()) << endl;
                return;
        }
 
        // Run configure in user lyx directory
-       Path p(user_lyxdir);
+       Path p(user_lyxdir());
        ::system(configure_script.c_str());
        lyxerr << "LyX: " << _("Done!") << endl;
 }
@@ -826,7 +646,7 @@ int parse_sysdir(string const & arg, string const &)
                lyxerr << _("Missing directory for -sysdir switch") << endl;
                exit(1);
        }
-       system_lyxdir = arg;
+       system_lyxdir(arg);
        return 1;
 }
 
@@ -836,7 +656,7 @@ int parse_userdir(string const & arg, string const &)
                lyxerr << _("Missing directory for -userdir switch") << endl;
                exit(1);
        }
-       user_lyxdir = arg;
+       user_lyxdir(arg);
        return 1;
 }