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"
35 #include "lyxtextclasslist.h"
36 #include "lyxserver.h"
37 #include "MenuBackend.h"
38 #include "ToolbarBackend.h"
40 #include "frontends/Alert.h"
41 #include "frontends/lyx_gui.h"
43 #include "support/FileInfo.h"
44 #include "support/filetools.h"
45 #include "support/lyxlib.h"
46 #include "support/os.h"
47 #include "support/path.h"
48 #include "support/path_defines.h"
50 #include <boost/bind.hpp>
55 using lyx::support::AddName;
56 using lyx::support::AddPath;
57 using lyx::support::bformat;
58 using lyx::support::createDirectory;
59 using lyx::support::CreateLyXTmpDir;
60 using lyx::support::FileInfo;
61 using lyx::support::FileSearch;
62 using lyx::support::GetEnv;
63 using lyx::support::GetEnvPath;
64 using lyx::support::i18nLibFileSearch;
65 using lyx::support::LibFileSearch;
66 using lyx::support::os;
67 using lyx::support::Path;
68 using lyx::support::rtrim;
69 using lyx::support::setLyxPaths;
70 using lyx::support::system_lyxdir;
71 using lyx::support::user_lyxdir;
77 #ifndef CXX_GLOBAL_CSTD
84 extern void QuitLyX();
86 extern LyXServer * lyxserver;
90 boost::scoped_ptr<LastFiles> lastfiles;
92 // This is the global bufferlist object
93 BufferList bufferlist;
95 // convenient to have it here.
96 boost::scoped_ptr<kb_keymap> toplevel_keymap;
100 void showFileError(string const & error)
102 Alert::warning(_("Could not read configuration file"),
103 bformat(_("Error while reading the configuration file\n%1$s.\n"
104 "Please check your installation."), error));
110 LyX::LyX(int & argc, char * argv[])
112 // Here we need to parse the command line. At least
113 // we need to parse for "-dbg" and "-help"
114 bool const want_gui = easyParse(argc, argv);
116 // set the DisplayTranslator only once; should that be done here??
117 // if this should not be in this file, please also remove
118 // #include "graphics/GraphicsTypes.h" at the top -- Rob Lahaye.
119 lyx::graphics::setDisplayTranslator();
122 lyx_gui::parse_init(argc, argv);
124 // check for any spurious extra arguments
125 // other than documents
126 for (int argi = 1; argi < argc ; ++argi) {
127 if (argv[argi][0] == '-') {
128 lyxerr << bformat(_("Wrong command line option `%1$s'. Exiting."),
134 // Initialization of LyX (reads lyxrc and more)
135 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
137 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
140 lyx_gui::parse_lyxrc();
142 vector<string> files;
144 for (int argi = argc - 1; argi >= 1; --argi)
145 files.push_back(argv[argi]);
148 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
150 // Execute batch commands if available
151 if (!batch_command.empty()) {
153 lyxerr[Debug::INIT] << "About to handle -x '"
154 << batch_command << '\'' << endl;
156 Buffer * last_loaded = 0;
158 vector<string>::const_iterator it = files.begin();
159 vector<string>::const_iterator end = files.end();
161 for (; it != end; ++it) {
162 // get absolute path of file and add ".lyx" to
163 // the filename if necessary
164 string s = FileSearch(string(), *it, "lyx");
166 last_loaded = newFile(*it, string(), true);
168 Buffer * buf = bufferlist.newBuffer(s, false);
169 buf->error.connect(boost::bind(&LyX::printError, this, _1));
170 if (loadLyXFile(buf, s))
173 bufferlist.release(buf);
177 // try to dispatch to last loaded buffer first
179 bool success = false;
180 if (last_loaded->dispatch(batch_command, &success)) {
185 files.clear(); // the files are already loaded
188 lyx_gui::start(batch_command, files);
194 static void error_handler(int err_sig)
198 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
204 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
207 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
209 "Sorry, you have found a bug in LyX. "
210 "Please read the bug-reporting instructions "
211 "in Help->Introduction and send us a bug report, "
212 "if necessary. Thanks !" << endl;
219 // Deinstall the signal handlers
220 signal(SIGHUP, SIG_DFL);
221 signal(SIGINT, SIG_DFL);
222 signal(SIGFPE, SIG_DFL);
223 signal(SIGSEGV, SIG_DFL);
224 signal(SIGTERM, SIG_DFL);
226 LyX::emergencyCleanup();
228 lyxerr << "Bye." << endl;
229 if (err_sig!= SIGHUP &&
230 (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))
231 lyx::support::abort();
238 void LyX::printError(ErrorItem const & ei)
240 std::cerr << _("LyX: ") << ei.error
241 << ':' << ei.description << std::endl;
246 void LyX::init(bool gui)
248 signal(SIGHUP, error_handler);
249 signal(SIGFPE, error_handler);
250 signal(SIGSEGV, error_handler);
251 signal(SIGINT, error_handler);
252 signal(SIGTERM, error_handler);
254 bool const explicit_userdir = setLyxPaths();
256 // Check that user LyX directory is ok. We don't do that if
257 // running in batch mode.
259 queryUserLyXDir(explicit_userdir);
264 // Disable gui when easyparse says so
265 lyx_gui::use_gui = gui;
267 if (lyxrc.template_path.empty()) {
268 lyxrc.template_path = AddPath(system_lyxdir(), "templates");
271 if (lyxrc.lastfiles.empty()) {
272 lyxrc.lastfiles = AddName(user_lyxdir(), "lastfiles");
275 if (lyxrc.roman_font_name.empty())
276 lyxrc.roman_font_name = lyx_gui::roman_font_name();
277 if (lyxrc.sans_font_name.empty())
278 lyxrc.sans_font_name = lyx_gui::sans_font_name();
279 if (lyxrc.typewriter_font_name.empty())
280 lyxrc.typewriter_font_name = lyx_gui::typewriter_font_name();
283 // Read configuration files
286 readRcFile("lyxrc.defaults");
287 system_lyxrc = lyxrc;
288 system_formats = formats;
289 system_converters = converters;
290 system_lcolor = lcolor;
292 string prefsfile = "preferences";
293 // back compatibility to lyxs < 1.1.6
294 if (LibFileSearch(string(), prefsfile).empty())
296 if (!LibFileSearch(string(), prefsfile).empty())
297 readRcFile(prefsfile);
299 readEncodingsFile("encodings");
300 readLanguagesFile("languages");
303 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
308 toplevel_keymap.reset(new kb_keymap);
309 defaultKeyBindings(toplevel_keymap.get());
310 toplevel_keymap->read(lyxrc.bind_file);
313 readUIFile(lyxrc.ui_file);
316 if (lyxerr.debugging(Debug::LYXRC))
319 os::setTmpDir(CreateLyXTmpDir(lyxrc.tempdir_path));
320 if (lyxerr.debugging(Debug::INIT)) {
321 lyxerr << "LyX tmp dir: `" << os::getTmpDir() << '\'' << endl;
324 lyxerr[Debug::INIT] << "Reading lastfiles `"
325 << lyxrc.lastfiles << "'..." << endl;
326 lastfiles.reset(new LastFiles(lyxrc.lastfiles,
327 lyxrc.check_lastfiles,
328 lyxrc.num_lastfiles));
332 void LyX::defaultKeyBindings(kb_keymap * kbmap)
334 kbmap->bind("Right", LFUN_RIGHT);
335 kbmap->bind("Left", LFUN_LEFT);
336 kbmap->bind("Up", LFUN_UP);
337 kbmap->bind("Down", LFUN_DOWN);
339 kbmap->bind("Tab", LFUN_CELL_FORWARD);
340 kbmap->bind("ISO_Left_Tab", LFUN_CELL_FORWARD); // jbl 2001-23-02
342 kbmap->bind("Home", LFUN_HOME);
343 kbmap->bind("End", LFUN_END);
344 kbmap->bind("Prior", LFUN_PRIOR);
345 kbmap->bind("Next", LFUN_NEXT);
347 kbmap->bind("Return", LFUN_BREAKPARAGRAPH);
348 //kbmap->bind("~C-~S-~M-nobreakspace", LFUN_PROTECTEDSPACE);
350 kbmap->bind("Delete", LFUN_DELETE);
351 kbmap->bind("BackSpace", LFUN_BACKSPACE);
353 // sub- and superscript -MV
354 kbmap->bind("~S-underscore", LFUN_SUBSCRIPT);
355 kbmap->bind("~S-asciicircum", LFUN_SUPERSCRIPT);
357 // kbmap->bindings to enable the use of the numeric keypad
359 //kbmap->bind("KP_0", LFUN_SELFINSERT);
360 //kbmap->bind("KP_Decimal", LFUN_SELFINSERT);
361 kbmap->bind("KP_Enter", LFUN_BREAKPARAGRAPH);
362 //kbmap->bind("KP_1", LFUN_SELFINSERT);
363 //kbmap->bind("KP_2", LFUN_SELFINSERT);
364 //kbmap->bind("KP_3", LFUN_SELFINSERT);
365 //kbmap->bind("KP_4", LFUN_SELFINSERT);
366 //kbmap->bind("KP_5", LFUN_SELFINSERT);
367 //kbmap->bind("KP_6", LFUN_SELFINSERT);
368 //kbmap->bind("KP_Add", LFUN_SELFINSERT);
369 //kbmap->bind("KP_7", LFUN_SELFINSERT);
370 //kbmap->bind("KP_8", LFUN_SELFINSERT);
371 //kbmap->bind("KP_9", LFUN_SELFINSERT);
372 //kbmap->bind("KP_Divide", LFUN_SELFINSERT);
373 //kbmap->bind("KP_Multiply", LFUN_SELFINSERT);
374 //kbmap->bind("KP_Subtract", LFUN_SELFINSERT);
375 kbmap->bind("KP_Right", LFUN_RIGHT);
376 kbmap->bind("KP_Left", LFUN_LEFT);
377 kbmap->bind("KP_Up", LFUN_UP);
378 kbmap->bind("KP_Down", LFUN_DOWN);
379 kbmap->bind("KP_Home", LFUN_HOME);
380 kbmap->bind("KP_End", LFUN_END);
381 kbmap->bind("KP_Prior", LFUN_PRIOR);
382 kbmap->bind("KP_Next", LFUN_NEXT);
384 kbmap->bind("C-Tab", LFUN_CELL_SPLIT); // ale970515
385 kbmap->bind("S-Tab", LFUN_CELL_BACKWARD); // jug20000522
386 kbmap->bind("S-ISO_Left_Tab", LFUN_CELL_BACKWARD); // jbl 2001-23-02
390 void LyX::emergencyCleanup()
392 // what to do about tmpfiles is non-obvious. we would
393 // like to delete any we find, but our lyxdir might
394 // contain documents etc. which might be helpful on
397 bufferlist.emergencyWriteAll();
399 lyxserver->emergencyCleanup();
403 void LyX::deadKeyBindings(kb_keymap * kbmap)
405 // bindKeyings for transparent handling of deadkeys
406 // The keysyms are gotten from XFree86 X11R6
407 kbmap->bind("~C-~S-~M-dead_acute", LFUN_ACUTE);
408 kbmap->bind("~C-~S-~M-dead_breve", LFUN_BREVE);
409 kbmap->bind("~C-~S-~M-dead_caron", LFUN_CARON);
410 kbmap->bind("~C-~S-~M-dead_cedilla", LFUN_CEDILLA);
411 kbmap->bind("~C-~S-~M-dead_abovering", LFUN_CIRCLE);
412 kbmap->bind("~C-~S-~M-dead_circumflex", LFUN_CIRCUMFLEX);
413 kbmap->bind("~C-~S-~M-dead_abovedot", LFUN_DOT);
414 kbmap->bind("~C-~S-~M-dead_grave", LFUN_GRAVE);
415 kbmap->bind("~C-~S-~M-dead_doubleacute", LFUN_HUNG_UMLAUT);
416 kbmap->bind("~C-~S-~M-dead_macron", LFUN_MACRON);
417 // nothing with this name
418 // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
419 kbmap->bind("~C-~S-~M-dead_tilde", LFUN_TILDE);
420 kbmap->bind("~C-~S-~M-dead_diaeresis", LFUN_UMLAUT);
421 // nothing with this name either...
422 //kbmap->bind("~C-~S-~M-dead_underbar", LFUN_UNDERBAR);
423 kbmap->bind("~C-~S-~M-dead_belowdot", LFUN_UNDERDOT);
424 kbmap->bind("~C-~S-~M-dead_tie", LFUN_TIE);
425 kbmap->bind("~C-~S-~M-dead_ogonek", LFUN_OGONEK);
429 void LyX::queryUserLyXDir(bool explicit_userdir)
431 string const configure_script = AddName(system_lyxdir(), "configure");
433 // Does user directory exist?
434 FileInfo fileInfo(user_lyxdir());
435 if (fileInfo.isOK() && fileInfo.isDir()) {
437 FileInfo script(configure_script);
438 FileInfo defaults(AddName(user_lyxdir(), "lyxrc.defaults"));
439 if (defaults.isOK() && script.isOK()
440 && defaults.getModificationTime() < script.getModificationTime()) {
441 lyxerr << _("LyX: reconfiguring user directory")
443 Path p(user_lyxdir());
444 ::system(configure_script.c_str());
445 lyxerr << "LyX: " << _("Done!") << endl;
450 first_start = !explicit_userdir;
452 lyxerr << bformat(_("LyX: Creating directory %1$s"
453 " and running configure..."), user_lyxdir()) << endl;
455 if (!createDirectory(user_lyxdir(), 0755)) {
456 // Failed, let's use $HOME instead.
457 user_lyxdir(GetEnvPath("HOME"));
458 lyxerr << bformat(_("Failed. Will use %1$s instead."),
459 user_lyxdir()) << endl;
463 // Run configure in user lyx directory
464 Path p(user_lyxdir());
465 ::system(configure_script.c_str());
466 lyxerr << "LyX: " << _("Done!") << endl;
470 void LyX::readRcFile(string const & name)
472 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
474 string const lyxrc_path = LibFileSearch(string(), name);
475 if (!lyxrc_path.empty()) {
477 lyxerr[Debug::INIT] << "Found " << name
478 << " in " << lyxrc_path << endl;
480 if (lyxrc.read(lyxrc_path) >= 0)
488 // Read the ui file `name'
489 void LyX::readUIFile(string const & name)
499 struct keyword_item uitags[ui_last - 1] = {
500 { "include", ui_include },
501 { "menuset", ui_menuset },
502 { "toolbar", ui_toolbar },
503 { "toolbars", ui_toolbars }
506 // Ensure that a file is read only once (prevents include loops)
507 static std::list<string> uifiles;
508 std::list<string>::const_iterator it = uifiles.begin();
509 std::list<string>::const_iterator end = uifiles.end();
510 it = std::find(it, end, name);
512 lyxerr[Debug::INIT] << "UI file '" << name
513 << "' has been read already. "
514 << "Is this an include loop?"
519 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
521 string const ui_path = LibFileSearch("ui", name, "ui");
523 if (ui_path.empty()) {
524 lyxerr[Debug::INIT] << "Could not find " << name << endl;
528 uifiles.push_back(name);
530 lyxerr[Debug::INIT] << "Found " << name
531 << " in " << ui_path << endl;
532 LyXLex lex(uitags, ui_last - 1);
533 lex.setFile(ui_path);
535 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
539 if (lyxerr.debugging(Debug::PARSER))
540 lex.printTable(lyxerr);
546 string const file = lex.getString();
551 menubackend.read(lex);
555 toolbarbackend.read(lex);
559 toolbarbackend.readToolbars(lex);
563 if (!rtrim(lex.getString()).empty())
564 lex.printError("LyX::ReadUIFile: "
565 "Unknown menu tag: `$$Token'");
572 // Read the languages file `name'
573 void LyX::readLanguagesFile(string const & name)
575 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
577 string const lang_path = LibFileSearch(string(), name);
578 if (lang_path.empty()) {
582 languages.read(lang_path);
586 // Read the encodings file `name'
587 void LyX::readEncodingsFile(string const & name)
589 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
591 string const enc_path = LibFileSearch(string(), name);
592 if (enc_path.empty()) {
596 encodings.read(enc_path);
605 /// return the the number of arguments consumed
606 typedef boost::function<int(string const &, string const &)> cmd_helper;
608 int parse_dbg(string const & arg, string const &)
611 lyxerr << _("List of supported debug flags:") << endl;
612 Debug::showTags(lyxerr);
615 lyxerr << bformat(_("Setting debug level to %1$s"), arg) << endl;
617 lyxerr.level(Debug::value(arg));
618 Debug::showLevel(lyxerr, lyxerr.level());
623 int parse_help(string const &, string const &)
626 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
627 "Command line switches (case sensitive):\n"
628 "\t-help summarize LyX usage\n"
629 "\t-userdir dir try to set user directory to dir\n"
630 "\t-sysdir dir try to set system directory to dir\n"
631 "\t-geometry WxH+X+Y set geometry of the main window\n"
632 "\t-dbg feature[,feature]...\n"
633 " select the features to debug.\n"
634 " Type `lyx -dbg' to see the list of features\n"
635 "\t-x [--execute] command\n"
636 " where command is a lyx command.\n"
637 "\t-e [--export] fmt\n"
638 " where fmt is the export format of choice.\n"
639 "\t-i [--import] fmt file.xxx\n"
640 " where fmt is the import format of choice\n"
641 " and file.xxx is the file to be imported.\n"
642 "\t-version summarize version and build info\n"
643 "Check the LyX man page for more details.") << endl;
648 int parse_version(string const &, string const &)
650 lyxerr << "LyX " << lyx_version
651 << " of " << lyx_release_date << endl;
652 lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
654 lyxerr << lyx_version_info << endl;
659 int parse_sysdir(string const & arg, string const &)
662 lyxerr << _("Missing directory for -sysdir switch") << endl;
669 int parse_userdir(string const & arg, string const &)
672 lyxerr << _("Missing directory for -userdir switch") << endl;
679 int parse_execute(string const & arg, string const &)
682 lyxerr << _("Missing command string after --execute switch") << endl;
686 // Argh. Setting gui to false segfaults..
687 // FIXME: when ? how ?
692 int parse_export(string const & type, string const &)
695 lyxerr << _("Missing file type [eg latex, ps...] after "
696 "--export switch") << endl;
699 batch = "buffer-export " + type;
704 int parse_import(string const & type, string const & file)
707 lyxerr << _("Missing file type [eg latex, ps...] after "
708 "--import switch") << endl;
712 lyxerr << _("Missing filename for --import") << endl;
716 batch = "buffer-import " + type + ' ' + file;
723 bool LyX::easyParse(int & argc, char * argv[])
725 std::map<string, cmd_helper> cmdmap;
727 cmdmap["-dbg"] = parse_dbg;
728 cmdmap["-help"] = parse_help;
729 cmdmap["--help"] = parse_help;
730 cmdmap["-version"] = parse_version;
731 cmdmap["--version"] = parse_version;
732 cmdmap["-sysdir"] = parse_sysdir;
733 cmdmap["-userdir"] = parse_userdir;
734 cmdmap["-x"] = parse_execute;
735 cmdmap["--execute"] = parse_execute;
736 cmdmap["-e"] = parse_export;
737 cmdmap["--export"] = parse_export;
738 cmdmap["-i"] = parse_import;
739 cmdmap["--import"] = parse_import;
741 for (int i = 1; i < argc; ++i) {
742 std::map<string, cmd_helper>::const_iterator it
743 = cmdmap.find(argv[i]);
745 // don't complain if not found - may be parsed later
746 if (it == cmdmap.end())
749 string arg((i + 1 < argc) ? argv[i + 1] : "");
750 string arg2((i + 2 < argc) ? argv[i + 2] : "");
752 int const remove = 1 + it->second(arg, arg2);
754 // Now, remove used arguments by shifting
755 // the following ones remove places down.
757 for (int j = i; j < argc; ++j)
758 argv[j] = argv[j + remove];
762 batch_command = batch;