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;
67 extern string system_lyxdir;
68 extern string build_lyxdir;
69 extern string user_lyxdir;
73 boost::scoped_ptr<LastFiles> lastfiles;
75 // This is the global bufferlist object
76 BufferList bufferlist;
78 // convenient to have it here.
79 boost::scoped_ptr<kb_keymap> toplevel_keymap;
83 void showFileError(string const & error)
85 Alert::warning(_("Could not read configuration file"),
86 bformat(_("Error while reading the configuration file\n%1$s.\n"
87 "Please check your installation."), error));
93 LyX::LyX(int & argc, char * argv[])
95 // Here we need to parse the command line. At least
96 // we need to parse for "-dbg" and "-help"
97 bool const want_gui = easyParse(argc, argv);
99 // set the DisplayTranslator only once; should that be done here??
100 // if this should not be in this file, please also remove
101 // #include "graphics/GraphicsTypes.h" at the top -- Rob Lahaye.
102 lyx::graphics::setDisplayTranslator();
105 lyx_gui::parse_init(argc, argv);
108 // check for any spurious extra arguments
109 // other than documents
110 for (int argi = 1; argi < argc ; ++argi) {
111 if (argv[argi][0] == '-') {
112 lyxerr << bformat(_("Wrong command line option `%1$s'. Exiting."),
118 // Initialization of LyX (reads lyxrc and more)
119 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
121 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
124 lyx_gui::parse_lyxrc();
127 vector<string> files;
129 for (int argi = argc - 1; argi >= 1; --argi) {
130 files.push_back(argv[argi]);
134 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
136 // Execute batch commands if available
137 if (!batch_command.empty()) {
139 lyxerr[Debug::INIT] << "About to handle -x '"
140 << batch_command << '\'' << endl;
142 Buffer * last_loaded = 0;
144 vector<string>::const_iterator it = files.begin();
145 vector<string>::const_iterator end = files.end();
147 for (; it != end; ++it) {
148 // get absolute path of file and add ".lyx" to
149 // the filename if necessary
150 string s = FileSearch(string(), *it, "lyx");
152 last_loaded = newFile(*it, "");
154 last_loaded = bufferlist.newBuffer(s, false);
155 last_loaded->error.connect(boost::bind(&LyX::printError, this, _1));
156 if (!loadLyXFile(last_loaded, s)) {
157 bufferlist.release(last_loaded);
158 last_loaded = newFile(*it, string());
163 // try to dispatch to last loaded buffer first
165 bool success = false;
166 if (last_loaded->dispatch(batch_command, &success)) {
171 files.clear(); // the files are already loaded
174 lyx_gui::start(batch_command, files);
180 static void error_handler(int err_sig)
184 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
190 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
193 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
195 "Sorry, you have found a bug in LyX. "
196 "Please read the bug-reporting instructions "
197 "in Help->Introduction and send us a bug report, "
198 "if necessary. Thanks !" << endl;
205 // Deinstall the signal handlers
206 signal(SIGHUP, SIG_DFL);
207 signal(SIGINT, SIG_DFL);
208 signal(SIGFPE, SIG_DFL);
209 signal(SIGSEGV, SIG_DFL);
210 signal(SIGTERM, SIG_DFL);
212 LyX::emergencyCleanup();
214 lyxerr << "Bye." << endl;
215 if (err_sig!= SIGHUP &&
216 (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))
217 lyx::support::abort();
224 void LyX::printError(ErrorItem const & ei)
226 std::cerr << _("LyX: ") << ei.error
227 << ':' << ei.description << std::endl;
232 void LyX::init(bool gui)
234 signal(SIGHUP, error_handler);
235 signal(SIGFPE, error_handler);
236 signal(SIGSEGV, error_handler);
237 signal(SIGINT, error_handler);
238 signal(SIGTERM, error_handler);
240 bool const explicit_userdir = setLyxPaths();
242 // Check that user LyX directory is ok. We don't do that if
243 // running in batch mode.
245 queryUserLyXDir(explicit_userdir);
250 // Disable gui when easyparse says so
251 lyx_gui::use_gui = gui;
253 if (lyxrc.template_path.empty()) {
254 lyxrc.template_path = AddPath(system_lyxdir, "templates");
257 if (lyxrc.lastfiles.empty()) {
258 lyxrc.lastfiles = AddName(user_lyxdir, "lastfiles");
261 if (lyxrc.roman_font_name.empty())
262 lyxrc.roman_font_name = lyx_gui::roman_font_name();
263 if (lyxrc.sans_font_name.empty())
264 lyxrc.sans_font_name = lyx_gui::sans_font_name();
265 if (lyxrc.typewriter_font_name.empty())
266 lyxrc.typewriter_font_name = lyx_gui::typewriter_font_name();
269 // Read configuration files
272 readRcFile("lyxrc.defaults");
273 system_lyxrc = lyxrc;
274 system_formats = formats;
275 system_converters = converters;
276 system_lcolor = lcolor;
278 string prefsfile = "preferences";
279 // back compatibility to lyxs < 1.1.6
280 if (LibFileSearch(string(), prefsfile).empty())
282 if (!LibFileSearch(string(), prefsfile).empty())
283 readRcFile(prefsfile);
285 readEncodingsFile("encodings");
286 readLanguagesFile("languages");
289 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
294 toplevel_keymap.reset(new kb_keymap);
295 defaultKeyBindings(toplevel_keymap.get());
296 toplevel_keymap->read(lyxrc.bind_file);
299 readUIFile(lyxrc.ui_file);
302 if (lyxerr.debugging(Debug::LYXRC))
305 os::setTmpDir(CreateLyXTmpDir(lyxrc.tempdir_path));
306 if (lyxerr.debugging(Debug::INIT)) {
307 lyxerr << "LyX tmp dir: `" << os::getTmpDir() << '\'' << endl;
310 lyxerr[Debug::INIT] << "Reading lastfiles `"
311 << lyxrc.lastfiles << "'..." << endl;
312 lastfiles.reset(new LastFiles(lyxrc.lastfiles,
313 lyxrc.check_lastfiles,
314 lyxrc.num_lastfiles));
318 void LyX::defaultKeyBindings(kb_keymap * kbmap)
320 kbmap->bind("Right", LFUN_RIGHT);
321 kbmap->bind("Left", LFUN_LEFT);
322 kbmap->bind("Up", LFUN_UP);
323 kbmap->bind("Down", LFUN_DOWN);
325 kbmap->bind("Tab", LFUN_CELL_FORWARD);
326 kbmap->bind("ISO_Left_Tab", LFUN_CELL_FORWARD); // jbl 2001-23-02
328 kbmap->bind("Home", LFUN_HOME);
329 kbmap->bind("End", LFUN_END);
330 kbmap->bind("Prior", LFUN_PRIOR);
331 kbmap->bind("Next", LFUN_NEXT);
333 kbmap->bind("Return", LFUN_BREAKPARAGRAPH);
334 //kbmap->bind("~C-~S-~M-nobreakspace", LFUN_PROTECTEDSPACE);
336 kbmap->bind("Delete", LFUN_DELETE);
337 kbmap->bind("BackSpace", LFUN_BACKSPACE);
339 // sub- and superscript -MV
340 kbmap->bind("~S-underscore", LFUN_SUBSCRIPT);
341 kbmap->bind("~S-asciicircum", LFUN_SUPERSCRIPT);
343 // kbmap->bindings to enable the use of the numeric keypad
345 //kbmap->bind("KP_0", LFUN_SELFINSERT);
346 //kbmap->bind("KP_Decimal", LFUN_SELFINSERT);
347 kbmap->bind("KP_Enter", LFUN_BREAKPARAGRAPH);
348 //kbmap->bind("KP_1", LFUN_SELFINSERT);
349 //kbmap->bind("KP_2", LFUN_SELFINSERT);
350 //kbmap->bind("KP_3", LFUN_SELFINSERT);
351 //kbmap->bind("KP_4", LFUN_SELFINSERT);
352 //kbmap->bind("KP_5", LFUN_SELFINSERT);
353 //kbmap->bind("KP_6", LFUN_SELFINSERT);
354 //kbmap->bind("KP_Add", LFUN_SELFINSERT);
355 //kbmap->bind("KP_7", LFUN_SELFINSERT);
356 //kbmap->bind("KP_8", LFUN_SELFINSERT);
357 //kbmap->bind("KP_9", LFUN_SELFINSERT);
358 //kbmap->bind("KP_Divide", LFUN_SELFINSERT);
359 //kbmap->bind("KP_Multiply", LFUN_SELFINSERT);
360 //kbmap->bind("KP_Subtract", LFUN_SELFINSERT);
361 kbmap->bind("KP_Right", LFUN_RIGHT);
362 kbmap->bind("KP_Left", LFUN_LEFT);
363 kbmap->bind("KP_Up", LFUN_UP);
364 kbmap->bind("KP_Down", LFUN_DOWN);
365 kbmap->bind("KP_Home", LFUN_HOME);
366 kbmap->bind("KP_End", LFUN_END);
367 kbmap->bind("KP_Prior", LFUN_PRIOR);
368 kbmap->bind("KP_Next", LFUN_NEXT);
370 kbmap->bind("C-Tab", LFUN_CELL_SPLIT); // ale970515
371 kbmap->bind("S-Tab", LFUN_CELL_BACKWARD); // jug20000522
372 kbmap->bind("S-ISO_Left_Tab", LFUN_CELL_BACKWARD); // jbl 2001-23-02
376 void LyX::emergencyCleanup()
378 // what to do about tmpfiles is non-obvious. we would
379 // like to delete any we find, but our lyxdir might
380 // contain documents etc. which might be helpful on
383 bufferlist.emergencyWriteAll();
385 lyxserver->emergencyCleanup();
389 void LyX::deadKeyBindings(kb_keymap * kbmap)
391 // bindKeyings for transparent handling of deadkeys
392 // The keysyms are gotten from XFree86 X11R6
393 kbmap->bind("~C-~S-~M-dead_acute", LFUN_ACUTE);
394 kbmap->bind("~C-~S-~M-dead_breve", LFUN_BREVE);
395 kbmap->bind("~C-~S-~M-dead_caron", LFUN_CARON);
396 kbmap->bind("~C-~S-~M-dead_cedilla", LFUN_CEDILLA);
397 kbmap->bind("~C-~S-~M-dead_abovering", LFUN_CIRCLE);
398 kbmap->bind("~C-~S-~M-dead_circumflex", LFUN_CIRCUMFLEX);
399 kbmap->bind("~C-~S-~M-dead_abovedot", LFUN_DOT);
400 kbmap->bind("~C-~S-~M-dead_grave", LFUN_GRAVE);
401 kbmap->bind("~C-~S-~M-dead_doubleacute", LFUN_HUNG_UMLAUT);
402 kbmap->bind("~C-~S-~M-dead_macron", LFUN_MACRON);
403 // nothing with this name
404 // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
405 kbmap->bind("~C-~S-~M-dead_tilde", LFUN_TILDE);
406 kbmap->bind("~C-~S-~M-dead_diaeresis", LFUN_UMLAUT);
407 // nothing with this name either...
408 //kbmap->bind("~C-~S-~M-dead_underbar", LFUN_UNDERBAR);
409 kbmap->bind("~C-~S-~M-dead_belowdot", LFUN_UNDERDOT);
410 kbmap->bind("~C-~S-~M-dead_tie", LFUN_TIE);
411 kbmap->bind("~C-~S-~M-dead_ogonek", LFUN_OGONEK);
415 void LyX::queryUserLyXDir(bool explicit_userdir)
417 string const configure_script = AddName(system_lyxdir, "configure");
419 // Does user directory exist?
420 FileInfo fileInfo(user_lyxdir);
421 if (fileInfo.isOK() && fileInfo.isDir()) {
423 FileInfo script(configure_script);
424 FileInfo defaults(AddName(user_lyxdir, "lyxrc.defaults"));
425 if (defaults.isOK() && script.isOK()
426 && defaults.getModificationTime() < script.getModificationTime()) {
427 lyxerr << _("LyX: reconfiguring user directory")
430 ::system(configure_script.c_str());
431 lyxerr << "LyX: " << _("Done!") << endl;
436 first_start = !explicit_userdir;
438 lyxerr << bformat(_("LyX: Creating directory %1$s"
439 " and running configure..."), user_lyxdir) << endl;
441 if (!createDirectory(user_lyxdir, 0755)) {
442 // Failed, let's use $HOME instead.
443 user_lyxdir = GetEnvPath("HOME");
444 lyxerr << bformat(_("Failed. Will use %1$s instead."),
445 user_lyxdir) << endl;
449 // Run configure in user lyx directory
451 ::system(configure_script.c_str());
452 lyxerr << "LyX: " << _("Done!") << endl;
456 void LyX::readRcFile(string const & name)
458 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
460 string const lyxrc_path = LibFileSearch(string(), name);
461 if (!lyxrc_path.empty()) {
463 lyxerr[Debug::INIT] << "Found " << name
464 << " in " << lyxrc_path << endl;
466 if (lyxrc.read(lyxrc_path) >= 0)
474 // Read the ui file `name'
475 void LyX::readUIFile(string const & name)
485 struct keyword_item uitags[ui_last - 1] = {
486 { "include", ui_include },
487 { "menuset", ui_menuset },
488 { "toolbar", ui_toolbar },
489 { "toolbars", ui_toolbars }
492 // Ensure that a file is read only once (prevents include loops)
493 static std::list<string> uifiles;
494 std::list<string>::const_iterator it = uifiles.begin();
495 std::list<string>::const_iterator end = uifiles.end();
496 it = std::find(it, end, name);
498 lyxerr[Debug::INIT] << "UI file '" << name
499 << "' has been read already. "
500 << "Is this an include loop?"
505 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
507 string const ui_path = LibFileSearch("ui", name, "ui");
509 if (ui_path.empty()) {
510 lyxerr[Debug::INIT] << "Could not find " << name << endl;
514 uifiles.push_back(name);
516 lyxerr[Debug::INIT] << "Found " << name
517 << " in " << ui_path << endl;
518 LyXLex lex(uitags, ui_last - 1);
519 lex.setFile(ui_path);
521 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
525 if (lyxerr.debugging(Debug::PARSER))
526 lex.printTable(lyxerr);
532 string const file = lex.getString();
537 menubackend.read(lex);
541 toolbarbackend.read(lex);
545 toolbarbackend.readToolbars(lex);
549 if (!rtrim(lex.getString()).empty())
550 lex.printError("LyX::ReadUIFile: "
551 "Unknown menu tag: `$$Token'");
558 // Read the languages file `name'
559 void LyX::readLanguagesFile(string const & name)
561 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
563 string const lang_path = LibFileSearch(string(), name);
564 if (lang_path.empty()) {
568 languages.read(lang_path);
572 // Read the encodings file `name'
573 void LyX::readEncodingsFile(string const & name)
575 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
577 string const enc_path = LibFileSearch(string(), name);
578 if (enc_path.empty()) {
582 encodings.read(enc_path);
591 /// return the the number of arguments consumed
592 typedef boost::function<int(string const &, string const &)> cmd_helper;
594 int parse_dbg(string const & arg, string const &)
597 lyxerr << _("List of supported debug flags:") << endl;
598 Debug::showTags(lyxerr);
601 lyxerr << bformat(_("Setting debug level to %1$s"), arg) << endl;
603 lyxerr.level(Debug::value(arg));
604 Debug::showLevel(lyxerr, lyxerr.level());
609 int parse_help(string const &, string const &)
612 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
613 "Command line switches (case sensitive):\n"
614 "\t-help summarize LyX usage\n"
615 "\t-userdir dir try to set user directory to dir\n"
616 "\t-sysdir dir try to set system directory to dir\n"
617 "\t-geometry WxH+X+Y set geometry of the main window\n"
618 "\t-dbg feature[,feature]...\n"
619 " select the features to debug.\n"
620 " Type `lyx -dbg' to see the list of features\n"
621 "\t-x [--execute] command\n"
622 " where command is a lyx command.\n"
623 "\t-e [--export] fmt\n"
624 " where fmt is the export format of choice.\n"
625 "\t-i [--import] fmt file.xxx\n"
626 " where fmt is the import format of choice\n"
627 " and file.xxx is the file to be imported.\n"
628 "\t-version summarize version and build info\n"
629 "Check the LyX man page for more details.") << endl;
634 int parse_version(string const &, string const &)
636 lyxerr << "LyX " << lyx_version
637 << " of " << lyx_release_date << endl;
638 lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
640 lyxerr << lyx_version_info << endl;
645 int parse_sysdir(string const & arg, string const &)
648 lyxerr << _("Missing directory for -sysdir switch") << endl;
655 int parse_userdir(string const & arg, string const &)
658 lyxerr << _("Missing directory for -userdir switch") << endl;
665 int parse_execute(string const & arg, string const &)
668 lyxerr << _("Missing command string after --execute switch") << endl;
672 // Argh. Setting gui to false segfaults..
673 // FIXME: when ? how ?
678 int parse_export(string const & type, string const &)
681 lyxerr << _("Missing file type [eg latex, ps...] after "
682 "--export switch") << endl;
685 batch = "buffer-export " + type;
690 int parse_import(string const & type, string const & file)
693 lyxerr << _("Missing file type [eg latex, ps...] after "
694 "--import switch") << endl;
698 lyxerr << _("Missing filename for --import") << endl;
702 batch = "buffer-import " + type + ' ' + file;
709 bool LyX::easyParse(int & argc, char * argv[])
711 std::map<string, cmd_helper> cmdmap;
713 cmdmap["-dbg"] = parse_dbg;
714 cmdmap["-help"] = parse_help;
715 cmdmap["--help"] = parse_help;
716 cmdmap["-version"] = parse_version;
717 cmdmap["--version"] = parse_version;
718 cmdmap["-sysdir"] = parse_sysdir;
719 cmdmap["-userdir"] = parse_userdir;
720 cmdmap["-x"] = parse_execute;
721 cmdmap["--execute"] = parse_execute;
722 cmdmap["-e"] = parse_export;
723 cmdmap["--export"] = parse_export;
724 cmdmap["-i"] = parse_import;
725 cmdmap["--import"] = parse_import;
727 for (int i = 1; i < argc; ++i) {
728 std::map<string, cmd_helper>::const_iterator it
729 = cmdmap.find(argv[i]);
731 // don't complain if not found - may be parsed later
732 if (it == cmdmap.end())
735 string arg((i + 1 < argc) ? argv[i + 1] : "");
736 string arg2((i + 2 < argc) ? argv[i + 2] : "");
738 int const remove = 1 + it->second(arg, arg2);
740 // Now, remove used arguments by shifting
741 // the following ones remove places down.
743 for (int j = i; j < argc; ++j)
744 argv[j] = argv[j + remove];
748 batch_command = batch;