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 user_lyxdir;
72 boost::scoped_ptr<LastFiles> lastfiles;
74 // This is the global bufferlist object
75 BufferList bufferlist;
77 // convenient to have it here.
78 boost::scoped_ptr<kb_keymap> toplevel_keymap;
82 void showFileError(string const & error)
84 Alert::warning(_("Could not read configuration file"),
85 bformat(_("Error while reading the configuration file\n%1$s.\n"
86 "Please check your installation."), error));
92 LyX::LyX(int & argc, char * argv[])
94 // Here we need to parse the command line. At least
95 // we need to parse for "-dbg" and "-help"
96 bool const want_gui = easyParse(argc, argv);
98 // set the DisplayTranslator only once; should that be done here??
99 // if this should not be in this file, please also remove
100 // #include "graphics/GraphicsTypes.h" at the top -- Rob Lahaye.
101 lyx::graphics::setDisplayTranslator();
104 lyx_gui::parse_init(argc, argv);
107 // check for any spurious extra arguments
108 // other than documents
109 for (int argi = 1; argi < argc ; ++argi) {
110 if (argv[argi][0] == '-') {
111 lyxerr << bformat(_("Wrong command line option `%1$s'. Exiting."),
117 // Initialization of LyX (reads lyxrc and more)
118 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
120 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
123 lyx_gui::parse_lyxrc();
126 vector<string> files;
128 for (int argi = argc - 1; argi >= 1; --argi) {
129 files.push_back(argv[argi]);
133 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
135 // Execute batch commands if available
136 if (!batch_command.empty()) {
138 lyxerr[Debug::INIT] << "About to handle -x '"
139 << batch_command << '\'' << endl;
141 Buffer * last_loaded = 0;
143 vector<string>::const_iterator it = files.begin();
144 vector<string>::const_iterator end = files.end();
146 for (; it != end; ++it) {
147 // get absolute path of file and add ".lyx" to
148 // the filename if necessary
149 string s = FileSearch(string(), *it, "lyx");
151 last_loaded = newFile(*it, "");
153 last_loaded = bufferlist.newBuffer(s, false);
154 last_loaded->error.connect(boost::bind(&LyX::printError, this, _1));
155 if (!loadLyXFile(last_loaded, s)) {
156 bufferlist.release(last_loaded);
157 last_loaded = newFile(*it, string());
162 // try to dispatch to last loaded buffer first
164 bool success = false;
165 if (last_loaded->dispatch(batch_command, &success)) {
170 files.clear(); // the files are already loaded
173 lyx_gui::start(batch_command, files);
179 static void error_handler(int err_sig)
183 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
189 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
192 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
194 "Sorry, you have found a bug in LyX. "
195 "Please read the bug-reporting instructions "
196 "in Help->Introduction and send us a bug report, "
197 "if necessary. Thanks !" << endl;
204 // Deinstall the signal handlers
205 signal(SIGHUP, SIG_DFL);
206 signal(SIGINT, SIG_DFL);
207 signal(SIGFPE, SIG_DFL);
208 signal(SIGSEGV, SIG_DFL);
209 signal(SIGTERM, SIG_DFL);
211 LyX::emergencyCleanup();
213 lyxerr << "Bye." << endl;
214 if (err_sig!= SIGHUP &&
215 (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))
216 lyx::support::abort();
223 void LyX::printError(ErrorItem const & ei)
225 std::cerr << _("LyX: ") << ei.error
226 << ':' << ei.description << std::endl;
231 void LyX::init(bool gui)
233 signal(SIGHUP, error_handler);
234 signal(SIGFPE, error_handler);
235 signal(SIGSEGV, error_handler);
236 signal(SIGINT, error_handler);
237 signal(SIGTERM, error_handler);
239 bool const explicit_userdir = setLyxPaths();
241 // Check that user LyX directory is ok. We don't do that if
242 // running in batch mode.
244 queryUserLyXDir(explicit_userdir);
249 // Disable gui when easyparse says so
250 lyx_gui::use_gui = gui;
252 if (lyxrc.template_path.empty()) {
253 lyxrc.template_path = AddPath(system_lyxdir, "templates");
256 if (lyxrc.lastfiles.empty()) {
257 lyxrc.lastfiles = AddName(user_lyxdir, "lastfiles");
260 if (lyxrc.roman_font_name.empty())
261 lyxrc.roman_font_name = lyx_gui::roman_font_name();
262 if (lyxrc.sans_font_name.empty())
263 lyxrc.sans_font_name = lyx_gui::sans_font_name();
264 if (lyxrc.typewriter_font_name.empty())
265 lyxrc.typewriter_font_name = lyx_gui::typewriter_font_name();
268 // Read configuration files
271 readRcFile("lyxrc.defaults");
272 system_lyxrc = lyxrc;
273 system_formats = formats;
274 system_converters = converters;
275 system_lcolor = lcolor;
277 string prefsfile = "preferences";
278 // back compatibility to lyxs < 1.1.6
279 if (LibFileSearch(string(), prefsfile).empty())
281 if (!LibFileSearch(string(), prefsfile).empty())
282 readRcFile(prefsfile);
284 readEncodingsFile("encodings");
285 readLanguagesFile("languages");
288 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
293 toplevel_keymap.reset(new kb_keymap);
294 defaultKeyBindings(toplevel_keymap.get());
295 toplevel_keymap->read(lyxrc.bind_file);
298 readUIFile(lyxrc.ui_file);
301 if (lyxerr.debugging(Debug::LYXRC))
304 os::setTmpDir(CreateLyXTmpDir(lyxrc.tempdir_path));
305 if (lyxerr.debugging(Debug::INIT)) {
306 lyxerr << "LyX tmp dir: `" << os::getTmpDir() << '\'' << endl;
309 lyxerr[Debug::INIT] << "Reading lastfiles `"
310 << lyxrc.lastfiles << "'..." << endl;
311 lastfiles.reset(new LastFiles(lyxrc.lastfiles,
312 lyxrc.check_lastfiles,
313 lyxrc.num_lastfiles));
317 void LyX::defaultKeyBindings(kb_keymap * kbmap)
319 kbmap->bind("Right", LFUN_RIGHT);
320 kbmap->bind("Left", LFUN_LEFT);
321 kbmap->bind("Up", LFUN_UP);
322 kbmap->bind("Down", LFUN_DOWN);
324 kbmap->bind("Tab", LFUN_CELL_FORWARD);
325 kbmap->bind("ISO_Left_Tab", LFUN_CELL_FORWARD); // jbl 2001-23-02
327 kbmap->bind("Home", LFUN_HOME);
328 kbmap->bind("End", LFUN_END);
329 kbmap->bind("Prior", LFUN_PRIOR);
330 kbmap->bind("Next", LFUN_NEXT);
332 kbmap->bind("Return", LFUN_BREAKPARAGRAPH);
333 //kbmap->bind("~C-~S-~M-nobreakspace", LFUN_PROTECTEDSPACE);
335 kbmap->bind("Delete", LFUN_DELETE);
336 kbmap->bind("BackSpace", LFUN_BACKSPACE);
338 // sub- and superscript -MV
339 kbmap->bind("~S-underscore", LFUN_SUBSCRIPT);
340 kbmap->bind("~S-asciicircum", LFUN_SUPERSCRIPT);
342 // kbmap->bindings to enable the use of the numeric keypad
344 //kbmap->bind("KP_0", LFUN_SELFINSERT);
345 //kbmap->bind("KP_Decimal", LFUN_SELFINSERT);
346 kbmap->bind("KP_Enter", LFUN_BREAKPARAGRAPH);
347 //kbmap->bind("KP_1", LFUN_SELFINSERT);
348 //kbmap->bind("KP_2", LFUN_SELFINSERT);
349 //kbmap->bind("KP_3", LFUN_SELFINSERT);
350 //kbmap->bind("KP_4", LFUN_SELFINSERT);
351 //kbmap->bind("KP_5", LFUN_SELFINSERT);
352 //kbmap->bind("KP_6", LFUN_SELFINSERT);
353 //kbmap->bind("KP_Add", LFUN_SELFINSERT);
354 //kbmap->bind("KP_7", LFUN_SELFINSERT);
355 //kbmap->bind("KP_8", LFUN_SELFINSERT);
356 //kbmap->bind("KP_9", LFUN_SELFINSERT);
357 //kbmap->bind("KP_Divide", LFUN_SELFINSERT);
358 //kbmap->bind("KP_Multiply", LFUN_SELFINSERT);
359 //kbmap->bind("KP_Subtract", LFUN_SELFINSERT);
360 kbmap->bind("KP_Right", LFUN_RIGHT);
361 kbmap->bind("KP_Left", LFUN_LEFT);
362 kbmap->bind("KP_Up", LFUN_UP);
363 kbmap->bind("KP_Down", LFUN_DOWN);
364 kbmap->bind("KP_Home", LFUN_HOME);
365 kbmap->bind("KP_End", LFUN_END);
366 kbmap->bind("KP_Prior", LFUN_PRIOR);
367 kbmap->bind("KP_Next", LFUN_NEXT);
369 kbmap->bind("C-Tab", LFUN_CELL_SPLIT); // ale970515
370 kbmap->bind("S-Tab", LFUN_CELL_BACKWARD); // jug20000522
371 kbmap->bind("S-ISO_Left_Tab", LFUN_CELL_BACKWARD); // jbl 2001-23-02
375 void LyX::emergencyCleanup()
377 // what to do about tmpfiles is non-obvious. we would
378 // like to delete any we find, but our lyxdir might
379 // contain documents etc. which might be helpful on
382 bufferlist.emergencyWriteAll();
384 lyxserver->emergencyCleanup();
388 void LyX::deadKeyBindings(kb_keymap * kbmap)
390 // bindKeyings for transparent handling of deadkeys
391 // The keysyms are gotten from XFree86 X11R6
392 kbmap->bind("~C-~S-~M-dead_acute", LFUN_ACUTE);
393 kbmap->bind("~C-~S-~M-dead_breve", LFUN_BREVE);
394 kbmap->bind("~C-~S-~M-dead_caron", LFUN_CARON);
395 kbmap->bind("~C-~S-~M-dead_cedilla", LFUN_CEDILLA);
396 kbmap->bind("~C-~S-~M-dead_abovering", LFUN_CIRCLE);
397 kbmap->bind("~C-~S-~M-dead_circumflex", LFUN_CIRCUMFLEX);
398 kbmap->bind("~C-~S-~M-dead_abovedot", LFUN_DOT);
399 kbmap->bind("~C-~S-~M-dead_grave", LFUN_GRAVE);
400 kbmap->bind("~C-~S-~M-dead_doubleacute", LFUN_HUNG_UMLAUT);
401 kbmap->bind("~C-~S-~M-dead_macron", LFUN_MACRON);
402 // nothing with this name
403 // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
404 kbmap->bind("~C-~S-~M-dead_tilde", LFUN_TILDE);
405 kbmap->bind("~C-~S-~M-dead_diaeresis", LFUN_UMLAUT);
406 // nothing with this name either...
407 //kbmap->bind("~C-~S-~M-dead_underbar", LFUN_UNDERBAR);
408 kbmap->bind("~C-~S-~M-dead_belowdot", LFUN_UNDERDOT);
409 kbmap->bind("~C-~S-~M-dead_tie", LFUN_TIE);
410 kbmap->bind("~C-~S-~M-dead_ogonek", LFUN_OGONEK);
414 void LyX::queryUserLyXDir(bool explicit_userdir)
416 string const configure_script = AddName(system_lyxdir, "configure");
418 // Does user directory exist?
419 FileInfo fileInfo(user_lyxdir);
420 if (fileInfo.isOK() && fileInfo.isDir()) {
422 FileInfo script(configure_script);
423 FileInfo defaults(AddName(user_lyxdir, "lyxrc.defaults"));
424 if (defaults.isOK() && script.isOK()
425 && defaults.getModificationTime() < script.getModificationTime()) {
426 lyxerr << _("LyX: reconfiguring user directory")
429 ::system(configure_script.c_str());
430 lyxerr << "LyX: " << _("Done!") << endl;
435 first_start = !explicit_userdir;
437 lyxerr << bformat(_("LyX: Creating directory %1$s"
438 " and running configure..."), user_lyxdir) << endl;
440 if (!createDirectory(user_lyxdir, 0755)) {
441 // Failed, let's use $HOME instead.
442 user_lyxdir = GetEnvPath("HOME");
443 lyxerr << bformat(_("Failed. Will use %1$s instead."),
444 user_lyxdir) << endl;
448 // Run configure in user lyx directory
450 ::system(configure_script.c_str());
451 lyxerr << "LyX: " << _("Done!") << endl;
455 void LyX::readRcFile(string const & name)
457 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
459 string const lyxrc_path = LibFileSearch(string(), name);
460 if (!lyxrc_path.empty()) {
462 lyxerr[Debug::INIT] << "Found " << name
463 << " in " << lyxrc_path << endl;
465 if (lyxrc.read(lyxrc_path) >= 0)
473 // Read the ui file `name'
474 void LyX::readUIFile(string const & name)
484 struct keyword_item uitags[ui_last - 1] = {
485 { "include", ui_include },
486 { "menuset", ui_menuset },
487 { "toolbar", ui_toolbar },
488 { "toolbars", ui_toolbars }
491 // Ensure that a file is read only once (prevents include loops)
492 static std::list<string> uifiles;
493 std::list<string>::const_iterator it = uifiles.begin();
494 std::list<string>::const_iterator end = uifiles.end();
495 it = std::find(it, end, name);
497 lyxerr[Debug::INIT] << "UI file '" << name
498 << "' has been read already. "
499 << "Is this an include loop?"
504 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
506 string const ui_path = LibFileSearch("ui", name, "ui");
508 if (ui_path.empty()) {
509 lyxerr[Debug::INIT] << "Could not find " << name << endl;
513 uifiles.push_back(name);
515 lyxerr[Debug::INIT] << "Found " << name
516 << " in " << ui_path << endl;
517 LyXLex lex(uitags, ui_last - 1);
518 lex.setFile(ui_path);
520 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
524 if (lyxerr.debugging(Debug::PARSER))
525 lex.printTable(lyxerr);
531 string const file = lex.getString();
536 menubackend.read(lex);
540 toolbarbackend.read(lex);
544 toolbarbackend.readToolbars(lex);
548 if (!rtrim(lex.getString()).empty())
549 lex.printError("LyX::ReadUIFile: "
550 "Unknown menu tag: `$$Token'");
557 // Read the languages file `name'
558 void LyX::readLanguagesFile(string const & name)
560 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
562 string const lang_path = LibFileSearch(string(), name);
563 if (lang_path.empty()) {
567 languages.read(lang_path);
571 // Read the encodings file `name'
572 void LyX::readEncodingsFile(string const & name)
574 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
576 string const enc_path = LibFileSearch(string(), name);
577 if (enc_path.empty()) {
581 encodings.read(enc_path);
590 /// return the the number of arguments consumed
591 typedef boost::function<int(string const &, string const &)> cmd_helper;
593 int parse_dbg(string const & arg, string const &)
596 lyxerr << _("List of supported debug flags:") << endl;
597 Debug::showTags(lyxerr);
600 lyxerr << bformat(_("Setting debug level to %1$s"), arg) << endl;
602 lyxerr.level(Debug::value(arg));
603 Debug::showLevel(lyxerr, lyxerr.level());
608 int parse_help(string const &, string const &)
611 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
612 "Command line switches (case sensitive):\n"
613 "\t-help summarize LyX usage\n"
614 "\t-userdir dir try to set user directory to dir\n"
615 "\t-sysdir dir try to set system directory to dir\n"
616 "\t-geometry WxH+X+Y set geometry of the main window\n"
617 "\t-dbg feature[,feature]...\n"
618 " select the features to debug.\n"
619 " Type `lyx -dbg' to see the list of features\n"
620 "\t-x [--execute] command\n"
621 " where command is a lyx command.\n"
622 "\t-e [--export] fmt\n"
623 " where fmt is the export format of choice.\n"
624 "\t-i [--import] fmt file.xxx\n"
625 " where fmt is the import format of choice\n"
626 " and file.xxx is the file to be imported.\n"
627 "\t-version summarize version and build info\n"
628 "Check the LyX man page for more details.") << endl;
633 int parse_version(string const &, string const &)
635 lyxerr << "LyX " << lyx_version
636 << " of " << lyx_release_date << endl;
637 lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
639 lyxerr << lyx_version_info << endl;
644 int parse_sysdir(string const & arg, string const &)
647 lyxerr << _("Missing directory for -sysdir switch") << endl;
654 int parse_userdir(string const & arg, string const &)
657 lyxerr << _("Missing directory for -userdir switch") << endl;
664 int parse_execute(string const & arg, string const &)
667 lyxerr << _("Missing command string after --execute switch") << endl;
671 // Argh. Setting gui to false segfaults..
672 // FIXME: when ? how ?
677 int parse_export(string const & type, string const &)
680 lyxerr << _("Missing file type [eg latex, ps...] after "
681 "--export switch") << endl;
684 batch = "buffer-export " + type;
689 int parse_import(string const & type, string const & file)
692 lyxerr << _("Missing file type [eg latex, ps...] after "
693 "--import switch") << endl;
697 lyxerr << _("Missing filename for --import") << endl;
701 batch = "buffer-import " + type + ' ' + file;
708 bool LyX::easyParse(int & argc, char * argv[])
710 std::map<string, cmd_helper> cmdmap;
712 cmdmap["-dbg"] = parse_dbg;
713 cmdmap["-help"] = parse_help;
714 cmdmap["--help"] = parse_help;
715 cmdmap["-version"] = parse_version;
716 cmdmap["--version"] = parse_version;
717 cmdmap["-sysdir"] = parse_sysdir;
718 cmdmap["-userdir"] = parse_userdir;
719 cmdmap["-x"] = parse_execute;
720 cmdmap["--execute"] = parse_execute;
721 cmdmap["-e"] = parse_export;
722 cmdmap["--export"] = parse_export;
723 cmdmap["-i"] = parse_import;
724 cmdmap["--import"] = parse_import;
726 for (int i = 1; i < argc; ++i) {
727 std::map<string, cmd_helper>::const_iterator it
728 = cmdmap.find(argv[i]);
730 // don't complain if not found - may be parsed later
731 if (it == cmdmap.end())
734 string arg((i + 1 < argc) ? argv[i + 1] : "");
735 string arg2((i + 2 < argc) ? argv[i + 2] : "");
737 int const remove = 1 + it->second(arg, arg2);
739 // Now, remove used arguments by shifting
740 // the following ones remove places down.
742 for (int j = i; j < argc; ++j)
743 argv[j] = argv[j + remove];
747 batch_command = batch;