3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alfredo Braunstein
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * \author André Pönitz
12 * Full author contact details are available in file CREDITS.
20 #include "support/filetools.h"
21 #include "support/lyxlib.h"
22 #include "support/FileInfo.h"
23 #include "support/path.h"
24 #include "support/path_defines.h"
29 #include "graphics/GraphicsTypes.h"
31 #include "bufferlist.h"
33 #include "buffer_funcs.h"
34 #include "lyxserver.h"
37 #include "ToolbarBackend.h"
38 #include "MenuBackend.h"
40 #include "lastfiles.h"
42 #include "converter.h"
44 #include "lyxtextclasslist.h"
46 #include "frontends/Alert.h"
47 #include "frontends/lyx_gui.h"
49 #include <boost/function.hpp>
50 #include <boost/bind.hpp>
51 #include <boost/signals/signal1.hpp>
57 using namespace lyx::support;
62 #ifndef CXX_GLOBAL_CSTD
68 extern void QuitLyX();
70 extern LyXServer * lyxserver;
74 boost::scoped_ptr<LastFiles> lastfiles;
76 // This is the global bufferlist object
77 BufferList bufferlist;
79 // convenient to have it here.
80 boost::scoped_ptr<kb_keymap> toplevel_keymap;
84 void showFileError(string const & error)
86 Alert::warning(_("Could not read configuration file"),
87 bformat(_("Error while reading the configuration file\n%1$s.\n"
88 "Please check your installation."), error));
94 LyX::LyX(int & argc, char * argv[])
96 // Here we need to parse the command line. At least
97 // we need to parse for "-dbg" and "-help"
98 bool const want_gui = easyParse(argc, argv);
100 // set the DisplayTranslator only once; should that be done here??
101 // if this should not be in this file, please also remove
102 // #include "graphics/GraphicsTypes.h" at the top -- Rob Lahaye.
103 lyx::graphics::setDisplayTranslator();
106 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();
126 vector<string> files;
128 for (int argi = argc - 1; argi >= 1; --argi)
129 files.push_back(argv[argi]);
132 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
134 // Execute batch commands if available
135 if (!batch_command.empty()) {
137 lyxerr[Debug::INIT] << "About to handle -x '"
138 << batch_command << '\'' << endl;
140 Buffer * last_loaded = 0;
142 vector<string>::const_iterator it = files.begin();
143 vector<string>::const_iterator end = files.end();
145 for (; it != end; ++it) {
146 // get absolute path of file and add ".lyx" to
147 // the filename if necessary
148 string s = FileSearch(string(), *it, "lyx");
150 last_loaded = newFile(*it, string(), true);
152 Buffer * buf = bufferlist.newBuffer(s, false);
153 buf->error.connect(boost::bind(&LyX::printError, this, _1));
154 if (loadLyXFile(buf, s))
157 bufferlist.release(buf);
161 // try to dispatch to last loaded buffer first
163 bool success = false;
164 if (last_loaded->dispatch(batch_command, &success)) {
169 files.clear(); // the files are already loaded
172 lyx_gui::start(batch_command, files);
178 static void error_handler(int err_sig)
182 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
188 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
191 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
193 "Sorry, you have found a bug in LyX. "
194 "Please read the bug-reporting instructions "
195 "in Help->Introduction and send us a bug report, "
196 "if necessary. Thanks !" << endl;
203 // Deinstall the signal handlers
204 signal(SIGHUP, SIG_DFL);
205 signal(SIGINT, SIG_DFL);
206 signal(SIGFPE, SIG_DFL);
207 signal(SIGSEGV, SIG_DFL);
208 signal(SIGTERM, SIG_DFL);
210 LyX::emergencyCleanup();
212 lyxerr << "Bye." << endl;
213 if (err_sig!= SIGHUP &&
214 (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))
215 lyx::support::abort();
222 void LyX::printError(ErrorItem const & ei)
224 std::cerr << _("LyX: ") << ei.error
225 << ':' << ei.description << std::endl;
230 void LyX::init(bool gui)
232 signal(SIGHUP, error_handler);
233 signal(SIGFPE, error_handler);
234 signal(SIGSEGV, error_handler);
235 signal(SIGINT, error_handler);
236 signal(SIGTERM, error_handler);
238 bool const explicit_userdir = setLyxPaths();
240 // Check that user LyX directory is ok. We don't do that if
241 // running in batch mode.
243 queryUserLyXDir(explicit_userdir);
248 // Disable gui when easyparse says so
249 lyx_gui::use_gui = gui;
251 if (lyxrc.template_path.empty()) {
252 lyxrc.template_path = AddPath(system_lyxdir(), "templates");
255 if (lyxrc.lastfiles.empty()) {
256 lyxrc.lastfiles = AddName(user_lyxdir(), "lastfiles");
259 if (lyxrc.roman_font_name.empty())
260 lyxrc.roman_font_name = lyx_gui::roman_font_name();
261 if (lyxrc.sans_font_name.empty())
262 lyxrc.sans_font_name = lyx_gui::sans_font_name();
263 if (lyxrc.typewriter_font_name.empty())
264 lyxrc.typewriter_font_name = lyx_gui::typewriter_font_name();
267 // Read configuration files
270 readRcFile("lyxrc.defaults");
271 system_lyxrc = lyxrc;
272 system_formats = formats;
273 system_converters = converters;
274 system_lcolor = lcolor;
276 string prefsfile = "preferences";
277 // back compatibility to lyxs < 1.1.6
278 if (LibFileSearch(string(), prefsfile).empty())
280 if (!LibFileSearch(string(), prefsfile).empty())
281 readRcFile(prefsfile);
283 readEncodingsFile("encodings");
284 readLanguagesFile("languages");
287 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
292 toplevel_keymap.reset(new kb_keymap);
293 defaultKeyBindings(toplevel_keymap.get());
294 toplevel_keymap->read(lyxrc.bind_file);
297 readUIFile(lyxrc.ui_file);
300 if (lyxerr.debugging(Debug::LYXRC))
303 os::setTmpDir(CreateLyXTmpDir(lyxrc.tempdir_path));
304 if (lyxerr.debugging(Debug::INIT)) {
305 lyxerr << "LyX tmp dir: `" << os::getTmpDir() << '\'' << endl;
308 lyxerr[Debug::INIT] << "Reading lastfiles `"
309 << lyxrc.lastfiles << "'..." << endl;
310 lastfiles.reset(new LastFiles(lyxrc.lastfiles,
311 lyxrc.check_lastfiles,
312 lyxrc.num_lastfiles));
316 void LyX::defaultKeyBindings(kb_keymap * kbmap)
318 kbmap->bind("Right", LFUN_RIGHT);
319 kbmap->bind("Left", LFUN_LEFT);
320 kbmap->bind("Up", LFUN_UP);
321 kbmap->bind("Down", LFUN_DOWN);
323 kbmap->bind("Tab", LFUN_CELL_FORWARD);
324 kbmap->bind("ISO_Left_Tab", LFUN_CELL_FORWARD); // jbl 2001-23-02
326 kbmap->bind("Home", LFUN_HOME);
327 kbmap->bind("End", LFUN_END);
328 kbmap->bind("Prior", LFUN_PRIOR);
329 kbmap->bind("Next", LFUN_NEXT);
331 kbmap->bind("Return", LFUN_BREAKPARAGRAPH);
332 //kbmap->bind("~C-~S-~M-nobreakspace", LFUN_PROTECTEDSPACE);
334 kbmap->bind("Delete", LFUN_DELETE);
335 kbmap->bind("BackSpace", LFUN_BACKSPACE);
337 // sub- and superscript -MV
338 kbmap->bind("~S-underscore", LFUN_SUBSCRIPT);
339 kbmap->bind("~S-asciicircum", LFUN_SUPERSCRIPT);
341 // kbmap->bindings to enable the use of the numeric keypad
343 //kbmap->bind("KP_0", LFUN_SELFINSERT);
344 //kbmap->bind("KP_Decimal", LFUN_SELFINSERT);
345 kbmap->bind("KP_Enter", LFUN_BREAKPARAGRAPH);
346 //kbmap->bind("KP_1", LFUN_SELFINSERT);
347 //kbmap->bind("KP_2", LFUN_SELFINSERT);
348 //kbmap->bind("KP_3", LFUN_SELFINSERT);
349 //kbmap->bind("KP_4", LFUN_SELFINSERT);
350 //kbmap->bind("KP_5", LFUN_SELFINSERT);
351 //kbmap->bind("KP_6", LFUN_SELFINSERT);
352 //kbmap->bind("KP_Add", LFUN_SELFINSERT);
353 //kbmap->bind("KP_7", LFUN_SELFINSERT);
354 //kbmap->bind("KP_8", LFUN_SELFINSERT);
355 //kbmap->bind("KP_9", LFUN_SELFINSERT);
356 //kbmap->bind("KP_Divide", LFUN_SELFINSERT);
357 //kbmap->bind("KP_Multiply", LFUN_SELFINSERT);
358 //kbmap->bind("KP_Subtract", LFUN_SELFINSERT);
359 kbmap->bind("KP_Right", LFUN_RIGHT);
360 kbmap->bind("KP_Left", LFUN_LEFT);
361 kbmap->bind("KP_Up", LFUN_UP);
362 kbmap->bind("KP_Down", LFUN_DOWN);
363 kbmap->bind("KP_Home", LFUN_HOME);
364 kbmap->bind("KP_End", LFUN_END);
365 kbmap->bind("KP_Prior", LFUN_PRIOR);
366 kbmap->bind("KP_Next", LFUN_NEXT);
368 kbmap->bind("C-Tab", LFUN_CELL_SPLIT); // ale970515
369 kbmap->bind("S-Tab", LFUN_CELL_BACKWARD); // jug20000522
370 kbmap->bind("S-ISO_Left_Tab", LFUN_CELL_BACKWARD); // jbl 2001-23-02
374 void LyX::emergencyCleanup()
376 // what to do about tmpfiles is non-obvious. we would
377 // like to delete any we find, but our lyxdir might
378 // contain documents etc. which might be helpful on
381 bufferlist.emergencyWriteAll();
383 lyxserver->emergencyCleanup();
387 void LyX::deadKeyBindings(kb_keymap * kbmap)
389 // bindKeyings for transparent handling of deadkeys
390 // The keysyms are gotten from XFree86 X11R6
391 kbmap->bind("~C-~S-~M-dead_acute", LFUN_ACUTE);
392 kbmap->bind("~C-~S-~M-dead_breve", LFUN_BREVE);
393 kbmap->bind("~C-~S-~M-dead_caron", LFUN_CARON);
394 kbmap->bind("~C-~S-~M-dead_cedilla", LFUN_CEDILLA);
395 kbmap->bind("~C-~S-~M-dead_abovering", LFUN_CIRCLE);
396 kbmap->bind("~C-~S-~M-dead_circumflex", LFUN_CIRCUMFLEX);
397 kbmap->bind("~C-~S-~M-dead_abovedot", LFUN_DOT);
398 kbmap->bind("~C-~S-~M-dead_grave", LFUN_GRAVE);
399 kbmap->bind("~C-~S-~M-dead_doubleacute", LFUN_HUNG_UMLAUT);
400 kbmap->bind("~C-~S-~M-dead_macron", LFUN_MACRON);
401 // nothing with this name
402 // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
403 kbmap->bind("~C-~S-~M-dead_tilde", LFUN_TILDE);
404 kbmap->bind("~C-~S-~M-dead_diaeresis", LFUN_UMLAUT);
405 // nothing with this name either...
406 //kbmap->bind("~C-~S-~M-dead_underbar", LFUN_UNDERBAR);
407 kbmap->bind("~C-~S-~M-dead_belowdot", LFUN_UNDERDOT);
408 kbmap->bind("~C-~S-~M-dead_tie", LFUN_TIE);
409 kbmap->bind("~C-~S-~M-dead_ogonek", LFUN_OGONEK);
413 void LyX::queryUserLyXDir(bool explicit_userdir)
415 string const configure_script = AddName(system_lyxdir(), "configure");
417 // Does user directory exist?
418 FileInfo fileInfo(user_lyxdir());
419 if (fileInfo.isOK() && fileInfo.isDir()) {
421 FileInfo script(configure_script);
422 FileInfo defaults(AddName(user_lyxdir(), "lyxrc.defaults"));
423 if (defaults.isOK() && script.isOK()
424 && defaults.getModificationTime() < script.getModificationTime()) {
425 lyxerr << _("LyX: reconfiguring user directory")
427 Path p(user_lyxdir());
428 ::system(configure_script.c_str());
429 lyxerr << "LyX: " << _("Done!") << endl;
434 first_start = !explicit_userdir;
436 lyxerr << bformat(_("LyX: Creating directory %1$s"
437 " and running configure..."), user_lyxdir()) << endl;
439 if (!createDirectory(user_lyxdir(), 0755)) {
440 // Failed, let's use $HOME instead.
441 user_lyxdir(GetEnvPath("HOME"));
442 lyxerr << bformat(_("Failed. Will use %1$s instead."),
443 user_lyxdir()) << endl;
447 // Run configure in user lyx directory
448 Path p(user_lyxdir());
449 ::system(configure_script.c_str());
450 lyxerr << "LyX: " << _("Done!") << endl;
454 void LyX::readRcFile(string const & name)
456 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
458 string const lyxrc_path = LibFileSearch(string(), name);
459 if (!lyxrc_path.empty()) {
461 lyxerr[Debug::INIT] << "Found " << name
462 << " in " << lyxrc_path << endl;
464 if (lyxrc.read(lyxrc_path) >= 0)
472 // Read the ui file `name'
473 void LyX::readUIFile(string const & name)
483 struct keyword_item uitags[ui_last - 1] = {
484 { "include", ui_include },
485 { "menuset", ui_menuset },
486 { "toolbar", ui_toolbar },
487 { "toolbars", ui_toolbars }
490 // Ensure that a file is read only once (prevents include loops)
491 static std::list<string> uifiles;
492 std::list<string>::const_iterator it = uifiles.begin();
493 std::list<string>::const_iterator end = uifiles.end();
494 it = std::find(it, end, name);
496 lyxerr[Debug::INIT] << "UI file '" << name
497 << "' has been read already. "
498 << "Is this an include loop?"
503 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
505 string const ui_path = LibFileSearch("ui", name, "ui");
507 if (ui_path.empty()) {
508 lyxerr[Debug::INIT] << "Could not find " << name << endl;
512 uifiles.push_back(name);
514 lyxerr[Debug::INIT] << "Found " << name
515 << " in " << ui_path << endl;
516 LyXLex lex(uitags, ui_last - 1);
517 lex.setFile(ui_path);
519 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
523 if (lyxerr.debugging(Debug::PARSER))
524 lex.printTable(lyxerr);
530 string const file = lex.getString();
535 menubackend.read(lex);
539 toolbarbackend.read(lex);
543 toolbarbackend.readToolbars(lex);
547 if (!rtrim(lex.getString()).empty())
548 lex.printError("LyX::ReadUIFile: "
549 "Unknown menu tag: `$$Token'");
556 // Read the languages file `name'
557 void LyX::readLanguagesFile(string const & name)
559 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
561 string const lang_path = LibFileSearch(string(), name);
562 if (lang_path.empty()) {
566 languages.read(lang_path);
570 // Read the encodings file `name'
571 void LyX::readEncodingsFile(string const & name)
573 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
575 string const enc_path = LibFileSearch(string(), name);
576 if (enc_path.empty()) {
580 encodings.read(enc_path);
589 /// return the the number of arguments consumed
590 typedef boost::function<int(string const &, string const &)> cmd_helper;
592 int parse_dbg(string const & arg, string const &)
595 lyxerr << _("List of supported debug flags:") << endl;
596 Debug::showTags(lyxerr);
599 lyxerr << bformat(_("Setting debug level to %1$s"), arg) << endl;
601 lyxerr.level(Debug::value(arg));
602 Debug::showLevel(lyxerr, lyxerr.level());
607 int parse_help(string const &, string const &)
610 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
611 "Command line switches (case sensitive):\n"
612 "\t-help summarize LyX usage\n"
613 "\t-userdir dir try to set user directory to dir\n"
614 "\t-sysdir dir try to set system directory to dir\n"
615 "\t-geometry WxH+X+Y set geometry of the main window\n"
616 "\t-dbg feature[,feature]...\n"
617 " select the features to debug.\n"
618 " Type `lyx -dbg' to see the list of features\n"
619 "\t-x [--execute] command\n"
620 " where command is a lyx command.\n"
621 "\t-e [--export] fmt\n"
622 " where fmt is the export format of choice.\n"
623 "\t-i [--import] fmt file.xxx\n"
624 " where fmt is the import format of choice\n"
625 " and file.xxx is the file to be imported.\n"
626 "\t-version summarize version and build info\n"
627 "Check the LyX man page for more details.") << endl;
632 int parse_version(string const &, string const &)
634 lyxerr << "LyX " << lyx_version
635 << " of " << lyx_release_date << endl;
636 lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
638 lyxerr << lyx_version_info << endl;
643 int parse_sysdir(string const & arg, string const &)
646 lyxerr << _("Missing directory for -sysdir switch") << endl;
653 int parse_userdir(string const & arg, string const &)
656 lyxerr << _("Missing directory for -userdir switch") << endl;
663 int parse_execute(string const & arg, string const &)
666 lyxerr << _("Missing command string after --execute switch") << endl;
670 // Argh. Setting gui to false segfaults..
671 // FIXME: when ? how ?
676 int parse_export(string const & type, string const &)
679 lyxerr << _("Missing file type [eg latex, ps...] after "
680 "--export switch") << endl;
683 batch = "buffer-export " + type;
688 int parse_import(string const & type, string const & file)
691 lyxerr << _("Missing file type [eg latex, ps...] after "
692 "--import switch") << endl;
696 lyxerr << _("Missing filename for --import") << endl;
700 batch = "buffer-import " + type + ' ' + file;
707 bool LyX::easyParse(int & argc, char * argv[])
709 std::map<string, cmd_helper> cmdmap;
711 cmdmap["-dbg"] = parse_dbg;
712 cmdmap["-help"] = parse_help;
713 cmdmap["--help"] = parse_help;
714 cmdmap["-version"] = parse_version;
715 cmdmap["--version"] = parse_version;
716 cmdmap["-sysdir"] = parse_sysdir;
717 cmdmap["-userdir"] = parse_userdir;
718 cmdmap["-x"] = parse_execute;
719 cmdmap["--execute"] = parse_execute;
720 cmdmap["-e"] = parse_export;
721 cmdmap["--export"] = parse_export;
722 cmdmap["-i"] = parse_import;
723 cmdmap["--import"] = parse_import;
725 for (int i = 1; i < argc; ++i) {
726 std::map<string, cmd_helper>::const_iterator it
727 = cmdmap.find(argv[i]);
729 // don't complain if not found - may be parsed later
730 if (it == cmdmap.end())
733 string arg((i + 1 < argc) ? argv[i + 1] : "");
734 string arg2((i + 2 < argc) ? argv[i + 2] : "");
736 int const remove = 1 + it->second(arg, arg2);
738 // Now, remove used arguments by shifting
739 // the following ones remove places down.
741 for (int j = i; j < argc; ++j)
742 argv[j] = argv[j + remove];
746 batch_command = batch;