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>
54 using namespace lyx::support;
59 #ifndef CXX_GLOBAL_CSTD
65 extern void QuitLyX();
67 extern LyXServer * lyxserver;
71 boost::scoped_ptr<LastFiles> lastfiles;
73 // This is the global bufferlist object
74 BufferList bufferlist;
76 // convenient to have it here.
77 boost::scoped_ptr<kb_keymap> toplevel_keymap;
81 void showFileError(string const & error)
83 Alert::warning(_("Could not read configuration file"),
84 bformat(_("Error while reading the configuration file\n%1$s.\n"
85 "Please check your installation."), error));
91 LyX::LyX(int & argc, char * argv[])
93 // Here we need to parse the command line. At least
94 // we need to parse for "-dbg" and "-help"
95 bool const want_gui = easyParse(argc, argv);
97 // set the DisplayTranslator only once; should that be done here??
98 // if this should not be in this file, please also remove
99 // #include "graphics/GraphicsTypes.h" at the top -- Rob Lahaye.
100 lyx::graphics::setDisplayTranslator();
103 lyx_gui::parse_init(argc, argv);
105 // check for any spurious extra arguments
106 // other than documents
107 for (int argi = 1; argi < argc ; ++argi) {
108 if (argv[argi][0] == '-') {
109 lyxerr << bformat(_("Wrong command line option `%1$s'. Exiting."),
115 // Initialization of LyX (reads lyxrc and more)
116 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
118 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
121 lyx_gui::parse_lyxrc();
123 vector<string> files;
125 for (int argi = argc - 1; argi >= 1; --argi)
126 files.push_back(argv[argi]);
129 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
131 // Execute batch commands if available
132 if (!batch_command.empty()) {
134 lyxerr[Debug::INIT] << "About to handle -x '"
135 << batch_command << '\'' << endl;
137 Buffer * last_loaded = 0;
139 vector<string>::const_iterator it = files.begin();
140 vector<string>::const_iterator end = files.end();
142 for (; it != end; ++it) {
143 // get absolute path of file and add ".lyx" to
144 // the filename if necessary
145 string s = FileSearch(string(), *it, "lyx");
147 last_loaded = newFile(*it, string(), true);
149 Buffer * buf = bufferlist.newBuffer(s, false);
150 buf->error.connect(boost::bind(&LyX::printError, this, _1));
151 if (loadLyXFile(buf, s))
154 bufferlist.release(buf);
158 // try to dispatch to last loaded buffer first
160 bool success = false;
161 if (last_loaded->dispatch(batch_command, &success)) {
166 files.clear(); // the files are already loaded
169 lyx_gui::start(batch_command, files);
175 static void error_handler(int err_sig)
179 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
185 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
188 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
190 "Sorry, you have found a bug in LyX. "
191 "Please read the bug-reporting instructions "
192 "in Help->Introduction and send us a bug report, "
193 "if necessary. Thanks !" << endl;
200 // Deinstall the signal handlers
201 signal(SIGHUP, SIG_DFL);
202 signal(SIGINT, SIG_DFL);
203 signal(SIGFPE, SIG_DFL);
204 signal(SIGSEGV, SIG_DFL);
205 signal(SIGTERM, SIG_DFL);
207 LyX::emergencyCleanup();
209 lyxerr << "Bye." << endl;
210 if (err_sig!= SIGHUP &&
211 (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))
212 lyx::support::abort();
219 void LyX::printError(ErrorItem const & ei)
221 std::cerr << _("LyX: ") << ei.error
222 << ':' << ei.description << std::endl;
227 void LyX::init(bool gui)
229 signal(SIGHUP, error_handler);
230 signal(SIGFPE, error_handler);
231 signal(SIGSEGV, error_handler);
232 signal(SIGINT, error_handler);
233 signal(SIGTERM, error_handler);
235 bool const explicit_userdir = setLyxPaths();
237 // Check that user LyX directory is ok. We don't do that if
238 // running in batch mode.
240 queryUserLyXDir(explicit_userdir);
245 // Disable gui when easyparse says so
246 lyx_gui::use_gui = gui;
248 if (lyxrc.template_path.empty()) {
249 lyxrc.template_path = AddPath(system_lyxdir(), "templates");
252 if (lyxrc.lastfiles.empty()) {
253 lyxrc.lastfiles = AddName(user_lyxdir(), "lastfiles");
256 if (lyxrc.roman_font_name.empty())
257 lyxrc.roman_font_name = lyx_gui::roman_font_name();
258 if (lyxrc.sans_font_name.empty())
259 lyxrc.sans_font_name = lyx_gui::sans_font_name();
260 if (lyxrc.typewriter_font_name.empty())
261 lyxrc.typewriter_font_name = lyx_gui::typewriter_font_name();
264 // Read configuration files
267 readRcFile("lyxrc.defaults");
268 system_lyxrc = lyxrc;
269 system_formats = formats;
270 system_converters = converters;
271 system_lcolor = lcolor;
273 string prefsfile = "preferences";
274 // back compatibility to lyxs < 1.1.6
275 if (LibFileSearch(string(), prefsfile).empty())
277 if (!LibFileSearch(string(), prefsfile).empty())
278 readRcFile(prefsfile);
280 readEncodingsFile("encodings");
281 readLanguagesFile("languages");
284 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
289 toplevel_keymap.reset(new kb_keymap);
290 defaultKeyBindings(toplevel_keymap.get());
291 toplevel_keymap->read(lyxrc.bind_file);
294 readUIFile(lyxrc.ui_file);
297 if (lyxerr.debugging(Debug::LYXRC))
300 os::setTmpDir(CreateLyXTmpDir(lyxrc.tempdir_path));
301 if (lyxerr.debugging(Debug::INIT)) {
302 lyxerr << "LyX tmp dir: `" << os::getTmpDir() << '\'' << endl;
305 lyxerr[Debug::INIT] << "Reading lastfiles `"
306 << lyxrc.lastfiles << "'..." << endl;
307 lastfiles.reset(new LastFiles(lyxrc.lastfiles,
308 lyxrc.check_lastfiles,
309 lyxrc.num_lastfiles));
313 void LyX::defaultKeyBindings(kb_keymap * kbmap)
315 kbmap->bind("Right", LFUN_RIGHT);
316 kbmap->bind("Left", LFUN_LEFT);
317 kbmap->bind("Up", LFUN_UP);
318 kbmap->bind("Down", LFUN_DOWN);
320 kbmap->bind("Tab", LFUN_CELL_FORWARD);
321 kbmap->bind("ISO_Left_Tab", LFUN_CELL_FORWARD); // jbl 2001-23-02
323 kbmap->bind("Home", LFUN_HOME);
324 kbmap->bind("End", LFUN_END);
325 kbmap->bind("Prior", LFUN_PRIOR);
326 kbmap->bind("Next", LFUN_NEXT);
328 kbmap->bind("Return", LFUN_BREAKPARAGRAPH);
329 //kbmap->bind("~C-~S-~M-nobreakspace", LFUN_PROTECTEDSPACE);
331 kbmap->bind("Delete", LFUN_DELETE);
332 kbmap->bind("BackSpace", LFUN_BACKSPACE);
334 // sub- and superscript -MV
335 kbmap->bind("~S-underscore", LFUN_SUBSCRIPT);
336 kbmap->bind("~S-asciicircum", LFUN_SUPERSCRIPT);
338 // kbmap->bindings to enable the use of the numeric keypad
340 //kbmap->bind("KP_0", LFUN_SELFINSERT);
341 //kbmap->bind("KP_Decimal", LFUN_SELFINSERT);
342 kbmap->bind("KP_Enter", LFUN_BREAKPARAGRAPH);
343 //kbmap->bind("KP_1", LFUN_SELFINSERT);
344 //kbmap->bind("KP_2", LFUN_SELFINSERT);
345 //kbmap->bind("KP_3", LFUN_SELFINSERT);
346 //kbmap->bind("KP_4", LFUN_SELFINSERT);
347 //kbmap->bind("KP_5", LFUN_SELFINSERT);
348 //kbmap->bind("KP_6", LFUN_SELFINSERT);
349 //kbmap->bind("KP_Add", LFUN_SELFINSERT);
350 //kbmap->bind("KP_7", LFUN_SELFINSERT);
351 //kbmap->bind("KP_8", LFUN_SELFINSERT);
352 //kbmap->bind("KP_9", LFUN_SELFINSERT);
353 //kbmap->bind("KP_Divide", LFUN_SELFINSERT);
354 //kbmap->bind("KP_Multiply", LFUN_SELFINSERT);
355 //kbmap->bind("KP_Subtract", LFUN_SELFINSERT);
356 kbmap->bind("KP_Right", LFUN_RIGHT);
357 kbmap->bind("KP_Left", LFUN_LEFT);
358 kbmap->bind("KP_Up", LFUN_UP);
359 kbmap->bind("KP_Down", LFUN_DOWN);
360 kbmap->bind("KP_Home", LFUN_HOME);
361 kbmap->bind("KP_End", LFUN_END);
362 kbmap->bind("KP_Prior", LFUN_PRIOR);
363 kbmap->bind("KP_Next", LFUN_NEXT);
365 kbmap->bind("C-Tab", LFUN_CELL_SPLIT); // ale970515
366 kbmap->bind("S-Tab", LFUN_CELL_BACKWARD); // jug20000522
367 kbmap->bind("S-ISO_Left_Tab", LFUN_CELL_BACKWARD); // jbl 2001-23-02
371 void LyX::emergencyCleanup()
373 // what to do about tmpfiles is non-obvious. we would
374 // like to delete any we find, but our lyxdir might
375 // contain documents etc. which might be helpful on
378 bufferlist.emergencyWriteAll();
380 lyxserver->emergencyCleanup();
384 void LyX::deadKeyBindings(kb_keymap * kbmap)
386 // bindKeyings for transparent handling of deadkeys
387 // The keysyms are gotten from XFree86 X11R6
388 kbmap->bind("~C-~S-~M-dead_acute", LFUN_ACUTE);
389 kbmap->bind("~C-~S-~M-dead_breve", LFUN_BREVE);
390 kbmap->bind("~C-~S-~M-dead_caron", LFUN_CARON);
391 kbmap->bind("~C-~S-~M-dead_cedilla", LFUN_CEDILLA);
392 kbmap->bind("~C-~S-~M-dead_abovering", LFUN_CIRCLE);
393 kbmap->bind("~C-~S-~M-dead_circumflex", LFUN_CIRCUMFLEX);
394 kbmap->bind("~C-~S-~M-dead_abovedot", LFUN_DOT);
395 kbmap->bind("~C-~S-~M-dead_grave", LFUN_GRAVE);
396 kbmap->bind("~C-~S-~M-dead_doubleacute", LFUN_HUNG_UMLAUT);
397 kbmap->bind("~C-~S-~M-dead_macron", LFUN_MACRON);
398 // nothing with this name
399 // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
400 kbmap->bind("~C-~S-~M-dead_tilde", LFUN_TILDE);
401 kbmap->bind("~C-~S-~M-dead_diaeresis", LFUN_UMLAUT);
402 // nothing with this name either...
403 //kbmap->bind("~C-~S-~M-dead_underbar", LFUN_UNDERBAR);
404 kbmap->bind("~C-~S-~M-dead_belowdot", LFUN_UNDERDOT);
405 kbmap->bind("~C-~S-~M-dead_tie", LFUN_TIE);
406 kbmap->bind("~C-~S-~M-dead_ogonek", LFUN_OGONEK);
410 void LyX::queryUserLyXDir(bool explicit_userdir)
412 string const configure_script = AddName(system_lyxdir(), "configure");
414 // Does user directory exist?
415 FileInfo fileInfo(user_lyxdir());
416 if (fileInfo.isOK() && fileInfo.isDir()) {
418 FileInfo script(configure_script);
419 FileInfo defaults(AddName(user_lyxdir(), "lyxrc.defaults"));
420 if (defaults.isOK() && script.isOK()
421 && defaults.getModificationTime() < script.getModificationTime()) {
422 lyxerr << _("LyX: reconfiguring user directory")
424 Path p(user_lyxdir());
425 ::system(configure_script.c_str());
426 lyxerr << "LyX: " << _("Done!") << endl;
431 first_start = !explicit_userdir;
433 lyxerr << bformat(_("LyX: Creating directory %1$s"
434 " and running configure..."), user_lyxdir()) << endl;
436 if (!createDirectory(user_lyxdir(), 0755)) {
437 // Failed, let's use $HOME instead.
438 user_lyxdir(GetEnvPath("HOME"));
439 lyxerr << bformat(_("Failed. Will use %1$s instead."),
440 user_lyxdir()) << endl;
444 // Run configure in user lyx directory
445 Path p(user_lyxdir());
446 ::system(configure_script.c_str());
447 lyxerr << "LyX: " << _("Done!") << endl;
451 void LyX::readRcFile(string const & name)
453 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
455 string const lyxrc_path = LibFileSearch(string(), name);
456 if (!lyxrc_path.empty()) {
458 lyxerr[Debug::INIT] << "Found " << name
459 << " in " << lyxrc_path << endl;
461 if (lyxrc.read(lyxrc_path) >= 0)
469 // Read the ui file `name'
470 void LyX::readUIFile(string const & name)
480 struct keyword_item uitags[ui_last - 1] = {
481 { "include", ui_include },
482 { "menuset", ui_menuset },
483 { "toolbar", ui_toolbar },
484 { "toolbars", ui_toolbars }
487 // Ensure that a file is read only once (prevents include loops)
488 static std::list<string> uifiles;
489 std::list<string>::const_iterator it = uifiles.begin();
490 std::list<string>::const_iterator end = uifiles.end();
491 it = std::find(it, end, name);
493 lyxerr[Debug::INIT] << "UI file '" << name
494 << "' has been read already. "
495 << "Is this an include loop?"
500 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
502 string const ui_path = LibFileSearch("ui", name, "ui");
504 if (ui_path.empty()) {
505 lyxerr[Debug::INIT] << "Could not find " << name << endl;
509 uifiles.push_back(name);
511 lyxerr[Debug::INIT] << "Found " << name
512 << " in " << ui_path << endl;
513 LyXLex lex(uitags, ui_last - 1);
514 lex.setFile(ui_path);
516 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
520 if (lyxerr.debugging(Debug::PARSER))
521 lex.printTable(lyxerr);
527 string const file = lex.getString();
532 menubackend.read(lex);
536 toolbarbackend.read(lex);
540 toolbarbackend.readToolbars(lex);
544 if (!rtrim(lex.getString()).empty())
545 lex.printError("LyX::ReadUIFile: "
546 "Unknown menu tag: `$$Token'");
553 // Read the languages file `name'
554 void LyX::readLanguagesFile(string const & name)
556 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
558 string const lang_path = LibFileSearch(string(), name);
559 if (lang_path.empty()) {
563 languages.read(lang_path);
567 // Read the encodings file `name'
568 void LyX::readEncodingsFile(string const & name)
570 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
572 string const enc_path = LibFileSearch(string(), name);
573 if (enc_path.empty()) {
577 encodings.read(enc_path);
586 /// return the the number of arguments consumed
587 typedef boost::function<int(string const &, string const &)> cmd_helper;
589 int parse_dbg(string const & arg, string const &)
592 lyxerr << _("List of supported debug flags:") << endl;
593 Debug::showTags(lyxerr);
596 lyxerr << bformat(_("Setting debug level to %1$s"), arg) << endl;
598 lyxerr.level(Debug::value(arg));
599 Debug::showLevel(lyxerr, lyxerr.level());
604 int parse_help(string const &, string const &)
607 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
608 "Command line switches (case sensitive):\n"
609 "\t-help summarize LyX usage\n"
610 "\t-userdir dir try to set user directory to dir\n"
611 "\t-sysdir dir try to set system directory to dir\n"
612 "\t-geometry WxH+X+Y set geometry of the main window\n"
613 "\t-dbg feature[,feature]...\n"
614 " select the features to debug.\n"
615 " Type `lyx -dbg' to see the list of features\n"
616 "\t-x [--execute] command\n"
617 " where command is a lyx command.\n"
618 "\t-e [--export] fmt\n"
619 " where fmt is the export format of choice.\n"
620 "\t-i [--import] fmt file.xxx\n"
621 " where fmt is the import format of choice\n"
622 " and file.xxx is the file to be imported.\n"
623 "\t-version summarize version and build info\n"
624 "Check the LyX man page for more details.") << endl;
629 int parse_version(string const &, string const &)
631 lyxerr << "LyX " << lyx_version
632 << " of " << lyx_release_date << endl;
633 lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
635 lyxerr << lyx_version_info << endl;
640 int parse_sysdir(string const & arg, string const &)
643 lyxerr << _("Missing directory for -sysdir switch") << endl;
650 int parse_userdir(string const & arg, string const &)
653 lyxerr << _("Missing directory for -userdir switch") << endl;
660 int parse_execute(string const & arg, string const &)
663 lyxerr << _("Missing command string after --execute switch") << endl;
667 // Argh. Setting gui to false segfaults..
668 // FIXME: when ? how ?
673 int parse_export(string const & type, string const &)
676 lyxerr << _("Missing file type [eg latex, ps...] after "
677 "--export switch") << endl;
680 batch = "buffer-export " + type;
685 int parse_import(string const & type, string const & file)
688 lyxerr << _("Missing file type [eg latex, ps...] after "
689 "--import switch") << endl;
693 lyxerr << _("Missing filename for --import") << endl;
697 batch = "buffer-import " + type + ' ' + file;
704 bool LyX::easyParse(int & argc, char * argv[])
706 std::map<string, cmd_helper> cmdmap;
708 cmdmap["-dbg"] = parse_dbg;
709 cmdmap["-help"] = parse_help;
710 cmdmap["--help"] = parse_help;
711 cmdmap["-version"] = parse_version;
712 cmdmap["--version"] = parse_version;
713 cmdmap["-sysdir"] = parse_sysdir;
714 cmdmap["-userdir"] = parse_userdir;
715 cmdmap["-x"] = parse_execute;
716 cmdmap["--execute"] = parse_execute;
717 cmdmap["-e"] = parse_export;
718 cmdmap["--export"] = parse_export;
719 cmdmap["-i"] = parse_import;
720 cmdmap["--import"] = parse_import;
722 for (int i = 1; i < argc; ++i) {
723 std::map<string, cmd_helper>::const_iterator it
724 = cmdmap.find(argv[i]);
726 // don't complain if not found - may be parsed later
727 if (it == cmdmap.end())
730 string arg((i + 1 < argc) ? argv[i + 1] : "");
731 string arg2((i + 2 < argc) ? argv[i + 2] : "");
733 int const remove = 1 + it->second(arg, arg2);
735 // Now, remove used arguments by shifting
736 // the following ones remove places down.
738 for (int j = i; j < argc; ++j)
739 argv[j] = argv[j + remove];
743 batch_command = batch;