3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
6 * \author Lars Gullik Bjønnes
14 #include "support/filetools.h"
15 #include "support/lyxlib.h"
16 #include "support/FileInfo.h"
17 #include "support/path.h"
18 #include "support/path_defines.h"
23 #include "graphics/GraphicsTypes.h"
25 #include "bufferlist.h"
27 #include "buffer_funcs.h"
28 #include "lyxserver.h"
31 #include "ToolbarBackend.h"
32 #include "MenuBackend.h"
34 #include "lastfiles.h"
36 #include "converter.h"
38 #include "lyxtextclasslist.h"
40 #include "frontends/Alert.h"
41 #include "frontends/lyx_gui.h"
43 #include <boost/function.hpp>
44 #include <boost/bind.hpp>
45 #include <boost/signals/signal1.hpp>
51 using namespace lyx::support;
56 #ifndef CXX_GLOBAL_CSTD
62 extern void LoadLyXFile(string const &);
63 extern void QuitLyX();
65 extern LyXServer * lyxserver;
69 boost::scoped_ptr<LastFiles> lastfiles;
71 // This is the global bufferlist object
72 BufferList bufferlist;
74 // convenient to have it here.
75 boost::scoped_ptr<kb_keymap> toplevel_keymap;
79 void showFileError(string const & error)
81 Alert::warning(_("Could not read configuration file"),
82 bformat(_("Error while reading the configuration file\n%1$s.\n"
83 "Please check your installation."), error));
89 LyX::LyX(int & argc, char * argv[])
91 // Here we need to parse the command line. At least
92 // we need to parse for "-dbg" and "-help"
93 bool const want_gui = easyParse(argc, argv);
95 // set the DisplayTranslator only once; should that be done here??
96 // if this should not be in this file, please also remove
97 // #include "graphics/GraphicsTypes.h" at the top -- Rob Lahaye.
98 lyx::graphics::setDisplayTranslator();
101 lyx_gui::parse_init(argc, argv);
104 // check for any spurious extra arguments
105 // other than documents
106 for (int argi = 1; argi < argc ; ++argi) {
107 if (argv[argi][0] == '-') {
108 lyxerr << bformat(_("Wrong command line option `%1$s'. Exiting."),
114 // Initialization of LyX (reads lyxrc and more)
115 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
117 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
120 lyx_gui::parse_lyxrc();
123 vector<string> files;
125 for (int argi = argc - 1; argi >= 1; --argi) {
126 files.push_back(argv[argi]);
130 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
132 // Execute batch commands if available
133 if (!batch_command.empty()) {
135 lyxerr[Debug::INIT] << "About to handle -x '"
136 << batch_command << '\'' << endl;
138 Buffer * last_loaded = 0;
140 vector<string>::const_iterator it = files.begin();
141 vector<string>::const_iterator end = files.end();
143 for (; it != end; ++it) {
144 // get absolute path of file and add ".lyx" to
145 // the filename if necessary
146 string s = FileSearch(string(), *it, "lyx");
148 last_loaded = newFile(*it, "");
150 last_loaded = bufferlist.newBuffer(s, false);
151 last_loaded->error.connect(boost::bind(&LyX::printError, this, _1));
152 if (!loadLyXFile(last_loaded, s)) {
153 bufferlist.release(last_loaded);
154 last_loaded = newFile(*it, string());
159 // try to dispatch to last loaded buffer first
161 bool success = false;
162 if (last_loaded->dispatch(batch_command, &success)) {
167 files.clear(); // the files are already loaded
170 lyx_gui::start(batch_command, files);
176 static void error_handler(int err_sig)
180 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
186 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
189 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
191 "Sorry, you have found a bug in LyX. "
192 "Please read the bug-reporting instructions "
193 "in Help->Introduction and send us a bug report, "
194 "if necessary. Thanks !" << endl;
201 // Deinstall the signal handlers
202 signal(SIGHUP, SIG_DFL);
203 signal(SIGINT, SIG_DFL);
204 signal(SIGFPE, SIG_DFL);
205 signal(SIGSEGV, SIG_DFL);
206 signal(SIGTERM, SIG_DFL);
208 LyX::emergencyCleanup();
210 lyxerr << "Bye." << endl;
211 if (err_sig!= SIGHUP &&
212 (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))
213 lyx::support::abort();
220 void LyX::printError(ErrorItem const & ei)
222 std::cerr << _("LyX: ") << ei.error
223 << ':' << ei.description << std::endl;
228 void LyX::init(bool gui)
230 signal(SIGHUP, error_handler);
231 signal(SIGFPE, error_handler);
232 signal(SIGSEGV, error_handler);
233 signal(SIGINT, error_handler);
234 signal(SIGTERM, error_handler);
236 bool const explicit_userdir = setLyxPaths();
238 // Check that user LyX directory is ok. We don't do that if
239 // running in batch mode.
241 queryUserLyXDir(explicit_userdir);
246 // Disable gui when easyparse says so
247 lyx_gui::use_gui = gui;
249 if (lyxrc.template_path.empty()) {
250 lyxrc.template_path = AddPath(system_lyxdir(), "templates");
253 if (lyxrc.lastfiles.empty()) {
254 lyxrc.lastfiles = AddName(user_lyxdir(), "lastfiles");
257 if (lyxrc.roman_font_name.empty())
258 lyxrc.roman_font_name = lyx_gui::roman_font_name();
259 if (lyxrc.sans_font_name.empty())
260 lyxrc.sans_font_name = lyx_gui::sans_font_name();
261 if (lyxrc.typewriter_font_name.empty())
262 lyxrc.typewriter_font_name = lyx_gui::typewriter_font_name();
265 // Read configuration files
268 readRcFile("lyxrc.defaults");
269 system_lyxrc = lyxrc;
270 system_formats = formats;
271 system_converters = converters;
272 system_lcolor = lcolor;
274 string prefsfile = "preferences";
275 // back compatibility to lyxs < 1.1.6
276 if (LibFileSearch(string(), prefsfile).empty())
278 if (!LibFileSearch(string(), prefsfile).empty())
279 readRcFile(prefsfile);
281 readEncodingsFile("encodings");
282 readLanguagesFile("languages");
285 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
290 toplevel_keymap.reset(new kb_keymap);
291 defaultKeyBindings(toplevel_keymap.get());
292 toplevel_keymap->read(lyxrc.bind_file);
295 readUIFile(lyxrc.ui_file);
298 if (lyxerr.debugging(Debug::LYXRC))
301 os::setTmpDir(CreateLyXTmpDir(lyxrc.tempdir_path));
302 if (lyxerr.debugging(Debug::INIT)) {
303 lyxerr << "LyX tmp dir: `" << os::getTmpDir() << '\'' << endl;
306 lyxerr[Debug::INIT] << "Reading lastfiles `"
307 << lyxrc.lastfiles << "'..." << endl;
308 lastfiles.reset(new LastFiles(lyxrc.lastfiles,
309 lyxrc.check_lastfiles,
310 lyxrc.num_lastfiles));
314 void LyX::defaultKeyBindings(kb_keymap * kbmap)
316 kbmap->bind("Right", LFUN_RIGHT);
317 kbmap->bind("Left", LFUN_LEFT);
318 kbmap->bind("Up", LFUN_UP);
319 kbmap->bind("Down", LFUN_DOWN);
321 kbmap->bind("Tab", LFUN_CELL_FORWARD);
322 kbmap->bind("ISO_Left_Tab", LFUN_CELL_FORWARD); // jbl 2001-23-02
324 kbmap->bind("Home", LFUN_HOME);
325 kbmap->bind("End", LFUN_END);
326 kbmap->bind("Prior", LFUN_PRIOR);
327 kbmap->bind("Next", LFUN_NEXT);
329 kbmap->bind("Return", LFUN_BREAKPARAGRAPH);
330 //kbmap->bind("~C-~S-~M-nobreakspace", LFUN_PROTECTEDSPACE);
332 kbmap->bind("Delete", LFUN_DELETE);
333 kbmap->bind("BackSpace", LFUN_BACKSPACE);
335 // sub- and superscript -MV
336 kbmap->bind("~S-underscore", LFUN_SUBSCRIPT);
337 kbmap->bind("~S-asciicircum", LFUN_SUPERSCRIPT);
339 // kbmap->bindings to enable the use of the numeric keypad
341 //kbmap->bind("KP_0", LFUN_SELFINSERT);
342 //kbmap->bind("KP_Decimal", LFUN_SELFINSERT);
343 kbmap->bind("KP_Enter", LFUN_BREAKPARAGRAPH);
344 //kbmap->bind("KP_1", LFUN_SELFINSERT);
345 //kbmap->bind("KP_2", LFUN_SELFINSERT);
346 //kbmap->bind("KP_3", LFUN_SELFINSERT);
347 //kbmap->bind("KP_4", LFUN_SELFINSERT);
348 //kbmap->bind("KP_5", LFUN_SELFINSERT);
349 //kbmap->bind("KP_6", LFUN_SELFINSERT);
350 //kbmap->bind("KP_Add", LFUN_SELFINSERT);
351 //kbmap->bind("KP_7", LFUN_SELFINSERT);
352 //kbmap->bind("KP_8", LFUN_SELFINSERT);
353 //kbmap->bind("KP_9", LFUN_SELFINSERT);
354 //kbmap->bind("KP_Divide", LFUN_SELFINSERT);
355 //kbmap->bind("KP_Multiply", LFUN_SELFINSERT);
356 //kbmap->bind("KP_Subtract", LFUN_SELFINSERT);
357 kbmap->bind("KP_Right", LFUN_RIGHT);
358 kbmap->bind("KP_Left", LFUN_LEFT);
359 kbmap->bind("KP_Up", LFUN_UP);
360 kbmap->bind("KP_Down", LFUN_DOWN);
361 kbmap->bind("KP_Home", LFUN_HOME);
362 kbmap->bind("KP_End", LFUN_END);
363 kbmap->bind("KP_Prior", LFUN_PRIOR);
364 kbmap->bind("KP_Next", LFUN_NEXT);
366 kbmap->bind("C-Tab", LFUN_CELL_SPLIT); // ale970515
367 kbmap->bind("S-Tab", LFUN_CELL_BACKWARD); // jug20000522
368 kbmap->bind("S-ISO_Left_Tab", LFUN_CELL_BACKWARD); // jbl 2001-23-02
372 void LyX::emergencyCleanup()
374 // what to do about tmpfiles is non-obvious. we would
375 // like to delete any we find, but our lyxdir might
376 // contain documents etc. which might be helpful on
379 bufferlist.emergencyWriteAll();
381 lyxserver->emergencyCleanup();
385 void LyX::deadKeyBindings(kb_keymap * kbmap)
387 // bindKeyings for transparent handling of deadkeys
388 // The keysyms are gotten from XFree86 X11R6
389 kbmap->bind("~C-~S-~M-dead_acute", LFUN_ACUTE);
390 kbmap->bind("~C-~S-~M-dead_breve", LFUN_BREVE);
391 kbmap->bind("~C-~S-~M-dead_caron", LFUN_CARON);
392 kbmap->bind("~C-~S-~M-dead_cedilla", LFUN_CEDILLA);
393 kbmap->bind("~C-~S-~M-dead_abovering", LFUN_CIRCLE);
394 kbmap->bind("~C-~S-~M-dead_circumflex", LFUN_CIRCUMFLEX);
395 kbmap->bind("~C-~S-~M-dead_abovedot", LFUN_DOT);
396 kbmap->bind("~C-~S-~M-dead_grave", LFUN_GRAVE);
397 kbmap->bind("~C-~S-~M-dead_doubleacute", LFUN_HUNG_UMLAUT);
398 kbmap->bind("~C-~S-~M-dead_macron", LFUN_MACRON);
399 // nothing with this name
400 // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
401 kbmap->bind("~C-~S-~M-dead_tilde", LFUN_TILDE);
402 kbmap->bind("~C-~S-~M-dead_diaeresis", LFUN_UMLAUT);
403 // nothing with this name either...
404 //kbmap->bind("~C-~S-~M-dead_underbar", LFUN_UNDERBAR);
405 kbmap->bind("~C-~S-~M-dead_belowdot", LFUN_UNDERDOT);
406 kbmap->bind("~C-~S-~M-dead_tie", LFUN_TIE);
407 kbmap->bind("~C-~S-~M-dead_ogonek", LFUN_OGONEK);
411 void LyX::queryUserLyXDir(bool explicit_userdir)
413 string const configure_script = AddName(system_lyxdir(), "configure");
415 // Does user directory exist?
416 FileInfo fileInfo(user_lyxdir());
417 if (fileInfo.isOK() && fileInfo.isDir()) {
419 FileInfo script(configure_script);
420 FileInfo defaults(AddName(user_lyxdir(), "lyxrc.defaults"));
421 if (defaults.isOK() && script.isOK()
422 && defaults.getModificationTime() < script.getModificationTime()) {
423 lyxerr << _("LyX: reconfiguring user directory")
425 Path p(user_lyxdir());
426 ::system(configure_script.c_str());
427 lyxerr << "LyX: " << _("Done!") << endl;
432 first_start = !explicit_userdir;
434 lyxerr << bformat(_("LyX: Creating directory %1$s"
435 " and running configure..."), user_lyxdir()) << endl;
437 if (!createDirectory(user_lyxdir(), 0755)) {
438 // Failed, let's use $HOME instead.
439 user_lyxdir(GetEnvPath("HOME"));
440 lyxerr << bformat(_("Failed. Will use %1$s instead."),
441 user_lyxdir()) << endl;
445 // Run configure in user lyx directory
446 Path p(user_lyxdir());
447 ::system(configure_script.c_str());
448 lyxerr << "LyX: " << _("Done!") << endl;
452 void LyX::readRcFile(string const & name)
454 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
456 string const lyxrc_path = LibFileSearch(string(), name);
457 if (!lyxrc_path.empty()) {
459 lyxerr[Debug::INIT] << "Found " << name
460 << " in " << lyxrc_path << endl;
462 if (lyxrc.read(lyxrc_path) >= 0)
470 // Read the ui file `name'
471 void LyX::readUIFile(string const & name)
481 struct keyword_item uitags[ui_last - 1] = {
482 { "include", ui_include },
483 { "menuset", ui_menuset },
484 { "toolbar", ui_toolbar },
485 { "toolbars", ui_toolbars }
488 // Ensure that a file is read only once (prevents include loops)
489 static std::list<string> uifiles;
490 std::list<string>::const_iterator it = uifiles.begin();
491 std::list<string>::const_iterator end = uifiles.end();
492 it = std::find(it, end, name);
494 lyxerr[Debug::INIT] << "UI file '" << name
495 << "' has been read already. "
496 << "Is this an include loop?"
501 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
503 string const ui_path = LibFileSearch("ui", name, "ui");
505 if (ui_path.empty()) {
506 lyxerr[Debug::INIT] << "Could not find " << name << endl;
510 uifiles.push_back(name);
512 lyxerr[Debug::INIT] << "Found " << name
513 << " in " << ui_path << endl;
514 LyXLex lex(uitags, ui_last - 1);
515 lex.setFile(ui_path);
517 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
521 if (lyxerr.debugging(Debug::PARSER))
522 lex.printTable(lyxerr);
528 string const file = lex.getString();
533 menubackend.read(lex);
537 toolbarbackend.read(lex);
541 toolbarbackend.readToolbars(lex);
545 if (!rtrim(lex.getString()).empty())
546 lex.printError("LyX::ReadUIFile: "
547 "Unknown menu tag: `$$Token'");
554 // Read the languages file `name'
555 void LyX::readLanguagesFile(string const & name)
557 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
559 string const lang_path = LibFileSearch(string(), name);
560 if (lang_path.empty()) {
564 languages.read(lang_path);
568 // Read the encodings file `name'
569 void LyX::readEncodingsFile(string const & name)
571 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
573 string const enc_path = LibFileSearch(string(), name);
574 if (enc_path.empty()) {
578 encodings.read(enc_path);
587 /// return the the number of arguments consumed
588 typedef boost::function<int(string const &, string const &)> cmd_helper;
590 int parse_dbg(string const & arg, string const &)
593 lyxerr << _("List of supported debug flags:") << endl;
594 Debug::showTags(lyxerr);
597 lyxerr << bformat(_("Setting debug level to %1$s"), arg) << endl;
599 lyxerr.level(Debug::value(arg));
600 Debug::showLevel(lyxerr, lyxerr.level());
605 int parse_help(string const &, string const &)
608 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
609 "Command line switches (case sensitive):\n"
610 "\t-help summarize LyX usage\n"
611 "\t-userdir dir try to set user directory to dir\n"
612 "\t-sysdir dir try to set system directory to dir\n"
613 "\t-geometry WxH+X+Y set geometry of the main window\n"
614 "\t-dbg feature[,feature]...\n"
615 " select the features to debug.\n"
616 " Type `lyx -dbg' to see the list of features\n"
617 "\t-x [--execute] command\n"
618 " where command is a lyx command.\n"
619 "\t-e [--export] fmt\n"
620 " where fmt is the export format of choice.\n"
621 "\t-i [--import] fmt file.xxx\n"
622 " where fmt is the import format of choice\n"
623 " and file.xxx is the file to be imported.\n"
624 "\t-version summarize version and build info\n"
625 "Check the LyX man page for more details.") << endl;
630 int parse_version(string const &, string const &)
632 lyxerr << "LyX " << lyx_version
633 << " of " << lyx_release_date << endl;
634 lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
636 lyxerr << lyx_version_info << endl;
641 int parse_sysdir(string const & arg, string const &)
644 lyxerr << _("Missing directory for -sysdir switch") << endl;
651 int parse_userdir(string const & arg, string const &)
654 lyxerr << _("Missing directory for -userdir switch") << endl;
661 int parse_execute(string const & arg, string const &)
664 lyxerr << _("Missing command string after --execute switch") << endl;
668 // Argh. Setting gui to false segfaults..
669 // FIXME: when ? how ?
674 int parse_export(string const & type, string const &)
677 lyxerr << _("Missing file type [eg latex, ps...] after "
678 "--export switch") << endl;
681 batch = "buffer-export " + type;
686 int parse_import(string const & type, string const & file)
689 lyxerr << _("Missing file type [eg latex, ps...] after "
690 "--import switch") << endl;
694 lyxerr << _("Missing filename for --import") << endl;
698 batch = "buffer-import " + type + ' ' + file;
705 bool LyX::easyParse(int & argc, char * argv[])
707 std::map<string, cmd_helper> cmdmap;
709 cmdmap["-dbg"] = parse_dbg;
710 cmdmap["-help"] = parse_help;
711 cmdmap["--help"] = parse_help;
712 cmdmap["-version"] = parse_version;
713 cmdmap["--version"] = parse_version;
714 cmdmap["-sysdir"] = parse_sysdir;
715 cmdmap["-userdir"] = parse_userdir;
716 cmdmap["-x"] = parse_execute;
717 cmdmap["--execute"] = parse_execute;
718 cmdmap["-e"] = parse_export;
719 cmdmap["--export"] = parse_export;
720 cmdmap["-i"] = parse_import;
721 cmdmap["--import"] = parse_import;
723 for (int i = 1; i < argc; ++i) {
724 std::map<string, cmd_helper>::const_iterator it
725 = cmdmap.find(argv[i]);
727 // don't complain if not found - may be parsed later
728 if (it == cmdmap.end())
731 string arg((i + 1 < argc) ? argv[i + 1] : "");
732 string arg2((i + 2 < argc) ? argv[i + 2] : "");
734 int const remove = 1 + it->second(arg, arg2);
736 // Now, remove used arguments by shifting
737 // the following ones remove places down.
739 for (int j = i; j < argc; ++j)
740 argv[j] = argv[j + remove];
744 batch_command = batch;