1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
19 #include "lyx_gui_misc.h"
21 #include "support/path.h"
22 #include "support/filetools.h"
23 #include "bufferlist.h"
25 #include "support/FileInfo.h"
26 #include "lastfiles.h"
28 #include "lyxserver.h"
35 extern void LoadLyXFile(string const &);
39 string system_tempdir;
40 string user_lyxdir; // Default $HOME/.lyx
42 // Should this be kept global? Asger says Yes.
45 LastFiles * lastfiles;
47 // This is the global bufferlist object
48 BufferList bufferlist;
50 LyXServer * lyxserver = 0;
51 // this should be static, but I need it in buffer.C
52 bool finished = false; // flag, that we are quitting the program
54 // convenient to have it here.
55 kb_keymap * toplevel_keymap;
58 LyX::LyX(int * argc, char * argv[])
60 // Prevent crash with --help
64 // Here we need to parse the command line. At least
65 // we need to parse for "-dbg" and "-help"
66 bool gui = easyParse(argc, argv);
68 // Global bindings (this must be done as early as possible.) (Lgb)
69 toplevel_keymap = new kb_keymap;
70 // Fill the toplevel_keymap with some defaults
71 for (LyXRC::Bindings::const_iterator cit = lyxrc.bindings.begin();
72 cit != lyxrc.bindings.end(); ++cit) {
73 toplevel_keymap->bind((*cit).first.c_str(), (*cit).second);
76 // Make the GUI object, and let it take care of the
77 // command line arguments that concerns it.
78 lyxerr[Debug::INIT] << "Initializing LyXGUI..." << endl;
79 lyxGUI = new LyXGUI(this, argc, argv, gui);
80 lyxerr[Debug::INIT] << "Initializing LyXGUI...done" << endl;
82 // Initialization of LyX (reads lyxrc and more)
83 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
84 init(argc, argv, gui);
85 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
89 // Load the files specified in the command line.
90 // Now the GUI and LyX have taken care of their arguments, so
91 // the only thing left on the command line should be
94 lyxerr[Debug::INFO] << "Opening document..." << endl;
96 lyxerr[Debug::INFO] << "Opening documents..." << endl;
98 Buffer * last_loaded = 0;
100 for (int argi = (*argc) - 1; argi >= 1; --argi) {
101 Buffer * loadb = bufferlist.loadLyXFile(argv[argi]);
108 string splash = i18nLibFileSearch("examples", "splash.lyx");
109 lyxerr.debug() << "Opening splash document "
110 << splash << "..." << endl;
111 Buffer * loadb = bufferlist.loadLyXFile(splash);
117 if (last_loaded != 0) {
118 lyxerr.debug() << "Yes we loaded some files." << endl;
120 lyxGUI->regBuf(last_loaded);
123 // Execute batch commands if available
124 if (!batch_command.empty() && last_loaded) {
125 lyxerr << "About to handle -x '" << batch_command << "'" << endl;
126 //Buffer buffer("Script Buffer");
127 //buffer.Dispatch(batch_command);
128 last_loaded->Dispatch(batch_command);
129 lyxerr << "We are done!" << endl;
130 return; // Maybe we could do something more clever than aborting..
133 // Let the ball begin...
138 // A destructor is always necessary (asierra-970604)
146 extern "C" void error_handler(int err_sig);
148 void LyX::init(int */*argc*/, char **argv, bool gui)
150 // Install the signal handlers
151 signal(SIGHUP, error_handler);
152 signal(SIGFPE, error_handler);
153 signal(SIGSEGV, error_handler);
154 signal(SIGINT, error_handler);
155 signal(SIGTERM, error_handler);
158 // Determine path of binary
162 string binpath = subst(argv[0], '\\', '/');
163 string binname = OnlyFilename(argv[0]);
164 // Sorry for system specific code. (SMiyata)
165 if (suffixIs(binname, ".exe"))
166 binname.erase(binname.length()-4, string::npos);
168 binpath = ExpandPath(binpath); // This expands ./ and ~/
170 if (!AbsolutePath(binpath)) {
171 string binsearchpath = GetEnvPath("PATH");
172 // This will make "src/lyx" work always :-)
173 binsearchpath += ";.";
174 binpath = FileOpenSearch(binsearchpath, argv[0]);
177 fullbinpath = binpath;
178 binpath = MakeAbsPath(OnlyPath(binpath));
180 // In case we are running in place and compiled with shared libraries
181 if (suffixIs(binpath, "/.libs/"))
182 binpath.erase(binpath.length()-6, string::npos);
184 if (binpath.empty()) {
185 lyxerr << _("Warning: could not determine path of binary.")
187 << _("If you have problems, try starting LyX with an absolute path.")
190 lyxerr[Debug::INIT] << "Path of binary: " << binpath << endl;
193 // Determine system directory.
196 // Directories are searched in this order:
197 // 1) -sysdir command line parameter
198 // 2) LYX_DIR_11x environment variable
199 // 3) Maybe <path of binary>/TOP_SRCDIR/lib
200 // 4) <path of binary>/../share/<name of binary>/
201 // 4a) repeat 4 after following the Symlink if <path of
202 // binary> is a symbolic link.
203 // 5) hardcoded lyx_dir
204 // The directory is checked for the presence of the file
205 // "chkconfig.ltx", and if that is present, the directory
206 // is accepted as the system directory.
207 // If no chkconfig.ltx file is found, a warning is given,
208 // and the hardcoded lyx_dir is used.
210 // If we had a command line switch, system_lyxdir is already set
212 if (!system_lyxdir.empty())
213 searchpath= MakeAbsPath(system_lyxdir) + ';';
215 // LYX_DIR_11x environment variable
216 string lyxdir = GetEnvPath("LYX_DIR_11x");
218 if (!lyxdir.empty()) {
219 lyxerr[Debug::INIT] << "LYX_DIR_11x: " << lyxdir << endl;
220 searchpath += lyxdir + ';';
223 // <path of binary>/TOP_SRCDIR/lib
224 build_lyxdir = MakeAbsPath("../lib", binpath);
225 if (!FileSearch(build_lyxdir, "lyxrc.defaults").empty()) {
226 searchpath += MakeAbsPath(AddPath(TOP_SRCDIR, "lib"),
228 lyxerr[Debug::INIT] << "Checking whether LyX is run in "
229 "place... yes" << endl;
232 << "Checking whether LyX is run in place... no"
234 build_lyxdir.clear();
239 // Path of binary/../share/name of binary/
240 searchpath += NormalizePath(AddPath(binpath, "../share/") +
241 OnlyFilename(binname)) + ';';
244 FileInfo file(fullbinpath, true);
245 FollowLink = file.isLink();
248 if (LyXReadLink(fullbinpath, Link)) {
250 binpath = MakeAbsPath(OnlyPath(fullbinpath));
256 } while (FollowLink);
259 searchpath += LYX_DIR;
261 // If debugging, show complete search path
262 lyxerr[Debug::INIT] << "System directory search path: "
263 << searchpath << endl;
265 string const filename = "chkconfig.ltx";
266 string temp = FileOpenSearch(searchpath, filename, string());
267 system_lyxdir = OnlyPath(temp);
269 // Reduce "path/../path" stuff out of system directory
270 system_lyxdir = NormalizePath(system_lyxdir);
272 bool path_shown = false;
274 // Warn if environment variable is set, but unusable
275 if (!lyxdir.empty()) {
276 if (system_lyxdir != NormalizePath(lyxdir)) {
277 lyxerr <<_("LYX_DIR_11x environment variable no good.")
279 << _("System directory set to: ")
280 << system_lyxdir << endl;
285 // Warn the user if we couldn't find "chkconfig.ltx"
286 if (system_lyxdir == "./") {
287 lyxerr <<_("LyX Warning! Couldn't determine system directory.")
288 <<_("Try the '-sysdir' command line parameter or")
289 <<_("set the environment variable LYX_DIR_11x to the "
290 "LyX system directory")
291 << _("containing the file `chkconfig.ltx'.") << endl;
293 lyxerr << _("Using built-in default ")
294 << LYX_DIR << _(" but expect problems.")
297 lyxerr << _("Expect problems.") << endl;
298 system_lyxdir = LYX_DIR;
302 // Report the system directory if debugging is on
304 lyxerr[Debug::INIT] << "System directory: '"
305 << system_lyxdir << '\'' << endl;
308 // Determine user lyx-dir
311 user_lyxdir = AddPath(GetEnvPath("HOME"), string(".") + PACKAGE);
312 lyxerr[Debug::INIT] << "User LyX directory: '"
313 << user_lyxdir << '\'' << endl;
315 // Check that user LyX directory is ok.
319 // Load the layouts first
322 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
326 // Shine up lyxrc defaults
329 // Default template path: system_dir/templates
330 if (lyxrc.template_path.empty()){
331 lyxrc.template_path = AddPath(system_lyxdir, "templates");
334 // Default lastfiles file: $HOME/.lyx/lastfiles
335 if (lyxrc.lastfiles.empty()){
336 lyxrc.lastfiles = AddName(user_lyxdir, "lastfiles");
339 // Calculate screen dpi as average of x-DPI and y-DPI:
340 // Disable gui when either lyxrc or easyparse says so
342 lyxrc.use_gui = false;
344 // Calculate screen dpi as average of x-DPI and y-DPI:
346 Screen * scr = DefaultScreenOfDisplay(fl_get_display());
347 lyxrc.dpi = ((HeightOfScreen(scr)* 25.4 / HeightMMOfScreen(scr)) +
348 (WidthOfScreen(scr)* 25.4 / WidthMMOfScreen(scr))) / 2;
349 lyxerr[Debug::INFO] << "DPI setting detected to be "
350 << lyxrc.dpi + 0.5 << endl;
352 lyxrc.dpi = 1; // I hope this is safe
356 // Read configuration files
359 ReadRcFile("lyxrc.defaults");
362 // Ensure that we have really read a bind file, so that LyX is
364 if (!lyxrc.hasBindFile)
365 lyxrc.ReadBindFile();
367 if (lyxerr.debugging(Debug::LYXRC)) {
371 // Create temp directory
372 system_tempdir = CreateLyXTmpDir(lyxrc.tempdir_path);
373 if (lyxerr.debugging(Debug::INIT)) {
374 lyxerr << "LyX tmp dir: `" << system_tempdir << '\'' << endl;
377 // load the lastfiles mini-database
378 lyxerr[Debug::INIT] << "Reading lastfiles `"
379 << lyxrc.lastfiles << "'..." << endl;
380 lastfiles = new LastFiles(lyxrc.lastfiles,
381 lyxrc.check_lastfiles,
382 lyxrc.num_lastfiles);
384 // start up the lyxserver. (is this a bit early?) (Lgb)
385 // 0.12 this will be way to early, we need the GUI to be initialized
386 // first, so move it for now.
387 // lyxserver = new LyXServer;
391 // This one is not allowed to use anything on the main form, since that
392 // one does not exist yet. (Asger)
393 void LyX::queryUserLyXDir()
395 // Does user directory exist?
396 FileInfo fileInfo(user_lyxdir);
397 if (fileInfo.isOK() && fileInfo.isDir()) {
405 if (!AskQuestion(_("You don't have a personal LyX directory."),
406 _("It is needed to keep your own configuration."),
407 _("Should I try to set it up for you (recommended)?"))) {
408 lyxerr << _("Running without personal LyX directory.") << endl;
409 // No, let's use $HOME instead.
410 user_lyxdir = GetEnvPath("HOME");
414 // Tell the user what is going on
415 lyxerr << _("LyX: Creating directory ") << user_lyxdir
416 << _(" and running configure...") << endl;
418 // Create directory structure
419 if (!createDirectory(user_lyxdir, 0755)) {
420 // Failed, let's use $HOME instead.
421 user_lyxdir = GetEnvPath("HOME");
422 lyxerr << _("Failed. Will use ") << user_lyxdir
423 << _(" instead.") << endl;
427 // Run configure in user lyx directory
429 system(AddName(system_lyxdir, "configure").c_str());
430 lyxerr << "LyX: " << _("Done!") << endl;
434 // Read the rc file `name'
435 void LyX::ReadRcFile(string const & name)
437 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
439 string lyxrc_path = LibFileSearch(string(), name);
440 if (!lyxrc_path.empty()){
441 lyxerr[Debug::INIT] << "Found " << name
442 << " in " << lyxrc_path << endl;
443 if (lyxrc.read(lyxrc_path) < 0) {
444 WriteAlert(_("LyX Warning!"),
445 _("Error while reading ")+lyxrc_path+".",
446 _("Using built-in defaults."));
449 lyxerr[Debug::INIT] << "Could not find " << name << endl;
453 // Set debugging level and report result to user
454 void setDebuggingLevel(string const & dbgLevel)
456 lyxerr << _("Setting debug level to ") << dbgLevel << endl;
457 lyxerr.level(Debug::value(dbgLevel));
458 Debug::showLevel(lyxerr, lyxerr.level());
462 // Give command line help
463 void commandLineHelp()
465 lyxerr << "LyX " LYX_VERSION << " of " LYX_RELEASE << endl;
467 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
468 "Command line switches (case sensitive):\n"
469 "\t-help summarize LyX usage\n"
470 "\t-sysdir x try to set system directory to x\n"
471 "\t-width x set the width of the main window\n"
472 "\t-height y set the height of the main window\n"
473 "\t-xpos x set the x position of the main window\n"
474 "\t-ypos y set the y position of the main window\n"
475 "\t-dbg feature[,feature]...\n"
476 " select the features to debug.\n"
477 " Type `lyx -dbg' to see the list of features\n"
478 "Check the LyX man page for more options.") << endl;
482 bool LyX::easyParse(int * argc, char * argv[])
485 for(int i = 1; i < *argc; ++i) {
486 string arg = argv[i];
488 // Check for -dbg int
491 setDebuggingLevel(argv[i + 1]);
493 // Now, remove these two arguments by shifting
494 // the following two places down.
496 for (int j = i; j < (*argc); ++j)
497 argv[j] = argv[j + 2];
498 --i; // After shift, check this number again.
500 lyxerr << _("List of supported debug flags:")
502 Debug::showTags(lyxerr);
506 // Check for "-sysdir"
507 else if (arg == "-sysdir") {
509 system_lyxdir = argv[i + 1];
511 // Now, remove these two arguments by shifting
512 // the following two places down.
514 for (int j= i; j < (*argc); ++j)
515 argv[j] = argv[j + 2];
516 --i; // After shift, check this number again.
518 lyxerr << _("Missing directory for -sysdir switch!")
520 // Check for --help or -help
521 } else if (arg == "--help" || arg == "-help") {
525 // Check for "-nw": No XWindows as for emacs this should
526 // give a LyX that could be used in a terminal window.
527 //else if (arg == "-nw") {
531 // Check for "-x": Execute commands
532 else if (arg == "-x" || arg == "--execute") {
534 batch_command = string(argv[i + 1]);
536 // Now, remove these two arguments by shifting
537 // the following two places down.
539 for (int j = i; j < (*argc); ++j)
540 argv[j] = argv[j + 2];
541 --i; // After shift, check this number again.
545 lyxerr << _("Missing command string after -x switch!") << endl;
547 // Argh. Setting gui to false segfaults..
551 else if (arg == "-e" || arg == "--export") {
553 string type(argv[i+1]);
556 for (int j = i; j < (*argc); ++j)
557 argv[j] = argv[j + 2];
558 --i; // After shift, check this number again.
562 else if (type == "ps")
564 else if (type == "text" || type == "txt")
567 if (type == "latex" || type == "postscript"
568 || type == "ascii" || type == "html")
569 batch_command = "buffer-export " + type;
571 lyxerr << _("Unknown file type '")
572 << type << _("' after ")
573 << arg << _(" switch!") << endl;
575 lyxerr << _("Missing file type [eg latex, "
577 << arg << _(" switch!") << endl;
584 void error_handler(int err_sig)
588 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
594 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
597 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
599 "Sorry, you have found a bug in LyX."
600 " If possible, please read 'Known bugs'\n"
601 "under the Help menu and then send us "
602 "a full bug report. Thanks!" << endl;
609 // Deinstall the signal handlers
610 signal(SIGHUP, SIG_DFL);
611 signal(SIGINT, SIG_DFL);
612 signal(SIGFPE, SIG_DFL);
613 signal(SIGSEGV, SIG_DFL);
614 signal(SIGTERM, SIG_DFL);
616 bufferlist.emergencyWriteAll();
618 lyxerr << "Bye." << endl;
619 if(err_sig!= SIGHUP && (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))