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.
21 #include "buffer_funcs.h"
22 #include "bufferlist.h"
23 #include "converter.h"
26 #include "errorlist.h"
31 #include "lastfiles.h"
36 #include "lyxtextclasslist.h"
37 #include "lyxserver.h"
38 #include "MenuBackend.h"
39 #include "ToolbarBackend.h"
41 #include "mathed/math_inset.h"
43 #include "frontends/Alert.h"
44 #include "frontends/lyx_gui.h"
45 #include "frontends/LyXView.h"
47 #include "support/FileInfo.h"
48 #include "support/filetools.h"
49 #include "support/lyxlib.h"
50 #include "support/os.h"
51 #include "support/path.h"
52 #include "support/path_defines.h"
54 #include <boost/bind.hpp>
59 using lyx::support::AddName;
60 using lyx::support::AddPath;
61 using lyx::support::bformat;
62 using lyx::support::createDirectory;
63 using lyx::support::createLyXTmpDir;
64 using lyx::support::FileInfo;
65 using lyx::support::FileSearch;
66 using lyx::support::GetEnv;
67 using lyx::support::GetEnvPath;
68 using lyx::support::i18nLibFileSearch;
69 using lyx::support::LibFileSearch;
70 using lyx::support::Path;
71 using lyx::support::rtrim;
72 using lyx::support::setLyxPaths;
73 using lyx::support::system_lyxdir;
74 using lyx::support::user_lyxdir;
76 using lyx::support::os::getTmpDir;
77 using lyx::support::os::setTmpDir;
83 #ifndef CXX_GLOBAL_CSTD
90 extern void QuitLyX();
92 extern LyXServer * lyxserver;
94 // This is the global bufferlist object
95 BufferList bufferlist;
97 // convenient to have it here.
98 boost::scoped_ptr<kb_keymap> toplevel_keymap;
102 void showFileError(string const & error)
104 Alert::warning(_("Could not read configuration file"),
105 bformat(_("Error while reading the configuration file\n%1$s.\n"
106 "Please check your installation."), error));
113 boost::scoped_ptr<LyX> LyX::singleton_;
115 void LyX::exec(int & argc, char * argv[])
117 BOOST_ASSERT(!singleton_.get());
118 // We must return from this before launching the gui so that
119 // other parts of the code can access singleton_ through
120 // LyX::ref and LyX::cref.
121 singleton_.reset(new LyX);
122 // Start the real execution loop.
123 singleton_->priv_exec(argc, argv);
129 BOOST_ASSERT(singleton_.get());
130 return *singleton_.get();
134 LyX const & LyX::cref()
136 BOOST_ASSERT(singleton_.get());
137 return *singleton_.get();
146 LastFiles & LyX::lastfiles()
148 BOOST_ASSERT(lastfiles_.get());
149 return *lastfiles_.get();
153 LastFiles const & LyX::lastfiles() const
155 BOOST_ASSERT(lastfiles_.get());
156 return *lastfiles_.get();
160 void LyX::addLyXView(boost::shared_ptr<LyXView> const & lyxview)
162 views_.push_back(lyxview);
166 Buffer const * const LyX::updateInset(InsetBase const * inset) const
171 Buffer const * buffer_ptr = 0;
172 ViewList::const_iterator it = views_.begin();
173 ViewList::const_iterator const end = views_.end();
174 for (; it != end; ++it) {
175 Buffer const * ptr = (*it)->updateInset(inset);
183 void LyX::priv_exec(int & argc, char * argv[])
185 // Here we need to parse the command line. At least
186 // we need to parse for "-dbg" and "-help"
187 bool const want_gui = easyParse(argc, argv);
190 lyx_gui::parse_init(argc, argv);
192 // check for any spurious extra arguments
193 // other than documents
194 for (int argi = 1; argi < argc ; ++argi) {
195 if (argv[argi][0] == '-') {
196 lyxerr << bformat(_("Wrong command line option `%1$s'. Exiting."),
202 // Initialization of LyX (reads lyxrc and more)
203 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
205 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
208 lyx_gui::parse_lyxrc();
212 vector<string> files;
214 for (int argi = argc - 1; argi >= 1; --argi)
215 files.push_back(argv[argi]);
218 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
220 // Execute batch commands if available
221 if (!batch_command.empty()) {
223 lyxerr[Debug::INIT] << "About to handle -x '"
224 << batch_command << '\'' << endl;
226 Buffer * last_loaded = 0;
228 vector<string>::const_iterator it = files.begin();
229 vector<string>::const_iterator end = files.end();
231 for (; it != end; ++it) {
232 // get absolute path of file and add ".lyx" to
233 // the filename if necessary
234 string s = FileSearch(string(), *it, "lyx");
236 last_loaded = newFile(*it, string(), true);
238 Buffer * buf = bufferlist.newBuffer(s, false);
239 buf->error.connect(boost::bind(&LyX::printError, this, _1));
240 if (loadLyXFile(buf, s))
243 bufferlist.release(buf);
247 // try to dispatch to last loaded buffer first
249 bool success = false;
250 if (last_loaded->dispatch(batch_command, &success)) {
255 files.clear(); // the files are already loaded
258 lyx_gui::start(batch_command, files);
264 static void error_handler(int err_sig)
266 // Throw away any signals other than the first one received.
267 static sig_atomic_t handling_error = false;
270 handling_error = true;
272 // We have received a signal indicating a fatal error, so
273 // try and save the data ASAP.
274 LyX::cref().emergencyCleanup();
276 // These lyxerr calls may or may not work:
278 // Signals are asynchronous, so the main program may be in a very
279 // fragile state when a signal is processed and thus while a signal
280 // handler function executes.
281 // In general, therefore, we should avoid performing any
282 // I/O operations or calling most library and system functions from
285 // This shouldn't matter here, however, as we've already invoked
289 lyxerr << "\nlyx: SIGHUP signal caught\nBye." << endl;
292 lyxerr << "\nlyx: SIGFPE signal caught\nBye." << endl;
295 lyxerr << "\nlyx: SIGSEGV signal caught\n"
296 "Sorry, you have found a bug in LyX. "
297 "Please read the bug-reporting instructions "
298 "in Help->Introduction and send us a bug report, "
299 "if necessary. Thanks !\nBye." << endl;
307 // Deinstall the signal handlers
308 signal(SIGHUP, SIG_DFL);
309 signal(SIGINT, SIG_DFL);
310 signal(SIGFPE, SIG_DFL);
311 signal(SIGSEGV, SIG_DFL);
312 signal(SIGTERM, SIG_DFL);
314 if (err_sig == SIGSEGV ||
315 (err_sig != SIGHUP && !GetEnv("LYXDEBUG").empty()))
316 lyx::support::abort();
323 void LyX::printError(ErrorItem const & ei)
325 std::cerr << _("LyX: ") << ei.error
326 << ':' << ei.description << std::endl;
331 void LyX::init(bool gui)
333 signal(SIGHUP, error_handler);
334 signal(SIGFPE, error_handler);
335 signal(SIGSEGV, error_handler);
336 signal(SIGINT, error_handler);
337 signal(SIGTERM, error_handler);
338 // SIGPIPE can be safely ignored.
340 bool const explicit_userdir = setLyxPaths();
342 // Check that user LyX directory is ok. We don't do that if
343 // running in batch mode.
345 queryUserLyXDir(explicit_userdir);
350 // Disable gui when easyparse says so
351 lyx_gui::use_gui = gui;
353 if (lyxrc.template_path.empty()) {
354 lyxrc.template_path = AddPath(system_lyxdir(), "templates");
357 if (lyxrc.lastfiles.empty()) {
358 lyxrc.lastfiles = AddName(user_lyxdir(), "lastfiles");
361 if (lyxrc.roman_font_name.empty())
362 lyxrc.roman_font_name = lyx_gui::roman_font_name();
363 if (lyxrc.sans_font_name.empty())
364 lyxrc.sans_font_name = lyx_gui::sans_font_name();
365 if (lyxrc.typewriter_font_name.empty())
366 lyxrc.typewriter_font_name = lyx_gui::typewriter_font_name();
369 // Read configuration files
372 readRcFile("lyxrc.defaults");
373 system_lyxrc = lyxrc;
374 system_formats = formats;
375 system_converters = converters;
376 system_lcolor = lcolor;
378 string prefsfile = "preferences";
379 // back compatibility to lyxs < 1.1.6
380 if (LibFileSearch(string(), prefsfile).empty())
382 if (!LibFileSearch(string(), prefsfile).empty())
383 readRcFile(prefsfile);
385 readEncodingsFile("encodings");
386 readLanguagesFile("languages");
389 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
394 toplevel_keymap.reset(new kb_keymap);
395 defaultKeyBindings(toplevel_keymap.get());
396 toplevel_keymap->read(lyxrc.bind_file);
399 readUIFile(lyxrc.ui_file);
402 if (lyxerr.debugging(Debug::LYXRC))
405 setTmpDir(createLyXTmpDir(lyxrc.tempdir_path));
406 if (getTmpDir().empty()) {
407 Alert::error(_("Could not create temporary directory"),
408 bformat(_("Could not create a temporary directory in\n"
409 "%1$s. Make sure that this\n"
410 "path exists and is writable and try again."),
411 lyxrc.tempdir_path));
412 // createLyXTmpDir() tries sufficiently hard to create a
413 // usable temp dir, so the probability to come here is
414 // close to zero. We therefore don't try to overcome this
415 // problem with e.g. asking the user for a new path and
416 // trying again but simply exit.
420 if (lyxerr.debugging(Debug::INIT)) {
421 lyxerr << "LyX tmp dir: `" << getTmpDir() << '\'' << endl;
424 lyxerr[Debug::INIT] << "Reading lastfiles `"
425 << lyxrc.lastfiles << "'..." << endl;
426 lastfiles_.reset(new LastFiles(lyxrc.lastfiles,
427 lyxrc.check_lastfiles,
428 lyxrc.num_lastfiles));
432 void LyX::defaultKeyBindings(kb_keymap * kbmap)
434 kbmap->bind("Right", FuncRequest(LFUN_RIGHT));
435 kbmap->bind("Left", FuncRequest(LFUN_LEFT));
436 kbmap->bind("Up", FuncRequest(LFUN_UP));
437 kbmap->bind("Down", FuncRequest(LFUN_DOWN));
439 kbmap->bind("Tab", FuncRequest(LFUN_CELL_FORWARD));
440 kbmap->bind("ISO_Left_Tab", FuncRequest(LFUN_CELL_FORWARD));
442 kbmap->bind("Home", FuncRequest(LFUN_HOME));
443 kbmap->bind("End", FuncRequest(LFUN_END));
444 kbmap->bind("Prior", FuncRequest(LFUN_PRIOR));
445 kbmap->bind("Next", FuncRequest(LFUN_NEXT));
447 kbmap->bind("Return", FuncRequest(LFUN_BREAKPARAGRAPH));
448 //kbmap->bind("~C-~S-~M-nobreakspace", FuncRequest(LFUN_PROTECTEDSPACE));
450 kbmap->bind("Delete", FuncRequest(LFUN_DELETE));
451 kbmap->bind("BackSpace", FuncRequest(LFUN_BACKSPACE));
453 // kbmap->bindings to enable the use of the numeric keypad
455 //kbmap->bind("KP_0", FuncRequest(LFUN_SELFINSERT));
456 //kbmap->bind("KP_Decimal", FuncRequest(LFUN_SELFINSERT));
457 kbmap->bind("KP_Enter", FuncRequest(LFUN_BREAKPARAGRAPH));
458 //kbmap->bind("KP_1", FuncRequest(LFUN_SELFINSERT));
459 //kbmap->bind("KP_2", FuncRequest(LFUN_SELFINSERT));
460 //kbmap->bind("KP_3", FuncRequest(LFUN_SELFINSERT));
461 //kbmap->bind("KP_4", FuncRequest(LFUN_SELFINSERT));
462 //kbmap->bind("KP_5", FuncRequest(LFUN_SELFINSERT));
463 //kbmap->bind("KP_6", FuncRequest(LFUN_SELFINSERT));
464 //kbmap->bind("KP_Add", FuncRequest(LFUN_SELFINSERT));
465 //kbmap->bind("KP_7", FuncRequest(LFUN_SELFINSERT));
466 //kbmap->bind("KP_8", FuncRequest(LFUN_SELFINSERT));
467 //kbmap->bind("KP_9", FuncRequest(LFUN_SELFINSERT));
468 //kbmap->bind("KP_Divide", FuncRequest(LFUN_SELFINSERT));
469 //kbmap->bind("KP_Multiply", FuncRequest(LFUN_SELFINSERT));
470 //kbmap->bind("KP_Subtract", FuncRequest(LFUN_SELFINSERT));
471 kbmap->bind("KP_Right", FuncRequest(LFUN_RIGHT));
472 kbmap->bind("KP_Left", FuncRequest(LFUN_LEFT));
473 kbmap->bind("KP_Up", FuncRequest(LFUN_UP));
474 kbmap->bind("KP_Down", FuncRequest(LFUN_DOWN));
475 kbmap->bind("KP_Home", FuncRequest(LFUN_HOME));
476 kbmap->bind("KP_End", FuncRequest(LFUN_END));
477 kbmap->bind("KP_Prior", FuncRequest(LFUN_PRIOR));
478 kbmap->bind("KP_Next", FuncRequest(LFUN_NEXT));
480 kbmap->bind("C-Tab", FuncRequest(LFUN_CELL_SPLIT));
481 kbmap->bind("S-Tab", FuncRequest(LFUN_CELL_BACKWARD));
482 kbmap->bind("S-ISO_Left_Tab", FuncRequest(LFUN_CELL_BACKWARD));
486 void LyX::emergencyCleanup() const
488 // what to do about tmpfiles is non-obvious. we would
489 // like to delete any we find, but our lyxdir might
490 // contain documents etc. which might be helpful on
493 bufferlist.emergencyWriteAll();
495 lyxserver->emergencyCleanup();
499 void LyX::deadKeyBindings(kb_keymap * kbmap)
501 // bindKeyings for transparent handling of deadkeys
502 // The keysyms are gotten from XFree86 X11R6
503 kbmap->bind("~C-~S-~M-dead_acute", FuncRequest(LFUN_ACUTE));
504 kbmap->bind("~C-~S-~M-dead_breve", FuncRequest(LFUN_BREVE));
505 kbmap->bind("~C-~S-~M-dead_caron", FuncRequest(LFUN_CARON));
506 kbmap->bind("~C-~S-~M-dead_cedilla", FuncRequest(LFUN_CEDILLA));
507 kbmap->bind("~C-~S-~M-dead_abovering", FuncRequest(LFUN_CIRCLE));
508 kbmap->bind("~C-~S-~M-dead_circumflex", FuncRequest(LFUN_CIRCUMFLEX));
509 kbmap->bind("~C-~S-~M-dead_abovedot", FuncRequest(LFUN_DOT));
510 kbmap->bind("~C-~S-~M-dead_grave", FuncRequest(LFUN_GRAVE));
511 kbmap->bind("~C-~S-~M-dead_doubleacute", FuncRequest(LFUN_HUNG_UMLAUT));
512 kbmap->bind("~C-~S-~M-dead_macron", FuncRequest(LFUN_MACRON));
513 // nothing with this name
514 // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
515 kbmap->bind("~C-~S-~M-dead_tilde", FuncRequest(LFUN_TILDE));
516 kbmap->bind("~C-~S-~M-dead_diaeresis", FuncRequest(LFUN_UMLAUT));
517 // nothing with this name either...
518 //kbmap->bind("~C-~S-~M-dead_underbar", FuncRequest(LFUN_UNDERBAR));
519 kbmap->bind("~C-~S-~M-dead_belowdot", FuncRequest(LFUN_UNDERDOT));
520 kbmap->bind("~C-~S-~M-dead_tie", FuncRequest(LFUN_TIE));
521 kbmap->bind("~C-~S-~M-dead_ogonek",FuncRequest(LFUN_OGONEK));
525 void LyX::queryUserLyXDir(bool explicit_userdir)
527 string const configure_script = AddName(system_lyxdir(), "configure");
529 // Does user directory exist?
530 FileInfo fileInfo(user_lyxdir());
531 if (fileInfo.isOK() && fileInfo.isDir()) {
533 FileInfo script(configure_script);
534 FileInfo defaults(AddName(user_lyxdir(), "lyxrc.defaults"));
535 if (defaults.isOK() && script.isOK()
536 && defaults.getModificationTime() < script.getModificationTime()) {
537 lyxerr << _("LyX: reconfiguring user directory")
539 Path p(user_lyxdir());
540 ::system(configure_script.c_str());
541 lyxerr << "LyX: " << _("Done!") << endl;
546 first_start = !explicit_userdir;
548 lyxerr << bformat(_("LyX: Creating directory %1$s"
549 " and running configure..."), user_lyxdir()) << endl;
551 if (!createDirectory(user_lyxdir(), 0755)) {
552 // Failed, let's use $HOME instead.
553 user_lyxdir(GetEnvPath("HOME"));
554 lyxerr << bformat(_("Failed. Will use %1$s instead."),
555 user_lyxdir()) << endl;
559 // Run configure in user lyx directory
560 Path p(user_lyxdir());
561 ::system(configure_script.c_str());
562 lyxerr << "LyX: " << _("Done!") << endl;
566 void LyX::readRcFile(string const & name)
568 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
570 string const lyxrc_path = LibFileSearch(string(), name);
571 if (!lyxrc_path.empty()) {
573 lyxerr[Debug::INIT] << "Found " << name
574 << " in " << lyxrc_path << endl;
576 if (lyxrc.read(lyxrc_path) >= 0)
584 // Read the ui file `name'
585 void LyX::readUIFile(string const & name)
595 struct keyword_item uitags[ui_last - 1] = {
596 { "include", ui_include },
597 { "menuset", ui_menuset },
598 { "toolbar", ui_toolbar },
599 { "toolbars", ui_toolbars }
602 // Ensure that a file is read only once (prevents include loops)
603 static std::list<string> uifiles;
604 std::list<string>::const_iterator it = uifiles.begin();
605 std::list<string>::const_iterator end = uifiles.end();
606 it = std::find(it, end, name);
608 lyxerr[Debug::INIT] << "UI file '" << name
609 << "' has been read already. "
610 << "Is this an include loop?"
615 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
617 string const ui_path = LibFileSearch("ui", name, "ui");
619 if (ui_path.empty()) {
620 lyxerr[Debug::INIT] << "Could not find " << name << endl;
624 uifiles.push_back(name);
626 lyxerr[Debug::INIT] << "Found " << name
627 << " in " << ui_path << endl;
628 LyXLex lex(uitags, ui_last - 1);
629 lex.setFile(ui_path);
631 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
635 if (lyxerr.debugging(Debug::PARSER))
636 lex.printTable(lyxerr);
642 string const file = lex.getString();
647 menubackend.read(lex);
651 toolbarbackend.read(lex);
655 toolbarbackend.readToolbars(lex);
659 if (!rtrim(lex.getString()).empty())
660 lex.printError("LyX::ReadUIFile: "
661 "Unknown menu tag: `$$Token'");
668 // Read the languages file `name'
669 void LyX::readLanguagesFile(string const & name)
671 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
673 string const lang_path = LibFileSearch(string(), name);
674 if (lang_path.empty()) {
678 languages.read(lang_path);
682 // Read the encodings file `name'
683 void LyX::readEncodingsFile(string const & name)
685 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
687 string const enc_path = LibFileSearch(string(), name);
688 if (enc_path.empty()) {
692 encodings.read(enc_path);
701 /// return the the number of arguments consumed
702 typedef boost::function<int(string const &, string const &)> cmd_helper;
704 int parse_dbg(string const & arg, string const &)
707 lyxerr << _("List of supported debug flags:") << endl;
708 Debug::showTags(lyxerr);
711 lyxerr << bformat(_("Setting debug level to %1$s"), arg) << endl;
713 lyxerr.level(Debug::value(arg));
714 Debug::showLevel(lyxerr, lyxerr.level());
719 int parse_help(string const &, string const &)
722 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
723 "Command line switches (case sensitive):\n"
724 "\t-help summarize LyX usage\n"
725 "\t-userdir dir try to set user directory to dir\n"
726 "\t-sysdir dir try to set system directory to dir\n"
727 "\t-geometry WxH+X+Y set geometry of the main window\n"
728 "\t-dbg feature[,feature]...\n"
729 " select the features to debug.\n"
730 " Type `lyx -dbg' to see the list of features\n"
731 "\t-x [--execute] command\n"
732 " where command is a lyx command.\n"
733 "\t-e [--export] fmt\n"
734 " where fmt is the export format of choice.\n"
735 "\t-i [--import] fmt file.xxx\n"
736 " where fmt is the import format of choice\n"
737 " and file.xxx is the file to be imported.\n"
738 "\t-version summarize version and build info\n"
739 "Check the LyX man page for more details.") << endl;
744 int parse_version(string const &, string const &)
746 lyxerr << "LyX " << lyx_version
747 << " of " << lyx_release_date << endl;
748 lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
750 lyxerr << lyx_version_info << endl;
755 int parse_sysdir(string const & arg, string const &)
758 lyxerr << _("Missing directory for -sysdir switch") << endl;
765 int parse_userdir(string const & arg, string const &)
768 lyxerr << _("Missing directory for -userdir switch") << endl;
775 int parse_execute(string const & arg, string const &)
778 lyxerr << _("Missing command string after --execute switch") << endl;
782 // Argh. Setting gui to false segfaults..
783 // FIXME: when ? how ?
788 int parse_export(string const & type, string const &)
791 lyxerr << _("Missing file type [eg latex, ps...] after "
792 "--export switch") << endl;
795 batch = "buffer-export " + type;
800 int parse_import(string const & type, string const & file)
803 lyxerr << _("Missing file type [eg latex, ps...] after "
804 "--import switch") << endl;
808 lyxerr << _("Missing filename for --import") << endl;
812 batch = "buffer-import " + type + ' ' + file;
819 bool LyX::easyParse(int & argc, char * argv[])
821 std::map<string, cmd_helper> cmdmap;
823 cmdmap["-dbg"] = parse_dbg;
824 cmdmap["-help"] = parse_help;
825 cmdmap["--help"] = parse_help;
826 cmdmap["-version"] = parse_version;
827 cmdmap["--version"] = parse_version;
828 cmdmap["-sysdir"] = parse_sysdir;
829 cmdmap["-userdir"] = parse_userdir;
830 cmdmap["-x"] = parse_execute;
831 cmdmap["--execute"] = parse_execute;
832 cmdmap["-e"] = parse_export;
833 cmdmap["--export"] = parse_export;
834 cmdmap["-i"] = parse_import;
835 cmdmap["--import"] = parse_import;
837 for (int i = 1; i < argc; ++i) {
838 std::map<string, cmd_helper>::const_iterator it
839 = cmdmap.find(argv[i]);
841 // don't complain if not found - may be parsed later
842 if (it == cmdmap.end())
845 string arg((i + 1 < argc) ? argv[i + 1] : "");
846 string arg2((i + 2 < argc) ? argv[i + 2] : "");
848 int const remove = 1 + it->second(arg, arg2);
850 // Now, remove used arguments by shifting
851 // the following ones remove places down.
853 for (int j = i; j < argc; ++j)
854 argv[j] = argv[j + remove];
858 batch_command = batch;