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 // Now the GUI and LyX have taken care of their arguments, so
81 // the only thing left on the command line should be
82 // filenames. Let's check anyway.
83 for (int argi = 1; argi < *argc ; ++argi) {
84 if (argv[argi][0] == '-') {
85 lyxerr << _("Wrong command line option `")
87 << _("'. Exiting.") << endl;
94 // Initialization of LyX (reads lyxrc and more)
95 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
96 init(argc, argv, gui);
97 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
101 // Load the files specified in the command line.
103 lyxerr[Debug::INFO] << "Opening document..." << endl;
104 else if ((*argc) > 2)
105 lyxerr[Debug::INFO] << "Opening documents..." << endl;
107 Buffer * last_loaded = 0;
109 for (int argi = (*argc) - 1; argi >= 1; --argi) {
110 Buffer * loadb = bufferlist.loadLyXFile(argv[argi]);
117 string splash = i18nLibFileSearch("examples", "splash.lyx");
118 lyxerr[Debug::INIT] << "Opening splash document "
119 << splash << "..." << endl;
120 Buffer * loadb = bufferlist.loadLyXFile(splash);
126 if (last_loaded != 0) {
127 lyxerr[Debug::INIT] << "Yes we loaded some files." << endl;
129 lyxGUI->regBuf(last_loaded);
132 // Execute batch commands if available
133 if (!batch_command.empty()) {
134 lyxerr << "About to handle -x '" << batch_command << "'" << endl;
136 // no buffer loaded, create one
138 last_loaded = bufferlist.newFile("tmpfile", string());
140 // try to dispatch to last loaded buffer first
141 bool dispatched = last_loaded->Dispatch(batch_command);
143 // if this was successful, return.
144 // Maybe we could do something more clever than aborting...
146 lyxerr << "We are done!" << endl;
150 // otherwise, let the GUI handle the batch command
151 lyxGUI->regBuf(last_loaded);
152 lyxGUI->getLyXView()->getLyXFunc()->Dispatch(batch_command);
157 // Let the ball begin...
162 // A destructor is always necessary (asierra-970604)
170 extern "C" void error_handler(int err_sig);
172 void LyX::init(int */*argc*/, char **argv, bool gui)
174 // Install the signal handlers
175 signal(SIGHUP, error_handler);
176 signal(SIGFPE, error_handler);
177 signal(SIGSEGV, error_handler);
178 signal(SIGINT, error_handler);
179 signal(SIGTERM, error_handler);
182 // Determine path of binary
186 string binpath = subst(argv[0], '\\', '/');
187 string binname = OnlyFilename(argv[0]);
188 // Sorry for system specific code. (SMiyata)
189 if (suffixIs(binname, ".exe"))
190 binname.erase(binname.length()-4, string::npos);
192 binpath = ExpandPath(binpath); // This expands ./ and ~/
194 if (!AbsolutePath(binpath)) {
195 string binsearchpath = GetEnvPath("PATH");
196 // This will make "src/lyx" work always :-)
197 binsearchpath += ";.";
198 binpath = FileOpenSearch(binsearchpath, argv[0]);
201 fullbinpath = binpath;
202 binpath = MakeAbsPath(OnlyPath(binpath));
204 // In case we are running in place and compiled with shared libraries
205 if (suffixIs(binpath, "/.libs/"))
206 binpath.erase(binpath.length()-6, string::npos);
208 if (binpath.empty()) {
209 lyxerr << _("Warning: could not determine path of binary.")
211 << _("If you have problems, try starting LyX with an absolute path.")
214 lyxerr[Debug::INIT] << "Path of binary: " << binpath << endl;
217 // Determine system directory.
220 // Directories are searched in this order:
221 // 1) -sysdir command line parameter
222 // 2) LYX_DIR_11x environment variable
223 // 3) Maybe <path of binary>/TOP_SRCDIR/lib
224 // 4) <path of binary>/../share/<name of binary>/
225 // 4a) repeat 4 after following the Symlink if <path of
226 // binary> is a symbolic link.
227 // 5) hardcoded lyx_dir
228 // The directory is checked for the presence of the file
229 // "chkconfig.ltx", and if that is present, the directory
230 // is accepted as the system directory.
231 // If no chkconfig.ltx file is found, a warning is given,
232 // and the hardcoded lyx_dir is used.
234 // If we had a command line switch, system_lyxdir is already set
236 if (!system_lyxdir.empty())
237 searchpath= MakeAbsPath(system_lyxdir) + ';';
239 // LYX_DIR_11x environment variable
240 string lyxdir = GetEnvPath("LYX_DIR_11x");
242 if (!lyxdir.empty()) {
243 lyxerr[Debug::INIT] << "LYX_DIR_11x: " << lyxdir << endl;
244 searchpath += lyxdir + ';';
247 // <path of binary>/TOP_SRCDIR/lib
248 build_lyxdir = MakeAbsPath("../lib", binpath);
249 if (!FileSearch(build_lyxdir, "lyxrc.defaults").empty()) {
250 searchpath += MakeAbsPath(AddPath(TOP_SRCDIR, "lib"),
252 lyxerr[Debug::INIT] << "Checking whether LyX is run in "
253 "place... yes" << endl;
256 << "Checking whether LyX is run in place... no"
258 build_lyxdir.erase();
263 // Path of binary/../share/name of binary/
264 searchpath += NormalizePath(AddPath(binpath, "../share/") +
265 OnlyFilename(binname)) + ';';
268 FileInfo file(fullbinpath, true);
269 FollowLink = file.isLink();
272 if (LyXReadLink(fullbinpath, Link)) {
274 binpath = MakeAbsPath(OnlyPath(fullbinpath));
280 } while (FollowLink);
283 searchpath += LYX_DIR;
285 // If debugging, show complete search path
286 lyxerr[Debug::INIT] << "System directory search path: "
287 << searchpath << endl;
289 string const filename = "chkconfig.ltx";
290 string temp = FileOpenSearch(searchpath, filename, string());
291 system_lyxdir = OnlyPath(temp);
293 // Reduce "path/../path" stuff out of system directory
294 system_lyxdir = NormalizePath(system_lyxdir);
296 bool path_shown = false;
298 // Warn if environment variable is set, but unusable
299 if (!lyxdir.empty()) {
300 if (system_lyxdir != NormalizePath(lyxdir)) {
301 lyxerr <<_("LYX_DIR_11x environment variable no good.")
303 << _("System directory set to: ")
304 << system_lyxdir << endl;
309 // Warn the user if we couldn't find "chkconfig.ltx"
310 if (system_lyxdir == "./") {
311 lyxerr <<_("LyX Warning! Couldn't determine system directory.")
312 <<_("Try the '-sysdir' command line parameter or")
313 <<_("set the environment variable LYX_DIR_11x to the "
314 "LyX system directory")
315 << _("containing the file `chkconfig.ltx'.") << endl;
317 lyxerr << _("Using built-in default ")
318 << LYX_DIR << _(" but expect problems.")
321 lyxerr << _("Expect problems.") << endl;
322 system_lyxdir = LYX_DIR;
326 // Report the system directory if debugging is on
328 lyxerr[Debug::INIT] << "System directory: '"
329 << system_lyxdir << '\'' << endl;
332 // Determine user lyx-dir
335 // Directories are searched in this order:
336 // 1) -userdir command line parameter
337 // 2) LYX_USERDIR_11x environment variable
338 // 3) $HOME/.<name of binary>
340 // If we had a command line switch, user_lyxdir is already set
341 bool explicit_userdir = true;
342 if (user_lyxdir.empty()) {
344 // LYX_USERDIR_11x environment variable
345 user_lyxdir = GetEnvPath("LYX_USERDIR_11x");
348 if (user_lyxdir.empty())
349 user_lyxdir = AddPath(GetEnvPath("HOME"),
350 string(".") + PACKAGE);
351 explicit_userdir = false;
354 lyxerr[Debug::INIT] << "User LyX directory: '"
355 << user_lyxdir << '\'' << endl;
357 // Check that user LyX directory is ok.
358 queryUserLyXDir(explicit_userdir);
361 // Load the layouts first
364 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
368 // Shine up lyxrc defaults
371 // Default template path: system_dir/templates
372 if (lyxrc.template_path.empty()){
373 lyxrc.template_path = AddPath(system_lyxdir, "templates");
376 // Default lastfiles file: $HOME/.lyx/lastfiles
377 if (lyxrc.lastfiles.empty()){
378 lyxrc.lastfiles = AddName(user_lyxdir, "lastfiles");
381 // Disable gui when either lyxrc or easyparse says so
383 lyxrc.use_gui = false;
385 // Calculate screen dpi as average of x-DPI and y-DPI:
387 lyxrc.dpi = getScreenDPI();
388 lyxerr[Debug::INIT] << "DPI setting detected to be "
389 << lyxrc.dpi + 0.5 << endl;
391 lyxrc.dpi = 1; // I hope this is safe
395 // Read configuration files
398 ReadRcFile("lyxrc.defaults");
401 // Ensure that we have really read a bind file, so that LyX is
403 if (!lyxrc.hasBindFile)
404 lyxrc.ReadBindFile();
407 // Bind the X dead keys to the corresponding LyX functions if
409 if (lyxrc.override_x_deadkeys)
410 deadKeyBindings(toplevel_keymap);
412 if (lyxerr.debugging(Debug::LYXRC)) {
416 // Create temp directory
417 system_tempdir = CreateLyXTmpDir(lyxrc.tempdir_path);
418 if (lyxerr.debugging(Debug::INIT)) {
419 lyxerr << "LyX tmp dir: `" << system_tempdir << '\'' << endl;
422 // load the lastfiles mini-database
423 lyxerr[Debug::INIT] << "Reading lastfiles `"
424 << lyxrc.lastfiles << "'..." << endl;
425 lastfiles = new LastFiles(lyxrc.lastfiles,
426 lyxrc.check_lastfiles,
427 lyxrc.num_lastfiles);
429 // start up the lyxserver. (is this a bit early?) (Lgb)
430 // 0.12 this will be way to early, we need the GUI to be initialized
431 // first, so move it for now.
432 // lyxserver = new LyXServer;
435 // These are the default bindings known to LyX
436 void LyX::defaultKeyBindings(kb_keymap * kbmap)
438 kbmap->bind("Right", LFUN_RIGHT);
439 kbmap->bind("Left", LFUN_LEFT);
440 kbmap->bind("Up", LFUN_UP);
441 kbmap->bind("Down", LFUN_DOWN);
443 kbmap->bind("Tab", LFUN_TAB);
445 kbmap->bind("Home", LFUN_HOME);
446 kbmap->bind("End", LFUN_END);
447 kbmap->bind("Prior", LFUN_PRIOR);
448 kbmap->bind("Next", LFUN_NEXT);
450 kbmap->bind("Return", LFUN_BREAKPARAGRAPH);
451 kbmap->bind("~C-~S-~M-nobreakspace", LFUN_PROTECTEDSPACE);
453 kbmap->bind("Delete", LFUN_DELETE);
454 kbmap->bind("BackSpace", LFUN_BACKSPACE);
456 // kbmap->bindings to enable the use of the numeric keypad
458 kbmap->bind("KP_0", LFUN_SELFINSERT);
459 kbmap->bind("KP_Decimal", LFUN_SELFINSERT);
460 kbmap->bind("KP_Enter", LFUN_SELFINSERT);
461 kbmap->bind("KP_1", LFUN_SELFINSERT);
462 kbmap->bind("KP_2", LFUN_SELFINSERT);
463 kbmap->bind("KP_3", LFUN_SELFINSERT);
464 kbmap->bind("KP_4", LFUN_SELFINSERT);
465 kbmap->bind("KP_5", LFUN_SELFINSERT);
466 kbmap->bind("KP_6", LFUN_SELFINSERT);
467 kbmap->bind("KP_Add", LFUN_SELFINSERT);
468 kbmap->bind("KP_7", LFUN_SELFINSERT);
469 kbmap->bind("KP_8", LFUN_SELFINSERT);
470 kbmap->bind("KP_9", LFUN_SELFINSERT);
471 kbmap->bind("KP_Divide", LFUN_SELFINSERT);
472 kbmap->bind("KP_Multiply", LFUN_SELFINSERT);
473 kbmap->bind("KP_Subtract", LFUN_SELFINSERT);
475 /* Most self-insert keys are handled in the 'default:' section of
476 * WorkAreaKeyPress - so we don't have to define them all.
477 * However keys explicit decleared as self-insert are
478 * handled seperatly (LFUN_SELFINSERT.) Lgb. */
480 kbmap->bind("C-Tab", LFUN_TABINSERT); // ale970515
481 kbmap->bind("S-Tab", LFUN_SHIFT_TAB); // jug20000522
484 // LyX can optionally take over the handling of deadkeys
485 void LyX::deadKeyBindings(kb_keymap * kbmap)
487 // bindKeyings for transparent handling of deadkeys
488 // The keysyms are gotten from XFree86 X11R6
489 kbmap->bind("~C-~S-~M-dead_acute", LFUN_ACUTE);
490 kbmap->bind("~C-~S-~M-dead_breve", LFUN_BREVE);
491 kbmap->bind("~C-~S-~M-dead_caron", LFUN_CARON);
492 kbmap->bind("~C-~S-~M-dead_cedilla", LFUN_CEDILLA);
493 kbmap->bind("~C-~S-~M-dead_abovering", LFUN_CIRCLE);
494 kbmap->bind("~C-~S-~M-dead_circumflex", LFUN_CIRCUMFLEX);
495 kbmap->bind("~C-~S-~M-dead_abovedot", LFUN_DOT);
496 kbmap->bind("~C-~S-~M-dead_grave", LFUN_GRAVE);
497 kbmap->bind("~C-~S-~M-dead_doubleacute", LFUN_HUNG_UMLAUT);
498 kbmap->bind("~C-~S-~M-dead_macron", LFUN_MACRON);
499 // nothing with this name
500 // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
501 kbmap->bind("~C-~S-~M-dead_tilde", LFUN_TILDE);
502 kbmap->bind("~C-~S-~M-dead_diaeresis", LFUN_UMLAUT);
503 // nothing with this name either...
504 //kbmap->bind("~C-~S-~M-dead_underbar", LFUN_UNDERBAR);
505 kbmap->bind("~C-~S-~M-dead_belowdot", LFUN_UNDERDOT);
506 kbmap->bind("~C-~S-~M-dead_tie", LFUN_TIE);
507 kbmap->bind("~C-~S-~M-dead_ogonek", LFUN_OGONEK);
512 // This one is not allowed to use anything on the main form, since that
513 // one does not exist yet. (Asger)
514 void LyX::queryUserLyXDir(bool explicit_userdir)
516 // Does user directory exist?
517 FileInfo fileInfo(user_lyxdir);
518 if (fileInfo.isOK() && fileInfo.isDir()) {
526 // Different wording if the user specifically requested a directory
527 if (!AskQuestion( explicit_userdir
528 ? _("You have specified an invalid LyX directory.")
529 : _("You don't have a personal LyX directory.") ,
531 _("It is needed to keep your own configuration."),
532 _("Should I try to set it up for you (recommended)?"))) {
533 lyxerr << _("Running without personal LyX directory.") << endl;
534 // No, let's use $HOME instead.
535 user_lyxdir = GetEnvPath("HOME");
539 // Tell the user what is going on
540 lyxerr << _("LyX: Creating directory ") << user_lyxdir
541 << _(" and running configure...") << endl;
543 // Create directory structure
544 if (!createDirectory(user_lyxdir, 0755)) {
545 // Failed, let's use $HOME instead.
546 user_lyxdir = GetEnvPath("HOME");
547 lyxerr << _("Failed. Will use ") << user_lyxdir
548 << _(" instead.") << endl;
552 // Run configure in user lyx directory
554 system(AddName(system_lyxdir, "configure").c_str());
555 lyxerr << "LyX: " << _("Done!") << endl;
559 // Read the rc file `name'
560 void LyX::ReadRcFile(string const & name)
562 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
564 string lyxrc_path = LibFileSearch(string(), name);
565 if (!lyxrc_path.empty()){
566 lyxerr[Debug::INIT] << "Found " << name
567 << " in " << lyxrc_path << endl;
568 if (lyxrc.read(lyxrc_path) < 0) {
569 WriteAlert(_("LyX Warning!"),
570 _("Error while reading ")+lyxrc_path+".",
571 _("Using built-in defaults."));
574 lyxerr[Debug::INIT] << "Could not find " << name << endl;
578 // Set debugging level and report result to user
579 void setDebuggingLevel(string const & dbgLevel)
581 lyxerr << _("Setting debug level to ") << dbgLevel << endl;
582 lyxerr.level(Debug::value(dbgLevel));
583 Debug::showLevel(lyxerr, lyxerr.level());
587 // Give command line help
588 void commandLineHelp()
590 lyxerr << "LyX " LYX_VERSION << " of " LYX_RELEASE << endl;
592 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
593 "Command line switches (case sensitive):\n"
594 "\t-help summarize LyX usage\n"
595 "\t-userdir dir try to set user directory to dir\n"
596 "\t-sysdir dir try to set system directory to dir\n"
597 "\t-width x set the width of the main window\n"
598 "\t-height y set the height of the main window\n"
599 "\t-xpos x set the x position of the main window\n"
600 "\t-ypos y set the y position of the main window\n"
601 "\t-dbg feature[,feature]...\n"
602 " select the features to debug.\n"
603 " Type `lyx -dbg' to see the list of features\n"
604 "Check the LyX man page for more options.") << endl;
607 bool LyX::easyParse(int * argc, char * argv[])
610 int removeargs = 0; // used when options are read
611 for(int i = 1; i < *argc; ++i) {
612 string arg = argv[i];
614 // Check for -dbg int
617 setDebuggingLevel(argv[i + 1]);
620 lyxerr << _("List of supported debug flags:")
622 Debug::showTags(lyxerr);
626 // Check for "-sysdir"
627 else if (arg == "-sysdir") {
629 system_lyxdir = argv[i + 1];
632 lyxerr << _("Missing directory for -sysdir switch!")
637 // Check for "-userdir"
638 else if (arg == "-userdir") {
640 user_lyxdir = argv[i + 1];
643 lyxerr << _("Missing directory for -userdir switch!")
648 // Check for --help or -help
649 else if (arg == "--help" || arg == "-help") {
653 // Check for "-nw": No XWindows as for emacs this should
654 // give a LyX that could be used in a terminal window.
655 //else if (arg == "-nw") {
659 // Check for "-x": Execute commands
660 else if (arg == "-x" || arg == "--execute") {
662 batch_command = string(argv[i + 1]);
666 lyxerr << _("Missing command string after -x switch!") << endl;
668 // Argh. Setting gui to false segfaults..
672 else if (arg == "-e" || arg == "--export") {
674 string type(argv[i+1]);
679 else if (type == "ps")
681 else if (type == "text" || type == "txt")
684 if (type == "latex" || type == "postscript"
685 || type == "ascii" || type == "html")
686 batch_command = "buffer-export " + type;
688 lyxerr << _("Unknown file type '")
689 << type << _("' after ")
690 << arg << _(" switch!") << endl;
692 lyxerr << _("Missing file type [eg latex, "
694 << arg << _(" switch!") << endl;
696 else if (arg == "--import") {
698 string type(argv[i+1]);
699 string file(argv[i+2]);
702 batch_command = "buffer-import " + type + " " + file;
703 lyxerr << "batch_command: "
704 << batch_command << endl;
707 lyxerr << _("Missing type [eg latex, "
709 << arg << _(" switch!") << endl;
712 if (removeargs > 0) {
713 // Now, remove used arguments by shifting
714 // the following ones removeargs places down.
715 (*argc) -= removeargs;
716 for (int j = i; j < (*argc); ++j)
717 argv[j] = argv[j + removeargs];
718 --i; // After shift, check this number again.
729 void error_handler(int err_sig)
733 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
739 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
742 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
744 "Sorry, you have found a bug in LyX."
745 " If possible, please read 'Known bugs'\n"
746 "under the Help menu and then send us "
747 "a full bug report. Thanks!" << endl;
754 // Deinstall the signal handlers
755 signal(SIGHUP, SIG_DFL);
756 signal(SIGINT, SIG_DFL);
757 signal(SIGFPE, SIG_DFL);
758 signal(SIGSEGV, SIG_DFL);
759 signal(SIGTERM, SIG_DFL);
761 bufferlist.emergencyWriteAll();
763 lyxerr << "Bye." << endl;
764 if(err_sig!= SIGHUP &&
765 (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))