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 "frontends/Alert.h"
42 #include "frontends/lyx_gui.h"
44 #include "support/FileInfo.h"
45 #include "support/filetools.h"
46 #include "support/lyxlib.h"
47 #include "support/os.h"
48 #include "support/path.h"
49 #include "support/path_defines.h"
51 #include <boost/bind.hpp>
56 using lyx::support::AddName;
57 using lyx::support::AddPath;
58 using lyx::support::bformat;
59 using lyx::support::createDirectory;
60 using lyx::support::CreateLyXTmpDir;
61 using lyx::support::FileInfo;
62 using lyx::support::FileSearch;
63 using lyx::support::GetEnv;
64 using lyx::support::GetEnvPath;
65 using lyx::support::i18nLibFileSearch;
66 using lyx::support::LibFileSearch;
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;
73 namespace os = lyx::support::os;
79 #ifndef CXX_GLOBAL_CSTD
86 extern void QuitLyX();
88 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);
117 lyx_gui::parse_init(argc, argv);
119 // check for any spurious extra arguments
120 // other than documents
121 for (int argi = 1; argi < argc ; ++argi) {
122 if (argv[argi][0] == '-') {
123 lyxerr << bformat(_("Wrong command line option `%1$s'. Exiting."),
129 // Initialization of LyX (reads lyxrc and more)
130 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
132 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
135 lyx_gui::parse_lyxrc();
137 vector<string> files;
139 for (int argi = argc - 1; argi >= 1; --argi)
140 files.push_back(argv[argi]);
143 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
145 // Execute batch commands if available
146 if (!batch_command.empty()) {
148 lyxerr[Debug::INIT] << "About to handle -x '"
149 << batch_command << '\'' << endl;
151 Buffer * last_loaded = 0;
153 vector<string>::const_iterator it = files.begin();
154 vector<string>::const_iterator end = files.end();
156 for (; it != end; ++it) {
157 // get absolute path of file and add ".lyx" to
158 // the filename if necessary
159 string s = FileSearch(string(), *it, "lyx");
161 last_loaded = newFile(*it, string(), true);
163 Buffer * buf = bufferlist.newBuffer(s, false);
164 buf->error.connect(boost::bind(&LyX::printError, this, _1));
165 if (loadLyXFile(buf, s))
168 bufferlist.release(buf);
172 // try to dispatch to last loaded buffer first
174 bool success = false;
175 if (last_loaded->dispatch(batch_command, &success)) {
180 files.clear(); // the files are already loaded
183 lyx_gui::start(batch_command, files);
189 static void error_handler(int err_sig)
193 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
199 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
202 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
204 "Sorry, you have found a bug in LyX. "
205 "Please read the bug-reporting instructions "
206 "in Help->Introduction and send us a bug report, "
207 "if necessary. Thanks !" << endl;
213 // This will be received if lyx tries to write to a socket
214 // whose reading end was closed. It can safely be ignored,
215 // as in this case the ::write() system call will return -1
216 // and errno will be set to EPIPE
221 // Deinstall the signal handlers
222 signal(SIGHUP, SIG_DFL);
223 signal(SIGINT, SIG_DFL);
224 signal(SIGFPE, SIG_DFL);
225 signal(SIGSEGV, SIG_DFL);
226 signal(SIGTERM, SIG_DFL);
227 signal(SIGPIPE, SIG_DFL);
229 LyX::emergencyCleanup();
231 lyxerr << "Bye." << endl;
232 if (err_sig!= SIGHUP &&
233 (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))
234 lyx::support::abort();
241 void LyX::printError(ErrorItem const & ei)
243 std::cerr << _("LyX: ") << ei.error
244 << ':' << ei.description << std::endl;
249 void LyX::init(bool gui)
251 signal(SIGHUP, error_handler);
252 signal(SIGFPE, error_handler);
253 signal(SIGSEGV, error_handler);
254 signal(SIGINT, error_handler);
255 signal(SIGTERM, error_handler);
256 signal(SIGPIPE, error_handler);
258 bool const explicit_userdir = setLyxPaths();
260 // Check that user LyX directory is ok. We don't do that if
261 // running in batch mode.
263 queryUserLyXDir(explicit_userdir);
268 // Disable gui when easyparse says so
269 lyx_gui::use_gui = gui;
271 if (lyxrc.template_path.empty()) {
272 lyxrc.template_path = AddPath(system_lyxdir(), "templates");
275 if (lyxrc.lastfiles.empty()) {
276 lyxrc.lastfiles = AddName(user_lyxdir(), "lastfiles");
279 if (lyxrc.roman_font_name.empty())
280 lyxrc.roman_font_name = lyx_gui::roman_font_name();
281 if (lyxrc.sans_font_name.empty())
282 lyxrc.sans_font_name = lyx_gui::sans_font_name();
283 if (lyxrc.typewriter_font_name.empty())
284 lyxrc.typewriter_font_name = lyx_gui::typewriter_font_name();
287 // Read configuration files
290 readRcFile("lyxrc.defaults");
291 system_lyxrc = lyxrc;
292 system_formats = formats;
293 system_converters = converters;
294 system_lcolor = lcolor;
296 string prefsfile = "preferences";
297 // back compatibility to lyxs < 1.1.6
298 if (LibFileSearch(string(), prefsfile).empty())
300 if (!LibFileSearch(string(), prefsfile).empty())
301 readRcFile(prefsfile);
303 readEncodingsFile("encodings");
304 readLanguagesFile("languages");
307 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
312 toplevel_keymap.reset(new kb_keymap);
313 defaultKeyBindings(toplevel_keymap.get());
314 toplevel_keymap->read(lyxrc.bind_file);
317 readUIFile(lyxrc.ui_file);
320 if (lyxerr.debugging(Debug::LYXRC))
323 os::setTmpDir(CreateLyXTmpDir(lyxrc.tempdir_path));
324 if (lyxerr.debugging(Debug::INIT)) {
325 lyxerr << "LyX tmp dir: `" << os::getTmpDir() << '\'' << endl;
328 lyxerr[Debug::INIT] << "Reading lastfiles `"
329 << lyxrc.lastfiles << "'..." << endl;
330 lastfiles.reset(new LastFiles(lyxrc.lastfiles,
331 lyxrc.check_lastfiles,
332 lyxrc.num_lastfiles));
336 void LyX::defaultKeyBindings(kb_keymap * kbmap)
338 kbmap->bind("Right", FuncRequest(LFUN_RIGHT));
339 kbmap->bind("Left", FuncRequest(LFUN_LEFT));
340 kbmap->bind("Up", FuncRequest(LFUN_UP));
341 kbmap->bind("Down", FuncRequest(LFUN_DOWN));
343 kbmap->bind("Tab", FuncRequest(LFUN_CELL_FORWARD));
344 kbmap->bind("ISO_Left_Tab", FuncRequest(LFUN_CELL_FORWARD));
346 kbmap->bind("Home", FuncRequest(LFUN_HOME));
347 kbmap->bind("End", FuncRequest(LFUN_END));
348 kbmap->bind("Prior", FuncRequest(LFUN_PRIOR));
349 kbmap->bind("Next", FuncRequest(LFUN_NEXT));
351 kbmap->bind("Return", FuncRequest(LFUN_BREAKPARAGRAPH));
352 //kbmap->bind("~C-~S-~M-nobreakspace", FuncRequest(LFUN_PROTECTEDSPACE));
354 kbmap->bind("Delete", FuncRequest(LFUN_DELETE));
355 kbmap->bind("BackSpace", FuncRequest(LFUN_BACKSPACE));
357 // sub- and superscript -MV
358 kbmap->bind("~S-underscore", FuncRequest(LFUN_SUBSCRIPT));
359 kbmap->bind("~S-asciicircum", FuncRequest(LFUN_SUPERSCRIPT));
361 // kbmap->bindings to enable the use of the numeric keypad
363 //kbmap->bind("KP_0", FuncRequest(LFUN_SELFINSERT));
364 //kbmap->bind("KP_Decimal", FuncRequest(LFUN_SELFINSERT));
365 kbmap->bind("KP_Enter", FuncRequest(LFUN_BREAKPARAGRAPH));
366 //kbmap->bind("KP_1", FuncRequest(LFUN_SELFINSERT));
367 //kbmap->bind("KP_2", FuncRequest(LFUN_SELFINSERT));
368 //kbmap->bind("KP_3", FuncRequest(LFUN_SELFINSERT));
369 //kbmap->bind("KP_4", FuncRequest(LFUN_SELFINSERT));
370 //kbmap->bind("KP_5", FuncRequest(LFUN_SELFINSERT));
371 //kbmap->bind("KP_6", FuncRequest(LFUN_SELFINSERT));
372 //kbmap->bind("KP_Add", FuncRequest(LFUN_SELFINSERT));
373 //kbmap->bind("KP_7", FuncRequest(LFUN_SELFINSERT));
374 //kbmap->bind("KP_8", FuncRequest(LFUN_SELFINSERT));
375 //kbmap->bind("KP_9", FuncRequest(LFUN_SELFINSERT));
376 //kbmap->bind("KP_Divide", FuncRequest(LFUN_SELFINSERT));
377 //kbmap->bind("KP_Multiply", FuncRequest(LFUN_SELFINSERT));
378 //kbmap->bind("KP_Subtract", FuncRequest(LFUN_SELFINSERT));
379 kbmap->bind("KP_Right", FuncRequest(LFUN_RIGHT));
380 kbmap->bind("KP_Left", FuncRequest(LFUN_LEFT));
381 kbmap->bind("KP_Up", FuncRequest(LFUN_UP));
382 kbmap->bind("KP_Down", FuncRequest(LFUN_DOWN));
383 kbmap->bind("KP_Home", FuncRequest(LFUN_HOME));
384 kbmap->bind("KP_End", FuncRequest(LFUN_END));
385 kbmap->bind("KP_Prior", FuncRequest(LFUN_PRIOR));
386 kbmap->bind("KP_Next", FuncRequest(LFUN_NEXT));
388 kbmap->bind("C-Tab", FuncRequest(LFUN_CELL_SPLIT));
389 kbmap->bind("S-Tab", FuncRequest(LFUN_CELL_BACKWARD));
390 kbmap->bind("S-ISO_Left_Tab", FuncRequest(LFUN_CELL_BACKWARD));
394 void LyX::emergencyCleanup()
396 // what to do about tmpfiles is non-obvious. we would
397 // like to delete any we find, but our lyxdir might
398 // contain documents etc. which might be helpful on
401 bufferlist.emergencyWriteAll();
403 lyxserver->emergencyCleanup();
407 void LyX::deadKeyBindings(kb_keymap * kbmap)
409 // bindKeyings for transparent handling of deadkeys
410 // The keysyms are gotten from XFree86 X11R6
411 kbmap->bind("~C-~S-~M-dead_acute", FuncRequest(LFUN_ACUTE));
412 kbmap->bind("~C-~S-~M-dead_breve", FuncRequest(LFUN_BREVE));
413 kbmap->bind("~C-~S-~M-dead_caron", FuncRequest(LFUN_CARON));
414 kbmap->bind("~C-~S-~M-dead_cedilla", FuncRequest(LFUN_CEDILLA));
415 kbmap->bind("~C-~S-~M-dead_abovering", FuncRequest(LFUN_CIRCLE));
416 kbmap->bind("~C-~S-~M-dead_circumflex", FuncRequest(LFUN_CIRCUMFLEX));
417 kbmap->bind("~C-~S-~M-dead_abovedot", FuncRequest(LFUN_DOT));
418 kbmap->bind("~C-~S-~M-dead_grave", FuncRequest(LFUN_GRAVE));
419 kbmap->bind("~C-~S-~M-dead_doubleacute", FuncRequest(LFUN_HUNG_UMLAUT));
420 kbmap->bind("~C-~S-~M-dead_macron", FuncRequest(LFUN_MACRON));
421 // nothing with this name
422 // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
423 kbmap->bind("~C-~S-~M-dead_tilde", FuncRequest(LFUN_TILDE));
424 kbmap->bind("~C-~S-~M-dead_diaeresis", FuncRequest(LFUN_UMLAUT));
425 // nothing with this name either...
426 //kbmap->bind("~C-~S-~M-dead_underbar", FuncRequest(LFUN_UNDERBAR));
427 kbmap->bind("~C-~S-~M-dead_belowdot", FuncRequest(LFUN_UNDERDOT));
428 kbmap->bind("~C-~S-~M-dead_tie", FuncRequest(LFUN_TIE));
429 kbmap->bind("~C-~S-~M-dead_ogonek",FuncRequest(LFUN_OGONEK));
433 void LyX::queryUserLyXDir(bool explicit_userdir)
435 string const configure_script = AddName(system_lyxdir(), "configure");
437 // Does user directory exist?
438 FileInfo fileInfo(user_lyxdir());
439 if (fileInfo.isOK() && fileInfo.isDir()) {
441 FileInfo script(configure_script);
442 FileInfo defaults(AddName(user_lyxdir(), "lyxrc.defaults"));
443 if (defaults.isOK() && script.isOK()
444 && defaults.getModificationTime() < script.getModificationTime()) {
445 lyxerr << _("LyX: reconfiguring user directory")
447 Path p(user_lyxdir());
448 ::system(configure_script.c_str());
449 lyxerr << "LyX: " << _("Done!") << endl;
454 first_start = !explicit_userdir;
456 lyxerr << bformat(_("LyX: Creating directory %1$s"
457 " and running configure..."), user_lyxdir()) << endl;
459 if (!createDirectory(user_lyxdir(), 0755)) {
460 // Failed, let's use $HOME instead.
461 user_lyxdir(GetEnvPath("HOME"));
462 lyxerr << bformat(_("Failed. Will use %1$s instead."),
463 user_lyxdir()) << endl;
467 // Run configure in user lyx directory
468 Path p(user_lyxdir());
469 ::system(configure_script.c_str());
470 lyxerr << "LyX: " << _("Done!") << endl;
474 void LyX::readRcFile(string const & name)
476 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
478 string const lyxrc_path = LibFileSearch(string(), name);
479 if (!lyxrc_path.empty()) {
481 lyxerr[Debug::INIT] << "Found " << name
482 << " in " << lyxrc_path << endl;
484 if (lyxrc.read(lyxrc_path) >= 0)
492 // Read the ui file `name'
493 void LyX::readUIFile(string const & name)
503 struct keyword_item uitags[ui_last - 1] = {
504 { "include", ui_include },
505 { "menuset", ui_menuset },
506 { "toolbar", ui_toolbar },
507 { "toolbars", ui_toolbars }
510 // Ensure that a file is read only once (prevents include loops)
511 static std::list<string> uifiles;
512 std::list<string>::const_iterator it = uifiles.begin();
513 std::list<string>::const_iterator end = uifiles.end();
514 it = std::find(it, end, name);
516 lyxerr[Debug::INIT] << "UI file '" << name
517 << "' has been read already. "
518 << "Is this an include loop?"
523 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
525 string const ui_path = LibFileSearch("ui", name, "ui");
527 if (ui_path.empty()) {
528 lyxerr[Debug::INIT] << "Could not find " << name << endl;
532 uifiles.push_back(name);
534 lyxerr[Debug::INIT] << "Found " << name
535 << " in " << ui_path << endl;
536 LyXLex lex(uitags, ui_last - 1);
537 lex.setFile(ui_path);
539 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
543 if (lyxerr.debugging(Debug::PARSER))
544 lex.printTable(lyxerr);
550 string const file = lex.getString();
555 menubackend.read(lex);
559 toolbarbackend.read(lex);
563 toolbarbackend.readToolbars(lex);
567 if (!rtrim(lex.getString()).empty())
568 lex.printError("LyX::ReadUIFile: "
569 "Unknown menu tag: `$$Token'");
576 // Read the languages file `name'
577 void LyX::readLanguagesFile(string const & name)
579 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
581 string const lang_path = LibFileSearch(string(), name);
582 if (lang_path.empty()) {
586 languages.read(lang_path);
590 // Read the encodings file `name'
591 void LyX::readEncodingsFile(string const & name)
593 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
595 string const enc_path = LibFileSearch(string(), name);
596 if (enc_path.empty()) {
600 encodings.read(enc_path);
609 /// return the the number of arguments consumed
610 typedef boost::function<int(string const &, string const &)> cmd_helper;
612 int parse_dbg(string const & arg, string const &)
615 lyxerr << _("List of supported debug flags:") << endl;
616 Debug::showTags(lyxerr);
619 lyxerr << bformat(_("Setting debug level to %1$s"), arg) << endl;
621 lyxerr.level(Debug::value(arg));
622 Debug::showLevel(lyxerr, lyxerr.level());
627 int parse_help(string const &, string const &)
630 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
631 "Command line switches (case sensitive):\n"
632 "\t-help summarize LyX usage\n"
633 "\t-userdir dir try to set user directory to dir\n"
634 "\t-sysdir dir try to set system directory to dir\n"
635 "\t-geometry WxH+X+Y set geometry of the main window\n"
636 "\t-dbg feature[,feature]...\n"
637 " select the features to debug.\n"
638 " Type `lyx -dbg' to see the list of features\n"
639 "\t-x [--execute] command\n"
640 " where command is a lyx command.\n"
641 "\t-e [--export] fmt\n"
642 " where fmt is the export format of choice.\n"
643 "\t-i [--import] fmt file.xxx\n"
644 " where fmt is the import format of choice\n"
645 " and file.xxx is the file to be imported.\n"
646 "\t-version summarize version and build info\n"
647 "Check the LyX man page for more details.") << endl;
652 int parse_version(string const &, string const &)
654 lyxerr << "LyX " << lyx_version
655 << " of " << lyx_release_date << endl;
656 lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
658 lyxerr << lyx_version_info << endl;
663 int parse_sysdir(string const & arg, string const &)
666 lyxerr << _("Missing directory for -sysdir switch") << endl;
673 int parse_userdir(string const & arg, string const &)
676 lyxerr << _("Missing directory for -userdir switch") << endl;
683 int parse_execute(string const & arg, string const &)
686 lyxerr << _("Missing command string after --execute switch") << endl;
690 // Argh. Setting gui to false segfaults..
691 // FIXME: when ? how ?
696 int parse_export(string const & type, string const &)
699 lyxerr << _("Missing file type [eg latex, ps...] after "
700 "--export switch") << endl;
703 batch = "buffer-export " + type;
708 int parse_import(string const & type, string const & file)
711 lyxerr << _("Missing file type [eg latex, ps...] after "
712 "--import switch") << endl;
716 lyxerr << _("Missing filename for --import") << endl;
720 batch = "buffer-import " + type + ' ' + file;
727 bool LyX::easyParse(int & argc, char * argv[])
729 std::map<string, cmd_helper> cmdmap;
731 cmdmap["-dbg"] = parse_dbg;
732 cmdmap["-help"] = parse_help;
733 cmdmap["--help"] = parse_help;
734 cmdmap["-version"] = parse_version;
735 cmdmap["--version"] = parse_version;
736 cmdmap["-sysdir"] = parse_sysdir;
737 cmdmap["-userdir"] = parse_userdir;
738 cmdmap["-x"] = parse_execute;
739 cmdmap["--execute"] = parse_execute;
740 cmdmap["-e"] = parse_export;
741 cmdmap["--export"] = parse_export;
742 cmdmap["-i"] = parse_import;
743 cmdmap["--import"] = parse_import;
745 for (int i = 1; i < argc; ++i) {
746 std::map<string, cmd_helper>::const_iterator it
747 = cmdmap.find(argv[i]);
749 // don't complain if not found - may be parsed later
750 if (it == cmdmap.end())
753 string arg((i + 1 < argc) ? argv[i + 1] : "");
754 string arg2((i + 2 < argc) ? argv[i + 2] : "");
756 int const remove = 1 + it->second(arg, arg2);
758 // Now, remove used arguments by shifting
759 // the following ones remove places down.
761 for (int j = i; j < argc; ++j)
762 argv[j] = argv[j + remove];
766 batch_command = batch;