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 * ====================================================== */
21 #include "lyx_gui_misc.h"
23 #include "support/path.h"
24 #include "support/filetools.h"
25 #include "bufferlist.h"
27 #include "support/FileInfo.h"
28 #include "lastfiles.h"
30 #include "lyxserver.h"
37 extern void LoadLyXFile(string const &);
41 string system_tempdir;
42 string user_lyxdir; // Default $HOME/.lyx
44 // Should this be kept global? Asger says Yes.
47 LastFiles * lastfiles;
49 // This is the global bufferlist object
50 BufferList bufferlist;
52 LyXServer * lyxserver = 0;
53 // this should be static, but I need it in buffer.C
54 bool finished = false; // flag, that we are quitting the program
56 // convenient to have it here.
57 kb_keymap * toplevel_keymap;
60 LyX::LyX(int * argc, char * argv[])
62 // Prevent crash with --help
66 // Here we need to parse the command line. At least
67 // we need to parse for "-dbg" and "-help"
68 bool gui = easyParse(argc, argv);
70 // Global bindings (this must be done as early as possible.) (Lgb)
71 toplevel_keymap = new kb_keymap;
72 defaultKeyBindings(toplevel_keymap);
74 // Make the GUI object, and let it take care of the
75 // command line arguments that concerns it.
76 lyxerr[Debug::INIT] << "Initializing LyXGUI..." << endl;
77 lyxGUI = new LyXGUI(this, argc, argv, gui);
78 lyxerr[Debug::INIT] << "Initializing LyXGUI...done" << endl;
80 // Initialization of LyX (reads lyxrc and more)
81 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
82 init(argc, argv, gui);
83 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
87 // Load the files specified in the command line.
88 // Now the GUI and LyX have taken care of their arguments, so
89 // the only thing left on the command line should be
92 lyxerr[Debug::INFO] << "Opening document..." << endl;
94 lyxerr[Debug::INFO] << "Opening documents..." << endl;
96 Buffer * last_loaded = 0;
98 for (int argi = (*argc) - 1; argi >= 1; --argi) {
99 Buffer * loadb = bufferlist.loadLyXFile(argv[argi]);
106 string splash = i18nLibFileSearch("examples", "splash.lyx");
107 lyxerr.debug() << "Opening splash document "
108 << splash << "..." << endl;
109 Buffer * loadb = bufferlist.loadLyXFile(splash);
115 if (last_loaded != 0) {
116 lyxerr.debug() << "Yes we loaded some files." << endl;
118 lyxGUI->regBuf(last_loaded);
121 // Execute batch commands if available
122 if (!batch_command.empty()) {
123 lyxerr << "About to handle -x '" << batch_command << "'" << endl;
125 // no buffer loaded, create one
127 last_loaded = bufferlist.newFile("tmpfile", string());
129 // try to dispatch to last loaded buffer first
130 bool dispatched = last_loaded->Dispatch(batch_command);
132 // if this was successful, return.
133 // Maybe we could do something more clever than aborting...
135 lyxerr << "We are done!" << endl;
139 // otherwise, let the GUI handle the batch command
140 lyxGUI->regBuf(last_loaded);
141 lyxGUI->getLyXView()->getLyXFunc()->Dispatch(batch_command);
146 // Let the ball begin...
151 // A destructor is always necessary (asierra-970604)
159 extern "C" void error_handler(int err_sig);
161 void LyX::init(int */*argc*/, char **argv, bool gui)
163 // Install the signal handlers
164 std::signal(SIGHUP, error_handler);
165 std::signal(SIGFPE, error_handler);
166 std::signal(SIGSEGV, error_handler);
167 std::signal(SIGINT, error_handler);
168 std::signal(SIGTERM, error_handler);
171 // Determine path of binary
175 string binpath = subst(argv[0], '\\', '/');
176 string binname = OnlyFilename(argv[0]);
177 // Sorry for system specific code. (SMiyata)
178 if (suffixIs(binname, ".exe"))
179 binname.erase(binname.length()-4, string::npos);
181 binpath = ExpandPath(binpath); // This expands ./ and ~/
183 if (!AbsolutePath(binpath)) {
184 string binsearchpath = GetEnvPath("PATH");
185 // This will make "src/lyx" work always :-)
186 binsearchpath += ";.";
187 binpath = FileOpenSearch(binsearchpath, argv[0]);
190 fullbinpath = binpath;
191 binpath = MakeAbsPath(OnlyPath(binpath));
193 // In case we are running in place and compiled with shared libraries
194 if (suffixIs(binpath, "/.libs/"))
195 binpath.erase(binpath.length()-6, string::npos);
197 if (binpath.empty()) {
198 lyxerr << _("Warning: could not determine path of binary.")
200 << _("If you have problems, try starting LyX with an absolute path.")
203 lyxerr[Debug::INIT] << "Path of binary: " << binpath << endl;
206 // Determine system directory.
209 // Directories are searched in this order:
210 // 1) -sysdir command line parameter
211 // 2) LYX_DIR_11x environment variable
212 // 3) Maybe <path of binary>/TOP_SRCDIR/lib
213 // 4) <path of binary>/../share/<name of binary>/
214 // 4a) repeat 4 after following the Symlink if <path of
215 // binary> is a symbolic link.
216 // 5) hardcoded lyx_dir
217 // The directory is checked for the presence of the file
218 // "chkconfig.ltx", and if that is present, the directory
219 // is accepted as the system directory.
220 // If no chkconfig.ltx file is found, a warning is given,
221 // and the hardcoded lyx_dir is used.
223 // If we had a command line switch, system_lyxdir is already set
225 if (!system_lyxdir.empty())
226 searchpath= MakeAbsPath(system_lyxdir) + ';';
228 // LYX_DIR_11x environment variable
229 string lyxdir = GetEnvPath("LYX_DIR_11x");
231 if (!lyxdir.empty()) {
232 lyxerr[Debug::INIT] << "LYX_DIR_11x: " << lyxdir << endl;
233 searchpath += lyxdir + ';';
236 // <path of binary>/TOP_SRCDIR/lib
237 build_lyxdir = MakeAbsPath("../lib", binpath);
238 if (!FileSearch(build_lyxdir, "lyxrc.defaults").empty()) {
239 searchpath += MakeAbsPath(AddPath(TOP_SRCDIR, "lib"),
241 lyxerr[Debug::INIT] << "Checking whether LyX is run in "
242 "place... yes" << endl;
245 << "Checking whether LyX is run in place... no"
247 build_lyxdir.erase();
252 // Path of binary/../share/name of binary/
253 searchpath += NormalizePath(AddPath(binpath, "../share/") +
254 OnlyFilename(binname)) + ';';
257 FileInfo file(fullbinpath, true);
258 FollowLink = file.isLink();
261 if (LyXReadLink(fullbinpath, Link)) {
263 binpath = MakeAbsPath(OnlyPath(fullbinpath));
269 } while (FollowLink);
272 searchpath += LYX_DIR;
274 // If debugging, show complete search path
275 lyxerr[Debug::INIT] << "System directory search path: "
276 << searchpath << endl;
278 string const filename = "chkconfig.ltx";
279 string temp = FileOpenSearch(searchpath, filename, string());
280 system_lyxdir = OnlyPath(temp);
282 // Reduce "path/../path" stuff out of system directory
283 system_lyxdir = NormalizePath(system_lyxdir);
285 bool path_shown = false;
287 // Warn if environment variable is set, but unusable
288 if (!lyxdir.empty()) {
289 if (system_lyxdir != NormalizePath(lyxdir)) {
290 lyxerr <<_("LYX_DIR_11x environment variable no good.")
292 << _("System directory set to: ")
293 << system_lyxdir << endl;
298 // Warn the user if we couldn't find "chkconfig.ltx"
299 if (system_lyxdir == "./") {
300 lyxerr <<_("LyX Warning! Couldn't determine system directory.")
301 <<_("Try the '-sysdir' command line parameter or")
302 <<_("set the environment variable LYX_DIR_11x to the "
303 "LyX system directory")
304 << _("containing the file `chkconfig.ltx'.") << endl;
306 lyxerr << _("Using built-in default ")
307 << LYX_DIR << _(" but expect problems.")
310 lyxerr << _("Expect problems.") << endl;
311 system_lyxdir = LYX_DIR;
315 // Report the system directory if debugging is on
317 lyxerr[Debug::INIT] << "System directory: '"
318 << system_lyxdir << '\'' << endl;
321 // Determine user lyx-dir
324 user_lyxdir = AddPath(GetEnvPath("HOME"), string(".") + PACKAGE);
325 lyxerr[Debug::INIT] << "User LyX directory: '"
326 << user_lyxdir << '\'' << endl;
328 // Check that user LyX directory is ok.
332 // Load the layouts first
335 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
339 // Shine up lyxrc defaults
342 // Default template path: system_dir/templates
343 if (lyxrc.template_path.empty()){
344 lyxrc.template_path = AddPath(system_lyxdir, "templates");
347 // Default lastfiles file: $HOME/.lyx/lastfiles
348 if (lyxrc.lastfiles.empty()){
349 lyxrc.lastfiles = AddName(user_lyxdir, "lastfiles");
352 // Disable gui when either lyxrc or easyparse says so
354 lyxrc.use_gui = false;
356 // Calculate screen dpi as average of x-DPI and y-DPI:
358 lyxrc.dpi = getScreenDPI();
359 lyxerr[Debug::INFO] << "DPI setting detected to be "
360 << lyxrc.dpi + 0.5 << endl;
362 lyxrc.dpi = 1; // I hope this is safe
366 // Read configuration files
369 ReadRcFile("lyxrc.defaults");
372 // Ensure that we have really read a bind file, so that LyX is
374 if (!lyxrc.hasBindFile)
375 lyxrc.ReadBindFile();
378 // Bind the X dead keys to the corresponding LyX functions if
380 if (lyxrc.override_x_deadkeys)
381 deadKeyBindings(toplevel_keymap);
383 if (lyxerr.debugging(Debug::LYXRC)) {
387 // Create temp directory
388 system_tempdir = CreateLyXTmpDir(lyxrc.tempdir_path);
389 if (lyxerr.debugging(Debug::INIT)) {
390 lyxerr << "LyX tmp dir: `" << system_tempdir << '\'' << endl;
393 // load the lastfiles mini-database
394 lyxerr[Debug::INIT] << "Reading lastfiles `"
395 << lyxrc.lastfiles << "'..." << endl;
396 lastfiles = new LastFiles(lyxrc.lastfiles,
397 lyxrc.check_lastfiles,
398 lyxrc.num_lastfiles);
400 // start up the lyxserver. (is this a bit early?) (Lgb)
401 // 0.12 this will be way to early, we need the GUI to be initialized
402 // first, so move it for now.
403 // lyxserver = new LyXServer;
406 // These are the default bindings known to LyX
407 void LyX::defaultKeyBindings(kb_keymap * kbmap)
409 kbmap->bind("Right", LFUN_RIGHT);
410 kbmap->bind("Left", LFUN_LEFT);
411 kbmap->bind("Up", LFUN_UP);
412 kbmap->bind("Down", LFUN_DOWN);
414 kbmap->bind("Tab", LFUN_TAB);
416 kbmap->bind("Home", LFUN_HOME);
417 kbmap->bind("End", LFUN_END);
418 kbmap->bind("Prior", LFUN_PRIOR);
419 kbmap->bind("Next", LFUN_NEXT);
421 kbmap->bind("Return", LFUN_BREAKPARAGRAPH);
422 kbmap->bind("~C-~S-~M-nobreakspace", LFUN_PROTECTEDSPACE);
424 kbmap->bind("Delete", LFUN_DELETE);
425 kbmap->bind("BackSpace", LFUN_BACKSPACE);
427 // kbmap->bindings to enable the use of the numeric keypad
429 kbmap->bind("KP_0", LFUN_SELFINSERT);
430 kbmap->bind("KP_Decimal", LFUN_SELFINSERT);
431 kbmap->bind("KP_Enter", LFUN_SELFINSERT);
432 kbmap->bind("KP_1", LFUN_SELFINSERT);
433 kbmap->bind("KP_2", LFUN_SELFINSERT);
434 kbmap->bind("KP_3", LFUN_SELFINSERT);
435 kbmap->bind("KP_4", LFUN_SELFINSERT);
436 kbmap->bind("KP_5", LFUN_SELFINSERT);
437 kbmap->bind("KP_6", LFUN_SELFINSERT);
438 kbmap->bind("KP_Add", LFUN_SELFINSERT);
439 kbmap->bind("KP_7", LFUN_SELFINSERT);
440 kbmap->bind("KP_8", LFUN_SELFINSERT);
441 kbmap->bind("KP_9", LFUN_SELFINSERT);
442 kbmap->bind("KP_Divide", LFUN_SELFINSERT);
443 kbmap->bind("KP_Multiply", LFUN_SELFINSERT);
444 kbmap->bind("KP_Subtract", LFUN_SELFINSERT);
446 /* Most self-insert keys are handled in the 'default:' section of
447 * WorkAreaKeyPress - so we don't have to define them all.
448 * However keys explicit decleared as self-insert are
449 * handled seperatly (LFUN_SELFINSERT.) Lgb. */
451 kbmap->bind("C-Tab", LFUN_TABINSERT); // ale970515
454 // LyX can optionally take over the handling of deadkeys
455 void LyX::deadKeyBindings(kb_keymap * kbmap)
457 // bindKeyings for transparent handling of deadkeys
458 // The keysyms are gotten from XFree86 X11R6
459 kbmap->bind("~C-~S-~M-dead_acute", LFUN_ACUTE);
460 kbmap->bind("~C-~S-~M-dead_breve", LFUN_BREVE);
461 kbmap->bind("~C-~S-~M-dead_caron", LFUN_CARON);
462 kbmap->bind("~C-~S-~M-dead_cedilla", LFUN_CEDILLA);
463 kbmap->bind("~C-~S-~M-dead_abovering", LFUN_CIRCLE);
464 kbmap->bind("~C-~S-~M-dead_circumflex", LFUN_CIRCUMFLEX);
465 kbmap->bind("~C-~S-~M-dead_abovedot", LFUN_DOT);
466 kbmap->bind("~C-~S-~M-dead_grave", LFUN_GRAVE);
467 kbmap->bind("~C-~S-~M-dead_doubleacute", LFUN_HUNG_UMLAUT);
468 kbmap->bind("~C-~S-~M-dead_macron", LFUN_MACRON);
469 // nothing with this name
470 // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
471 kbmap->bind("~C-~S-~M-dead_tilde", LFUN_TILDE);
472 kbmap->bind("~C-~S-~M-dead_diaeresis", LFUN_UMLAUT);
473 // nothing with this name either...
474 //kbmap->bind("~C-~S-~M-dead_underbar", LFUN_UNDERBAR);
475 kbmap->bind("~C-~S-~M-dead_belowdot", LFUN_UNDERDOT);
476 kbmap->bind("~C-~S-~M-dead_tie", LFUN_TIE);
477 kbmap->bind("~C-~S-~M-dead_ogonek", LFUN_OGONEK);
482 // This one is not allowed to use anything on the main form, since that
483 // one does not exist yet. (Asger)
484 void LyX::queryUserLyXDir()
486 // Does user directory exist?
487 FileInfo fileInfo(user_lyxdir);
488 if (fileInfo.isOK() && fileInfo.isDir()) {
496 if (!AskQuestion(_("You don't have a personal LyX directory."),
497 _("It is needed to keep your own configuration."),
498 _("Should I try to set it up for you (recommended)?"))) {
499 lyxerr << _("Running without personal LyX directory.") << endl;
500 // No, let's use $HOME instead.
501 user_lyxdir = GetEnvPath("HOME");
505 // Tell the user what is going on
506 lyxerr << _("LyX: Creating directory ") << user_lyxdir
507 << _(" and running configure...") << endl;
509 // Create directory structure
510 if (!createDirectory(user_lyxdir, 0755)) {
511 // Failed, let's use $HOME instead.
512 user_lyxdir = GetEnvPath("HOME");
513 lyxerr << _("Failed. Will use ") << user_lyxdir
514 << _(" instead.") << endl;
518 // Run configure in user lyx directory
520 system(AddName(system_lyxdir, "configure").c_str());
521 lyxerr << "LyX: " << _("Done!") << endl;
525 // Read the rc file `name'
526 void LyX::ReadRcFile(string const & name)
528 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
530 string lyxrc_path = LibFileSearch(string(), name);
531 if (!lyxrc_path.empty()){
532 lyxerr[Debug::INIT] << "Found " << name
533 << " in " << lyxrc_path << endl;
534 if (lyxrc.read(lyxrc_path) < 0) {
535 WriteAlert(_("LyX Warning!"),
536 _("Error while reading ")+lyxrc_path+".",
537 _("Using built-in defaults."));
540 lyxerr[Debug::INIT] << "Could not find " << name << endl;
544 // Set debugging level and report result to user
545 void setDebuggingLevel(string const & dbgLevel)
547 lyxerr << _("Setting debug level to ") << dbgLevel << endl;
548 lyxerr.level(Debug::value(dbgLevel));
549 Debug::showLevel(lyxerr, lyxerr.level());
553 // Give command line help
554 void commandLineHelp()
556 lyxerr << "LyX " LYX_VERSION << " of " LYX_RELEASE << endl;
558 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
559 "Command line switches (case sensitive):\n"
560 "\t-help summarize LyX usage\n"
561 "\t-sysdir x try to set system directory to x\n"
562 "\t-width x set the width of the main window\n"
563 "\t-height y set the height of the main window\n"
564 "\t-xpos x set the x position of the main window\n"
565 "\t-ypos y set the y position of the main window\n"
566 "\t-dbg feature[,feature]...\n"
567 " select the features to debug.\n"
568 " Type `lyx -dbg' to see the list of features\n"
569 "Check the LyX man page for more options.") << endl;
573 bool LyX::easyParse(int * argc, char * argv[])
576 for(int i = 1; i < *argc; ++i) {
577 string arg = argv[i];
579 // Check for -dbg int
582 setDebuggingLevel(argv[i + 1]);
584 // Now, remove these two arguments by shifting
585 // the following two places down.
587 for (int j = i; j < (*argc); ++j)
588 argv[j] = argv[j + 2];
589 --i; // After shift, check this number again.
591 lyxerr << _("List of supported debug flags:")
593 Debug::showTags(lyxerr);
597 // Check for "-sysdir"
598 else if (arg == "-sysdir") {
600 system_lyxdir = argv[i + 1];
602 // Now, remove these two arguments by shifting
603 // the following two places down.
605 for (int j= i; j < (*argc); ++j)
606 argv[j] = argv[j + 2];
607 --i; // After shift, check this number again.
609 lyxerr << _("Missing directory for -sysdir switch!")
611 // Check for --help or -help
612 } else if (arg == "--help" || arg == "-help") {
616 // Check for "-nw": No XWindows as for emacs this should
617 // give a LyX that could be used in a terminal window.
618 //else if (arg == "-nw") {
622 // Check for "-x": Execute commands
623 else if (arg == "-x" || arg == "--execute") {
625 batch_command = string(argv[i + 1]);
627 // Now, remove these two arguments by shifting
628 // the following two places down.
630 for (int j = i; j < (*argc); ++j)
631 argv[j] = argv[j + 2];
632 --i; // After shift, check this number again.
636 lyxerr << _("Missing command string after -x switch!") << endl;
638 // Argh. Setting gui to false segfaults..
642 else if (arg == "-e" || arg == "--export") {
644 string type(argv[i+1]);
647 for (int j = i; j < (*argc); ++j)
648 argv[j] = argv[j + 2];
649 --i; // After shift, check this number again.
653 else if (type == "ps")
655 else if (type == "text" || type == "txt")
658 if (type == "latex" || type == "postscript"
659 || type == "ascii" || type == "html")
660 batch_command = "buffer-export " + type;
662 lyxerr << _("Unknown file type '")
663 << type << _("' after ")
664 << arg << _(" switch!") << endl;
666 lyxerr << _("Missing file type [eg latex, "
668 << arg << _(" switch!") << endl;
671 else if (arg == "--import") {
673 string type(argv[i+1]);
674 string file(argv[i+2]);
677 for (int j = i; j < (*argc); ++j)
678 argv[j] = argv[j + 3];
679 --i; // After shift, check this number again.
681 batch_command = "buffer-import " + type + " " + file;
682 lyxerr << "batch_command: "
683 << batch_command << endl;
686 lyxerr << _("Missing type [eg latex, "
688 << arg << _(" switch!") << endl;
697 void error_handler(int err_sig)
701 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
707 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
710 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
712 "Sorry, you have found a bug in LyX."
713 " If possible, please read 'Known bugs'\n"
714 "under the Help menu and then send us "
715 "a full bug report. Thanks!" << endl;
722 // Deinstall the signal handlers
723 signal(SIGHUP, SIG_DFL);
724 signal(SIGINT, SIG_DFL);
725 signal(SIGFPE, SIG_DFL);
726 signal(SIGSEGV, SIG_DFL);
727 signal(SIGTERM, SIG_DFL);
729 bufferlist.emergencyWriteAll();
731 lyxerr << "Bye." << endl;
732 if(err_sig!= SIGHUP && (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))