1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-1999 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"
32 extern void LoadLyXFile(string const &);
36 string system_tempdir;
37 string user_lyxdir; // Default $HOME/.lyx
39 // Should this be kept global? Asger says Yes.
42 LastFiles * lastfiles;
45 // This is the global bufferlist object
46 BufferList bufferlist;
48 LyXServer * lyxserver = 0;
49 // this should be static, but I need it in buffer.C
50 bool finished = false; // flag, that we are quitting the program
52 // convenient to have it here.
53 kb_keymap * toplevel_keymap;
56 LyX::LyX(int * argc, char * argv[])
58 // Prevent crash with --help
62 // Here we need to parse the command line. At least
63 // we need to parse for "-dbg" and "-help"
64 bool gui = easyParse(argc, argv);
66 // Global bindings (this must be done as early as possible.) (Lgb)
67 toplevel_keymap = new kb_keymap;
69 lyxerr[Debug::INIT] << "Initializing lyxrc" << endl;
72 // Make the GUI object, and let it take care of the
73 // command line arguments that concerns it.
74 lyxerr[Debug::INIT] << "Initializing LyXGUI..." << endl;
75 lyxGUI = new LyXGUI(this, argc, argv, gui);
76 lyxerr[Debug::INIT] << "Initializing LyXGUI...done" << endl;
78 // Initialization of LyX (reads lyxrc and more)
79 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
81 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
85 // Load the files specified in the command line.
86 // Now the GUI and LyX have taken care of their arguments, so
87 // the only thing left on the command line should be
90 lyxerr[Debug::INFO] << "Opening document..." << endl;
92 lyxerr[Debug::INFO] << "Opening documents..." << endl;
94 Buffer * last_loaded = 0;
96 for (int argi = (*argc) - 1; argi >= 1; --argi) {
97 Buffer * loadb = bufferlist.loadLyXFile(argv[argi]);
104 string splash = i18nLibFileSearch("examples", "splash.lyx");
105 lyxerr.debug() << "Opening splash document "
106 << splash << "..." << endl;
107 Buffer * loadb = bufferlist.loadLyXFile(splash);
113 if (last_loaded != 0) {
114 lyxerr.debug() << "Yes we loaded some files." << endl;
115 lyxGUI->regBuf(last_loaded);
118 // Execute batch commands if available
119 if (!batch_command.empty() && last_loaded) {
120 lyxerr << "About to handle -x '" << batch_command << "'" << endl;
121 //Buffer buffer("Script Buffer");
122 //buffer.Dispatch(batch_command);
123 last_loaded->Dispatch(batch_command);
124 lyxerr << "We are done!" << endl;
125 return; // Maybe we could do something more clever than aborting..
128 // Let the ball begin...
133 // A destructor is always necessary (asierra-970604)
144 extern "C" void error_handler(int err_sig);
146 void LyX::init(int */*argc*/, char **argv)
148 // Install the signal handlers
149 signal(SIGHUP, error_handler);
150 signal(SIGFPE, error_handler);
151 signal(SIGSEGV, error_handler);
152 signal(SIGINT, error_handler);
153 signal(SIGTERM, error_handler);
156 // Determine path of binary
160 string binpath = subst(argv[0], '\\', '/');
161 string binname = OnlyFilename(argv[0]);
162 // Sorry for system specific code. (SMiyata)
163 if (suffixIs(binname, ".exe")) binname.erase(binname.length()-4, string::npos);
165 binpath = ExpandPath(binpath); // This expands ./ and ~/
167 if (!AbsolutePath(binpath)) {
168 string binsearchpath = GetEnvPath("PATH");
169 binsearchpath += ";."; // This will make "src/lyx" work always :-)
170 binpath = FileOpenSearch(binsearchpath, argv[0]);
173 fullbinpath = binpath;
174 binpath = MakeAbsPath(OnlyPath(binpath));
176 if (binpath.empty()) {
177 lyxerr << _("Warning: could not determine path of binary.")
179 << _("If you have problems, try starting LyX with an absolute path.")
182 lyxerr[Debug::INIT] << "Path of binary: " << binpath << endl;
185 // Determine system directory.
188 // Directories are searched in this order:
189 // 1) -sysdir command line parameter
190 // 2) LYX_DIR_11x environment variable
191 // 3) Maybe <path of binary>/TOP_SRCDIR/lib
192 // 4) <path of binary>/../share/<name of binary>/
193 // 4a) repeat 4 after following the Symlink if <path of
194 // binary> is a symbolic link.
195 // 5) hardcoded lyx_dir
196 // The directory is checked for the presence of the file
197 // "chkconfig.ltx", and if that is present, the directory
198 // is accepted as the system directory.
199 // If no chkconfig.ltx file is found, a warning is given,
200 // and the hardcoded lyx_dir is used.
202 // If we had a command line switch, system_lyxdir is already set
204 if (!system_lyxdir.empty())
205 searchpath= MakeAbsPath(system_lyxdir) + ';';
207 // LYX_DIR_11x environment variable
208 string lyxdir = GetEnvPath("LYX_DIR_11x");
210 if (!lyxdir.empty()) {
211 lyxerr[Debug::INIT] << "LYX_DIR_11x: " << lyxdir << endl;
212 searchpath += lyxdir + ';';
215 // <path of binary>/TOP_SRCDIR/lib
216 build_lyxdir = MakeAbsPath("../lib", binpath);
217 if (!FileSearch(build_lyxdir, "lyxrc.defaults").empty()) {
218 searchpath += MakeAbsPath(AddPath(TOP_SRCDIR, "lib"),
220 lyxerr[Debug::INIT] << "Checking whether LyX is run in "
221 "place... yes" << endl;
224 << "Checking whether LyX is run in place... no"
226 build_lyxdir.clear();
232 // Path of binary/../share/name of binary/
233 searchpath += NormalizePath(AddPath(binpath, "../share/") +
234 OnlyFilename(binname)) + ';';
237 FileInfo file(fullbinpath, true);
238 FollowLink = file.isLink();
241 if (LyXReadLink(fullbinpath, Link)) {
243 binpath = MakeAbsPath(OnlyPath(fullbinpath));
249 } while (FollowLink);
252 searchpath += LYX_DIR;
254 // If debugging, show complete search path
255 lyxerr[Debug::INIT] << "System directory search path: "
256 << searchpath << endl;
258 string const filename = "chkconfig.ltx";
259 string temp = FileOpenSearch(searchpath, filename, string());
260 system_lyxdir = OnlyPath(temp);
262 // Reduce "path/../path" stuff out of system directory
263 system_lyxdir = NormalizePath(system_lyxdir);
265 bool path_shown = false;
267 // Warn if environment variable is set, but unusable
268 if (!lyxdir.empty()) {
269 if (system_lyxdir != NormalizePath(lyxdir)) {
270 lyxerr <<_("LYX_DIR_11x environment variable no good.")
272 << _("System directory set to: ")
273 << system_lyxdir << endl;
278 // Warn the user if we couldn't find "chkconfig.ltx"
279 if (system_lyxdir.empty()) {
280 lyxerr <<_("LyX Warning! Couldn't determine system directory.")
281 <<_("Try the '-sysdir' command line parameter or")
282 <<_("set the environment variable LYX_DIR_11x to the "
283 "LyX system directory")
284 << _("containing the file `chkconfig.ltx'.") << endl;
286 lyxerr << _("Using built-in default ")
287 << LYX_DIR << _(" but expect problems.")
290 lyxerr << _("Expect problems.") << endl;
291 system_lyxdir = LYX_DIR;
295 // Report the system directory if debugging is on
297 lyxerr[Debug::INIT] << "System directory: '"
298 << system_lyxdir << '\'' << endl;
301 // Determine user lyx-dir
304 user_lyxdir = AddPath(GetEnvPath("HOME"), string(".") + LYX_NAME);
305 lyxerr[Debug::INIT] << "User LyX directory: '"
306 << user_lyxdir << '\'' << endl;
308 // Check that user LyX directory is ok.
312 // Load the layouts first
315 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
319 // Shine up lyxrc defaults
322 // Default template path: system_dir/templates
323 if (lyxrc->template_path.empty()){
324 lyxrc->template_path = AddPath(system_lyxdir, "templates");
327 // Default lastfiles file: $HOME/.lyx/lastfiles
328 if (lyxrc->lastfiles.empty()){
329 lyxrc->lastfiles = AddName(user_lyxdir, "lastfiles");
332 // Calculate screen dpi as average of x-DPI and y-DPI:
333 Screen * scr= (DefaultScreenOfDisplay(fl_get_display()));
334 lyxrc->dpi = ((HeightOfScreen(scr)* 25.4 / HeightMMOfScreen(scr)) +
335 (WidthOfScreen(scr)* 25.4 / WidthMMOfScreen(scr))) / 2;
336 lyxerr[Debug::INFO] << "DPI setting detected to be "
337 << lyxrc->dpi+0.5 << endl;
340 // Read configuration files
343 ReadRcFile("lyxrc.defaults");
346 // Ensure that we have really read a bind file, so that LyX is
348 if (!lyxrc->hasBindFile)
349 lyxrc->ReadBindFile();
351 if (lyxerr.debugging(Debug::LYXRC)) {
355 // Create temp directory
356 system_tempdir = CreateLyXTmpDir(lyxrc->tempdir_path);
357 if (lyxerr.debugging(Debug::INIT)) {
358 lyxerr << "LyX tmp dir: `" << system_tempdir << '\'' << endl;
361 // load the lastfiles mini-database
362 lyxerr[Debug::INIT] << "Reading lastfiles `"
363 << lyxrc->lastfiles << "'..." << endl;
364 lastfiles = new LastFiles(lyxrc->lastfiles,
365 lyxrc->check_lastfiles,
366 lyxrc->num_lastfiles);
368 // start up the lyxserver. (is this a bit early?) (Lgb)
369 // 0.12 this will be way to early, we need the GUI to be initialized
370 // first, so move it for now.
371 // lyxserver = new LyXServer;
375 // This one is not allowed to use anything on the main form, since that
376 // one does not exist yet. (Asger)
377 void LyX::queryUserLyXDir()
379 // Does user directory exist?
380 FileInfo fileInfo(user_lyxdir);
381 if (fileInfo.isOK() && fileInfo.isDir()) {
389 if (!AskQuestion(_("You don't have a personal LyX directory."),
390 _("It is needed to keep your own configuration."),
391 _("Should I try to set it up for you (recommended)?"))) {
392 lyxerr << _("Running without personal LyX directory.") << endl;
393 // No, let's use $HOME instead.
394 user_lyxdir = GetEnvPath("HOME");
398 // Tell the user what is going on
399 lyxerr << _("LyX: Creating directory ") << user_lyxdir
400 << _(" and running configure...") << endl;
402 // Create directory structure
403 if (!createDirectory(user_lyxdir, 0755)) {
404 // Failed, let's use $HOME instead.
405 user_lyxdir = GetEnvPath("HOME");
406 lyxerr << _("Failed. Will use ") << user_lyxdir
407 << _(" instead.") << endl;
411 // Run configure in user lyx directory
413 system(AddName(system_lyxdir, "configure").c_str());
414 lyxerr << "LyX: " << _("Done!") << endl;
418 // Read the rc file `name'
419 void LyX::ReadRcFile(string const & name)
421 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
423 string lyxrc_path = LibFileSearch(string(), name);
424 if (!lyxrc_path.empty()){
425 lyxerr[Debug::INIT] << "Found " << name
426 << " in " << lyxrc_path << endl;
427 if (lyxrc->read(lyxrc_path) < 0) {
428 WriteAlert(_("LyX Warning!"),
429 _("Error while reading ")+lyxrc_path+".",
430 _("Using built-in defaults."));
433 lyxerr[Debug::INIT] << "Could not find " << name << endl;
437 // Set debugging level and report result to user
438 void setDebuggingLevel(string const & dbgLevel)
440 lyxerr << _("Setting debug level to ") << dbgLevel << endl;
441 lyxerr.level(Debug::value(dbgLevel));
442 lyxerr[Debug::INFO] << "Debugging INFO #" << Debug::INFO << endl;
443 lyxerr[Debug::INIT] << "Debugging INIT #" << Debug::INIT << endl;
444 lyxerr[Debug::KEY] << "Debugging KEY #" << Debug::KEY << endl;
445 lyxerr[Debug::TOOLBAR] << "Debugging TOOLBAR #" << Debug::TOOLBAR << endl;
446 lyxerr[Debug::PARSER] << "Debugging LEX and PARSER #" << Debug::PARSER << endl;
447 lyxerr[Debug::LYXRC] << "Debugging LYXRC #" << Debug::LYXRC << endl;
448 lyxerr[Debug::KBMAP] << "Debugging KBMAP #" << Debug::KBMAP << endl;
449 lyxerr[Debug::LATEX] << "Debugging LATEX #" << Debug::LATEX << endl;
450 lyxerr[Debug::MATHED] << "Debugging MATHED #" << Debug::MATHED << endl;
451 lyxerr[Debug::FONT] << "Debugging FONT #" << Debug::FONT << endl;
452 lyxerr[Debug::TCLASS] << "Debugging TCLASS #" << Debug::TCLASS << endl;
453 lyxerr[Debug::LYXVC] << "Debugging LYXVC #" << Debug::LYXVC << endl;
454 lyxerr[Debug::LYXSERVER] << "Debugging LYXSERVER #" << Debug::LYXSERVER << endl;
458 // Give command line help
459 void commandLineHelp()
461 lyxerr << "LyX " LYX_VERSION << " of " LYX_RELEASE << endl;
463 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
464 "Command line switches (case sensitive):\n"
465 "\t-help summarize LyX usage\n"
466 "\t-sysdir x try to set system directory to x\n"
467 "\t-width x set the width of the main window\n"
468 "\t-height y set the height of the main window\n"
469 "\t-xpos x set the x position of the main window\n"
470 "\t-ypos y set the y position of the main window\n"
471 "\t-dbg n where n is a sum of debugging options. Try -dbg 65535 -help\n"
472 "\t-Reverse swaps foreground & background colors\n"
473 "\t-Mono runs LyX in black and white mode\n"
474 "\t-FastSelection use a fast routine for drawing selections\n\n"
475 "Check the LyX man page for more options.") << endl;
479 bool LyX::easyParse(int * argc, char * argv[])
482 for(int i = 1; i < *argc; ++i) {
483 string arg = argv[i];
484 // Check for -dbg int
487 setDebuggingLevel(argv[i + 1]);
489 // Now, remove these two arguments by shifting
490 // the following two places down.
492 for (int j = i; j < (*argc); ++j)
493 argv[j] = argv[j + 2];
494 --i; // After shift, check this number again.
496 lyxerr << _("Missing number for -dbg switch!")
499 // Check for "-sysdir"
500 else if (arg == "-sysdir") {
502 system_lyxdir = argv[i + 1];
504 // Now, remove these two arguments by shifting
505 // the following two places down.
507 for (int j= i; j < (*argc); ++j)
508 argv[j] = argv[j + 2];
509 --i; // After shift, check this number again.
511 lyxerr << _("Missing directory for -sysdir switch!")
513 // Check for --help or -help
514 } else if (arg == "--help" || arg == "-help") {
518 // Check for "-nw": No window
519 else if (arg == "-nw") {
523 // Check for "-x": Execute commands
524 else if (arg == "-x" || arg == "--execute") {
526 batch_command = string(argv[i + 1]);
528 // Now, remove these two arguments by shifting
529 // the following two places down.
531 for (int j = i; j < (*argc); ++j)
532 argv[j] = argv[j + 2];
533 --i; // After shift, check this number again.
537 lyxerr << _("Missing command string after -x switch!") << endl;
539 // Argh. Setting gui to false segfaults..
543 else if (arg == "-e" || arg == "--export") {
545 string type(argv[i+1]);
548 for (int j = i; j < (*argc); ++j)
549 argv[j] = argv[j + 2];
550 --i; // After shift, check this number again.
554 else if (type == "ps")
556 else if (type == "text" || type == "txt")
559 if (type == "latex" || type == "postscript"
560 || type == "ascii" || type == "html")
561 batch_command = "buffer-export " + type;
563 lyxerr << _("Unknown file type '")
564 << type << _("' after ")
565 << arg << _(" switch!") << endl;
568 lyxerr << _("Missing file type [eg latex, "
570 << arg << _(" switch!") << endl;
577 void error_handler(int err_sig)
581 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
587 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
590 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
592 "Sorry, you have found a bug in LyX."
593 " If possible, please read 'Known bugs'\n"
594 "under the Help menu and then send us "
595 "a full bug report. Thanks!" << endl;
602 // Deinstall the signal handlers
603 signal(SIGHUP, SIG_DFL);
604 signal(SIGINT, SIG_DFL);
605 signal(SIGFPE, SIG_DFL);
606 signal(SIGSEGV, SIG_DFL);
607 signal(SIGTERM, SIG_DFL);
609 bufferlist.emergencyWriteAll();
611 lyxerr << "Bye." << endl;
612 if(err_sig!= SIGHUP && (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))