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"
40 #include "ToolbarBackend.h"
42 #include "mathed/math_inset.h"
44 #include "frontends/Alert.h"
45 #include "frontends/lyx_gui.h"
46 #include "frontends/LyXView.h"
48 #include "support/FileInfo.h"
49 #include "support/filetools.h"
50 #include "support/lyxlib.h"
51 #include "support/os.h"
52 #include "support/path.h"
53 #include "support/path_defines.h"
55 #include <boost/bind.hpp>
60 using lyx::support::AddName;
61 using lyx::support::AddPath;
62 using lyx::support::bformat;
63 using lyx::support::createDirectory;
64 using lyx::support::createLyXTmpDir;
65 using lyx::support::FileInfo;
66 using lyx::support::FileSearch;
67 using lyx::support::GetEnv;
68 using lyx::support::i18nLibFileSearch;
69 using lyx::support::LibFileSearch;
70 using lyx::support::Path;
71 using lyx::support::QuoteName;
72 using lyx::support::rtrim;
73 using lyx::support::setLyxPaths;
74 using lyx::support::system_lyxdir;
75 using lyx::support::user_lyxdir;
77 using lyx::support::os::getTmpDir;
78 using lyx::support::os::setTmpDir;
80 namespace os = lyx::support::os;
86 #ifndef CXX_GLOBAL_CSTD
93 extern void QuitLyX();
95 extern LyXServer * lyxserver;
97 // This is the global bufferlist object
98 BufferList bufferlist;
100 // convenient to have it here.
101 boost::scoped_ptr<kb_keymap> toplevel_keymap;
105 void showFileError(string const & error)
107 Alert::warning(_("Could not read configuration file"),
108 bformat(_("Error while reading the configuration file\n%1$s.\n"
109 "Please check your installation."), error));
116 boost::scoped_ptr<LyX> LyX::singleton_;
118 void LyX::exec(int & argc, char * argv[])
120 BOOST_ASSERT(!singleton_.get());
121 // We must return from this before launching the gui so that
122 // other parts of the code can access singleton_ through
123 // LyX::ref and LyX::cref.
124 singleton_.reset(new LyX);
125 // Start the real execution loop.
126 singleton_->priv_exec(argc, argv);
132 BOOST_ASSERT(singleton_.get());
133 return *singleton_.get();
137 LyX const & LyX::cref()
139 BOOST_ASSERT(singleton_.get());
140 return *singleton_.get();
149 LastFiles & LyX::lastfiles()
151 BOOST_ASSERT(lastfiles_.get());
152 return *lastfiles_.get();
156 LastFiles const & LyX::lastfiles() const
158 BOOST_ASSERT(lastfiles_.get());
159 return *lastfiles_.get();
163 void LyX::addLyXView(boost::shared_ptr<LyXView> const & lyxview)
165 views_.push_back(lyxview);
169 Buffer const * const LyX::updateInset(InsetBase const * inset) const
174 Buffer const * buffer_ptr = 0;
175 ViewList::const_iterator it = views_.begin();
176 ViewList::const_iterator const end = views_.end();
177 for (; it != end; ++it) {
178 Buffer const * ptr = (*it)->updateInset(inset);
186 void LyX::priv_exec(int & argc, char * argv[])
188 // Here we need to parse the command line. At least
189 // we need to parse for "-dbg" and "-help"
190 bool const want_gui = easyParse(argc, argv);
193 lyx_gui::parse_init(argc, argv);
195 // check for any spurious extra arguments
196 // other than documents
197 for (int argi = 1; argi < argc ; ++argi) {
198 if (argv[argi][0] == '-') {
199 lyxerr << bformat(_("Wrong command line option `%1$s'. Exiting."),
205 // Initialization of LyX (reads lyxrc and more)
206 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
208 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
211 lyx_gui::parse_lyxrc();
215 vector<string> files;
217 for (int argi = argc - 1; argi >= 1; --argi)
218 files.push_back(argv[argi]);
221 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
223 // Execute batch commands if available
224 if (!batch_command.empty()) {
226 lyxerr[Debug::INIT] << "About to handle -x '"
227 << batch_command << '\'' << endl;
229 Buffer * last_loaded = 0;
231 vector<string>::const_iterator it = files.begin();
232 vector<string>::const_iterator end = files.end();
234 for (; it != end; ++it) {
235 // get absolute path of file and add ".lyx" to
236 // the filename if necessary
237 string s = FileSearch(string(), *it, "lyx");
239 last_loaded = newFile(*it, string(), true);
241 Buffer * buf = bufferlist.newBuffer(s, false);
242 buf->error.connect(boost::bind(&LyX::printError, this, _1));
243 if (loadLyXFile(buf, s))
246 bufferlist.release(buf);
250 // try to dispatch to last loaded buffer first
252 bool success = false;
253 if (last_loaded->dispatch(batch_command, &success)) {
258 files.clear(); // the files are already loaded
261 lyx_gui::start(batch_command, files);
267 static void error_handler(int err_sig)
269 // Throw away any signals other than the first one received.
270 static sig_atomic_t handling_error = false;
273 handling_error = true;
275 // We have received a signal indicating a fatal error, so
276 // try and save the data ASAP.
277 LyX::cref().emergencyCleanup();
279 // These lyxerr calls may or may not work:
281 // Signals are asynchronous, so the main program may be in a very
282 // fragile state when a signal is processed and thus while a signal
283 // handler function executes.
284 // In general, therefore, we should avoid performing any
285 // I/O operations or calling most library and system functions from
288 // This shouldn't matter here, however, as we've already invoked
292 lyxerr << "\nlyx: SIGHUP signal caught\nBye." << endl;
295 lyxerr << "\nlyx: SIGFPE signal caught\nBye." << endl;
298 lyxerr << "\nlyx: SIGSEGV signal caught\n"
299 "Sorry, you have found a bug in LyX. "
300 "Please read the bug-reporting instructions "
301 "in Help->Introduction and send us a bug report, "
302 "if necessary. Thanks !\nBye." << endl;
310 // Deinstall the signal handlers
311 signal(SIGHUP, SIG_DFL);
312 signal(SIGINT, SIG_DFL);
313 signal(SIGFPE, SIG_DFL);
314 signal(SIGSEGV, SIG_DFL);
315 signal(SIGTERM, SIG_DFL);
317 if (err_sig == SIGSEGV ||
318 (err_sig != SIGHUP && !GetEnv("LYXDEBUG").empty()))
319 lyx::support::abort();
326 void LyX::printError(ErrorItem const & ei)
328 std::cerr << _("LyX: ") << ei.error
329 << ':' << ei.description << std::endl;
334 void LyX::init(bool gui)
336 signal(SIGHUP, error_handler);
337 signal(SIGFPE, error_handler);
338 signal(SIGSEGV, error_handler);
339 signal(SIGINT, error_handler);
340 signal(SIGTERM, error_handler);
341 // SIGPIPE can be safely ignored.
343 bool const explicit_userdir = setLyxPaths();
345 // Check that user LyX directory is ok. We don't do that if
346 // running in batch mode.
348 queryUserLyXDir(explicit_userdir);
353 // Disable gui when easyparse says so
354 lyx_gui::use_gui = gui;
356 if (lyxrc.template_path.empty()) {
357 lyxrc.template_path = AddPath(system_lyxdir(), "templates");
360 if (lyxrc.lastfiles.empty()) {
361 lyxrc.lastfiles = AddName(user_lyxdir(), "lastfiles");
364 if (lyxrc.roman_font_name.empty())
365 lyxrc.roman_font_name = lyx_gui::roman_font_name();
366 if (lyxrc.sans_font_name.empty())
367 lyxrc.sans_font_name = lyx_gui::sans_font_name();
368 if (lyxrc.typewriter_font_name.empty())
369 lyxrc.typewriter_font_name = lyx_gui::typewriter_font_name();
372 // Read configuration files
375 readRcFile("lyxrc.defaults");
376 system_lyxrc = lyxrc;
377 system_formats = formats;
378 system_converters = converters;
379 system_movers = movers;
380 system_lcolor = lcolor;
382 string prefsfile = "preferences";
383 // back compatibility to lyxs < 1.1.6
384 if (LibFileSearch(string(), prefsfile).empty())
386 if (!LibFileSearch(string(), prefsfile).empty())
387 readRcFile(prefsfile);
389 readEncodingsFile("encodings");
390 readLanguagesFile("languages");
393 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
398 toplevel_keymap.reset(new kb_keymap);
399 defaultKeyBindings(toplevel_keymap.get());
400 toplevel_keymap->read(lyxrc.bind_file);
403 readUIFile(lyxrc.ui_file);
406 if (lyxerr.debugging(Debug::LYXRC))
409 setTmpDir(createLyXTmpDir(lyxrc.tempdir_path));
410 if (getTmpDir().empty()) {
411 Alert::error(_("Could not create temporary directory"),
412 bformat(_("Could not create a temporary directory in\n"
413 "%1$s. Make sure that this\n"
414 "path exists and is writable and try again."),
415 lyxrc.tempdir_path));
416 // createLyXTmpDir() tries sufficiently hard to create a
417 // usable temp dir, so the probability to come here is
418 // close to zero. We therefore don't try to overcome this
419 // problem with e.g. asking the user for a new path and
420 // trying again but simply exit.
424 if (lyxerr.debugging(Debug::INIT)) {
425 lyxerr << "LyX tmp dir: `" << getTmpDir() << '\'' << endl;
428 lyxerr[Debug::INIT] << "Reading lastfiles `"
429 << lyxrc.lastfiles << "'..." << endl;
430 lastfiles_.reset(new LastFiles(lyxrc.lastfiles,
431 lyxrc.check_lastfiles,
432 lyxrc.num_lastfiles));
436 void LyX::defaultKeyBindings(kb_keymap * kbmap)
438 kbmap->bind("Right", FuncRequest(LFUN_RIGHT));
439 kbmap->bind("Left", FuncRequest(LFUN_LEFT));
440 kbmap->bind("Up", FuncRequest(LFUN_UP));
441 kbmap->bind("Down", FuncRequest(LFUN_DOWN));
443 kbmap->bind("Tab", FuncRequest(LFUN_CELL_FORWARD));
444 kbmap->bind("ISO_Left_Tab", FuncRequest(LFUN_CELL_FORWARD));
446 kbmap->bind("Home", FuncRequest(LFUN_HOME));
447 kbmap->bind("End", FuncRequest(LFUN_END));
448 kbmap->bind("Prior", FuncRequest(LFUN_PRIOR));
449 kbmap->bind("Next", FuncRequest(LFUN_NEXT));
451 kbmap->bind("Return", FuncRequest(LFUN_BREAKPARAGRAPH));
452 //kbmap->bind("~C-~S-~M-nobreakspace", FuncRequest(LFUN_PROTECTEDSPACE));
454 kbmap->bind("Delete", FuncRequest(LFUN_DELETE));
455 kbmap->bind("BackSpace", FuncRequest(LFUN_BACKSPACE));
457 // kbmap->bindings to enable the use of the numeric keypad
459 //kbmap->bind("KP_0", FuncRequest(LFUN_SELFINSERT));
460 //kbmap->bind("KP_Decimal", FuncRequest(LFUN_SELFINSERT));
461 kbmap->bind("KP_Enter", FuncRequest(LFUN_BREAKPARAGRAPH));
462 //kbmap->bind("KP_1", FuncRequest(LFUN_SELFINSERT));
463 //kbmap->bind("KP_2", FuncRequest(LFUN_SELFINSERT));
464 //kbmap->bind("KP_3", FuncRequest(LFUN_SELFINSERT));
465 //kbmap->bind("KP_4", FuncRequest(LFUN_SELFINSERT));
466 //kbmap->bind("KP_5", FuncRequest(LFUN_SELFINSERT));
467 //kbmap->bind("KP_6", FuncRequest(LFUN_SELFINSERT));
468 //kbmap->bind("KP_Add", FuncRequest(LFUN_SELFINSERT));
469 //kbmap->bind("KP_7", FuncRequest(LFUN_SELFINSERT));
470 //kbmap->bind("KP_8", FuncRequest(LFUN_SELFINSERT));
471 //kbmap->bind("KP_9", FuncRequest(LFUN_SELFINSERT));
472 //kbmap->bind("KP_Divide", FuncRequest(LFUN_SELFINSERT));
473 //kbmap->bind("KP_Multiply", FuncRequest(LFUN_SELFINSERT));
474 //kbmap->bind("KP_Subtract", FuncRequest(LFUN_SELFINSERT));
475 kbmap->bind("KP_Right", FuncRequest(LFUN_RIGHT));
476 kbmap->bind("KP_Left", FuncRequest(LFUN_LEFT));
477 kbmap->bind("KP_Up", FuncRequest(LFUN_UP));
478 kbmap->bind("KP_Down", FuncRequest(LFUN_DOWN));
479 kbmap->bind("KP_Home", FuncRequest(LFUN_HOME));
480 kbmap->bind("KP_End", FuncRequest(LFUN_END));
481 kbmap->bind("KP_Prior", FuncRequest(LFUN_PRIOR));
482 kbmap->bind("KP_Next", FuncRequest(LFUN_NEXT));
484 kbmap->bind("C-Tab", FuncRequest(LFUN_CELL_SPLIT));
485 kbmap->bind("S-Tab", FuncRequest(LFUN_CELL_BACKWARD));
486 kbmap->bind("S-ISO_Left_Tab", FuncRequest(LFUN_CELL_BACKWARD));
490 void LyX::emergencyCleanup() const
492 // what to do about tmpfiles is non-obvious. we would
493 // like to delete any we find, but our lyxdir might
494 // contain documents etc. which might be helpful on
497 bufferlist.emergencyWriteAll();
499 lyxserver->emergencyCleanup();
503 void LyX::deadKeyBindings(kb_keymap * kbmap)
505 // bindKeyings for transparent handling of deadkeys
506 // The keysyms are gotten from XFree86 X11R6
507 kbmap->bind("~C-~S-~M-dead_acute", FuncRequest(LFUN_ACUTE));
508 kbmap->bind("~C-~S-~M-dead_breve", FuncRequest(LFUN_BREVE));
509 kbmap->bind("~C-~S-~M-dead_caron", FuncRequest(LFUN_CARON));
510 kbmap->bind("~C-~S-~M-dead_cedilla", FuncRequest(LFUN_CEDILLA));
511 kbmap->bind("~C-~S-~M-dead_abovering", FuncRequest(LFUN_CIRCLE));
512 kbmap->bind("~C-~S-~M-dead_circumflex", FuncRequest(LFUN_CIRCUMFLEX));
513 kbmap->bind("~C-~S-~M-dead_abovedot", FuncRequest(LFUN_DOT));
514 kbmap->bind("~C-~S-~M-dead_grave", FuncRequest(LFUN_GRAVE));
515 kbmap->bind("~C-~S-~M-dead_doubleacute", FuncRequest(LFUN_HUNG_UMLAUT));
516 kbmap->bind("~C-~S-~M-dead_macron", FuncRequest(LFUN_MACRON));
517 // nothing with this name
518 // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
519 kbmap->bind("~C-~S-~M-dead_tilde", FuncRequest(LFUN_TILDE));
520 kbmap->bind("~C-~S-~M-dead_diaeresis", FuncRequest(LFUN_UMLAUT));
521 // nothing with this name either...
522 //kbmap->bind("~C-~S-~M-dead_underbar", FuncRequest(LFUN_UNDERBAR));
523 kbmap->bind("~C-~S-~M-dead_belowdot", FuncRequest(LFUN_UNDERDOT));
524 kbmap->bind("~C-~S-~M-dead_tie", FuncRequest(LFUN_TIE));
525 kbmap->bind("~C-~S-~M-dead_ogonek",FuncRequest(LFUN_OGONEK));
529 void LyX::queryUserLyXDir(bool explicit_userdir)
531 string const configure_script = AddName(system_lyxdir(), "configure");
532 string const configure_command = "sh " + QuoteName(configure_script);
534 // Does user directory exist?
535 FileInfo fileInfo(user_lyxdir());
536 if (fileInfo.isOK() && fileInfo.isDir()) {
538 FileInfo script(configure_script);
539 FileInfo defaults(AddName(user_lyxdir(), "lyxrc.defaults"));
540 if (defaults.isOK() && script.isOK()
541 && defaults.getModificationTime() < script.getModificationTime()) {
542 lyxerr << _("LyX: reconfiguring user directory")
544 Path p(user_lyxdir());
545 ::system(configure_command.c_str());
546 lyxerr << "LyX: " << _("Done!") << endl;
551 first_start = !explicit_userdir;
553 lyxerr << bformat(_("LyX: Creating directory %1$s"
554 " and running configure..."), user_lyxdir()) << endl;
556 if (!createDirectory(user_lyxdir(), 0755)) {
557 // Failed, let's use $HOME instead.
558 user_lyxdir(os::homepath());
559 lyxerr << bformat(_("Failed. Will use %1$s instead."),
560 user_lyxdir()) << endl;
564 // Run configure in user lyx directory
565 Path p(user_lyxdir());
566 ::system(configure_command.c_str());
567 lyxerr << "LyX: " << _("Done!") << endl;
571 void LyX::readRcFile(string const & name)
573 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
575 string const lyxrc_path = LibFileSearch(string(), name);
576 if (!lyxrc_path.empty()) {
578 lyxerr[Debug::INIT] << "Found " << name
579 << " in " << lyxrc_path << endl;
581 if (lyxrc.read(lyxrc_path) >= 0)
589 // Read the ui file `name'
590 void LyX::readUIFile(string const & name)
600 struct keyword_item uitags[ui_last - 1] = {
601 { "include", ui_include },
602 { "menuset", ui_menuset },
603 { "toolbar", ui_toolbar },
604 { "toolbars", ui_toolbars }
607 // Ensure that a file is read only once (prevents include loops)
608 static std::list<string> uifiles;
609 std::list<string>::const_iterator it = uifiles.begin();
610 std::list<string>::const_iterator end = uifiles.end();
611 it = std::find(it, end, name);
613 lyxerr[Debug::INIT] << "UI file '" << name
614 << "' has been read already. "
615 << "Is this an include loop?"
620 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
622 string const ui_path = LibFileSearch("ui", name, "ui");
624 if (ui_path.empty()) {
625 lyxerr[Debug::INIT] << "Could not find " << name << endl;
629 uifiles.push_back(name);
631 lyxerr[Debug::INIT] << "Found " << name
632 << " in " << ui_path << endl;
633 LyXLex lex(uitags, ui_last - 1);
634 lex.setFile(ui_path);
636 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
640 if (lyxerr.debugging(Debug::PARSER))
641 lex.printTable(lyxerr);
647 string const file = lex.getString();
652 menubackend.read(lex);
656 toolbarbackend.read(lex);
660 toolbarbackend.readToolbars(lex);
664 if (!rtrim(lex.getString()).empty())
665 lex.printError("LyX::ReadUIFile: "
666 "Unknown menu tag: `$$Token'");
673 // Read the languages file `name'
674 void LyX::readLanguagesFile(string const & name)
676 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
678 string const lang_path = LibFileSearch(string(), name);
679 if (lang_path.empty()) {
683 languages.read(lang_path);
687 // Read the encodings file `name'
688 void LyX::readEncodingsFile(string const & name)
690 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
692 string const enc_path = LibFileSearch(string(), name);
693 if (enc_path.empty()) {
697 encodings.read(enc_path);
706 /// return the the number of arguments consumed
707 typedef boost::function<int(string const &, string const &)> cmd_helper;
709 int parse_dbg(string const & arg, string const &)
712 lyxerr << _("List of supported debug flags:") << endl;
713 Debug::showTags(lyxerr);
716 lyxerr << bformat(_("Setting debug level to %1$s"), arg) << endl;
718 lyxerr.level(Debug::value(arg));
719 Debug::showLevel(lyxerr, lyxerr.level());
724 int parse_help(string const &, string const &)
727 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
728 "Command line switches (case sensitive):\n"
729 "\t-help summarize LyX usage\n"
730 "\t-userdir dir try to set user directory to dir\n"
731 "\t-sysdir dir try to set system directory to dir\n"
732 "\t-geometry WxH+X+Y set geometry of the main window\n"
733 "\t-dbg feature[,feature]...\n"
734 " select the features to debug.\n"
735 " Type `lyx -dbg' to see the list of features\n"
736 "\t-x [--execute] command\n"
737 " where command is a lyx command.\n"
738 "\t-e [--export] fmt\n"
739 " where fmt is the export format of choice.\n"
740 "\t-i [--import] fmt file.xxx\n"
741 " where fmt is the import format of choice\n"
742 " and file.xxx is the file to be imported.\n"
743 "\t-version summarize version and build info\n"
744 "Check the LyX man page for more details.") << endl;
749 int parse_version(string const &, string const &)
751 lyxerr << "LyX " << lyx_version
752 << " of " << lyx_release_date << endl;
753 lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
755 lyxerr << lyx_version_info << endl;
760 int parse_sysdir(string const & arg, string const &)
763 lyxerr << _("Missing directory for -sysdir switch") << endl;
770 int parse_userdir(string const & arg, string const &)
773 lyxerr << _("Missing directory for -userdir switch") << endl;
780 int parse_execute(string const & arg, string const &)
783 lyxerr << _("Missing command string after --execute switch") << endl;
787 // Argh. Setting gui to false segfaults..
788 // FIXME: when ? how ?
793 int parse_export(string const & type, string const &)
796 lyxerr << _("Missing file type [eg latex, ps...] after "
797 "--export switch") << endl;
800 batch = "buffer-export " + type;
805 int parse_import(string const & type, string const & file)
808 lyxerr << _("Missing file type [eg latex, ps...] after "
809 "--import switch") << endl;
813 lyxerr << _("Missing filename for --import") << endl;
817 batch = "buffer-import " + type + ' ' + file;
824 bool LyX::easyParse(int & argc, char * argv[])
826 std::map<string, cmd_helper> cmdmap;
828 cmdmap["-dbg"] = parse_dbg;
829 cmdmap["-help"] = parse_help;
830 cmdmap["--help"] = parse_help;
831 cmdmap["-version"] = parse_version;
832 cmdmap["--version"] = parse_version;
833 cmdmap["-sysdir"] = parse_sysdir;
834 cmdmap["-userdir"] = parse_userdir;
835 cmdmap["-x"] = parse_execute;
836 cmdmap["--execute"] = parse_execute;
837 cmdmap["-e"] = parse_export;
838 cmdmap["--export"] = parse_export;
839 cmdmap["-i"] = parse_import;
840 cmdmap["--import"] = parse_import;
842 for (int i = 1; i < argc; ++i) {
843 std::map<string, cmd_helper>::const_iterator it
844 = cmdmap.find(argv[i]);
846 // don't complain if not found - may be parsed later
847 if (it == cmdmap.end())
850 string arg((i + 1 < argc) ? argv[i + 1] : "");
851 string arg2((i + 2 < argc) ? argv[i + 2] : "");
853 int const remove = 1 + it->second(arg, arg2);
855 // Now, remove used arguments by shifting
856 // the following ones remove places down.
858 for (int j = i; j < argc; ++j)
859 argv[j] = argv[j + remove];
863 batch_command = batch;