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"
33 extern void LoadLyXFile(string const &);
37 string system_tempdir;
38 string user_lyxdir; // Default $HOME/.lyx
40 // Should this be kept global? Asger says Yes.
43 LastFiles * lastfiles;
46 // This is the global bufferlist object
47 BufferList bufferlist;
49 LyXServer * lyxserver = 0;
50 // this should be static, but I need it in buffer.C
51 bool finished = false; // flag, that we are quitting the program
53 // convenient to have it here.
54 kb_keymap * toplevel_keymap;
57 LyX::LyX(int * argc, char * argv[])
59 // Prevent crash with --help
63 // Here we need to parse the command line. At least
64 // we need to parse for "-dbg" and "-help"
65 bool gui = easyParse(argc, argv);
67 // Global bindings (this must be done as early as possible.) (Lgb)
68 toplevel_keymap = new kb_keymap;
70 lyxerr[Debug::INIT] << "Initializing lyxrc" << endl;
73 // Make the GUI object, and let it take care of the
74 // command line arguments that concerns it.
75 lyxerr[Debug::INIT] << "Initializing LyXGUI..." << endl;
76 lyxGUI = new LyXGUI(this, argc, argv, gui);
77 lyxerr[Debug::INIT] << "Initializing LyXGUI...done" << endl;
79 // Initialization of LyX (reads lyxrc and more)
80 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
82 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
86 // Load the files specified in the command line.
87 // Now the GUI and LyX have taken care of their arguments, so
88 // the only thing left on the command line should be
91 lyxerr[Debug::INFO] << "Opening document..." << endl;
93 lyxerr[Debug::INFO] << "Opening documents..." << endl;
95 Buffer * last_loaded = 0;
97 for (int argi = (*argc) - 1; argi >= 1; --argi) {
98 Buffer * loadb = bufferlist.loadLyXFile(argv[argi]);
105 string splash = i18nLibFileSearch("examples", "splash.lyx");
106 lyxerr.debug() << "Opening splash document "
107 << splash << "..." << endl;
108 Buffer * loadb = bufferlist.loadLyXFile(splash);
114 if (last_loaded != 0) {
115 lyxerr.debug() << "Yes we loaded some files." << endl;
116 lyxGUI->regBuf(last_loaded);
119 // Execute batch commands if available
120 if (!batch_command.empty() && last_loaded) {
121 lyxerr << "About to handle -x '" << batch_command << "'" << endl;
122 //Buffer buffer("Script Buffer");
123 //buffer.Dispatch(batch_command);
124 last_loaded->Dispatch(batch_command);
125 lyxerr << "We are done!" << endl;
126 return; // Maybe we could do something more clever than aborting..
129 // Let the ball begin...
134 // A destructor is always necessary (asierra-970604)
142 extern "C" void error_handler(int err_sig);
144 void LyX::init(int */*argc*/, char **argv)
146 // Install the signal handlers
147 signal(SIGHUP, error_handler);
148 signal(SIGFPE, error_handler);
149 signal(SIGSEGV, error_handler);
150 signal(SIGINT, error_handler);
151 signal(SIGTERM, error_handler);
154 // Determine path of binary
158 string binpath = subst(argv[0], '\\', '/');
159 string binname = OnlyFilename(argv[0]);
160 // Sorry for system specific code. (SMiyata)
161 if (suffixIs(binname, ".exe"))
162 binname.erase(binname.length()-4, string::npos);
164 binpath = ExpandPath(binpath); // This expands ./ and ~/
166 if (!AbsolutePath(binpath)) {
167 string binsearchpath = GetEnvPath("PATH");
168 // This will make "src/lyx" work always :-)
169 binsearchpath += ";.";
170 binpath = FileOpenSearch(binsearchpath, argv[0]);
173 fullbinpath = binpath;
174 binpath = MakeAbsPath(OnlyPath(binpath));
176 // In case we are running in place and compiled with shared libraries
177 if (suffixIs(binpath, "/.libs/"))
178 binpath.erase(binpath.length()-6, string::npos);
180 if (binpath.empty()) {
181 lyxerr << _("Warning: could not determine path of binary.")
183 << _("If you have problems, try starting LyX with an absolute path.")
186 lyxerr[Debug::INIT] << "Path of binary: " << binpath << endl;
189 // Determine system directory.
192 // Directories are searched in this order:
193 // 1) -sysdir command line parameter
194 // 2) LYX_DIR_11x environment variable
195 // 3) Maybe <path of binary>/TOP_SRCDIR/lib
196 // 4) <path of binary>/../share/<name of binary>/
197 // 4a) repeat 4 after following the Symlink if <path of
198 // binary> is a symbolic link.
199 // 5) hardcoded lyx_dir
200 // The directory is checked for the presence of the file
201 // "chkconfig.ltx", and if that is present, the directory
202 // is accepted as the system directory.
203 // If no chkconfig.ltx file is found, a warning is given,
204 // and the hardcoded lyx_dir is used.
206 // If we had a command line switch, system_lyxdir is already set
208 if (!system_lyxdir.empty())
209 searchpath= MakeAbsPath(system_lyxdir) + ';';
211 // LYX_DIR_11x environment variable
212 string lyxdir = GetEnvPath("LYX_DIR_11x");
214 if (!lyxdir.empty()) {
215 lyxerr[Debug::INIT] << "LYX_DIR_11x: " << lyxdir << endl;
216 searchpath += lyxdir + ';';
219 // <path of binary>/TOP_SRCDIR/lib
220 build_lyxdir = MakeAbsPath("../lib", binpath);
221 if (!FileSearch(build_lyxdir, "lyxrc.defaults").empty()) {
222 searchpath += MakeAbsPath(AddPath(TOP_SRCDIR, "lib"),
224 lyxerr[Debug::INIT] << "Checking whether LyX is run in "
225 "place... yes" << endl;
228 << "Checking whether LyX is run in place... no"
230 build_lyxdir.clear();
235 // Path of binary/../share/name of binary/
236 searchpath += NormalizePath(AddPath(binpath, "../share/") +
237 OnlyFilename(binname)) + ';';
240 FileInfo file(fullbinpath, true);
241 FollowLink = file.isLink();
244 if (LyXReadLink(fullbinpath, Link)) {
246 binpath = MakeAbsPath(OnlyPath(fullbinpath));
252 } while (FollowLink);
255 searchpath += LYX_DIR;
257 // If debugging, show complete search path
258 lyxerr[Debug::INIT] << "System directory search path: "
259 << searchpath << endl;
261 string const filename = "chkconfig.ltx";
262 string temp = FileOpenSearch(searchpath, filename, string());
263 system_lyxdir = OnlyPath(temp);
265 // Reduce "path/../path" stuff out of system directory
266 system_lyxdir = NormalizePath(system_lyxdir);
268 bool path_shown = false;
270 // Warn if environment variable is set, but unusable
271 if (!lyxdir.empty()) {
272 if (system_lyxdir != NormalizePath(lyxdir)) {
273 lyxerr <<_("LYX_DIR_11x environment variable no good.")
275 << _("System directory set to: ")
276 << system_lyxdir << endl;
281 // Warn the user if we couldn't find "chkconfig.ltx"
282 if (system_lyxdir == "./") {
283 lyxerr <<_("LyX Warning! Couldn't determine system directory.")
284 <<_("Try the '-sysdir' command line parameter or")
285 <<_("set the environment variable LYX_DIR_11x to the "
286 "LyX system directory")
287 << _("containing the file `chkconfig.ltx'.") << endl;
289 lyxerr << _("Using built-in default ")
290 << LYX_DIR << _(" but expect problems.")
293 lyxerr << _("Expect problems.") << endl;
294 system_lyxdir = LYX_DIR;
298 // Report the system directory if debugging is on
300 lyxerr[Debug::INIT] << "System directory: '"
301 << system_lyxdir << '\'' << endl;
304 // Determine user lyx-dir
307 user_lyxdir = AddPath(GetEnvPath("HOME"), string(".") + PACKAGE);
308 lyxerr[Debug::INIT] << "User LyX directory: '"
309 << user_lyxdir << '\'' << endl;
311 // Check that user LyX directory is ok.
315 // Load the layouts first
318 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
322 // Shine up lyxrc defaults
325 // Default template path: system_dir/templates
326 if (lyxrc->template_path.empty()){
327 lyxrc->template_path = AddPath(system_lyxdir, "templates");
330 // Default lastfiles file: $HOME/.lyx/lastfiles
331 if (lyxrc->lastfiles.empty()){
332 lyxrc->lastfiles = AddName(user_lyxdir, "lastfiles");
335 // Calculate screen dpi as average of x-DPI and y-DPI:
336 Screen * scr = DefaultScreenOfDisplay(fl_get_display());
337 lyxrc->dpi = ((HeightOfScreen(scr)* 25.4 / HeightMMOfScreen(scr)) +
338 (WidthOfScreen(scr)* 25.4 / WidthMMOfScreen(scr))) / 2;
339 lyxerr[Debug::INFO] << "DPI setting detected to be "
340 << lyxrc->dpi + 0.5 << endl;
343 // Read configuration files
346 ReadRcFile("lyxrc.defaults");
349 // Ensure that we have really read a bind file, so that LyX is
351 if (!lyxrc->hasBindFile)
352 lyxrc->ReadBindFile();
354 if (lyxerr.debugging(Debug::LYXRC)) {
358 // Create temp directory
359 system_tempdir = CreateLyXTmpDir(lyxrc->tempdir_path);
360 if (lyxerr.debugging(Debug::INIT)) {
361 lyxerr << "LyX tmp dir: `" << system_tempdir << '\'' << endl;
364 // load the lastfiles mini-database
365 lyxerr[Debug::INIT] << "Reading lastfiles `"
366 << lyxrc->lastfiles << "'..." << endl;
367 lastfiles = new LastFiles(lyxrc->lastfiles,
368 lyxrc->check_lastfiles,
369 lyxrc->num_lastfiles);
371 // start up the lyxserver. (is this a bit early?) (Lgb)
372 // 0.12 this will be way to early, we need the GUI to be initialized
373 // first, so move it for now.
374 // lyxserver = new LyXServer;
378 // This one is not allowed to use anything on the main form, since that
379 // one does not exist yet. (Asger)
380 void LyX::queryUserLyXDir()
382 // Does user directory exist?
383 FileInfo fileInfo(user_lyxdir);
384 if (fileInfo.isOK() && fileInfo.isDir()) {
392 if (!AskQuestion(_("You don't have a personal LyX directory."),
393 _("It is needed to keep your own configuration."),
394 _("Should I try to set it up for you (recommended)?"))) {
395 lyxerr << _("Running without personal LyX directory.") << endl;
396 // No, let's use $HOME instead.
397 user_lyxdir = GetEnvPath("HOME");
401 // Tell the user what is going on
402 lyxerr << _("LyX: Creating directory ") << user_lyxdir
403 << _(" and running configure...") << endl;
405 // Create directory structure
406 if (!createDirectory(user_lyxdir, 0755)) {
407 // Failed, let's use $HOME instead.
408 user_lyxdir = GetEnvPath("HOME");
409 lyxerr << _("Failed. Will use ") << user_lyxdir
410 << _(" instead.") << endl;
414 // Run configure in user lyx directory
416 system(AddName(system_lyxdir, "configure").c_str());
417 lyxerr << "LyX: " << _("Done!") << endl;
421 // Read the rc file `name'
422 void LyX::ReadRcFile(string const & name)
424 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
426 string lyxrc_path = LibFileSearch(string(), name);
427 if (!lyxrc_path.empty()){
428 lyxerr[Debug::INIT] << "Found " << name
429 << " in " << lyxrc_path << endl;
430 if (lyxrc->read(lyxrc_path) < 0) {
431 WriteAlert(_("LyX Warning!"),
432 _("Error while reading ")+lyxrc_path+".",
433 _("Using built-in defaults."));
436 lyxerr[Debug::INIT] << "Could not find " << name << endl;
440 // Set debugging level and report result to user
441 void setDebuggingLevel(string const & dbgLevel)
443 lyxerr << _("Setting debug level to ") << dbgLevel << endl;
444 lyxerr.level(Debug::value(dbgLevel));
445 Debug::showLevel(lyxerr, lyxerr.level());
449 // Give command line help
450 void commandLineHelp()
452 lyxerr << "LyX " LYX_VERSION << " of " LYX_RELEASE << endl;
454 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
455 "Command line switches (case sensitive):\n"
456 "\t-help summarize LyX usage\n"
457 "\t-sysdir x try to set system directory to x\n"
458 "\t-width x set the width of the main window\n"
459 "\t-height y set the height of the main window\n"
460 "\t-xpos x set the x position of the main window\n"
461 "\t-ypos y set the y position of the main window\n"
462 "\t-dbg feature[,feature]...\n"
463 " select the features to debug.\n"
464 " Type `lyx -dbg' to see the list of features\n"
465 "\t-Reverse swaps foreground & background colors\n"
466 "\t-Mono runs LyX in black and white mode\n"
467 "\t-FastSelection use a fast routine for drawing selections\n\n"
468 "Check the LyX man page for more options.") << endl;
472 bool LyX::easyParse(int * argc, char * argv[])
475 for(int i = 1; i < *argc; ++i) {
476 string arg = argv[i];
478 // Check for -dbg int
481 setDebuggingLevel(argv[i + 1]);
483 // Now, remove these two arguments by shifting
484 // the following two places down.
486 for (int j = i; j < (*argc); ++j)
487 argv[j] = argv[j + 2];
488 --i; // After shift, check this number again.
490 lyxerr << _("List of supported debug flags:")
492 Debug::showTags(lyxerr);
497 // Check for "-sysdir"
498 else if (arg == "-sysdir") {
500 system_lyxdir = argv[i + 1];
502 // Now, remove these two arguments by shifting
503 // the following two places down.
505 for (int j= i; j < (*argc); ++j)
506 argv[j] = argv[j + 2];
507 --i; // After shift, check this number again.
509 lyxerr << _("Missing directory for -sysdir switch!")
511 // Check for --help or -help
512 } else if (arg == "--help" || arg == "-help") {
516 // Check for "-nw": No window
517 else if (arg == "-nw") {
521 // Check for "-x": Execute commands
522 else if (arg == "-x" || arg == "--execute") {
524 batch_command = string(argv[i + 1]);
526 // Now, remove these two arguments by shifting
527 // the following two places down.
529 for (int j = i; j < (*argc); ++j)
530 argv[j] = argv[j + 2];
531 --i; // After shift, check this number again.
535 lyxerr << _("Missing command string after -x switch!") << endl;
537 // Argh. Setting gui to false segfaults..
541 else if (arg == "-e" || arg == "--export") {
543 string type(argv[i+1]);
546 for (int j = i; j < (*argc); ++j)
547 argv[j] = argv[j + 2];
548 --i; // After shift, check this number again.
552 else if (type == "ps")
554 else if (type == "text" || type == "txt")
557 if (type == "latex" || type == "postscript"
558 || type == "ascii" || type == "html")
559 batch_command = "buffer-export " + type;
561 lyxerr << _("Unknown file type '")
562 << type << _("' after ")
563 << arg << _(" switch!") << endl;
566 lyxerr << _("Missing file type [eg latex, "
568 << arg << _(" switch!") << endl;
575 void error_handler(int err_sig)
579 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
585 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
588 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
590 "Sorry, you have found a bug in LyX."
591 " If possible, please read 'Known bugs'\n"
592 "under the Help menu and then send us "
593 "a full bug report. Thanks!" << endl;
600 // Deinstall the signal handlers
601 signal(SIGHUP, SIG_DFL);
602 signal(SIGINT, SIG_DFL);
603 signal(SIGFPE, SIG_DFL);
604 signal(SIGSEGV, SIG_DFL);
605 signal(SIGTERM, SIG_DFL);
607 bufferlist.emergencyWriteAll();
609 lyxerr << "Bye." << endl;
610 if(err_sig!= SIGHUP && (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))